Debian Codegraph @

tests/test.rs

raw symbols SCIP
1 extern crate cobsrust-analyzer cargo cobs 0.2.3 cobs/;
2 extern crate quickcheckrust-analyzer cargo cobs 0.2.3 quickcheck/;
3
4 use quickcheckrust-analyzer cargo quickcheck 1.1.0 crate/::{quickcheckrust-analyzer cargo quickcheck 1.1.0 tester/quickcheck()., TestResultrust-analyzer cargo quickcheck 1.1.0 tester/TestResult#};
5 use cobsrust-analyzer cargo cobs 0.2.3 crate/::{max_encoding_lengthrust-analyzer cargo cobs 0.2.3 max_encoding_length()., encoderust-analyzer cargo cobs 0.2.3 enc/encode()., decoderust-analyzer cargo cobs 0.2.3 dec/decode().};
6 #[cfg(feature="use_std")]
7 use cobsrust-analyzer cargo cobs 0.2.3 crate/::{encode_vecrust-analyzer cargo cobs 0.2.3 enc/encode_vec()., decode_vecrust-analyzer cargo cobs 0.2.3 dec/decode_vec()., encode_vec_with_sentinelrust-analyzer cargo cobs 0.2.3 enc/encode_vec_with_sentinel()., decode_vec_with_sentinelrust-analyzer cargo cobs 0.2.3 dec/decode_vec_with_sentinel().};
8 use cobsrust-analyzer cargo cobs 0.2.3 crate/::{CobsEncoderrust-analyzer cargo cobs 0.2.3 enc/CobsEncoder#, CobsDecoderrust-analyzer cargo cobs 0.2.3 dec/CobsDecoder#};
9
10 fn test_pairrust-analyzer cargo cobs 0.2.3 test_pair().(source: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8>, encoded: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8>) {
11 let mut test_encoded = encoded.clonerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][Clone]clone().();
12 let mut test_decoded = source.clonerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][Clone]clone().();
13
14 // Mangle data to ensure data is re-populated correctly
15 test_encoded.iter_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter_mut().().for_eachrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/iter/impl#[`IterMut<'a, T>`][Iterator]for_each().(|i| *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().i = 0x80);
16 encoderust-analyzer cargo cobs 0.2.3 enc/encode().(&source[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index()...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFull#]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index()., &mut test_encoded[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`IndexMut<I>`]index_mut()...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFull#]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`IndexMut<I>`]index_mut().);
17
18 // Mangle data to ensure data is re-populated correctly
19 test_decoded.iter_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter_mut().().for_eachrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/iter/impl#[`IterMut<'a, T>`][Iterator]for_each().(|i| *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().i = 0x80);
20 decoderust-analyzer cargo cobs 0.2.3 dec/decode().(&encoded[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index()...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFull#]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index()., &mut test_decoded[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`IndexMut<I>`]index_mut()...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFull#]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`IndexMut<I>`]index_mut().).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
21
22 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(encoded, test_encoded);
23 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(source, test_decoded);
24 }
25
26 #[cfg(feature="use_std")]
27 fn test_roundtriprust-analyzer cargo cobs 0.2.3 test_roundtrip().(source: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8>) {
28 let encoded = encode_vecrust-analyzer cargo cobs 0.2.3 enc/encode_vec().(&source);
29 let decoded = decode_vecrust-analyzer cargo cobs 0.2.3 dec/decode_vec().(&encoded).expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]expect().("decode_vec");
30 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(source, decoded);
31 }
32
33 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
34 fn decode_malforemdrust-analyzer cargo cobs 0.2.3 decode_malforemd().() {
35 let malformed_buf: [u8;32] = [68, 69, 65, 68, 66, 69, 69, 70, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
36 let mut dest_buf : [u8;32] = [0;32];
37 if let Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(()) = decoderust-analyzer cargo cobs 0.2.3 dec/decode().(&malformed_buf, &mut dest_buf){
38 return;
39 } else {
40 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(false, "invalid test result.");
41 }
42 }
43
44 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
45 fn stream_roundtriprust-analyzer cargo cobs 0.2.3 stream_roundtrip().() {
46 for ct in 1..=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeInclusive#1000 {
47 let source: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8> = (ct..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#2*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul().ct)
48 .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|x: usize| (x rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[usize][`BitAnd<Self>`]bitand().&rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[usize][`BitAnd<Self>`]bitand(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[usize][`BitAnd<Self>`]bitand().0xFF) as u8)
49 .collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().();
50
51 let mut dest = vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![0u8; max_encoding_lengthrust-analyzer cargo cobs 0.2.3 max_encoding_length().(source.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().())];
52
53 let sz_en = {
54 let mut ce = CobsEncoderrust-analyzer cargo cobs 0.2.3 enc/CobsEncoder#::newrust-analyzer cargo cobs 0.2.3 enc/impl#[`CobsEncoder<'a>`]new().(&mut dest);
55
56 for c in source.chunksrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]chunks().(17) {
57 ce.pushrust-analyzer cargo cobs 0.2.3 enc/impl#[`CobsEncoder<'a>`]push().(c).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
58 }
59 let sz = ce.finalizerust-analyzer cargo cobs 0.2.3 enc/impl#[`CobsEncoder<'a>`]finalize().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
60 sz
61 };
62
63 let mut decoded = source.clonerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][Clone]clone().();
64 decoded.iter_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter_mut().().for_eachrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/iter/impl#[`IterMut<'a, T>`][Iterator]for_each().(|i| *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().i = 0x80);
65 let sz_de = {
66 let mut cd = CobsDecoderrust-analyzer cargo cobs 0.2.3 dec/CobsDecoder#::newrust-analyzer cargo cobs 0.2.3 dec/impl#[`CobsDecoder<'a>`]new().(&mut decoded);
67
68 for c in dest[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index().0..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#sz_en]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index()..chunksrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]chunks().(11) {
69 cd.pushrust-analyzer cargo cobs 0.2.3 dec/impl#[`CobsDecoder<'a>`]push().(c).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
70 }
71 let sz_msg = cd.feedrust-analyzer cargo cobs 0.2.3 dec/impl#[`CobsDecoder<'a>`]feed().(0).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().();
72 sz_msg
73 };
74
75 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(sz_de, source.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().());
76 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(source, decoded);
77 }
78
79 }
80
81 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
82 fn test_max_encoding_lengthrust-analyzer cargo cobs 0.2.3 test_max_encoding_length().() {
83 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(max_encoding_lengthrust-analyzer cargo cobs 0.2.3 max_encoding_length().(253), 254);
84 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(max_encoding_lengthrust-analyzer cargo cobs 0.2.3 max_encoding_length().(254), 255);
85 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(max_encoding_lengthrust-analyzer cargo cobs 0.2.3 max_encoding_length().(255), 257);
86 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(max_encoding_lengthrust-analyzer cargo cobs 0.2.3 max_encoding_length().(254 *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul(). 2), 255 *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul(). 2);
87 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(max_encoding_lengthrust-analyzer cargo cobs 0.2.3 max_encoding_length().(254 *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul(). 2 +rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). 1), 256 *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul(). 2);
88 }
89
90 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
91 fn test_encode_1rust-analyzer cargo cobs 0.2.3 test_encode_1().() {
92 test_pairrust-analyzer cargo cobs 0.2.3 test_pair().(vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![10, 11, 0, 12], vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![3, 10, 11, 2, 12])
93 }
94
95 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
96 fn test_encode_2rust-analyzer cargo cobs 0.2.3 test_encode_2().() {
97 test_pairrust-analyzer cargo cobs 0.2.3 test_pair().(vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![0, 0, 1, 0], vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![1, 1, 2, 1, 1])
98 }
99
100 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
101 fn test_encode_3rust-analyzer cargo cobs 0.2.3 test_encode_3().() {
102 test_pairrust-analyzer cargo cobs 0.2.3 test_pair().(vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![255, 0], vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![2, 255, 1])
103 }
104
105 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
106 fn test_encode_4rust-analyzer cargo cobs 0.2.3 test_encode_4().() {
107 test_pairrust-analyzer cargo cobs 0.2.3 test_pair().(vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![1], vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![2, 1])
108 }
109
110 #[cfg(feature="use_std")]
111 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
112 fn test_roundtrip_1rust-analyzer cargo cobs 0.2.3 test_roundtrip_1().() {
113 test_roundtriprust-analyzer cargo cobs 0.2.3 test_roundtrip().(vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![1,2,3]);
114 }
115
116 #[cfg(feature="use_std")]
117 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
118 fn test_roundtrip_2rust-analyzer cargo cobs 0.2.3 test_roundtrip_2().() {
119 for i in 0..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#5usize {
120 let mut v = Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]new().();
121 for j in 0..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#252+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().i {
122 v.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(j as u8);
123 }
124 test_roundtriprust-analyzer cargo cobs 0.2.3 test_roundtrip().(v);
125 }
126 }
127
128 #[cfg(feature="use_std")]
129 fn identityrust-analyzer cargo cobs 0.2.3 identity().(source: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8>, sentinel: u8) -> TestResultrust-analyzer cargo quickcheck 1.1.0 tester/TestResult# {
130 let encoded = encode_vec_with_sentinelrust-analyzer cargo cobs 0.2.3 enc/encode_vec_with_sentinel().(&source[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index()...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFull#]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index()., sentinel);
131
132 // Check that the sentinel doesn't show up in the encoded message
133 for x in encoded.iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().() {
134 if *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().x rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]eq().sentinel {
135 return TestResultrust-analyzer cargo quickcheck 1.1.0 tester/TestResult#::errorrust-analyzer cargo quickcheck 1.1.0 tester/impl#[TestResult]error().("Sentinel found in encoded message.");
136 }
137 }
138
139 // Check that the decoding the encoded message returns the original message
140 match decode_vec_with_sentinelrust-analyzer cargo cobs 0.2.3 dec/decode_vec_with_sentinel().(&encoded[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index()...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFull#]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index()., sentinel) {
141 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(decoded) => {
142 if source rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/partial_eq/impl#[`Vec<T, A1>`][`PartialEq<Vec<U, A2>>`]eq().==rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/partial_eq/impl#[`Vec<T, A1>`][`PartialEq<Vec<U, A2>>`]eq(). rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/partial_eq/impl#[`Vec<T, A1>`][`PartialEq<Vec<U, A2>>`]eq().decoded {
143 TestResultrust-analyzer cargo quickcheck 1.1.0 tester/TestResult#::passedrust-analyzer cargo quickcheck 1.1.0 tester/impl#[TestResult]passed().()
144 } else {
145 TestResultrust-analyzer cargo quickcheck 1.1.0 tester/TestResult#::failedrust-analyzer cargo quickcheck 1.1.0 tester/impl#[TestResult]failed().()
146 }
147 }
148 Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(_) => TestResultrust-analyzer cargo quickcheck 1.1.0 tester/TestResult#::errorrust-analyzer cargo quickcheck 1.1.0 tester/impl#[TestResult]error().("Decoding Error"),
149 }
150 }
151
152 #[cfg(feature="use_std")]
153 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
154 fn test_encode_decode_with_sentinelrust-analyzer cargo cobs 0.2.3 test_encode_decode_with_sentinel().() {
155 quickcheckrust-analyzer cargo quickcheck 1.1.0 tester/quickcheck().(identityrust-analyzer cargo cobs 0.2.3 identity(). as fn(Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8>, u8) -> TestResultrust-analyzer cargo quickcheck 1.1.0 tester/TestResult#);
156 }
157
158 #[cfg(feature="use_std")]
159 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
160 fn test_encode_decoderust-analyzer cargo cobs 0.2.3 test_encode_decode().() {
161 fn identity_default_sentinelrust-analyzer cargo cobs 0.2.3 identity_default_sentinel().(source: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8>) -> TestResultrust-analyzer cargo quickcheck 1.1.0 tester/TestResult# {
162 identityrust-analyzer cargo cobs 0.2.3 identity().(source, 0)
163 }
164 quickcheckrust-analyzer cargo quickcheck 1.1.0 tester/quickcheck().(identity_default_sentinelrust-analyzer cargo cobs 0.2.3 identity_default_sentinel(). as fn(Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8>) -> TestResultrust-analyzer cargo quickcheck 1.1.0 tester/TestResult#);
165 }
166
167 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
168 fn wikipedia_ex_6rust-analyzer cargo cobs 0.2.3 wikipedia_ex_6().() {
169 let mut unencoded: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8> = vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![];
170
171 (1..=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeInclusive#0xFE).for_eachrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#for_each().(|i| unencoded.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(i));
172
173 // NOTE: trailing 0x00 is implicit
174 let mut encoded: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8> = vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![];
175 encoded.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(0xFF);
176 (1..=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeInclusive#0xFE).for_eachrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#for_each().(|i| encoded.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(i));
177
178 test_pairrust-analyzer cargo cobs 0.2.3 test_pair().(unencoded, encoded);
179 }
180
181 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
182 fn wikipedia_ex_7rust-analyzer cargo cobs 0.2.3 wikipedia_ex_7().() {
183 let mut unencoded: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8> = vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![];
184
185 (0..=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeInclusive#0xFE).for_eachrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#for_each().(|i| unencoded.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(i));
186
187 // NOTE: trailing 0x00 is implicit
188 let mut encoded: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8> = vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![];
189 encoded.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(0x01);
190 encoded.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(0xFF);
191 (1..=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeInclusive#0xFE).for_eachrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#for_each().(|i| encoded.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(i));
192
193 test_pairrust-analyzer cargo cobs 0.2.3 test_pair().(unencoded, encoded);
194 }
195
196 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
197 fn wikipedia_ex_8rust-analyzer cargo cobs 0.2.3 wikipedia_ex_8().() {
198 let mut unencoded: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8> = vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![];
199
200 (1..=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeInclusive#0xFF).for_eachrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#for_each().(|i| unencoded.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(i));
201
202 // NOTE: trailing 0x00 is implicit
203 let mut encoded: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8> = vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![];
204 encoded.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(0xFF);
205 (1..=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeInclusive#0xFE).for_eachrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#for_each().(|i| encoded.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(i));
206 encoded.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(0x02);
207 encoded.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(0xFF);
208
209 test_pairrust-analyzer cargo cobs 0.2.3 test_pair().(unencoded, encoded);
210 }
211
212 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
213 fn wikipedia_ex_9rust-analyzer cargo cobs 0.2.3 wikipedia_ex_9().() {
214 let mut unencoded: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8> = vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![];
215
216 (2..=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeInclusive#0xFF).for_eachrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#for_each().(|i| unencoded.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(i));
217 unencoded.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(0x00);
218
219 // NOTE: trailing 0x00 is implicit
220 let mut encoded: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8> = vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![];
221 encoded.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(0xFF);
222 (2..=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeInclusive#0xFF).for_eachrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#for_each().(|i| encoded.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(i));
223 encoded.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(0x01);
224 encoded.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(0x01);
225
226 test_pairrust-analyzer cargo cobs 0.2.3 test_pair().(unencoded, encoded);
227 }
228
229 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
230 fn wikipedia_ex_10rust-analyzer cargo cobs 0.2.3 wikipedia_ex_10().() {
231 let mut unencoded: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8> = vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![];
232
233 (3..=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeInclusive#0xFF).for_eachrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#for_each().(|i| unencoded.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(i));
234 unencoded.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(0x00);
235 unencoded.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(0x01);
236
237 // NOTE: trailing 0x00 is implicit
238 let mut encoded: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8> = vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![];
239 encoded.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(0xFE);
240 (3..=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeInclusive#0xFF).for_eachrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#for_each().(|i| encoded.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(i));
241 encoded.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(0x02);
242 encoded.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(0x01);
243
244 test_pairrust-analyzer cargo cobs 0.2.3 test_pair().(unencoded, encoded);
245 }
246
247 #[cfg(feature="use_std")]
248 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
249 fn issue_15rust-analyzer cargo cobs 0.2.3 issue_15().() {
250 // Reported: https://github.com/awelkie/cobs.rs/issues/15
251
252 let my_string_buf = b"\x00\x11\x00\x22";
253 let max_len = cobsrust-analyzer cargo cobs 0.2.3 crate/::max_encoding_lengthrust-analyzer cargo cobs 0.2.3 max_encoding_length().(my_string_buf.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().());
254 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(max_len <rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt(). 128);
255 let mut buf = [0u8; 128];
256
257 let len = cobsrust-analyzer cargo cobs 0.2.3 crate/::encode_with_sentinelrust-analyzer cargo cobs 0.2.3 enc/encode_with_sentinel().(
258 my_string_buf,
259 &mut buf,
260 b'\x00');
261
262 let cobs_buf = &buf[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index().0..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#len]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index().;
263
264 let mut decoded_dest_buf = [0u8; 128];
265 let new_len = cobsrust-analyzer cargo cobs 0.2.3 crate/::decode_with_sentinelrust-analyzer cargo cobs 0.2.3 dec/decode_with_sentinel().(
266 cobs_buf,
267 &mut decoded_dest_buf,
268 b'\x00').unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
269 let decoded_buf = &decoded_dest_buf[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index().0..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#new_len]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index().;
270
271 printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!("{:?} {:?} {:?}", my_string_buf, cobs_buf, decoded_buf);
272 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(my_string_buf, decoded_buf);
273 }