1 /*******************************************************************************
2 
3     Copyright:
4         Copyright (c) 2009-2016 dunnhumby Germany GmbH. All rights reserved.
5 
6     License:
7         Boost Software License Version 1.0. See LICENSE_BOOST.txt for details.
8         Alternatively, this file may be distributed under the terms of the Tango
9         3-Clause BSD License (see LICENSE_BSD.txt for details).
10 
11 ********************************************************************************/
12 
13 module ocean.text.convert.Integer_tango_test;
14 
15 import ocean.meta.types.Qualifiers;
16 import ocean.text.convert.Integer_tango;
17 import ocean.core.Test;
18 
19 unittest
20 {
21     char[64] tmp;
22 
23     test(toInt("1") is 1);
24     test(toLong("1") is 1);
25     test(toInt("1", 10) is 1);
26     test(toLong("1", 10) is 1);
27     test(toUlong("1", 10) is 1);
28     test(toUlong("18446744073709551615") is ulong.max);
29 
30     test(atoi ("12345") is 12345);
31     test(itoa (tmp, 12345) == "12345");
32 
33     test(parse( "0"w ) ==  0 );
34     test(parse( "1"w ) ==  1 );
35     test(parse( "-1"w ) ==  -1 );
36     test(parse( "+1"w ) ==  1 );
37 
38     // numerical limits
39     test(parse( "-2147483648" ) == int.min );
40     test(parse(  "2147483647" ) == int.max );
41     test(parse(  "4294967295" ) == uint.max );
42 
43     test(parse( "-9223372036854775808" ) == long.min );
44     test(parse( "9223372036854775807" ) == long.max );
45     test(parse( "18446744073709551615" ) == ulong.max );
46 
47     // hex
48     test(parse( "a", 16) == 0x0A );
49     test(parse( "b", 16) == 0x0B );
50     test(parse( "c", 16) == 0x0C );
51     test(parse( "d", 16) == 0x0D );
52     test(parse( "e", 16) == 0x0E );
53     test(parse( "f", 16) == 0x0F );
54     test(parse( "A", 16) == 0x0A );
55     test(parse( "B", 16) == 0x0B );
56     test(parse( "C", 16) == 0x0C );
57     test(parse( "D", 16) == 0x0D );
58     test(parse( "E", 16) == 0x0E );
59     test(parse( "F", 16) == 0x0F );
60     test(parse( "FFFF", 16) == ushort.max );
61     test(parse( "ffffFFFF", 16) == uint.max );
62     test(parse( "ffffFFFFffffFFFF", 16u ) == ulong.max );
63     // oct
64     test(parse( "55", 8) == 5 + 8*5 );
65     test(parse( "100", 8) == 64 );
66     // bin
67     test(parse( "10000", 2) == 0x10 );
68     // trim
69     test(parse( "    \t20") == 20 );
70     test(parse( "    \t-20") == -20 );
71     test(parse( "-    \t 20") == -20 );
72     // recognise radix prefix
73     test(parse( "0xFFFF" ) == ushort.max );
74     test(parse( "0XffffFFFF" ) == uint.max );
75     test(parse( "0o55") == 5 + 8*5 );
76     test(parse( "0O55" ) == 5 + 8*5 );
77     test(parse( "0b10000") == 0x10 );
78     test(parse( "0B10000") == 0x10 );
79 
80     // prefix tests
81     auto str = "0x";
82     test(parse( str[0..1] ) ==  0 );
83     test(parse("0x10", 10) == 0);
84     test(parse("0b10", 10) == 0);
85     test(parse("0o10", 10) == 0);
86     test(parse("0b10") == 0b10);
87     test(parse("0o10") == 8);
88     test(parse("0b10", 2) == 0b10);
89     test(parse("0o10", 8) == 8);
90 
91     // revised tests
92     test(format(tmp, 10, "d") == "10");
93     test(format(tmp, -10, "d") == "-10");
94 
95     test(format(tmp, 10L, "u") == "10");
96     test(format(tmp, 10L, "U") == "10");
97     test(format(tmp, 10L, "g") == "10");
98     test(format(tmp, 10L, "G") == "10");
99     test(format(tmp, 10L, "o") == "12");
100     test(format(tmp, 10L, "O") == "12");
101     test(format(tmp, 10L, "b") == "1010");
102     test(format(tmp, 10L, "B") == "1010");
103     test(format(tmp, 10L, "x") == "a");
104     test(format(tmp, 10L, "X") == "A");
105 
106     test(format(tmp, 10L, "d+") == "+10");
107     test(format(tmp, 10L, "d ") == " 10");
108     test(format(tmp, 10L, "d#") == "10");
109     test(format(tmp, 10L, "x#") == "0xa");
110     test(format(tmp, 10L, "X#") == "0XA");
111     test(format(tmp, 10L, "b#") == "0b1010");
112     test(format(tmp, 10L, "o#") == "0o12");
113 
114     test(format(tmp, 10L, "d1") == "10");
115     test(format(tmp, 10L, "d8") == "00000010");
116     test(format(tmp, 10L, "x8") == "0000000a");
117     test(format(tmp, 10L, "X8") == "0000000A");
118     test(format(tmp, 10L, "b8") == "00001010");
119     test(format(tmp, 10L, "o8") == "00000012");
120 
121     test(format(tmp, 10L, "d1#") == "10");
122     test(format(tmp, 10L, "d6#") == "000010");
123     test(format(tmp, 10L, "x6#") == "0x00000a");
124     test(format(tmp, 10L, "X6#") == "0X00000A");
125 
126     char[8] tmp1;
127     test(format(tmp1, 10L, "b12#") == "0b001010");
128     test(format(tmp1, 10L, "o12#") == "0o000012");
129 
130     test(format(tmp, long.min, "d") == "-9223372036854775808", tmp);
131     test(format(tmp, long.max, "d") ==  "9223372036854775807", tmp);
132     test(format(tmp, cast(ubyte) -1, "b") ==  "11111111", tmp);
133     test(format(tmp, -1, "b") ==  "11111111111111111111111111111111", tmp);
134 }
135 
136 unittest
137 {
138     auto x = toInt("42");
139     test(x == 42);
140 }
141 
142 unittest
143 {
144     auto x = toLong("42");
145     test(x == 42);
146 }
147 
148 unittest
149 {
150     auto x = toUlong("42");
151     test(x == 42);
152 }
153 
154 unittest
155 {
156     auto x = toString(42);
157     test(x == "42");
158 }
159 
160 unittest
161 {
162     wchar[] x = toString16(42);
163     test(x == "42");
164 }
165 
166 unittest
167 {
168     dchar[] x = toString32(42);
169     test(x == "42");
170 }
171 
172 unittest
173 {
174     char[10] buff;
175     auto s = format(buff, 42, "x");
176     test(s == "2a", s);
177 
178     int x = 43;
179     s = format(buff, x);
180     test(s == "43");
181 }
182 
183 unittest
184 {
185     uint ate;
186     auto x = parse("-422", 0, &ate);
187     test(x == -422);
188     test(ate == 4);
189 }
190 
191 unittest
192 {
193     uint ate;
194     auto x = convert("422", 10, &ate);
195     test(x == 422);
196     test(ate == 3);
197 }
198 
199 unittest
200 {
201     bool sign;
202     uint radix;
203     auto x = trim("  0xFF ", sign, radix);
204     test(x == 4);
205     test(sign == false);
206     test(radix == 16);
207 }
208 
209 unittest
210 {
211     char[10] buff;
212     auto s = itoa(buff, 42);
213     test(s == "42");
214 }
215 
216 unittest
217 {
218     auto s = consume("422 abc");
219     test(s == "422");
220 }