blob: 6dd3a841a12e486d309e9f9e40e94b013efb810f [file] [log] [blame]
utatane.tea@gmail.comfe5e95d2017-03-21 11:31:43 +00001function shouldBe(actual, expected)
2{
3 if (actual !== expected)
4 throw new Error(`bad value: expected:(${expected}),actual:(${actual})`);
5}
6
7function expected(num, radix)
8{
9 let characters = "0123456789abcdefghijklmnopqrstuvwxyz";
10 let result = "";
11 let negative = false;
12 if (num < 0) {
13 negative = true;
14 num = -num;
15 }
16
17 do {
18 const index = num % radix;
19 result = characters[index] + result;
20 num = (num - index) / radix;
21 } while (num);
22
23 if (negative)
24 return '-' + result;
25 return result;
26}
27
28{
29 function int32ToString(num, radix)
30 {
31 return num.toString(radix);
32 }
33 noInline(int32ToString);
34
35 for (var i = 0; i < 1e3; ++i) {
36 shouldBe(int32ToString(i, 16), expected(i, 16));
37 shouldBe(int32ToString(-i, 16), expected(-i, 16));
38 }
39
40 shouldBe(int32ToString(0xffffffffff, 16), expected(0xffffffffff, 16));
41 shouldBe(int32ToString(0.1, 16), `0.1999999999999a`);
42 shouldBe(int32ToString(-0.1, 16), `-0.1999999999999a`);
43 shouldBe(int32ToString(new Number(0xff), 16), `ff`);
44}
45
46{
47 function int52ToString(num, radix)
48 {
49 return fiatInt52(num).toString(radix);
50 }
51 noInline(int52ToString);
52
53 for (var i = 0; i < 1e3; ++i) {
54 shouldBe(int52ToString(0xffffffff + i, 16), expected(0xffffffff + i, 16));
55 shouldBe(int52ToString(-(0xffffffff + i), 16), expected(-(0xffffffff + i), 16));
56 }
57
58 shouldBe(int52ToString(0xff, 16), `ff`);
59 shouldBe(int52ToString(0.1, 16), `0.1999999999999a`);
60 shouldBe(int52ToString(-0.1, 16), `-0.1999999999999a`);
61 shouldBe(int52ToString(new Number(0xff), 16), `ff`);
62}
63
64{
65 function doubleToString(num, radix)
66 {
67 return num.toString(radix);
68 }
69 noInline(doubleToString);
70
71 for (var i = 0; i < 1e3; ++i) {
72 shouldBe(doubleToString(1.01, 16), `1.028f5c28f5c29`);
73 shouldBe(doubleToString(-1.01, 16), `-1.028f5c28f5c29`);
74 }
75
76 shouldBe(doubleToString(0xff, 16), `ff`);
77 shouldBe(doubleToString(0.1, 16), `0.1999999999999a`);
78 shouldBe(doubleToString(-0.1, 16), `-0.1999999999999a`);
79 shouldBe(doubleToString(new Number(0xff), 16), `ff`);
80}