use rand::Rng;
use std::collections::HashMap;
use crate::common::StringUtils;
use super::CharacterSet;
#[test]
fn test_random() {
let mut r = rand::thread_rng();
let mut bytes: Vec<u8> = vec![0; 1000];
bytes.fill_with(|| r.gen());
assert_eq!(
CharacterSet::UTF8,
StringUtils::guessCharset(&bytes, &HashMap::new()).unwrap()
);
}
#[test]
fn test_short_shift_jis1() {
do_test(&[0x8b, 0xe0, 0x8b, 0x9b], CharacterSet::Shift_JIS, "SJIS");
}
#[test]
fn test_short_iso885911() {
do_test(&[0x62, 0xe5, 0x64], CharacterSet::ISO8859_1, "ISO8859_1");
}
#[test]
fn test_short_utf8() {
do_test(
&[0x45, 0x73, 0x70, 0x61, 0xc3, 0xb1, 0x6f, 0x6c],
CharacterSet::UTF8,
"UTF8",
);
}
#[test]
fn test_mixed_shift_jis1() {
do_test(
&[0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x8b, 0xe0, 0x21],
CharacterSet::Shift_JIS,
"SJIS",
);
}
#[test]
fn test_utf16_be() {
do_test(
&[0xFE, 0xFF, 0x8c, 0x03, 0x53, 0x8b, 0x67, 0xdc],
CharacterSet::UTF16BE,
CharacterSet::UTF16BE.get_charset_name(),
);
}
#[test]
fn test_utf16_le() {
do_test(
&[0xFF, 0xFE, 0x03, 0x8c, 0x8b, 0x53, 0xdc, 0x67],
CharacterSet::UTF16LE,
CharacterSet::UTF16LE.get_charset_name(),
);
}
fn do_test(bytes: &[u8], charset: CharacterSet, encoding: &str) {
let guessedCharset = StringUtils::guessCharset(bytes, &HashMap::new()).unwrap();
let guessedEncoding = StringUtils::guessEncoding(bytes, &HashMap::new()).unwrap();
assert_eq!(charset, guessedCharset);
assert_eq!(encoding, guessedEncoding);
}