KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jodd > format > TestFormat


1 package jodd.format;
2
3 import junit.framework.TestCase;
4
5 public class TestFormat extends TestCase {
6
7     public void testIntLong() {
8         assertEquals("1", Format.sprintf("%d", 1));
9         assertEquals("17", Format.sprintf("%d", 17));
10         assertEquals("173", Format.sprintf("%d", 173));
11         assertEquals("173000000", Format.sprintf("%d", 173000000));
12
13         assertEquals("+173", Format.sprintf("%+i", 173));
14         assertEquals("+173", Format.sprintf("%+0i", 173));
15         assertEquals("173", Format.sprintf("%0i", 173));
16
17         assertEquals("+173", Format.sprintf("%+u", 173));
18         assertEquals("+173", Format.sprintf("%+0u", 173));
19         assertEquals("173", Format.sprintf("%0u", 173));
20
21         assertEquals("AD", Format.sprintf("%X", 173));
22         assertEquals("ad", Format.sprintf("%+x", 173));
23         assertEquals("7", Format.sprintf("%+0X", 7));
24         assertEquals("0X7", Format.sprintf("%#X", 7));
25         assertEquals("0x7", Format.sprintf("%#x", 7));
26         assertEquals("0XAD", Format.sprintf("%#X", 173));
27         assertEquals("255", Format.sprintf("%o", 173));
28
29         int i = -1;
30         assertEquals("-1", Format.sprintf("%d", i));
31         assertEquals("-1", Format.sprintf("%i", i));
32         assertEquals("4294967295", Format.sprintf("%u", i)); // 2^32 - 1
33
assertEquals("ffffffff", Format.sprintf("%x", i));
34
35         i = 2147483647; // 2^16 - 1
36
assertEquals("2147483647", Format.sprintf("%d", i));
37         assertEquals("2147483647", Format.sprintf("%i", i));
38         assertEquals("2147483647", Format.sprintf("%u", i));
39         assertEquals("7fffffff", Format.sprintf("%x", i));
40
41         i++;
42         assertEquals("-2147483648", Format.sprintf("%d", i));
43         assertEquals("-2147483648", Format.sprintf("%i", i));
44         assertEquals("2147483648", Format.sprintf("%u", i));
45         assertEquals("80000000", Format.sprintf("%x", i));
46
47         long l = 2147483648L; // 2 ^ 16
48
assertEquals("2147483648", Format.sprintf("%d", l));
49         assertEquals("-2147483648", Format.sprintf("%i", l));
50         assertEquals("2147483648", Format.sprintf("%u", l));
51         assertEquals("80000000", Format.sprintf("%x", l));
52
53         l = -2147483649L;
54         assertEquals("-2147483649", Format.sprintf("%d", l));
55         assertEquals("2147483647", Format.sprintf("%i", l));
56         assertEquals("2147483647", Format.sprintf("%u", l));
57         assertEquals("FFFFFFFF7FFFFFFF", Format.sprintf("%X", l));
58
59         l = -1;
60         assertEquals("-1", Format.sprintf("%d", l));
61         assertEquals("-1", Format.sprintf("%i", l));
62         assertEquals("4294967295", Format.sprintf("%u", l));
63         assertEquals("ffffffffffffffff", Format.sprintf("%x", l));
64     }
65
66
67     public void testFormatedInt() {
68         assertEquals("0001", Format.sprintf("%04d", 1));
69         assertEquals("+001", Format.sprintf("%+04d", 1));
70         assertEquals("-001", Format.sprintf("%+04d", -1));
71         assertEquals("+0001", Format.sprintf("%+~04d", 1));
72         assertEquals("0001", Format.sprintf("%~04d", 1));
73         assertEquals("-0001", Format.sprintf("%+~04d", -1));
74
75         assertEquals(" 1", Format.sprintf("%4d", 1));
76         assertEquals(" +1", Format.sprintf("%+4d", 1));
77         assertEquals(" -1", Format.sprintf("%+4d", -1));
78         assertEquals("+1 ", Format.sprintf("%-+4d", 1));
79         assertEquals("-1 ", Format.sprintf("%-+4d", -1));
80     }
81
82     public void testStrings() {
83         assertEquals("A", Format.sprintf("%c", 'A'));
84         assertEquals("str", Format.sprintf("%s", "str"));
85         assertEquals("% 1", Format.sprintf("%% %i", 1));
86         assertEquals("% % % 1", Format.sprintf("%% %% %% %i", 1));
87         assertEquals("1 %%", Format.sprintf("%i %%", 1));
88
89         assertEquals("q w", Format.sprintf("%s %s", new String[]{"q", "w"}));
90         assertEquals(" q w", Format.sprintf(" %s %s", new String[]{"q", "w"}));
91         assertEquals("q w ", Format.sprintf("%s %s ", new String[]{"q", "w"}));
92         assertEquals(" q w 1 2 3 ", Format.sprintf(" %s %s %s %s %s ", new String[]{"q", "w", "1", "2", "3"}));
93
94         assertEquals("q%w", Format.sprintf("%s%%%s", new String[]{"q", "w"}));
95         assertEquals("q%", Format.sprintf("%s%%", new String[]{"q"}));
96         assertEquals("q% ", Format.sprintf("%s%% ", new String[]{"q"}));
97         assertEquals("q %", Format.sprintf("%s %%", new String[]{"q"}));
98         assertEquals("q % ", Format.sprintf("%s %% ", new String[]{"q"}));
99         assertEquals("%q", Format.sprintf("%%%s", new String[]{"q"}));
100         assertEquals("% q", Format.sprintf("%% %s", new String[]{"q"}));
101         assertEquals("% q ", Format.sprintf("%% %s ", new String[]{"q"}));
102         assertEquals(" % q ", Format.sprintf(" %% %s ", new String[]{"q"}));
103     }
104
105     public void testReals() {
106         assertEquals("1.700000", Format.sprintf("%f", 1.7));
107         assertEquals("1.7", Format.sprintf("%1.1f", 1.7));
108         assertEquals("1.7", Format.sprintf("%2.1f", 1.7));
109         assertEquals("1.7", Format.sprintf("%3.1f", 1.7));
110         assertEquals(" 1.7", Format.sprintf("%4.1f", 1.7));
111         assertEquals("1.70", Format.sprintf("%4.2f", 1.7));
112         assertEquals("1.79", Format.sprintf("%4.2f", 1.79999999999));
113
114         assertEquals("17.3", Format.sprintf("%1.1f", 17.3));
115         assertEquals("17.3", Format.sprintf("%2.1f", 17.3));
116         assertEquals("17.3", Format.sprintf("%3.1f", 17.3));
117         assertEquals("17.3", Format.sprintf("%4.1f", 17.3));
118         assertEquals("17.30", Format.sprintf("%4.2f", 17.3));
119         assertEquals(" 17.3", Format.sprintf("%5.1f", 17.3));
120         assertEquals("17.30", Format.sprintf("%5.2f", 17.3));
121     }
122 }
123
Popular Tags