github.com/tetratelabs/wazero@v1.2.1/internal/platform/fdset_test.go (about) 1 package platform 2 3 import ( 4 "runtime" 5 "testing" 6 7 "github.com/tetratelabs/wazero/internal/testing/require" 8 ) 9 10 func TestFdSet(t *testing.T) { 11 if runtime.GOOS != "linux" && runtime.GOOS != "darwin" { 12 t.Skip("not supported") 13 } 14 15 allBitsSetAtIndex0 := FdSet{} 16 allBitsSetAtIndex0.Bits[0] = -1 17 18 tests := []struct { 19 name string 20 init FdSet 21 exec func(fdSet *FdSet) 22 expected FdSet 23 }{ 24 { 25 name: "all bits set", 26 exec: func(fdSet *FdSet) { 27 for fd := 0; fd < nfdbits; fd++ { 28 fdSet.Set(fd) 29 } 30 }, 31 expected: allBitsSetAtIndex0, 32 }, 33 { 34 name: "all bits cleared", 35 init: allBitsSetAtIndex0, 36 exec: func(fdSet *FdSet) { 37 for fd := 0; fd < nfdbits; fd++ { 38 fdSet.Clear(fd) 39 } 40 }, 41 expected: FdSet{}, 42 }, 43 { 44 name: "zero should clear all bits", 45 init: allBitsSetAtIndex0, 46 exec: func(fdSet *FdSet) { 47 fdSet.Zero() 48 }, 49 expected: FdSet{}, 50 }, 51 { 52 name: "is-set should return true for all bits", 53 init: allBitsSetAtIndex0, 54 exec: func(fdSet *FdSet) { 55 for i := range fdSet.Bits { 56 require.True(t, fdSet.IsSet(i)) 57 } 58 }, 59 expected: allBitsSetAtIndex0, 60 }, 61 { 62 name: "is-set should return true for all odd bits", 63 init: FdSet{}, 64 exec: func(fdSet *FdSet) { 65 for fd := 1; fd < nfdbits; fd += 2 { 66 fdSet.Set(fd) 67 } 68 for fd := 0; fd < nfdbits; fd++ { 69 isSet := fdSet.IsSet(fd) 70 if fd&0x1 == 0x1 { 71 require.True(t, isSet) 72 } else { 73 require.False(t, isSet) 74 } 75 } 76 fdSet.Zero() 77 }, 78 expected: FdSet{}, 79 }, 80 { 81 name: "should clear all even bits", 82 init: allBitsSetAtIndex0, 83 exec: func(fdSet *FdSet) { 84 for fd := 0; fd < nfdbits; fd += 2 { 85 fdSet.Clear(fd) 86 } 87 for fd := 0; fd < nfdbits; fd++ { 88 isSet := fdSet.IsSet(fd) 89 if fd&0x1 == 0x1 { 90 require.True(t, isSet) 91 } else { 92 require.False(t, isSet) 93 } 94 } 95 fdSet.Zero() 96 }, 97 expected: FdSet{}, 98 }, 99 } 100 101 for _, tt := range tests { 102 tc := tt 103 t.Run(tc.name, func(t *testing.T) { 104 x := tc.init 105 tc.exec(&x) 106 require.Equal(t, tc.expected, x) 107 }) 108 } 109 }