home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / gnustuff / tos / gcctest / tests05.zoo / tstdio.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-03-02  |  17.8 KB  |  965 lines

  1. /*            E x e r c i s e
  2.  *
  3.  * This program exercises the stdio routines. It does not validate the
  4.  * routines but provides a convenient way to quickly check the functionality
  5.  * of the code.
  6.  */
  7.  
  8. #ifdef _BSD
  9. # include <strings.h>
  10. # define remove unlink
  11. # define sleep(x)    usleep((x)*500)
  12. #else
  13. # include <stdlib.h>
  14. # include <string.h>
  15. #endif
  16. #include <stdio.h>
  17.  
  18. #ifdef atarist
  19. #include <memory.h>
  20. #include <unistd.h>
  21. #endif
  22.  
  23. #ifndef SEEK_SET
  24. /* lseek() origins */
  25. #define    SEEK_SET    0        /* from beginning of file */
  26. #define    SEEK_CUR    1        /* from current location */
  27. #define    SEEK_END    2        /* from end of file */
  28. #endif
  29.  
  30. #define UCHAR(x)    ((int) ((x) & 0xff))
  31.  
  32. #define DEC -123
  33. #define INT 255
  34. #define UNS (~0)
  35. #define TESTFILE    "test.dat"
  36. #define LARGEBUFS    16
  37. #ifdef        MSDOS
  38. # define    TTY    "con"
  39. #else
  40. # ifdef atarist
  41. #  define    TTY    "CON:"
  42. # else
  43. #  define    TTY    "/dev/tty"
  44. # endif
  45. #endif
  46.  
  47. #ifndef atarist
  48. extern void *malloc();            /* memory allocator */
  49. extern char *strcpy();            /* string copy */
  50. extern char *strcat();            /* string concatenation */
  51. extern int strcmp();            /* string compare */
  52. extern void exit();            /* exit */
  53. #endif
  54.  
  55. FILE *fp;                /* per test file pointer */
  56.  
  57. /*
  58.  * Line Buffered Write Test
  59.  *
  60.  * Write to a terminal. This tests that the output buffer is
  61.  * flushed on receipt of a \n.
  62.  */
  63.  
  64. void lbw_test()
  65.  
  66. {
  67.   int i;
  68.  
  69.   puts("\nLine buffered write test");
  70. #ifdef atarist
  71.   if ((fp = fopen(TTY, "wt")) != NULL) {
  72. #else
  73.   if ((fp = fopen(TTY, "w")) != NULL) {
  74. #endif
  75.     puts("<pause>ABCDEFGH");
  76.     puts("<pause>ABCD<pause>EFGH");
  77.     for (i = 0; i < 8; i++)
  78.       putc('A'+i, fp), sleep(1);
  79.     putc('\n', fp);
  80.     for (i = 0; i < 8; i++) {
  81.       putc('A'+i, fp);
  82.       if (i == 3)
  83.     fflush(fp);
  84.       sleep(1);
  85.     }
  86.     fclose(fp);
  87.     puts("");
  88.   }
  89. }
  90.  
  91. /*
  92.  * Unbuffered Write Test
  93.  *
  94.  * Test that characters are written directly to the output device
  95.  * when the stream is unbuffered.
  96.  */
  97.  
  98. void ubw_test()
  99.  
  100. {
  101.   int i;
  102.  
  103.   puts("\nUnbuffered write test");
  104. #ifdef atarist
  105.   if ((fp = fopen(TTY, "wt")) != NULL) {
  106. #else
  107.   if ((fp = fopen(TTY, "w")) != NULL) {
  108. #endif
  109.     setbuf(fp, (char *) 0);
  110.     puts("A<pause>B<pause>C<pause>D<pause>E<pause>F<pause>G<pause>H<pause>");
  111.     puts("A<pause>B<pause>C<pause>D<pause>E<pause>F<pause>G<pause>H<pause>");
  112.     for (i = 0; i < 8; i++)
  113.       putc('A'+i, fp), sleep(1);
  114.     putc('\n', fp);
  115.     for (i = 0; i < 8; i++)
  116.       putc('A'+i, fp), sleep(1);
  117.     fclose(fp);
  118.     puts("");
  119.   }
  120. }
  121.  
  122. /*
  123.  * Buffered Write Test
  124.  *
  125.  * Test that the data is written to the terminal on a per buffer
  126.  * basis.
  127.  */
  128.  
  129. void bw_test()
  130.  
  131. {
  132.   int i;
  133.  
  134.   puts("\nFully buffered write test");
  135. #ifdef atarist
  136.   if ((fp = fopen(TTY, "wt")) != NULL) {
  137. #else
  138.   if ((fp = fopen(TTY, "w")) != NULL) {
  139. #endif
  140.     setvbuf(fp, (char *) 0, _IOFBF, 4);
  141.     puts("<pause>ABCD<pause>EFGH<pause>");
  142. #ifdef atarist
  143.     /* in text mode when we putc('\n', fp) we add a '\r' so we are off
  144.        by one char
  145.      */
  146.     puts("AB<pause>CDEF<pause>GH<pause>");
  147. #else
  148.     puts("ABC<pause>DEFG<pause>H<pause>");
  149. #endif
  150.     for (i = 0; i < 8; i++)
  151.       putc('A'+i, fp), sleep(1);
  152.     putc('\n', fp);
  153.     for (i = 0; i < 8; i++)
  154.       putc('A'+i, fp), sleep(1);
  155.     fclose(fp);
  156.     sleep(2);
  157.     puts("");
  158.   }
  159. }
  160.  
  161. /* Formatted Output Test
  162.  *
  163.  * This exercises the output formatting code.
  164.  */
  165.  
  166. void fp_test()
  167.  
  168. {
  169.   int i, j, k, l;
  170.   char buf[7];
  171.   char *prefix = buf;
  172.   char tp[20];
  173.  
  174.   puts("\nFormatted output test");
  175.   printf("prefix  6d      6o      6x      6X      6u\n");
  176.   strcpy(prefix, "%");
  177.   for (i = 0; i < 2; i++) {
  178.     for (j = 0; j < 2; j++) {
  179.       for (k = 0; k < 2; k++) {
  180.     for (l = 0; l < 2; l++) {
  181.       strcpy(prefix, "%");
  182.       if (i == 0) strcat(prefix, "-");
  183.       if (j == 0) strcat(prefix, "+");
  184.       if (k == 0) strcat(prefix, "#");
  185.       if (l == 0) strcat(prefix, "0");
  186.       printf("%5s |", prefix);
  187.       strcpy(tp, prefix);
  188.       strcat(tp, "6d |");
  189.       printf(tp, DEC);
  190.       strcpy(tp, prefix);
  191.       strcat(tp, "6o |");
  192.       printf(tp, INT);
  193.       strcpy(tp, prefix);
  194.       strcat(tp, "6x |");
  195.       printf(tp, INT);
  196.       strcpy(tp, prefix);
  197.       strcat(tp, "6X |");
  198.       printf(tp, INT);
  199.       strcpy(tp, prefix);
  200.       strcat(tp, "6u |");
  201.       printf(tp, UNS);
  202.       printf("\n");
  203.     }
  204.       }
  205.     }
  206.   }
  207. }
  208.  
  209. /*
  210.  * String Output Test
  211.  *
  212.  * Test the string printf code.
  213.  */
  214.  
  215. void sw_test()
  216.  
  217. {
  218.   int i;
  219.   char buf[80];
  220.  
  221.   puts("\nTest sprintf functionality");
  222.   puts("13 bytes in 'Testing 1 2 3'");
  223.   i = sprintf(buf, "Testing %d %d %d", 1, 2, 3);
  224.   printf("%d bytes in '%s'\n", i, buf);
  225. }
  226.  
  227. /*
  228.  * String Input Test
  229.  *
  230.  * Test the string scanf code.
  231.  */
  232.  
  233. void sr_test()
  234.  
  235. {
  236.   int i, j;
  237.   char buf[80];
  238.  
  239.   puts("\nTest sscanf functionality");
  240.   puts("2 items yielding 25 and 'thompson'");
  241.   i = sscanf("25 thompson", "%d%s", &j, buf);
  242.   printf("%d items yielding %d and '%s'\n", i, j, buf);
  243. }
  244.  
  245. /*
  246.  * File Write and Read Test
  247.  *
  248.  * Test that a file can be written to and read from.
  249.  */
  250.  
  251. void frw_test()
  252.  
  253. {
  254.   int i, j, k;
  255.   char buf[80];
  256.  
  257.   puts("\nFile write and read check");
  258. #ifdef atarist
  259.   if ((fp = fopen(TESTFILE, "wt")) != NULL) {
  260. #else
  261.   if ((fp = fopen(TESTFILE, "w")) != NULL) {
  262. #endif
  263.     puts("3 items yielding 56, 789 and '56'");
  264.     puts("1 item yielding 'a72'");
  265.     fprintf(fp, "56789 0123 56a72");
  266. #ifdef atarist
  267.     if (freopen(TESTFILE, "rt", fp) != fp)
  268. #else
  269.     if (freopen(TESTFILE, "r", fp) != fp)
  270. #endif
  271.       puts("Cannot open file for reading");
  272.     else {
  273.       i = fscanf(fp, "%2d%d%*d %[0-9]", &j, &k, buf);
  274.       printf("%d items yielding %d, %d and '%s'\n", i, j, k, buf);
  275.       i = fscanf(fp, "%s", buf);
  276.       printf("%d item yielding '%s'\n", i, buf);
  277.       fclose(fp);
  278.     }
  279.   }
  280. }
  281.  
  282. /*
  283.  * File Seek Test
  284.  *
  285.  * Test that seek operations within files work.
  286.  */
  287.  
  288. void fs_test()
  289.  
  290. {
  291.   int i, j;
  292.  
  293.   puts("\nFile seek test");
  294. #ifdef atarist
  295.   if ((fp = fopen(TESTFILE, "wb")) != NULL) {
  296. #else
  297.   if ((fp = fopen(TESTFILE, "w")) != NULL) {
  298. #endif
  299.     for (i = 0; i < 256; i++)
  300.       putc(i, fp);
  301. #ifdef atarist
  302.     if (freopen(TESTFILE, "rb", fp) != fp)
  303. #else
  304.     if (freopen(TESTFILE, "r", fp) != fp)
  305. #endif
  306.       puts("Cannot open file for reading");
  307.     else {
  308.       for (i = 1; i <= 255; i++) {
  309.         printf("\r%3d ", i);
  310.     fflush(stdout);
  311.         fseek(fp, (long) -i, SEEK_END);
  312.     if ((j = getc(fp)) != 256-i) {
  313.       printf("SEEK_END failed %d\n", j);
  314.       break;
  315.     }
  316.     if (fseek(fp, (long) i, SEEK_SET)) {
  317.       puts("Cannot SEEK_SET");
  318.       break;
  319.     }
  320.     if ((j = getc(fp)) != i) {
  321.       printf("SEEK_SET failed %d\n", j);
  322.       break;
  323.     }
  324.     if (fseek(fp, (long) i, SEEK_SET)) {
  325.       puts("Cannot SEEK_SET");
  326.       break;
  327.     }
  328.     if (fseek(fp, (long) (i >= 128 ? -128 : 128), SEEK_CUR)) {
  329.       puts("Cannot SEEK_CUR");
  330.       break;
  331.     }
  332.     if ((j = getc(fp)) != (i >= 128 ? i-128 : i+128)) {
  333.       printf("SEEK_CUR failed %d\n", j);
  334.       break;
  335.     }
  336.       }
  337.       if (i > 255)
  338.     puts("ok");
  339.       fclose(fp);
  340.     }
  341.   }
  342. }
  343.  
  344. /*
  345.  * Test gets()
  346.  *
  347.  * Checks that gets() works.
  348.  */
  349.  
  350. void gets_test()
  351.  
  352. {
  353.   char buf[80];
  354.  
  355.   puts("\nGets functionality");
  356.   puts("... Type a line and have it echoed ...");
  357.   gets(buf);
  358.   puts(buf);
  359. }
  360.  
  361. /*
  362.  * Fputs Test
  363.  *
  364.  * Check that fputs() works into unbuffered streams.
  365.  */
  366.  
  367. void fputs_test()
  368.  
  369. {
  370.   puts("\nUnbuffered fputs test");
  371. #ifdef atarist
  372.   if ((fp = fopen(TTY, "wt")) != NULL) {
  373. #else
  374.   if ((fp = fopen(TTY, "w")) != NULL) {
  375. #endif
  376.     setbuf(fp, (char *) 0);
  377.     puts("ABCDEFGH<pause>");
  378.     puts("ABCDEFGH<pause>");
  379.     fputs("ABCDEFGH", fp), sleep(1);
  380.     fputs("\nABCDEFGH", fp), sleep(1);
  381.     fclose(fp);
  382.     puts("");
  383.   }
  384. }
  385.  
  386. /*
  387.  * Fprintf Test
  388.  *
  389.  * Check that fprintf() works into unbuffered streams.
  390.  */
  391.  
  392. void fprint_test()
  393.  
  394. {
  395.   puts("\nUnbuffered fprintf test");
  396. #ifdef atarist
  397.   if ((fp = fopen(TTY, "wt")) != NULL) {
  398. #else
  399.   if ((fp = fopen(TTY, "w")) != NULL) {
  400. #endif
  401.     setbuf(fp, (char *) 0);
  402.     puts("ABCDEFGH<pause>");
  403.     puts("ABCDEFGH<pause>");
  404.     fprintf(fp, "ABCDEFGH"), sleep(1);
  405.     fprintf(fp, "\nABCDEFGH"), sleep(1);
  406.     fclose(fp);
  407.     puts("");
  408.   }
  409. }
  410.  
  411. /*
  412.  * Fgets Test
  413.  *
  414.  * Check that fgets() works.
  415.  */
  416.  
  417. void fgets_test()
  418.  
  419. {
  420.   char buf[80];
  421.  
  422.   puts("\nFgets functionality");
  423.   puts("a");
  424.   puts("<pause>ab");
  425.   puts("<pause>abc");
  426.   puts("<pause>abcd");
  427.   puts("<pause>abcde");
  428.   puts("<pause>abcdef");
  429.   puts("<pause>abcdefg<pause>");
  430.   puts("<pause>abcdefg<pause>h");
  431. #ifdef atarist
  432.   if ((fp = fopen(TESTFILE, "wt")) != NULL) {
  433. #else
  434.   if ((fp = fopen(TESTFILE, "w")) != NULL) {
  435. #endif
  436.     fputs("a\n", fp);
  437.     fputs("ab\n", fp);
  438.     fputs("abc\n", fp);
  439.     fputs("abcd\n", fp);
  440.     fputs("abcde\n", fp);
  441.     fputs("abcdef\n", fp);
  442.     fputs("abcdefg\n", fp);
  443.     fputs("abcdefgh\n", fp);
  444.     fclose(fp);
  445. #ifdef atarist
  446.     if ((fp = fopen(TESTFILE, "rt")) != NULL) {
  447. #else
  448.     if ((fp = fopen(TESTFILE, "r")) != NULL) {
  449. #endif
  450.       while (fgets(buf, 8, fp) != NULL) {
  451.     fputs(buf, stdout);
  452.     fflush(stdout);
  453.     sleep(1);
  454.       }
  455.       fclose(fp);
  456.     }
  457.   }
  458. }
  459.  
  460. /*
  461.  * Word Read and Write Test
  462.  *
  463.  * Check that putw and getw work.
  464.  */
  465.  
  466. void word_test()
  467.  
  468. {
  469.   int i, j;
  470.  
  471.   puts("\nPutw and Readw Test");
  472. #ifdef atarist
  473.   if ((fp = fopen(TESTFILE, "wb")) != NULL) {
  474. #else
  475.   if ((fp = fopen(TESTFILE, "w")) != NULL) {
  476. #endif
  477.     for (i = 0; i < 256; i++)
  478.       putw(i, fp);
  479.     putc(0, fp);
  480.     fclose(fp);
  481. #ifdef atarist
  482.     if ((fp = fopen(TESTFILE, "rb")) != NULL) {
  483. #else
  484.     if ((fp = fopen(TESTFILE, "r")) != NULL) {
  485. #endif
  486.       for (i = 0; i < 256; i++) {
  487.     printf("\r%3d", i);
  488.     fflush(stdout);
  489.     if ((j = getw(fp)) != i) {
  490.       printf(" failed %d", j);
  491.       break;
  492.     }
  493.       }
  494.       if (i == 256 /* && getw(fp) == EOF && feof(fp)*/)
  495.     fputs(" ok", stdout);
  496.       puts("");
  497.       fclose(fp);
  498.     }
  499.   }
  500. }
  501.  
  502. /*
  503.  * Append Test
  504.  *
  505.  * Check that appends go to the end of the file.
  506.  */
  507.  
  508. void a_test()
  509.  
  510. {
  511.   int ch;
  512.  
  513.   puts("\nAppend Test");
  514. #ifdef atarist
  515.   if ((fp = fopen(TESTFILE, "wt")) != NULL) {
  516. #else
  517.   if ((fp = fopen(TESTFILE, "w")) != NULL) {
  518. #endif
  519.     putc('a', fp);
  520. #ifdef atarist
  521.     if ((fp = freopen(TESTFILE, "at", fp)) == NULL) {
  522. #else
  523.     if ((fp = freopen(TESTFILE, "a", fp)) == NULL) {
  524. #endif
  525.       puts("Cannot freopen file");
  526.       return;
  527.     }
  528.     if (fseek(fp, 0L, 0) == EOF) {
  529.       puts("Cannot fseek to start");
  530.       return;
  531.     }
  532.     putc('@', fp);
  533. #ifdef atarist
  534.     if ((fp = freopen(TESTFILE, "rt", fp)) == NULL) {
  535. #else
  536.     if ((fp = freopen(TESTFILE, "r", fp)) == NULL) {
  537. #endif
  538.       puts("Cannot freopen file");
  539.       return;
  540.     }
  541.     if ((ch = getc(fp)) != 'a')
  542.       printf("Failed a - %c\n", ch);
  543.     else if ((ch = getc(fp)) != '@')
  544.       printf("Failed @ - %c\n", ch);
  545.     else if ((ch = getc(fp)) != EOF)
  546.       printf("Failed EOF - %d\n", ch);
  547.     else
  548.       puts("Ok");
  549.     fclose(fp);
  550.     return;
  551.   }
  552. }
  553.  
  554. /*
  555.  * Write and Read Update Test
  556.  *
  557.  * Write a file in update mode, then try to read it.
  558.  */
  559.  
  560. void uwr_test()
  561.  
  562. {
  563.   int i, j;
  564.  
  565.   puts("\nWrite and Read Update Test");
  566. #ifdef atarist
  567.   if ((fp = fopen(TESTFILE, "wb+")) != NULL) {
  568. #else
  569.   if ((fp = fopen(TESTFILE, "w+")) != NULL) {
  570. #endif
  571.     for (i = 0; i < (3*BUFSIZ)/2; i++)
  572.       putc(i, fp);
  573.     rewind(fp);
  574.     for (i = 0; i < (3*BUFSIZ)/2; i++) {
  575.       printf("\r%4d", i);
  576.       fflush(stdout);
  577.       j = getc(fp);
  578.       if (j != UCHAR(i)) {
  579.     printf(" failed %d expect %d got %d\n", j, UCHAR(i), j);
  580.     break;
  581.       }
  582.     }
  583.     if (i == (3*BUFSIZ)/2)
  584.       puts(" ok");
  585.     if (getc(fp) != EOF)
  586.       puts(" failed to find eof");
  587.     else {
  588.       for (i = 0; i < BUFSIZ/2; i++)
  589.     putc(i, fp);
  590.       fseek(fp, (long) (3*BUFSIZ)/2, SEEK_SET);
  591.       for (i = 0; i < BUFSIZ/2; i++) {
  592.     printf("\r%4d", i);
  593.     fflush(stdout);
  594.     j = getc(fp);
  595.     if (j != UCHAR(i)) {
  596.       printf(" failed %d expect %d got %d\n", j, UCHAR(i), j);
  597.       break;
  598.     }
  599.       }
  600.       if (i == BUFSIZ/2)
  601.     puts(" ok");
  602.     }
  603.     fclose(fp);
  604.   }
  605. }
  606.  
  607. /*
  608.  * Write, Append and Read Update Test
  609.  *
  610.  * Write a file in update mode, close it, append to it and read it.
  611.  */
  612.  
  613. void uawr_test()
  614.  
  615. {
  616.   int i, j;
  617.  
  618.   puts("\nWrite, Append and Read Update Test");
  619. #ifdef atarist
  620.   if ((fp = fopen(TESTFILE, "wb")) != NULL) {
  621. #else
  622.   if ((fp = fopen(TESTFILE, "w")) != NULL) {
  623. #endif
  624.     for (i = 0; i < (3*BUFSIZ)/2; i++)
  625.       putc(i, fp);
  626.     fclose(fp);
  627. #ifdef atarist
  628.     if ((fp = fopen(TESTFILE, "ab+")) != NULL) {
  629. #else
  630.     if ((fp = fopen(TESTFILE, "a+")) != NULL) {
  631. #endif
  632.       for (i = 0; i < BUFSIZ/2; i++)
  633.     putc(i, fp);
  634.       fseek(fp, (long) (3*BUFSIZ)/2, SEEK_SET);
  635.       for (i = 0; i < BUFSIZ/2; i++) {
  636.     printf("\r%4d", i);
  637.     fflush(stdout);
  638.     j = getc(fp);
  639.     if (j != UCHAR(i)) {
  640.       printf(" failed %d expect %d got %d\n", j, UCHAR(i), j);
  641.       break;
  642.     }
  643.       }
  644.       if (i == BUFSIZ/2)
  645.     puts(" ok");
  646.       rewind(fp);
  647.       for (i = 0; i < (3*BUFSIZ)/2; i++) {
  648.     printf("\r%4d", i);
  649.     fflush(stdout);
  650.     j = getc(fp);
  651.     if (j != UCHAR(i)) {
  652.       printf(" failed at %d\n", j);
  653.       break;
  654.     }
  655.       }
  656.       if (i == (3*BUFSIZ)/2)
  657.     puts(" ok");
  658.     }
  659.     fclose(fp);
  660.   }
  661. }
  662.  
  663. /*
  664.  * Write, Read, Write and Read Update Test
  665.  *
  666.  * Write a file in update mode, read it, write it and read it.
  667.  */
  668.  
  669. void uwrwr_test()
  670.  
  671. {
  672.   int i, j;
  673.  
  674.   puts("\nWrite, Read, Write and Read Update Test");
  675. #ifdef atarist
  676.   if ((fp = fopen(TESTFILE, "wb")) != NULL) {
  677. #else
  678.   if ((fp = fopen(TESTFILE, "w")) != NULL) {
  679. #endif
  680.     for (i = 0; i < (3*BUFSIZ)/2; i++)
  681.       putc(i, fp);
  682.     fclose(fp);
  683. #ifdef atarist
  684.     if ((fp = fopen(TESTFILE, "rb+")) != NULL) {
  685. #else
  686.     if ((fp = fopen(TESTFILE, "r+")) != NULL) {
  687. #endif
  688.       for (i = 0; i < (3*BUFSIZ)/2; i++) {
  689.     printf("\r%4d", i);
  690.     fflush(stdout);
  691.     j = getc(fp);
  692.     if (j != UCHAR(i)) {
  693.       printf(" failed %d expect %d got %d\n", j, UCHAR(i), j);
  694.       break;
  695.     }
  696.       }
  697.       if (i == (3*BUFSIZ)/2)
  698.     puts(" ok");
  699.       if (getc(fp) != EOF)
  700.     puts(" failed to find eof");
  701.       else {
  702.     for (i = 0; i < BUFSIZ/2; i++)
  703.       putc(i, fp);
  704.     rewind(fp);
  705.     for (i = 0; i < (3*BUFSIZ)/2; i++)
  706.       putc((3*BUFSIZ)/2-i, fp);
  707.     fseek(fp, (long) (3*BUFSIZ)/2, SEEK_SET);
  708.     for (i = 0; i < BUFSIZ/2; i++) {
  709.       printf("\r%4d", i);
  710.       fflush(stdout);
  711.       j = getc(fp);
  712.       if (j != UCHAR(i)) {
  713.         printf(" failed %d expect %d got %d\n", j, UCHAR(i), j);
  714.         break;
  715.       }
  716.     }
  717.     if (i == BUFSIZ/2)
  718.       puts(" ok");
  719.     rewind(fp);
  720.     for (i = 0; i < (3*BUFSIZ)/2; i++) {
  721.       printf("\r%4d", i);
  722.       fflush(stdout);
  723.       j = getc(fp);
  724.       if (j != UCHAR((3*BUFSIZ)/2-i)) {
  725.         printf(" failed %d\n", j);
  726.         break;
  727.       }
  728.     }
  729.     if (i == (3*BUFSIZ)/2)
  730.       puts(" ok");
  731.       }
  732.       fclose(fp);
  733.     }
  734.   }
  735. }
  736.  
  737. /*
  738.  * Fwrite Test
  739.  *
  740.  * Test fwrite with small loads and large loads.
  741.  */
  742.  
  743. void fwrite_test()
  744.  
  745. {
  746.   unsigned int i, j;
  747.   char buf[1023];
  748.   char bbuf[3071];
  749.   double sqrt();
  750.   void free();
  751.   char *p;
  752.  
  753.   puts("\nFwrite Test");
  754. #ifdef atarist
  755.   if ((fp = fopen(TESTFILE, "wb+")) != NULL) {
  756. #else
  757.   if ((fp = fopen(TESTFILE, "w+")) != NULL) {
  758. #endif
  759.     for (i = 0; i < sizeof(buf); i++)
  760.       buf[i] = i;
  761.     for (i = 0; i < sizeof(bbuf); i++)
  762.       bbuf[i] = 19-i;
  763.  
  764.     for (i = 0; i < 256; i++) {
  765.       printf("\r%4d", i);
  766.       fflush(stdout);
  767.       if (fwrite(buf, 1, sizeof(buf), fp) != sizeof(buf)) {
  768.     puts(" failed\n");
  769.     return;
  770.       }
  771.       putc(0, fp);
  772.     }
  773.     puts(" small write ok");
  774.     rewind(fp);
  775.     for (i = 0; i < 256; i++) {
  776.       printf("\r%4d", i);
  777.       fflush(stdout);
  778.       for (j = 0; j < sizeof(buf); j++) {
  779.     if (getc(fp) != UCHAR(j)) {
  780.       puts(" failed\n");
  781.       return;
  782.     }
  783.       }
  784.       if (getc(fp) != 0) {
  785.     puts(" failed\n");
  786.     return;
  787.       }
  788.     }
  789.     puts(" verified ok");
  790.  
  791.     rewind(fp);
  792.     for (i = 0; i < 256; i++) {
  793.       printf("\r%4d", i);
  794.       fflush(stdout);
  795.       if (fwrite(bbuf, 1, sizeof(bbuf), fp) != sizeof(bbuf)) {
  796.     puts(" failed\n");
  797.     return;
  798.       }
  799.       putc(0, fp);
  800.     }
  801.     puts(" large write ok");
  802.     rewind(fp);
  803.     for (i = 0; i < 256; i++) {
  804.       printf("\r%4d", i);
  805.       fflush(stdout);
  806.       for (j = 0; j < sizeof(bbuf); j++) {
  807.     if (getc(fp) != UCHAR(19-j)) {
  808.       puts(" failed\n");
  809.       return;
  810.     }
  811.       }
  812.       if (getc(fp) != 0) {
  813.     puts(" failed\n");
  814.     return;
  815.       }
  816.     }
  817.     puts(" verified ok");
  818.  
  819.     rewind(fp);
  820.     if ((p = (char *) malloc(48*1024)) == 0) {
  821.       puts("No memory for large write test");
  822.       return;
  823.     }
  824.     for (j = 13, i = 48*1024; --i; j++)
  825.       p[i] = j;
  826.     fwrite(p, 48*1024, 1, fp);
  827.     rewind(fp);
  828.     for (i = 48*1024; --i; )
  829.       p[i] = 0;
  830.     fread(p, 48*1024, 1, fp);
  831.     for (j = 13, i = 48*1024; --i; j++) {
  832.       if (i % 1024 == 0) {
  833.     printf("\r%5u", i);
  834.     fflush(stdout);
  835.       }
  836.       if (p[i] != (char) j) {
  837.     printf("\r%5u failed %d instead of %d\n", i, p[i], UCHAR(j));
  838.     free(p);
  839.     return;
  840.       }
  841.     }
  842.     printf("\r%5u ok\n", i);
  843.     free(p);
  844.   }
  845. }
  846.  
  847. /*
  848.  * Test the exit code
  849.  *
  850.  * Load an exit handler and check buffer flushing.
  851.  */
  852. #ifndef _BSD
  853. static void handler()
  854.  
  855. {
  856.   fputs("Exit handler called ok\n", fp);
  857.   fflush(fp);
  858.   fputs("Buffer flush ok\n", fp);
  859.   sleep(2);
  860. }
  861.  
  862. void exit_test()
  863.  
  864. {
  865.   int atexit();
  866.  
  867.   puts("\nExit Test");
  868. #ifdef atarist
  869.   if ((fp = fopen(TTY, "wt")) == NULL) {
  870. #else
  871.   if ((fp = fopen(TTY, "w")) == NULL) {
  872. #endif
  873.     puts("Cannot open tty for exit test");
  874.     return;
  875.   }
  876.   setvbuf(fp, (char *) 0, _IOFBF, BUFSIZ);
  877.   if (atexit(handler) != 0)
  878.     puts("Exit handler not lodged");
  879. }
  880. #endif
  881.  
  882. /* Temporary File Test
  883.  *
  884.  * Check the names produced by tmpnam.
  885.  */
  886.  
  887. void tmp_test()
  888.  
  889. {
  890.   int i;
  891.   char buf[20];
  892.   char *tf;
  893.  
  894. #if 0 /* this is a silly test ++jrb */
  895.   puts("\nTemporary File Names");
  896.   for (i = 10; i--; ) {
  897.     tf = tmpnam((char *) 0);
  898.     fputs(tf, stdout);
  899.     if (strlen(tf) == L_tmpnam-1)
  900.       puts(" ok");
  901.     else
  902.       puts(" failed");
  903.   }
  904. #endif
  905.   if ((fp = tmpfile()) == 0) {
  906.     puts("Cannot make temporary file");
  907.     return;
  908.   }
  909.   printf("Temporary file");
  910.   fputs("123456", fp);
  911.   rewind(fp);
  912.   fgets(buf, 20, fp);
  913.   if (strcmp(buf, "123456") != 0)
  914.     puts(" failed");
  915.   else
  916.     puts(" ok");
  917. }
  918.  
  919. #ifndef atarist
  920. /* Id test
  921.  */
  922.  
  923. extern char *ctermid(), *cuserid();
  924. void id_test()
  925.  
  926. {
  927.   fputs("User id  : ", stdout);
  928.   puts(cuserid((char *) 0));
  929.   fputs("Terminal : ", stdout);
  930.   puts(ctermid((char *) 0));
  931. }
  932. #endif
  933.  
  934. int main()
  935.  
  936. {
  937. #ifndef atarist
  938.   id_test();
  939. #endif
  940.   lbw_test();
  941.   ubw_test();
  942.   bw_test();
  943.   fp_test();
  944.   sw_test();
  945.   sr_test();
  946.   frw_test();
  947.   fs_test();
  948.   fputs_test();
  949.   fprint_test();
  950.   gets_test();
  951.   fgets_test();
  952.   word_test();
  953.   fwrite_test();
  954.   a_test();
  955.   uwr_test();
  956.   uawr_test();
  957.   uwrwr_test();
  958.   tmp_test();
  959. #ifndef _BSD
  960.   exit_test();
  961. #endif
  962.   remove(TESTFILE);
  963.   return 0;
  964. }
  965.