linera-witty 0.15.21

Generation of WIT compatible host code from Rust code
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
// Copyright (c) Zefchain Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

//! Tests for the `WitStore` derive macro.

#[path = "common/types.rs"]
mod types;

use std::{fmt::Debug, iter, rc::Rc, sync::Arc};

use linera_witty::{hlist, InstanceWithMemory, Layout, MockInstance, WitStore};

use self::types::{
    Branch, Enum, Leaf, RecordWithDoublePadding, SimpleWrapper, SliceWrapper,
    SpecializedGenericEnum, SpecializedGenericStruct, StructWithHeapFields, StructWithLists,
    TupleWithPadding, TupleWithoutPadding,
};

/// Checks that a wrapper type is properly stored in memory and lowered into its flat layout.
#[test]
fn test_simple_bool_wrapper() {
    test_store_in_memory(SimpleWrapper(true), &[1], &[]);
    test_store_in_memory(SimpleWrapper(false), &[0], &[]);

    test_lower_to_flat_layout(SimpleWrapper(true), hlist![1], &[]);
    test_lower_to_flat_layout(SimpleWrapper(false), hlist![0], &[]);
}

/// Checks that a type with multiple fields ordered in a way that doesn't require any padding is
/// properly stored in memory and lowered into its flat layout.
#[test]
fn test_tuple_struct_without_padding() {
    let data = TupleWithoutPadding(0x0123_4567_89ab_cdef_u64, 0x0011_2233_i32, 0x4455_i16);

    test_store_in_memory(
        data,
        &[
            0xef, 0xcd, 0xab, 0x89, 0x67, 0x45, 0x23, 0x01, 0x33, 0x22, 0x11, 0x00, 0x55, 0x44, 0,
            0,
        ],
        &[],
    );
    test_lower_to_flat_layout(
        data,
        hlist![0x0123_4567_89ab_cdef_i64, 0x0011_2233_i32, 0x0000_4455_i32],
        &[],
    );
}

/// Checks that a type with multiple fields ordered in a way that requires padding between two of its
/// fields is properly stored in memory and lowered into its flat layout.
#[test]
fn test_tuple_struct_with_padding() {
    let data = TupleWithPadding(0x0123_u16, 0x4567_89ab_u32, 0x0011_2233_4455_6677_i64);

    test_store_in_memory(
        data,
        &[
            0x23, 0x01, 0, 0, 0xab, 0x89, 0x67, 0x45, 0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11,
            0x00,
        ],
        &[],
    );
    test_lower_to_flat_layout(
        data,
        hlist![0x0000_0123_i32, 0x4567_89ab_i32, 0x0011_2233_4455_6677_i64],
        &[],
    );
}

/// Checks that a type with multiple named fields ordered in a way that requires padding before two
/// fields is properly stored in memory and lowered into its flat layout.
#[test]
fn test_named_struct_with_double_padding() {
    let data = RecordWithDoublePadding {
        first: 0x0123_u16,
        second: 0x0011_2233_u32,
        third: 0x45_i8,
        fourth: 0x6789_abcd_ef44_5566_i64,
    };

    test_store_in_memory(
        data,
        &[
            0x23, 0x01, 0, 0, 0x33, 0x22, 0x11, 0x00, 0x45, 0, 0, 0, 0, 0, 0, 0, 0x66, 0x55, 0x44,
            0xef, 0xcd, 0xab, 0x89, 0x67,
        ],
        &[],
    );
    test_lower_to_flat_layout(
        data,
        hlist![
            0x0000_0123_i32,
            0x0011_2233_i32,
            0x0000_0045_i32,
            0x6789_abcd_ef44_5566_i64,
        ],
        &[],
    );
}

