aboutsummaryrefslogtreecommitdiff
path: root/v_windows/v/vlib/v/tests/shift_test.v
blob: 0a8997d46b0d171f42cab992ad9ccc8062142c40 (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
type MyInt = int

fn test_shift_operators() {
	// check that shift works with all integer types
	// as the right-hand side operand
	a := 1
	b := 1024
	i := 10
	assert b == a << i8(i)
	assert b == a << byte(i)
	assert b == a << i16(i)
	assert b == a << u16(i)
	assert b == a << int(i)
	assert b == a << u32(i)
	assert b == a << i64(i)
	assert b == a << u64(i)
	assert a == b >> i8(i)
	assert a == b >> byte(i)
	assert a == b >> i16(i)
	assert a == b >> u16(i)
	assert a == b >> int(i)
	assert a == b >> u32(i)
	assert a == b >> i64(i)
	assert a == b >> u64(i)
	// check that shift operation result type is
	// the same as the type of the left-hand side operand
	mut c := u64(0)
	d := u64(1)
	c = d << i8(63)
	assert c == 9223372036854775808
	// check that shift-assign works with all types
	// of integers on the right-hand side
	mut e := 1
	e <<= i8(i)
	assert e == b
	e >>= i8(i)
	assert e == a
	e <<= i16(i)
	assert e == b
	e >>= i16(i)
	assert e == a
	e <<= int(i)
	assert e == b
	e >>= int(i)
	assert e == a
	mut e2 := i64(1)
	e2 <<= i64(i)
	assert e2 == b
	e2 >>= i64(i)
	assert e2 == a
	e <<= byte(i)
	assert e == b
	e >>= byte(i)
	assert e == a
	e <<= u16(i)
	assert e == b
	e >>= u16(i)
	assert e == a
	mut e3 := u64(1)
	e3 <<= u32(i)
	assert e3 == b
	e3 >>= u32(i)
	assert e == a
	e3 <<= u64(i)
	assert e3 == b
	e3 >>= u64(i)
	assert e3 == a
	// Test shifts with custom int types
	x := MyInt(2)
	assert x << 2 == 8
}