Skip to content

Commit cffce5c

Browse files
committed
1 parent 8d900a2 commit cffce5c

File tree

7 files changed

+98
-21
lines changed

7 files changed

+98
-21
lines changed

.github/workflows/main.yml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,30 @@ jobs:
3333
command: test
3434
args: --all
3535

36+
test_miri:
37+
name: Miri Test
38+
runs-on: ubuntu-latest
39+
steps:
40+
- uses: actions/checkout@v2
41+
- uses: actions-rs/toolchain@v1
42+
with:
43+
toolchain: nightly
44+
override: true
45+
- run: rustup component add miri
46+
- run: cargo miri test
47+
48+
test_miri_big_endian:
49+
name: Miri Test Big Endian
50+
runs-on: ubuntu-latest
51+
steps:
52+
- uses: actions/checkout@v2
53+
- uses: actions-rs/toolchain@v1
54+
with:
55+
toolchain: nightly
56+
override: true
57+
- run: rustup target add mips64-unknown-linux-gnuabi64 && rustup component add miri
58+
- run: cargo miri test --target mips64-unknown-linux-gnuabi64
59+
3660
examples:
3761
name: Examples
3862
runs-on: ubuntu-latest

src/impls/boxed.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,13 @@ where
6969
mod tests {
7070
use super::*;
7171
use crate::ctx::*;
72+
use crate::native_endian;
7273
use rstest::rstest;
7374

7475
#[rstest(input, expected, expected_rest,
7576
case(
7677
&[0xEF, 0xBE],
77-
Box::new(0xBEEF),
78+
Box::new(native_endian!(0xBEEF)),
7879
bits![Msb0, u8;]
7980
),
8081
)]

src/impls/cow.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,13 @@ where
3434
#[cfg(test)]
3535
mod tests {
3636
use super::*;
37+
use crate::native_endian;
3738
use rstest::rstest;
3839

3940
#[rstest(input, expected, expected_rest,
4041
case(
4142
&[0xEF, 0xBE],
42-
Cow::Owned(0xBEEF),
43+
Cow::Owned(native_endian!(0xBEEF)),
4344
bits![Msb0, u8;]
4445
),
4546
)]

