001: /*-
002: * See the file LICENSE for redistribution information.
003: *
004: * Copyright (c) 2002,2008 Oracle. All rights reserved.
005: *
006: * $Id: UtfTest.java,v 1.18.2.2 2008/01/07 15:14:36 cwl Exp $
007: */
008:
009: package com.sleepycat.util.test;
010:
011: import java.io.DataOutputStream;
012: import java.util.Arrays;
013:
014: import junit.framework.Test;
015: import junit.framework.TestCase;
016: import junit.framework.TestSuite;
017:
018: import com.sleepycat.collections.test.DbTestUtil;
019: import com.sleepycat.util.FastOutputStream;
020: import com.sleepycat.util.UtfOps;
021:
022: /**
023: * @author Mark Hayes
024: */
025: public class UtfTest extends TestCase {
026:
027: public static void main(String[] args) throws Exception {
028:
029: junit.framework.TestResult tr = junit.textui.TestRunner
030: .run(suite());
031: if (tr.errorCount() > 0 || tr.failureCount() > 0) {
032: System.exit(1);
033: } else {
034: System.exit(0);
035: }
036: }
037:
038: public static Test suite() throws Exception {
039:
040: TestSuite suite = new TestSuite(UtfTest.class);
041: return suite;
042: }
043:
044: public UtfTest(String name) {
045:
046: super (name);
047: }
048:
049: public void setUp() {
050:
051: DbTestUtil.printTestName("UtfTest." + getName());
052: }
053:
054: /**
055: * Compares the UtfOps implementation to the java.util.DataOutputStream
056: * (and by implication DataInputStream) implementation, character for
057: * character in the full Unicode set.
058: */
059: public void testMultibyte() throws Exception {
060:
061: char c = 0;
062: byte[] buf = new byte[10];
063: byte[] javaBuf = new byte[10];
064: char[] cArray = new char[1];
065: FastOutputStream javaBufStream = new FastOutputStream(javaBuf);
066: DataOutputStream javaOutStream = new DataOutputStream(
067: javaBufStream);
068:
069: try {
070: for (int cInt = Character.MIN_VALUE; cInt <= Character.MAX_VALUE; cInt += 1) {
071: c = (char) cInt;
072: cArray[0] = c;
073: int byteLen = UtfOps.getByteLength(cArray);
074:
075: javaBufStream.reset();
076: javaOutStream.writeUTF(new String(cArray));
077: int javaByteLen = javaBufStream.size() - 2;
078:
079: if (byteLen != javaByteLen) {
080: fail("Character 0x" + Integer.toHexString(c)
081: + " UtfOps size " + byteLen
082: + " != JavaIO size " + javaByteLen);
083: }
084:
085: Arrays.fill(buf, (byte) 0);
086: UtfOps.charsToBytes(cArray, 0, buf, 0, 1);
087:
088: if (byteLen == 1 && buf[0] == (byte) 0xff) {
089: fail("Character 0x"
090: + Integer.toHexString(c)
091: + " was encoded as FF, which is reserved for null");
092: }
093:
094: for (int i = 0; i < byteLen; i += 1) {
095: if (buf[i] != javaBuf[i + 2]) {
096: fail("Character 0x" + Integer.toHexString(c)
097: + " byte offset " + i + " UtfOps byte "
098: + Integer.toHexString(buf[i])
099: + " != JavaIO byte "
100: + Integer.toHexString(javaBuf[i + 2]));
101: }
102: }
103:
104: int charLen = UtfOps.getCharLength(buf, 0, byteLen);
105: if (charLen != 1) {
106: fail("Character 0x" + Integer.toHexString(c)
107: + " UtfOps char len " + charLen
108: + " but should be one");
109: }
110:
111: cArray[0] = (char) 0;
112: int len = UtfOps.bytesToChars(buf, 0, cArray, 0,
113: byteLen, true);
114: if (len != byteLen) {
115: fail("Character 0x" + Integer.toHexString(c)
116: + " UtfOps bytesToChars(w/byteLen) len "
117: + len + " but should be " + byteLen);
118: }
119:
120: if (cArray[0] != c) {
121: fail("Character 0x" + Integer.toHexString(c)
122: + " UtfOps bytesToChars(w/byteLen) char "
123: + Integer.toHexString(cArray[0]));
124: }
125:
126: cArray[0] = (char) 0;
127: len = UtfOps.bytesToChars(buf, 0, cArray, 0, 1, false);
128: if (len != byteLen) {
129: fail("Character 0x" + Integer.toHexString(c)
130: + " UtfOps bytesToChars(w/charLen) len "
131: + len + " but should be " + byteLen);
132: }
133:
134: if (cArray[0] != c) {
135: fail("Character 0x" + Integer.toHexString(c)
136: + " UtfOps bytesToChars(w/charLen) char "
137: + Integer.toHexString(cArray[0]));
138: }
139:
140: String s = new String(cArray, 0, 1);
141: byte[] sBytes = UtfOps.stringToBytes(s);
142: if (sBytes.length != byteLen) {
143: fail("Character 0x" + Integer.toHexString(c)
144: + " UtfOps stringToBytes() len "
145: + sBytes.length + " but should be "
146: + byteLen);
147: }
148:
149: for (int i = 0; i < byteLen; i += 1) {
150: if (sBytes[i] != javaBuf[i + 2]) {
151: fail("Character 0x" + Integer.toHexString(c)
152: + " byte offset " + i + " UtfOps byte "
153: + Integer.toHexString(sBytes[i])
154: + " != JavaIO byte "
155: + Integer.toHexString(javaBuf[i + 2]));
156: }
157: }
158: }
159: } catch (Exception e) {
160: System.out.println("Character 0x" + Integer.toHexString(c)
161: + " exception occurred");
162: throw e;
163: }
164: }
165: }
|