KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jodd > format > FormatTest


1 // Copyright (c) 2003-2007, Jodd Team (jodd.sf.net). All Rights Reserved.
2

3 package jodd.format;
4
5 import junit.framework.TestCase;
6
7 public class FormatTest extends TestCase {
8
9     public void testIntLong() {
10         assertEquals("1", Printf.str("%d", 1));
11         assertEquals("17", Printf.str("%d", 17));
12         assertEquals("173", Printf.str("%d", 173));
13         assertEquals("173000000", Printf.str("%d", 173000000));
14
15         assertEquals("+173", Printf.str("%+i", 173));
16         assertEquals("+173", Printf.str("%+0i", 173));
17         assertEquals("173", Printf.str("%0i", 173));
18
19         assertEquals("+173", Printf.str("%+u", 173));
20         assertEquals("+173", Printf.str("%+0u", 173));
21         assertEquals("173", Printf.str("%0u", 173));
22
23         assertEquals("AD", Printf.str("%X", 173));
24         assertEquals("ad", Printf.str("%+x", 173));
25         assertEquals("7", Printf.str("%+0X", 7));
26         assertEquals("0X7", Printf.str("%#X", 7));
27         assertEquals("0x7", Printf.str("%#x", 7));
28         assertEquals("0XAD", Printf.str("%#X", 173));
29         assertEquals("255", Printf.str("%o", 173));
30
31         int i = -1;
32         assertEquals("-1", Printf.str("%,d", i));
33         assertEquals("-1", Printf.str("%,i", i));
34         assertEquals("4294967295", Printf.str("%u", i)); // 2^32 - 1
35
assertEquals("4,294,967,295", Printf.str("%,u", i));
36         assertEquals("ffffffff", Printf.str("%x", i));
37         assertEquals("ffff ffff", Printf.str("%,x", i));
38
39         i = 2147483647; // 2^31 - 1 (max int)
40
assertEquals(Integer.MAX_VALUE, i);
41         assertEquals("2147483647", Printf.str("%d", i));
42         assertEquals("2147483647", Printf.str("%i", i));
43         assertEquals("2147483647", Printf.str("%u", i));
44         assertEquals("2,147,483,647", Printf.str("%,d", i));
45         assertEquals("2,147,483,647", Printf.str("%,i", i));
46         assertEquals("2,147,483,647", Printf.str("%,u", i));
47         assertEquals("7fffffff", Printf.str("%x", i));
48         assertEquals("7fff ffff", Printf.str("%,x", i));
49
50         i++; // -2^31 (min int)
51
assertEquals(Integer.MIN_VALUE, i);
52         assertEquals("-2147483648", Printf.str("%d", i));
53         assertEquals("-2147483648", Printf.str("%i", i));
54         assertEquals("2147483648", Printf.str("%u", i));
55         assertEquals("-2,147,483,648", Printf.str("%,d", i));
56         assertEquals("-2,147,483,648", Printf.str("%,i", i));
57         assertEquals("2,147,483,648", Printf.str("%,u", i));
58         assertEquals("80000000", Printf.str("%x", i));
59         assertEquals("8000 0000", Printf.str("%,x", i));
60
61
62
63
64         long l = 2147483648L; // 2^31 (max int + 1)
65
assertEquals("2147483648", Printf.str("%d", l));
66         assertEquals("-2147483648", Printf.str("%i", l));
67         assertEquals("2147483648", Printf.str("%u", l));
68         assertEquals("2,147,483,648", Printf.str("%,d", l));
69         assertEquals("-2,147,483,648", Printf.str("%,i", l));
70         assertEquals("2,147,483,648", Printf.str("%,u", l));
71         assertEquals("80000000", Printf.str("%x", l));
72         assertEquals("8000 0000", Printf.str("%,x", l));
73
74         l = -2147483649L; // -2^31-1 (min int - 1)
75
assertEquals("-2147483649", Printf.str("%d", l));
76         assertEquals("2147483647", Printf.str("%i", l));
77         assertEquals("18446744071562067967", Printf.str("%u", l));
78         assertEquals("-2,147,483,649", Printf.str("%,d", l));
79         assertEquals("2,147,483,647", Printf.str("%,i", l));
80         assertEquals("18,446,744,071,562,067,967", Printf.str("%,u", l));
81         assertEquals("FFFFFFFF7FFFFFFF", Printf.str("%X", l));
82         assertEquals("FFFF FFFF 7FFF FFFF", Printf.str("%,X", l));
83
84         l = -1;
85         assertEquals("-1", Printf.str("%d", l));
86         assertEquals("-1", Printf.str("%i", l));
87         assertEquals("18446744073709551615", Printf.str("%u", l));
88         assertEquals("18,446,744,073,709,551,615", Printf.str("%,u", l));
89         assertEquals("FFFFFFFFFFFFFFFF", Printf.str("%X", l));
90         assertEquals("FFFF FFFF FFFF FFFF", Printf.str("%,X", l));
91
92         l = Long.MAX_VALUE;
93         assertEquals("9223372036854775807", Printf.str("%d", l));
94         assertEquals("9,223,372,036,854,775,807", Printf.str("%,d", l));
95         assertEquals("-1", Printf.str("%i", l));
96         assertEquals("9223372036854775807", Printf.str("%u", l));
97         assertEquals("9,223,372,036,854,775,807", Printf.str("%,u", l));
98         assertEquals("7FFFFFFFFFFFFFFF", Printf.str("%X", l));
99         assertEquals("7FFF FFFF FFFF FFFF", Printf.str("%,X", l));
100
101         l = Long.MIN_VALUE;
102         assertEquals("-9223372036854775808", Printf.str("%d", l));
103         assertEquals("-9,223,372,036,854,775,808", Printf.str("%,d", l));
104         assertEquals("0", Printf.str("%i", l));
105         assertEquals("9223372036854775808", Printf.str("%u", l));
106         assertEquals("9,223,372,036,854,775,808", Printf.str("%,u", l));
107         assertEquals("8000000000000000", Printf.str("%X", l));
108         assertEquals("8000 0000 0000 0000", Printf.str("%,X", l));
109     }
110
111     public void testChar() {
112         assertEquals("A", Printf.str("%c", 'A'));
113         assertEquals("--- A ---", Printf.str("--- %c ---", 'A'));
114     }
115
116
117     public void testFormatedInt() {
118         assertEquals("0001", Printf.str("%04d", 1));
119         assertEquals("+001", Printf.str("%+04d", 1));
120         assertEquals("-001", Printf.str("%+04d", -1));
121         assertEquals("+0001", Printf.str("%+~04d", 1));
122         assertEquals("0001", Printf.str("%~04d", 1));
123         assertEquals("-0001", Printf.str("%+~04d", -1));
124
125         assertEquals(" 1", Printf.str("%4d", 1));
126         assertEquals(" +1", Printf.str("%+4d", 1));
127         assertEquals(" -1", Printf.str("%+4d", -1));
128         assertEquals("+1 ", Printf.str("%-+4d", 1));
129         assertEquals("-1 ", Printf.str("%-+4d", -1));
130
131         assertEquals("1", Printf.str("%,d", 1));
132         assertEquals("12", Printf.str("%,d", 12));
133         assertEquals("123", Printf.str("%,d", 123));
134         assertEquals("1,234", Printf.str("%,d", 1234));
135         assertEquals("12,345", Printf.str("%,d", 12345));
136         assertEquals("123,456", Printf.str("%,d", 123456));
137     }
138
139     public void testStrings() {
140         assertEquals("A", Printf.str("%c", 'A'));
141         assertEquals("str", Printf.str("%s", "str"));
142         assertEquals("% 1", Printf.str("%% %i", 1));
143         assertEquals("% % % 1", Printf.str("%% %% %% %i", 1));
144         assertEquals("1 %%", Printf.str("%i %%", 1));
145
146         assertEquals("q w", Printf.str("%s %s", new String JavaDoc[]{"q", "w"}));
147         assertEquals(" q w", Printf.str(" %s %s", new String JavaDoc[]{"q", "w"}));
148         assertEquals("q w ", Printf.str("%s %s ", new String JavaDoc[]{"q", "w"}));
149         assertEquals(" q w 1 2 3 ", Printf.str(" %s %s %s %s %s ", new String JavaDoc[]{"q", "w", "1", "2", "3"}));
150
151         assertEquals("q%w", Printf.str("%s%%%s", new String JavaDoc[]{"q", "w"}));
152         assertEquals("q%%", Printf.str("%s%%", new String JavaDoc[]{"q"}));
153         assertEquals("q%% ", Printf.str("%s%% ", new String JavaDoc[]{"q"}));
154         assertEquals("q %%", Printf.str("%s %%", new String JavaDoc[]{"q"}));
155         assertEquals("q %% ", Printf.str("%s %% ", new String JavaDoc[]{"q"}));
156         assertEquals("%q", Printf.str("%%%s", new String JavaDoc[]{"q"}));
157         assertEquals("% q", Printf.str("%% %s", new String JavaDoc[]{"q"}));
158         assertEquals("% q ", Printf.str("%% %s ", new String JavaDoc[]{"q"}));
159         assertEquals(" % q ", Printf.str(" %% %s ", new String JavaDoc[]{"q"}));
160     }
161
162     public void testFloats() {
163         assertEquals("1.700000", Printf.str("%f", 1.7));
164         assertEquals("1.7", Printf.str("%1.1f", 1.7));
165         assertEquals("1.7", Printf.str("%2.1f", 1.7));
166         assertEquals("1.7", Printf.str("%3.1f", 1.7));
167         assertEquals(" 1.7", Printf.str("%4.1f", 1.7));
168         assertEquals("1.70", Printf.str("%4.2f", 1.7));
169         assertEquals("1.79", Printf.str("%4.2f", 1.79999999999));
170
171         assertEquals("17.3", Printf.str("%1.1f", 17.3));
172         assertEquals("17.3", Printf.str("%2.1f", 17.3));
173         assertEquals("17.3", Printf.str("%3.1f", 17.3));
174         assertEquals("17.3", Printf.str("%4.1f", 17.3));
175         assertEquals("17.30", Printf.str("%4.2f", 17.3));
176         assertEquals(" 17.3", Printf.str("%5.1f", 17.3));
177         assertEquals("17.30", Printf.str("%5.2f", 17.3));
178
179         assertEquals("1.100000E+002", Printf.str("%E", 1.1e2));
180         assertEquals("110", Printf.str("%G", 1.1e2));
181         assertEquals("1.100000E+013", Printf.str("%E", 1.1e13));
182         assertEquals("1.1E+013", Printf.str("%G", 1.1e13));
183         assertEquals("1.100000e+010", Printf.str("%e", 1.1e10));
184         assertEquals("1.1e+010", Printf.str("%g", 1.1e10));
185         assertEquals("1.1000e+010", Printf.str("%.4e", 1.1e10));
186         assertEquals("1.1e+010", Printf.str("%.4g", 1.1e10));
187         assertEquals("1.0010e+012", Printf.str("%.4e", 100.1e10));
188         assertEquals("1.001e+012", Printf.str("%.4g", 100.1e10));
189         assertEquals("1.0010e+012", Printf.str("%6.4e", 100.1e10));
190         assertEquals("1.001e+012", Printf.str("%g", 100.1e10));
191     }
192
193     public void testBoolean() {
194         assertEquals("true", Printf.str("%l", true));
195         assertEquals("false", Printf.str("%l", false));
196         assertEquals("TRUE", Printf.str("%L", true));
197         assertEquals("FALSE", Printf.str("%L", false));
198         assertEquals("true", Printf.str("%l", 123));
199         assertEquals("false", Printf.str("%l", 0));
200         assertEquals("TRUE", Printf.str("%L", 123));
201         assertEquals("FALSE", Printf.str("%L", 0));
202         assertEquals("true", Printf.str("%l", 123L));
203         assertEquals("false", Printf.str("%l", 0L));
204         assertEquals("TRUE", Printf.str("%L", 123L));
205         assertEquals("FALSE", Printf.str("%L", 0L));
206     }
207
208
209     public void testIntRanges() {
210         int i;
211
212         i = 0;
213         assertEquals(Integer.toString(i), Printf.str("%d", i));
214         assertEquals(Integer.toString(i), Printf.str("%i", i));
215         assertEquals(Integer.toString(i), Printf.str("%x", i));
216         assertEquals(Integer.toString(i), Printf.str("%u", i));
217         assertEquals(Integer.toString(i), Printf.str("%o", i));
218         assertEquals(Integer.toString(i), Printf.str("%b", i));
219
220         i = 1; long v = 1;
221         assertEquals(Integer.toString(i), Printf.str("%d", i));
222         assertEquals(Integer.toString(i), Printf.str("%i", i));
223         assertEquals(Long.toString(v, 16), Printf.str("%x", i));
224         assertEquals(Long.toString(v), Printf.str("%u", i));
225         assertEquals(Long.toString(v, 8), Printf.str("%o", i));
226         assertEquals(Long.toString(v, 2), Printf.str("%b", i));
227
228         i = Integer.MAX_VALUE; v = (1L << 31) - 1;
229         assertEquals(Integer.toString(i), Printf.str("%d", i));
230         assertEquals(Integer.toString(i), Printf.str("%i", i));
231         assertEquals(Long.toString(v, 16), Printf.str("%x", i));
232         assertEquals(Long.toString(v), Printf.str("%u", i));
233         assertEquals(Long.toString(v, 8), Printf.str("%o", i));
234         assertEquals(Long.toString(v, 2), Printf.str("%b", i));
235
236         i = Integer.MIN_VALUE; v = (1L << 31);
237         assertEquals(Integer.toString(i), Printf.str("%d", i));
238         assertEquals(Integer.toString(i), Printf.str("%i", i));
239         assertEquals(Long.toString(v, 16), Printf.str("%x", i));
240         assertEquals(Long.toString(v), Printf.str("%u", i));
241         assertEquals(Long.toString(v, 8), Printf.str("%o", i));
242         assertEquals(Long.toString(v, 2), Printf.str("%b", i));
243
244         i = -1; v = (1L << 32) - 1;
245         assertEquals(Integer.toString(i), Printf.str("%d", i));
246         assertEquals(Integer.toString(i), Printf.str("%i", i));
247         assertEquals(Long.toString(v, 16), Printf.str("%x", i));
248         assertEquals(Long.toString(v), Printf.str("%u", i));
249         assertEquals(Long.toString(v, 8), Printf.str("%o", i));
250         assertEquals(Long.toString(v, 2), Printf.str("%b", i));
251     }
252
253     public void testLongRanges() {
254         long l;
255
256         l = 0;
257         assertEquals(Long.toString(l), Printf.str("%d", l));
258         assertEquals(Long.toString(l), Printf.str("%i", l));
259         assertEquals(Long.toString(l), Printf.str("%x", l));
260         assertEquals(Long.toString(l), Printf.str("%u", l));
261         assertEquals(Long.toString(l), Printf.str("%o", l));
262         assertEquals(Long.toString(l), Printf.str("%b", l));
263
264         l = 1;
265         assertEquals(Long.toString(l), Printf.str("%d", l));
266         assertEquals(Long.toString(l), Printf.str("%i", l));
267         assertEquals(Long.toString(l, 16), Printf.str("%x", l));
268         assertEquals(Long.toString(l), Printf.str("%u", l));
269         assertEquals(Long.toString(l, 8), Printf.str("%o", l));
270         assertEquals(Long.toString(l, 2), Printf.str("%b", l));
271
272         l = Integer.MAX_VALUE; long v = (1L << 31) - 1;
273         assertEquals(Long.toString(l), Printf.str("%d", l));
274         assertEquals(Long.toString(l), Printf.str("%i", l));
275         assertEquals(Long.toString(v, 16), Printf.str("%x", l));
276         assertEquals(Long.toString(v), Printf.str("%u", l));
277         assertEquals(Long.toString(v, 8), Printf.str("%o", l));
278         assertEquals(Long.toString(v, 2), Printf.str("%b", l));
279
280         l = Integer.MIN_VALUE;
281         assertEquals(Long.toString(l), Printf.str("%d", l));
282         assertEquals(Long.toString(l), Printf.str("%i", l));
283         assertEquals(Long.toHexString(l), Printf.str("%x", l));
284         assertEquals("18446744071562067968", Printf.str("%u", l));
285         assertEquals(Long.toOctalString(l), Printf.str("%o", l));
286         assertEquals(Long.toBinaryString(l), Printf.str("%b", l));
287
288         l = -1;
289         assertEquals(Long.toString(l), Printf.str("%d", l));
290         assertEquals(Long.toString(l), Printf.str("%i", l));
291         assertEquals(Long.toHexString(l), Printf.str("%x", l));
292         assertEquals("18446744073709551615", Printf.str("%u", l));
293         assertEquals(Long.toOctalString(l), Printf.str("%o", l));
294         assertEquals(Long.toBinaryString(l), Printf.str("%b", l));
295
296
297         l = ((long) Integer.MAX_VALUE) + 1; v = (1L << 31);
298         assertEquals(Long.toString(l), Printf.str("%d", l));
299         assertEquals(Long.toString(Integer.MIN_VALUE), Printf.str("%i", l));
300         assertEquals(Long.toString(v, 16), Printf.str("%x", l));
301         assertEquals(Long.toString(v), Printf.str("%u", l));
302         assertEquals(Long.toString(v, 8), Printf.str("%o", l));
303         assertEquals(Long.toString(v, 2), Printf.str("%b", l));
304
305
306         l = ((long)Integer.MIN_VALUE) - 1;
307         assertEquals(Long.toString(l), Printf.str("%d", l));
308         assertEquals(Long.toString(Integer.MAX_VALUE), Printf.str("%i", l));
309         assertEquals(Long.toHexString(l), Printf.str("%x", l));
310         assertEquals("18446744071562067967", Printf.str("%u", l));
311         assertEquals(Long.toOctalString(l), Printf.str("%o", l));
312         assertEquals(Long.toBinaryString(l), Printf.str("%b", l));
313
314         l = Long.MAX_VALUE;
315         assertEquals(Long.toString(l), Printf.str("%d", l));
316         assertEquals(Long.toString(-1), Printf.str("%i", l));
317         assertEquals(Long.toHexString(l), Printf.str("%x", l));
318         assertEquals("9223372036854775807", Printf.str("%u", l));
319         assertEquals(Long.toOctalString(l), Printf.str("%o", l));
320         assertEquals(Long.toBinaryString(l), Printf.str("%b", l));
321
322
323         l = Long.MIN_VALUE;
324         assertEquals(Long.toString(l), Printf.str("%d", l));
325         assertEquals(Long.toString(0), Printf.str("%i", l));
326         assertEquals(Long.toHexString(l), Printf.str("%x", l));
327         assertEquals("9223372036854775808", Printf.str("%u", l));
328         assertEquals(Long.toOctalString(l), Printf.str("%o", l));
329         assertEquals(Long.toBinaryString(l), Printf.str("%b", l));
330     }
331
332
333
334     public void testBinary() {
335         assertEquals("1", Printf.str("%b", 1));
336         assertEquals("11", Printf.str("%b", 3));
337         assertEquals("1101", Printf.str("%b", 13));
338         assertEquals("11111111111111111111111111110011", Printf.str("%b", -13));
339         assertEquals("11111111 11111111 11111111 11110011", Printf.str("%,b", -13));
340         assertEquals("1111111111111111111111111111111111111111111111111111111111110011", Printf.str("%b", -13L));
341         assertEquals("11111111 11111111 11111111 11111111 11111111 11111111 11111111 11110011", Printf.str("%,b", -13L));
342     }
343
344     public void testSuccessive() {
345         String JavaDoc fmt = "...%i...%i...";
346         PrintfFormat pf = new PrintfFormat();
347         fmt = pf.reinit(fmt).form(1);
348         fmt = pf.reinit(fmt).form(2);
349         assertEquals("...1...2...", fmt);
350     }
351
352 }
353
Popular Tags