1
+ use core :: array :: ArrayTrait ;
1
2
use core :: fmt :: {Debug , Formatter };
3
+ use super :: util :: one_shift_left_bytes_u256 ;
2
4
3
5
/// A byte array with storage format similar to `core::ByteArray`, but
4
6
/// suitable for reading data from it.
@@ -35,8 +37,13 @@ pub impl ByteArrayImpl of ByteArrayTrait {
35
37
if data . len () == 0 {
36
38
assert! (num_last_bytes == 0 );
37
39
} else {
40
+ assert! (num_last_bytes > 0 );
38
41
assert! (num_last_bytes <= 31 );
39
- // TODO: check that unused bytes are zeroed.
42
+ let last : u256 = (* data . at (data . len () - 1 )). into ();
43
+ assert! (
44
+ last / one_shift_left_bytes_u256 (num_last_bytes ) == 0 ,
45
+ " ByteArrayImpl::new: last value is too large"
46
+ );
40
47
}
41
48
ByteArray { num_last_bytes , data }
42
49
}
@@ -62,3 +69,100 @@ pub impl ByteArrayImpl of ByteArrayTrait {
62
69
}
63
70
}
64
71
}
72
+
73
+ #[cfg(test)]
74
+ mod tests {
75
+ use super :: {ByteArray , ByteArrayImpl };
76
+ use pyth :: util :: array_felt252_to_bytes31;
77
+
78
+ #[test]
79
+ fn empty_byte_array () {
80
+ let mut array = ByteArrayImpl :: new (array! [], 0 );
81
+ assert! (array . len () == 0 );
82
+ assert! (array . pop_front () == Option :: None );
83
+ }
84
+
85
+ #[test]
86
+ fn byte_array_3_zeros () {
87
+ let mut array = ByteArrayImpl :: new (array_felt252_to_bytes31 (array! [0 ]), 3 );
88
+ assert! (array . len () == 3 );
89
+ assert! (array . pop_front () == Option :: Some ((0. try_into (). unwrap (), 3 )));
90
+ assert! (array . len () == 0 );
91
+ assert! (array . pop_front () == Option :: None );
92
+ }
93
+
94
+ #[test]
95
+ fn byte_array_3_bytes () {
96
+ let mut array = ByteArrayImpl :: new (array_felt252_to_bytes31 (array! [0x010203 ]), 3 );
97
+ assert! (array . len () == 3 );
98
+ assert! (array . pop_front () == Option :: Some ((0x010203 . try_into (). unwrap (), 3 )));
99
+ assert! (array . len () == 0 );
100
+ assert! (array . pop_front () == Option :: None );
101
+ }
102
+
103
+ #[test]
104
+ fn byte_array_single_full () {
105
+ let value_31_bytes = 0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f ;
106
+ let mut array = ByteArrayImpl :: new (array_felt252_to_bytes31 (array! [value_31_bytes ]), 31 );
107
+ assert! (array . len () == 31 );
108
+ assert! (array . pop_front () == Option :: Some ((value_31_bytes . try_into (). unwrap (), 31 )));
109
+ assert! (array . len () == 0 );
110
+ assert! (array . pop_front () == Option :: None );
111
+ }
112
+
113
+ #[test]
114
+ fn byte_array_two_full () {
115
+ let value_31_bytes = 0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f ;
116
+ let value2_31_bytes = 0x2122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f ;
117
+ let mut array = ByteArrayImpl :: new (
118
+ array_felt252_to_bytes31 (array! [value_31_bytes , value2_31_bytes ]), 31
119
+ );
120
+ assert! (array . len () == 62 );
121
+ assert! (array . pop_front () == Option :: Some ((value_31_bytes . try_into (). unwrap (), 31 )));
122
+ assert! (array . len () == 31 );
123
+ assert! (array . pop_front () == Option :: Some ((value2_31_bytes . try_into (). unwrap (), 31 )));
124
+ assert! (array . len () == 0 );
125
+ assert! (array . pop_front () == Option :: None );
126
+ }
127
+
128
+ #[test]
129
+ fn byte_array_3_items () {
130
+ let value_31_bytes = 0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f ;
131
+ let value2_31_bytes = 0x2122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f ;
132
+ let value3_5_bytes = 0x4142434445 ;
133
+ let mut array = ByteArrayImpl :: new (
134
+ array_felt252_to_bytes31 (array! [value_31_bytes , value2_31_bytes , value3_5_bytes ]), 5
135
+ );
136
+ assert! (array . len () == 67 );
137
+ assert! (array . pop_front () == Option :: Some ((value_31_bytes . try_into (). unwrap (), 31 )));
138
+ assert! (array . len () == 36 );
139
+ assert! (array . pop_front () == Option :: Some ((value2_31_bytes . try_into (). unwrap (), 31 )));
140
+ assert! (array . len () == 5 );
141
+ assert! (array . pop_front () == Option :: Some ((value3_5_bytes . try_into (). unwrap (), 5 )));
142
+ assert! (array . pop_front () == Option :: None );
143
+ }
144
+
145
+ #[test]
146
+ #[should_panic]
147
+ fn byte_array_empty_invalid () {
148
+ ByteArrayImpl :: new (array! [], 5 );
149
+ }
150
+
151
+ #[test]
152
+ #[should_panic]
153
+ fn byte_array_last_too_large () {
154
+ ByteArrayImpl :: new (array_felt252_to_bytes31 (array! [1 , 2 , 3 ]), 35 );
155
+ }
156
+
157
+ #[test]
158
+ #[should_panic]
159
+ fn byte_array_last_zero_invalid () {
160
+ ByteArrayImpl :: new (array_felt252_to_bytes31 (array! [1 , 2 , 0 ]), 0 );
161
+ }
162
+
163
+ #[test]
164
+ #[should_panic]
165
+ fn byte_array_last_too_many_bytes () {
166
+ ByteArrayImpl :: new (array_felt252_to_bytes31 (array! [1 , 2 , 0x010203 ]), 2 );
167
+ }
168
+ }
0 commit comments