/// Checks that a type that contains a field with a type that also has `WitStore` derived for it is
/// properly stored in memory and lowered into its flat layout.
#[test]
fn test_nested_types() {
    let data = Branch {
        tag: 0x0123_u16,
        first_leaf: Leaf {
            first: false,
            second: 0x4567_89ab_cdef_0011_2233_4455_6677_8899_u128,
        },
        second_leaf: Leaf {
            first: true,
            second: 0xaabb_ccdd_eeff_0f1e_2d3c_4b5a_6978_8796_u128,
        },
    };

    test_store_in_memory(
        data,
        &[
            0x23, 0x01, 0, 0, 0, 0, 0, 0, 0x00, 0, 0, 0, 0, 0, 0, 0, 0x99, 0x88, 0x77, 0x66, 0x55,
            0x44, 0x33, 0x22, 0x11, 0x00, 0xef, 0xcd, 0xab, 0x89, 0x67, 0x45, 0x01, 0, 0, 0, 0, 0,
            0, 0, 0x96, 0x87, 0x78, 0x69, 0x5a, 0x4b, 0x3c, 0x2d, 0x1e, 0x0f, 0xff, 0xee, 0xdd,
            0xcc, 0xbb, 0xaa,
        ],
        &[],
    );
    test_lower_to_flat_layout(
        data,
        hlist![
            0x0000_0123_i32,
            0x0000_0000_i32,
            0x2233_4455_6677_8899_i64,
            0x4567_89ab_cdef_0011_i64,
            0x0000_0001_i32,
            0x2d3c_4b5a_6978_8796_i64,
            0xaabb_ccdd_eeff_0f1e_u64 as i64,
        ],
        &[],
    );
}

/// Checks that an enum type's variants are properly stored in memory and lowered into its flat
/// layout.
#[test]
fn test_enum_type() {
    let data = Enum::Empty;

    test_store_in_memory(
        data,
        &[
            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
            0x00, 0x00, 0x00, 0x00, 0, 0, 0, 0, 0, 0,
        ],
        &[],
    );
    test_lower_to_flat_layout(
        data,
        hlist![
            0x0000_0000_i32,
            0x0000_0000_0000_0000_i64,
            0x0000_0000_i32,
            0x0000_0000_i32,
            0x0000_0000_i32,
            0x0000_0000_i32,
            0x0000_0000_i32,
            0x0000_0000_i32,
            0x0000_0000_i32,
            0x0000_0000_i32,
            0x0000_0000_i32,
        ],
        &[],
    );

    let data = Enum::LargeVariantWithLooseAlignment(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);

    test_store_in_memory(
        data,
        &[
            0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05,
            0x06, 0x07, 0x08, 0x09, 0, 0, 0, 0, 0, 0,
        ],
        &[],
    );
    test_lower_to_flat_layout(
        data,
        hlist![
            0x0000_0001_i32,
            0x0000_0000_0000_0000_i64,
            0x0000_0001_i32,
            0x0000_0002_i32,
            0x0000_0003_i32,
            0x0000_0004_i32,
            0x0000_0005_i32,
            0x0000_0006_i32,
            0x0000_0007_i32,
            0x0000_0008_i32,
            0x0000_0009_i32,
        ],
        &[],
    );

    let data = Enum::SmallerVariantWithStrictAlignment {
        inner: 0x0102_0304_0506_0708_u64,
    };

    test_store_in_memory(
        data,
        &[
            0x02, 0, 0, 0, 0, 0, 0, 0, 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0, 0, 0, 0,
            0, 0, 0, 0,
        ],
        &[],
    );
    test_lower_to_flat_layout(
        data,
        hlist![
            0x0000_0002_i32,
            0x0102_0304_0506_0708_i64,
            0x0000_0000_i32,
            0x0000_0000_i32,
            0x0000_0000_i32,
            0x0000_0000_i32,
            0x0000_0000_i32,
            0x0000_0000_i32,
            0x0000_0000_i32,
            0x0000_0000_i32,
            0x0000_0000_i32,
        ],
        &[],
    );
}

/// Checks that a generic type with a specialization request is properly stored in memory and
/// lowered into its flat layout.
#[test]
fn test_specialized_generic_struct() {
    let data = SpecializedGenericStruct {
        first: 200_u8,
        second: -200_i16,
        both: vec![(4, -4), (3, -3), (2, -2), (1, -1)],
    };

    let expected_heap = [
        0x04, 0, 0xfc, 0xff, 0x03, 0, 0xfd, 0xff, 0x02, 0, 0xfe, 0xff, 0x01, 0, 0xff, 0xff,
    ];

    test_store_in_memory(
        data.clone(),
        &[
            0xc8, 0, 0x38, 0xff, 0x0c, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
        ],
        &expected_heap,
    );
    test_lower_to_flat_layout(
        data,
        hlist![0x0000_00c8_i32, -200_i32, 0x0000_0000_i32, 0x0000_0004_i32],
        &expected_heap,
    );
}

