2019-08-04 10:16:44 +03:00
|
|
|
const (
|
|
|
|
a = 3
|
2019-09-01 22:37:22 +03:00
|
|
|
u = u64(1)
|
|
|
|
)
|
2019-08-04 10:16:44 +03:00
|
|
|
|
|
|
|
fn test_const() {
|
2019-09-01 22:37:22 +03:00
|
|
|
b := (true && true) || false
|
|
|
|
assert b == true
|
|
|
|
assert a == 3
|
|
|
|
assert u == u64(1)
|
2019-09-23 02:14:10 +03:00
|
|
|
assert u == 1 // make sure this works without the cast
|
2019-09-01 22:37:22 +03:00
|
|
|
}
|
2019-08-05 17:57:54 +03:00
|
|
|
|
2019-08-12 02:58:08 +03:00
|
|
|
fn test_str_methods() {
|
|
|
|
assert i8(1).str() == '1'
|
|
|
|
assert i8(-1).str() == '-1'
|
|
|
|
assert i16(1).str() == '1'
|
|
|
|
assert i16(-1).str() == '-1'
|
2019-09-01 22:37:22 +03:00
|
|
|
assert int(1).str() == '1'
|
|
|
|
assert int(-1).str() == '-1'
|
2020-09-18 12:56:16 +03:00
|
|
|
assert int(2147483647).str() == '2147483647'
|
|
|
|
assert int(2147483648).str() == '-2147483648'
|
|
|
|
assert int(-2147483648).str() == '-2147483648'
|
2019-08-12 02:58:08 +03:00
|
|
|
assert i64(1).str() == '1'
|
|
|
|
assert i64(-1).str() == '-1'
|
|
|
|
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'
|
2020-05-04 14:20:18 +03:00
|
|
|
assert voidptr(-1).str() == 'ffffffffffffffff'
|
|
|
|
assert voidptr(1).str() == '1'
|
2021-08-22 11:10:24 +03:00
|
|
|
assert (&byte(-1)).str() == 'ffffffffffffffff'
|
|
|
|
assert (&byte(1)).str() == '1'
|
2020-05-04 14:20:18 +03:00
|
|
|
assert byteptr(-1).str() == 'ffffffffffffffff'
|
|
|
|
assert byteptr(1).str() == '1'
|
2019-08-12 02:58:08 +03:00
|
|
|
}
|
|
|
|
|
2020-04-02 20:02:49 +03:00
|
|
|
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)
|
2019-11-29 22:42:32 +03:00
|
|
|
}
|
|
|
|
|
2020-04-14 20:38:11 +03:00
|
|
|
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))
|
|
|
|
}
|
|
|
|
|
2019-12-03 16:09:37 +03:00
|
|
|
fn test_i8_print() {
|
|
|
|
b := i8(0)
|
|
|
|
println(b)
|
|
|
|
c := i16(7)
|
|
|
|
println(c)
|
|
|
|
d := u16(6)
|
|
|
|
println(d)
|
|
|
|
assert true
|
|
|
|
}
|
|
|
|
|
2019-08-09 11:21:21 +03:00
|
|
|
/*
|
2019-08-05 17:57:54 +03:00
|
|
|
fn test_cmp() {
|
|
|
|
assert 1 ≠ 2
|
|
|
|
assert 1 ⩽ 2
|
|
|
|
assert 1 ⩾ 0
|
2019-09-01 22:37:22 +03:00
|
|
|
}
|
2019-08-09 11:21:21 +03:00
|
|
|
*/
|
2020-09-25 13:02:32 +03:00
|
|
|
type MyInt = int
|
2019-12-05 18:47:29 +03:00
|
|
|
|
|
|
|
fn test_int_alias() {
|
2020-05-16 17:12:23 +03:00
|
|
|
i := MyInt(2)
|
2019-12-05 18:47:29 +03:00
|
|
|
assert i + 10 == 12
|
|
|
|
}
|
2020-02-04 19:44:39 +03:00
|
|
|
|
|
|
|
fn test_hex() {
|
|
|
|
x := u64(10)
|
2020-03-11 02:38:11 +03:00
|
|
|
assert x.hex() == 'a'
|
|
|
|
b := 1234
|
|
|
|
assert b.hex() == '4d2'
|
|
|
|
b1 := -1
|
|
|
|
assert b1.hex() == 'ffffffff'
|
2021-08-22 11:10:24 +03:00
|
|
|
// unsigned tests
|
|
|
|
assert u8(12).hex() == '0c'
|
|
|
|
assert u8(255).hex() == 'ff'
|
|
|
|
assert u16(65535).hex() == 'ffff'
|
|
|
|
assert u32(-1).hex() == 'ffffffff'
|
|
|
|
assert u64(-1).hex() == 'ffffffffffffffff'
|
|
|
|
// signed tests
|
|
|
|
assert i8(-1).hex() == 'ff'
|
|
|
|
assert i8(12).hex() == '0c'
|
|
|
|
assert i16(32767).hex() == '7fff'
|
|
|
|
assert int(2147483647).hex() == '7fffffff'
|
|
|
|
assert i64(9223372036854775807).hex() == '7fffffffffffffff'
|
2020-02-04 19:44:39 +03:00
|
|
|
}
|
2020-02-23 14:33:07 +03:00
|
|
|
|
2020-07-14 11:10:36 +03:00
|
|
|
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)
|
2021-12-15 10:00:38 +03:00
|
|
|
// C.char is unsigned on arm64, but signed on amd64, by default
|
|
|
|
$if arm64 {
|
|
|
|
assert int(x6) == 255
|
|
|
|
} $else {
|
|
|
|
assert int(x6) == -1
|
|
|
|
}
|
2020-07-14 11:10:36 +03:00
|
|
|
x7 := 0b0
|
|
|
|
assert x7 == 0
|
|
|
|
x8 := -0b0
|
|
|
|
assert x8 == 0
|
|
|
|
}
|
|
|
|
|
2020-02-23 14:33:07 +03:00
|
|
|
fn test_oct() {
|
|
|
|
x1 := 0o12
|
|
|
|
assert x1 == 10
|
2020-02-24 01:43:04 +03:00
|
|
|
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
|
2020-02-23 14:33:07 +03:00
|
|
|
}
|
2020-03-19 09:24:49 +03:00
|
|
|
|
2020-07-14 16:46:13 +03:00
|
|
|
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
|
2020-07-16 17:29:07 +03:00
|
|
|
assert 0o1_73 == 123
|
|
|
|
assert -0o17_5 == -125
|
2020-07-28 09:09:19 +03:00
|
|
|
assert -0o175 == -125
|
2020-07-14 16:46:13 +03:00
|
|
|
|
|
|
|
// hex
|
2020-07-16 17:29:07 +03:00
|
|
|
assert 0xFF == 255
|
2020-07-14 16:46:13 +03:00
|
|
|
assert 0xF_F == 255
|
|
|
|
|
|
|
|
// f32 or f64
|
|
|
|
assert 312_2.55 == 3122.55
|
|
|
|
assert 312_2.55 == 3122.55
|
|
|
|
}
|
|
|
|
|
2020-03-19 09:24:49 +03:00
|
|
|
fn test_int_decl() {
|
|
|
|
x1 := 0
|
|
|
|
x2 := 1333
|
|
|
|
x3 := -88955
|
|
|
|
x4 := 2000000000
|
|
|
|
x5 := -1999999999
|
2021-01-07 22:32:02 +03:00
|
|
|
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'
|
2020-03-23 22:05:37 +03:00
|
|
|
x7 := u64(-321314588900011)
|
2021-01-07 22:32:02 +03:00
|
|
|
assert typeof(x7).name == 'u64'
|
2020-03-19 09:24:49 +03:00
|
|
|
}
|
2020-03-25 00:23:59 +03:00
|
|
|
|
2020-04-02 18:16:17 +03:00
|
|
|
fn test_int_to_hex() {
|
|
|
|
// array hex
|
2020-05-16 17:12:23 +03:00
|
|
|
st := [byte(`V`), `L`, `A`, `N`, `G`]
|
|
|
|
assert st.hex() == '564c414e47'
|
2020-04-02 18:16:17 +03:00
|
|
|
assert st.hex().len == 10
|
|
|
|
st1 := [byte(0x41)].repeat(100)
|
2020-05-16 17:12:23 +03:00
|
|
|
assert st1.hex() == '41'.repeat(100)
|
|
|
|
// --- int to hex tests
|
2020-04-02 18:16:17 +03:00
|
|
|
c0 := 12
|
|
|
|
// 8Bit
|
2020-08-02 00:06:08 +03:00
|
|
|
assert byte(0).hex() == '00'
|
|
|
|
assert byte(c0).hex() == '0c'
|
|
|
|
assert i8(c0).hex() == '0c'
|
2020-05-16 17:12:23 +03:00
|
|
|
assert byte(127).hex() == '7f'
|
|
|
|
assert i8(127).hex() == '7f'
|
|
|
|
assert byte(255).hex() == 'ff'
|
|
|
|
assert byte(-1).hex() == 'ff'
|
2020-04-02 18:16:17 +03:00
|
|
|
// 16bit
|
2020-05-16 17:12:23 +03:00
|
|
|
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'
|
2020-04-02 18:16:17 +03:00
|
|
|
// 32bit
|
2020-05-16 17:12:23 +03:00
|
|
|
assert u32(0).hex() == '0'
|
|
|
|
assert c0.hex() == 'c'
|
|
|
|
assert u32(c0).hex() == 'c'
|
|
|
|
assert 2147483647.hex() == '7fffffff'
|
|
|
|
assert u32(2147483647).hex() == '7fffffff'
|
2020-05-27 06:42:48 +03:00
|
|
|
assert (-1).hex() == 'ffffffffffffffff'
|
2020-05-16 17:12:23 +03:00
|
|
|
assert u32(4294967295).hex() == 'ffffffff'
|
2020-04-02 18:16:17 +03:00
|
|
|
// 64 bit
|
2020-05-16 17:12:23 +03:00
|
|
|
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'
|
2020-04-02 18:16:17 +03:00
|
|
|
}
|
2021-09-30 09:32:20 +03:00
|
|
|
|
|
|
|
fn test_repeat() {
|
|
|
|
b := byte(`V`)
|
|
|
|
assert b.repeat(5) == 'VVVVV'
|
|
|
|
assert b.repeat(1) == b.ascii_str()
|
|
|
|
assert b.repeat(0) == ''
|
|
|
|
}
|