List:Commits« Previous MessageNext Message »
From:John David Duncan Date:December 3 2009 3:08am
Subject:bzr commit into mysql-5.1-telco-6.3-ndbjtie branch (jdd:3145)
View as plain text  
#At file:///Users/jdd/Desktop/dev/6.3-ndbjtie/ based on revid:jdd@stripped9qd21hp8d

 3145 John David Duncan	2009-12-02
      clean up MySqlUtilsCharsetMapTest.java

    modified:
      storage/ndb/ndbjtie/test/test/MySqlUtilsCharsetMapTest.java
=== modified file 'storage/ndb/ndbjtie/test/test/MySqlUtilsCharsetMapTest.java'
--- a/storage/ndb/ndbjtie/test/test/MySqlUtilsCharsetMapTest.java	2009-12-02 22:52:48 +0000
+++ b/storage/ndb/ndbjtie/test/test/MySqlUtilsCharsetMapTest.java	2009-12-03 03:07:41 +0000
@@ -66,16 +66,16 @@ public class MySqlUtilsCharsetMapTest {
         int len = c.length;
         ByteBuffer bb = ByteBuffer.allocateDirect(len);
         for(int i = 0 ; i < len ; i++) 
-            bb.put((byte) c[i]);
-        
+            bb.put((byte) c[i]);        
+        bb.rewind();
         return bb;
     }
 
     String bbdump (ByteBuffer sbb) {
         ByteBuffer bb = sbb.asReadOnlyBuffer();
-        bb.rewind();
         byte[] bytes = new byte[bb.capacity()];
         bb.get(bytes);
+        bb.rewind();
         return Arrays.toString(bytes);
     }
     