/// Checks that a generic enum with a specialization request type's variants are properly stored in
/// memory and lowered into its flat layout.
#[test]
fn test_specialized_generic_enum_type() {
    let data = SpecializedGenericEnum::None;

    test_store_in_memory(
        data,
        &[
            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        ],
        &[],
    );
    test_lower_to_flat_layout(
        data,
        hlist![0x0000_0000_i32, 0x0000_0000_i32, 0x0000_0000_i32],
        &[],
    );

    let data = SpecializedGenericEnum::First(None);

    test_store_in_memory(
        data,
        &[
            0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        ],
        &[],
    );
    test_lower_to_flat_layout(
        data,
        hlist![0x0000_0001_i32, 0x0000_0000_i32, 0x0000_0000_i32],
        &[],
    );

    let data = SpecializedGenericEnum::First(Some(false));

    test_store_in_memory(
        data,
        &[
            0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        ],
        &[],
    );
    test_lower_to_flat_layout(
        data,
        hlist![0x0000_0001_i32, 0x0000_0001_i32, 0x0000_0000_i32],
        &[],
    );

    let data = SpecializedGenericEnum::First(Some(true));

    test_store_in_memory(
        data,
        &[
            0x01, 0x00, 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        ],
        &[],
    );
    test_lower_to_flat_layout(
        data,
        hlist![0x0000_0001_i32, 0x0000_0001_i32, 0x0000_0001_i32],
        &[],
    );

    let data = SpecializedGenericEnum::MaybeSecond { maybe: None };

    test_store_in_memory(
        data,
        &[
            0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        ],
        &[],
    );
    test_lower_to_flat_layout(
        data,
        hlist![0x0000_0002_i32, 0x0000_0000_i32, 0x0000_0000_i32],
        &[],
    );

    let data = SpecializedGenericEnum::MaybeSecond { maybe: Some(9) };

    test_store_in_memory(
        data,
        &[
            0x02, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00,
        ],
        &[],
    );
    test_lower_to_flat_layout(
        data,
        hlist![0x0000_0002_i32, 0x0000_0001_i32, 0x0000_0009_i32],
        &[],
    );
}

/// Checks that a type with fields stored in the heap is properly stored in memory and lowered into
/// its flat layout.
#[test]
fn test_heap_allocated_fields() {
    let data = StructWithHeapFields {
        boxed: Box::new(SimpleWrapper(false)),
        rced: Rc::new(Leaf {
            first: true,
            second: 0x7071_7273_7475_7677_7879_7a7b_7c7d_7e7f_u128,
        }),
        arced: Arc::new(Enum::SmallerVariantWithStrictAlignment {
            inner: 0x4041_4243_4445_4647_u64,
        }),
    };

    test_store_in_memory(
        data.clone(),
        &[
            0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0x7f, 0x7e, 0x7d, 0x7c, 0x7b, 0x7a,
            0x79, 0x78, 0x77, 0x76, 0x75, 0x74, 0x73, 0x72, 0x71, 0x70, 2, 0, 0, 0, 0, 0, 0, 0,
            0x47, 0x46, 0x45, 0x44, 0x43, 0x42, 0x41, 0x40, 0, 0, 0, 0, 0, 0, 0, 0,
        ],
        &[],
    );
    test_lower_to_flat_layout(
        data,
        hlist![
            0_i32,
            1_i32,
            0x7879_7a7b_7c7d_7e7f_i64,
            0x7071_7273_7475_7677_i64,
            2_i32,
            0x4041_4243_4445_4647_i64,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
        ],
        &[],
    );
}

/// Check that a slice type is properly stored in memory and lowered into its flat layout.
#[test]
fn test_slice() {
    let data = [
        SimpleWrapper(false),
        SimpleWrapper(false),
        SimpleWrapper(true),
        SimpleWrapper(true),
    ];

    test_store_in_memory(data.as_slice(), &[8, 0, 0, 0, 4, 0, 0, 0], &[0, 0, 1, 1]);
    test_lower_to_flat_layout(data.as_slice(), hlist![0_i32, 4_i32,], &[0, 0, 1, 1]);
}

