• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Open-Sn / opensn / #5

02 Apr 2026 01:04PM UTC coverage: 74.858% (-1.2%) from 76.085%
#5

push

web-flow
Merge pull request #1009 from andrsd/unit-tests

Removing `opensn-test`

20988 of 28037 relevant lines covered (74.86%)

65922489.9 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

48.98
/framework/data_types/byte_array.h
1
// SPDX-FileCopyrightText: 2024 The OpenSn Authors <https://open-sn.github.io/opensn/>
2
// SPDX-License-Identifier: MIT
3

4
#pragma once
5

6
#include <vector>
7
#include <stdexcept>
8
#include <string>
9
#include <cstring>
10
#include <cstddef>
11

12
namespace opensn
13
{
14

15
class ByteArray
3,537,670✔
16
{
17
public:
18
  ByteArray() = default;
19
  explicit ByteArray(const size_t raw_data_size) : raw_data_(raw_data_size) {}
20
  explicit ByteArray(std::vector<std::byte>&& raw_data) : raw_data_(std::move(raw_data)) {}
21
  explicit ByteArray(const std::vector<std::byte>& raw_data) : raw_data_(raw_data) {}
22

23
  /**
24
   * Uses the template type T to convert an associated value (of type T)
25
   * to a sub-array of std::bytes and adds it to the internal byte-array.
26
   *
27
   * The template type T must support sizeof.
28
   */
29
  template <typename T>
30
  void Write(const T& value)
147,503,243✔
31
  {
32
    const size_t num_bytes = sizeof(T);
147,503,243✔
33
    std::vector<std::byte> dest(num_bytes);
147,503,243✔
34
    std::memcpy(dest.data(), &value, num_bytes);
147,503,243✔
35
    raw_data_.insert(raw_data_.end(), dest.data(), dest.data() + num_bytes);
147,503,243✔
36
  }
147,503,243✔
37

38
  /**
39
   * Uses the template type `T` to convert `sizeof(T)` number of bytes to
40
   * a value of type `T`. The bytes are pulled from the internal byte-array
41
   * starting at the internal address `m_offset` which can be viewed with a
42
   * call to `Offset()`. This offset is incremented by the amount of bytes
43
   * used and can be reset with a call to `Seek(0)`.
44
   *
45
   * Bounds-checking is performed by checking if the internal byte array
46
   * has the required number of bytes available. If this check fails then
47
   * this call will return an `out_of_range` exception.
48
   */
49
  template <typename T>
50
  T Read()
51
  {
52
    const size_t num_bytes = sizeof(T);
53
    if ((offset_ + num_bytes - 1) >= raw_data_.size())
54
      throw std::out_of_range(
55
        std::string("ByteArray reading error. ") + " Typename: " + std::string(typeid(T).name()) +
56
        " m_offset: " + std::to_string(offset_) + " size: " + std::to_string(raw_data_.size()) +
57
        " num_bytes to read: " + std::to_string(num_bytes));
58

59
    T value;
60
    std::memcpy(&value, &raw_data_[offset_], num_bytes);
61
    offset_ += num_bytes;
62

63
    return value;
64
  }
65

66
  /**
67
   * Uses the template type T to convert sizeof(T) number of bytes to
68
   * a value of type T. The bytes are pulled from the internal byte-array
69
   * starting at the internal address specified by the argument "address".
70
   * An optional argument next_address can be used to return the location
71
   * of the value after the value read.
72
   * This offset is the given "address" argument incremented by the amount
73
   * of bytes used.
74
   *
75
   * Bounds-checking is performed by checking if the internal byte array
76
   * has the required number of bytes available. If this check fails then
77
   * this call will return an `out_of_range` exception.
78
   */
79
  template <typename T>
80
  T Read(const size_t address, size_t* next_address = nullptr) const
×
81
  {
82
    const size_t num_bytes = sizeof(T);
×
83
    if ((address + num_bytes - 1) >= raw_data_.size())
×
84
      throw std::logic_error(
×
85
        std::string("ByteArray reading error. ") + " Typename: " + std::string(typeid(T).name()) +
×
86
        " address: " + std::to_string(address) + " size: " + std::to_string(raw_data_.size()) +
×
87
        " num_bytes to read: " + std::to_string(num_bytes));
×
88

89
    T value;
90
    std::memcpy(&value, &raw_data_[address], num_bytes);
×
91
    if (next_address != nullptr)
×
92
      *next_address = address + num_bytes;
×
93

94
    return value;
×
95
  }
96

97
  /// Appends a `ByteArray` to the current internal byte array.
98
  void Append(const ByteArray& other_raw)
×
99
  {
100
    auto& master = raw_data_;
×
101

102
    const auto& slave = other_raw.Data();
×
103
    master.insert(master.end(), slave.begin(), slave.end());
×
104
  }
×
105

106
  /// Appends bytes from a `std::vector<std::byte>` to the internal byte array.
107
  void Append(const std::vector<std::byte>& other_raw)
108
  {
109
    auto& master = raw_data_;
110

111
    const auto& slave = other_raw;
112
    master.insert(master.end(), slave.begin(), slave.end());
113
  }
114

115
  /// Clears the internal byte array and resets the address offset to zero.
116
  void Clear()
117
  {
118
    raw_data_.clear();
119
    offset_ = 0;
120
  }
121

122
  /// Moves the address marker to the supplied address.
123
  void Seek(const size_t address = 0) { offset_ = address; }
124

125
  /// Returns the internal address marker position.
126
  size_t Offset() const { return offset_; }
127

128
  /// Determines if the internal address marker is beyond the internal byte array.
129
  bool EndOfBuffer() const { return offset_ >= raw_data_.size(); }
130

131
  /// Returns the current size of the internal byte array.
132
  size_t Size() const { return raw_data_.size(); }
133

134
  /// Returns a reference to the internal byte array.
135
  std::vector<std::byte>& Data() { return raw_data_; }
136

137
  /// Returns a const reference of the internal byte array.
138
  const std::vector<std::byte>& Data() const { return raw_data_; }
1,120✔
139

1,201,544✔
140
protected:
1,201,550✔
141
  std::vector<std::byte> raw_data_;
146,301,502✔
142
  size_t offset_ = 0;
146,301,502✔
143
};
146,301,502✔
144

×
145
} // namespace opensn
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc