mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
77 lines
1.4 KiB
V
77 lines
1.4 KiB
V
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
|
|
}
|
|
}
|