/// Checks that a [`Vec`] type is properly stored in memory and lowered into its flat layout.
#[test]
fn test_vec() {
    let data = vec![
        SimpleWrapper(true),
        SimpleWrapper(false),
        SimpleWrapper(true),
    ];

    test_store_in_memory(data.clone(), &[8, 0, 0, 0, 3, 0, 0, 0], &[1, 0, 1]);
    test_lower_to_flat_layout(data, hlist![0_i32, 3_i32,], &[1, 0, 1]);
}

/// Check that a boxed slice type is properly stored in memory and lowered into its flat layout.
#[test]
fn test_boxed_slice() {
    let data: Box<[Enum]> = Box::new([
        Enum::LargeVariantWithLooseAlignment(10, 20, 30, 40, 50, 60, 70, 80, 90, 100),
        Enum::Empty,
        Enum::Empty,
        Enum::SmallerVariantWithStrictAlignment { inner: 0xFFFF_FFFF },
    ]);

    let heap_memory = iter::empty()
        .chain(
            iter::empty()
                .chain([1])
                .chain([0; 7])
                .chain([10, 20, 30, 40, 50, 60, 70, 80, 90, 100])
                .chain([0; 6]),
        )
        .chain(iter::empty().chain([0]).chain([0; 23]))
        .chain(iter::empty().chain([0]).chain([0; 23]))
        .chain(
            iter::empty()
                .chain([2])
                .chain([0; 7])
                .chain([0xff, 0xff, 0xff, 0xff])
                .chain([0; 12]),
        )
        .collect::<Vec<u8>>();

    test_store_in_memory(data.clone(), &[8, 0, 0, 0, 4, 0, 0, 0], &heap_memory);
    test_lower_to_flat_layout(data, hlist![0_i32, 4_i32,], &heap_memory);
}

/// Check that a rc-ed slice type is properly stored in memory and lowered into its flat layout.
#[test]
fn test_rced_slice() {
    let data: Rc<[Leaf]> = Rc::new([
        Leaf {
            first: true,
            second: 0x0011_2233_4455_6677_8899_aabb_ccdd_eeff,
        },
        Leaf {
            first: false,
            second: 0xffee_ddcc_bbaa_9988_7766_5544_3322_1100,
        },
    ]);

    let heap_memory = iter::empty()
        .chain(iter::empty().chain([1]).chain([0; 7]).chain([
            0xff, 0xee, 0xdd, 0xcc, 0xbb, 0xaa, 0x99, 0x88, 0x77, 0x66, 0x55, 0x44, 0x33, 0x22,
            0x11, 0x00,
        ]))
        .chain(iter::empty().chain([0]).chain([0; 7]).chain([
            0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd,
            0xee, 0xff,
        ]))
        .collect::<Vec<u8>>();

    test_store_in_memory(data.clone(), &[8, 0, 0, 0, 2, 0, 0, 0], &heap_memory);
    test_lower_to_flat_layout(data, hlist![0_i32, 2_i32,], &heap_memory);
}

/// Check that a rc-ed slice type is properly stored in memory and lowered into its flat layout.
#[test]
fn test_arced_slice() {
    let data: Arc<[RecordWithDoublePadding]> = Arc::new([
        RecordWithDoublePadding {
            first: 0x0300,
            second: 0x4422_1100,
            third: -2,
            fourth: -3,
        },
        RecordWithDoublePadding {
            first: 32_767,
            second: 9,
            third: 127,
            fourth: -32_768,
        },
    ]);

    let heap_memory = iter::empty()
        .chain(
            iter::empty()
                .chain([0x00, 0x03])
                .chain([0; 2])
                .chain([0x00, 0x11, 0x22, 0x44])
                .chain([0xfe])
                .chain([0; 7])
                .chain([0xfd, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]),
        )
        .chain(
            iter::empty()
                .chain([0xff, 0x7f])
                .chain([0; 2])
                .chain([9, 0, 0, 0])
                .chain([0x7f])
                .chain([0; 7])
                .chain([0x00, 0x80, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]),
        )
        .collect::<Vec<u8>>();

    test_store_in_memory(data.clone(), &[8, 0, 0, 0, 2, 0, 0, 0], &heap_memory);
    test_lower_to_flat_layout(data, hlist![0_i32, 2_i32,], &heap_memory);
}

