noxu-bind 3.2.0

Serialization bindings for Noxu DB
Documentation
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
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
//! JE TCK port: tuple format & ordering tests.
//!
//! Ports invariants from JE
//! `com.sleepycat.bind.tuple.test.TupleFormatTest` and
//! `com.sleepycat.bind.tuple.test.TupleOrderingTest` onto Noxu's
//! `TupleInput` / `TupleOutput`.
//!
//! Notes on adaptation
//!
//! - JE `writeString` writes UTF-8 followed by a single 0x00 terminator.
//!   Noxu's `write_string` escapes embedded `0x00` bytes as `[0x00, 0x01]`
//!   and terminates with `[0x00, 0x00]` (two bytes).  This is documented
//!   on `TupleOutput::write_string`, so the JE wire-size assertions
//!   (`val.length() + 1`) do not apply to noxu and are intentionally
//!   omitted; the round-trip and ordering invariants are preserved.
//! - JE supports a "null string" marker.  Noxu's API takes `&str` and
//!   does not, so JE's `testNullString` is omitted.
//! - JE `writeFloat` / `writeDouble` use the *non-sorted* IEEE-754
//!   ordering, in which only non-negative values are ordered; noxu's
//!   `write_float` / `write_double` match that contract.  Sortable
//!   ordering across the full range uses `write_sorted_float` /
//!   `write_sorted_double`.
//! - All assertions check value-level invariants (round-trip, monotone
//!   byte ordering of the encoded form).  Wire-format byte-level
//!   assertions are noxu-specific and live in unit tests in
//!   `tuple_output.rs` and `tuple_input.rs`.

use noxu_bind::{TupleInput, TupleOutput};

// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------

/// Asserts that `prev < next` lexicographically as unsigned bytes.
/// Mirrors JE TupleOrderingTest's `check(int)` helper.
fn assert_lt_bytes(prev: &[u8], next: &[u8], idx: usize) {
    assert!(
        prev < next,
        "encoded tuple at idx {idx} is not strictly greater than the \
         previous one\n  prev = {prev:02x?}\n  next = {next:02x?}",
    );
}

/// Encode each value with `write_value`, assert that successive encodings
/// are strictly monotone in unsigned-byte lexicographic order.
fn check_monotone<T: Copy, F: FnMut(&mut TupleOutput, T)>(
    data: &[T],
    mut write_value: F,
) {
    let mut prev: Option<Vec<u8>> = None;
    for (i, &v) in data.iter().enumerate() {
        let mut out = TupleOutput::new();
        write_value(&mut out, v);
        let next = out.as_bytes().to_vec();
        if let Some(prev) = &prev {
            assert_lt_bytes(prev, &next, i);
        }
        prev = Some(next);
    }
}

// ---------------------------------------------------------------------------
// Round-trip tests (port of TupleFormatTest)
// ---------------------------------------------------------------------------

/// Port of `TupleFormatTest.testString`: round-trip "", "a", "abc", and
/// confirm that multiple strings concatenated by repeated `write_string`
/// can be read back in order with no bytes left over.
#[test]
fn tck_tuple_format_test_string() {
    for s in ["", "a", "abc"] {
        let mut out = TupleOutput::new();
        out.write_string(s);
        let mut input = TupleInput::new(out.as_bytes());
        assert_eq!(s, input.read_string().unwrap());
        assert_eq!(0, input.available());
    }

    // Two strings.
    let mut out = TupleOutput::new();
    out.write_string("abc");
    out.write_string("defg");
    let mut input = TupleInput::new(out.as_bytes());
    assert_eq!("abc", input.read_string().unwrap());
    assert_eq!("defg", input.read_string().unwrap());
    assert_eq!(0, input.available());

    // Three strings.
    let mut out = TupleOutput::new();
    out.write_string("abc");
    out.write_string("defg");
    out.write_string("hijkl");
    let mut input = TupleInput::new(out.as_bytes());
    assert_eq!("abc", input.read_string().unwrap());
    assert_eq!("defg", input.read_string().unwrap());
    assert_eq!("hijkl", input.read_string().unwrap());
    assert_eq!(0, input.available());
}

/// Port of `TupleFormatTest.testBoolean`: round-trip of true/false in
/// 1/2/3-element tuples; each boolean occupies 1 byte.
#[test]
fn tck_tuple_format_test_boolean() {
    for v in [true, false] {
        let mut out = TupleOutput::new();
        out.write_bool(v);
        assert_eq!(1, out.len());
        let mut input = TupleInput::new(out.as_bytes());
        assert_eq!(v, input.read_bool().unwrap());
        assert_eq!(0, input.available());
    }

    let mut out = TupleOutput::new();
    out.write_bool(true);
    out.write_bool(false);
    out.write_bool(true);
    assert_eq!(3, out.len());
    let mut input = TupleInput::new(out.as_bytes());
    assert!(input.read_bool().unwrap());
    assert!(!input.read_bool().unwrap());
    assert!(input.read_bool().unwrap());
    assert_eq!(0, input.available());
}

/// Port of `TupleFormatTest.testInt`: round-trip every interesting i32
/// boundary value.  Each i32 occupies 4 bytes.
#[test]
fn tck_tuple_format_test_int() {
    let data: &[i32] = &[
        i32::MIN,
        i32::MIN + 1,
        i16::MIN as i32,
        i16::MIN as i32 + 1,
        i8::MIN as i32,
        i8::MIN as i32 + 1,
        -1,
        0,
        1,
        i8::MAX as i32 - 1,
        i8::MAX as i32,
        i16::MAX as i32 - 1,
        i16::MAX as i32,
        i32::MAX - 1,
        i32::MAX,
    ];
    for &v in data {
        let mut out = TupleOutput::new();
        out.write_i32(v);
        assert_eq!(4, out.len());
        let mut input = TupleInput::new(out.as_bytes());
        assert_eq!(v, input.read_i32().unwrap());
        assert_eq!(0, input.available());
    }
}