src/impls/primitive.rs

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ ImplDekuTraits!(f64);
223223
#[cfg(test)]
224224
mod tests {
225225
use super::*;
226+
use crate::native_endian;
226227
use rstest::rstest;
227228

228229
static ENDIAN: Endian = Endian::new();
@@ -244,13 +245,23 @@ mod tests {
244245
}
245246

246247
TestPrimitive!(test_u8, u8, vec![0xAAu8], 0xAAu8);
247-
TestPrimitive!(test_u16, u16, vec![0xABu8, 0xCD], 0xCDAB);
248-
TestPrimitive!(test_u32, u32, vec![0xABu8, 0xCD, 0xEF, 0xBE], 0xBEEFCDAB);
248+
TestPrimitive!(
249+
test_u16,
250+
u16,
251+
vec![0xABu8, 0xCD],
252+
native_endian!(0xCDAB_u16)
253+
);
254+
TestPrimitive!(
255+
test_u32,
256+
u32,
257+
vec![0xABu8, 0xCD, 0xEF, 0xBE],
258+
native_endian!(0xBEEFCDAB_u32)
259+
);
249260
TestPrimitive!(
250261
test_u64,
251262
u64,
252263
vec![0xABu8, 0xCD, 0xEF, 0xBE, 0xAB, 0xCD, 0xFE, 0xC0],
253-
0xC0FECDABBEEFCDAB
264+
native_endian!(0xC0FECDABBEEFCDAB_u64)
254265
);
255266
TestPrimitive!(
256267
test_u128,
@@ -259,26 +270,31 @@ mod tests {
259270
0xABu8, 0xCD, 0xEF, 0xBE, 0xAB, 0xCD, 0xFE, 0xC0, 0xAB, 0xCD, 0xEF, 0xBE, 0xAB, 0xCD,
260271
0xFE, 0xC0
261272
],
262-
0xC0FECDABBEEFCDABC0FECDABBEEFCDAB
273+
native_endian!(0xC0FECDABBEEFCDABC0FECDABBEEFCDAB_u128)
263274
);
264275
TestPrimitive!(
265276
test_usize,
266277
usize,
267278
vec![0xABu8, 0xCD, 0xEF, 0xBE, 0xAB, 0xCD, 0xFE, 0xC0],
268279
if core::mem::size_of::<usize>() == 8 {
269-
0xC0FECDABBEEFCDAB
280+
native_endian!(0xC0FECDABBEEFCDAB_usize)
270281
} else {
271-
0xBEEFCDAB
282+
native_endian!(0xBEEFCDAB_usize)
272283
}
273284
);
274285
TestPrimitive!(test_i8, i8, vec![0xFBu8], -5);
275-
TestPrimitive!(test_i16, i16, vec![0xFDu8, 0xFE], -259);
276-
TestPrimitive!(test_i32, i32, vec![0x02u8, 0x3F, 0x01, 0xEF], -0x10FEC0FE);
286+
TestPrimitive!(test_i16, i16, vec![0xFDu8, 0xFE], native_endian!(-259_i16));
287+
TestPrimitive!(
288+
test_i32,
289+
i32,
290+
vec![0x02u8, 0x3F, 0x01, 0xEF],
291+
native_endian!(-0x10FEC0FE_i32)
292+
);
277293
TestPrimitive!(
278294
test_i64,
279295
i64,
280296
vec![0x02u8, 0x3F, 0x01, 0xEF, 0x01, 0x3F, 0x01, 0xEF],
281-
-0x10FEC0FE10FEC0FE
297+
native_endian!(-0x10FEC0FE10FEC0FE_i64)
282298
);
283299
TestPrimitive!(
284300
test_i128,
@@ -287,24 +303,29 @@ mod tests {
287303
0x02u8, 0x3F, 0x01, 0xEF, 0x01, 0x3F, 0x01, 0xEF, 0x01, 0x3F, 0x01, 0xEF, 0x01, 0x3F,
288304
0x01, 0xEF
289305
],
290-
-0x10FEC0FE10FEC0FE10FEC0FE10FEC0FE
306+
native_endian!(-0x10FEC0FE10FEC0FE10FEC0FE10FEC0FE_i128)
291307
);
292308
TestPrimitive!(
293309
test_isize,
294310
isize,
295311
vec![0x02u8, 0x3F, 0x01, 0xEF, 0x01, 0x3F, 0x01, 0xEF],
296312
if core::mem::size_of::<isize>() == 8 {
297-
-0x10FEC0FE10FEC0FE
313+
native_endian!(-0x10FEC0FE10FEC0FE_isize)
298314
} else {
299-
-0x10FEC0FE
315+
native_endian!(-0x10FEC0FE_isize)
300316
}
301317
);
302-
TestPrimitive!(test_f32, f32, vec![0xA6u8, 0x9B, 0xC4, 0xBB], -0.006);
318+
TestPrimitive!(
319+
test_f32,
320+
f32,
321+
vec![0xA6u8, 0x9B, 0xC4, 0xBB],
322+
native_endian!(-0.006_f32)
323+
);
303324
TestPrimitive!(
304325
test_f64,
305326
f64,
306327
vec![0xFAu8, 0x7E, 0x6A, 0xBC, 0x74, 0x93, 0x78, 0xBF],
307-
-0.006
328+
native_endian!(-0.006_f64)
308329
);
309330

310331
#[rstest(input, endian, bit_size, expected, expected_rest,

src/impls/tuple.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,15 +83,16 @@ ImplDekuTupleTraits! { A, B, C, D, E, F, G, H, I, J, K, }
8383
#[cfg(test)]
8484
mod tests {
8585
use super::*;
86+
use crate::native_endian;
8687
use core::fmt::Debug;
8788

8889
use rstest::rstest;
8990

9091
#[rstest(input, expected, expected_rest,
91-
case::length_1([0xef, 0xbe, 0xad, 0xde].as_ref(), (0xdeadbeefu32,), bits![Msb0, u8;]),
92-
case::length_2([1, 0x24, 0x98, 0x82, 0].as_ref(), (true, 0x829824u32), bits![Msb0, u8;]),
92+
case::length_1([0xef, 0xbe, 0xad, 0xde].as_ref(), (native_endian!(0xdeadbeef_u32),), bits![Msb0, u8;]),
93+
case::length_2([1, 0x24, 0x98, 0x82, 0].as_ref(), (true, native_endian!(0x829824_u32)), bits![Msb0, u8;]),
9394
case::length_11([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10].as_ref(), (0u8, 1u8, 2u8, 3u8, 4u8, 5u8, 6u8, 7u8, 8u8, 9u8, 10u8), bits![Msb0, u8;]),
94-
case::extra_rest([1, 0x24, 0x98, 0x82, 0, 0].as_ref(), (true, 0x829824u32), bits![Msb0, u8; 0, 0, 0, 0, 0, 0, 0, 0]),
95+
case::extra_rest([1, 0x24, 0x98, 0x82, 0, 0].as_ref(), (true, native_endian!(0x829824_u32)), bits![Msb0, u8; 0, 0, 0, 0, 0, 0, 0, 0]),
9596
)]
9697
fn test_tuple_read<'a, T>(input: &'a [u8], expected: T, expected_rest: &BitSlice<Msb0, u8>)
9798
where
@@ -104,8 +105,8 @@ mod tests {
104105
}
105106

106107
#[rstest(input, expected,
107-
case::length_1((0xdeadbeefu32,), vec![0xef, 0xbe, 0xad, 0xde]),
108-
case::length_2((true, 0x829824u32), vec![1, 0x24, 0x98, 0x82, 0]),
108+
case::length_1((native_endian!(0xdeadbeef_u32),), vec![0xef, 0xbe, 0xad, 0xde]),
109+
case::length_2((true, native_endian!(0x829824_u32)), vec![1, 0x24, 0x98, 0x82, 0]),
109110
case::length_11((0u8, 1u8, 2u8, 3u8, 4u8, 5u8, 6u8, 7u8, 8u8, 9u8, 10u8), vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
110111
)]
111112
fn test_tuple_write<T>(input: T, expected: Vec<u8>)

src/lib.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,3 +363,31 @@ where
363363
Ok(())
364364
}
365365
}
366+
367+
#[cfg(test)]
368+
pub mod test_utils {
369+
/// Converts value to native endian
370+
///
371+
/// Input is assumed to be little endian and the result is swapped if
372+
/// target is big endian.
373+
#[macro_export]
374+
macro_rules! native_endian {
375+
($num:expr) => {{
376+
#[cfg(target_endian = "little")]
377+
let res = $num;
378+
379+
#[cfg(target_endian = "big")]
380+
let res = {
381+
use bitvec::macros::internal::funty::IsNumber;
382+
383+
let mut val = $num;
384+
let bytes = val.to_le_bytes();
385+
val = <_>::from_be_bytes(bytes);
386+
387+
val
388+
};
389+
390+
res
391+
}};
392+
}
393+
}

tests/test_compile/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#[test]
22
#[cfg(not(tarpaulin))]
3+
#[cfg_attr(miri, ignore)]
34
fn test_compile() {
45
let t = trybuild::TestCases::new();
56
t.compile_fail("tests/test_compile/cases/*.rs");

0 commit comments

Comments
 (0)