1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
|
#include <forstio/test/suite.hpp>
#include "../c++/abstract/forst.hpp"
#include "../c++/forst_chunk.hpp"
#include <cstring>
#include <string>
namespace {
namespace sch {
using namespace kel::lbm::sch;
}
using namespace kel::lbm;
template <typename Schema>
error_or<void> round_trip(const data<Schema> &from, data<Schema> &to){
codec<Schema, enc::Forst> c;
data<Schema, enc::Forst> bytes;
auto eov = c.encode(from, bytes);
if(eov.is_error()){
return eov;
}
return c.decode(bytes, to);
}
template <typename Schema>
uint64_t encoded_size(const data<Schema> &from){
codec<Schema, enc::Forst> c;
data<Schema, enc::Forst> bytes;
auto eov = c.encode(from, bytes);
(void) eov;
return bytes.size().get();
}
SAW_TEST("Forst / layout traits match the declared table"){
using namespace kel::lbm::impl;
static_assert(forst_info<sch::UInt8>::static_size == 1u, "u8");
static_assert(forst_info<sch::Float32>::static_size == 4u, "f32");
static_assert(forst_info<sch::Float64>::static_size == 8u, "f64");
static_assert(forst_info<sch::UInt8>::layers == 0u, "primitives are fixed");
static_assert(forst_info<sch::String>::static_size == 16u, "length + offset");
static_assert(forst_info<sch::String>::layers == 1u, "a string is dynamic");
static_assert(forst_info<sch::Array<sch::UInt64, 1u>>::static_size == 16u,
"one extent + offset");
static_assert(forst_info<sch::Array<sch::UInt64, 2u>>::static_size == 24u,
"two extents + offset");
static_assert(forst_info<sch::FixedArray<sch::Float32, 9u>>::static_size == 36u,
"a fixed array is its elements inline");
static_assert(forst_info<sch::FixedArray<sch::Float32, 9u>>::layers == 0u,
"and stays fixed");
SAW_EXPECT(true, "static asserts carry this one");
}
/**
* The property the whole design rests on: an array of a dynamic element needs
* a second layer, and must therefore be rejected rather than mis-encoded.
*/
SAW_TEST("Forst / nesting depth is counted, so two layers are detectable"){
using namespace kel::lbm::impl;
static_assert(forst_info<sch::Array<sch::String, 1u>>::layers == 2u,
"Array<String> is two layers and must not pass the codec's assert");
static_assert(
forst_info<sch::Array<sch::Array<sch::UInt64,1u>, 1u>>::layers == 2u,
"Array<Array<...>> likewise");
using Flat = sch::Struct<
sch::Member<sch::String, "a">,
sch::Member<sch::Array<sch::UInt64, 1u>, "b">
>;
static_assert(forst_info<Flat>::layers == 1u,
"Several dynamic members side by side are still one layer");
// And the thing the result manifest depends on: an array of a *fixed*
// struct stays at one layer.
using FixedElem = sch::Struct<
sch::Member<sch::FixedArray<sch::UInt8, 32u>, "name">,
sch::Member<sch::UInt64, "bytes">
>;
static_assert(forst_info<FixedElem>::layers == 0u, "fixed struct");
static_assert(forst_info<sch::Array<FixedElem, 1u>>::layers == 1u,
"an array of fixed structs is what keeps the manifest flat");
SAW_EXPECT(true, "static asserts carry this one");
}
SAW_TEST("Forst / primitives round trip exactly"){
{
data<sch::Float32> from; from.set(-1.5f);
data<sch::Float32> to;
SAW_EXPECT_EOV(round_trip(from, to), "f32");
SAW_EXPECT(to.get() == -1.5f, "f32 survives bit for bit");
}
{
data<sch::Float64> from; from.set(1.0 / 3.0);
data<sch::Float64> to;
SAW_EXPECT_EOV(round_trip(from, to), "f64");
SAW_EXPECT(to.get() == 1.0 / 3.0, "f64 survives bit for bit");
}
{
// The edges, where a width or sign mistake would show.
data<sch::UInt64> from; from.set(~uint64_t{0u});
data<sch::UInt64> to;
SAW_EXPECT_EOV(round_trip(from, to), "u64");
SAW_EXPECT(to.get() == ~uint64_t{0u}, "u64 max survives");
}
{
data<sch::Int32> from; from.set(-2147483647 - 1);
data<sch::Int32> to;
SAW_EXPECT_EOV(round_trip(from, to), "i32");
SAW_EXPECT(to.get() == -2147483647 - 1, "i32 min survives");
}
{
data<sch::Bool> from; from.set(true);
data<sch::Bool> to;
SAW_EXPECT_EOV(round_trip(from, to), "bool");
SAW_EXPECT(to.get(), "bool survives");
SAW_EXPECT(encoded_size(from) == 1u, "and costs one byte");
}
}
SAW_TEST("Forst / strings round trip, including the empty one"){
{
data<sch::String> from{std::string{"velocity"}};
data<sch::String> to;
SAW_EXPECT_EOV(round_trip(from, to), "string");
SAW_EXPECT(to.stl_view() == "velocity", "contents survive");
SAW_EXPECT(encoded_size(from) == 16u + 8u, "slot plus contents");
}
{
data<sch::String> from{std::string{""}};
data<sch::String> to{std::string{"not empty"}};
SAW_EXPECT_EOV(round_trip(from, to), "empty string");
SAW_EXPECT(to.stl_view().empty(), "an empty string stays empty");
SAW_EXPECT(encoded_size(from) == 16u, "and costs only its slot");
}
{
// Embedded NULs and high bytes: this is a byte string, not C text.
std::string raw{"a\0b\xff", 4u};
data<sch::String> from{raw};
data<sch::String> to;
SAW_EXPECT_EOV(round_trip(from, to), "binary string");
SAW_EXPECT(to.stl_string() == raw, "NULs and high bytes survive");
}
}
SAW_TEST("Forst / several dynamic members share the one layer"){
using Schema = sch::Struct<
sch::Member<sch::String, "first">,
sch::Member<sch::UInt32, "middle">,
sch::Member<sch::String, "last">
>;
data<Schema> from;
from.get<"first">().set(std::string{"alpha"});
from.get<"middle">().set(42u);
from.get<"last">().set(std::string{"omega"});
data<Schema> to;
SAW_EXPECT_EOV(round_trip(from, to), "struct with two strings");
SAW_EXPECT(to.get<"first">().stl_view() == "alpha", "first survives");
SAW_EXPECT(to.get<"middle">().get() == 42u, "the fixed member between them survives");
SAW_EXPECT(to.get<"last">().stl_view() == "omega", "and so does the second string");
// 16 + 4 + 16 static, then 5 + 5 of contents.
SAW_EXPECT(encoded_size(from) == 36u + 10u, "no padding anywhere");
}
SAW_TEST("Forst / an array of fixed structs round trips"){
using Elem = sch::Struct<
sch::Member<sch::UInt32, "id">,
sch::Member<sch::Float32, "value">
>;
using Schema = sch::Struct<sch::Member<sch::Array<Elem, 1u>, "items">>;
data<Schema> from;
for(uint32_t i = 0u; i < 4u; ++i){
data<Elem> elem;
elem.get<"id">().set(i);
elem.get<"value">().set(static_cast<float>(i) * 0.5f);
auto eov = from.get<"items">().add(std::move(elem));
SAW_EXPECT_EOV(eov, "array grows");
}
data<Schema> to;
SAW_EXPECT_EOV(round_trip(from, to), "array of structs");
SAW_EXPECT(to.get<"items">().meta().flat_at(0u).get() == 4u,
"the extent survives");
for(uint64_t i = 0u; i < 4u; ++i){
SAW_EXPECT(to.get<"items">().at({{i}}).get<"id">().get() == i,
"element ids survive");
SAW_EXPECT(to.get<"items">().at({{i}}).get<"value">().get()
== static_cast<float>(i) * 0.5f, "element values survive");
}
// 16 static for the slot, 8 per element.
SAW_EXPECT(encoded_size(from) == 16u + 4u * 8u, "exact, unpadded");
}
/**
* The claim the whole encoding choice rests on: a struct of chunks is entirely
* fixed size, so its Forst form is the packed field data itself. If this ever
* stops holding, sending results costs a serialisation pass and a second
* buffer -- in a browser tab, possibly one that does not exist.
*/
SAW_TEST("Forst / a struct of chunks is exactly its raw bytes"){
constexpr uint64_t dim_x = 16u;
constexpr uint64_t dim_y = 8u;
using Velocity = sch::Chunk<sch::Vector<sch::Float32, 2u>, 0u, dim_x, dim_y>;
using Density = sch::Chunk<sch::Scalar<sch::Float32>, 0u, dim_x, dim_y>;
using Macro = sch::Struct<
sch::Member<Velocity, "velocity">,
sch::Member<Density, "density">
>;
// The standard-layout assumption the zero-copy path depends on.
static_assert(sizeof(data<sch::Vector<sch::Float32, 2u>>) == 2u * sizeof(float),
"A Vector must be its components with nothing around them");
static_assert(sizeof(data<sch::Scalar<sch::Float32>>) == sizeof(float),
"A Scalar must be its component with nothing around it");
static_assert(kel::lbm::impl::forst_info<Macro>::layers == 0u,
"A struct of chunks has no dynamic part at all");
constexpr uint64_t expected = dim_x * dim_y * (2u + 1u) * 4u;
static_assert(kel::lbm::impl::forst_info<Macro>::static_size == expected,
"and occupies exactly the field data");
data<Macro> from;
for(uint64_t y = 0u; y < dim_y; ++y){
for(uint64_t x = 0u; x < dim_x; ++x){
auto &vel = from.get<"velocity">().at({{x, y}});
vel.at({{0u}}).set(static_cast<float>(x));
vel.at({{1u}}).set(static_cast<float>(y));
from.get<"density">().at({{x, y}}).at({}).set(
static_cast<float>(x * dim_y + y));
}
}
codec<Macro, enc::Forst> c;
data<Macro, enc::Forst> bytes;
SAW_EXPECT_EOV(c.encode(from, bytes), "encode");
SAW_EXPECT(bytes.size().get() == expected,
"The encoding adds not one byte of framing");
// And the bytes are the buffer, not merely the same size as it.
const auto *velocity_raw = reinterpret_cast<const uint8_t *>(
from.get<"velocity">().flat_data());
SAW_EXPECT(std::memcmp(bytes.raw(), velocity_raw, dim_x * dim_y * 2u * 4u) == 0,
"The velocity chunk encodes to its own memory, byte for byte");
const auto *density_raw = reinterpret_cast<const uint8_t *>(
from.get<"density">().flat_data());
SAW_EXPECT(std::memcmp(bytes.raw() + dim_x * dim_y * 2u * 4u, density_raw,
dim_x * dim_y * 4u) == 0,
"and the density chunk follows it with no gap");
data<Macro> to;
SAW_EXPECT_EOV(c.decode(bytes, to), "decode");
SAW_EXPECT(to.get<"velocity">().at({{3u, 5u}}).at({{1u}}).get() == 5.0f,
"values come back where they went in");
SAW_EXPECT(to.get<"density">().at({{3u, 5u}}).at({}).get()
== static_cast<float>(3u * dim_y + 5u), "for every member");
}
SAW_TEST("Forst / a chunk's ghost ring is part of the encoding"){
// Ghost=1 widens each side by two, and flat_data covers the ring. A reader
// that assumed the logical extents would be short by the whole border.
using Df = sch::Chunk<sch::FixedArray<sch::Float32, 9u>, 1u, 8u, 4u>;
constexpr uint64_t stored = (8u + 2u) * (4u + 2u);
static_assert(kel::lbm::impl::forst_info<Df>::static_size == stored * 9u * 4u,
"The encoded size is the padded extents, not the logical ones");
// Wrapped in a Struct rather than encoded bare: chunk.hpp already
// specialises data<Chunk<...>, Encode> for any encoding, so a top level
// data<Chunk, enc::Forst> is ambiguous against it. enc::Json has the same
// clash. A field is always a member of a struct in practice.
using Field = sch::Struct<sch::Member<Df, "dfs">>;
data<Field> from;
from.get<"dfs">().at({{0u, 0u}}).at({3u}).set(7.0f);
data<Field> to;
SAW_EXPECT_EOV(round_trip(from, to), "df chunk");
SAW_EXPECT(to.get<"dfs">().at({{0u, 0u}}).at({3u}).get() == 7.0f,
"interior survives");
}
SAW_TEST("Forst / truncated input is rejected rather than read past"){
using Schema = sch::Struct<
sch::Member<sch::String, "name">,
sch::Member<sch::UInt64, "count">
>;
data<Schema> from;
from.get<"name">().set(std::string{"something"});
from.get<"count">().set(5u);
codec<Schema, enc::Forst> c;
data<Schema, enc::Forst> bytes;
SAW_EXPECT_EOV(c.encode(from, bytes), "encode");
// Cut the contents off and the string's length no longer fits the buffer.
auto truncated = bytes.bytes();
truncated.resize(truncated.size() - 4u);
data<Schema, enc::Forst> damaged{std::move(truncated)};
data<Schema> to;
SAW_EXPECT(c.decode(damaged, to).is_error(),
"A string reaching past the end must be an error, not a read");
// Cut below the static section and it must fail before indexing anything.
data<Schema, enc::Forst> stub{std::vector<uint8_t>(4u, 0u)};
SAW_EXPECT(c.decode(stub, to).is_error(),
"A buffer shorter than the static section must be rejected up front");
SAW_EXPECT(c.decode(data<Schema, enc::Forst>{}, to).is_error(),
"and so must an empty one");
}
SAW_TEST("Forst / a hostile offset cannot escape the buffer"){
using Schema = sch::Struct<sch::Member<sch::String, "name">>;
data<Schema> from;
from.get<"name">().set(std::string{"abc"});
codec<Schema, enc::Forst> c;
data<Schema, enc::Forst> bytes;
SAW_EXPECT_EOV(c.encode(from, bytes), "encode");
// A length near the top of the range must not wrap when added to a start
// that is itself inside the buffer.
{
auto tampered = bytes.bytes();
const uint64_t huge = ~uint64_t{0u} - 8u;
std::memcpy(tampered.data(), &huge, 8u);
data<Schema, enc::Forst> evil{std::move(tampered)};
data<Schema> to;
SAW_EXPECT(c.decode(evil, to).is_error(), "An absurd length is rejected");
}
// An offset pointing far outside likewise.
{
auto tampered = bytes.bytes();
const uint64_t far = ~uint64_t{0u} / 2u;
std::memcpy(tampered.data() + 8u, &far, 8u);
data<Schema, enc::Forst> evil{std::move(tampered)};
data<Schema> to;
SAW_EXPECT(c.decode(evil, to).is_error(), "An absurd offset is rejected");
}
}
}
|