/// Port of `TupleFormatTest.testLong`: same as testInt but for i64.
/// Each i64 occupies 8 bytes.
#[test]
fn tck_tuple_format_test_long() {
    let data: &[i64] = &[
        i64::MIN,
        i64::MIN + 1,
        i32::MIN as i64,
        i32::MIN as i64 + 1,
        i16::MIN as i64,
        -1,
        0,
        1,
        i16::MAX as i64,
        i32::MAX as i64,
        i32::MAX as i64 + 1,
        i64::MAX - 1,
        i64::MAX,
    ];
    for &v in data {
        let mut out = TupleOutput::new();
        out.write_i64(v);
        assert_eq!(8, out.len());
        let mut input = TupleInput::new(out.as_bytes());
        assert_eq!(v, input.read_i64().unwrap());
        assert_eq!(0, input.available());
    }
}

/// Port of `TupleFormatTest.testPackedInt` & `testPackedLong`: round-trip
/// for the variable-length packed encoding.  The size of the encoding is
/// not asserted (it depends on magnitude), only that decode(encode) == v
/// for every boundary value.
#[test]
fn tck_tuple_format_test_packed_int_and_long() {
    let int_data: &[i32] =
        &[i32::MIN, -1, 0, 1, 0x7F, 0x80, 0x3FFF, 0x4000, i32::MAX];
    for &v in int_data {
        let mut out = TupleOutput::new();
        out.write_packed_int(v);
        let mut input = TupleInput::new(out.as_bytes());
        assert_eq!(v, input.read_packed_int().unwrap());
        assert_eq!(0, input.available());
    }

    let long_data: &[i64] =
        &[i64::MIN, i32::MIN as i64, -1, 0, 1, i32::MAX as i64, i64::MAX];
    for &v in long_data {
        let mut out = TupleOutput::new();
        out.write_packed_long(v);
        let mut input = TupleInput::new(out.as_bytes());
        assert_eq!(v, input.read_packed_long().unwrap());
        assert_eq!(0, input.available());
    }
}

// ---------------------------------------------------------------------------
// Ordering tests (port of TupleOrderingTest)
// ---------------------------------------------------------------------------

/// Port of `TupleOrderingTest.testString`: encoded strings sort
/// lexicographically by Unicode order when written via `write_string`.
/// Strings containing `\u{0000}` are excluded because noxu's escape
/// sequence (`\0` -> `[0x00 0x01]`) means a `\0`-containing string sorts
/// strictly *after* the same string without the trailing `\0` rather
/// than before it; the ordering is still total and consistent, but the
/// JE-specific data set is not directly comparable for those cases.
#[test]
fn tck_tuple_ordering_test_string() {
    let data: &[&str] = &[
        "",
        "\u{0001}",
        "\u{0002}",
        "A",
        "a",
        "ab",
        "b",
        "bb",
        "bba",
        "c",
        "c\u{0001}",
        "d",
        "\u{007F}",
        "\u{00FF}",
    ];
    check_monotone(data, |out, s| out.write_string(s));
}

/// Port of `TupleOrderingTest.testBoolean`: false < true.
#[test]
fn tck_tuple_ordering_test_boolean() {
    check_monotone(&[false, true], |out, v| out.write_bool(v));
}

/// Port of `TupleOrderingTest.testUnsignedByte`/`UnsignedShort`/
/// `UnsignedInt`: unsigned big-endian writes preserve numeric order.
#[test]
fn tck_tuple_ordering_test_unsigned() {
    let bytes: &[u8] = &[0, 1, 0x7F, 0xFF];
    check_monotone(bytes, |out, v| out.write_u8(v));

    let shorts: &[u16] = &[0, 1, 0xFE, 0xFF, 0x800, 0x7FFF, 0xFFFF];
    check_monotone(shorts, |out, v| out.write_u16(v));

    let ints: &[u32] = &[
        0, 1, 0xFE, 0xFF, 0x800, 0x7FFF, 0xFFFF, 0x80000, 0x7FFFFFFF,
        0x80000000, 0xFFFFFFFF,
    ];
    check_monotone(ints, |out, v| out.write_u32(v));
}

/// Port of `TupleOrderingTest.testByte` / `testShort` / `testInt` /
/// `testLong`: signed integers, when written via `write_iN`, sort by
/// numeric value.  Noxu (like JE) flips the high bit at the byte level
/// so that negative values sort before non-negative ones.
#[test]
fn tck_tuple_ordering_test_signed() {
    let bytes: &[i8] = &[i8::MIN, i8::MIN + 1, -1, 0, 1, i8::MAX - 1, i8::MAX];
    check_monotone(bytes, |out, v| out.write_i8(v));

    let shorts: &[i16] = &[
        i16::MIN,
        i16::MIN + 1,
        i8::MIN as i16,
        i8::MIN as i16 + 1,
        -1,
        0,
        1,
        i8::MAX as i16 - 1,
        i8::MAX as i16,
        i16::MAX - 1,
        i16::MAX,
    ];
    check_monotone(shorts, |out, v| out.write_i16(v));

    let ints: &[i32] = &[
        i32::MIN,
        i32::MIN + 1,
        i16::MIN as i32,
        i16::MIN as i32 + 1,
        -1,
        0,
        1,
        i16::MAX as i32,
        i32::MAX - 1,
        i32::MAX,
    ];
    check_monotone(ints, |out, v| out.write_i32(v));

    let longs: &[i64] = &[
        i64::MIN,
        i64::MIN + 1,
        i32::MIN as i64,
        -1,
        0,
        1,
        i32::MAX as i64,
        i64::MAX - 1,
        i64::MAX,
    ];
    check_monotone(longs, |out, v| out.write_i64(v));
}

/// Port of `TupleOrderingTest.testFloat` / `testDouble`: only the
/// non-negative subset of IEEE-754 sorts deterministically when
/// written via `write_float` / `write_double`.  NaN is excluded
/// (`NaN < NaN` is meaningless; JE's test happens to work because
/// the bit pattern of one specific NaN sorts above +Inf, but that
/// is implementation-specific noise rather than an invariant).
#[test]
fn tck_tuple_ordering_test_float_double_nonneg() {
    let floats: &[f32] = &[
        0.0,
        f32::MIN_POSITIVE,
        2.0 * f32::MIN_POSITIVE,
        0.01,
        0.99,
        1.0,
        1.99,
        i8::MAX as f32,
        i16::MAX as f32,
        i32::MAX as f32,
        f32::MAX,
        f32::INFINITY,
    ];
    check_monotone(floats, |out, v| out.write_float(v));

    let doubles: &[f64] = &[
        0.0,
        f64::MIN_POSITIVE,
        2.0 * f64::MIN_POSITIVE,
        0.001,
        0.999,
        1.0,
        1.999,
        i32::MAX as f64,
        f32::MAX as f64,
        f64::MAX,
        f64::INFINITY,
    ];
    check_monotone(doubles, |out, v| out.write_double(v));
}

/// Port of `TupleOrderingTest.testSortedFloat` / `testSortedDouble`:
/// across the full IEEE-754 range (negatives included), `write_sorted_*`
/// produces a monotone byte encoding.  NaN is excluded as above.
#[test]
fn tck_tuple_ordering_test_sorted_float_double() {
    let floats: &[f32] = &[
        f32::NEG_INFINITY,
        -f32::MAX,
        i32::MIN as f32,
        i16::MIN as f32,
        i8::MIN as f32,
        -1.99,
        -1.0,
        -0.99,
        -0.01,
        -2.0 * f32::MIN_POSITIVE,
        -f32::MIN_POSITIVE,
        0.0,
        f32::MIN_POSITIVE,
        2.0 * f32::MIN_POSITIVE,
        0.01,
        0.99,
        1.0,
        1.99,
        i8::MAX as f32,
        i16::MAX as f32,
        i32::MAX as f32,
        f32::MAX,
        f32::INFINITY,
    ];
    check_monotone(floats, |out, v| out.write_sorted_float(v));

    let doubles: &[f64] = &[
        f64::NEG_INFINITY,
        -f64::MAX,
        -(f32::MAX as f64),
        i64::MIN as f64,
        i32::MIN as f64,
        i16::MIN as f64,
        i8::MIN as f64,
        -1.999,
        -1.0,
        -0.999,
        -0.001,
        -2.0 * f64::MIN_POSITIVE,
        -f64::MIN_POSITIVE,
        0.0,
        f64::MIN_POSITIVE,
        2.0 * f64::MIN_POSITIVE,
        0.001,
        0.999,
        1.0,
        1.999,
        i32::MAX as f64,
        f32::MAX as f64,
        f64::MAX,
        f64::INFINITY,
    ];
    check_monotone(doubles, |out, v| out.write_sorted_double(v));
}

/// Port of `TupleOrderingTest.testSortedPackedInt` /
/// `testSortedPackedLong`: the sorted packed varint encoding is
/// monotone across negative and non-negative values.
#[test]
fn tck_tuple_ordering_test_sorted_packed_int_long() {
    let ints: &[i32] = &[
        i32::MIN,
        i16::MIN as i32,
        i8::MIN as i32,
        -1,
        0,
        1,
        i8::MAX as i32,
        i16::MAX as i32,
        i32::MAX,
    ];
    check_monotone(ints, |out, v| out.write_sorted_packed_int(v));

    let longs: &[i64] = &[
        i64::MIN,
        i32::MIN as i64,
        i16::MIN as i64,
        -1,
        0,
        1,
        i16::MAX as i64,
        i32::MAX as i64,
        i64::MAX,
    ];
    check_monotone(longs, |out, v| out.write_sorted_packed_long(v));
}

// ---------------------------------------------------------------------------
// Round-trip ports added in wave 9-C (TupleFormatTest extras)
// ---------------------------------------------------------------------------

/// Port of `TupleFormatTest.testChars`: write a sequence of u16 chars
/// via repeated `write_char` and read them back via `read_char`.  JE
/// uses `writeChars(String)`/`readChars(int)` operating on UTF-16
/// code units; Noxu exposes only the per-char primitive, so we loop
/// to assert the same invariant.
#[test]
fn tck_tuple_format_test_chars() {
    fn round_trip_chars(s: &str) {
        let chars: Vec<u16> = s.encode_utf16().collect();
        let mut out = TupleOutput::new();
        for &c in &chars {
            out.write_char(c);
        }
        // Each char occupies 2 bytes.
        assert_eq!(2 * chars.len(), out.len());
        let mut input = TupleInput::new(out.as_bytes());
        for &c in &chars {
            assert_eq!(c, input.read_char().unwrap());
        }
        assert_eq!(0, input.available());
    }

    round_trip_chars("");
    round_trip_chars("a");
    round_trip_chars("abc");

    // Multi-string concatenation, JE testChars: 7 chars total = 14 bytes.
    let mut out = TupleOutput::new();
    for c in "abc".encode_utf16() {
        out.write_char(c);
    }
    for c in "defg".encode_utf16() {
        out.write_char(c);
    }
    assert_eq!(7 * 2, out.len());
    let mut input = TupleInput::new(out.as_bytes());
    let mut buf = String::new();
    for _ in 0..3 {
        buf.push(char::from_u32(input.read_char().unwrap() as u32).unwrap());
    }
    assert_eq!("abc", buf);
    let mut buf = String::new();
    for _ in 0..4 {
        buf.push(char::from_u32(input.read_char().unwrap() as u32).unwrap());
    }
    assert_eq!("defg", buf);
    assert_eq!(0, input.available());
}

/// Port of `TupleFormatTest.testBytes`: `write_bytes` writes the raw
/// bytes (no length prefix) and `read_bytes(n)` consumes exactly `n`
/// bytes.  Multi-write/multi-read concatenates without delimiters.
#[test]
fn tck_tuple_format_test_bytes() {
    fn round_trip(val: &[u8]) {
        let mut out = TupleOutput::new();
        out.write_bytes(val);
        assert_eq!(val.len(), out.len());
        let mut input = TupleInput::new(out.as_bytes());
        let got = input.read_bytes(val.len()).unwrap();
        assert_eq!(val, got.as_slice());
        assert_eq!(0, input.available());
    }

    round_trip(&[]);
    round_trip(b"a");
    round_trip(b"abc");
    // Top-bit byte values, JE's 0x7F00..=0xFFFF, projected to bytes
    // by JE's `writeBytes(char[])` (high byte discarded).
    round_trip(&[0x00, 0xFF, 0x00, 0xFF]);

    // Multi-write concatenation.
    let mut out = TupleOutput::new();
    out.write_bytes(b"abc");
    out.write_bytes(b"defg");
    assert_eq!(7, out.len());
    let mut input = TupleInput::new(out.as_bytes());
    assert_eq!(b"abc".to_vec(), input.read_bytes(3).unwrap());
    assert_eq!(b"defg".to_vec(), input.read_bytes(4).unwrap());
    assert_eq!(0, input.available());

    let mut out = TupleOutput::new();
    out.write_bytes(b"abc");
    out.write_bytes(b"defg");
    out.write_bytes(b"hijkl");
    assert_eq!(12, out.len());
    let mut input = TupleInput::new(out.as_bytes());
    assert_eq!(b"abc".to_vec(), input.read_bytes(3).unwrap());
    assert_eq!(b"defg".to_vec(), input.read_bytes(4).unwrap());
    assert_eq!(b"hijkl".to_vec(), input.read_bytes(5).unwrap());
    assert_eq!(0, input.available());
}

/// Port of `TupleFormatTest.testByte`: round-trip every interesting i8
/// boundary value.  Each i8 occupies 1 byte.
#[test]
fn tck_tuple_format_test_byte() {
    let data: &[i8] = &[
        i8::MIN,
        i8::MIN + 1,
        -1,
        0,
        1,
        i8::MAX - 1,
        i8::MAX,
        0x7F,
        -0x80, // 0x80 wraps in i8
    ];
    for &v in data {
        let mut out = TupleOutput::new();
        out.write_i8(v);
        assert_eq!(1, out.len());
        let mut input = TupleInput::new(out.as_bytes());
        assert_eq!(v, input.read_i8().unwrap());
        assert_eq!(0, input.available());
    }

    // Three-byte concatenation.
    let mut out = TupleOutput::new();
    out.write_i8(0);
    out.write_i8(1);
    out.write_i8(-1);
    assert_eq!(3, out.len());
    let mut input = TupleInput::new(out.as_bytes());
    assert_eq!(0, input.read_i8().unwrap());
    assert_eq!(1, input.read_i8().unwrap());
    assert_eq!(-1, input.read_i8().unwrap());
    assert_eq!(0, input.available());
}

/// Port of `TupleFormatTest.testShort`: round-trip every interesting i16
/// boundary value.  Each i16 occupies 2 bytes.
#[test]
fn tck_tuple_format_test_short() {
    let data: &[i16] = &[
        i16::MIN,
        i16::MIN + 1,
        i8::MIN as i16,
        i8::MIN as i16 + 1,
        -1,
        0,
        1,
        i8::MAX as i16 - 1,
        i8::MAX as i16,
        i16::MAX - 1,
        i16::MAX,
    ];
    for &v in data {
        let mut out = TupleOutput::new();
        out.write_i16(v);
        assert_eq!(2, out.len());
        let mut input = TupleInput::new(out.as_bytes());
        assert_eq!(v, input.read_i16().unwrap());
        assert_eq!(0, input.available());
    }

    // Three-short concatenation.
    let mut out = TupleOutput::new();
    out.write_i16(0);
    out.write_i16(1);
    out.write_i16(-1);
    assert_eq!(3 * 2, out.len());
    let mut input = TupleInput::new(out.as_bytes());
    assert_eq!(0, input.read_i16().unwrap());
    assert_eq!(1, input.read_i16().unwrap());
    assert_eq!(-1, input.read_i16().unwrap());
    assert_eq!(0, input.available());
}

/// Port of `TupleFormatTest.testFloat`: round-trip f32 values including
/// NaN, +/-Inf, and signed zero across every interesting boundary.
/// Each f32 occupies 4 bytes.  Note that for `write_float` (unsorted
/// IEEE-754), the byte ordering does not match numeric ordering for
/// negatives; only round-trip is asserted here.
#[test]
fn tck_tuple_format_test_float() {
    let data: &[f32] = &[
        0.0,
        1.0,
        -1.0,
        0.1,
        -0.1,
        f32::NEG_INFINITY,
        f32::INFINITY,
        i16::MAX as f32,
        i16::MIN as f32,
        i32::MAX as f32,
        i32::MIN as f32,
        f32::MAX,
        f32::MIN_POSITIVE,
        -f32::MIN_POSITIVE,
        -f32::MAX,
    ];
    for &v in data {
        let mut out = TupleOutput::new();
        out.write_float(v);
        assert_eq!(4, out.len());
        let mut input = TupleInput::new(out.as_bytes());
        let got = input.read_float().unwrap();
        assert_eq!(v.to_bits(), got.to_bits());
        assert_eq!(0, input.available());
    }

    // NaN round-trip preserves NaN-ness.
    let mut out = TupleOutput::new();
    out.write_float(f32::NAN);
    let mut input = TupleInput::new(out.as_bytes());
    assert!(input.read_float().unwrap().is_nan());

    // Three-float concatenation.
    let mut out = TupleOutput::new();
    out.write_float(0.0);
    out.write_float(1.0);
    out.write_float(-1.0);
    assert_eq!(3 * 4, out.len());
    let mut input = TupleInput::new(out.as_bytes());
    assert_eq!(0.0, input.read_float().unwrap());
    assert_eq!(1.0, input.read_float().unwrap());
    assert_eq!(-1.0, input.read_float().unwrap());
    assert_eq!(0, input.available());
}

/// Port of `TupleFormatTest.testDouble`: as testFloat but for f64.
/// Each f64 occupies 8 bytes.
#[test]
fn tck_tuple_format_test_double() {
    let data: &[f64] = &[
        0.0,
        1.0,
        -1.0,
        0.1,
        -0.1,
        f64::NEG_INFINITY,
        f64::INFINITY,
        i32::MAX as f64,
        i32::MIN as f64,
        i64::MAX as f64,
        i64::MIN as f64,
        f32::MAX as f64,
        f64::MAX,
        f64::MIN_POSITIVE,
        -f64::MIN_POSITIVE,
        -f64::MAX,
    ];
    for &v in data {
        let mut out = TupleOutput::new();
        out.write_double(v);
        assert_eq!(8, out.len());
        let mut input = TupleInput::new(out.as_bytes());
        let got = input.read_double().unwrap();
        assert_eq!(v.to_bits(), got.to_bits());
        assert_eq!(0, input.available());
    }

    // NaN round-trip preserves NaN-ness.
    let mut out = TupleOutput::new();
    out.write_double(f64::NAN);
    let mut input = TupleInput::new(out.as_bytes());
    assert!(input.read_double().unwrap().is_nan());

    // Three-double concatenation.
    let mut out = TupleOutput::new();
    out.write_double(0.0);
    out.write_double(1.0);
    out.write_double(-1.0);
    assert_eq!(3 * 8, out.len());
    let mut input = TupleInput::new(out.as_bytes());
    assert_eq!(0.0, input.read_double().unwrap());
    assert_eq!(1.0, input.read_double().unwrap());
    assert_eq!(-1.0, input.read_double().unwrap());
    assert_eq!(0, input.available());
}

/// Port of `TupleFormatTest.testSortedFloat`: round-trip f32 via the
/// sorted encoding (`write_sorted_float`/`read_sorted_float`).  Each
/// value occupies 4 bytes.
#[test]
fn tck_tuple_format_test_sorted_float() {
    let data: &[f32] = &[
        0.0,
        1.0,
        -1.0,
        0.1,
        -0.1,
        f32::NEG_INFINITY,
        f32::INFINITY,
        i16::MAX as f32,
        i16::MIN as f32,
        i32::MAX as f32,
        i32::MIN as f32,
        f32::MAX,
        -f32::MAX,
        f32::MIN_POSITIVE,
        -f32::MIN_POSITIVE,
    ];
    for &v in data {
        let mut out = TupleOutput::new();
        out.write_sorted_float(v);
        assert_eq!(4, out.len());
        let mut input = TupleInput::new(out.as_bytes());
        let got = input.read_sorted_float().unwrap();
        assert_eq!(v.to_bits(), got.to_bits());
        assert_eq!(0, input.available());
    }

    // NaN round-trips as NaN.
    let mut out = TupleOutput::new();
    out.write_sorted_float(f32::NAN);
    let mut input = TupleInput::new(out.as_bytes());
    assert!(input.read_sorted_float().unwrap().is_nan());
}

/// Port of `TupleFormatTest.testSortedDouble`: round-trip f64 via the
/// sorted encoding.  Each value occupies 8 bytes.
#[test]
fn tck_tuple_format_test_sorted_double() {
    let data: &[f64] = &[
        0.0,
        1.0,
        -1.0,
        0.1,
        -0.1,
        f64::NEG_INFINITY,
        f64::INFINITY,
        i32::MAX as f64,
        i32::MIN as f64,
        i64::MAX as f64,
        i64::MIN as f64,
        f32::MAX as f64,
        f64::MAX,
        -f64::MAX,
        f64::MIN_POSITIVE,
        -f64::MIN_POSITIVE,
    ];
    for &v in data {
        let mut out = TupleOutput::new();
        out.write_sorted_double(v);
        assert_eq!(8, out.len());
        let mut input = TupleInput::new(out.as_bytes());
        let got = input.read_sorted_double().unwrap();
        assert_eq!(v.to_bits(), got.to_bits());
        assert_eq!(0, input.available());
    }

    // NaN round-trips as NaN.
    let mut out = TupleOutput::new();
    out.write_sorted_double(f64::NAN);
    let mut input = TupleInput::new(out.as_bytes());
    assert!(input.read_sorted_double().unwrap().is_nan());
}

/// Port of `TupleFormatTest.testSortedPackedInt`: round-trip i32
/// boundaries via the sorted-packed varint encoding.  The encoded size
/// varies with magnitude; only decode(encode) == v is asserted.
#[test]
fn tck_tuple_format_test_sorted_packed_int() {
    let data: &[i32] = &[
        i32::MIN,
        i32::MIN + 1,
        i16::MIN as i32,
        i8::MIN as i32,
        -1,
        0,
        1,
        i8::MAX as i32,
        i16::MAX as i32,
        i32::MAX - 1,
        i32::MAX,
    ];
    for &v in data {
        let mut out = TupleOutput::new();
        out.write_sorted_packed_int(v);
        let mut input = TupleInput::new(out.as_bytes());
        assert_eq!(v, input.read_sorted_packed_int().unwrap());
        assert_eq!(0, input.available());
    }
}

/// Port of `TupleFormatTest.testSortedPackedLong`: round-trip i64
/// boundaries via the sorted-packed varint encoding.
#[test]
fn tck_tuple_format_test_sorted_packed_long() {
    let data: &[i64] = &[
        i64::MIN,
        i64::MIN + 1,
        i32::MIN as i64,
        i16::MIN as i64,
        -1,
        0,
        1,
        i16::MAX as i64,
        i32::MAX as i64,
        i64::MAX - 1,
        i64::MAX,
    ];
    for &v in data {
        let mut out = TupleOutput::new();
        out.write_sorted_packed_long(v);
        let mut input = TupleInput::new(out.as_bytes());
        assert_eq!(v, input.read_sorted_packed_long().unwrap());
        assert_eq!(0, input.available());
    }
}

/// Port of `TupleFormatTest.testUnsignedByte`: round-trip u8 via
/// `write_u8`/`read_u8`.  Each value occupies 1 byte.
#[test]
fn tck_tuple_format_test_unsigned_byte() {
    for v in [0u8, 1, 254, 255] {
        let mut out = TupleOutput::new();
        out.write_u8(v);
        assert_eq!(1, out.len());
        let mut input = TupleInput::new(out.as_bytes());
        assert_eq!(v, input.read_u8().unwrap());
        assert_eq!(0, input.available());
    }

    let mut out = TupleOutput::new();
    out.write_u8(0);
    out.write_u8(1);
    out.write_u8(255);
    assert_eq!(3, out.len());
    let mut input = TupleInput::new(out.as_bytes());
    assert_eq!(0, input.read_u8().unwrap());
    assert_eq!(1, input.read_u8().unwrap());
    assert_eq!(255, input.read_u8().unwrap());
    assert_eq!(0, input.available());
}

