Main Page | Class Hierarchy | Data Structures | Directories | File List | Data Fields | Related Pages

UtfTest.java

00001 /*-
00002  * See the file LICENSE for redistribution information.
00003  *
00004  * Copyright (c) 2002-2005
00005  *      Sleepycat Software.  All rights reserved.
00006  *
00007  * $Id: UtfTest.java,v 12.1 2005/01/31 19:27:36 mark Exp $
00008  */
00009 
00010 package com.sleepycat.util.test;
00011 
00012 import java.io.DataOutputStream;
00013 import java.util.Arrays;
00014 
00015 import junit.framework.Test;
00016 import junit.framework.TestCase;
00017 import junit.framework.TestSuite;
00018 
00019 import com.sleepycat.collections.test.DbTestUtil;
00020 import com.sleepycat.util.FastOutputStream;
00021 import com.sleepycat.util.UtfOps;
00022 
00026 public class UtfTest extends TestCase {
00027 
00028     public static void main(String[] args)
00029         throws Exception {
00030 
00031         junit.framework.TestResult tr =
00032             junit.textui.TestRunner.run(suite());
00033         if (tr.errorCount() > 0 ||
00034             tr.failureCount() > 0) {
00035             System.exit(1);
00036         } else {
00037             System.exit(0);
00038         }
00039     }
00040 
00041     public static Test suite()
00042         throws Exception {
00043 
00044         TestSuite suite = new TestSuite(UtfTest.class);
00045         return suite;
00046     }
00047 
00048     public UtfTest(String name) {
00049 
00050         super(name);
00051     }
00052 
00053     public void setUp() {
00054 
00055         DbTestUtil.printTestName("UtfTest." + getName());
00056     }
00057 
00063     public void testMultibyte()
00064         throws Exception {
00065 
00066         char c = 0;
00067         byte[] buf = new byte[10];
00068         byte[] javaBuf = new byte[10];
00069         char[] cArray = new char[1];
00070         FastOutputStream javaBufStream = new FastOutputStream(javaBuf);
00071         DataOutputStream javaOutStream = new DataOutputStream(javaBufStream);
00072 
00073         try {
00074             for (int cInt = Character.MIN_VALUE; cInt <= Character.MAX_VALUE;
00075                  cInt += 1) {
00076                 c = (char) cInt;
00077                 cArray[0] = c;
00078                 int byteLen = UtfOps.getByteLength(cArray);
00079 
00080                 javaBufStream.reset();
00081                 javaOutStream.writeUTF(new String(cArray));
00082                 int javaByteLen = javaBufStream.size() - 2;
00083 
00084                 if (byteLen != javaByteLen) {
00085                     fail("Character 0x" + Integer.toHexString(c) +
00086                          " UtfOps size " + byteLen +
00087                          " != JavaIO size " + javaByteLen);
00088                 }
00089 
00090                 Arrays.fill(buf, (byte) 0);
00091                 UtfOps.charsToBytes(cArray, 0, buf, 0, 1);
00092 
00093                 if (byteLen == 1 && buf[0] == (byte) 0xff) {
00094                     fail("Character 0x" + Integer.toHexString(c) +
00095                          " was encoded as FF, which is reserved for null");
00096                 }
00097 
00098                 for (int i = 0; i < byteLen; i += 1) {
00099                     if (buf[i] != javaBuf[i + 2]) {
00100                         fail("Character 0x" + Integer.toHexString(c) +
00101                              " byte offset " + i +
00102                              " UtfOps byte " + Integer.toHexString(buf[i]) +
00103                              " != JavaIO byte " +
00104                              Integer.toHexString(javaBuf[i + 2]));
00105                     }
00106                 }
00107 
00108                 int charLen = UtfOps.getCharLength(buf, 0, byteLen);
00109                 if (charLen != 1) {
00110                     fail("Character 0x" + Integer.toHexString(c) +
00111                          " UtfOps char len " + charLen +
00112                          " but should be one");
00113                 }
00114 
00115                 cArray[0] = (char) 0;
00116                 int len = UtfOps.bytesToChars(buf, 0, cArray, 0, byteLen,
00117                                               true);
00118                 if (len != byteLen) {
00119                     fail("Character 0x" + Integer.toHexString(c) +
00120                          " UtfOps bytesToChars(w/byteLen) len " + len +
00121                          " but should be " + byteLen);
00122                 }
00123 
00124                 if (cArray[0] != c) {
00125                     fail("Character 0x" + Integer.toHexString(c) +
00126                          " UtfOps bytesToChars(w/byteLen) char " +
00127                          Integer.toHexString(cArray[0]));
00128                 }
00129 
00130                 cArray[0] = (char) 0;
00131                 len = UtfOps.bytesToChars(buf, 0, cArray, 0, 1, false);
00132                 if (len != byteLen) {
00133                     fail("Character 0x" + Integer.toHexString(c) +
00134                          " UtfOps bytesToChars(w/charLen) len " + len +
00135                          " but should be " + byteLen);
00136                 }
00137 
00138                 if (cArray[0] != c) {
00139                     fail("Character 0x" + Integer.toHexString(c) +
00140                          " UtfOps bytesToChars(w/charLen) char " +
00141                          Integer.toHexString(cArray[0]));
00142                 }
00143 
00144                 String s = new String(cArray, 0, 1);
00145                 byte[] sBytes = UtfOps.stringToBytes(s);
00146                 if (sBytes.length != byteLen) {
00147                     fail("Character 0x" + Integer.toHexString(c) +
00148                          " UtfOps stringToBytes() len " + sBytes.length +
00149                          " but should be " + byteLen);
00150                 }
00151 
00152                 for (int i = 0; i < byteLen; i += 1) {
00153                     if (sBytes[i] != javaBuf[i + 2]) {
00154                         fail("Character 0x" + Integer.toHexString(c) +
00155                              " byte offset " + i +
00156                              " UtfOps byte " + Integer.toHexString(sBytes[i]) +
00157                              " != JavaIO byte " +
00158                              Integer.toHexString(javaBuf[i + 2]));
00159                     }
00160                 }
00161             }
00162         } catch (Exception e) {
00163             System.out.println("Character 0x" + Integer.toHexString(c) +
00164                                " exception occurred");
00165             throw e;
00166         }
00167     }
00168 }
00169 

Generated on Sun Dec 25 12:14:56 2005 for Berkeley DB 4.4.16 by  doxygen 1.4.2