]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-52_1/main/tests/core/src/com/ibm/icu/dev/test/util/TextTrieMapTest.java
Clean up imports.
[Dictionary.git] / jars / icu4j-52_1 / main / tests / core / src / com / ibm / icu / dev / test / util / TextTrieMapTest.java
1 /*
2  *******************************************************************************
3  * Copyright (C) 2007, International Business Machines Corporation and         *
4  * others. All Rights Reserved.                                                *
5  *******************************************************************************
6 */
7 package com.ibm.icu.dev.test.util;
8
9 import java.util.Iterator;
10
11 import com.ibm.icu.dev.test.TestFmwk;
12 import com.ibm.icu.impl.TextTrieMap;
13
14 public class TextTrieMapTest extends TestFmwk {
15
16     private static final Integer SUN = new Integer(1);
17     private static final Integer MON = new Integer(2);
18     private static final Integer TUE = new Integer(3);
19     private static final Integer WED = new Integer(4);
20     private static final Integer THU = new Integer(5);
21     private static final Integer FRI = new Integer(6);
22     private static final Integer SAT = new Integer(7);
23
24     private static final Integer FOO = new Integer(-1);
25     private static final Integer BAR = new Integer(-2);
26     
27     private static final Object[][] TESTDATA = {
28         {"Sunday", SUN},
29         {"Monday", MON},
30         {"Tuesday", TUE},
31         {"Wednesday", WED},
32         {"Thursday", THU},
33         {"Friday", FRI},
34         {"Saturday", SAT},
35         {"Sun", SUN},
36         {"Mon", MON},
37         {"Tue", TUE},
38         {"Wed", WED},
39         {"Thu", THU},
40         {"Fri", FRI},
41         {"Sat", SAT},
42         {"S", SUN},
43         {"M", MON},
44         {"T", TUE},
45         {"W", WED},
46         {"T", THU},
47         {"F", FRI},
48         {"S", SAT}
49     };
50
51     private static final Object[][] TESTCASES = {
52         {"Sunday", SUN, SUN},
53         {"sunday", null, SUN},
54         {"Mo", MON, MON},
55         {"mo", null, MON},
56         {"Thursday Friday", THU, THU},
57         {"T", new Object[]{TUE, THU}, new Object[]{TUE, THU}},
58         {"TEST", new Object[]{TUE, THU}, new Object[]{TUE, THU}},
59         {"SUN", new Object[]{SUN, SAT}, SUN},
60         {"super", null, SUN},
61         {"NO", null, null}
62     };
63     
64     public static void main(String[] args) throws Exception {
65         TextTrieMapTest test = new TextTrieMapTest();
66         test.run(args);
67     }
68
69     public void TestCaseSensitive() {
70         Iterator itr = null;
71         TextTrieMap map = new TextTrieMap(false);
72         for (int i = 0; i < TESTDATA.length; i++) {
73             map.put((String)TESTDATA[i][0], TESTDATA[i][1]);
74         }
75
76         logln("Test for get(String)");
77         for (int i = 0; i < TESTCASES.length; i++) {
78             itr = map.get((String)TESTCASES[i][0]);
79             checkResult(itr, TESTCASES[i][1]);
80         }
81
82         logln("Test for get(String, int)");
83         StringBuffer textBuf = new StringBuffer();
84         for (int i = 0; i < TESTCASES.length; i++) {
85             textBuf.setLength(0);
86             for (int j = 0; j < i; j++) {
87                 textBuf.append('X');
88             }
89             textBuf.append(TESTCASES[i][0]);
90             itr = map.get(textBuf.toString(), i);
91             checkResult(itr, TESTCASES[i][1]);
92         }
93
94         // Add duplicated entry
95         map.put("Sunday", FOO);
96         // Add duplicated entry with different casing
97         map.put("sunday", BAR);
98
99         // Make sure the all entries are returned
100         itr = map.get("Sunday");
101         checkResult(itr, new Object[]{FOO, SUN});
102     }
103
104     public void TestCaseInsensitive() {
105         Iterator itr = null;
106         TextTrieMap map = new TextTrieMap(true);
107         for (int i = 0; i < TESTDATA.length; i++) {
108             map.put((String)TESTDATA[i][0], TESTDATA[i][1]);
109         }
110
111         logln("Test for get(String)");
112         for (int i = 0; i < TESTCASES.length; i++) {
113             itr = map.get((String)TESTCASES[i][0]);
114             checkResult(itr, TESTCASES[i][2]);
115         }
116         
117         logln("Test for get(String, int)");
118         StringBuffer textBuf = new StringBuffer();
119         for (int i = 0; i < TESTCASES.length; i++) {
120             textBuf.setLength(0);
121             for (int j = 0; j < i; j++) {
122                 textBuf.append('X');
123             }
124             textBuf.append(TESTCASES[i][0]);
125             itr = map.get(textBuf.toString(), i);
126             checkResult(itr, TESTCASES[i][2]);
127         }
128
129         // Add duplicated entry
130         map.put("Sunday", FOO);
131         // Add duplicated entry with different casing
132         map.put("sunday", BAR);
133
134         // Make sure the all entries are returned
135         itr = map.get("Sunday");
136         checkResult(itr, new Object[]{SUN, FOO, BAR});
137     }
138
139     private boolean eql(Object o1, Object o2) {
140         if (o1 == null || o2 == null) {
141             if (o1 == null && o2 == null) {
142                 return true;
143             }
144             return false;
145         }
146         return o1.equals(o2);
147     }
148
149     private void checkResult(Iterator itr, Object expected) {
150         if (itr == null) {
151             if (expected != null) {
152                 errln("FAIL: Empty results - Expected: " + expected);
153             }
154             return;
155         }
156         if (expected == null && itr != null) {
157             errln("FAIL: Empty result is expected");
158             return;
159         }
160
161         Object[] exp;
162         if (expected instanceof Object[]) {
163             exp = (Object[])expected;
164         } else {
165             exp = new Object[]{expected};
166         }
167
168         boolean[] found = new boolean[exp.length];
169         while (itr.hasNext()) {
170             Object val = itr.next();
171             for (int i = 0; i < exp.length; i++) {
172                 if (eql(exp[i], val)) {
173                     found[i] = true;
174                 }
175             }
176         }
177         for (int i = 0; i < exp.length; i++) {
178             if (found[i] == false) {
179                 errln("FAIL: The search result does not contain " + exp[i]);
180             }
181         }
182     }
183 }