-
Notifications
You must be signed in to change notification settings - Fork 12
/
number.test
259 lines (254 loc) · 7.23 KB
/
number.test
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
if {"::tcltest" ni [namespace children]} {
package require tcltest
namespace import ::tcltest::*
}
package require rl_json
test number-1.1.1 {Create a json number: 1 (was native number} -body { #<<<
set n 1
expr {$n+0}
list [json number $n] $n
} -cleanup {
unset -nocomplain n
} -result [list 1 1]
#>>>
test number-1.1.2 {Create a json number: 123 (was string} -body { #<<<
set n [string trim " 1"]
list [json number $n] $n
} -cleanup {
unset -nocomplain n
} -result [list 1 1]
#>>>
test number-1.2 {Create a json number: 0} -body { #<<<
json number 0
} -result 0
#>>>
test number-1.3 {Create a json number: real} -body { #<<<
json number 42.1
} -result 42.1
#>>>
test number-1.4.1 {Create a json number: scientific notation, native double} -body { #<<<
set n 1e6
expr {$n+0}
list [json number $n] $n
} -result {1e6 1e6}
#>>>
test number-1.4.2 {Create a json number: scientific notation, string} -body { #<<<
set n [string trim " 1e6"]
list [json number $n] $n
} -result {1000000.0 1e6}
#>>>
test number-1.5 {Create a max 32 bit signed int} -body { #<<<
json number [expr {2**31-1}]
} -result 2147483647
#>>>
test number-1.6 {Create a min 32 bit signed int} -body { #<<<
json number [expr {-1*(2**31)}]
} -result -2147483648
#>>>
test number-1.7 {Create a max 32 bit unsigned int} -body { #<<<
json number [expr {2**32-1}]
} -result 4294967295
#>>>
test number-1.8 {Create a max 64 bit signed int} -body { #<<<
json number [expr {2**63-1}]
} -result 9223372036854775807
#>>>
test number-1.9 {Create a min 64 bit signed int} -body { #<<<
json number [expr {-1*(2**63)}]
} -result -9223372036854775808
#>>>
test number-1.10 {Create a max 64 bit unsigned int} -body { #<<<
json number [expr {2**64-1}]
} -result 18446744073709551615
#>>>
test number-1.11 {Create an positive bignum} -body { #<<<
json number [expr {2**1000-1}]
} -result 10715086071862673209484250490600018105614048117055336074437503883703510511249361224931983788156958581275946729175531468251871452856923140435984577574698574803934567774824230985421074605062371141877954182153046474983581941267398767559165543946077062914571196477686542167660429831652624386837205668069375
#>>>
test number-1.12 {Create an negative bignum} -body { #<<<
json number [expr {-1*(2**1000-1)}]
} -result -10715086071862673209484250490600018105614048117055336074437503883703510511249361224931983788156958581275946729175531468251871452856923140435984577574698574803934567774824230985421074605062371141877954182153046474983581941267398767559165543946077062914571196477686542167660429831652624386837205668069375
#>>>
test number-1.13 {string} -body { #<<<
json number [string cat 4 2]
} -result 42
#>>>
test number-1.14.1 {positive octal} -body { #<<<
set n 077
expr {$n+0} ;# Convert to number type
list [json number $n] $n
} -cleanup {
unset -nocomplain n
} -result {63 077}
#>>>
test number-1.14.2 {negative octal} -body { #<<<
set n -077
expr {$n+0} ;# Convert to number type
list [json number $n] $n
} -cleanup {
unset -nocomplain n
} -result {-63 -077}
#>>>
test number-1.15.1 {positive hex} -body { #<<<
set n 0xA0
expr {$n+0} ;# Convert to number type
list [json number $n] $n
} -cleanup {
unset -nocomplain n
} -result {160 0xA0}
#>>>
test number-1.15.2 {negative hex} -body { #<<<
set n -0xA0
expr {$n+0} ;# Convert to number type
list [json number $n] $n
} -cleanup {
unset -nocomplain n
} -result {-160 -0xA0}
#>>>
test number-1.16.1 {positive double, valid leading 0} -body { #<<<
set n 0.5e3
expr {$n+0} ;# Convert to number type
list [json number $n] $n
} -cleanup {
unset -nocomplain n
} -result {0.5e3 0.5e3}
#>>>
test number-1.16.2 {negative double, valid leading 0} -body { #<<<
set n -0.5e3
expr {$n+0} ;# Convert to number type
list [json number $n] $n
} -cleanup {
unset -nocomplain n
} -result {-0.5e3 -0.5e3}
#>>>
test number-1.17.1 {trailing whitespace: ' '} -body { #<<<
set n {42 }
expr {$n+0} ;# Convert to number type
list [json number $n] $n
} -cleanup {
unset -nocomplain n
} -result [list 42 {42 }]
#>>>
test number-1.17.2 {trailing whitespace: '\n'} -body { #<<<
set n 42\n
expr {$n+0} ;# Convert to number type
list [json number $n] $n
} -cleanup {
unset -nocomplain n
} -result [list 42 42\n]
#>>>
test number-1.17.3 {trailing whitespace: '\t'} -body { #<<<
set n 42\t
expr {$n+0} ;# Convert to number type
list [json number $n] $n
} -cleanup {
unset -nocomplain n
} -result [list 42 42\t]
#>>>
test number-1.17.4 {trailing whitespace: '\v'} -body { #<<<
set n 42\v
expr {$n+0} ;# Convert to number type
list [json number $n] $n
} -cleanup {
unset -nocomplain n
} -result [list 42 42\v]
#>>>
test number-1.17.5 {trailing whitespace: '\r'} -body { #<<<
set n 42\r
expr {$n+0} ;# Convert to number type
list [json number $n] $n
} -cleanup {
unset -nocomplain n
} -result [list 42 42\r]
#>>>
test number-1.17.6 {trailing whitespace: '\f'} -body { #<<<
set n 42\f
expr {$n+0} ;# Convert to number type
list [json number $n] $n
} -cleanup {
unset -nocomplain n
} -result [list 42 42\f]
#>>>
test number-1.18.1 {leading whitespace: ' '} -body { #<<<
set n { 42}
expr {$n+0} ;# Convert to number type
list [json number $n] $n
} -cleanup {
unset -nocomplain n
} -result [list 42 { 42}]
#>>>
test number-1.18.2 {leading whitespace: '\n'} -body { #<<<
set n \n42
expr {$n+0} ;# Convert to number type
list [json number $n] $n
} -cleanup {
unset -nocomplain n
} -result [list 42 \n42]
#>>>
test number-1.18.3 {leading whitespace: '\t'} -body { #<<<
set n \t42
expr {$n+0} ;# Convert to number type
list [json number $n] $n
} -cleanup {
unset -nocomplain n
} -result [list 42 \t42]
#>>>
test number-1.18.4 {leading whitespace: '\v'} -body { #<<<
set n \v42
expr {$n+0} ;# Convert to number type
list [json number $n] $n
} -cleanup {
unset -nocomplain n
} -result [list 42 \v42]
#>>>
test number-1.18.5 {leading whitespace: '\r'} -body { #<<<
set n \r42
expr {$n+0} ;# Convert to number type
list [json number $n] $n
} -cleanup {
unset -nocomplain n
} -result [list 42 \r42]
#>>>
test number-1.18.6 {leading whitespace: '\f'} -body { #<<<
set n \f42
expr {$n+0} ;# Convert to number type
list [json number $n] $n
} -cleanup {
unset -nocomplain n
} -result [list 42 \f42]
#>>>
test number-2.1 {Too few args} -body { #<<<
set code [catch {
json number
} r o]
list $code $r [dict get $o -errorcode]
} -result {1 {wrong # args: should be "*number value"} {TCL WRONGARGS}} -match glob
#>>>
test number-2.2 {Too many args} -body { #<<<
set code [catch {
json number foo bar
} r o]
list $code $r [dict get $o -errorcode]
} -result {1 {wrong # args: should be "*number value"} {TCL WRONGARGS}} -match glob
#>>>
test number-2.3 {json number, not a number} -body { #<<<
set code [catch {json number foo} r o]
list $code $r [dict get $o -errorcode]
} -cleanup {
unset -nocomplain code r o
} -result [list 1 {can't use non-numeric string as operand of "+"} {ARITH DOMAIN {non-numeric string}}]
#>>>
test number-2.4 {json number, not a number: empty string} -body { #<<<
set code [catch {json number ""} r o]
list $code $r [dict get $o -errorcode]
} -cleanup {
unset -nocomplain code r o
} -result [list 1 {can't use empty string as operand of "+"} {ARITH DOMAIN {empty string}}]
#>>>
::tcltest::cleanupTests
return
# Local Variables:
# mode: tcl
# tab-width: 4
# End:
# vim: ft=tcl foldmethod=marker foldmarker=<<<,>>> ts=4 shiftwidth=4