test.c 5.1 KB

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