test_put_get_value_large_file.c 13 KB

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