sys: Add test cases

This commit is contained in:
Cheer Xiao 2014-09-20 23:11:20 +02:00
parent 555b050120
commit c51c4e2bec
2 changed files with 83 additions and 0 deletions

30
sys/fcntl_test.go Normal file
View 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
View 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])
}