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