test/t40a.c

http://www.minix3.org/ · C · 141 lines · 102 code · 19 blank · 20 comment · 35 complexity · 0d0a78e8c77bc32e70a277033e573769 MD5 · raw file

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