1 | '--------------------------------------------------------------------
|
---|
2 | ' Test case of SPrintf Function and etc...
|
---|
3 | '--------------------------------------------------------------------
|
---|
4 |
|
---|
5 | Imports ActiveBasic.Strings
|
---|
6 | Imports ActiveBasic.Strings.Detail
|
---|
7 |
|
---|
8 | Namespace SPrintFTest
|
---|
9 |
|
---|
10 | Sub FormatTest()
|
---|
11 | Dim s = Nothing As String, e As Long, sign As Boolean
|
---|
12 |
|
---|
13 | s = FloatToChars(1., e, sign)
|
---|
14 | UnitTest("FloatToChars(1)", s = "10000000000000000" And e = 0 And sign = False)
|
---|
15 | s = FloatToChars(-93.75e-3, e, sign)
|
---|
16 | UnitTest("FloatToChars(-93.75e-3)", s = "93750000000000000" And e = 1 - 3 And sign = True)
|
---|
17 | s = FloatToChars(.0, e, sign)
|
---|
18 | UnitTest("FloatToChars(0)", s = "00000000000000000" And e = 0 And sign = False)
|
---|
19 |
|
---|
20 | s = FormatFloatE(9.876543e021, 6, 0, None)
|
---|
21 | UnitTest("FormatFloatE(9876543e+21)", s, "9.876543e+21")
|
---|
22 | s = FormatFloatE(7.81250000E-03, 8, 0, Cap)
|
---|
23 | UnitTest("FormatFloatE(7.81250000E-03)", s, "7.81250000E-03")
|
---|
24 |
|
---|
25 | s = FormatFloatE(1.2345e67, 4, 15, 0)
|
---|
26 | UnitTest("FormatFloatE(1.2345e+67, field width = 15)", s, " 1.2345e+67")
|
---|
27 | s = FormatFloatE(1.2345e67, 4, 15, Zero)
|
---|
28 | UnitTest("FormatFloatE(1.2345e+67, field width = 15, Zero)", s, "000001.2345e+67")
|
---|
29 | s = FormatFloatE(-1.2345e67, 4, 15, 0)
|
---|
30 | UnitTest("FormatFloatE(-1.2345e+67, field width = 15)", s, " -1.2345e+67")
|
---|
31 | s = FormatFloatE(-1.2345e67, 4, 15, Zero)
|
---|
32 | UnitTest("FormatFloatE(-1.2345e+67, field width = 15, Zero)", s, "-00001.2345e+67")
|
---|
33 | s = FormatFloatE(1.2345e67, 4, 15, Sign)
|
---|
34 | UnitTest("FormatFloatE(1.2345e+67, field width = 15, Sign)", s, " +1.2345e+67")
|
---|
35 | s = FormatFloatE(1.2345e67, 4, 15, Zero Or Sign)
|
---|
36 | UnitTest("FormatFloatE(1.2345e+67, field width = 15, Zero Or Sign)", s, "+00001.2345e+67")
|
---|
37 | s = FormatFloatE(1.2345e67, 4, 15, Zero Or Blank)
|
---|
38 | UnitTest("FormatFloatE(1.2345e+67, field width = 15, Zero Or Blank)", s, " 00001.2345e+67")
|
---|
39 | s = FormatFloatE(-1.2345e67, 4, 15, Zero Or Sign)
|
---|
40 | UnitTest("FormatFloatE(-1.2345e+67, field width = 15, Zero Or Sign)", s, "-00001.2345e+67")
|
---|
41 | s = FormatFloatE(-1.2345e67, 4, 15, Zero Or Blank)
|
---|
42 | UnitTest("FormatFloatE(-1.2345e+67, field width = 15, Zero Or Blank)", s, "-00001.2345e+67")
|
---|
43 |
|
---|
44 | s = FormatFloatE(1.2345e+67, 4, 0, Cap)
|
---|
45 | UnitTest("FormatFloatE(1.2345E+67, Cap)", s, "1.2345E+67")
|
---|
46 | s = FormatFloatE(1.2345e67, 3, 0, None)
|
---|
47 | UnitTest("FormatFloatE(1.2345e+67, precision = 3)", s, "1.234e+67")
|
---|
48 |
|
---|
49 | s = FormatIntegerU(777, 0, 0, None)
|
---|
50 | UnitTest("FormatIntegerU(777)", s, "777")
|
---|
51 | s = FormatIntegerU(513, 0, 5, None)
|
---|
52 | UnitTest("FormatIntegerU(513, field width = 5)", s, " 513")
|
---|
53 |
|
---|
54 | s = FormatIntegerD(-3, 2, 0, Sign)
|
---|
55 | UnitTest("FormatIntegerD(-3, precision = 2)", s, "-03")
|
---|
56 | s = FormatIntegerD(3, 0, 5, Sign)
|
---|
57 | UnitTest("FormatIntegerD(+3, field width = 5)", s, " +3")
|
---|
58 |
|
---|
59 | s = FormatIntegerO(&o1234567, DWORD_MAX, 0, None)
|
---|
60 | UnitTest("FormatIntegerO(&o1234567)", s, "1234567")
|
---|
61 | s = FormatIntegerO(&o1234567, DWORD_MAX, 0, Alt)
|
---|
62 | UnitTest("FormatIntegerO(&o1234567, Alt)", s, "01234567")
|
---|
63 | s = FormatIntegerO(0, DWORD_MAX, 0, Alt)
|
---|
64 | UnitTest("FormatIntegerO(0, Alt)", s, "0")
|
---|
65 |
|
---|
66 | s = FormatIntegerX(&hffff, DWORD_MAX, 0, None)
|
---|
67 | UnitTest("FormatIntegerX(&hffff)", s, "ffff")
|
---|
68 | s = FormatIntegerX(&hffff, DWORD_MAX, 0, Cap)
|
---|
69 | UnitTest("FormatIntegerX(&hffff, Cap)", s, "FFFF")
|
---|
70 | s = FormatIntegerX(&h12345678, DWORD_MAX, 0, Alt)
|
---|
71 | UnitTest("FormatIntegerX(&h12345678, Alt)", s, "0x12345678")
|
---|
72 | s = FormatIntegerX(1, 2, 0, Alt Or Cap)
|
---|
73 | UnitTest("FormatIntegerX(1, precision = 2, Alt, Cap)", s, "0X01")
|
---|
74 | s = FormatIntegerX(0, 4, 0, Alt)
|
---|
75 | UnitTest("FormatIntegerX(0, precision = 4, Alt)", s, "0000")
|
---|
76 |
|
---|
77 | s = FormatFloatF(1, 2, 0, None)
|
---|
78 | UnitTest("FormatFloatF(1.00, precision = 2)", s, "1.00")
|
---|
79 | s = FormatFloatF(123.456, 3, 0, None)
|
---|
80 | UnitTest("FormatFloatF(123.456, precision = 3)", s, "123.456")
|
---|
81 | s = FormatFloatF(2., 0, 0, Alt)
|
---|
82 | UnitTest("FormatFloatF(2., precision = 0, Alt", s, "2.")
|
---|
83 | s = FormatFloatF(-1.234e-1, 3, 0, None)
|
---|
84 | UnitTest("FormatFloatF(-1.234e-1, precision = 3) = -0.123", s, "-0.123")
|
---|
85 | s = FormatFloatF(-1.234e-2, 3, 0, None)
|
---|
86 | UnitTest("FormatFloatF(-1.234e-2, precision = 3) = -0.012", s, "-0.012")
|
---|
87 | s = FormatFloatF(-1.234e-3, 3, 0, None)
|
---|
88 | UnitTest("FormatFloatF(-1.234e-3, precision = 3) = -0.001", s, "-0.001")
|
---|
89 | s = FormatFloatF(-1.234e-4, 3, 0, None)
|
---|
90 | UnitTest("FormatFloatF(-1.234e-4, precision = 3) = -0.000", s, "-0.000")
|
---|
91 | '現状では末尾の桁の誤差を制御しきれないので、文字列全体の一致比較を避けている。
|
---|
92 | s = FormatFloatF(12345678901234567e3, 2, 0, None)
|
---|
93 | UnitTest("FormatFloatF(1.00, precision = 2)", Right$(s, 6) = "000.00")
|
---|
94 |
|
---|
95 | s = FormatIntegerLO(&hFFFFFFFFFFFFFFFF, 0, 0, Alt)
|
---|
96 | UnitTest("FormatIntegerLO(&hFFFFFFFFFFFFFFFF, Alt) = 01777777777777777777777", s, "01777777777777777777777")
|
---|
97 | s = FormatIntegerLX(&h123456789abcdef0, 0, 0, None)
|
---|
98 | UnitTest("FormatIntegerLX(&h123456789abcdef0)", s, "123456789abcdef0")
|
---|
99 | s = FormatIntegerLU(8589934590 As QWord, DWORD_MAX, 0, None)
|
---|
100 | UnitTest("FormatIntegerLU(8589934590)", s, "8589934590")
|
---|
101 | s = FormatIntegerLD(-8589934590, DWORD_MAX, 0, None)
|
---|
102 | UnitTest("FormatIntegerLD(-8589934590)", s, "-8589934590")
|
---|
103 |
|
---|
104 | s = FormatFloatG(1.25, DWORD_MAX, 0, None)
|
---|
105 | UnitTest("FormatFloatG(1.25)", s, "1.25")
|
---|
106 | s = FormatFloatG(0.1, 2, 0, None)
|
---|
107 | UnitTest("FormatFloatG(0.1, precision = 2)", s, "0.1")
|
---|
108 | s = FormatFloatG(1.2e+60, DWORD_MAX, 0, None)
|
---|
109 | UnitTest("FormatFloatG(1.2e+60)", s, "1.2e+60")
|
---|
110 | s = FormatFloatG(1.2e-60, DWORD_MAX, 0, None)
|
---|
111 | UnitTest("FormatFloatG(1.2e-60)", s, "1.2e-60")
|
---|
112 | s = FormatFloatG(1.0, 2, 0, Alt)
|
---|
113 | UnitTest("FormatFloatG(1.0, precision = 2, Alt)", s, "1.0")
|
---|
114 | s = FormatFloatG(0.01, 2, 0, Alt)
|
---|
115 | UnitTest("FormatFloatG(0.01, precision = 2, Alt)", s, "0.010")
|
---|
116 | s = FormatFloatG(0.000123456, 4, 0, None)
|
---|
117 | UnitTest("FormatFloatG(0.000123456, precision = 4) = 0.0001234", s, "0.0001234")
|
---|
118 | s = FormatFloatG(0.0000123456, 4, 0, None)
|
---|
119 | UnitTest("FormatFloatG(0.0000123456, precision = 4) = 1.234e-05", s, "1.234e-05")
|
---|
120 |
|
---|
121 | s = FormatFloatA(1, DWORD_MAX, 0, None)
|
---|
122 | UnitTest("FormatFloatA(1)", s, "0x1p+0")
|
---|
123 | s = FormatFloatA(2.2250738585072014e-308, DWORD_MAX, 0, None)
|
---|
124 | UnitTest("FormatFloatA(2.2250738585072014e-308) = 0x1p-1022", s, "0x1p-1022")
|
---|
125 | s = FormatFloatA(1.11253692925360070e-308, DWORD_MAX, 0, None)
|
---|
126 | UnitTest("FormatFloatA(1.11253692925360070e-308) = 0x0.fp-1022", s, "0x0.8p-1022")
|
---|
127 | s = FormatFloatA(3.1415926535897932384626433832795, DWORD_MAX, 0, None)
|
---|
128 | UnitTest("FormatFloatA(π) = 0x1.921fb54442d18p+1", s, "0x1.921fb54442d18p+1")
|
---|
129 | s = FormatFloatA(0, DWORD_MAX, 0, None)
|
---|
130 | UnitTest("FormatFloatA(0)", s, "0x0p+0")
|
---|
131 |
|
---|
132 | s = FormatString("123456", DWORD_MAX, 0, None)
|
---|
133 | UnitTest(Ex"FormatString(\q123456\q)", s, "123456")
|
---|
134 | s = FormatString("123456", 3, 0, None)
|
---|
135 | UnitTest(Ex"FormatString(\q123456\q, precision = 3) = \q123\q", s, "123")
|
---|
136 | s = FormatString("123456", 3, 6, None)
|
---|
137 | UnitTest(Ex"FormatString(\q123456\q, precision = 3, field width = 6) = \q 123\q",
|
---|
138 | s, " 123")
|
---|
139 |
|
---|
140 | s = FormatCharacter(Asc("Y"), DWORD_MAX, 0, None)
|
---|
141 | UnitTest(Ex"FormatCharacter(\qY\q)", s, "Y")
|
---|
142 | s = FormatCharacter(Asc("I"), DWORD_MAX, 3, LeftSide)
|
---|
143 | UnitTest(Ex"FormatCharacter(\qI\q, Left)", s, "I ")
|
---|
144 | End Sub
|
---|
145 |
|
---|
146 | Sub TestMain()
|
---|
147 | FormatTest()
|
---|
148 |
|
---|
149 | Imports System
|
---|
150 |
|
---|
151 | UnitTest(Ex"SPrintf(\q123\q)", ActiveBasic.Strings.Detail.SPrintf("'123'", 0, 0), "'123'")
|
---|
152 | UnitTest(Ex"%s, \qhello\q", SPrintf("'%s'", "hello"), "'hello'")
|
---|
153 | UnitTest(Ex"%7s, \qhello\q", SPrintf("'%7s'", "hello"), "' hello'")
|
---|
154 | UnitTest(Ex"%*s, -7, \qhello\q", SPrintf("'%*s'", New Int32(-7), "hello"), "'hello '")
|
---|
155 | UnitTest(Ex"%.3s, \qhello\q", SPrintf("'%.3s'", "hello"), "'hel'")
|
---|
156 | UnitTest(Ex"%.*s, 4, \qhello\q", SPrintf("'%.*s'", New Int32(4), "hello"), "'hell'")
|
---|
157 | UnitTest(Ex"%-5.3s, \qhello\q", SPrintf("'%-5.3s'", "hello"), "'hel '")
|
---|
158 | UnitTest(Ex"%*.*s, 2, 1, \qhello\q", SPrintf("'%*.*s'", New Int32(2), New Int32(1), "hello"), "' h'")
|
---|
159 | #ifdef UNICODE
|
---|
160 | UnitTest(Ex"%c, '0'", SPrintf("'%c'", New System.UInt16(&h30)), "'0'")
|
---|
161 | #else
|
---|
162 | UnitTest(Ex"%c, '0'", SPrintf("'%c'", New System.SByte(&h30)), "'0'")
|
---|
163 | #endif
|
---|
164 | Dim i = Nothing As Object, mi = Nothing As Object
|
---|
165 | i = New Int32(10)
|
---|
166 | mi = New Int32(-10)
|
---|
167 | UnitTest(Ex"%d, 10", SPrintf("'%d'", i), "'10'")
|
---|
168 | UnitTest(Ex"%+d, 10", SPrintf("'%+d'", i), "'+10'")
|
---|
169 | UnitTest(Ex"% d, 10", SPrintf("'% d'", i), "' 10'")
|
---|
170 | UnitTest(Ex"%4d, 10", SPrintf("'%4d'", i), "' 10'")
|
---|
171 | UnitTest(Ex"%4d, -10", SPrintf("'%4d'", mi), "' -10'")
|
---|
172 | UnitTest(Ex"%04d, 10", SPrintf("'%04d'", i), "'0010'")
|
---|
173 | UnitTest(Ex"%04d, -10", SPrintf("'%04d'", mi), "'-010'")
|
---|
174 | UnitTest(Ex"%5.3d, 10", SPrintf("'%5.3d'", i), "' 010'")
|
---|
175 |
|
---|
176 | UnitTest(Ex"%i, 10", SPrintf("'%i'", i), "'10'")
|
---|
177 | UnitTest(Ex"%ld, -4294967296", SPrintf("'%ld'", New System.Int64(-4294967296)), "'-4294967296'")
|
---|
178 | UnitTest(Ex"%li, -4294967296", SPrintf("'%li'", New System.Int64(-4294967296)), "'-4294967296'")
|
---|
179 | UnitTest(Ex"%u, 4294967295", SPrintf("'%u'", New UInt32(4294967295)), "'4294967295'")
|
---|
180 | UnitTest(Ex"%lu, 4294967296", SPrintf("'%lu'", New UInt64(4294967296)), "'4294967296'")
|
---|
181 | UnitTest(Ex"%x, ffffffff", SPrintf("'%x'", New UInt32(4294967295)), "'ffffffff'")
|
---|
182 | UnitTest(Ex"%#x, 0xffffffff", SPrintf("'%#x'", New UInt32(4294967295)), "'0xffffffff'")
|
---|
183 | UnitTest(Ex"%&x, &hffffffff", SPrintf("'%&x'", New UInt32(4294967295)), "'&hffffffff'")
|
---|
184 | UnitTest(Ex"%lx, 12345789abcdef", SPrintf("'%lx'", New UInt64(&h12345789abcdef)), "'12345789abcdef'")
|
---|
185 | UnitTest(Ex"%lX, 12345789ABCDEF", SPrintf("'%lX'", New UInt64(&h12345789abcdef)), "'12345789ABCDEF'")
|
---|
186 | Dim o777 = Nothing As Object
|
---|
187 | o777 = New UInt32(&o777)
|
---|
188 | UnitTest(Ex"%o, 777", SPrintf("'%o'", o777), "'777'")
|
---|
189 | UnitTest(Ex"%#o, 0777", SPrintf("'%#o'", o777), "'0777'")
|
---|
190 | UnitTest(Ex"%&o, &o777", SPrintf("'%&o'", o777), "'&o777'")
|
---|
191 | UnitTest(Ex"%&O, &O777", SPrintf("'%&O'", o777), "'&O777'")
|
---|
192 | UnitTest(Ex"%lo, 100000000000", SPrintf("'%lo'", New UInt64(&o100000000000)), "'100000000000'")
|
---|
193 |
|
---|
194 | Dim d = Nothing As Object
|
---|
195 | d = New System.Double(123.25)
|
---|
196 | UnitTest(Ex"%g, 123.25", SPrintf("'%g'", d), "'123.25'")
|
---|
197 | UnitTest(Ex"%f, 123.25", SPrintf("'%f'", d), "'123.250000'")
|
---|
198 | UnitTest(Ex"%e, 123.25", SPrintf("'%e'", d), "'1.232500e+02'")
|
---|
199 | UnitTest(Ex"%a, 123.25", SPrintf("'%a'", d), "'0x1.edp+6'")
|
---|
200 |
|
---|
201 | UnitTest(Ex"%%", SPrintf("'%%'", d), "'%'")
|
---|
202 | End Sub
|
---|
203 |
|
---|
204 | End Namespace 'SPrintFTest
|
---|
205 |
|
---|
206 | SPrintFTest.TestMain()
|
---|