Skip to content

Commit

Permalink
Clippy fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
mdecimus committed Jan 8, 2024
1 parent 598cba3 commit 4ff4c57
Show file tree
Hide file tree
Showing 8 changed files with 260 additions and 304 deletions.
2 changes: 1 addition & 1 deletion resources/date.json
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@
{
"header": " 4 8 15 16 23 42, 4 8 15 16 23 42, 4 8 15 16 23 42, ",
"expected": {
"year": 1915,
"year": 2015,
"month": 8,
"day": 4,
"hour": 16,
Expand Down
180 changes: 90 additions & 90 deletions src/decoders/base64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,96 +290,6 @@ impl<'x> MessageStream<'x> {
}
}

#[cfg(test)]
mod tests {
use crate::parsers::MessageStream;

#[test]
fn decode_base64() {
for (encoded_str, expected_result) in [
("VGVzdA==", "Test"),
("WWU=", "Ye"),
("QQ==", "A"),
("cm8=", "ro"),
(
"QXJlIHlvdSBhIFNoaW1hbm8gb3IgQ2FtcGFnbm9sbyBwZXJzb24/",
"Are you a Shimano or Campagnolo person?",
),
(
"PCFET0NUWVBFIGh0bWw+CjxodG1sPgo8Ym9keT4KPC9ib2R5Pgo8L2h0bWw+Cg==",
"<!DOCTYPE html>\n<html>\n<body>\n</body>\n</html>\n",
),
(
"PCFET0NUWVBFIGh0bWw+CjxodG1sPg\no8Ym9ke\nT4KPC 9ib2R5Pg\n o8L2h0bWw+Cg==",
"<!DOCTYPE html>\n<html>\n<body>\n</body>\n</html>\n",
),
("w6HDqcOtw7PDug==", "áéíóú"),
("====", ""),
("w6HDq!cOtw7PDug=", ""),
("w6 HD qcOt", "áéí"),
("cmáé", ""),
("áé", ""),
("w\n6\nH\nD\nq\nc\nO\nt\nw\n7\n P\tD u g\n==", "áéíóú"),
("w6HDqcOtw7PDug==", "áéíóú"),
] {
assert_eq!(
super::base64_decode(encoded_str.as_bytes()).unwrap_or_default(),
expected_result.as_bytes(),
"Failed for {encoded_str:?}",
);
}
}

#[test]
fn decode_base64_mime() {
for (encoded_str, expected_result) in [
("VGVzdA==\r\n--boundary\n", "Test"),
(
"PCFET0NUWVBFIGh0bWw+CjxodG1sPg\no8Ym9ke\nT4KPC 9ib2R5Pg\n o8L2h0bWw+Cg==\r\n--boundary--\r\n",
"<!DOCTYPE html>\n<html>\n<body>\n</body>\n</html>\n",
),
("w6HDqcOtw7PDug==\r\n--boundary \n", "áéíóú"),
("w\n6\nH\nD\nq\nc\nO\nt\nw\n7\n P\tD u g\n==\r\n--boundary\n", "áéíóú"),
("w6HDqcOtw7PDug==--boundary", "áéíóú"),
(
"w6HDqcOtw7PDug==\n--boundary--",
"áéíóú",
),
(
"w\n6\nH\nD\nq\nc\nO\nt\nw\n7\n P\tD u g\n==\n--boundary",
"áéíóú",
),
] {
let mut s = MessageStream::new(encoded_str.as_bytes());
let (_, result) = s.decode_base64_mime(b"boundary");

assert_eq!(
result,
expected_result.as_bytes(),
"Failed for {encoded_str:?}",
);
}
}

#[test]
fn decode_base64_word() {
for (encoded_str, expected_result) in [
("w 6 H D q c O t w 7 P D u g== ?=", "áéíóú"),
("w6HDqcOtw7PDug==?=", "áéíóú"),
("w6HDqc\n Otw7PDug==?=", "áéíóú"),
("w6HDqcOtw7PDug================?=", "áéíóú"),
("?=", ""),
] {
let mut s = MessageStream::new(encoded_str.as_bytes());
assert_eq!(
s.decode_base64_word().unwrap(),
expected_result.as_bytes(),
"Failed for {encoded_str:?}",
);
}
}
}

/*
* Table adapted from Nick Galbreath's "High performance base64 encoder / decoder"
*
Expand Down Expand Up @@ -550,3 +460,93 @@ pub static BASE64_MAP: &[&[u32]] = &[
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
],
];

#[cfg(test)]
mod tests {
use crate::parsers::MessageStream;

#[test]
fn decode_base64() {
for (encoded_str, expected_result) in [
("VGVzdA==", "Test"),
("WWU=", "Ye"),
("QQ==", "A"),
("cm8=", "ro"),
(
"QXJlIHlvdSBhIFNoaW1hbm8gb3IgQ2FtcGFnbm9sbyBwZXJzb24/",
"Are you a Shimano or Campagnolo person?",
),
(
"PCFET0NUWVBFIGh0bWw+CjxodG1sPgo8Ym9keT4KPC9ib2R5Pgo8L2h0bWw+Cg==",
"<!DOCTYPE html>\n<html>\n<body>\n</body>\n</html>\n",
),
(
"PCFET0NUWVBFIGh0bWw+CjxodG1sPg\no8Ym9ke\nT4KPC 9ib2R5Pg\n o8L2h0bWw+Cg==",
"<!DOCTYPE html>\n<html>\n<body>\n</body>\n</html>\n",
),
("w6HDqcOtw7PDug==", "áéíóú"),
("====", ""),
("w6HDq!cOtw7PDug=", ""),
("w6 HD qcOt", "áéí"),
("cmáé", ""),
("áé", ""),
("w\n6\nH\nD\nq\nc\nO\nt\nw\n7\n P\tD u g\n==", "áéíóú"),
("w6HDqcOtw7PDug==", "áéíóú"),
] {
assert_eq!(
super::base64_decode(encoded_str.as_bytes()).unwrap_or_default(),
expected_result.as_bytes(),
"Failed for {encoded_str:?}",
);
}
}

#[test]
fn decode_base64_mime() {
for (encoded_str, expected_result) in [
("VGVzdA==\r\n--boundary\n", "Test"),
(
"PCFET0NUWVBFIGh0bWw+CjxodG1sPg\no8Ym9ke\nT4KPC 9ib2R5Pg\n o8L2h0bWw+Cg==\r\n--boundary--\r\n",
"<!DOCTYPE html>\n<html>\n<body>\n</body>\n</html>\n",
),
("w6HDqcOtw7PDug==\r\n--boundary \n", "áéíóú"),
("w\n6\nH\nD\nq\nc\nO\nt\nw\n7\n P\tD u g\n==\r\n--boundary\n", "áéíóú"),
("w6HDqcOtw7PDug==--boundary", "áéíóú"),
(
"w6HDqcOtw7PDug==\n--boundary--",
"áéíóú",
),
(
"w\n6\nH\nD\nq\nc\nO\nt\nw\n7\n P\tD u g\n==\n--boundary",
"áéíóú",
),
] {
let mut s = MessageStream::new(encoded_str.as_bytes());
let (_, result) = s.decode_base64_mime(b"boundary");

assert_eq!(
result,
expected_result.as_bytes(),
"Failed for {encoded_str:?}",
);
}
}

#[test]
fn decode_base64_word() {
for (encoded_str, expected_result) in [
("w 6 H D q c O t w 7 P D u g== ?=", "áéíóú"),
("w6HDqcOtw7PDug==?=", "áéíóú"),
("w6HDqc\n Otw7PDug==?=", "áéíóú"),
("w6HDqcOtw7PDug================?=", "áéíóú"),
("?=", ""),
] {
let mut s = MessageStream::new(encoded_str.as_bytes());
assert_eq!(
s.decode_base64_word().unwrap(),
expected_result.as_bytes(),
"Failed for {encoded_str:?}",
);
}
}
}
54 changes: 27 additions & 27 deletions src/decoders/charsets/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,33 +81,6 @@ pub fn no_op(_bytes: &[u8]) -> String {
"".to_string()
}

#[cfg(test)]
mod tests {
use super::charset_decoder;

#[test]
#[allow(clippy::uninlined_format_args)]
fn decoder_charset() {
let inputs = [
"l8",
//"utf-8",
"utf-7",
//"US-Ascii",
"csgb18030",
"iso-8859-1",
"extended_unix_code_packed_format_for_japanese",
];

for input in inputs {
assert!(
charset_decoder(input.as_bytes()).is_some(),
"Failed for {}",
input
);
}
}
}

// Perfect hashing table for charset names
static CH_HASH: &[u32] = &[
765, 765, 765, 765, 765, 765, 765, 765, 765, 765, 765, 765, 765, 765, 765, 765, 765, 765, 765,
Expand Down Expand Up @@ -1648,3 +1621,30 @@ static FNC_MAP: &[for<'x> fn(&'x [u8]) -> String; 758] = &[
no_op,
decoder_gb18030,
];

#[cfg(test)]
mod tests {
use super::charset_decoder;

#[test]
#[allow(clippy::uninlined_format_args)]
fn decoder_charset() {
let inputs = [
"l8",
//"utf-8",
"utf-7",
//"US-Ascii",
"csgb18030",
"iso-8859-1",
"extended_unix_code_packed_format_for_japanese",
];

for input in inputs {
assert!(
charset_decoder(input.as_bytes()).is_some(),
"Failed for {}",
input
);
}
}
}
Loading

0 comments on commit 4ff4c57

Please sign in to comment.