Skip to content

Commit

Permalink
chore: formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
teckmk committed Aug 7, 2024
1 parent a18da3c commit 882d867
Showing 1 changed file with 75 additions and 13 deletions.
88 changes: 75 additions & 13 deletions bitpack/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ extern crate bitval;

#[cfg(test)]
mod tests {
use bitpack::BitwisePackable;
use super::*;
use bitpack::BitwisePackable;
use bitval::Bitfield;

#[test]
Expand All @@ -17,7 +17,11 @@ mod tests {
c: bool,
}

let example = Example { a: true, b: false, c: true };
let example = Example {
a: true,
b: false,
c: true,
};
let packed = Example::pack(&example);
assert_eq!(packed, 0b00000101); // 5 in decimal
let unpacked = Example::unpack(packed);
Expand All @@ -37,7 +41,12 @@ mod tests {
d: bool,
}

let example = Example { a: true, b: false, c: true, d: true };
let example = Example {
a: true,
b: false,
c: true,
d: true,
};
let packed = Example::pack(&example);
assert_eq!(packed, 0b0000000000001101); // 13 in decimal
let unpacked = Example::unpack(packed);
Expand All @@ -59,7 +68,13 @@ mod tests {
e: bool,
}

let example = Example { a: true, b: false, c: true, d: true, e: false };
let example = Example {
a: true,
b: false,
c: true,
d: true,
e: false,
};
let packed = Example::pack(&example);
assert_eq!(packed, 0b00000000000000000000000000010101); // 21 in decimal
let unpacked = Example::unpack(packed);
Expand All @@ -85,7 +100,16 @@ mod tests {
h: bool,
}

let example = Example { a: true, b: false, c: true, d: true, e: false, f: true, g: false, h: true };
let example = Example {
a: true,
b: false,
c: true,
d: true,
e: false,
f: true,
g: false,
h: true,
};
let packed = Example::pack(&example);
assert_eq!(packed, 0b1011010101010101); // 0xB5B5 in hexadecimal
let unpacked = Example::unpack(packed);
Expand All @@ -100,7 +124,9 @@ mod tests {
}

#[test]
#[should_panic(expected = "Overflow occurred during packing: struct 'OverflowExample' has more boolean fields than can be packed in an u8 (8 bits).")]
#[should_panic(
expected = "Overflow occurred during packing: struct 'OverflowExample' has more boolean fields than can be packed in an u8 (8 bits)."
)]
fn test_overflow_packing_u8() {
#[derive(BitwisePackable)]
#[bitpack(size = "i8", overflow = false)]
Expand All @@ -116,12 +142,24 @@ mod tests {
i: bool,
}

let example = OverflowExample { a: true, b: false, c: true, d: true, e: false, f: true, g: false, h: true, i: false };
let example = OverflowExample {
a: true,
b: false,
c: true,
d: true,
e: false,
f: true,
g: false,
h: true,
i: false,
};
OverflowExample::pack(&example); // This should panic due to overflow
}

#[test]
#[should_panic(expected = "Overflow occurred during unpacking: struct 'OverflowExample' has more boolean fields than can be unpacked from an u8 (8 bits).")]
#[should_panic(
expected = "Overflow occurred during unpacking: struct 'OverflowExample' has more boolean fields than can be unpacked from an u8 (8 bits)."
)]
fn test_overflow_unpacking_u8() {
#[derive(BitwisePackable)]
#[bitpack(size = "i8", overflow = false)]
Expand Down Expand Up @@ -152,7 +190,13 @@ mod tests {
e: bool,
}

let example = Example { a: true, b: false, c: true, d: true, e: false };
let example = Example {
a: true,
b: false,
c: true,
d: true,
e: false,
};
let packed = Example::pack(&example);
let unpacked = Example::unpack(packed);
assert_eq!(unpacked.a, true);
Expand All @@ -163,7 +207,9 @@ mod tests {
}

#[test]
#[should_panic(expected = "Overflow occurred during packing: struct 'OverflowDynamic' has more boolean fields than can be packed in the provided Bitfield size.")]
#[should_panic(
expected = "Overflow occurred during packing: struct 'OverflowDynamic' has more boolean fields than can be packed in the provided Bitfield size."
)]
fn test_overflow_packing_auto() {
#[derive(BitwisePackable)]
#[bitpack(size = "auto", overflow = false)]
Expand All @@ -187,14 +233,30 @@ mod tests {
}

let example = OverflowDynamic {
a: true, b: false, c: true, d: true, e: false, f: true, g: false, h: true,
i: false, j: true, k: false, l: true, m: true, n: false, o: true, p: false,
a: true,
b: false,
c: true,
d: true,
e: false,
f: true,
g: false,
h: true,
i: false,
j: true,
k: false,
l: true,
m: true,
n: false,
o: true,
p: false,
};
OverflowDynamic::pack(&example); // This should panic due to overflow
}

#[test]
#[should_panic(expected = "Overflow occurred during unpacking: struct 'OverflowDynamic' has more boolean fields than can be unpacked from the provided Bitfield size.")]
#[should_panic(
expected = "Overflow occurred during unpacking: struct 'OverflowDynamic' has more boolean fields than can be unpacked from the provided Bitfield size."
)]
fn test_overflow_unpacking_auto() {
#[derive(BitwisePackable)]
#[bitpack(size = "auto", overflow = false)]
Expand Down

0 comments on commit 882d867

Please sign in to comment.