001: /*
002: *******************************************************************************
003: * Copyright (C) 2006, International Business Machines Corporation and *
004: * others. All Rights Reserved. *
005: *******************************************************************************
006: */
007: package com.ibm.icu.dev.test.util;
008:
009: import com.ibm.icu.dev.test.TestFmwk;
010: import com.ibm.icu.impl.TextTrieMap;
011:
012: public class TextTrieMapTest extends TestFmwk {
013:
014: private static final Integer SUN = new Integer(1);
015: private static final Integer MON = new Integer(2);
016: private static final Integer TUE = new Integer(3);
017: private static final Integer WED = new Integer(4);
018: private static final Integer THU = new Integer(5);
019: private static final Integer FRI = new Integer(6);
020: private static final Integer SAT = new Integer(7);
021:
022: private static final Integer FOO = new Integer(-1);
023: private static final Integer BAR = new Integer(-2);
024:
025: private static final Object[][] TESTDATA = { { "Sunday", SUN },
026: { "Monday", MON }, { "Tuesday", TUE },
027: { "Wednesday", WED }, { "Thursday", THU },
028: { "Friday", FRI }, { "Saturday", SAT }, { "Sun", SUN },
029: { "Mon", MON }, { "Tue", TUE }, { "Wed", WED },
030: { "Thu", THU }, { "Fri", FRI }, { "Sat", SAT },
031: { "S", SUN }, { "M", MON }, { "T", TUE }, { "W", WED },
032: { "T", THU }, { "F", FRI }, { "S", SAT } };
033:
034: private static final Object[][] TESTCASES = {
035: { "Sunday", SUN, SUN }, { "sunday", null, SUN },
036: { "Mo", MON, MON }, { "mo", null, MON },
037: { "Thursday Friday", THU, THU }, { "T", THU, THU },
038: { "TEST", THU, THU }, { "SUN", SAT, SUN },
039: { "super", null, SAT }, { "NO", null, null } };
040:
041: public static void main(String[] args) throws Exception {
042: TextTrieMapTest test = new TextTrieMapTest();
043: test.run(args);
044: }
045:
046: public void TestCaseSensitive() {
047: TextTrieMap map = new TextTrieMap(false);
048: for (int i = 0; i < TESTDATA.length; i++) {
049: map.put((String) TESTDATA[i][0], TESTDATA[i][1]);
050: }
051:
052: logln("Test for get(String)");
053: for (int i = 0; i < TESTCASES.length; i++) {
054: Object value = map.get((String) TESTCASES[i][0]);
055: if (!eql(value, TESTCASES[i][1])) {
056: errln("Invalid search results - Expected:"
057: + TESTCASES[i][1] + " Actual:" + value);
058: }
059: }
060:
061: logln("Test for get(String, int)");
062: StringBuffer textBuf = new StringBuffer();
063: for (int i = 0; i < TESTCASES.length; i++) {
064: textBuf.setLength(0);
065: for (int j = 0; j < i; j++) {
066: textBuf.append('X');
067: }
068: textBuf.append(TESTCASES[i][0]);
069: Object value = map.get(textBuf.toString(), i);
070: if (!eql(value, TESTCASES[i][1])) {
071: errln("Invalid search results - Expected:"
072: + TESTCASES[i][1] + " Actual:" + value);
073: }
074: }
075:
076: // Add duplicated entry
077: Object prev = map.put("Sunday", FOO);
078: if (!eql(prev, SUN)) {
079: errln("The previous value of duplicated entry is not valid - Expected:"
080: + SUN + " Actual:" + prev);
081: }
082: // Make sure the value is updated
083: Object value = map.get("Sunday");
084: if (!eql(value, FOO)) {
085: errln("The map value is not valid - Expected:" + FOO
086: + " Actual:" + value);
087: }
088:
089: // Add duplicated entry with different casing
090: prev = map.put("sunday", BAR);
091: if (!eql(prev, null)) {
092: errln("The value should be new in the trie map - Expected:null"
093: + " Actual:" + prev);
094: }
095: // Make sure the value is valid
096: value = map.get("sunday");
097: if (!eql(value, BAR)) {
098: errln("The map value is not valid - Expected:" + BAR
099: + " Actual:" + value);
100: }
101:
102: }
103:
104: public void TestCaseInsensitive() {
105: TextTrieMap map = new TextTrieMap(true);
106: for (int i = 0; i < TESTDATA.length; i++) {
107: map.put((String) TESTDATA[i][0], TESTDATA[i][1]);
108: }
109:
110: logln("Test for get(String)");
111: for (int i = 0; i < TESTCASES.length; i++) {
112: Object value = map.get((String) TESTCASES[i][0]);
113: if (!eql(value, TESTCASES[i][2])) {
114: errln("Invalid search results - Expected:"
115: + TESTCASES[i][2] + " Actual:" + value);
116: }
117: }
118:
119: logln("Test for get(String, int)");
120: StringBuffer textBuf = new StringBuffer();
121: for (int i = 0; i < TESTCASES.length; i++) {
122: textBuf.setLength(0);
123: for (int j = 0; j < i; j++) {
124: textBuf.append('X');
125: }
126: textBuf.append(TESTCASES[i][0]);
127: Object value = map.get(textBuf.toString(), i);
128: if (!eql(value, TESTCASES[i][2])) {
129: errln("Invalid search results - Expected:"
130: + TESTCASES[i][2] + " Actual:" + value);
131: }
132: }
133:
134: // Add duplicated entry
135: Object prev = map.put("Sunday", FOO);
136: if (!eql(prev, SUN)) {
137: errln("The previous value of duplicated entry is not valid - Expected:"
138: + SUN + " Actual:" + prev);
139: }
140: // Make sure the value is updated
141: Object value = map.get("Sunday");
142: if (!eql(value, FOO)) {
143: errln("The map value is not valid - Expected:" + FOO
144: + " Actual:" + value);
145: }
146:
147: // Add duplicated entry with different casing
148: prev = map.put("sunday", BAR);
149: if (!eql(prev, FOO)) {
150: errln("The value should be new in the trie map - Expected:"
151: + FOO + " Actual:" + prev);
152: }
153: // Make sure the value is updated
154: value = map.get("sunday");
155: if (!eql(value, BAR)) {
156: errln("The map value is not valid - Expected:" + BAR
157: + " Actual:" + value);
158: }
159:
160: }
161:
162: private boolean eql(Object o1, Object o2) {
163: if (o1 == null || o2 == null) {
164: if (o1 == null && o2 == null) {
165: return true;
166: }
167: return false;
168: }
169: return o1.equals(o2);
170: }
171: }
|