aboutsummaryrefslogtreecommitdiff
path: root/v_windows/v/vlib/v/tests/pointers_multilevel_casts_test.v
blob: 3c55e58bf0472b41cf002d8cd7ea9267b7c290dc (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
struct Struct {
	name string
	x    int
}

fn test_byte_pointer_casts() {
	unsafe {
		pb := &byte(1)
		ppb := &&byte(2)
		pppb := &&&byte(3)
		ppppb := &&&&byte(4)
		assert voidptr(pb).str() == '1'
		assert voidptr(ppb).str() == '2'
		assert voidptr(pppb).str() == '3'
		assert voidptr(ppppb).str() == '4'
	}
}

fn test_char_pointer_casts() {
	unsafe {
		pc := &char(5)
		ppc := &&char(6)
		pppc := &&&char(7)
		ppppc := &&&&char(8)
		assert voidptr(pc).str() == '5'
		assert voidptr(ppc).str() == '6'
		assert voidptr(pppc).str() == '7'
		assert voidptr(ppppc).str() == '8'
	}
}

fn test_struct_pointer_casts() {
	unsafe {
		ps := &Struct(9)
		pps := &&Struct(10)
		ppps := &&&Struct(11)
		pppps := &&&&Struct(12)
		assert voidptr(ps).str() == '9'
		assert voidptr(pps).str() == 'a'
		assert voidptr(ppps).str() == 'b'
		assert voidptr(pppps).str() == 'c'
	}
}

fn test_struct_pointer_casts_with_field_selectors() {
	ss := &Struct{
		name: 'abc'
		x: 123
	}
	dump(ss)
	pss := voidptr(ss)
	if &Struct(pss).name == 'abc' {
		assert true
	}
	if &Struct(pss).x == 123 {
		// &Struct cast and selecting .x
		assert true
	}
	if &&Struct(pss) != 0 {
		// &&Struct
		assert true
	}
}

fn test_pointer_casts_with_indexing() {
	mut numbers := [5]u64{}
	numbers[0] = 123
	numbers[1] = 456
	unsafe {
		pnumbers := voidptr(&numbers[0])
		assert &u64(pnumbers)[0] == 123
		assert &u64(pnumbers)[1] == 456
		idx := 1
		assert &u64(pnumbers)[idx] == 456
	}
}