golang.org/x/sys@v0.20.1-0.20240517151509-673e0f94c16d/unix/fdset_test.go (about)

     1  // Copyright 2019 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  //go:build aix || darwin || dragonfly || freebsd || linux || netbsd || openbsd || solaris
     6  
     7  package unix_test
     8  
     9  import (
    10  	"testing"
    11  
    12  	"golang.org/x/sys/unix"
    13  )
    14  
    15  func TestFdSet(t *testing.T) {
    16  	var fdSet unix.FdSet
    17  	fdSet.Zero()
    18  	for fd := 0; fd < unix.FD_SETSIZE; fd++ {
    19  		if fdSet.IsSet(fd) {
    20  			t.Fatalf("Zero did not clear fd %d", fd)
    21  		}
    22  		fdSet.Set(fd)
    23  	}
    24  
    25  	for fd := 0; fd < unix.FD_SETSIZE; fd++ {
    26  		if !fdSet.IsSet(fd) {
    27  			t.Fatalf("IsSet(%d): expected true, got false", fd)
    28  		}
    29  	}
    30  
    31  	fdSet.Zero()
    32  	for fd := 0; fd < unix.FD_SETSIZE; fd++ {
    33  		if fdSet.IsSet(fd) {
    34  			t.Fatalf("Zero did not clear fd %d", fd)
    35  		}
    36  	}
    37  
    38  	for fd := 1; fd < unix.FD_SETSIZE; fd += 2 {
    39  		fdSet.Set(fd)
    40  	}
    41  
    42  	for fd := 0; fd < unix.FD_SETSIZE; fd++ {
    43  		if fd&0x1 == 0x1 {
    44  			if !fdSet.IsSet(fd) {
    45  				t.Fatalf("IsSet(%d): expected true, got false", fd)
    46  			}
    47  		} else {
    48  			if fdSet.IsSet(fd) {
    49  				t.Fatalf("IsSet(%d): expected false, got true", fd)
    50  			}
    51  		}
    52  	}
    53  
    54  	for fd := 1; fd < unix.FD_SETSIZE; fd += 2 {
    55  		fdSet.Clear(fd)
    56  	}
    57  
    58  	for fd := 0; fd < unix.FD_SETSIZE; fd++ {
    59  		if fdSet.IsSet(fd) {
    60  			t.Fatalf("Clear(%d) did not clear fd", fd)
    61  		}
    62  	}
    63  }