1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38 package ffx.utilities;
39
40 import static org.junit.Assert.assertEquals;
41
42 import org.junit.Test;
43
44
45 public class Hybrid36Test {
46
47 private static int random_seed = 13;
48
49 private static void checkDecodeException(int width, String s, String expected_msg) {
50 String msg = "";
51 try {
52 Hybrid36.decode(width, s);
53 } catch (Error e) {
54 msg = e.toString();
55 }
56 checkString(msg, "java.lang.Error: " + expected_msg);
57 }
58
59 private static void checkEncodeException(int width, int value, String expected_msg) {
60 String msg = "";
61 try {
62 Hybrid36.encode(width, value);
63 } catch (Error e) {
64 msg = e.toString();
65 }
66 checkString(msg, "java.lang.Error: " + expected_msg);
67 }
68
69 private static void checkInt(int result, int expected) {
70 assertEquals(" checkInt", result, expected);
71 if (result != expected) {
72 System.out.println("" + result + " != " + expected);
73 }
74 }
75
76 private static void checkString(String result, String expected) {
77 assertEquals(" checkString", result, expected);
78 if (!result.equals(expected)) {
79 System.out.println("\"" + result + "\" != \"" + expected + "\"");
80 }
81 }
82
83 private static int kernighan_and_ritchie_rand() {
84 random_seed = random_seed * 1103515245 + 12345;
85 int result = (random_seed / 65536) % 32768;
86 if (result < 0) {
87 result += 32768;
88 }
89 return result;
90 }
91
92 private static void recycle4(int value, String encoded) {
93 String s = Hybrid36.encode(4, value);
94 checkString(s, encoded);
95 int d = Hybrid36.decode(4, s);
96 checkInt(d, value);
97 }
98
99 private static void recycle5(int value, String encoded) {
100 String s = Hybrid36.encode(5, value);
101 checkString(s, encoded);
102 int d = Hybrid36.decode(5, s);
103 checkInt(d, value);
104 }
105
106
107 @Test
108 public void testDecode() {
109 checkInt(Hybrid36.decode(4, " "), 0);
110 checkInt(Hybrid36.decode(4, " -0"), 0);
111 checkInt(Hybrid36.decode(5, " "), 0);
112 checkInt(Hybrid36.decode(5, " -0"), 0);
113 checkDecodeException(4, "", "invalid number literal.");
114 checkDecodeException(4, " 0", "invalid number literal.");
115 checkDecodeException(4, " abc", "invalid number literal.");
116 checkDecodeException(4, "abc-", "invalid number literal.");
117 checkDecodeException(4, "A=BC", "invalid number literal.");
118 checkDecodeException(4, "40a0", "invalid number literal.");
119 checkDecodeException(4, "40A0", "invalid number literal.");
120 checkDecodeException(5, "", "invalid number literal.");
121 checkDecodeException(5, " 0", "invalid number literal.");
122 checkDecodeException(5, " abcd", "invalid number literal.");
123 checkDecodeException(5, "ABCD-", "invalid number literal.");
124 checkDecodeException(5, "a=bcd", "invalid number literal.");
125 checkDecodeException(5, "410b0", "invalid number literal.");
126 checkDecodeException(5, "410B0", "invalid number literal.");
127 checkDecodeException(3, "AAA", "unsupported width.");
128 checkDecodeException(6, "zzzzzz", "unsupported width.");
129 }
130
131
132 @Test
133 public void testEncode() {
134 recycle4(-999, "-999");
135 recycle4(-78, " -78");
136 recycle4(-6, " -6");
137 recycle4(0, " 0");
138 recycle4(9999, "9999");
139 recycle4(10000, "A000");
140 recycle4(10001, "A001");
141 recycle4(10002, "A002");
142 recycle4(10003, "A003");
143 recycle4(10004, "A004");
144 recycle4(10005, "A005");
145 recycle4(10006, "A006");
146 recycle4(10007, "A007");
147 recycle4(10008, "A008");
148 recycle4(10009, "A009");
149 recycle4(10010, "A00A");
150 recycle4(10011, "A00B");
151 recycle4(10012, "A00C");
152 recycle4(10013, "A00D");
153 recycle4(10014, "A00E");
154 recycle4(10015, "A00F");
155 recycle4(10016, "A00G");
156 recycle4(10017, "A00H");
157 recycle4(10018, "A00I");
158 recycle4(10019, "A00J");
159 recycle4(10020, "A00K");
160 recycle4(10021, "A00L");
161 recycle4(10022, "A00M");
162 recycle4(10023, "A00N");
163 recycle4(10024, "A00O");
164 recycle4(10025, "A00P");
165 recycle4(10026, "A00Q");
166 recycle4(10027, "A00R");
167 recycle4(10028, "A00S");
168 recycle4(10029, "A00T");
169 recycle4(10030, "A00U");
170 recycle4(10031, "A00V");
171 recycle4(10032, "A00W");
172 recycle4(10033, "A00X");
173 recycle4(10034, "A00Y");
174 recycle4(10035, "A00Z");
175 recycle4(10036, "A010");
176 recycle4(10046, "A01A");
177 recycle4(10071, "A01Z");
178 recycle4(10072, "A020");
179 recycle4(10000 + 36 * 36 - 1, "A0ZZ");
180 recycle4(10000 + 36 * 36, "A100");
181 recycle4(10000 + 36 * 36 * 36 - 1, "AZZZ");
182 recycle4(10000 + 36 * 36 * 36, "B000");
183 recycle4(10000 + 26 * 36 * 36 * 36 - 1, "ZZZZ");
184 recycle4(10000 + 26 * 36 * 36 * 36, "a000");
185 recycle4(10000 + 26 * 36 * 36 * 36 + 35, "a00z");
186 recycle4(10000 + 26 * 36 * 36 * 36 + 36, "a010");
187 recycle4(10000 + 26 * 36 * 36 * 36 + 36 * 36 - 1, "a0zz");
188 recycle4(10000 + 26 * 36 * 36 * 36 + 36 * 36, "a100");
189 recycle4(10000 + 26 * 36 * 36 * 36 + 36 * 36 * 36 - 1, "azzz");
190 recycle4(10000 + 26 * 36 * 36 * 36 + 36 * 36 * 36, "b000");
191 recycle4(10000 + 2 * 26 * 36 * 36 * 36 - 1, "zzzz");
192 recycle5(-9999, "-9999");
193 recycle5(-123, " -123");
194 recycle5(-45, " -45");
195 recycle5(-6, " -6");
196 recycle5(0, " 0");
197 recycle5(12, " 12");
198 recycle5(345, " 345");
199 recycle5(6789, " 6789");
200 recycle5(99999, "99999");
201 recycle5(100000, "A0000");
202 recycle5(100010, "A000A");
203 recycle5(100035, "A000Z");
204 recycle5(100036, "A0010");
205 recycle5(100046, "A001A");
206 recycle5(100071, "A001Z");
207 recycle5(100072, "A0020");
208 recycle5(100000 + 36 * 36 - 1, "A00ZZ");
209 recycle5(100000 + 36 * 36, "A0100");
210 recycle5(100000 + 36 * 36 * 36 - 1, "A0ZZZ");
211 recycle5(100000 + 36 * 36 * 36, "A1000");
212 recycle5(100000 + 36 * 36 * 36 * 36 - 1, "AZZZZ");
213 recycle5(100000 + 36 * 36 * 36 * 36, "B0000");
214 recycle5(100000 + 2 * 36 * 36 * 36 * 36, "C0000");
215 recycle5(100000 + 26 * 36 * 36 * 36 * 36 - 1, "ZZZZZ");
216 recycle5(100000 + 26 * 36 * 36 * 36 * 36, "a0000");
217 recycle5(100000 + 26 * 36 * 36 * 36 * 36 + 36 - 1, "a000z");
218 recycle5(100000 + 26 * 36 * 36 * 36 * 36 + 36, "a0010");
219 recycle5(100000 + 26 * 36 * 36 * 36 * 36 + 36 * 36 - 1, "a00zz");
220 recycle5(100000 + 26 * 36 * 36 * 36 * 36 + 36 * 36, "a0100");
221 recycle5(100000 + 26 * 36 * 36 * 36 * 36 + 36 * 36 * 36 - 1, "a0zzz");
222 recycle5(100000 + 26 * 36 * 36 * 36 * 36 + 36 * 36 * 36, "a1000");
223 recycle5(100000 + 26 * 36 * 36 * 36 * 36 + 36 * 36 * 36 * 36 - 1, "azzzz");
224 recycle5(100000 + 26 * 36 * 36 * 36 * 36 + 36 * 36 * 36 * 36, "b0000");
225 recycle5(100000 + 2 * 26 * 36 * 36 * 36 * 36 - 1, "zzzzz");
226
227 checkEncodeException(4, -1000, "value out of range.");
228 checkEncodeException(4, 2436112, "value out of range.");
229 checkEncodeException(5, -10000, "value out of range.");
230 checkEncodeException(5, 87440032, "value out of range.");
231 checkEncodeException(3, 0, "unsupported width.");
232 checkEncodeException(6, 0, "unsupported width.");
233
234 int value = -9999;
235 while (value < 100000 + 2 * 26 * 36 * 36 * 36 * 36) {
236 checkInt(Hybrid36.decode(5, Hybrid36.encode(5, value)), value);
237 value += kernighan_and_ritchie_rand() % 10000;
238 }
239 }
240 }