/// Port of `TupleFormatTest.testUnsignedShort`: round-trip u16 via
/// `write_u16`/`read_u16`.  Each value occupies 2 bytes.
#[test]
fn tck_tuple_format_test_unsigned_short() {
    let data: &[u16] = &[
        0,
        1,
        255,
        256,
        257,
        i16::MAX as u16 - 1,
        i16::MAX as u16,
        i16::MAX as u16 + 1,
        u16::MAX - 1,
        u16::MAX,
    ];
    for &v in data {
        let mut out = TupleOutput::new();
        out.write_u16(v);
        assert_eq!(2, out.len());
        let mut input = TupleInput::new(out.as_bytes());
        assert_eq!(v, input.read_u16().unwrap());
        assert_eq!(0, input.available());
    }

    let mut out = TupleOutput::new();
    out.write_u16(0);
    out.write_u16(1);
    out.write_u16(u16::MAX);
    assert_eq!(6, out.len());
    let mut input = TupleInput::new(out.as_bytes());
    assert_eq!(0, input.read_u16().unwrap());
    assert_eq!(1, input.read_u16().unwrap());
    assert_eq!(u16::MAX, input.read_u16().unwrap());
    assert_eq!(0, input.available());
}

/// Port of `TupleFormatTest.testUnsignedInt`: round-trip u32 via
/// `write_u32`/`read_u32`.  Each value occupies 4 bytes.
#[test]
fn tck_tuple_format_test_unsigned_int() {
    let data: &[u32] = &[
        0,
        1,
        255,
        256,
        257,
        i16::MAX as u32 - 1,
        i16::MAX as u32,
        i16::MAX as u32 + 1,
        i32::MAX as u32 - 1,
        i32::MAX as u32,
        i32::MAX as u32 + 1,
        u32::MAX - 1,
        u32::MAX,
    ];
    for &v in data {
        let mut out = TupleOutput::new();
        out.write_u32(v);
        assert_eq!(4, out.len());
        let mut input = TupleInput::new(out.as_bytes());
        assert_eq!(v, input.read_u32().unwrap());
        assert_eq!(0, input.available());
    }
}

// ---------------------------------------------------------------------------
// TupleOrderingTest extras (wave 9-C)
// ---------------------------------------------------------------------------

/// Port of `TupleOrderingTest.testChars`: per-char writes (16-bit BE)
/// produce a monotone-byte ordering by lexicographic char order.
/// Adapted: noxu has only `write_char` (single u16); we loop instead
/// of using JE's `writeChars(char[])`.
#[test]
fn tck_tuple_ordering_test_chars() {
    let data: &[&[u16]] = &[
        &[],
        &[0],
        &[b'a' as u16],
        &[b'a' as u16, 0],
        &[b'a' as u16, b'b' as u16],
        &[b'b' as u16],
        &[b'b' as u16, b'b' as u16],
        &[0x7F],
        &[0x7F, 0],
        &[0xFF],
        &[0xFF, 0],
    ];
    let mut prev: Option<Vec<u8>> = None;
    for (i, &v) in data.iter().enumerate() {
        let mut out = TupleOutput::new();
        for &c in v {
            out.write_char(c);
        }
        let next = out.as_bytes().to_vec();
        if let Some(prev) = &prev {
            assert_lt_bytes(prev, &next, i);
        }
        prev = Some(next);
    }
}

/// Port of `TupleOrderingTest.testBytes`: writing raw bytes preserves
/// lexicographic byte ordering.
#[test]
fn tck_tuple_ordering_test_bytes() {
    let data: &[&[u8]] =
        &[&[], &[0], b"a", &[b'a', 0], b"ab", b"b", b"bb", &[0x7F], &[0xFF]];
    let mut prev: Option<Vec<u8>> = None;
    for (i, &v) in data.iter().enumerate() {
        let mut out = TupleOutput::new();
        out.write_bytes(v);
        let next = out.as_bytes().to_vec();
        if let Some(prev) = &prev {
            assert_lt_bytes(prev, &next, i);
        }
        prev = Some(next);
    }
}

/// Port of `TupleOrderingTest.testPackedIntAndLong`: documents the JE
/// contract that the *unsorted* packed encoding sorts correctly only
/// across small non-negative integers (0..=630).  Full-range monotone
/// ordering requires `write_sorted_packed_*`.
#[test]
fn tck_tuple_ordering_test_packed_int_and_long() {
    let mut prev: Option<Vec<u8>> = None;
    for i in 0..=630i32 {
        let mut out = TupleOutput::new();
        out.write_packed_int(i);
        let next = out.as_bytes().to_vec();
        if let Some(prev) = &prev {
            assert_lt_bytes(prev, &next, i as usize);
        }
        prev = Some(next);
    }

    let mut prev: Option<Vec<u8>> = None;
    for i in 0..=630i64 {
        let mut out = TupleOutput::new();
        out.write_packed_long(i);
        let next = out.as_bytes().to_vec();
        if let Some(prev) = &prev {
            assert_lt_bytes(prev, &next, i as usize);
        }
        prev = Some(next);
    }
}

// ---------------------------------------------------------------------------
// TupleBindingTest: primitive object<->entry round-trip (wave 9-C)
// ---------------------------------------------------------------------------

