diff options
author | Indrajith K L | 2022-12-03 17:00:20 +0530 |
---|---|---|
committer | Indrajith K L | 2022-12-03 17:00:20 +0530 |
commit | f5c4671bfbad96bf346bd7e9a21fc4317b4959df (patch) | |
tree | 2764fc62da58f2ba8da7ed341643fc359873142f /v_windows/v/vlib/v/tests/unsafe_test.v | |
download | cli-tools-windows-master.tar.gz cli-tools-windows-master.tar.bz2 cli-tools-windows-master.zip |
Diffstat (limited to 'v_windows/v/vlib/v/tests/unsafe_test.v')
-rw-r--r-- | v_windows/v/vlib/v/tests/unsafe_test.v | 104 |
1 files changed, 104 insertions, 0 deletions
diff --git a/v_windows/v/vlib/v/tests/unsafe_test.v b/v_windows/v/vlib/v/tests/unsafe_test.v new file mode 100644 index 0000000..75436b8 --- /dev/null +++ b/v_windows/v/vlib/v/tests/unsafe_test.v @@ -0,0 +1,104 @@ +fn test_ptr_assign() { + v := [int(5), 6, 77, 1] + unsafe { + mut p := &v[0] + (*p)++ + p++ // p now points to v[1] + (*p) += 2 + p += 2 // p now points to v[3] + *p = 31 + } + assert v[0] == 6 + assert v[1] == 8 + assert v[2] == 77 + assert v[3] == 31 +} + +fn test_double_ptr() { + i := 5 + j := 7 + unsafe { + mut x := &i + mut p := &x + (*p) = &j + assert x == &j + } + // /////// + mut x := &int(0) + unsafe { + mut p := &x + (*p) = &int(1) + } + assert ptr_str(x) == ptr_str(&int(1)) +} + +fn test_ptr_infix() { + v := 4 + mut q := unsafe { &v - 1 } + q = unsafe { q + 3 } + assert ptr_str(q) == ptr_str(unsafe { &v + 2 }) +} + +struct S1 { +} + +[unsafe] +fn (s S1) f() { +} + +fn test_funcs() { + s := S1{} + unsafe { s.f() } + _ = C.strerror(0) // [trusted] function prototype in builtin/cfns.c.v +} + +fn test_if_expr_unsafe() { + i := 4 + ii := 123 + p := if true { unsafe { &i } } else { unsafe { &ii } } + assert *p == 4 +} + +fn unsafe_if_stmt() int { + i := 4 + unsafe { + if true { + return (&i)[0] + } + } + return i +} + +fn test_unsafe_if_stmt() { + x := unsafe_if_stmt() + assert x == 4 +} + +const fixedbytes = [100]byte{} + +fn test_unsafe_pointers() { + fsize := fixedbytes.len + src := &fixedbytes[0] + // + b := []byte{} + eprintln('b.data before: $b.data') + eprintln('b.len before: $b.len') + eprintln('b.cap before: $b.cap') + assert b.len == 0 + unsafe { + // here b will be setup to work with the mmaped region + mut pdata := &b.data + mut plen := &b.len + mut pcap := &b.cap + // note that pdata, plen, pcap are used here: + *pdata = src + *plen = int(fsize) + *pcap = int(fsize) + } + assert b.len == 100 + assert b.cap == 100 + assert b.data == src + eprintln('b.data after: $b.data') + eprintln('b.len after: $b.len') + eprintln('b.cap after: $b.cap') +} |