File size: 7,590 Bytes
f0f4f2b |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 |
use crate::entity::NormalizerSettings;
use crate::tests::FILES_SAMPLES;
use crate::utils::*;
use encoding::DecoderTrap;
use std::fs::File;
use std::io::Read;
use std::path::PathBuf;
#[test]
fn test_is_unicode_range_secondary() {
assert!(!is_unicode_range_secondary("Something"));
assert!(is_unicode_range_secondary("Extended"));
}
#[test]
fn test_unicode_range() {
for _ in 1..10 {
let tests = [
('a', "Basic Latin"),
('я', "Cyrillic"),
('ย', "Thai"),
('↓', "Arrows"),
('∅', "Mathematical Operators"),
('ͽ', "Greek and Coptic"),
];
for test in &tests {
assert_eq!(unicode_range(test.0), Some(test.1));
}
}
}
#[test]
fn test_is_ascii() {
let tests = [
('a', true),
('я', false),
('ย', false),
('↓', false),
('7', true),
];
for test in &tests {
assert_eq!(test.0.is_ascii(), test.1);
}
}
#[test]
fn test_remove_accent() {
let tests = [('á', 'a'), ('É', 'E'), ('Ǔ', 'U'), ('↓', '↓')];
for test in &tests {
assert_eq!(remove_accent(test.0), test.1);
}
}
#[test]
fn test_range_scan() {
let test = "aÁ[!Я";
let res = range_scan(test);
assert_eq!(res.len(), 3);
assert!(res.contains("Basic Latin"));
assert!(res.contains("Latin-1 Supplement"));
assert!(res.contains("Cyrillic"));
}
#[test]
fn test_is_multi_byte_encoding() {
let tests = [("utf-8", true), ("ascii", false), ("euc-jp", true)];
for test in &tests {
assert_eq!(is_multi_byte_encoding(test.0), test.1);
}
}
#[test]
fn test_identify_sig_or_bom() {
let tests = [
(
b"\xef\xbb\xbf lol kek".as_slice(),
Some("utf-8".to_string()),
),
(b"lol kek".as_slice(), None),
];
for test in &tests {
assert_eq!(identify_sig_or_bom(test.0).0, test.1);
}
}
#[test]
fn test_iana_name() {
let tests = [
("utf8", Some("utf-8")),
("csibm866", Some("ibm866")),
("whatever", None),
("korean", Some("euc-kr")),
];
for test in &tests {
assert_eq!(iana_name(test.0), test.1);
}
}
#[test]
fn test_is_cp_similar() {
let tests = [
("iso-8859-14", "windows-1254", true),
("iso-8859-14", "euc-kr", false),
];
for test in &tests {
assert_eq!(is_cp_similar(test.0, test.1), test.2);
}
}
#[test]
fn test_any_specified_encoding() {
let tests = [
(b"<head><meta charset=\"utf8\"".as_slice(), Some("utf-8".to_string())),
(b"<head coding='korean'> blah".as_slice(), Some("euc-kr".to_string())),
(
b"\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x01,\x00\x00\x00\xaf\x08\x06\x00\x00\x00G.\xe3\xb7\x00\x00\x1c\xfdIDATx".as_slice(),
None,
),
(b"<?xml version=\"1.0\" encoding=\"EUC-JP\"?>", Some("euc-jp".to_string())),
(b"<html><head><meta charset=\"utf-8\"></head></html>", Some("utf-8".to_string())),
(b"<html><head><meta charset=\"utf-57\"></head></html>", None),
(b"# coding: utf-8", Some("utf-8".to_string())),
(b"<?xml version=\"1.0\" encoding=\"UTF-8\"?>", Some("utf-8".to_string())),
(b"<?xml version=\"1.0\" encoding=\"US-ASCII\"?>", Some("windows-1252".to_string())),
(b"<html><head><meta charset=WINDOWS-1252></head></html>", Some("windows-1252".to_string())),
(b"<html><head><meta charset=\"WINDOWS-1256\"></head></html>", Some("windows-1256".to_string())),
];
for test in &tests {
assert_eq!(any_specified_encoding(test.0, 4096), test.1);
}
}
#[test]
fn test_cp_similarity() {
let tests = [
("iso-8859-14", "windows-1254", 0.75, 1.0), // high similarity
("windows-1250", "windows-1253", 0.5, 0.75), // low similarity
("iso-8859-14", "euc-kr", 0.0, 0.0), // eur-kr is multi-byte
];
for test in &tests {
let sim = cp_similarity(test.0, test.1);
assert!(
sim >= test.2,
"{} <-> {} found similarity is {}",
test.0,
test.1,
sim
);
assert!(
sim <= test.3,
"{} <-> {} found similarity is {}",
test.0,
test.1,
sim
);
}
}
#[test]
fn test_is_suspiciously_successive_range() {
let tests = [
(None, Some("Cyrillic"), true),
(Some("Cyrillic"), Some("Cyrillic"), false),
(Some("Latin"), Some("Latin Extended"), false),
(Some("Emoticons"), Some("Latin Extended"), false),
(
Some("Latin"),
Some("Combining Diacritical Marks Supplement"),
false,
),
(
Some("Cyrillic Extended-A"),
Some("Cyrillic Extended-B"),
false,
),
(Some("Hiragana"), Some("Katakana"), false),
(Some("Hiragana"), Some("CJK Radicals Supplement"), false),
(
Some("CJK Radicals Supplement"),
Some("Alphabetic Presentation Forms"),
false,
),
(Some("CJK Radicals Supplement"), Some("Punctuation"), false),
(Some("Cyrillic"), Some("Basic Latin"), true),
(Some("Cyrillic"), Some("Sundanese"), true),
];
for test in &tests {
assert_eq!(
is_suspiciously_successive_range(test.0, test.1),
test.2,
"<= {:?} {:?}",
test.0,
test.1,
);
}
}
#[test]
fn test_decode_test() {
let tests = [
(b"\x61\x52\x6f\x64\x20\x5a\x61\x52\x6f\x64\x20\x5a\xaa\xd8\x80\xd9\x80\xd9\x80\xd9\xb9\xd8\x80\xd9\x80\xd9\x80\xd9\x80\xd9\xaf\xd8\x8a\xd9\x80\xd9\x80\xd9\x84\xd9\xd8\x20\xd9\xa7\xd9\x84\xd9\x80\xd9\x80\xd8\x80\xd9\xaa\xd9\x80\xd9\x80\xd9\x80\xd9\x80\xd9\x88\xd9\x82\xd9\x80\xd9\x80\xd9\x8a\xd9\x80\xd9\x80\xd9\x80\xd8\x80\x20\xaa\x85\xd9\x80\xd9\x80\xd9\x80\xd9\x86\xd9\xd9\x20\xd9\x82\xd9\x80\xd8\x80\xd9\xa8\xd9\x80\xd9\x80\x00\x84".to_vec(), "euc-jp", false),
(b"\x61\x52\x6f\x64\x20\x5a\x61\x52\x6f\x64\x20\x5a\xaa\xd8".to_vec(), "windows-1251", true),
];
for test in &tests {
let res = decode(&test.0, test.1, DecoderTrap::Strict, true, false);
assert_eq!(res.is_ok(), test.2);
}
}
#[test]
fn test_decode_wrong_chunks() {
// read multibyte files, split to chunks (with non-complete sequences)
// and decode it without fail
// The idea is that decode function should ignore errors in the beginning and ending of chunk
let settings = NormalizerSettings::default();
for sample in &*FILES_SAMPLES {
if sample.1.iter().any(|e| is_multi_byte_encoding(e)) {
let mut path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
path.push(format!("src/tests/data/samples/{}", sample.0));
let mut file = File::open(path.to_str().unwrap()).expect("Cannot open file");
let mut buffer = Vec::new();
file.read_to_end(&mut buffer).expect("Cannot read file");
for chunk in buffer.chunks(settings.chunk_size) {
let status = decode(
chunk,
sample.1.first().unwrap(),
DecoderTrap::Strict,
true,
true,
);
assert!(
status.is_ok(),
"Decode error for sample {}, {}",
sample.0,
status.unwrap_err()
);
}
}
}
}
|