/// Check that a type with a slice field is properly stored in memory and lowered into its
/// flat layout.
#[test]
fn test_slice_field() {
    let slice = [
        TupleWithoutPadding(0, 1, 2),
        TupleWithoutPadding(3, 4, 5),
        TupleWithoutPadding(6, 7, 8),
    ];
    let data = SliceWrapper(&slice);

    let expected_memory = iter::empty()
        .chain(
            iter::empty()
                .chain([0, 0, 0, 0, 0, 0, 0, 0])
                .chain([1, 0, 0, 0])
                .chain([2, 0])
                .chain([0; 2]),
        )
        .chain(
            iter::empty()
                .chain([3, 0, 0, 0, 0, 0, 0, 0])
                .chain([4, 0, 0, 0])
                .chain([5, 0])
                .chain([0; 2]),
        )
        .chain(
            iter::empty()
                .chain([6, 0, 0, 0, 0, 0, 0, 0])
                .chain([7, 0, 0, 0])
                .chain([8, 0])
                .chain([0; 2]),
        )
        .collect::<Vec<u8>>();

    test_store_in_memory(data, &[8, 0, 0, 0, 3, 0, 0, 0], &expected_memory);
    test_lower_to_flat_layout(data, hlist![0_i32, 3_i32], &expected_memory);
}

/// Checks that a type with list fields is properly stored in memory and lowered into its
/// flat layout.
#[test]
fn test_list_fields() {
    let data = StructWithLists {
        vec: vec![
            SimpleWrapper(false),
            SimpleWrapper(true),
            SimpleWrapper(false),
            SimpleWrapper(true),
        ],
        boxed_slice: Box::new([TupleWithPadding(1, 0, -1), TupleWithPadding(10, 11, 12)]),
        rced_slice: Rc::new([
            Leaf {
                first: true,
                second: 0x0011_2233_4455_6677_8899_aabb_ccdd_eeff,
            },
            Leaf {
                first: false,
                second: 0xffee_ddcc_bbaa_9988_7766_5544_3322_1100,
            },
            Leaf {
                first: false,
                second: 0xf0e1_d2c3_b4a5_9687_7869_5a4b_3c2d_1e0f,
            },
        ]),
        arced_slice: Arc::new([
            RecordWithDoublePadding {
                first: 0x1020,
                second: 0x0a0b_0c0d,
                third: 0x7a,
                fourth: -0x0abb_ccdd_eeff_0011,
            },
            RecordWithDoublePadding {
                first: 0x1525,
                second: 0x8191_a1b1,
                third: -0x7a,
                fourth: 0x0abb_ccdd_eeff_0011,
            },
        ]),
    };

    let vec_contents = [0, 1, 0, 1];

    let boxed_contents = iter::empty()
        .chain(
            iter::empty()
                .chain([1, 0])
                .chain([0; 2])
                .chain([0, 0, 0, 0])
                .chain([0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]),
        )
        .chain(
            iter::empty()
                .chain([10, 0])
                .chain([0; 2])
                .chain([11, 0, 0, 0])
                .chain([12, 0, 0, 0, 0, 0, 0, 0]),
        );

    let rced_contents = iter::empty()
        .chain(iter::empty().chain([1]).chain([0; 7]).chain([
            0xff, 0xee, 0xdd, 0xcc, 0xbb, 0xaa, 0x99, 0x88, 0x77, 0x66, 0x55, 0x44, 0x33, 0x22,
            0x11, 0x00,
        ]))
        .chain(iter::empty().chain([0]).chain([0; 7]).chain([
            0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd,
            0xee, 0xff,
        ]))
        .chain(iter::empty().chain([0]).chain([0; 7]).chain([
            0x0f, 0x1e, 0x2d, 0x3c, 0x4b, 0x5a, 0x69, 0x78, 0x87, 0x96, 0xa5, 0xb4, 0xc3, 0xd2,
            0xe1, 0xf0,
        ]));

    let arced_contents = iter::empty()
        .chain(
            iter::empty()
                .chain([0x20, 0x10])
                .chain([0; 2])
                .chain([0x0d, 0x0c, 0x0b, 0x0a])
                .chain([0x7a])
                .chain([0; 7])
                .chain([0xef, 0xff, 0x00, 0x11, 0x22, 0x33, 0x44, 0xf5]),
        )
        .chain(
            iter::empty()
                .chain([0x25, 0x15])
                .chain([0; 2])
                .chain([0xb1, 0xa1, 0x91, 0x81])
                .chain([0x86])
                .chain([0; 7])
                .chain([0x11, 0x00, 0xff, 0xee, 0xdd, 0xcc, 0xbb, 0x0a]),
        );

    let expected_heap = iter::empty()
        .chain(vec_contents)
        .chain([0; 4])
        .chain(boxed_contents)
        .chain(rced_contents)
        .chain(arced_contents)
        .collect::<Vec<_>>();

    test_store_in_memory(
        data.clone(),
        &[
            32, 0, 0, 0, 4, 0, 0, 0, 40, 0, 0, 0, 2, 0, 0, 0, 72, 0, 0, 0, 3, 0, 0, 0, 144, 0, 0,
            0, 2, 0, 0, 0,
        ],
        &expected_heap,
    );
    test_lower_to_flat_layout(
        data,
        hlist![0_i32, 4_i32, 8_i32, 2_i32, 40_i32, 3_i32, 112_i32, 2_i32],
        &expected_heap,
    );
}

