• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // This file is part of ICU4X. For terms of use, please see the file
2 // called LICENSE at the top level of the ICU4X source tree
3 // (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).
4 
5 //! Example data useful for testing ZeroVec.
6 
7 // This module is included directly in tests and can trigger the dead_code
8 // warning since not all samples are used in each test
9 #![allow(dead_code)]
10 
11 #[repr(align(8))]
12 struct Aligned<T>(pub T);
13 
14 // This is aligned so that we can test unaligned behavior at odd offsets
15 const ALIGNED_TEST_BUFFER_LE: Aligned<[u8; 80]> = Aligned([
16     0x00, 0x01, 0x02, 0x00, 0x04, 0x05, 0x06, 0x00, 0x08, 0x09, 0x0a, 0x00, 0x0c, 0x0d, 0x0e, 0x00,
17     0x10, 0x11, 0x12, 0x00, 0x14, 0x15, 0x16, 0x00, 0x18, 0x19, 0x1a, 0x00, 0x1c, 0x1d, 0x1e, 0x00,
18     0x20, 0x21, 0x22, 0x00, 0x24, 0x25, 0x26, 0x00, 0x28, 0x29, 0x2a, 0x00, 0x2c, 0x2d, 0x2e, 0x00,
19     0x30, 0x31, 0x32, 0x00, 0x34, 0x35, 0x36, 0x00, 0x38, 0x39, 0x3a, 0x00, 0x3c, 0x3d, 0x3e, 0x00,
20     0x40, 0x41, 0x42, 0x00, 0x44, 0x45, 0x46, 0x00, 0x48, 0x49, 0x4a, 0x00, 0x4c, 0x4d, 0x4e, 0x00,
21 ]);
22 
23 /// An example byte array intended to be used in `ZeroVec<u32>`.
24 pub const TEST_BUFFER_LE: &[u8] = &ALIGNED_TEST_BUFFER_LE.0;
25 
26 /// u32 numbers corresponding to the above byte array.
27 pub const TEST_SLICE: &[u32] = &[
28     0x020100, 0x060504, 0x0a0908, 0x0e0d0c, 0x121110, 0x161514, 0x1a1918, 0x1e1d1c, 0x222120,
29     0x262524, 0x2a2928, 0x2e2d2c, 0x323130, 0x363534, 0x3a3938, 0x3e3d3c, 0x424140, 0x464544,
30     0x4a4948, 0x4e4d4c,
31 ];
32 
33 /// The sum of the numbers in TEST_SLICE.
34 pub const TEST_SUM: u32 = 52629240;
35 
36 /// Representation of TEST_SLICE in JSON.
37 pub const JSON_STR: &str = "[131328,394500,657672,920844,1184016,1447188,1710360,1973532,2236704,2499876,2763048,3026220,3289392,3552564,3815736,4078908,4342080,4605252,4868424,5131596]";
38 
39 /// Representation of TEST_SLICE in Bincode.
40 pub const BINCODE_BUF: &[u8] = &[
41     80, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 0, 4, 5, 6, 0, 8, 9, 10, 0, 12, 13, 14, 0, 16, 17, 18, 0, 20,
42     21, 22, 0, 24, 25, 26, 0, 28, 29, 30, 0, 32, 33, 34, 0, 36, 37, 38, 0, 40, 41, 42, 0, 44, 45,
43     46, 0, 48, 49, 50, 0, 52, 53, 54, 0, 56, 57, 58, 0, 60, 61, 62, 0, 64, 65, 66, 0, 68, 69, 70,
44     0, 72, 73, 74, 0, 76, 77, 78, 0,
45 ];
46 
47 /// Representation of a VarZeroVec<str> with contents ["w", "ω", "文", "��"]
48 pub const TEST_VARZEROSLICE_BYTES: &[u8] = &[
49     4, 0, 0, 0, 0, 0, 1, 0, 3, 0, 6, 0, 119, 207, 137, 230, 150, 135, 240, 145, 132, 131,
50 ];
51 
52 #[test]
validate()53 fn validate() {
54     use crate::{VarZeroVec, ZeroVec};
55 
56     assert_eq!(
57         ZeroVec::<u32>::parse_bytes(TEST_BUFFER_LE).unwrap(),
58         ZeroVec::alloc_from_slice(TEST_SLICE)
59     );
60 
61     assert_eq!(TEST_SLICE.iter().sum::<u32>(), TEST_SUM);
62 
63     assert_eq!(
64         serde_json::from_str::<ZeroVec::<u32>>(JSON_STR).unwrap(),
65         ZeroVec::alloc_from_slice(TEST_SLICE)
66     );
67 
68     assert_eq!(
69         bincode::deserialize::<ZeroVec::<u32>>(BINCODE_BUF).unwrap(),
70         ZeroVec::alloc_from_slice(TEST_SLICE)
71     );
72 
73     VarZeroVec::<str>::parse_bytes(TEST_VARZEROSLICE_BYTES).unwrap();
74 }
75