progress on string utils tests

This commit is contained in:
Henry Schimke
2022-08-29 18:08:53 -05:00
parent 3389d7d53a
commit 057a61db18
2 changed files with 42 additions and 36 deletions

View File

@@ -14,58 +14,61 @@
* limitations under the License.
*/
package com.google.zxing.common;
// package com.google.zxing.common;
import org.junit.Assert;
import org.junit.Test;
// import org.junit.Assert;
// import org.junit.Test;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Random;
// import java.nio.charset.Charset;
// import java.nio.charset.StandardCharsets;
// import java.util.Random;
/**
* Tests {@link StringUtils}.
*/
public final class StringUtilsTestCase extends Assert {
use rand::{Rng};
use encoding::{Encoding};
use std::{collections::HashMap, hash::Hash};
@Test
public void testRandom() {
Random r = new Random(1234L);
byte[] bytes = new byte[1000];
r.nextBytes(bytes);
assertEquals(Charset.defaultCharset(), StringUtils.guessCharset(bytes, null));
use crate::common::StringUtils;
#[test]
fn testRandom() {
let mut r = rand::thread_rng();
let bytes : Vec<u8> = vec![0;1000];
for i in 0..bytes.len() {
bytes[i] = r.gen();
}
assert_eq!(encoding::all::UTF_8.name(), StringUtils::guessCharset(&bytes, HashMap::new()).name());
}
@Test
public void testShortShiftJIS1() {
#[test]
fn testShortShiftJIS1() {
// 金魚
doTest(new byte[] { (byte) 0x8b, (byte) 0xe0, (byte) 0x8b, (byte) 0x9b, }, StringUtils.SHIFT_JIS_CHARSET, "SJIS");
doTest(&vec![ 0x8b, 0xe0, 0x8b, 0x9b, ], encoding::label::encoding_from_whatwg_label("SJIS").unwrap(), "SJIS");
}
@Test
public void testShortISO885911() {
#[test]
fn testShortISO885911() {
// båd
doTest(new byte[] { (byte) 0x62, (byte) 0xe5, (byte) 0x64, }, StandardCharsets.ISO_8859_1, "ISO8859_1");
}
@Test
public void testShortUTF81() {
#[test]
fn testShortUTF81() {
// Español
doTest(new byte[] { (byte) 0x45, (byte) 0x73, (byte) 0x70, (byte) 0x61, (byte) 0xc3,
(byte) 0xb1, (byte) 0x6f, (byte) 0x6c },
StandardCharsets.UTF_8, "UTF8");
}
@Test
public void testMixedShiftJIS1() {
#[test]
fn testMixedShiftJIS1() {
// Hello 金!
doTest(new byte[] { (byte) 0x48, (byte) 0x65, (byte) 0x6c, (byte) 0x6c, (byte) 0x6f,
(byte) 0x20, (byte) 0x8b, (byte) 0xe0, (byte) 0x21, },
StringUtils.SHIFT_JIS_CHARSET, "SJIS");
}
@Test
public void testUTF16BE() {
#[test]
fn testUTF16BE() {
// 调压柜
doTest(new byte[] { (byte) 0xFE, (byte) 0xFF, (byte) 0x8c, (byte) 0x03, (byte) 0x53, (byte) 0x8b,
(byte) 0x67, (byte) 0xdc, },
@@ -73,8 +76,8 @@ public final class StringUtilsTestCase extends Assert {
StandardCharsets.UTF_16.name());
}
@Test
public void testUTF16LE() {
#[test]
fn testUTF16LE() {
// 调压柜
doTest(new byte[] { (byte) 0xFF, (byte) 0xFE, (byte) 0x03, (byte) 0x8c, (byte) 0x8b, (byte) 0x53,
(byte) 0xdc, (byte) 0x67, },
@@ -82,11 +85,11 @@ public final class StringUtilsTestCase extends Assert {
StandardCharsets.UTF_16.name());
}
private static void doTest(byte[] bytes, Charset charset, String encoding) {
Charset guessedCharset = StringUtils.guessCharset(bytes, null);
String guessedEncoding = StringUtils.guessEncoding(bytes, null);
assertEquals(charset, guessedCharset);
assertEquals(encoding, guessedEncoding);
fn doTest(bytes :&Vec<u8>, charset : &dyn Encoding, encoding: &str) {
let guessedCharset = StringUtils::guessCharset(bytes, HashMap::new());
let guessedEncoding = StringUtils::guessEncoding(bytes, HashMap::new());
assert_eq!(charset.name(), guessedCharset.name());
assert_eq!(encoding, guessedEncoding);
}
/**
@@ -96,7 +99,7 @@ public final class StringUtilsTestCase extends Assert {
*
* @param args command line arguments
*/
public static void main(String[] args) {
fn main(String[] args) {
String text = args[0];
Charset charset = Charset.forName(args[1]);
StringBuilder declaration = new StringBuilder();
@@ -114,4 +117,4 @@ public final class StringUtilsTestCase extends Assert {
System.out.println(declaration);
}
}
// }

View File

@@ -11,6 +11,9 @@ use crate::DecodeHintType;
use crate::RXingResultPoint;
use encoding::Encoding;
#[cfg(test)]
mod StringUtilsTestCase;
/*
* Copyright (C) 2010 ZXing authors
*