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