1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

freestanding: add core linux syscalls and associated tests

This commit is contained in:
Dwight Schauer
2019-11-23 10:35:57 -06:00
committed by Alexander Medvednikov
parent 666509623e
commit e724792a67
9 changed files with 779 additions and 9 deletions

1
vlib/builtin/bare/.checks/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
checks

View File

@@ -0,0 +1,29 @@
module main
import os
fn failed (msg string) {
println ("!!! failed: $msg")
}
fn passed (msg string) {
println (">>> passed: $msg")
}
fn vcheck(vfile string) {
run_check := "v -freestanding --enable-globals run "
if 0 == os.system("$run_check $vfile/${vfile}.v") {
passed(run_check)
} else {
failed(run_check)
}
os.system("rm -f $vfile/$vfile")
}
fn main() {
vcheck("string")
vcheck("linuxsys")
exit(0)
}

View File

@@ -0,0 +1,83 @@
module main
__global fd [2]int
__global buffer [16]byte
const (
sample_text_file1 = ""
)
fn check_read_write_pipe() {
/*
Checks the following system calls:
sys_pipe
sys_write
sys_read
sys_close
*/
println ("checking pipe read/write")
fd[0] = -1
fd[1] = -1
assert fd[0] == -1
assert fd[1] == -1
a := sys_pipe(intptr(fd))
assert a != -1
assert fd[0] != -1
assert fd[1] != -1
test_data := "test_data"
b := test_data.len + 1
mut c := sys_write (fd[1], test_data.str, u64(b))
assert c == b
c = sys_read(fd[0], byteptr(buffer), u64(b))
assert c == b
assert buffer[b-1] == 0
for i in 0..b {
assert test_data[i] == buffer[i]
}
assert 0 == sys_close(fd[0])
assert 0 == sys_close(fd[1])
assert 0 != sys_close(-1)
println ("pipe read/write passed")
}
fn check_read_file() {
/*
Checks the following system calls:
sys_read
sys_write
sys_close
*/
test_file := "sample_text1.txt"
sample_text := "Do not change this text.\n"
println ("checking read file")
fd := sys_open(test_file.str, int(fcntl.o_rdonly), 0)
assert fd > 0
n := sample_text.len
c := sys_read(fd, buffer, u64(n*2))
assert c == n
for i in 0..n {
assert sample_text[i] == buffer[i]
}
assert 0 == sys_close(fd)
println("read file passed")
}
fn main() {
check_read_write_pipe()
check_read_file()
sys_exit(0)
}

View File

@@ -0,0 +1,5 @@
In this directory:
```
v run checks.v
```

View File

@@ -0,0 +1 @@
Do not change this text.

View File

@@ -0,0 +1,15 @@
module main
fn check_string_eq () {
println ("checking string_eq")
assert "monkey" != "rat"
some_animal := "a bird"
assert some_animal == "a bird"
println ("string_eq passed")
}
fn main () {
check_string_eq ()
sys_exit(0)
}