• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /netgear-WNDR4500v2-V1.0.0.60_1.0.38/ap/gpl/timemachine/db-4.7.25.NC/test/scr024/src/com/sleepycat/util/test/
1/*-
2 * See the file LICENSE for redistribution information.
3 *
4 * Copyright (c) 2002,2008 Oracle.  All rights reserved.
5 *
6 * $Id: UtfTest.java,v 12.7 2008/02/07 17:12:33 mark Exp $
7 */
8
9package com.sleepycat.util.test;
10
11import java.io.DataOutputStream;
12import java.util.Arrays;
13
14import junit.framework.Test;
15import junit.framework.TestCase;
16import junit.framework.TestSuite;
17
18import com.sleepycat.util.FastOutputStream;
19import com.sleepycat.util.UtfOps;
20import com.sleepycat.util.test.SharedTestUtils;
21
22/**
23 * @author Mark Hayes
24 */
25public class UtfTest extends TestCase {
26
27    public static void main(String[] args)
28        throws Exception {
29
30        junit.framework.TestResult tr =
31            junit.textui.TestRunner.run(suite());
32        if (tr.errorCount() > 0 ||
33            tr.failureCount() > 0) {
34            System.exit(1);
35        } else {
36            System.exit(0);
37        }
38    }
39
40    public static Test suite()
41        throws Exception {
42
43        TestSuite suite = new TestSuite(UtfTest.class);
44        return suite;
45    }
46
47    public UtfTest(String name) {
48
49        super(name);
50    }
51
52    public void setUp() {
53
54        SharedTestUtils.printTestName("UtfTest." + getName());
55    }
56
57    /**
58     * Compares the UtfOps implementation to the java.util.DataOutputStream
59     * (and by implication DataInputStream) implementation, character for
60     * character in the full Unicode set.
61     */
62    public void testMultibyte()
63        throws Exception {
64
65        char c = 0;
66        byte[] buf = new byte[10];
67        byte[] javaBuf = new byte[10];
68        char[] cArray = new char[1];
69        FastOutputStream javaBufStream = new FastOutputStream(javaBuf);
70        DataOutputStream javaOutStream = new DataOutputStream(javaBufStream);
71
72        try {
73            for (int cInt = Character.MIN_VALUE; cInt <= Character.MAX_VALUE;
74                 cInt += 1) {
75                c = (char) cInt;
76                cArray[0] = c;
77                int byteLen = UtfOps.getByteLength(cArray);
78
79                javaBufStream.reset();
80                javaOutStream.writeUTF(new String(cArray));
81                int javaByteLen = javaBufStream.size() - 2;
82
83                if (byteLen != javaByteLen) {
84                    fail("Character 0x" + Integer.toHexString(c) +
85                         " UtfOps size " + byteLen +
86                         " != JavaIO size " + javaByteLen);
87                }
88
89                Arrays.fill(buf, (byte) 0);
90                UtfOps.charsToBytes(cArray, 0, buf, 0, 1);
91
92                if (byteLen == 1 && buf[0] == (byte) 0xff) {
93                    fail("Character 0x" + Integer.toHexString(c) +
94                         " was encoded as FF, which is reserved for null");
95                }
96
97                for (int i = 0; i < byteLen; i += 1) {
98                    if (buf[i] != javaBuf[i + 2]) {
99                        fail("Character 0x" + Integer.toHexString(c) +
100                             " byte offset " + i +
101                             " UtfOps byte " + Integer.toHexString(buf[i]) +
102                             " != JavaIO byte " +
103                             Integer.toHexString(javaBuf[i + 2]));
104                    }
105                }
106
107                int charLen = UtfOps.getCharLength(buf, 0, byteLen);
108                if (charLen != 1) {
109                    fail("Character 0x" + Integer.toHexString(c) +
110                         " UtfOps char len " + charLen +
111                         " but should be one");
112                }
113
114                cArray[0] = (char) 0;
115                int len = UtfOps.bytesToChars(buf, 0, cArray, 0, byteLen,
116                                              true);
117                if (len != byteLen) {
118                    fail("Character 0x" + Integer.toHexString(c) +
119                         " UtfOps bytesToChars(w/byteLen) len " + len +
120                         " but should be " + byteLen);
121                }
122
123                if (cArray[0] != c) {
124                    fail("Character 0x" + Integer.toHexString(c) +
125                         " UtfOps bytesToChars(w/byteLen) char " +
126                         Integer.toHexString(cArray[0]));
127                }
128
129                cArray[0] = (char) 0;
130                len = UtfOps.bytesToChars(buf, 0, cArray, 0, 1, false);
131                if (len != byteLen) {
132                    fail("Character 0x" + Integer.toHexString(c) +
133                         " UtfOps bytesToChars(w/charLen) len " + len +
134                         " but should be " + byteLen);
135                }
136
137                if (cArray[0] != c) {
138                    fail("Character 0x" + Integer.toHexString(c) +
139                         " UtfOps bytesToChars(w/charLen) char " +
140                         Integer.toHexString(cArray[0]));
141                }
142
143                String s = new String(cArray, 0, 1);
144                byte[] sBytes = UtfOps.stringToBytes(s);
145                if (sBytes.length != byteLen) {
146                    fail("Character 0x" + Integer.toHexString(c) +
147                         " UtfOps stringToBytes() len " + sBytes.length +
148                         " but should be " + byteLen);
149                }
150
151                for (int i = 0; i < byteLen; i += 1) {
152                    if (sBytes[i] != javaBuf[i + 2]) {
153                        fail("Character 0x" + Integer.toHexString(c) +
154                             " byte offset " + i +
155                             " UtfOps byte " + Integer.toHexString(sBytes[i]) +
156                             " != JavaIO byte " +
157                             Integer.toHexString(javaBuf[i + 2]));
158                    }
159                }
160            }
161        } catch (Exception e) {
162            System.out.println("Character 0x" + Integer.toHexString(c) +
163                               " exception occurred");
164            throw e;
165        }
166    }
167}
168
169