Coverage Report

Created: 2026-10-31 06:59

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/brunsli/c/tests/test_utils.cc
Line
Count
Source
1
// Copyright (c) Google LLC 2019
2
//
3
// Use of this source code is governed by an MIT-style
4
// license that can be found in the LICENSE file or at
5
// https://opensource.org/licenses/MIT.
6
7
#include "./test_utils.h"
8
9
#include <cstddef>
10
#include <cstdint>
11
#include <cstdlib>
12
#include <fstream>
13
#include <ios>
14
#include <iterator>
15
#include <string>
16
#include <tuple>
17
#include <vector>
18
19
#if !defined(TEST_DATA_PATH)
20
#include <memory>
21
22
#include "tools/cpp/runfiles/runfiles.h"
23
#endif
24
25
namespace brunsli {
26
27
0
size_t StringOutputFunction(void* data, const uint8_t* buf, size_t count) {
28
0
  std::string* output = reinterpret_cast<std::string*>(data);
29
0
  output->append(reinterpret_cast<const char*>(buf), count);
30
0
  return count;
31
0
}
32
33
// clang-format off
34
static const uint8_t kSmallBrunsliFile[] = {
35
  /* Signature */
36
  0x0a, 0x04,
37
        0x42, 0xd2, 0xd5, 0x4e,
38
39
  /* Header */
40
  0x12, 0x08,
41
        0x08, 0x10, 0x10, 0x10, 0x18, 0x02, 0x20, 0x00,
42
43
  /* Internal */
44
  0x22, 0xa8, 0x00,
45
        0xe0, 0xb6, 0x01, 0x04, 0x41, 0x10, 0x5a, 0x06,
46
        0xc5, 0x3a, 0xac, 0x18, 0xdd, 0x4c, 0x55, 0x67,
47
        0xb5, 0x4f, 0xe9, 0x91, 0xe4, 0xbe, 0x6f, 0x19,
48
        0x69, 0x37, 0x53, 0x35, 0xe3, 0x3a, 0x6e, 0x06,  // 020
49
        0xf8, 0x01, 0x04, 0x2a, 0x8b, 0x08, 0x01, 0x00,
50
51
  /* Metadata */
52
  0x1a, 0x06,
53
        0x01,
54
        0x03, 0x00, 0x80, 0x3a, 0x03,
55
56
  /* Quant */
57
  0x2a, 0x82, 0x00,
58
        0x29, 0x53,
59
60
  /* Histo */
61
  0x32, 0xb8, 0x80, 0x00,
62
        0x00, 0x0e, 0x0b, 0xf4, 0x56, 0xcd, 0x01, 0x90,
63
        0x00, 0x09, 0xca, 0x8f, 0xd6, 0x18, 0x12, 0x9b,
64
        0x76, 0x27, 0xfd, 0x70, 0x2c, 0xd7, 0xb3, 0x6d,
65
        0xdc, 0xf7, 0x1e, 0x8f, 0x80, 0xa1, 0x17, 0x92,  // 020
66
        0xfe, 0x14, 0xa0, 0x7e, 0x79, 0x2f, 0xef, 0xf5,
67
        0xbd, 0x62, 0x32, 0x9c, 0xa0, 0xef, 0x33, 0xf0,
68
        0x1e, 0xcf, 0x02, 0xdb, 0x56, 0x5f, 0x0d, 0x2a,
69
70
  /* Unknown */
71
  0x7a, 0x15,
72
        'k', 'o', 't', ' ', 'l', 'o', 'm',
73
        'o', 'm', ' ', 'k', 'o', 'l', 'o',
74
        'l', ' ', 's', 'l', 'o', 'n', 'a',
75
76
  /* DC */
77
  0x3a, 0x8e, 0x80, 0x00,
78
        0x13, 0x00, 0x00, 0x00, 0x3a, 0x40, 0x97, 0xa7,
79
        0xcf, 0xf5, 0x00, 0x00, 0xff, 0xff,
80
81
  /* AC */
82
  0x42, 0xc2, 0x82, 0x00,
83
        0xf3, 0x4c, 0xc4, 0xdc, 0x00, 0xd0, 0x5d, 0xfe,
84
        0xf9, 0x3e, 0xc1, 0x15, 0xec, 0x84, 0xad, 0xdf,
85
        0x0e, 0x04, 0x59, 0xdb, 0x81, 0x2a, 0x43, 0x3d,
86
        0x95, 0xc1, 0x2a, 0x94, 0x57, 0x8d, 0x67, 0xea,  // 020
87
        0x85, 0x0f, 0x73, 0xb8, 0x9d, 0x0a, 0xa8, 0x26,
88
        0xad, 0x7a, 0xa3, 0x55, 0xa4, 0x53, 0x29, 0xa9,
89
        0xf0, 0x7b, 0x40, 0xe8, 0x32, 0x77, 0x05, 0x48,
90
        0xf2, 0x51, 0x4f, 0x05, 0x0a, 0xa5, 0xb9, 0xc5,  // 040
91
        0x2b, 0x5e, 0x0c, 0xce, 0xe2, 0x0a, 0x6b, 0x6d,
92
        0x37, 0x93, 0x33, 0x75, 0x43, 0x98, 0x5d, 0xe6,
93
        0x54, 0x93, 0x2f, 0x5f, 0x11, 0x97, 0x94, 0x54,
94
        0xed, 0xff, 0x9b, 0x5d, 0x20, 0x28, 0x47, 0xe4,  // 060
95
        0x77, 0x9d, 0x14, 0x81, 0x66, 0x04, 0xb6, 0x72,
96
        0x05, 0xe0, 0x54, 0x10, 0x4c, 0x2f, 0x51, 0x72,
97
        0x9e, 0x4b, 0xa3, 0x3c, 0x27, 0x1f, 0x14, 0x20,
98
        0x5e, 0xb7, 0x87, 0x34, 0xa7, 0xc8, 0x90, 0xc3,  // 080
99
        0x2f, 0x80, 0x41, 0x0f, 0xd0, 0xa9, 0x8e, 0x72,
100
        0xf7, 0x09, 0x85, 0x67, 0xd2, 0xa0, 0xad, 0x3a,
101
        0xf4, 0x28, 0x47, 0x31, 0x36, 0xe4, 0xbe, 0xa3,
102
        0x73, 0x85, 0x55, 0xc8, 0x16, 0x98, 0x0e, 0xb3,  // 0A0
103
        0xd0, 0xcd, 0x00, 0x54, 0x93, 0x8f, 0x35, 0xf3,
104
        0xe9, 0x6b, 0xac, 0xde, 0xd6, 0x1f, 0x64, 0x61,
105
        0x18, 0xf7, 0x6d, 0x9c, 0x2b, 0xc8, 0x7e, 0xc0,
106
        0xa3, 0xa1, 0x87, 0xa2, 0x02, 0x06, 0x71, 0xf5,  // 0C0
107
        0x9b, 0xd4, 0xec, 0xdd, 0x6b, 0xef, 0x99, 0x3a,
108
        0x9c, 0xa2, 0xaa, 0x49, 0xc6, 0x46, 0x66, 0xa5,
109
        0xb8, 0x1f, 0x4a, 0x2a, 0xf5, 0xdb, 0x10, 0xfa,
110
        0x17, 0x29, 0x27, 0x50, 0xe5, 0xf8, 0x34, 0x4d,  // 0E0
111
        0xc2, 0x95, 0x9c, 0x15, 0xa9, 0x71, 0xea, 0xda,
112
        0x6a, 0xf4, 0x31, 0x93, 0x4c, 0x1d, 0x52, 0x98,
113
        0x09, 0xc7, 0xd5, 0x24, 0x11, 0x63, 0x0a, 0x2f,
114
        0x25, 0x15, 0xfb, 0x2f, 0x08, 0x4a, 0x56, 0xed,  // 100
115
        0x27, 0x93, 0x08, 0x5c, 0x3a, 0x6d, 0x79, 0x78,
116
        0x94, 0xdc, 0xe3, 0xbf, 0x4a, 0xc0, 0x06, 0xcd,
117
        0x46, 0xb4, 0x12, 0x17, 0x20, 0x58, 0x21, 0x1a,
118
        0xa7, 0x52, 0xb9, 0x66, 0x08, 0x80, 0x9d, 0xcf,  // 120
119
        0x5d, 0x9b, 0xcd, 0x05, 0xc9, 0x6f, 0x51, 0x8c,
120
        0x62, 0xe9, 0xef, 0x28, 0xdb, 0xec, 0x4b, 0x61,
121
        0x04, 0x15, 0x9f, 0xf5, 0xfc, 0x52, 0x9b, 0xdb,
122
        0xaf, 0xde, 0x0a, 0x94, 0xc0, 0x7e, 0x84, 0x52,  // 140
123
        0xc6, 0x00
124
};
125
126
static const uint8_t kFallbackBrunsliFile[] = {
127
  /* Signature */
128
  0x0a, 0x04,
129
        0x42, 0xd2, 0xd5, 0x4e,
130
131
  /* Header */
132
  0x12, 0x02,
133
        0x18, 0x04,
134
135
  /* Internal */
136
  0x4a, 0x04,
137
        0xde, 0xad, 0xbe, 0xef
138
};
139
// clang-format on
140
141
0
std::vector<uint8_t> GetSmallBrunsliFile() {
142
0
  return std::vector<uint8_t>(kSmallBrunsliFile,
143
0
                              kSmallBrunsliFile + sizeof(kSmallBrunsliFile));
144
0
}
145
146
0
std::vector<uint8_t> GetFallbackBrunsliFile() {
147
0
  return std::vector<uint8_t>(
148
0
      kFallbackBrunsliFile,
149
0
      kFallbackBrunsliFile + sizeof(kFallbackBrunsliFile));
150
0
}
151
152
namespace {
153
0
uint32_t readU32(const uint8_t* data) {
154
0
  return data[3] | (data[2] << 8) | (data[1] << 16) | (data[0] << 24);
155
0
}
156
157
#if defined(TEST_DATA_PATH)
158
0
std::string GetTestDataPath(const std::string& filename) {
159
0
  return std::string(TEST_DATA_PATH "/") + filename;
160
0
}
161
#else
162
using bazel::tools::cpp::runfiles::Runfiles;
163
const std::unique_ptr<Runfiles> kRunfiles(Runfiles::Create(""));
164
std::string GetTestDataPath(const std::string& filename) {
165
  std::string root(BRUNSLI_ROOT_PACKAGE "/tests/testdata/");
166
  return kRunfiles->Rlocation(root + filename);
167
}
168
#endif
169
}  // namespace
170
171
std::vector<std::tuple<std::vector<uint8_t>>> ParseMar(const void* data,
172
0
                                                       size_t size) {
173
0
  std::vector<std::tuple<std::vector<uint8_t>>> result;
174
0
  const uint8_t* bytes = reinterpret_cast<const uint8_t*>(data);
175
0
  if (size < 8) std::abort();
176
0
  uint32_t sig = readU32(bytes);
177
0
  if (sig != 0x4D415231) std::abort();
178
0
  uint32_t index = readU32(bytes + 4);
179
0
  if ((index + 4 < index) || (index + 4 > size)) std::abort();
180
0
  uint32_t index_size = readU32(bytes + index);
181
0
  index += 4;
182
0
  if (index + index_size < index) std::abort();
183
0
  uint32_t index_end = index + index_size;
184
0
  if (index_end > size) std::abort();
185
0
  while (index < index_end) {
186
0
    if ((index + 13 < index) || (index + 13 > index_end)) std::abort();
187
0
    uint32_t offset = readU32(bytes + index);
188
0
    uint32_t len = readU32(bytes + index + 4);
189
0
    if (offset + len < offset || offset + len > size) std::abort();
190
0
    index += 12;
191
0
    while (bytes[index++]) {
192
0
      if (index == index_end) std::abort();
193
0
    }
194
0
    const uint8_t* start = bytes + offset;
195
0
    result.emplace_back(std::vector<uint8_t>(start, start + len));
196
0
  }
197
0
  return result;
198
0
}
199
200
0
std::vector<uint8_t> ReadTestData(const std::string& filename) {
201
0
  std::string full_path = GetTestDataPath(filename);
202
0
  std::ifstream file(full_path, std::ios::binary);
203
0
  std::vector<char> str((std::istreambuf_iterator<char>(file)),
204
0
                        std::istreambuf_iterator<char>());
205
0
  if (!file.good()) std::abort();
206
0
  const uint8_t* raw = reinterpret_cast<const uint8_t*>(str.data());
207
0
  std::vector<uint8_t> data(raw, raw + str.size());
208
0
  return data;
209
0
}
210
211
}  // namespace brunsli