2024-05-12 20:10:58 +02:00
|
|
|
use numf::format::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn format() {
|
|
|
|
let options = FormatOptions::default();
|
2024-09-06 11:27:18 +02:00
|
|
|
assert_eq!(Format::Dec.format_str(1337, &options), "1337");
|
2024-05-12 20:21:54 +02:00
|
|
|
assert_eq!(
|
2024-09-06 11:27:18 +02:00
|
|
|
Format::Dec.format_str(u128::MAX, &options),
|
2024-05-12 20:21:54 +02:00
|
|
|
format!("{}", u128::MAX)
|
|
|
|
);
|
2024-05-12 20:10:58 +02:00
|
|
|
|
2024-09-06 11:27:18 +02:00
|
|
|
assert_eq!(Format::Hex.format_str(0x1337, &options), "1337");
|
2024-05-12 20:21:54 +02:00
|
|
|
assert_eq!(
|
2024-09-06 11:27:18 +02:00
|
|
|
Format::Hex.format_str(u128::MAX, &options),
|
2024-05-12 20:21:54 +02:00
|
|
|
format!("{:X}", u128::MAX)
|
|
|
|
);
|
2024-05-12 20:10:58 +02:00
|
|
|
|
2024-05-12 20:21:54 +02:00
|
|
|
assert_eq!(
|
2024-09-06 11:27:18 +02:00
|
|
|
Format::Bin.format_str(0b1010001001010010010100111, &options),
|
2024-05-12 20:21:54 +02:00
|
|
|
"1010001001010010010100111"
|
|
|
|
);
|
|
|
|
assert_eq!(
|
2024-09-06 11:27:18 +02:00
|
|
|
Format::Bin.format_str(u128::MAX, &options),
|
2024-05-12 20:21:54 +02:00
|
|
|
format!("{:b}", u128::MAX)
|
|
|
|
);
|
2024-05-12 20:10:58 +02:00
|
|
|
|
2024-09-06 11:27:18 +02:00
|
|
|
assert_eq!(Format::Octal.format_str(0o13377331, &options), "13377331");
|
2024-05-12 20:21:54 +02:00
|
|
|
assert_eq!(
|
2024-09-06 11:27:18 +02:00
|
|
|
Format::Octal.format_str(u128::MAX, &options),
|
2024-05-12 20:21:54 +02:00
|
|
|
format!("{:o}", u128::MAX)
|
|
|
|
);
|
2024-05-12 20:10:58 +02:00
|
|
|
|
2024-09-06 11:27:18 +02:00
|
|
|
assert_eq!(Format::Base32.format_str(0x41414242, &options), "IFAUEQQ=");
|
2024-05-12 20:21:54 +02:00
|
|
|
assert_eq!(
|
2024-09-06 11:27:18 +02:00
|
|
|
Format::Base32.format_str(0x4141414141414141, &options),
|
2024-05-12 20:21:54 +02:00
|
|
|
"IFAUCQKBIFAUC==="
|
|
|
|
);
|
2024-05-12 20:10:58 +02:00
|
|
|
|
2024-09-06 11:27:18 +02:00
|
|
|
assert_eq!(Format::Base64.format_str(0x41414242, &options), "QUFCQg==");
|
2024-05-12 20:21:54 +02:00
|
|
|
assert_eq!(
|
2024-09-06 11:27:18 +02:00
|
|
|
Format::Base64.format_str(0x4141414141414141, &options),
|
2024-05-12 20:21:54 +02:00
|
|
|
"QUFBQUFBQUE="
|
|
|
|
);
|
2024-09-06 12:46:54 +02:00
|
|
|
|
|
|
|
assert_eq!(Format::Raw.format(0x1337, &options), vec![0x13, 0x37]);
|
|
|
|
assert_eq!(Format::Raw.format(0x0, &options), vec![0x0]);
|
2024-05-12 20:10:58 +02:00
|
|
|
}
|
2024-05-12 20:20:36 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn format_padding() {
|
|
|
|
let mut options = FormatOptions::default();
|
|
|
|
options.set_padding(true);
|
|
|
|
|
2024-09-06 11:27:18 +02:00
|
|
|
assert_eq!(Format::Dec.format_str(1337, &options), "1337");
|
2024-05-12 20:21:54 +02:00
|
|
|
assert_eq!(
|
2024-09-06 11:27:18 +02:00
|
|
|
Format::Dec.format_str(u128::MAX, &options),
|
2024-05-12 20:21:54 +02:00
|
|
|
format!("{}", u128::MAX)
|
|
|
|
);
|
2024-05-12 20:20:36 +02:00
|
|
|
|
2024-09-06 11:27:18 +02:00
|
|
|
assert_eq!(Format::Hex.format_str(0xFFF, &options), "0FFF");
|
|
|
|
assert_eq!(Format::Hex.format_str(0xFFFF, &options), "FFFF");
|
2024-05-12 20:21:54 +02:00
|
|
|
assert_eq!(
|
2024-09-06 11:27:18 +02:00
|
|
|
Format::Hex.format_str(u128::MAX, &options),
|
2024-05-12 20:21:54 +02:00
|
|
|
format!("{:X}", u128::MAX)
|
|
|
|
);
|
2024-05-12 20:20:36 +02:00
|
|
|
|
2024-05-12 20:21:54 +02:00
|
|
|
assert_eq!(
|
2024-09-06 11:27:18 +02:00
|
|
|
Format::Bin.format_str(0b11110000_00001111, &options),
|
2024-05-12 20:21:54 +02:00
|
|
|
"1111000000001111"
|
|
|
|
);
|
|
|
|
assert_eq!(
|
2024-09-06 11:27:18 +02:00
|
|
|
Format::Bin.format_str(0b110000_00001111, &options),
|
2024-05-12 20:21:54 +02:00
|
|
|
"0011000000001111"
|
|
|
|
);
|
|
|
|
assert_eq!(
|
2024-09-06 11:27:18 +02:00
|
|
|
Format::Bin.format_str(u128::MAX, &options),
|
2024-05-12 20:21:54 +02:00
|
|
|
format!("{:b}", u128::MAX)
|
|
|
|
);
|
2024-05-12 20:20:36 +02:00
|
|
|
|
2024-09-06 11:27:18 +02:00
|
|
|
assert_eq!(Format::Octal.format_str(0o13377331, &options), "13377331");
|
2024-05-12 20:21:54 +02:00
|
|
|
assert_eq!(
|
2024-09-06 11:27:18 +02:00
|
|
|
Format::Octal.format_str(u128::MAX, &options),
|
2024-05-12 20:21:54 +02:00
|
|
|
format!("{:o}", u128::MAX)
|
|
|
|
);
|
2024-05-12 20:20:36 +02:00
|
|
|
|
2024-09-06 11:27:18 +02:00
|
|
|
assert_eq!(Format::Base32.format_str(0x41414242, &options), "IFAUEQQ=");
|
2024-05-12 20:21:54 +02:00
|
|
|
assert_eq!(
|
2024-09-06 11:27:18 +02:00
|
|
|
Format::Base32.format_str(0x4141414141414141, &options),
|
2024-05-12 20:21:54 +02:00
|
|
|
"IFAUCQKBIFAUC==="
|
|
|
|
);
|
2024-05-12 20:20:36 +02:00
|
|
|
|
2024-09-06 11:27:18 +02:00
|
|
|
assert_eq!(Format::Base64.format_str(0x41414242, &options), "QUFCQg==");
|
2024-05-12 20:21:54 +02:00
|
|
|
assert_eq!(
|
2024-09-06 11:27:18 +02:00
|
|
|
Format::Base64.format_str(0x4141414141414141, &options),
|
2024-05-12 20:21:54 +02:00
|
|
|
"QUFBQUFBQUE="
|
|
|
|
);
|
2024-09-06 12:46:54 +02:00
|
|
|
|
|
|
|
assert_eq!(Format::Raw.format(0x1337, &options), vec![0x13, 0x37]);
|
|
|
|
assert_eq!(Format::Raw.format(0x0, &options), vec![0x0]);
|
2024-05-12 20:20:36 +02:00
|
|
|
}
|
2024-05-12 20:23:04 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn format_prefix() {
|
|
|
|
let mut options = FormatOptions::default();
|
|
|
|
options.set_prefix(true);
|
|
|
|
|
2024-09-06 11:27:18 +02:00
|
|
|
assert_eq!(Format::Dec.format_str(1337, &options), "0d1337");
|
2024-05-12 20:23:47 +02:00
|
|
|
assert_eq!(
|
2024-09-06 11:27:18 +02:00
|
|
|
Format::Dec.format_str(u128::MAX, &options),
|
2024-05-12 20:23:47 +02:00
|
|
|
format!("0d{}", u128::MAX)
|
|
|
|
);
|
2024-05-12 20:23:04 +02:00
|
|
|
|
2024-09-06 11:27:18 +02:00
|
|
|
assert_eq!(Format::Hex.format_str(0x1337, &options), "0x1337");
|
2024-05-12 20:23:47 +02:00
|
|
|
assert_eq!(
|
2024-09-06 11:27:18 +02:00
|
|
|
Format::Hex.format_str(u128::MAX, &options),
|
2024-05-12 20:23:47 +02:00
|
|
|
format!("0x{:X}", u128::MAX)
|
|
|
|
);
|
2024-05-12 20:23:04 +02:00
|
|
|
|
2024-05-12 20:23:47 +02:00
|
|
|
assert_eq!(
|
2024-09-06 11:27:18 +02:00
|
|
|
Format::Bin.format_str(0b1010001001010010010100111, &options),
|
2024-05-12 20:23:47 +02:00
|
|
|
"0b1010001001010010010100111"
|
|
|
|
);
|
|
|
|
assert_eq!(
|
2024-09-06 11:27:18 +02:00
|
|
|
Format::Bin.format_str(u128::MAX, &options),
|
2024-05-12 20:23:47 +02:00
|
|
|
format!("0b{:b}", u128::MAX)
|
|
|
|
);
|
2024-05-12 20:23:04 +02:00
|
|
|
|
2024-09-06 11:27:18 +02:00
|
|
|
assert_eq!(Format::Octal.format_str(0o13377331, &options), "0o13377331");
|
2024-05-12 20:23:47 +02:00
|
|
|
assert_eq!(
|
2024-09-06 11:27:18 +02:00
|
|
|
Format::Octal.format_str(u128::MAX, &options),
|
2024-05-12 20:23:47 +02:00
|
|
|
format!("0o{:o}", u128::MAX)
|
|
|
|
);
|
2024-05-12 20:23:04 +02:00
|
|
|
|
2024-05-12 20:23:47 +02:00
|
|
|
assert_eq!(
|
2024-09-06 11:27:18 +02:00
|
|
|
Format::Base32.format_str(0x41414242, &options),
|
|
|
|
"032sIFAUEQQ="
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
Format::Base32.format_str(0x4141414141414141, &options),
|
2024-05-12 20:23:47 +02:00
|
|
|
"032sIFAUCQKBIFAUC==="
|
|
|
|
);
|
2024-05-12 20:23:04 +02:00
|
|
|
|
2024-05-12 20:23:47 +02:00
|
|
|
assert_eq!(
|
2024-09-06 11:27:18 +02:00
|
|
|
Format::Base64.format_str(0x41414242, &options),
|
|
|
|
"0sQUFCQg=="
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
Format::Base64.format_str(0x4141414141414141, &options),
|
2024-05-12 20:23:47 +02:00
|
|
|
"0sQUFBQUFBQUE="
|
|
|
|
);
|
2024-09-06 12:46:54 +02:00
|
|
|
|
|
|
|
assert_eq!(Format::Raw.format(0x1337, &options), vec![0x0, 0x13, 0x37]);
|
|
|
|
assert_eq!(Format::Raw.format(0x0, &options), vec![0x0, 0x0]);
|
2024-05-12 20:23:04 +02:00
|
|
|
}
|
2024-05-12 20:25:48 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn format_padded_prefix() {
|
|
|
|
let mut options = FormatOptions::default();
|
|
|
|
options.set_prefix(true);
|
|
|
|
options.set_padding(true);
|
|
|
|
|
2024-09-06 11:27:18 +02:00
|
|
|
assert_eq!(Format::Dec.format_str(1337, &options), "0d1337");
|
2024-05-12 20:25:48 +02:00
|
|
|
assert_eq!(
|
2024-09-06 11:27:18 +02:00
|
|
|
Format::Dec.format_str(u128::MAX, &options),
|
2024-05-12 20:25:48 +02:00
|
|
|
format!("0d{}", u128::MAX)
|
|
|
|
);
|
|
|
|
|
2024-09-06 11:27:18 +02:00
|
|
|
assert_eq!(Format::Hex.format_str(0xFFF, &options), "0x0FFF");
|
|
|
|
assert_eq!(Format::Hex.format_str(0xFFFF, &options), "0xFFFF");
|
2024-05-12 20:25:48 +02:00
|
|
|
assert_eq!(
|
2024-09-06 11:27:18 +02:00
|
|
|
Format::Hex.format_str(u128::MAX, &options),
|
2024-05-12 20:25:48 +02:00
|
|
|
format!("0x{:X}", u128::MAX)
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(
|
2024-09-06 11:27:18 +02:00
|
|
|
Format::Bin.format_str(0b11110000_00001111, &options),
|
2024-05-12 20:25:48 +02:00
|
|
|
"0b1111000000001111"
|
|
|
|
);
|
|
|
|
assert_eq!(
|
2024-09-06 11:27:18 +02:00
|
|
|
Format::Bin.format_str(0b110000_00001111, &options),
|
2024-05-12 20:25:48 +02:00
|
|
|
"0b0011000000001111"
|
|
|
|
);
|
|
|
|
assert_eq!(
|
2024-09-06 11:27:18 +02:00
|
|
|
Format::Bin.format_str(u128::MAX, &options),
|
2024-05-12 20:25:48 +02:00
|
|
|
format!("0b{:b}", u128::MAX)
|
|
|
|
);
|
|
|
|
|
2024-09-06 11:27:18 +02:00
|
|
|
assert_eq!(Format::Octal.format_str(0o13377331, &options), "0o13377331");
|
2024-05-12 20:25:48 +02:00
|
|
|
assert_eq!(
|
2024-09-06 11:27:18 +02:00
|
|
|
Format::Octal.format_str(u128::MAX, &options),
|
2024-05-12 20:25:48 +02:00
|
|
|
format!("0o{:o}", u128::MAX)
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(
|
2024-09-06 11:27:18 +02:00
|
|
|
Format::Base32.format_str(0x41414242, &options),
|
|
|
|
"032sIFAUEQQ="
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
Format::Base32.format_str(0x4141414141414141, &options),
|
2024-05-12 20:25:48 +02:00
|
|
|
"032sIFAUCQKBIFAUC==="
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(
|
2024-09-06 11:27:18 +02:00
|
|
|
Format::Base64.format_str(0x41414242, &options),
|
|
|
|
"0sQUFCQg=="
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
Format::Base64.format_str(0x4141414141414141, &options),
|
2024-05-12 20:25:48 +02:00
|
|
|
"0sQUFBQUFBQUE="
|
|
|
|
);
|
2024-09-06 12:46:54 +02:00
|
|
|
|
|
|
|
assert_eq!(Format::Raw.format(0x1337, &options), vec![0x0, 0x13, 0x37]);
|
|
|
|
assert_eq!(Format::Raw.format(0x0, &options), vec![0x0, 0x0]);
|
2024-05-12 20:25:48 +02:00
|
|
|
}
|
2024-05-12 20:29:40 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn set_format_checker() {
|
|
|
|
let mut options = FormatOptions::default();
|
|
|
|
assert_eq!(options.format(), Format::Hex);
|
|
|
|
options.set_format(Format::Base32);
|
|
|
|
assert_eq!(options.format(), Format::Base32);
|
|
|
|
options.set_format(Format::Base64);
|
|
|
|
assert_eq!(options.format(), Format::Base64);
|
2024-09-06 11:27:18 +02:00
|
|
|
options.set_format(Format::Raw);
|
|
|
|
assert_eq!(options.format(), Format::Raw);
|
2024-05-12 20:29:40 +02:00
|
|
|
}
|
2024-05-13 13:39:56 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn parser_dec() {
|
2024-09-06 12:46:54 +02:00
|
|
|
assert_eq!(numf_parser_str::<u32>("1337").unwrap(), 1337);
|
|
|
|
assert_eq!(numf_parser_str::<u32>("0d1337").unwrap(), 1337);
|
2024-05-13 13:39:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn parser_bin() {
|
2024-09-06 12:46:54 +02:00
|
|
|
assert_eq!(numf_parser_str::<u32>("0b11001").unwrap(), 0b11001);
|
|
|
|
assert_eq!(numf_parser_str::<u32>("0b11001").unwrap(), 0b11001);
|
2024-05-13 13:39:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn parser_hex() {
|
2024-09-06 12:46:54 +02:00
|
|
|
assert_eq!(numf_parser_str::<u32>("0xdeadbeef").unwrap(), 0xdeadbeef);
|
2024-05-13 13:39:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn parser_oct() {
|
2024-09-06 12:46:54 +02:00
|
|
|
assert_eq!(numf_parser_str::<u32>("0o771171").unwrap(), 0o771171);
|
2024-05-13 13:39:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn parser_b64() {
|
2024-09-06 12:46:54 +02:00
|
|
|
assert_eq!(numf_parser_str::<u32>("0sQUFCQg==").unwrap(), 0x41414242);
|
2024-05-13 13:39:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn parser_b32() {
|
2024-09-06 12:46:54 +02:00
|
|
|
assert_eq!(numf_parser_str::<u32>("032sIFAUEQQ=").unwrap(), 0x41414242);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn parser_raw() {
|
|
|
|
assert_eq!(numf_parser_str::<u32>("\x00\x50\x60").unwrap(), 0x5060);
|
2024-05-13 13:51:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn parser_generics() {
|
2024-09-06 12:46:54 +02:00
|
|
|
assert_eq!(numf_parser_str::<u8>("55").unwrap(), 55);
|
|
|
|
assert_eq!(numf_parser_str::<u16>("55").unwrap(), 55);
|
|
|
|
assert_eq!(numf_parser_str::<u32>("55").unwrap(), 55);
|
|
|
|
assert_eq!(numf_parser_str::<u64>("55").unwrap(), 55);
|
|
|
|
assert_eq!(numf_parser_str::<u128>("55").unwrap(), 55);
|
2024-05-13 13:39:56 +02:00
|
|
|
}
|