test.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. ****************************************************************************
  3. *
  4. * MODULE: Vector library
  5. *
  6. * AUTHOR(S): Original author CERL, probably Dave Gerdes.
  7. * Update to GRASS 5.7 Radim Blazek.
  8. *
  9. * PURPOSE: Test portable r/w functions
  10. *
  11. * COPYRIGHT: (C) 2001-2010 by the GRASS Development Team
  12. *
  13. * This program is free software under the GNU General Public
  14. * License (>=v2). Read the file COPYING that comes with GRASS
  15. * for details.
  16. *
  17. *****************************************************************************/
  18. #include <grass/config.h>
  19. #include <stdlib.h>
  20. #include <stdio.h>
  21. #include <sys/types.h>
  22. #include <grass/vector.h>
  23. #include <grass/glocale.h>
  24. /* Test portable r/w functions */
  25. #define D_TEST 1.3333
  26. #define L_TEST 123456789
  27. #define S_TEST 12345
  28. #define C_TEST 123
  29. int main()
  30. {
  31. int i, j;
  32. int err;
  33. int byte_order;
  34. struct Port_info port;
  35. struct gvfile fp;
  36. int port_off_t;
  37. double db, td[] = { -(PORT_DOUBLE_MAX), -(D_TEST), -(PORT_DOUBLE_MIN),
  38. 0, PORT_DOUBLE_MIN, D_TEST, PORT_DOUBLE_MAX
  39. };
  40. float fb, tf[] = { -(PORT_FLOAT_MAX), -(D_TEST), -(PORT_FLOAT_MIN),
  41. 0, PORT_FLOAT_MIN, D_TEST, PORT_FLOAT_MAX
  42. };
  43. long lb, tl[] = { PORT_LONG_MIN, -(L_TEST), 0, L_TEST, PORT_LONG_MAX };
  44. int ib, ti[] = { PORT_INT_MIN, -(L_TEST), 0, L_TEST, PORT_INT_MAX };
  45. short sb, ts[] = { PORT_SHORT_MIN, -(S_TEST), 0, S_TEST, PORT_SHORT_MAX };
  46. char cb, tc[] = { PORT_CHAR_MIN, -(C_TEST), 0, C_TEST, PORT_CHAR_MAX };
  47. off_t ob, to[] = { PORT_LONG_MIN, -(L_TEST), 0, L_TEST, PORT_LONG_MAX };
  48. err = EXIT_SUCCESS;
  49. port_off_t = sizeof(off_t);
  50. if (NULL == (fp.file = fopen("test.tmp", "wb+"))) {
  51. G_fatal_error(_("Unable to open test.tmp file"));
  52. }
  53. fp.loaded = 0;
  54. dig_set_cur_port(&port);
  55. byte_order = ENDIAN_LITTLE;
  56. for (i = 0; i < 2; i++) {
  57. dig_init_portable(&(port), byte_order);
  58. for (j = 0; j < 7; j++) {
  59. dig_fseek(&fp, 0, SEEK_CUR);
  60. fprintf(fp.file, "double ");
  61. dig__fwrite_port_D(&(td[j]), 1, &fp);
  62. dig_fseek(&fp, -(PORT_DOUBLE), SEEK_CUR);
  63. dig__fread_port_D(&db, 1, &fp);
  64. dig_fflush(&fp);
  65. if (db != td[j]) {
  66. G_warning(_("Error in read/write portable double, byte_order = %d"
  67. " Written: %.16e3E Read: %.16e3E"),
  68. byte_order, td[j], db);
  69. err = EXIT_FAILURE;
  70. }
  71. }
  72. for (j = 0; j < 7; j++) {
  73. dig_fseek(&fp, 0, SEEK_CUR);
  74. fprintf(fp.file, "float ");
  75. dig__fwrite_port_F(&(tf[j]), 1, &fp);
  76. dig_fseek(&fp, -(PORT_FLOAT), SEEK_CUR);
  77. dig__fread_port_F(&fb, 1, &fp);
  78. dig_fflush(&fp);
  79. if (fb != tf[j]) {
  80. G_warning(_("Error in read/write portable float, byte_order = %d"
  81. " Written: %.8e3E Read: %.8e3E"),
  82. byte_order, tf[j], fb);
  83. err = EXIT_FAILURE;
  84. }
  85. }
  86. for (j = 0; j < 5; j++) {
  87. dig_fseek(&fp, 0, SEEK_CUR);
  88. fprintf(fp.file, "off_t ");
  89. dig__fwrite_port_O(&(to[j]), 1, &fp, port_off_t);
  90. dig_fseek(&fp, -(port_off_t), SEEK_CUR);
  91. dig__fread_port_O(&ob, 1, &fp, port_off_t);
  92. dig_fflush(&fp);
  93. if (ob != to[j]) {
  94. G_warning(_("Error in read/write portable off_t, byte_order = %d"
  95. " Written: %lu Read: %lu"),
  96. byte_order, (long unsigned) to[j], (long unsigned) ob);
  97. err = EXIT_FAILURE;
  98. }
  99. }
  100. for (j = 0; j < 5; j++) {
  101. dig_fseek(&fp, 0, SEEK_CUR);
  102. fprintf(fp.file, "long ");
  103. dig__fwrite_port_L(&(tl[j]), 1, &fp);
  104. dig_fseek(&fp, -(PORT_LONG), SEEK_CUR);
  105. dig__fread_port_L(&lb, 1, &fp);
  106. dig_fflush(&fp);
  107. if (lb != tl[j]) {
  108. G_warning(_("Error in read/write portable long, byte_order = %d"
  109. " Written: %lu Read: %lu"),
  110. byte_order, (long unsigned) tl[j], (long unsigned) lb);
  111. err = EXIT_FAILURE;
  112. }
  113. }
  114. for (j = 0; j < 5; j++) {
  115. dig_fseek(&fp, 0, SEEK_CUR);
  116. fprintf(fp.file, "int ");
  117. dig__fwrite_port_I(&(ti[j]), 1, &fp);
  118. dig_fseek(&fp, -(PORT_INT), SEEK_CUR);
  119. dig__fread_port_I(&ib, 1, &fp);
  120. dig_fflush(&fp);
  121. if (ib != ti[j]) {
  122. G_warning(_("Error in read/write portable int, byte_order = %d"
  123. " Written: %d Read: %d"),
  124. byte_order, ti[j], ib);
  125. err = EXIT_FAILURE;
  126. }
  127. }
  128. for (j = 0; j < 5; j++) {
  129. dig_fseek(&fp, 0, SEEK_CUR);
  130. fprintf(fp.file, "short ");
  131. dig__fwrite_port_S(&(ts[j]), 1, &fp);
  132. dig_fseek(&fp, -(PORT_SHORT), SEEK_CUR);
  133. dig__fread_port_S(&sb, 1, &fp);
  134. dig_fflush(&fp);
  135. if (sb != ts[j]) {
  136. G_warning(_("Error in read/write portable short, byte_order = %d"
  137. " Written: %d Read: %d"),
  138. byte_order, ts[j], sb);
  139. err = EXIT_FAILURE;
  140. }
  141. }
  142. for (j = 0; j < 5; j++) {
  143. dig_fseek(&fp, 0, SEEK_CUR);
  144. fprintf(fp.file, "char ");
  145. dig__fwrite_port_C(&(tc[j]), 1, &fp);
  146. dig_fseek(&fp, -(PORT_CHAR), SEEK_CUR);
  147. dig__fread_port_C(&cb, 1, &fp);
  148. dig_fflush(&fp);
  149. if (cb != tc[j]) {
  150. G_warning(_("Error in read/write portable char, byte_order = %d"
  151. " Written: %d Read: %d"),
  152. byte_order, tc[j], cb);
  153. err = EXIT_FAILURE;
  154. }
  155. }
  156. byte_order = ENDIAN_BIG;
  157. }
  158. fclose(fp.file);
  159. exit(err);
  160. }