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