summary refs log tree commit diff stats
path: root/tests/int/tints.nim
diff options
context:
space:
mode:
Diffstat (limited to 'tests/int/tints.nim')
-rw-r--r--tests/int/tints.nim150
1 files changed, 150 insertions, 0 deletions
diff --git a/tests/int/tints.nim b/tests/int/tints.nim
new file mode 100644
index 000000000..773e8ccad
--- /dev/null
+++ b/tests/int/tints.nim
@@ -0,0 +1,150 @@
+discard """
+  matrix: "; --backend:js --jsbigint64:off -d:nimStringHash2; --backend:js --jsbigint64:on"
+  output: '''
+0 0
+0 0
+Success'''
+"""
+# Test the different integer operations
+
+
+import std/private/jsutils
+
+var testNumber = 0
+
+template test(opr, a, b, c: untyped): untyped =
+  # test the expression at compile and runtime
+  block:
+    const constExpr = opr(a, b)
+    when constExpr != c:
+      {.error: "Test failed " & $constExpr & " " & $c.}
+    inc(testNumber)
+    #Echo("Test: " & $testNumber)
+    var aa = a
+    var bb = b
+    var varExpr = opr(aa, bb)
+    assert(varExpr == c)
+
+test(`+`, 12'i8, -13'i16, -1'i16)
+test(`shl`, 0b11, 0b100, 0b110000)
+whenJsNoBigInt64: discard
+do:
+  test(`shl`, 0b11'i64, 0b100'i64, 0b110000'i64)
+when not defined(js):
+  # mixed type shr needlessly complicates codegen with bigint
+  # and thus is not yet supported in JS for 64 bit ints
+  test(`shl`, 0b11'i32, 0b100'i64, 0b110000'i64)
+test(`shl`, 0b11'i32, 0b100'i32, 0b110000'i32)
+
+test(`or`, 0xf0f0'i16, 0x0d0d'i16, 0xfdfd'i16)
+test(`and`, 0xf0f0'i16, 0xfdfd'i16, 0xf0f0'i16)
+
+whenJsNoBigInt64: discard
+do:
+  test(`shr`, 0xffffffffffffffff'i64, 0x4'i64, 0xffffffffffffffff'i64)
+test(`shr`, 0xffff'i16, 0x4'i16, 0xffff'i16)
+test(`shr`, 0xff'i8, 0x4'i8, 0xff'i8)
+
+whenJsNoBigInt64: discard
+do:
+  test(`shr`, 0xffffffff'i64, 0x4'i64, 0x0fffffff'i64)
+test(`shr`, 0xffffffff'i32, 0x4'i32, 0xffffffff'i32)
+
+whenJsNoBigInt64: discard
+do:
+  test(`shl`, 0xffffffffffffffff'i64, 0x4'i64, 0xfffffffffffffff0'i64)
+test(`shl`, 0xffff'i16, 0x4'i16, 0xfff0'i16)
+test(`shl`, 0xff'i8, 0x4'i8, 0xf0'i8)
+
+whenJsNoBigInt64: discard
+do:
+  test(`shl`, 0xffffffff'i64, 0x4'i64, 0xffffffff0'i64)
+test(`shl`, 0xffffffff'i32, 0x4'i32, 0xfffffff0'i32)
+
+# bug #916
+proc unc(a: float): float =
+  return a
+
+echo int(unc(0.5)), " ", int(unc(-0.5))
+echo int(0.5), " ", int(-0.5)
+
+block: # Casts to uint
+  template testCast(fromValue: typed, toType: typed, expectedResult: typed) =
+    let src = fromValue
+    let dst = cast[toType](src)
+    if dst != expectedResult:
+      echo "Casting ", astToStr(fromValue), " to ", astToStr(toType), " = ", dst.int, " instead of ", astToStr(expectedResult)
+    doAssert(dst == expectedResult)
+
+  testCast(-1'i16, uint16, 0xffff'u16)
+  testCast(0xffff'u16, int16, -1'i16)
+
+  testCast(0xff'u16, uint8, 0xff'u8)
+  testCast(0xffff'u16, uint8, 0xff'u8)
+
+  testCast(-1'i16, uint32, 0xffffffff'u32)
+  testCast(0xffffffff'u32, int32, -1)
+
+  testCast(0xfffffffe'u32, int32, -2'i32)
+  testCast(0xffffff'u32, int16, -1'i32)
+
+  testCast(-5'i32, uint8, 251'u8)
+
+# issue #7174
+let c = 1'u
+let val = c > 0
+doAssert val
+
+block: # bug #6752
+  when not defined(js) or (defined(js) and compileOption("jsbigint64")):
+    let x = 711127'i64
+    doAssert x * 86400'i64 == 61441372800'i64
+
+block: # bug #17604
+  let a = 2147483648'u
+  doAssert (a and a) == a
+  doAssert (a or 0) == a
+
+block: # bitwise not
+  let
+    z8 = 0'u8
+    z16 = 0'u16
+    z32 = 0'u32
+    z64 = 0'u64
+  doAssert (not z8) == uint8.high
+  doAssert (not z16) == uint16.high
+  doAssert (not z32) == uint32.high
+  when not defined(js) or (defined(js) and compileOption("jsbigint64")):
+    doAssert (not z64) == uint64.high
+
+block: # shl
+  let i8 = int8.high
+  let i16 = int16.high
+  let i32 = int32.high
+  let i64 = int64.high
+  doAssert i8 shl 1 == -2
+  doAssert i8 shl 2 == -4
+  doAssert i16 shl 1 == -2
+  doAssert i16 shl 2 == -4
+  doAssert i32 shl 1 == -2
+  doAssert i32 shl 2 == -4
+  when not defined(js) or (defined(js) and compileOption("jsbigint64")):
+    doAssert i64 shl 1 == -2
+    doAssert i64 shl 2 == -4
+
+  let u8 = uint8.high
+  let u16 = uint16.high
+  let u32 = uint32.high
+  let u64 = uint64.high
+  doAssert u8 shl 1 == u8 - 1
+  doAssert u16 shl 1 == u16 - 1
+  doAssert u32 shl 1 == u32 - 1
+  when not defined(js) or (defined(js) and compileOption("jsbigint64")):
+    doAssert u64 shl 1 == u64 - 1
+
+block: # bug #23378
+  var neg = -1  # prevent compile-time evaluation
+  let n = abs BiggestInt neg
+  doAssert n == 1
+
+echo("Success") #OUT Success
e/ranger/defaults/apps.py?id=bba8d293c32050b79cfc2b0ac2807f324ef44800'>^
19f5ef2d ^
b6aff4c3 ^
bd0ede8d ^
cc952d63 ^
871c502d ^
23236d0c ^


cc952d63 ^
19f5ef2d ^


540d5033 ^

58c84bd6 ^
d012468d ^
58c84bd6 ^
845bd407 ^
13c70add ^
0e2b3164 ^
96883700 ^

035a39a8 ^
d012468d ^
f61767b1 ^
e70f8c83 ^



09449df0 ^
d012468d ^
845bd407 ^
d012468d ^
e70f8c83 ^


845bd407 ^
2052eb74 ^

d012468d ^
cc952d63 ^

0dc57267 ^
cc952d63 ^


23236d0c ^
e70f8c83 ^
ea87d005 ^
cc952d63 ^
d012468d ^
cc952d63 ^
4d9266ac ^
d012468d ^
cc952d63 ^
44a31d6c ^

4e0ca535 ^
44a31d6c ^
a44749f6 ^
23236d0c ^
e70f8c83 ^
23236d0c ^
8b909983 ^
cc952d63 ^
d46a05a8 ^
1b0786f2 ^



d46a05a8 ^
1b0786f2 ^

bd0ede8d ^
1b0786f2 ^


d46a05a8 ^
845bd407 ^




d46a05a8 ^
23236d0c ^

a7772cc6 ^
cc952d63 ^
23236d0c ^
d46a05a8 ^
23236d0c ^


cc952d63 ^
23236d0c ^

871c502d ^
cc952d63 ^
a7772cc6 ^
23236d0c ^
d46a05a8 ^
23236d0c ^




d383965f ^
23236d0c ^
d383965f ^
f01238dc ^
db41bc89 ^
e3744f40 ^
23236d0c ^
d46a05a8 ^
23236d0c ^




ea87d005 ^
0dc57267 ^




d46a05a8 ^
845bd407 ^






ea87d005 ^
d46a05a8 ^
845bd407 ^






ea87d005 ^
d46a05a8 ^
845bd407 ^

845bd407 ^
a7772cc6 ^

748d0a6a ^
6eb85224 ^







a5e1ccdd ^

















748d0a6a ^



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