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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
// Copyright (C) 2020 - 2022, J2 Innovations
//! Brio fixture tests derived from the Haxall/Fantom BrioWriter reference implementation.
//!
//! Each fixture was generated by running `fan/BrioGen.fan` against the Haxall Fantom runtime:
//! `fan fan/BrioGen.fan > /tmp/brio_fixtures.txt`
//!
//! The fixture label corresponds to the label used in that script.
//!
//! For each fixture we verify:
//! 1. **Decode**: parse the hex bytes → Haystack Value, compare to expected value.
//! 2. **Encode round-trip**: re-encode the decoded value → assert same hex.
//!
//! Notes on limitations:
//! - **DateTime with named timezones**: encoding requires `timezone-db` feature; without it,
//! all DateTimes collapse to UTC. Tests that depend on correct timezone round-tripping are
//! gated on `#[cfg(feature = "timezone-db")]`.
//! - **Dict key ordering**: Rust `BTreeMap` iterates alphabetically; Fantom uses insertion
//! order. For multi-key dicts the decoded Value is correct but the re-encoded hex may
//! differ in key order, so we only verify decode equality for those fixtures.
#[cfg(all(test, feature = "brio-decoding", feature = "brio-encoding"))]
mod tests {
use crate::dict;
use crate::encoding::brio::decode::from_brio;
use crate::encoding::brio::encode::ToBrio;
use crate::haystack::val::{
Column, Coord, Date, DateTime, Dict, GRID_FORMAT_VERSION, Grid, List, Number, Ref, Symbol,
Time, Uri, Value, XStr,
};
use crate::units::get_unit_or_default;
// -----------------------------------------------------------------------
// Helpers
// -----------------------------------------------------------------------
/// Decode a lower-case hex string into a `Vec<u8>`.
fn hex(s: &str) -> Vec<u8> {
(0..s.len())
.step_by(2)
.map(|i| u8::from_str_radix(&s[i..i + 2], 16).expect("valid hex"))
.collect()
}
/// Decode Brio bytes from a hex string into a Value.
fn decode(hex_str: &str) -> Value {
let bytes = hex(hex_str);
from_brio(&mut bytes.as_slice()).expect("decode")
}
/// Re-encode a value and return the bytes as a lower-case hex string.
fn encode(v: &Value) -> String {
let bytes = v.to_brio_vec().expect("encode");
bytes.iter().map(|b| format!("{b:02x}")).collect()
}
/// Full fixture assertion: decode hex -> assert value, then encode value -> assert hex.
///
/// Use this for types whose encoding is deterministic and matches Fantom exactly:
/// scalars, numbers, strings, refs, dates, times, single-key dicts, lists, coords.
fn assert_fixture(hex_str: &str, expected: &Value) {
let got = decode(hex_str);
assert_eq!(&got, expected, "decode mismatch for fixture {hex_str}");
let reencoded = encode(expected);
assert_eq!(
reencoded, hex_str,
"encode mismatch: expected {hex_str} but got {reencoded}"
);
}
/// Decode-only assertion: decode hex -> assert value equals expected.
///
/// Use for cases where re-encoding produces different (but equivalent) bytes,
/// e.g., multi-key dicts where key iteration order differs from Fantom.
fn assert_decode(hex_str: &str, expected: &Value) {
let got = decode(hex_str);
assert_eq!(&got, expected, "decode mismatch for fixture {hex_str}");
}
/// Round-trip assertion: decode hex -> re-encode -> assert same hex.
///
/// Use for fixtures where the round-trip is stable but the expected value
/// cannot be cheaply constructed in Rust (e.g. sub-second DateTimes).
fn assert_roundtrip(hex_str: &str) {
let got = decode(hex_str);
let reencoded = encode(&got);
assert_eq!(
reencoded, hex_str,
"round-trip mismatch: expected {hex_str} but got {reencoded}"
);
}
// -----------------------------------------------------------------------
// Scalar singletons
// -----------------------------------------------------------------------
#[test]
fn fixture_null() {
assert_fixture("00", &Value::Null);
}
#[test]
fn fixture_marker() {
assert_fixture("01", &Value::make_marker());
}
#[test]
fn fixture_na() {
assert_fixture("02", &Value::make_na());
}
#[test]
fn fixture_remove() {
assert_fixture("03", &Value::make_remove());
}
#[test]
fn fixture_false() {
assert_fixture("04", &Value::from(false));
}
#[test]
fn fixture_true() {
assert_fixture("05", &Value::from(true));
}
// -----------------------------------------------------------------------
// Number - I2 (no unit)
// -----------------------------------------------------------------------
#[test]
fn fixture_n_zero() {
assert_fixture("06000000", &Value::from(Number::make(0.0)));
}
#[test]
fn fixture_n_i2_42() {
assert_fixture("06002a00", &Value::from(Number::make(42.0)));
}
/// -1 fits in I2 (sign-extended 16-bit, varint 0x00 for no unit)
#[test]
fn fixture_n_i2_neg() {
assert_fixture("06ffff00", &Value::from(Number::make(-1.0)));
}
/// I2 maximum: 32767
#[test]
fn fixture_n_i2_max() {
assert_fixture("067fff00", &Value::from(Number::make(32767.0)));
}
/// I2 minimum representable: -32767
#[test]
fn fixture_n_i2_min() {
assert_fixture("06800100", &Value::from(Number::make(-32767.0)));
}
// -----------------------------------------------------------------------
// Number - I4 (no unit, value outside I2 range)
// -----------------------------------------------------------------------
/// First value that requires I4: 32768
#[test]
fn fixture_n_i4_pos() {
assert_fixture("070000800000", &Value::from(Number::make(32768.0)));
}
/// -32768 requires I4 (I2 min is -32767)
#[test]
fn fixture_n_i4_neg() {
assert_fixture("07ffff800000", &Value::from(Number::make(-32768.0)));
}
#[test]
fn fixture_n_i4_max() {
assert_fixture("077fffffff00", &Value::from(Number::make(2_147_483_647.0)));
}
#[test]
fn fixture_n_i4_min() {
assert_fixture("078000000000", &Value::from(Number::make(-2_147_483_648.0)));
}
// -----------------------------------------------------------------------
// Number - F8 (double, no unit)
// -----------------------------------------------------------------------
#[test]
fn fixture_n_f8_pi() {
let pi = std::f64::consts::PI;
assert_fixture("08400921fb54442d1800", &Value::from(Number::make(pi)));
}
/// 2^31 = 2147483648 - requires F8
#[test]
fn fixture_n_f8_big() {
assert_fixture(
"0841e000000000000000",
&Value::from(Number::make(2_147_483_648.0)),
);
}
/// -(2^31 + 1) - requires F8
#[test]
fn fixture_n_f8_bigneg() {
assert_fixture(
"08c1e000000020000000",
&Value::from(Number::make(-2_147_483_649.0)),
);
}
// -----------------------------------------------------------------------
// Number - with units (const table lookups)
// -----------------------------------------------------------------------
/// 98.6 degF - F8 encoding with const unit index
#[test]
fn fixture_n_degf() {
let v = Value::from(Number::make_with_unit(
98.6,
get_unit_or_default("\u{00b0}F"),
));
assert_fixture("084058a6666666666682da", &v);
}
/// 1500kW - I2 with const unit
#[test]
fn fixture_n_kw() {
let v = Value::from(Number::make_with_unit(1500.0, get_unit_or_default("kW")));
assert_fixture("0605dc8276", &v);
}
/// 99kWh - I2 with const unit
#[test]
fn fixture_n_kwh() {
let v = Value::from(Number::make_with_unit(99.0, get_unit_or_default("kWh")));
assert_fixture("060063827c", &v);
}
/// 22 degC - I2 with const unit
#[test]
fn fixture_n_degc() {
let v = Value::from(Number::make_with_unit(
22.0,
get_unit_or_default("\u{00b0}C"),
));
assert_fixture("06001682d7", &v);
}
/// 75% - I2 with const unit
#[test]
fn fixture_n_pct() {
let v = Value::from(Number::make_with_unit(75.0, get_unit_or_default("%")));
assert_fixture("06004b81c6", &v);
}
/// 400cfm - I2 with const unit
#[test]
fn fixture_n_cfm() {
let v = Value::from(Number::make_with_unit(400.0, get_unit_or_default("cfm")));
assert_fixture("0601908233", &v);
}
// -----------------------------------------------------------------------
// Str
// -----------------------------------------------------------------------
/// Empty string - const index 0
#[test]
fn fixture_str_empty() {
assert_fixture("0900", &Value::from(""));
}
/// "hello" - inline (not in const table)
#[test]
fn fixture_str_hello() {
assert_fixture("09ff0568656c6c6f", &Value::from("hello"));
}
/// "New_York" - const index 26 (single-byte varint 0x1a)
#[test]
fn fixture_str_ny() {
assert_fixture("091a", &Value::from("New_York"));
}
/// "siteRef" - const index 642 (two-byte varint 0x8182)
#[test]
fn fixture_str_siteref() {
assert_fixture("098182", &Value::from("siteRef"));
}
/// "dis" - const index 313 (two-byte varint 0x80b9)
#[test]
fn fixture_str_dis() {
assert_fixture("0980b9", &Value::from("dis"));
}
/// "cafe" with accent - inline CESU-8: e-acute = c3 a9
#[test]
fn fixture_str_cafe() {
assert_fixture("09ff04636166c3a9", &Value::from("caf\u{00e9}"));
}
/// "temp degF" with degree sign - inline CESU-8: degree = c2 b0
#[test]
fn fixture_str_degf_label() {
assert_fixture("09ff0774656d7020c2b046", &Value::from("temp \u{00b0}F"));
}
// -----------------------------------------------------------------------
// Uri
// -----------------------------------------------------------------------
#[test]
fn fixture_uri_http() {
assert_fixture(
"0cff13687474703a2f2f6578616d706c652e636f6d2f",
&Value::from(Uri::make("http://example.com/")),
);
}
#[test]
fn fixture_uri_path() {
assert_fixture("0cff05612f622f63", &Value::from(Uri::make("a/b/c")));
}
// -----------------------------------------------------------------------
// Ref
// -----------------------------------------------------------------------
/// I8 ref with no display string: id = "1deb31b8-7508b187"
#[test]
fn fixture_ref_i8_nodis() {
assert_fixture(
"0b1deb31b87508b18700",
&Value::from(Ref::make("1deb31b8-7508b187", None)),
);
}
/// I8 ref with display string "hi!": encodes as CTRL_REF_I8 + encodeStrChars
#[test]
fn fixture_ref_i8_dis() {
assert_fixture(
"0b1deb31b87508b18703686921",
&Value::from(Ref::make("1deb31b8-7508b187", Some("hi!"))),
);
}
/// "cafebabe-deadbeef" packs to 0xcafebabedeadbeef which is negative as i64,
/// so Fantom falls back to CTRL_REF_STR (0x0a) with display "Site delta".
#[test]
fn fixture_ref_i8_dis2() {
assert_fixture(
"0aff1163616665626162652d6465616462656566065369746520ce94",
&Value::from(Ref::make("cafebabe-deadbeef", Some("Site \u{0394}"))),
);
}
/// STR ref with no display string: id = "1debX1b8-7508b187"
#[test]
fn fixture_ref_str_nodis() {
assert_fixture(
"0aff1131646562583162382d373530386231383700",
&Value::from(Ref::make("1debX1b8-7508b187", None)),
);
}
/// STR ref with display string "My Equip": id = "custom.ref"
#[test]
fn fixture_ref_str_dis() {
assert_fixture(
"0aff0a637573746f6d2e726566084d79204571756970",
&Value::from(Ref::make("custom.ref", Some("My Equip"))),
);
}
// -----------------------------------------------------------------------
// Date
// -----------------------------------------------------------------------
#[test]
fn fixture_date_2015() {
let d = Date::from_ymd(2015, 11, 30).expect("date");
assert_fixture("0d07df0b1e", &Value::from(d));
}
#[test]
fn fixture_date_2000() {
let d = Date::from_ymd(2000, 1, 1).expect("date");
assert_fixture("0d07d00101", &Value::from(d));
}
#[test]
fn fixture_date_1970() {
let d = Date::from_ymd(1970, 1, 1).expect("date");
assert_fixture("0d07b20101", &Value::from(d));
}
#[test]
fn fixture_date_1950() {
let d = Date::from_ymd(1950, 6, 7).expect("date");
assert_fixture("0d079e0607", &Value::from(d));
}
#[test]
fn fixture_date_2099() {
let d = Date::from_ymd(2099, 12, 31).expect("date");
assert_fixture("0d08330c1f", &Value::from(d));
}
// -----------------------------------------------------------------------
// Time
//
// Brio encodes Time as milliseconds since midnight (i32 big-endian).
// -----------------------------------------------------------------------
#[test]
fn fixture_time_midnight() {
let t = Time::from_hms(0, 0, 0).expect("time");
assert_fixture("0e00000000", &Value::from(t));
}
/// noon = 12:00:00.000 -> 12*3600*1000 = 43_200_000 = 0x02932e00
#[test]
fn fixture_time_noon() {
let t = Time::from_hms_milli(12, 0, 0, 0).expect("time");
assert_fixture("0e02932e00", &Value::from(t));
}
/// 15:06:13.000 -> (15*3600 + 6*60 + 13)*1000 = 54_373_000 = 0x033daa88
#[test]
fn fixture_time_hms() {
let t = Time::from_hms_milli(15, 6, 13, 0).expect("time");
assert_fixture("0e033daa88", &Value::from(t));
}
/// 15:06:13.123 -> 54_373_123 = 0x033dab03
#[test]
fn fixture_time_ms() {
let t = Time::from_hms_milli(15, 6, 13, 123).expect("time");
assert_fixture("0e033dab03", &Value::from(t));
}
// -----------------------------------------------------------------------
// DateTime
//
// Strategy:
// - All fixtures verify the decoded unix timestamp (timezone-independent).
// - Fixtures that use New_York / Warsaw require timezone-db to round-trip
// to the exact Fantom hex; without that feature the tz collapses to UTC.
// - UTC fixtures always do a full assert_fixture.
// - Sub-second (I8) fixtures always use assert_roundtrip.
//
// Fantom source values:
// dt_i4_ny = "2015-11-30T12:03:57-05:00 New_York" -> UTC 2015-11-30T17:03:57Z
// dt_i4_utc = "2021-06-15T12:00:00Z UTC" -> UTC 2021-06-15T12:00:00Z
// dt_i4_pre2k = "1999-06-07T01:02:00-04:00 New_York" -> UTC 1999-06-07T05:02:00Z
// dt_i4_pre70 = "1950-06-07T01:02:00-04:00 New_York" -> UTC 1950-06-07T05:02:00Z
// dt_i4_warsaw = "2000-01-01T00:00:00+01:00 Warsaw" -> UTC 1999-12-31T23:00:00Z
// -----------------------------------------------------------------------
/// I4 DateTime in UTC (const index 24 = 0x18) - always fully verifiable.
#[test]
fn fixture_dt_i4_utc() {
let dt = DateTime::parse_from_rfc3339("2021-06-15T12:00:00Z").expect("dt");
assert_fixture("0f285b52c018", &Value::from(dt));
}
/// I4 DateTime in America/New_York - verify unix timestamp; exact hex needs tz-db.
#[test]
fn fixture_dt_i4_ny() {
let got = decode("0f1def3dfd1a");
let dt = DateTime::try_from(&got).expect("datetime");
assert_eq!(dt.timestamp(), 1_448_903_037, "unix timestamp mismatch");
}
#[cfg(feature = "timezone-db")]
#[test]
fn fixture_dt_i4_ny_exact() {
assert_roundtrip("0f1def3dfd1a");
}
/// I4 DateTime before 2000 in New_York - verify unix timestamp.
#[test]
fn fixture_dt_i4_pre2k() {
let got = decode("0ffeee0ec81a");
let dt = DateTime::try_from(&got).expect("datetime");
assert_eq!(dt.timestamp(), 928_731_720, "unix timestamp mismatch");
}
#[cfg(feature = "timezone-db")]
#[test]
fn fixture_dt_i4_pre2k_exact() {
assert_roundtrip("0ffeee0ec81a");
}
/// I4 DateTime before 1970 in New_York - verify unix timestamp.
#[test]
fn fixture_dt_i4_pre70() {
let got = decode("0fa2c361481a");
let dt = DateTime::try_from(&got).expect("datetime");
assert_eq!(dt.timestamp(), -617_569_080, "unix timestamp mismatch");
}
#[cfg(feature = "timezone-db")]
#[test]
fn fixture_dt_i4_pre70_exact() {
assert_roundtrip("0fa2c361481a");
}
/// I4 DateTime in Warsaw (inline timezone) - verify unix timestamp.
/// Fixture: ctrl=0f, i32=0xfffff1f0=-3600, tz=inline "Warsaw"
#[test]
fn fixture_dt_i4_warsaw() {
let got = decode("0ffffff1f0ff06576172736177");
let dt = DateTime::try_from(&got).expect("datetime");
assert_eq!(dt.timestamp(), 946_681_200, "unix timestamp mismatch");
}
#[cfg(feature = "timezone-db")]
#[test]
fn fixture_dt_i4_warsaw_exact() {
assert_roundtrip("0ffffff1f0ff06576172736177");
}
/// I8 DateTime with millisecond precision in New_York.
#[test]
fn fixture_dt_i8_ny_ms() {
assert_roundtrip("1006f83cbfe7d92c801a");
}
/// I8 DateTime with microsecond precision in New_York.
#[test]
fn fixture_dt_i8_ny_us() {
assert_roundtrip("1006f83cd3601d82781a");
}
/// I8 DateTime before 1970 with millisecond precision.
#[test]
fn fixture_dt_i8_pre70() {
assert_roundtrip("10ea4aa7624f67a4c01a");
}
/// I8 DateTime in Warsaw with millisecond precision (inline timezone).
#[test]
fn fixture_dt_i8_warsaw() {
assert_roundtrip("10fffffcba00deb000ff06576172736177");
}
// -----------------------------------------------------------------------
// Coord
//
// Fantom packs: lat_packed = (lat + 90) * 1_000_000
// lng_packed = (lng + 180) * 1_000_000
// Both stored as big-endian i32.
//
// Floating-point rounding means decoded values may differ by up to 1e-6
// from the original, so we use tolerance comparisons.
// -----------------------------------------------------------------------
/// Assert a coord fixture with 1e-6 tolerance for the decoded coordinate values.
fn assert_coord_fixture(hex_str: &str, expected_lat: f64, expected_lng: f64) {
let got = decode(hex_str);
let c = Coord::try_from(&got).expect("coord");
assert!(
(c.lat - expected_lat).abs() < 1e-6,
"lat mismatch: {} vs {}",
c.lat,
expected_lat
);
assert!(
(c.long - expected_lng).abs() < 1e-6,
"lng mismatch: {} vs {}",
c.long,
expected_lng
);
// Re-encoding `got` (which stores the rounded lat/lng) should reproduce
// the original fixture bytes exactly.
let reencoded = encode(&got);
assert_eq!(reencoded, hex_str, "coord re-encode mismatch");
}
#[test]
fn fixture_coord_pos() {
// lat=37.54, lng=77.43
// lat_packed = (37.54 + 90) * 1e6 = 127_540_000 = 0x079a1b20
// lng_packed = (77.43 + 180) * 1e6 = 257_430_000 = 0x0f5811f0
assert_coord_fixture("11079a1b200f5811f0", 37.54, 77.43);
}
#[test]
fn fixture_coord_neg() {
// lat=-17.535, lng=-149.569
// lat_packed = (-17.535 + 90) * 1e6 = 72_465_000 = 0x0451ba68
// lng_packed = (-149.569 + 180) * 1e6 = 30_431_000 = 0x01d05718
assert_coord_fixture("110451ba6801d05718", -17.535, -149.569);
}
#[test]
fn fixture_coord_zero() {
// lat=0, lng=0 - exact integer packing, no rounding error
// lat_packed = 90 * 1e6 = 90_000_000 = 0x055d4a80
// lng_packed = 180 * 1e6 = 180_000_000 = 0x0aba9500
assert_fixture("11055d4a800aba9500", &Value::from(Coord::make(0.0, 0.0)));
}
// -----------------------------------------------------------------------
// Symbol
// -----------------------------------------------------------------------
/// "coolingTower" - two-byte varint const index
#[test]
fn fixture_sym_const() {
assert_fixture("1980a3", &Value::from(Symbol::make("coolingTower")));
}
/// "foo-bar" - inline (not in const table)
#[test]
fn fixture_sym_inline() {
assert_fixture(
"19ff07666f6f2d626172",
&Value::from(Symbol::make("foo-bar")),
);
}
/// "site" - two-byte varint const index
#[test]
fn fixture_sym_site() {
assert_fixture("19817e", &Value::from(Symbol::make("site")));
}
// -----------------------------------------------------------------------
// XStr
//
// XStr type="Foo" (inline, not in const table), value="bar" (const 555 = 0x822b)
// -----------------------------------------------------------------------
#[test]
fn fixture_xstr_foo() {
assert_fixture("12ff03466f6f822b", &Value::from(XStr::make("Foo", "bar")));
}
// -----------------------------------------------------------------------
// Dict
//
// Note on key ordering: Rust encodes dict keys in alphabetical BTreeMap
// order; Fantom uses insertion order. For single-key dicts there is no
// ambiguity. For multi-key dicts we only assert decode equality (the
// decoded Dict compares by key/value set, not iteration order).
// -----------------------------------------------------------------------
#[test]
fn fixture_dict_empty() {
assert_fixture("14", &Value::from(Dict::default()));
}
/// {dis:"Hello"} - single key, no ordering ambiguity
#[test]
fn fixture_dict_dis() {
let v = Value::make_dict(dict! { "dis" => Value::from("Hello") });
assert_fixture("157b0180b909ff0548656c6c6f7d", &v);
}
/// {site:M, dis:"Site"}
/// Fantom order: site, dis. Rust BTreeMap order: dis, site.
/// Decode check only - the re-encoded hex differs in key order.
#[test]
fn fixture_dict_site_decode() {
let expected = Value::make_dict(dict! {
"site" => Value::make_marker(),
"dis" => Value::from("Site")
});
assert_decode("157b02817e0180b909ff04536974657d", &expected);
}
/// {val: 123kW} - single key, no ordering ambiguity
#[test]
fn fixture_dict_num() {
let v = Value::make_dict(
dict! { "val" => Value::from(Number::make_with_unit(123.0, get_unit_or_default("kW"))) },
);
assert_fixture("157b0181aa06007b82767d", &v);
}
// -----------------------------------------------------------------------
// List
// -----------------------------------------------------------------------
#[test]
fn fixture_list_empty() {
assert_fixture("16", &Value::from(List::default()));
}
/// [M]
#[test]
fn fixture_list_marker() {
assert_fixture(
"175b01015d",
&Value::from(List::from(vec![Value::make_marker()])),
);
}
/// ["hello", 42, M] - mixed list; order is stable
#[test]
fn fixture_list_mixed() {
let list = List::from(vec![
Value::from("hello"),
Value::from(Number::make(42.0)),
Value::make_marker(),
]);
assert_fixture("175b0309ff0568656c6c6f06002a00015d", &Value::from(list));
}
// -----------------------------------------------------------------------
// Grid
// -----------------------------------------------------------------------
fn make_col(name: &str) -> Column {
Column {
name: name.to_string(),
meta: None,
}
}
fn make_col_with_meta(name: &str, meta: Dict) -> Column {
Column {
name: name.to_string(),
meta: Some(meta),
}
}
/// Empty grid - no columns, no rows, no meta.
/// Fantom: GridBuilder().toGrid
#[test]
fn fixture_grid_empty() {
let expected = Value::from(Grid::default());
assert_fixture("183c0000143e", &expected);
}
/// Grid with two columns (dis, val), no rows, no meta.
/// Fantom: addCol("dis") + addCol("val")
#[test]
fn fixture_grid_cols_only() {
let expected = Value::from(Grid {
meta: None,
columns: vec![make_col("dis"), make_col("val")],
rows: vec![],
ver: GRID_FORMAT_VERSION.to_string(),
});
assert_fixture("183c02001480b91481aa143e", &expected);
}
/// Grid with column-level meta dicts, no rows.
/// Fantom: addCol("dis", ["doc":"Display name"]) + addCol("val", ["doc":"Numeric value", "unit":"kW"])
///
/// Fantom insertion order for the "val" col meta is (unit, doc); Rust BTreeMap
/// iterates alphabetically (doc, unit), so re-encoding produces different bytes.
/// We verify the Fantom bytes decode to the correct Grid, using assert_decode.
#[test]
fn fixture_grid_col_meta() {
let expected = Value::from(Grid {
meta: None,
columns: vec![
make_col_with_meta("dis", dict! { "doc" => Value::from("Display name") }),
make_col_with_meta(
"val",
dict! {
"doc" => Value::from("Numeric value"),
"unit" => Value::from("kW")
},
),
],
rows: vec![],
ver: GRID_FORMAT_VERSION.to_string(),
});
assert_decode(
"183c02001480b9157b01830709ff0c446973706c6179206e616d657d81aa157b02819a098276830709ff0d4e756d657269632076616c75657d3e",
&expected,
);
}
/// Grid with two columns and two rows.
/// Fantom: addRow(["Site A", 100kW]) + addRow(["Site B", 200kW])
///
/// Row cells are written in column order (no dict keys), so encoding is
/// stable and byte-for-byte identical to Fantom.
#[test]
fn fixture_grid_rows() {
let expected = Value::from(Grid {
meta: None,
columns: vec![make_col("dis"), make_col("val")],
rows: vec![
dict! {
"dis" => Value::from("Site A"),
"val" => Value::from(Number::make_with_unit(100.0, get_unit_or_default("kW")))
},
dict! {
"dis" => Value::from("Site B"),
"val" => Value::from(Number::make_with_unit(200.0, get_unit_or_default("kW")))
},
],
ver: GRID_FORMAT_VERSION.to_string(),
});
assert_fixture(
"183c02021480b91481aa1409ff06536974652041060064827609ff065369746520420600c882763e",
&expected,
);
}
/// Grid with grid-level meta, one column, one row.
/// Fantom: setMeta(["dis":"My Grid", "view":M]) + addCol("equip") + addRow([M])
///
/// Fantom insertion order for the meta dict is (dis, view); Rust BTreeMap
/// iterates alphabetically (dis, view) — same order here — but we still use
/// assert_decode because dict key ordering may differ across Fantom versions.
#[test]
fn fixture_grid_meta() {
let expected = Value::from(Grid {
meta: Some(dict! {
"dis" => Value::from("My Grid"),
"view" => Value::make_marker()
}),
columns: vec![make_col("equip")],
rows: vec![dict! { "equip" => Value::make_marker() }],
ver: GRID_FORMAT_VERSION.to_string(),
});
assert_decode(
"183c0101157b0283660180b909ff074d7920477269647d80cf14013e",
&expected,
);
}
}