@@ -83,8 +83,6 @@ public class MySqlUtilsCharsetMapTest {
     int bbcmp(ByteBuffer sbb1, ByteBuffer sbb2) {
         ByteBuffer bb1 = sbb1.asReadOnlyBuffer();
         ByteBuffer bb2 = sbb2.asReadOnlyBuffer();
-        bb1.rewind();
-        bb2.rewind();
         Byte b1, b2;
         do {
             b1 = bb1.get();
@@ -99,8 +97,6 @@ public class MySqlUtilsCharsetMapTest {
     int bbncmp(ByteBuffer sbb1, ByteBuffer sbb2, int n) {
         ByteBuffer bb1 = sbb1.asReadOnlyBuffer();
         ByteBuffer bb2 = sbb2.asReadOnlyBuffer();
-        bb1.rewind();
-        bb2.rewind();
         Byte b1, b2;
         int i = 0;
         do {
@@ -113,34 +109,41 @@ public class MySqlUtilsCharsetMapTest {
         return 0;
     }
     
+    public void printRecodeResult(int rcode, int lengths[], ByteBuffer b1,
+                                  ByteBuffer b2)
+    {
+        out.println("         Return code: " + rcode + " Len0: "
+                    + lengths[0] + " Len1: " + lengths[1]  + "\n"
+                    + "         " + bbdump(b1) + " => " + bbdump(b2)
+                    );
+    }
+        
+    
     public void test() {
+        int latin1_num, utf8_num, utf16_num;
         out.println("--> MySqlUtilsCharsetMapTest.test()");
 
         // load native library (better diagnostics doing it explicitely)
         loadSystemLibrary("ndbjtie");
-        
         CharsetMap csmap = CharsetMap.create();
-        int utf8_num = csmap.getUTF8CharsetNumber();
-        int utf16_num = csmap.getUTF16CharsetNumber();
-        
-        /* If this mysql build does not include UTF-8 and either UCS-2 or UTF-16 
-         then the test suite must fail. 
-         */    
-        out.println("UTF-8 charset num: " +  utf8_num + 
+                        
+        out.println("  --> Test that mysql includes UTF-8 and 16-bit Unicode");
+        utf8_num = csmap.getUTF8CharsetNumber();
+        utf16_num = csmap.getUTF16CharsetNumber();
+        out.println("       UTF-8 charset num: " +  utf8_num + 
                     "     UTF-16 or UCS-2 charset num: " +  utf16_num);
         assert( ! ((utf8_num == 0) || (utf16_num == 0)));
+        out.println("  <-- Test that mysql includes UTF-8 and 16-bit Unicode");
         
-        /* test csmap.getName()
-         */
-        String utf8 = csmap.getName(utf8_num);
-        assert(utf8.compareTo("UTF-8") == 0);
         
-        /* MySQL 5.1 and earlier will have UCS-2 but later versions may have true
-         UTF-16.  For information, print whether UTF-16 or UCS-2 is being used. 
-         */
+        out.println("  --> Test CharsetMap::getName()");
+        String utf8_name = csmap.getName(utf8_num);
         String utf16 = csmap.getMysqlName(csmap.getUTF16CharsetNumber());
-        out.println("Using mysql \"" + utf16 + "\" for UTF-16.");
-        
+        assert(utf8_name.compareTo("UTF-8") == 0);
+        /* MySQL 5.1 and earlier will have UCS-2 but later versions may have true
+         UTF-16.  For information, print whether UTF-16 or UCS-2 is being used. */
+        out.println("       Using mysql \"" + utf16 + "\" for UTF-16.");
+        out.println("  <-- Test CharsetMap::getName()");
         
         /* Now we're going to recode. 
          We test with the string "ülker", which begins with the character
@@ -154,108 +157,115 @@ public class MySqlUtilsCharsetMapTest {
         final char[] cmy_word_truncated = new char[] { 0xC3, 0xBC, 'l', 'k', 0 };
         final char[] cmy_bad_utf8       = new char[] { 'l' , 0xBC, 'a', 'd', 0 };
         
-        ByteBuffer my_word_latin1    = char2bb(cmy_word_latin1);
-        ByteBuffer my_word_utf8      = char2bb(cmy_word_utf8);
-        ByteBuffer my_word_truncated = char2bb(cmy_word_truncated);
-        ByteBuffer my_bad_utf8       = char2bb(cmy_bad_utf8);
-        
-        ByteBuffer result_buff_1 = ByteBuffer.allocateDirect(32);
-        ByteBuffer result_buff_2 = ByteBuffer.allocateDirect(32);
-        ByteBuffer result_buff_too_small = ByteBuffer.allocateDirect(32);
-        int[] lengths = new int[2];
-        
-        /* latin1 must be available to run the recode test */
-        int latin1_num = csmap.getCharsetNumber("latin1");    
-        out.println("latin1 charset number: " + latin1_num + 
-                    " standard name: " + csmap.getName(latin1_num));
-        assert(latin1_num != 0);
-        assert(csmap.getName(latin1_num).compareTo("windows-1252") == 0);
-        
-        out.println("Latin1 source string: " + bbdump(my_word_latin1) + "\n" +
-                    "UTF8 source string:   " + bbdump(my_word_utf8));
-        
-        /* RECODE TEST 1: recode from UTF-8 to Latin 1 */
-        lengths[0] = 7;
-        lengths[1] = 32;
-        my_word_utf8.rewind();
-        result_buff_1.rewind();
-        int rr1 = csmap.recode(lengths, utf8_num, latin1_num, 
-                               my_word_utf8, result_buff_1);
-        out.println("Recode Test 1 - UTF-8 to Latin-1:  " + rr1 + " "
-                    + lengths[0] + " " + lengths[1]  + "\n"
-                    + bbdump(my_word_utf8) + " => "
-                    + bbdump(result_buff_1));
-        assert(rr1 == CharsetMapConst.RecodeStatus.RECODE_OK);
-        assert(lengths[0] == 7);
-        assert(lengths[1] == 6);
-        assert(bbcmp(my_word_latin1, result_buff_1) == 0);
-        
-        /* RECODE TEST 2: recode from Latin1 to to UTF-8 */
-        lengths[0] = 6;
-        lengths[1] = 32;
-        my_word_latin1.rewind();
-        result_buff_2.rewind();
-        int rr2 = csmap.recode(lengths, latin1_num, utf8_num,
-                               my_word_latin1, result_buff_2);
-        out.println("Recode Test 2 - Latin-1 to UTF-8: " + rr2 + " " + lengths[0]
-                    + " " + lengths[1] + "\n" 
-                    + bbdump(my_word_latin1)  + " => "
-                    + bbdump(result_buff_2) + "\"");
-        assert(rr2 == CharsetMapConst.RecodeStatus.RECODE_OK);
-        assert(lengths[0] == 6);
-        assert(lengths[1] == 7);
-        assert(bbcmp(result_buff_2, my_word_utf8) == 0);
-        
-        /* RECODE TEST 3: recode with a too-small result buffer */
-        lengths[0] = 6;
-        lengths[1] = 4;
-        int rr3 = csmap.recode(lengths, latin1_num, utf8_num,
-                               my_word_latin1, result_buff_too_small);
-        out.println("Recode Test 3 - too-small buffer: "+ rr2 + " " + lengths[0]
-                    + " " + lengths[1] + "\n" +
-                    bbdump(my_word_latin1) + " => " + 
-                    bbdump(result_buff_1));
-        assert(rr3 == CharsetMapConst.RecodeStatus.RECODE_BUFF_TOO_SMALL);
-        assert(lengths[0] == 3);
-        assert(lengths[1] == 4);
-        /* Confirm that the first four characters were indeed recoded: */
-        assert(bbncmp(result_buff_too_small, my_word_truncated, 4) == 0);
-        
-        /* RECODE TEST 4: recode with an invalid character set */
-        int rr4 = csmap.recode(lengths, 0, 999, my_word_latin1, result_buff_2);
-        out.println("Recode Test 4 - invalid charset: " + rr4);
-        assert(rr4 == CharsetMapConst.RecodeStatus.RECODE_BAD_CHARSET);
-        
-        /* RECODE TEST 5: source string is ill-formed UTF-8 */
-        lengths[0] = 5;
-        lengths[1] = 32;
-        my_bad_utf8.rewind();
-        result_buff_2.rewind();
-        int rr5 = csmap.recode(lengths, utf8_num, latin1_num, 
-                               my_bad_utf8, result_buff_2);
-        out.println("Recode Test 5 - ill-formed source string: " + rr5);
-        assert(rr5 == CharsetMapConst.RecodeStatus.RECODE_BAD_SRC);
-               
-        /* RECODE TEST 6: convert an actual java string to UTF-8 */
-        result_buff_1.rewind();
-        result_buff_2.rewind();
-        final String my_java_str = "\u00FClker";
-        CharBuffer cb = result_buff_1.asCharBuffer();
-        cb.append(my_java_str);
-        lengths[0] = 12;
-        lengths[1] = 32;
-        result_buff_1.rewind();
-        int rr6 = csmap.recode(lengths, utf16_num, utf8_num, 
-                               result_buff_1, result_buff_2);
-        out.println("Recode Test 6 - Java String to UTF-8 "+ rr6 + " " 
-                    + lengths[0] + " " + lengths[1] + "\n" +
-                    bbdump(result_buff_1) + " => " + 
-                    bbdump(result_buff_2));
-    
-        assert(lengths[0]) == 12;
-        assert(lengths[1]) == 7;
-        assert(bbncmp(result_buff_2, my_word_utf8, 6) == 0);        
+   
+        out.println("  --> CharsetMap::recode() Tests");
 
+        { 
+            ByteBuffer my_word_latin1    = char2bb(cmy_word_latin1);
+            ByteBuffer my_word_utf8      = char2bb(cmy_word_utf8);
+            out.println("     --> Test that latin1 is available.");
+            latin1_num = csmap.getCharsetNumber("latin1");    
+            out.println("         latin1 charset number: " + latin1_num + 
+                        " standard name: " + csmap.getName(latin1_num));
+            assert(latin1_num != 0);
+            assert(csmap.getName(latin1_num).compareTo("windows-1252") == 0);        
+            out.println("         Latin1 source string: " + bbdump(my_word_latin1) + "\n" +
+                        "         UTF8 source string:   " + bbdump(my_word_utf8));
+            out.println("     <-- Test that latin1 is available.");
+        }
+        
+        {
+            out.println("     --> RECODE TEST 1: recode from UTF-8 to Latin 1");
+            ByteBuffer my_word_utf8      = char2bb(cmy_word_utf8);
+            ByteBuffer result_buff       = ByteBuffer.allocateDirect(16);
+            int[] lengths = new int[]  { 7 , 16 };
+            
+            int rr1 = csmap.recode(lengths, utf8_num, latin1_num, 
+                                   my_word_utf8, result_buff);
+            printRecodeResult(rr1, lengths, my_word_utf8, result_buff);
+            assert(rr1 == CharsetMapConst.RecodeStatus.RECODE_OK);
+            assert(lengths[0] == 7);
+            assert(lengths[1] == 6);
+            assert(bbcmp(char2bb(cmy_word_latin1), result_buff) == 0);
+            out.println("     <-- RECODE TEST 1");
+        }        
+        
+        {
+            out.println("     --> RECODE TEST 2: recode from Latin1 to to UTF-8");
+            ByteBuffer my_word_latin1    = char2bb(cmy_word_latin1);
+            ByteBuffer result_buff       = ByteBuffer.allocateDirect(16);
+            int[] lengths = new int[]  { 6 , 16 };
+
+            int rr2 = csmap.recode(lengths, latin1_num, utf8_num,
+                                   my_word_latin1, result_buff);
+            printRecodeResult(rr2, lengths, my_word_latin1, result_buff);
+            assert(rr2 == CharsetMapConst.RecodeStatus.RECODE_OK);
+            assert(lengths[0] == 6);
+            assert(lengths[1] == 7);
+            assert(bbcmp(result_buff, char2bb(cmy_word_utf8)) == 0);
+            out.println("     <-- RECODE TEST 2");
+        }
+
+        {
+            out.println("     --> RECODE TEST 3: too-small result buffer");
+            ByteBuffer my_word_latin1    = char2bb(cmy_word_latin1);
+            ByteBuffer result_buff       = ByteBuffer.allocateDirect(16);
+            ByteBuffer my_word_truncated = char2bb(cmy_word_truncated);
+            int[] lengths = new int[]  { 6 , 4 };   // 4 is too small
+  
+            int rr3 = csmap.recode(lengths, latin1_num, utf8_num,
+                               my_word_latin1, result_buff);
+            printRecodeResult(rr3, lengths, my_word_latin1, result_buff);
+            assert(rr3 == CharsetMapConst.RecodeStatus.RECODE_BUFF_TOO_SMALL);
+            assert(lengths[0] == 3);
+            assert(lengths[1] == 4);
+            /* Confirm that the first four characters were indeed recoded: */
+            assert(bbncmp(result_buff, char2bb(cmy_word_truncated), 4) == 0);
+            out.println("     <-- RECODE TEST 3");
+        }
+
+        {
+            out.println("     --> RECODE TEST 4: invalid character set");
+            ByteBuffer my_word_latin1    = char2bb(cmy_word_latin1);
+            ByteBuffer result_buff       = ByteBuffer.allocateDirect(16);
+            int[] lengths = new int[]  { 6 , 16 };           
+            int rr4 = csmap.recode(lengths, 0, 999, my_word_latin1, result_buff);
+            out.println("          Return code: " + rr4);
+            assert(rr4 == CharsetMapConst.RecodeStatus.RECODE_BAD_CHARSET);
+            out.println("     <-- RECODE TEST 4");
+        }
+        
+        {
+            out.println("     --> RECODE TEST 5: source string is ill-formed UTF-8");
+            ByteBuffer my_bad_utf8       = char2bb(cmy_bad_utf8);
+            ByteBuffer result_buff       = ByteBuffer.allocateDirect(16);
+            int[] lengths = new int[]  { 5 , 16 };          
+            int rr5 = csmap.recode(lengths, utf8_num, latin1_num, 
+                                   my_bad_utf8, result_buff);
+            out.println("          Return code: " + rr5);
+            assert(rr5 == CharsetMapConst.RecodeStatus.RECODE_BAD_SRC);
+            out.println("     <-- RECODE TEST 5");
+        }
+        
+        {
+            out.println("     --> RECODE TEST 6: convert an actual java string to UTF-8");
+            // Load the string into a ByteBuffer
+            ByteBuffer str_bb = ByteBuffer.allocateDirect(16);
+            CharBuffer cb = str_bb.asCharBuffer();
+            cb.append("\u00FClker");
+            cb.rewind();
+            ByteBuffer result_buff       = ByteBuffer.allocateDirect(16);            
+            int[] lengths = new int[]  { 12 , 16 };
+
+            int rr6 = csmap.recode(lengths, utf16_num, utf8_num, 
+                                   str_bb, result_buff);
+            printRecodeResult(rr6, lengths, str_bb, result_buff);            
+            assert(lengths[0]) == 12;
+            assert(lengths[1]) == 7;
+            assert(bbncmp(result_buff, char2bb(cmy_word_utf8), 6) == 0);        
+            out.println("     <-- RECODE TEST 6");
+        }
+        
         out.println();
         out.println("<-- MySqlUtilsCharsetMapTest.test()");
     };

Attachment: [text/bzr-bundle] bzr/jdd@sun.com-20091203030741-gqt40rrvfkfi1n48.bundle
Thread
bzr commit into mysql-5.1-telco-6.3-ndbjtie branch (jdd:3145)John David Duncan3 Dec