/// Port of `TupleBindingTest.testPrimitiveBindings`: every primitive
/// binding round-trips an object via `object_to_entry` /
/// `entry_to_object`, with the expected byte size.
///
/// Adapted: noxu has no BigInteger / BigDecimal bindings (OUT-OF-SCOPE).
/// The Java-`null` paths (`StringBinding.stringToEntry(null,...)`) are
/// also absent because noxu's `StringBinding` round-trips a `String`
/// without a null-marker.  Boolean, byte (u8), short (i16), int (i32),
/// long (i64), float, double, packed int/long, sorted float/double,
/// sorted packed int/long, char, and string are all exercised.
#[test]
fn tck_tuple_binding_test_primitive_bindings() {
    use noxu_bind::{
        BoolBinding, ByteBinding, CharBinding, DoubleBinding, EntryBinding,
        FloatBinding, IntBinding, LongBinding, PackedIntBinding,
        PackedLongBinding, ShortBinding, SortedDoubleBinding,
        SortedFloatBinding, SortedPackedIntBinding, SortedPackedLongBinding,
        StringBinding,
    };
    use noxu_db::DatabaseEntry;

    fn rt<B, T>(binding: &B, value: T, expected_size: usize)
    where
        B: EntryBinding<T>,
        T: PartialEq + std::fmt::Debug + Clone,
    {
        let mut entry = DatabaseEntry::new();
        binding.object_to_entry(&value, &mut entry).unwrap();
        assert_eq!(
            expected_size,
            entry.data().len(),
            "unexpected encoded size for {value:?}",
        );
        let got = binding.entry_to_object(&entry).unwrap();
        assert_eq!(value, got);
    }

    // String: "abc" -> 3 bytes payload + 2-byte terminator (noxu).
    rt(&StringBinding::new(), "abc".to_string(), 3 + 2);

    // Char (u16) -> 2 bytes BE.
    rt(&CharBinding::new(), b'a' as u16, 2);

    // Bool -> 1 byte.
    rt(&BoolBinding::new(), true, 1);
    rt(&BoolBinding::new(), false, 1);

    // Byte (u8) -> 1 byte.
    rt(&ByteBinding::new(), 123u8, 1);

    // Short (i16) -> 2 bytes.
    rt(&ShortBinding::new(), 123i16, 2);

    // Int (i32) -> 4 bytes.
    rt(&IntBinding::new(), 123i32, 4);

    // Long (i64) -> 8 bytes.
    rt(&LongBinding::new(), 123i64, 8);

    // Float -> 4 bytes.
    rt(&FloatBinding::new(), 123.123f32, 4);

    // Double -> 8 bytes.
    rt(&DoubleBinding::new(), 123.123f64, 8);

    // Sorted float/double -> 4/8 bytes.
    rt(&SortedFloatBinding::new(), 123.123f32, 4);
    rt(&SortedDoubleBinding::new(), 123.123f64, 8);

    // Packed int/long: actual encoding is variable-length.  Just
    // round-trip and assert decode(encode) == value.
    let int_b = PackedIntBinding::new();
    let long_b = PackedLongBinding::new();
    let mut e = DatabaseEntry::new();
    int_b.object_to_entry(&1234i32, &mut e).unwrap();
    assert_eq!(1234i32, int_b.entry_to_object(&e).unwrap());
    long_b.object_to_entry(&1234i64, &mut e).unwrap();
    assert_eq!(1234i64, long_b.entry_to_object(&e).unwrap());

    // Sorted packed int/long similarly.
    let sint_b = SortedPackedIntBinding::new();
    let slong_b = SortedPackedLongBinding::new();
    sint_b.object_to_entry(&1234i32, &mut e).unwrap();
    assert_eq!(1234i32, sint_b.entry_to_object(&e).unwrap());
    slong_b.object_to_entry(&1234i64, &mut e).unwrap();
    assert_eq!(1234i64, slong_b.entry_to_object(&e).unwrap());
}

// ---------------------------------------------------------------------------
// TupleBindingTest: TupleInputBinding-equivalent round-trip (wave 10-A)
// ---------------------------------------------------------------------------

/// Port of `TupleBindingTest.testTupleInputBinding`.
///
/// JE's `TupleInputBinding` is an `EntryBinding<TupleInput>` that copies
/// the input's underlying bytes into the entry verbatim, and on the way
/// back wraps the entry's bytes in a fresh `TupleInput`.
///
/// Noxu has no `TupleInputBinding` type, but `TupleInput::new(entry.data())`
/// is the same operation.  This test asserts the same invariant: writing a
/// string into a `TupleOutput`, copying its bytes into a `DatabaseEntry`,
/// and reading them back via `TupleInput` round-trips with no bytes left
/// over.  In noxu, `"abc"` encodes as 3 payload bytes + 2-byte terminator.
#[test]
fn tck_tuple_binding_test_tuple_input_binding() {
    use noxu_db::DatabaseEntry;

    let mut out = TupleOutput::new();
    out.write_string("abc");
    let mut entry = DatabaseEntry::new();
    entry.set_data_vec(out.into_vec());
    // JE asserts buffer.getSize() == 4 (3 + 1-byte terminator).
    // Noxu's terminator is 2 bytes — 3 + 2 = 5.
    assert_eq!(5, entry.data().len());

    // entryToObject equivalent: TupleInput wrapping the entry's bytes.
    let mut input = TupleInput::new(entry.data());
    assert_eq!("abc", input.read_string().unwrap());
    assert_eq!(0, input.available());
}

// ---------------------------------------------------------------------------
// TupleBindingTest: TupleMarshalledBinding-equivalent round-trip (wave 10-A)
// ---------------------------------------------------------------------------

/// A test analogue of JE's `MarshalledObject`: a value that knows how to
/// marshal itself into a `TupleOutput` and unmarshal from a `TupleInput`.
#[derive(Debug, PartialEq, Eq, Default, Clone)]
struct MarshalledData {
    data: String,
    index_key1: String,
    index_key2: String,
}

impl MarshalledData {
    fn marshal_data(&self, out: &mut TupleOutput) {
        out.write_string(&self.data);
        out.write_string(&self.index_key1);
        out.write_string(&self.index_key2);
    }

    fn unmarshal_data(input: &mut TupleInput) -> Self {
        let data = input.read_string().unwrap();
        let index_key1 = input.read_string().unwrap();
        let index_key2 = input.read_string().unwrap();
        Self { data, index_key1, index_key2 }
    }

