]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_2_1-src/src/com/ibm/icu/dev/test/util/ICUBinaryTest.java
icu4jsrc
[Dictionary.git] / jars / icu4j-4_2_1-src / src / com / ibm / icu / dev / test / util / ICUBinaryTest.java
1 /*\r
2 *******************************************************************************\r
3 * Copyright (C) 1996-2008, International Business Machines Corporation and    *\r
4 * others. All Rights Reserved.                                                *\r
5 *******************************************************************************\r
6 */\r
7 \r
8 package com.ibm.icu.dev.test.util;\r
9 \r
10 import com.ibm.icu.dev.test.TestFmwk;\r
11 import com.ibm.icu.impl.ICUBinary;\r
12 import java.io.IOException;\r
13 import java.io.ByteArrayInputStream;\r
14 \r
15 /**\r
16 * Testing class for Trie. Tests here will be simple, since both CharTrie and \r
17 * IntTrie are very similar and are heavily used in other parts of ICU4J.\r
18 * Codes using Tries are expected to have detailed tests.\r
19 * @author Syn Wee Quek\r
20 * @since release 2.1 Jan 01 2002\r
21 */\r
22 public final class ICUBinaryTest extends TestFmwk \r
23\r
24     // constructor ---------------------------------------------------\r
25   \r
26     /**\r
27     * Constructor\r
28     */\r
29     public ICUBinaryTest()\r
30     {\r
31     }\r
32       \r
33     // public methods -----------------------------------------------\r
34     \r
35     public static void main(String arg[]) \r
36     {\r
37         ICUBinaryTest test = new ICUBinaryTest();\r
38         try {\r
39             test.run(arg);\r
40         } catch (Exception e) {\r
41             test.errln("Error testing icubinarytest");\r
42         }\r
43     }\r
44     \r
45     /**\r
46      * Testing the constructors of the Tries\r
47      */\r
48     public void TestReadHeader() \r
49     {\r
50         byte formatid[] = {1, 2, 3, 4};\r
51         byte array[] = {\r
52             // header size\r
53             0, 0x18, \r
54             // magic numbers\r
55             (byte)0xda, 0x27, \r
56             // size\r
57             0, 0, \r
58             // reserved word\r
59             0, 0, \r
60             // bigendian\r
61             1, \r
62             // charset \r
63             0,\r
64             // charsize\r
65             2, \r
66             // reserved byte\r
67             0, \r
68             // data format id\r
69             1, 2, 3, 4,\r
70             // dataVersion\r
71             1, 2, 3, 4,\r
72             // unicodeVersion\r
73             3, 2, 0, 0\r
74         };\r
75         ByteArrayInputStream inputstream = new ByteArrayInputStream(array);\r
76         ICUBinary.Authenticate authenticate \r
77                 = new ICUBinary.Authenticate() {\r
78                     public boolean isDataVersionAcceptable(byte version[])\r
79                     {\r
80                         return version[0] == 1;\r
81                     }\r
82                 };\r
83         // check full data version\r
84         try {\r
85             ICUBinary.readHeader(inputstream, formatid, authenticate);\r
86         } catch (IOException e) {\r
87             errln("Failed: Lenient authenticate object should pass ICUBinary.readHeader");\r
88         }\r
89         // no restriction to the data version\r
90         try {\r
91             inputstream.reset();\r
92             ICUBinary.readHeader(inputstream, formatid, null);\r
93         } catch (IOException e) {\r
94             errln("Failed: Null authenticate object should pass ICUBinary.readHeader");\r
95         }\r
96         // lenient data version\r
97         try {\r
98             inputstream.reset();\r
99             ICUBinary.readHeader(inputstream, formatid, authenticate);\r
100         } catch (IOException e) {\r
101             errln("Failed: Lenient authenticate object should pass ICUBinary.readHeader");\r
102         }\r
103         // changing the version to an incorrect one, expecting failure\r
104         array[16] = 2;\r
105         try {\r
106             inputstream.reset();\r
107             ICUBinary.readHeader(inputstream, formatid, authenticate);\r
108             errln("Failed: Invalid version number should not pass authenticate object");\r
109         } catch (IOException e) {\r
110             logln("PASS: ICUBinary.readHeader with invalid version number failed as expected");\r
111         }\r
112     }\r
113 }\r
114 \r