Skip to content

Commit b40af1d

Browse files
committed
add basic tests
1 parent f26f298 commit b40af1d

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

gix-utils/src/btoi.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,13 @@ pub fn btou<I: MinNumTraits>(bytes: &[u8]) -> Result<I, ParseIntegerError> {
196196
btou_radix(bytes, 10)
197197
}
198198

199+
#[test]
200+
fn btou_assert() {
201+
assert_eq!(Ok(12345), btou(b"12345"));
202+
assert!(btou::<u8>(b"+1").is_err()); // only btoi allows signs
203+
assert!(btou::<u8>(b"256").is_err()); // overflow
204+
}
205+
199206
/// Converts a byte slice in a given base to an integer. Signs are not allowed.
200207
///
201208
/// # Errors
@@ -266,6 +273,12 @@ pub fn btou_radix<I: MinNumTraits>(bytes: &[u8], radix: u32) -> Result<I, ParseI
266273
Ok(result)
267274
}
268275

276+
#[test]
277+
fn btou_radix_assert() {
278+
assert_eq!(Ok(255), btou_radix(b"ff", 16));
279+
assert_eq!(Ok(42), btou_radix(b"101010", 2));
280+
}
281+
269282
/// Converts a byte slice to an integer.
270283
///
271284
/// Like [`btou`], but numbers may optionally start with a sign (`-` or `+`).
@@ -304,6 +317,18 @@ pub fn btoi<I: MinNumTraits>(bytes: &[u8]) -> Result<I, ParseIntegerError> {
304317
btoi_radix(bytes, 10)
305318
}
306319

320+
#[test]
321+
fn btoi_assert() {
322+
assert_eq!(Ok(123), btoi(b"123"));
323+
assert_eq!(Ok(123), btoi(b"+123"));
324+
assert_eq!(Ok(-123), btoi(b"-123"));
325+
326+
assert!(btoi::<u8>(b"123456789").is_err()); // overflow
327+
assert!(btoi::<u64>(b"-1").is_err()); // underflow
328+
329+
assert!(btoi::<i32>(b" 42").is_err()); // leading space
330+
}
331+
307332
/// Converts a byte slice in a given base to an integer.
308333
///
309334
/// Like [`btou_radix`], but numbers may optionally start with a sign
@@ -390,3 +415,10 @@ fn btoi_radix<I: MinNumTraits>(bytes: &[u8], radix: u32) -> Result<I, ParseInteg
390415

391416
Ok(result)
392417
}
418+
419+
#[test]
420+
fn btoi_radix_assert() {
421+
assert_eq!(Ok(10), btoi_radix(b"a", 16));
422+
assert_eq!(Ok(10), btoi_radix(b"+a", 16));
423+
assert_eq!(Ok(-42), btoi_radix(b"-101010", 2));
424+
}

0 commit comments

Comments
 (0)