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

View File

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