    /// Expected encoded size: each string contributes len + 2 bytes
    /// (3 components encoded back-to-back).
    fn expected_data_length(&self) -> usize {
        self.data.len()
            + 2
            + self.index_key1.len()
            + 2
            + self.index_key2.len()
            + 2
    }
}

/// Port of `TupleBindingTest.testTupleMarshalledBinding`.
///
/// Builds a `TupleBinding<MarshalledData>` whose `object_to_tuple` /
/// `tuple_to_object` delegate to the marshalled value's own marshal /
/// unmarshal methods.  Asserts:
///   1. encoded size matches `expected_data_length`,
///   2. round-trip preserves the `data` field (and the rest).
#[test]
fn tck_tuple_binding_test_tuple_marshalled_binding() {
    use noxu_bind::{EntryBinding, TupleBinding};
    use noxu_db::DatabaseEntry;

    struct MarshalledBinding;
    impl EntryBinding<MarshalledData> for MarshalledBinding {
        fn entry_to_object(
            &self,
            entry: &DatabaseEntry,
        ) -> noxu_bind::Result<MarshalledData> {
            let mut input = TupleInput::new(entry.data());
            self.tuple_to_object(&mut input)
        }
        fn object_to_entry(
            &self,
            object: &MarshalledData,
            entry: &mut DatabaseEntry,
        ) -> noxu_bind::Result<()> {
            let mut out = TupleOutput::new();
            self.object_to_tuple(object, &mut out)?;
            entry.set_data_vec(out.into_vec());
            Ok(())
        }
    }
    impl TupleBinding<MarshalledData> for MarshalledBinding {
        fn tuple_to_object(
            &self,
            input: &mut TupleInput,
        ) -> noxu_bind::Result<MarshalledData> {
            Ok(MarshalledData::unmarshal_data(input))
        }
        fn object_to_tuple(
            &self,
            object: &MarshalledData,
            output: &mut TupleOutput,
        ) -> noxu_bind::Result<()> {
            object.marshal_data(output);
            Ok(())
        }
    }

    let val = MarshalledData {
        data: "abc".to_string(),
        index_key1: String::new(),
        index_key2: String::new(),
    };
    let binding = MarshalledBinding;
    let mut entry = DatabaseEntry::new();
    binding.object_to_entry(&val, &mut entry).unwrap();
    assert_eq!(val.expected_data_length(), entry.data().len());

    let got = binding.entry_to_object(&entry).unwrap();
    assert_eq!("abc", got.data);
    assert_eq!(val, got);
}

// ---------------------------------------------------------------------------
// TupleBindingTest: TupleTupleMarshalledBinding-equivalent (wave 10-A)
// ---------------------------------------------------------------------------

/// Marshalled entity with a separate primary key field.  Mirrors JE's
/// `MarshalledObject` when used through `TupleTupleMarshalledBinding`.
#[derive(Debug, PartialEq, Eq, Default, Clone)]
struct MarshalledEntity {
    data: String,
    primary_key: String,
    index_key1: String,
    index_key2: String,
}

impl MarshalledEntity {
    fn expected_data_length(&self) -> usize {
        self.data.len()
            + 2
            + self.index_key1.len()
            + 2
            + self.index_key2.len()
            + 2
    }
    fn expected_key_length(&self) -> usize {
        self.primary_key.len() + 2
    }
}

/// Port of `TupleBindingTest.testTupleTupleMarshalledBinding`.
///
/// Defines an `EntityBinding<MarshalledEntity>` that splits the value across
/// a `key` entry (primary key) and a `data` entry (data + index keys).
/// Asserts:
///   1. encoded data size matches `expected_data_length`,
///   2. encoded key size matches `expected_key_length`,
///   3. round-trip preserves all four fields.
#[test]
fn tck_tuple_binding_test_tuple_tuple_marshalled_binding() {
    use noxu_bind::EntityBinding;
    use noxu_db::DatabaseEntry;

    struct MarshalledEntityBinding;
    impl EntityBinding<MarshalledEntity> for MarshalledEntityBinding {
        fn entry_to_object(
            &self,
            key: &DatabaseEntry,
            data: &DatabaseEntry,
        ) -> noxu_bind::Result<MarshalledEntity> {
            let mut k = TupleInput::new(key.data());
            let mut d = TupleInput::new(data.data());
            let primary_key = k.read_string().unwrap();
            let dat = d.read_string().unwrap();
            let i1 = d.read_string().unwrap();
            let i2 = d.read_string().unwrap();
            Ok(MarshalledEntity {
                data: dat,
                primary_key,
                index_key1: i1,
                index_key2: i2,
            })
        }
        fn object_to_key(
            &self,
            object: &MarshalledEntity,
            key: &mut DatabaseEntry,
        ) -> noxu_bind::Result<()> {
            let mut out = TupleOutput::new();
            out.write_string(&object.primary_key);
            key.set_data_vec(out.into_vec());
            Ok(())
        }
        fn object_to_data(
            &self,
            object: &MarshalledEntity,
            data: &mut DatabaseEntry,
        ) -> noxu_bind::Result<()> {
            let mut out = TupleOutput::new();
            out.write_string(&object.data);
            out.write_string(&object.index_key1);
            out.write_string(&object.index_key2);
            data.set_data_vec(out.into_vec());
            Ok(())
        }
    }

    let val = MarshalledEntity {
        data: "abc".to_string(),
        primary_key: "primary".to_string(),
        index_key1: "index1".to_string(),
        index_key2: "index2".to_string(),
    };
    let binding = MarshalledEntityBinding;
    let mut key_entry = DatabaseEntry::new();
    let mut data_entry = DatabaseEntry::new();
    binding.object_to_data(&val, &mut data_entry).unwrap();
    assert_eq!(val.expected_data_length(), data_entry.data().len());
    binding.object_to_key(&val, &mut key_entry).unwrap();
    assert_eq!(val.expected_key_length(), key_entry.data().len());

    let got = binding.entry_to_object(&key_entry, &data_entry).unwrap();
    assert_eq!("abc", got.data);
    assert_eq!("primary", got.primary_key);
    assert_eq!("index1", got.index_key1);
    assert_eq!("index2", got.index_key2);
    assert_eq!(val, got);
}