aboutsummaryrefslogtreecommitdiff
path: root/v_windows/v/old/vlib/builtin/int_test.v
blob: 900cf2736b283220e4e6ca244a9d983a33886574 (plain)
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
const (
	a = 3
	u = u64(1)
)

fn test_const() {
	b := (true && true) || false
	assert b == true
	assert a == 3
	assert u == u64(1)
	assert u == 1 // make sure this works without the cast
}

fn test_str_methods() {
	assert i8(1).str() == '1'
	assert i8(-1).str() == '-1'
	assert i16(1).str() == '1'
	assert i16(-1).str() == '-1'
	assert int(1).str() == '1'
	assert int(-1).str() == '-1'
	assert int(2147483647).str() == '2147483647'
	assert int(2147483648).str() == '-2147483648'
	assert int(-2147483648).str() == '-2147483648'
	assert i64(1).str() == '1'
	assert i64(-1).str() == '-1'
	// assert byte(1).str() == '1'
	// assert byte(-1).str() == '255'
	assert u16(1).str() == '1'
	assert u16(-1).str() == '65535'
	assert u32(1).str() == '1'
	assert u32(-1).str() == '4294967295'
	assert u64(1).str() == '1'
	assert u64(-1).str() == '18446744073709551615'
	assert voidptr(-1).str() == 'ffffffffffffffff'
	assert voidptr(1).str() == '1'
	assert byteptr(-1).str() == 'ffffffffffffffff'
	assert byteptr(1).str() == '1'
}

fn test_and_precendence() {
	assert (2 & 0 == 0) == ((2 & 0) == 0)
	assert (2 & 0 != 0) == ((2 & 0) != 0)
	assert (0 & 0 >= 0) == ((0 & 0) >= 0)
	assert (0 & 0 <= 0) == ((0 & 0) <= 0)
	assert (0 & 0 < 1) == ((0 & 0) < 1)
	assert (1 & 2 > 0) == ((1 & 2) > 0)
}

fn test_or_precendence() {
	assert (1 | 0 == 0) == ((1 | 0) == 0)
	assert (1 | 0 != 1) == ((1 | 0) != 1)
	assert (1 | 0 >= 2) == ((1 | 0) >= 2)
	assert (1 | 0 <= 0) == ((1 | 0) <= 0)
	assert (1 | 0 < 0) == ((1 | 0) < 0)
	assert (1 | 0 > 1) == ((1 | 0) > 1)
}

fn test_xor_precendence() {
	assert (1 ^ 0 == 2) == ((1 ^ 0) == 2)
	assert (1 ^ 0 != 2) == ((1 ^ 0) != 2)
	assert (1 ^ 0 >= 0) == ((1 ^ 0) >= 0)
	assert (1 ^ 0 <= 1) == ((1 ^ 0) <= 1)
	assert (1 ^ 0 < 0) == ((1 ^ 0) < 0)
	assert (1 ^ 0 > 1) == ((1 ^ 0) > 1)
}

fn test_left_shift_precendence() {
	assert (2 << 4 | 3) == ((2 << 4) | 3)
	assert (2 << 4 | 3) != (2 << (4 | 3))
}

fn test_right_shift_precendence() {
	assert (256 >> 4 | 3) == ((256 >> 4) | 3)
	assert (256 >> 4 | 3) != (256 >> (4 | 3))
}

fn test_i8_print() {
	b := i8(0)
	println(b)
	c := i16(7)
	println(c)
	d := u16(6)
	println(d)
	assert true
}

/*
fn test_cmp() {
	assert 1 ≠ 2
	assert 1 ⩽ 2
	assert 1 ⩾ 0
}
*/
type MyInt = int

fn test_int_alias() {
	i := MyInt(2)
	assert i + 10 == 12
}

fn test_hex() {
	x := u64(10)
	assert x.hex() == 'a'
	b := 1234
	assert b.hex() == '4d2'
	b1 := -1
	assert b1.hex() == 'ffffffff'
}

fn test_bin() {
	x1 := 0b10
	assert x1 == 2
	x2 := 0b10101010
	assert x2 == 0xAA
	x3 := -0b0000001
	assert x3 == -1
	x4 := 0b11111111
	assert x4 == 255
	x5 := byte(0b11111111)
	assert x5 == 255
	x6 := char(0b11111111)
	assert int(x6) == -1
	x7 := 0b0
	assert x7 == 0
	x8 := -0b0
	assert x8 == 0
}

fn test_oct() {
	x1 := 0o12
	assert x1 == 10
	x2 := 00000o350
	assert x2 == 232
	x3 := 000o00073
	assert x3 == 59
	x4 := 00000000
	assert x4 == 0
	x5 := 00000195
	assert x5 == 195
	x6 := -0o744
	assert x6 == -484
	x7 := -000o000042
	assert x7 == -34
	x8 := -0000112
	assert x8 == -112
	x9 := -000
	assert x9 == 0
}

fn test_num_separator() {
	// int
	assert 100_000_0 == 1000000
	assert -2_23_4_6 == -22346

	// bin
	assert 0b0_11 == 3
	assert -0b0_100 == -4

	// oct
	assert 0o1_73 == 123
	assert -0o17_5 == -125
	assert -0o175 == -125

	// hex
	assert 0xFF == 255
	assert 0xF_F == 255

	// f32 or f64
	assert 312_2.55 == 3122.55
	assert 312_2.55 == 3122.55

}

fn test_int_decl() {
	x1 := 0
	x2 := 1333
	x3 := -88955
	x4 := 2000000000
	x5 := -1999999999
	assert typeof(x1).name == 'int'
	assert typeof(x2).name == 'int'
	assert typeof(x3).name == 'int'
	assert typeof(x4).name == 'int'
	assert typeof(x5).name == 'int'
	x7 := u64(-321314588900011)
	assert typeof(x7).name == 'u64'
}

fn test_int_to_hex() {
	// array hex
	st := [byte(`V`), `L`, `A`, `N`, `G`]
	assert st.hex() == '564c414e47'
	assert st.hex().len == 10
	st1 := [byte(0x41)].repeat(100)
	assert st1.hex() == '41'.repeat(100)
	// --- int to hex tests
	c0 := 12
	// 8Bit
	assert byte(0).hex() == '00'
	assert byte(c0).hex() == '0c'
	assert i8(c0).hex() == '0c'
	assert byte(127).hex() == '7f'
	assert i8(127).hex() == '7f'
	assert byte(255).hex() == 'ff'
	assert byte(-1).hex() == 'ff'
	// 16bit
	assert u16(0).hex() == '0'
	assert i16(c0).hex() == 'c'
	assert u16(c0).hex() == 'c'
	assert i16(32767).hex() == '7fff'
	assert u16(32767).hex() == '7fff'
	assert i16(-1).hex() == 'ffff'
	assert u16(65535).hex() == 'ffff'
	// 32bit
	assert u32(0).hex() == '0'
	assert c0.hex() == 'c'
	assert u32(c0).hex() == 'c'
	assert 2147483647.hex() == '7fffffff'
	assert u32(2147483647).hex() == '7fffffff'
	assert (-1).hex() == 'ffffffffffffffff'
	assert u32(4294967295).hex() == 'ffffffff'
	// 64 bit
	assert u64(0).hex() == '0'
	assert i64(c0).hex() == 'c'
	assert u64(c0).hex() == 'c'
	assert i64(9223372036854775807).hex() == '7fffffffffffffff'
	assert u64(9223372036854775807).hex() == '7fffffffffffffff'
	assert i64(-1).hex() == 'ffffffffffffffff'
	assert u64(18446744073709551615).hex() == 'ffffffffffffffff'
}