home *** CD-ROM | disk | FTP | other *** search
/ Photo CD Demo 1 / Demo.bin / hdf / unix / hdf3_2r2 / test / thfile.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-10-30  |  7.8 KB  |  274 lines

  1. /***************************************************************************
  2. *
  3. *
  4. *                         NCSA HDF version 3.2r2
  5. *                            October 30, 1992
  6. *
  7. * NCSA HDF Version 3.2 source code and documentation are in the public
  8. * domain.  Specifically, we give to the public domain all rights for future
  9. * licensing of the source code, all resale rights, and all publishing rights.
  10. *
  11. * We ask, but do not require, that the following message be included in all
  12. * derived works:
  13. *
  14. * Portions developed at the National Center for Supercomputing Applications at
  15. * the University of Illinois at Urbana-Champaign, in collaboration with the
  16. * Information Technology Institute of Singapore.
  17. *
  18. * THE UNIVERSITY OF ILLINOIS GIVES NO WARRANTY, EXPRESSED OR IMPLIED, FOR THE
  19. * SOFTWARE AND/OR DOCUMENTATION PROVIDED, INCLUDING, WITHOUT LIMITATION,
  20. * WARRANTY OF MERCHANTABILITY AND WARRANTY OF FITNESS FOR A PARTICULAR PURPOSE
  21. *
  22. ****************************************************************************
  23. */
  24.  
  25. #ifdef RCSID
  26. static char RcsId[] = "@(#)$Revision: 1.8 $";
  27. #endif
  28. /*
  29. $Header: /hdf/hdf/v3.2r2/test/RCS/thfile.c,v 1.8 1992/10/30 19:05:24 koziol beta koziol $
  30.  
  31. $Log: thfile.c,v $
  32.  * Revision 1.8  1992/10/30  19:05:24  koziol
  33.  * Added a "const char *" to strcmp calls to make the SGI ANSI compiler hapy
  34.  *
  35.  * Revision 1.7  1992/07/16  19:34:08  mlivin
  36.  * changed re-opening of file to NOT include DFACC_CREATE
  37.  *
  38.  * Revision 1.6  1992/06/26  20:23:20  mlivin
  39.  * added in tests for Hishdf - open HDF, closed HDF, non-HDF, non-existing
  40.  *
  41.  * Revision 1.5  1992/06/22  23:04:42  chouck
  42.  * Removed calls to fork()
  43.  *
  44.  * Revision 1.4  1992/06/01  19:23:47  chouck
  45.  * Cleaned up output
  46.  *
  47.  * Revision 1.3  1992/05/31  15:23:30  mfolk
  48.  * Added uint8 * and uint16 casts to make Convex stop complaining.
  49.  *
  50.  * Revision 1.2  1992/05/28  14:24:01  chouck
  51.  * Added casts for calls to Hinquire()
  52.  *
  53.  * Revision 1.1  1992/02/10  20:59:34  chouck
  54.  * Initial revision
  55.  *
  56. */
  57. /*
  58.  
  59. * Hopen
  60. ** Create a file.
  61. ** Open an existing file.
  62. *** Normally.
  63. *** Read-only file with DFACC_WRITE.
  64. ** Open non-existent file.
  65. *** With DFACC_READ.
  66. *** With DFACC_WRITE.
  67. ** Create an illegal file.
  68. ** Open the same file twice.
  69. *** First with DFACC_WRITE then with DFACC_READ.
  70. *** First with DFACC_WRITE then with DFACC_WRITE.
  71. *** First with DFACC_READ and then with DFACC_WRITE.
  72. *** First with DFACC_<any> and then with DFACC_CREATE.
  73. ** Open more files than there is slots.
  74.  
  75. * Hclose
  76. ** Close a proper file.
  77. ** Close with an illegal file id.
  78. *** Random file id.
  79. *** Correct tag but bad slot.
  80.  
  81. * Hstartread
  82. ** Normal.
  83. ** With illegal file id.
  84. ** With illegal tag/ref.
  85. ** With wildcard.
  86. ** Open more access elements than there is space.
  87.  
  88. */
  89.  
  90. #include "hdf.h"
  91. #define TESTFILE_NAME "t.hdf"
  92. uint8 outbuf[4096], inbuf[4096];
  93.  
  94.  
  95. #define CHECK(ret, val, where) \
  96. {if(ret == val) { fprintf(stderr, "%s failed, line %d, code %d\n", \
  97.                           where, __LINE__, ret); \
  98.                   HEprint(stderr, 0); exit(1);}}
  99.  
  100. int main(argc, argv)
  101.     int argc;
  102.     char *argv[];
  103. {
  104.     int32 fid, fid1;
  105.     int32 aid1, aid2;
  106.     int32 fileid, length, offset, posn;
  107.     uint16 tag, ref;
  108.     int16 access, special;
  109.     int ret, i;
  110.     intn errors = 0;
  111.     bool ret_bool;
  112.  
  113.     for (i=0; i<4096; i++) outbuf[i] = (char) (i % 256);
  114.  
  115.     printf("Creating a file %s\n\n", TESTFILE_NAME);
  116.     fid = Hopen(TESTFILE_NAME, DFACC_CREATE, 0);
  117.     CHECK(fid, FAIL, "Hopen");
  118.  
  119.     ret_bool = Hishdf(TESTFILE_NAME);
  120.     CHECK(ret_bool, FALSE, "Hishdf");
  121.  
  122.     ret = Hnewref(fid);
  123.     CHECK(ret, FAIL, "Hnewref");
  124.  
  125.     printf("Reading / Writing to file\n");
  126.     ret = Hputelement(fid, (uint16) 100, 1, 
  127.                       (uint8 *) "testing 100 1", strlen("testing 100 1")+1);
  128.     CHECK(ret, FAIL, "Hputelement");
  129.  
  130.     ret = Hputelement(fid, (uint16) 100, (uint16) 4, outbuf, 2000);
  131.     CHECK(ret, FAIL, "Hputelement");
  132.  
  133.     ret = Hnewref(fid);
  134.     CHECK(ret, FAIL, "Hnewref");
  135.  
  136.     ret = Hputelement(fid, (uint16) 103, (uint16) 2, 
  137.                       (uint8 *) "element 103 2", strlen("element 103 2")+1);
  138.     CHECK(ret, FAIL, "Hputlement");
  139.  
  140.     ret = Hgetelement(fid, (uint16) 100, (uint16) 4, inbuf);
  141.     if(ret != 2000) {
  142.       fprintf(stderr, "Hgetelement returned wrong count: %d\n", ret);
  143.       errors++;
  144.     }
  145.  
  146.     for (i=0; i<ret; i++) {
  147.        if (inbuf[i] != outbuf[i])
  148.            printf("Wrong data at %d, out %d in %d\n", i, outbuf[i], inbuf[i]);
  149.        inbuf[i] = '\0';
  150.     }
  151.  
  152.     ret = Hputelement(fid, 102, 2, outbuf, 4096);
  153.     CHECK(ret, FAIL, "Hputlement");
  154.  
  155.     ret = Hclose(fid);
  156.     CHECK(ret, FAIL, "Hclose");
  157.  
  158.     printf("\nClosing and re-opening file %s\n\n", TESTFILE_NAME);
  159.     fid = Hopen(TESTFILE_NAME, DFACC_RDWR, 0);
  160.     CHECK(fid, FAIL, "Hopen");
  161.  
  162.     ret = Hnewref(fid);
  163.     CHECK(ret, FAIL, "Hnewref");
  164.  
  165.     aid1 = Hstartread(fid, 100, 1);
  166.     CHECK(aid1, FAIL, "Hstartread");
  167.  
  168.     ret = Hinquire(aid1, &fileid, &tag, &ref, &length, &offset, &posn,
  169.                   &access, &special);
  170.     CHECK(ret, FAIL, "Hinquire");
  171.  
  172.     printf("Verifying data\n\n");
  173.     ret = Hread(aid1, length, inbuf);
  174.     if(ret != 14) {
  175.       fprintf(stderr, "ERROR: Hread returned the wrong length: %d\n", ret);
  176.       errors++;
  177.     }
  178.  
  179.     if(strcmp((const char *)inbuf, "testing 100 1")) {
  180.       fprintf(stderr, "ERROR: Hread returned the wrong data\n");
  181.       fprintf(stderr, "\t       Is: %s\n", inbuf);
  182.       fprintf(stderr, "\tShould be: testing 100 1\n");
  183.       errors++;
  184.     }
  185.  
  186.     ret = Hnewref(fid);
  187.     CHECK(ret, FAIL, "Hnewref");
  188.  
  189.     printf("Testing a number of searching schemes\n\n");
  190.     ret = Hnextread(aid1, 100, DFREF_WILDCARD, DF_CURRENT);
  191.     CHECK(ret, FAIL, "Hnextread");
  192.  
  193.     ret = Hinquire(aid1, &fileid, &tag, &ref, &length, &offset, &posn,
  194.                   &access, &special);
  195.     CHECK(ret, FAIL, "Hinquire");
  196.  
  197.     ret = Hnextread(aid1, 100, DFREF_WILDCARD, DF_CURRENT);
  198.     if(ret != FAIL) {
  199.       fprintf(stderr, "ERROR: Found a non-existant element at line %d\n", 
  200.               __LINE__);
  201.       errors++;
  202.     }
  203.  
  204.     ret = Hnextread(aid1, DFTAG_WILDCARD, DFREF_WILDCARD, DF_START);
  205.     CHECK(ret, FAIL, "Hnextread");
  206.  
  207.     ret = Hinquire(aid1, &fileid, &tag, &ref, &length, &offset, &posn,
  208.                   &access, &special);
  209.     CHECK(ret, FAIL, "Hinquire");
  210.  
  211.     ret = Hnextread(aid1, DFTAG_WILDCARD, 3, DF_CURRENT);
  212.     if(ret != FAIL) {
  213.       fprintf(stderr, "ERROR: Found a non-existant element at line %d\n", 
  214.               __LINE__);
  215.       errors++;
  216.     }
  217.  
  218.     ret = Hnextread(aid1, DFTAG_WILDCARD, 2, DF_CURRENT);
  219.     CHECK(ret, FAIL, "Hnextread");
  220.  
  221.     ret = Hinquire(aid1, &fileid, &tag, &ref, &length, &offset, &posn,
  222.                   &access, &special);
  223.     CHECK(ret, FAIL, "Hinquire");
  224.  
  225.     aid2 = Hstartwrite(fid, 100, 1, 4);
  226.     if(aid2 == FAIL) {
  227.       fprintf(stderr, "ERROR: was not allowed to startwrite on existing object\n");
  228.       errors++;
  229.     }
  230.  
  231.     ret = Hwrite(aid1, 4, (uint8 *) "ABCD");
  232.     if(ret != FAIL) {
  233.       fprintf(stderr, "ERROR: was allowed to write to read access object\n");
  234.       errors++;
  235.     }
  236.  
  237.     ret = Hendaccess(aid1);
  238.     CHECK(ret, FAIL, "Hendaccess");
  239.  
  240.     ret = Hendaccess(aid2);
  241.     CHECK(ret, FAIL, "Hendaccess");
  242.     
  243.     printf("Attempting to gain multiple access to file (is allowed)\n");
  244.     fid1 = Hopen(TESTFILE_NAME, DFACC_READ, 0);
  245.     if(fid1 == FAIL) {
  246.       fprintf(stderr, "ERROR: Failed to have two concurrent access to file\n");
  247.       errors++;
  248.     }
  249.  
  250.     ret = Hnewref(fid1);
  251.     CHECK(ret, FAIL, "Hnewref");
  252.  
  253.     ret = Hclose(fid);
  254.     CHECK(ret, FAIL, "Hclose");
  255.  
  256.     ret = Hclose(fid1);
  257.     CHECK(ret, FAIL, "Hclose");
  258.  
  259.     ret_bool = Hishdf(TESTFILE_NAME);
  260.     CHECK(ret_bool, FALSE, "Hishdf");
  261.  
  262.     ret_bool = Hishdf(__FILE__);
  263.     CHECK(ret_bool, TRUE, "Hishdf");
  264.  
  265.     ret_bool = Hishdf("qqqqqqqq.qqq");  /* I sure hope it isn't there */
  266.     CHECK(ret, TRUE, "Hishdf");
  267.  
  268.     if(errors) 
  269.       fprintf(stderr, "\n\t>>> %d errors were encountered <<<\n\n");
  270.     else
  271.       fprintf(stderr, "\n\t>>> All tests passed <<< \n\n");
  272.  
  273. }
  274.