mirror of
https://github.com/go-sylixos/elvish.git
synced 2024-12-04 10:57:50 +08:00
sys: Add test cases
This commit is contained in:
parent
555b050120
commit
c51c4e2bec
30
sys/fcntl_test.go
Normal file
30
sys/fcntl_test.go
Normal file
|
@ -0,0 +1,30 @@
|
|||
package sys
|
||||
|
||||
import (
|
||||
"syscall"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func mustNil(e error) {
|
||||
if e != nil {
|
||||
panic("error is not nil")
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetSetNonblock(t *testing.T) {
|
||||
var p [2]int
|
||||
mustNil(syscall.Pipe(p[:]))
|
||||
for _, b := range []bool{true, false} {
|
||||
if e := SetNonblock(p[0], b); e != nil {
|
||||
t.Errorf("SetNonblock(%v, %v) => %v, want <nil>", p[0], b, e)
|
||||
}
|
||||
if nb, e := GetNonblock(p[0]); nb != b || e != nil {
|
||||
t.Errorf("GetNonblock(%v) => (%v, %v), want (%v, <nil>)", p[0], nb, e, b)
|
||||
}
|
||||
}
|
||||
syscall.Close(p[0])
|
||||
syscall.Close(p[1])
|
||||
if e := SetNonblock(p[0], true); e == nil {
|
||||
t.Errorf("SetNonblock(%v, true) => <nil>, want non-<nil>", p[0])
|
||||
}
|
||||
}
|
53
sys/select_test.go
Normal file
53
sys/select_test.go
Normal file
|
@ -0,0 +1,53 @@
|
|||
package sys
|
||||
|
||||
import (
|
||||
"syscall"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestFdSet(t *testing.T) {
|
||||
fs := NewFdSet(42, 233)
|
||||
fs.Set(77)
|
||||
fds := []int{42, 233, 77}
|
||||
for _, i := range fds {
|
||||
if !fs.IsSet(i) {
|
||||
t.Errorf("fs.IsSet(%d) => false, want true", i)
|
||||
}
|
||||
}
|
||||
fs.Clear(233)
|
||||
if fs.IsSet(233) {
|
||||
t.Errorf("fs.IsSet(233) => true, want false")
|
||||
}
|
||||
fs.Zero()
|
||||
for _, i := range fds {
|
||||
if fs.IsSet(i) {
|
||||
t.Errorf("fs.IsSet(%d) => true, want false", i)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestSelect(t *testing.T) {
|
||||
var p1, p2 [2]int
|
||||
mustNil(syscall.Pipe(p1[:]))
|
||||
mustNil(syscall.Pipe(p2[:]))
|
||||
fs := NewFdSet(p1[0], p2[0])
|
||||
var maxfd int
|
||||
if p1[0] > p2[0] {
|
||||
maxfd = p1[0] + 1
|
||||
} else {
|
||||
maxfd = p2[0] + 1
|
||||
}
|
||||
go func() {
|
||||
syscall.Write(p1[1], []byte("to p1"))
|
||||
syscall.Write(p2[1], []byte("to p2"))
|
||||
syscall.Close(p1[1])
|
||||
syscall.Close(p2[1])
|
||||
}()
|
||||
n, e := Select(maxfd+1, fs, nil, nil, nil)
|
||||
if n < 1 || e != nil {
|
||||
t.Errorf("Select(%v, %v, nil, nil, nil) => (%v, %v), want (>=1, <nil>)",
|
||||
maxfd+1, fs, n, e)
|
||||
}
|
||||
syscall.Close(p1[0])
|
||||
syscall.Close(p2[0])
|
||||
}
|
Loading…
Reference in New Issue
Block a user