test_put_get_value_large_file.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. /*****************************************************************************
  2. *
  3. * MODULE: Grass raster3d Library
  4. * AUTHOR(S): Soeren Gebbert, Braunschweig (GER) Jun 2011
  5. * soerengebbert <at> googlemail <dot> com
  6. *
  7. * PURPOSE: Unit and Integration tests
  8. *
  9. * COPYRIGHT: (C) 2000 by the GRASS Development Team
  10. *
  11. * This program is free software under the GNU General Public
  12. * License (>=v2). Read the file COPYING that comes with GRASS
  13. * for details.
  14. *
  15. *****************************************************************************/
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <math.h>
  19. #include "test_raster3d_lib.h"
  20. #include "grass/interpf.h"
  21. #define EPSILON 0.000000001
  22. #define RAND_VALUE_VECTOR_SIZE 1000
  23. static int test_large_file(int depths, int rows, int cols, int tile_size);
  24. static int test_large_file_zeros(int depths, int rows, int cols, int tile_size);
  25. static int test_large_file_random(int depths, int rows, int cols, int tile_size);
  26. static int test_large_file_sparse_random(int depths, int rows, int cols, int tile_size);
  27. /* *************************************************************** */
  28. /* Perform the coordinate transformation tests ****************** */
  29. /* *************************************************************** */
  30. int unit_test_put_get_value_large_file(int depths, int rows, int cols, int tile_size)
  31. {
  32. int sum = 0;
  33. G_message("\n++ Running raster3d put/get value large file unit tests ++");
  34. sum += test_large_file(depths, rows, cols, tile_size);
  35. sum += test_large_file_random(depths, rows, cols, tile_size);
  36. sum += test_large_file_sparse_random(depths, rows, cols, tile_size);
  37. sum += test_large_file(depths, rows, cols, tile_size);
  38. sum += test_large_file_zeros(depths, rows, cols, tile_size);
  39. sum += test_large_file(depths, rows, cols, tile_size);
  40. if (sum > 0)
  41. G_warning("\n-- raster3d put/get value large file unit tests failure --");
  42. else
  43. G_message("\n-- raster3d put/get value large file unit tests finished successfully --");
  44. return sum;
  45. }
  46. /* *************************************************************** */
  47. int test_large_file(int depths, int rows, int cols, int tile_size)
  48. {
  49. int sum = 0;
  50. int x, y, z, count;
  51. DCELL value;
  52. G_message("Testing DCELL put function for large files");
  53. RASTER3D_Region region;
  54. RASTER3D_Map *map = NULL;
  55. /* We need to set up a specific region for the new raster3d map.
  56. * First we safe the default region. */
  57. Rast3d_get_window(&region);
  58. region.bottom = -365.5;
  59. region.top = 365.5;
  60. region.south = -90;
  61. region.north = 90;
  62. region.west = -180;
  63. region.east = 180;
  64. region.rows = rows;
  65. region.cols = cols;
  66. region.depths = depths;
  67. Rast3d_adjust_region(&region);
  68. G_message("Creating 3D raster map");
  69. map = Rast3d_open_new_opt_tile_size("test_put_get_value_dcell_large",
  70. RASTER3D_USE_CACHE_XY, &region, DCELL_TYPE, tile_size);
  71. /* The window is the same as the map region ... of course */
  72. Rast3d_set_window_map(map, &region);
  73. /*Write -1 first to see if the tile handling works correctly */
  74. for(z = 0; z < region.depths; z++) {
  75. G_percent(z, region.depths, 1);
  76. for(y = 0; y < region.rows; y++) {
  77. for(x = 0; x < region.cols; x++) {
  78. /* Put the counter as cell value */
  79. value = -1;
  80. Rast3d_put_value(map, x, y, z, &value, DCELL_TYPE);
  81. }
  82. }
  83. }
  84. G_percent(1, 1, 1);
  85. G_message("Rewriting the values");
  86. /* Now write the values to be evaluated */
  87. count = 1;
  88. for(z = 0; z < region.depths; z++) {
  89. G_percent(z, region.depths, 1);
  90. for(y = 0; y < region.rows; y++) {
  91. for(x = 0; x < region.cols; x++) {
  92. /* Put the counter as cell value */
  93. value = count;
  94. Rast3d_put_value(map, x, y, z, &value, DCELL_TYPE);
  95. count++;
  96. }
  97. }
  98. }
  99. G_percent(1, 1, 1);
  100. G_message("Verifying 3D raster map");
  101. count = 1;
  102. for(z = 0; z < region.depths; z++) {
  103. G_percent(z, region.depths, 1);
  104. for(y = 0; y < region.rows; y++) {
  105. for(x = 0; x < region.cols; x++) {
  106. /* Check the counter as cell value */
  107. Rast3d_get_value(map, x, y, z, &value, DCELL_TYPE);
  108. if(fabs(value - (double)(count)) > EPSILON) {
  109. G_message("At: z %i y %i x %i -- value %.14lf != %.14lf\n",
  110. z, y, x, value, (double)(count));
  111. sum++;
  112. }
  113. count++;
  114. }
  115. }
  116. }
  117. G_percent(1, 1, 1);
  118. Rast3d_close(map);
  119. G_remove("grid3", "test_put_get_value_dcell_large");
  120. return sum;
  121. }
  122. /* *************************************************************** */
  123. int test_large_file_zeros(int depths, int rows, int cols, int tile_size)
  124. {
  125. int sum = 0;
  126. int x, y, z;
  127. DCELL value;
  128. G_message("Testing DCELL put function for large files filled with zeros");
  129. RASTER3D_Region region;
  130. RASTER3D_Map *map = NULL;
  131. /* We need to set up a specific region for the new raster3d map.
  132. * First we safe the default region. */
  133. Rast3d_get_window(&region);
  134. region.bottom = -365.5;
  135. region.top = 365.5;
  136. region.south = -90;
  137. region.north = 90;
  138. region.west = -180;
  139. region.east = 180;
  140. region.rows = rows;
  141. region.cols = cols;
  142. region.depths = depths;
  143. Rast3d_adjust_region(&region);
  144. G_message("Creating 3D raster map filled with zeros");
  145. map = Rast3d_open_new_opt_tile_size("test_put_get_value_dcell_large_zeros",
  146. RASTER3D_USE_CACHE_XY, &region, DCELL_TYPE, tile_size);
  147. /* The window is the same as the map region ... of course */
  148. Rast3d_set_window_map(map, &region);
  149. for(z = 0; z < region.depths; z++) {
  150. G_percent(z, region.depths, 1);
  151. for(y = 0; y < region.rows; y++) {
  152. for(x = 0; x < region.cols; x++) {
  153. /* Put the counter as cell value */
  154. value = 0.0;
  155. Rast3d_put_value(map, x, y, z, &value, DCELL_TYPE);
  156. }
  157. }
  158. }
  159. G_percent(1, 1, 1);
  160. /* Write everything to the disk */
  161. Rast3d_flush_all_tiles(map);
  162. Rast3d_close(map);
  163. G_message("Verifying 3D raster map filled with zeros");
  164. map = Rast3d_open_cell_old("test_put_get_value_dcell_large_zeros",
  165. G_mapset(), &region, DCELL_TYPE, RASTER3D_USE_CACHE_XY);
  166. for(z = 0; z < region.depths; z++) {
  167. G_percent(z, region.depths, 1);
  168. for(y = 0; y < region.rows; y++) {
  169. for(x = 0; x < region.cols; x++) {
  170. /* Check the counter as cell value */
  171. Rast3d_get_value(map, x, y, z, &value, DCELL_TYPE);
  172. if(value > EPSILON) {
  173. G_message("At: z %i y %i x %i -- value %.14lf != 0.0\n",
  174. z, y, x, value);
  175. sum++;
  176. }
  177. }
  178. }
  179. }
  180. G_percent(1, 1, 1);
  181. Rast3d_close(map);
  182. G_remove("grid3", "test_put_get_value_dcell_large_zeros");
  183. return sum;
  184. }
  185. /* *************************************************************** */
  186. int test_large_file_random(int depths, int rows, int cols, int tile_size)
  187. {
  188. int sum = 0;
  189. int x, y, z, i;
  190. DCELL value, random_value;
  191. DCELL *random_value_vector = G_calloc(RAND_VALUE_VECTOR_SIZE, sizeof(DCELL));
  192. G_message("Testing DCELL put function for large files filled with random values");
  193. RASTER3D_Region region;
  194. RASTER3D_Map *map = NULL;
  195. /* We need to set up a specific region for the new raster3d map.
  196. * First we safe the default region. */
  197. Rast3d_get_window(&region);
  198. region.bottom = -365.5;
  199. region.top = 365.5;
  200. region.south = -90;
  201. region.north = 90;
  202. region.west = -180;
  203. region.east = 180;
  204. region.rows = rows;
  205. region.cols = cols;
  206. region.depths = depths;
  207. Rast3d_adjust_region(&region);
  208. G_message("Creating 3D raster map filled with random values");
  209. map = Rast3d_open_new_opt_tile_size("test_put_get_value_dcell_large_random",
  210. RASTER3D_USE_CACHE_XY, &region, DCELL_TYPE, tile_size);
  211. /* The window is the same as the map region ... of course */
  212. Rast3d_set_window_map(map, &region);
  213. srand(1);
  214. /* We fill the random value vector */
  215. for(i = 0; i < RAND_VALUE_VECTOR_SIZE; i++) {
  216. random_value_vector[i] = (DCELL)rand();
  217. }
  218. i = 0;
  219. for(z = 0; z < region.depths; z++) {
  220. G_percent(z, region.depths, 1);
  221. for(y = 0; y < region.rows; y++) {
  222. for(x = 0; x < region.cols; x++) {
  223. /* Put the counter as cell value */
  224. value = random_value_vector[i];
  225. Rast3d_put_value(map, x, y, z, &value, DCELL_TYPE);
  226. i++;
  227. if(i == RAND_VALUE_VECTOR_SIZE)
  228. i = 0;
  229. }
  230. }
  231. }
  232. G_percent(1, 1, 1);
  233. /* Write everything to the disk */
  234. Rast3d_flush_all_tiles(map);
  235. Rast3d_close(map);
  236. G_message("Verifying 3D raster map filled with random values");
  237. map = Rast3d_open_cell_old("test_put_get_value_dcell_large_random",
  238. G_mapset(), &region, DCELL_TYPE, RASTER3D_USE_CACHE_XY);
  239. i = 0;
  240. for(z = 0; z < region.depths; z++) {
  241. G_percent(z, region.depths, 1);
  242. for(y = 0; y < region.rows; y++) {
  243. for(x = 0; x < region.cols; x++) {
  244. /* Check the counter as cell value */
  245. Rast3d_get_value(map, x, y, z, &value, DCELL_TYPE);
  246. random_value = random_value_vector[i];
  247. if(fabs(value - random_value) > EPSILON) {
  248. G_message("At: z %i y %i x %i -- value %.14lf != %.14lf\n",
  249. z, y, x, value, random_value);
  250. sum++;
  251. }
  252. i++;
  253. if(i == RAND_VALUE_VECTOR_SIZE)
  254. i = 0;
  255. }
  256. }
  257. }
  258. G_percent(1, 1, 1);
  259. Rast3d_close(map);
  260. G_free(random_value_vector);
  261. G_remove("grid3", "test_put_get_value_dcell_large_random");
  262. return sum;
  263. }
  264. /* *************************************************************** */
  265. int test_large_file_sparse_random(int depths, int rows, int cols, int tile_size)
  266. {
  267. int sum = 0;
  268. int x, y, z, i;
  269. DCELL value, random_value;
  270. DCELL *random_value_vector = G_calloc(RAND_VALUE_VECTOR_SIZE, sizeof(DCELL));
  271. G_message("Testing DCELL put function for large files filled with sparse random values");
  272. RASTER3D_Region region;
  273. RASTER3D_Map *map = NULL;
  274. /* We need to set up a specific region for the new raster3d map.
  275. * First we safe the default region. */
  276. Rast3d_get_window(&region);
  277. region.bottom = -365.5;
  278. region.top = 365.5;
  279. region.south = -90;
  280. region.north = 90;
  281. region.west = -180;
  282. region.east = 180;
  283. region.rows = rows;
  284. region.cols = cols;
  285. region.depths = depths;
  286. Rast3d_adjust_region(&region);
  287. G_message("Creating 3D raster map filled with sparse random values");
  288. map = Rast3d_open_new_opt_tile_size("test_put_get_value_dcell_large_sparse_random",
  289. RASTER3D_USE_CACHE_XY, &region, DCELL_TYPE, tile_size);
  290. /* The window is the same as the map region ... of course */
  291. Rast3d_set_window_map(map, &region);
  292. srand(1);
  293. /* We fill the random value vector */
  294. for(i = 0; i < RAND_VALUE_VECTOR_SIZE; i++) {
  295. /* Put the counter as cell value */
  296. value = (DCELL)rand();
  297. value /= RAND_MAX;
  298. if(value <= 0.7)
  299. value = 0.0;
  300. else if(value <= 0.8)
  301. value = 1.0;
  302. else if(value <= 0.9)
  303. value = 2.0;
  304. else if(value <= 1.0)
  305. value = 3.0;
  306. else
  307. value = 4.0;
  308. random_value_vector[i] = value;
  309. }
  310. i = 0;
  311. for(z = 0; z < region.depths; z++) {
  312. G_percent(z, region.depths, 1);
  313. for(y = 0; y < region.rows; y++) {
  314. for(x = 0; x < region.cols; x++) {
  315. /* Put the counter as cell value */
  316. value = random_value_vector[i];
  317. Rast3d_put_value(map, x, y, z, &value, DCELL_TYPE);
  318. i++;
  319. if(i == RAND_VALUE_VECTOR_SIZE)
  320. i = 0;
  321. }
  322. }
  323. }
  324. G_percent(1, 1, 1);
  325. /* Write everything to the disk */
  326. Rast3d_flush_all_tiles(map);
  327. Rast3d_close(map);
  328. G_message("Verifying 3D raster map filled with sparse random values");
  329. map = Rast3d_open_cell_old("test_put_get_value_dcell_large_sparse_random",
  330. G_mapset(), &region, DCELL_TYPE, RASTER3D_USE_CACHE_XY);
  331. i = 0;
  332. for(z = 0; z < region.depths; z++) {
  333. G_percent(z, region.depths, 1);
  334. for(y = 0; y < region.rows; y++) {
  335. for(x = 0; x < region.cols; x++) {
  336. /* Check the counter as cell value */
  337. Rast3d_get_value(map, x, y, z, &value, DCELL_TYPE);
  338. if(fabs(value - random_value_vector[i]) > EPSILON) {
  339. G_message("At: z %i y %i x %i -- value %.14lf != %.14lf\n",
  340. z, y, x, value, random_value);
  341. sum++;
  342. }
  343. i++;
  344. if(i == RAND_VALUE_VECTOR_SIZE)
  345. i = 0;
  346. }
  347. }
  348. }
  349. G_percent(1, 1, 1);
  350. Rast3d_close(map);
  351. G_free(random_value_vector);
  352. G_remove("grid3", "test_put_get_value_dcell_large_sparse_random");
  353. return sum;
  354. }