1 /* t40a.c 2 * 3 * Test FD_* macros 4 * 5 * Select works on regular files, (pseudo) terminal devices, streams-based 6 * files, FIFOs, pipes, and sockets. This test verifies the FD_* macros. 7 * 8 * This test is part of a bigger select test. It expects as argument which sub- 9 * test it is. 10 */ 11 12 #include <stdio.h> 13 #include <stdlib.h> 14 #include <unistd.h> 15 #include <sys/select.h> 16 #include <errno.h> 17 #include <limits.h> 18 19 #ifndef OPEN_MAX 20 # define OPEN_MAX 1024 21 #endif 22 23 #define MAX_ERROR 5 24 25 #include "common.h" 26 27 int main(int argc, char **argv) { 28 fd_set fds; 29 int i; 30 31 /* Get subtest number */ 32 if(argc != 2) { 33 printf("Usage: %s subtest_no\n", argv[0]); 34 exit(-1); 35 } else if(sscanf(argv[1], "%d", &subtest) != 1) { 36 printf("Usage: %s subtest_no\n", argv[0]); 37 exit(-1); 38 } 39 40 /* FD_ZERO */ 41 FD_ZERO(&fds); 42 for(i = 0; i < OPEN_MAX; i++) { 43 if(FD_ISSET(i, &fds)) { 44 em(1, "fd set should be completely empty"); 45 break; 46 } 47 } 48 49 /* FD_SET */ 50 for(i = 0; i < OPEN_MAX; i++) FD_SET(i, &fds); 51 for(i = 0; i < OPEN_MAX; i++) { 52 if(!FD_ISSET(i, &fds)) { 53 em(2, "fd set should be completely filled"); 54 break; 55 } 56 } 57 58 /* Reset to empty set and verify it's really empty */ 59 FD_ZERO(&fds); 60 for(i = 0; i < OPEN_MAX; i++) { 61 if(FD_ISSET(i, &fds)) { 62 em(3, "fd set should be completely empty"); 63 break; 64 } 65 } 66 67 /* Let's try a variation on filling the set */ 68 for(i = 0; i < OPEN_MAX; i += 2) FD_SET(i, &fds); 69 for(i = 0; i < OPEN_MAX - 1; i+= 2 ) { 70 if(!(FD_ISSET(i, &fds) && !FD_ISSET(i+1, &fds))) { 71 em(4, "bit pattern does not match"); 72 break; 73 } 74 } 75 76 /* Reset to empty set and verify it's really empty */ 77 FD_ZERO(&fds); 78 for(i = 0; i < OPEN_MAX; i++) { 79 if(FD_ISSET(i, &fds)) { 80 em(5,"fd set should be completely empty"); 81 break; 82 } 83 } 84 85 /* Let's try another variation on filling the set */ 86 for(i = 0; i < OPEN_MAX - 1; i += 2) FD_SET(i+1, &fds); 87 for(i = 0; i < OPEN_MAX - 1; i+= 2 ) { 88 if(!(FD_ISSET(i+1, &fds) && !FD_ISSET(i, &fds))) { 89 em(6, "bit pattern does not match"); 90 break; 91 } 92 } 93 94 /* Reset to empty set and verify it's really empty */ 95 FD_ZERO(&fds); 96 for(i = 0; i < OPEN_MAX; i++) { 97 if(FD_ISSET(i, &fds)) { 98 em(7, "fd set should be completely empty"); 99 break; 100 } 101 } 102 103 /* FD_CLR */ 104 for(i = 0; i < OPEN_MAX; i++) FD_SET(i, &fds); /* Set all bits */ 105 for(i = 0; i < OPEN_MAX; i++) FD_CLR(i, &fds); /* Clear all bits */ 106 for(i = 0; i < OPEN_MAX; i++) { 107 if(FD_ISSET(i, &fds)) { 108 em(8, "all bits in fd set should be cleared"); 109 break; 110 } 111 } 112 113 /* Reset to empty set and verify it's really empty */ 114 FD_ZERO(&fds); 115 for(i = 0; i < OPEN_MAX; i++) { 116 if(FD_ISSET(i, &fds)) { 117 em(9, "fd set should be completely empty"); 118 break; 119 } 120 } 121 122 for(i = 0; i < OPEN_MAX; i++) FD_SET(i, &fds); /* Set all bits */ 123 for(i = 0; i < OPEN_MAX; i += 2) FD_CLR(i, &fds); /* Clear all bits */ 124 for(i = 0; i < OPEN_MAX; i += 2) { 125 if(FD_ISSET(i, &fds)) { 126 em(10, "all even bits in fd set should be cleared"); 127 break; 128 } 129 } 130 131 exit(errct); 132 } 133