/// Tests that the `data` of type `T` and wrapped versions of it can be stored as a sequence of
/// bytes in memory and that they match the `expected` bytes.
fn test_store_in_memory<T>(
    data: T,
    expected_without_allocation: &[u8],
    expected_additionally_allocated: &[u8],
) where
    T: Clone + WitStore,
{
    test_single_store_in_memory(
        &data,
        expected_without_allocation,
        expected_additionally_allocated,
    );
    test_single_store_in_memory(
        &Box::new(data.clone()),
        expected_without_allocation,
        expected_additionally_allocated,
    );
    test_single_store_in_memory(
        &Rc::new(data),
        expected_without_allocation,
        expected_additionally_allocated,
    );
}

/// Tests that the `data` of type `T` can be stored as a sequence of bytes in memory and that it
/// matches the `expected` bytes.
fn test_single_store_in_memory<T>(
    data: &T,
    expected_without_allocation: &[u8],
    expected_additionally_allocated: &[u8],
) where
    T: WitStore,
{
    let mut instance = MockInstance::<()>::default();
    let mut memory = instance.memory().unwrap();

    let length = expected_without_allocation.len() as u32;
    let address = memory.allocate(length, 1).unwrap();

    data.store(&mut memory, address).unwrap();

    let additional_allocations_address = address.after::<T>();
    let additional_allocations_length = expected_additionally_allocated.len() as u32;

    assert_eq!(
        memory.read(address, length).unwrap(),
        expected_without_allocation
    );
    assert_eq!(
        memory
            .read(
                additional_allocations_address,
                additional_allocations_length
            )
            .unwrap(),
        expected_additionally_allocated
    );
}

/// Tests that the `data` of type `T` and wrapped versions of it can be lowered to their flat layout
/// and that they match the `expected` value.
fn test_lower_to_flat_layout<T>(
    data: T,
    expected: <T::Layout as Layout>::Flat,
    expected_memory: &[u8],
) where
    T: Clone + WitStore,
    <T::Layout as Layout>::Flat: Copy + Debug + Eq,
{
    test_single_lower_to_flat_layout(&data, &expected, expected_memory);
    test_single_lower_to_flat_layout(&Box::new(data.clone()), &expected, expected_memory);
    test_single_lower_to_flat_layout(&Rc::new(data.clone()), &expected, expected_memory);
    test_single_lower_to_flat_layout(&Arc::new(data), &expected, expected_memory);
}

/// Tests that the `data` of type `T` can be lowered to its flat layout and that it matches the
/// `expected` value.
fn test_single_lower_to_flat_layout<T>(
    data: &T,
    expected: &<T::Layout as Layout>::Flat,
    expected_memory: &[u8],
) where
    T: WitStore,
    <T::Layout as Layout>::Flat: Debug + Eq,
{
    let mut instance = MockInstance::<()>::default();
    let mut memory = instance.memory().unwrap();

    assert_eq!(data.lower(&mut memory).unwrap(), *expected);
    assert_eq!(&instance.memory_contents(), expected_memory);
}