test_put_get_value_large_file.c 13 KB

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