test_geom.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*****************************************************************************
  2. *
  3. * MODULE: Grass PDE Numerical Library
  4. * AUTHOR(S): Soeren Gebbert, Berlin (GER) Dec 2006
  5. * soerengebbert <at> gmx <dot> de
  6. *
  7. * PURPOSE: Unit tests for geometry calculations
  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 <stdio.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <grass/N_pde.h>
  20. #include <grass/raster3d.h>
  21. #include "test_gpde_lib.h"
  22. /* prototypes */
  23. static int test_geom_data(void);
  24. /* ************************************************************************* */
  25. /* Performe the geom_data unit tests *************************************** */
  26. /* ************************************************************************* */
  27. int unit_test_geom_data(void)
  28. {
  29. int sum = 0;
  30. G_message("\n++ Running geom_data unit tests ++");
  31. sum += test_geom_data();
  32. if (sum > 0)
  33. G_warning("\n-- geom_data unit tests failure --");
  34. else
  35. G_message("\n-- geom_data unit tests finished successfully --");
  36. return sum;
  37. }
  38. /* ************************************************************************* */
  39. /* ************************************************************************* */
  40. /* ************************************************************************* */
  41. int test_geom_data(void)
  42. {
  43. struct Cell_head region2d;
  44. RASTER3D_Region region3d;
  45. N_geom_data *geom = NULL;
  46. int sum = 0, i;
  47. double area = 0;
  48. G_get_set_window(&region2d);
  49. /*Set the defaults */
  50. Rast3d_init_defaults();
  51. /*get the current region */
  52. Rast3d_get_window(&region3d);
  53. geom = N_alloc_geom_data();
  54. if (!geom) {
  55. G_warning("error in N_alloc_geom_data");
  56. return 1;
  57. }
  58. N_free_geom_data(geom);
  59. geom = NULL;
  60. /* ************ 2d region *************** */
  61. geom = N_init_geom_data_2d(&region2d, geom);
  62. if (!geom) {
  63. G_warning("error in N_init_geom_data_2d");
  64. return 2;
  65. }
  66. geom = N_init_geom_data_2d(&region2d, geom);
  67. if (!geom) {
  68. G_warning("error in N_init_geom_data_2d");
  69. return 3;
  70. }
  71. if (geom->dim != 2)
  72. sum++;
  73. if (geom->planimetric == 0 && geom->area == NULL)
  74. sum++;
  75. if (geom->planimetric == 1 && geom->area != NULL)
  76. sum++;
  77. /*get areas */
  78. area = 0.0;
  79. if (geom->planimetric == 0) {
  80. for (i = 0; i < geom->rows; i++)
  81. area += N_get_geom_data_area_of_cell(geom, i);
  82. if (area == 0) {
  83. G_warning("Wrong area calculation in N_init_geom_data_2d");
  84. sum++;
  85. }
  86. }
  87. area = 0.0;
  88. if (geom->planimetric == 1) {
  89. for (i = 0; i < geom->rows; i++)
  90. area += N_get_geom_data_area_of_cell(geom, i);
  91. if (area == 0) {
  92. G_warning("Wrong area calculation in N_get_geom_data_area_of_cell");
  93. sum++;
  94. }
  95. }
  96. N_free_geom_data(geom);
  97. geom = NULL;
  98. /* ************ 3d region *************** */
  99. geom = N_init_geom_data_3d(&region3d, geom);
  100. if (!geom) {
  101. G_warning("error in N_init_geom_data_3d");
  102. return 2;
  103. }
  104. geom = N_init_geom_data_3d(&region3d, geom);
  105. if (!geom) {
  106. G_warning("error in N_init_geom_data_3d");
  107. return 3;
  108. }
  109. if (geom->dim != 3)
  110. sum++;
  111. if (geom->planimetric == 0 && geom->area == NULL)
  112. sum++;
  113. if (geom->planimetric == 1 && geom->area != NULL)
  114. sum++;
  115. /*get areas */
  116. area = 0.0;
  117. if (geom->planimetric == 0) {
  118. for (i = 0; i < geom->rows; i++)
  119. area += N_get_geom_data_area_of_cell(geom, i);
  120. if (area == 0) {
  121. G_warning("Wrong area calculation in N_get_geom_data_area_of_cell");
  122. sum++;
  123. }
  124. }
  125. area = 0.0;
  126. if (geom->planimetric == 1) {
  127. for (i = 0; i < geom->rows; i++)
  128. area += N_get_geom_data_area_of_cell(geom, i);
  129. if (area == 0) {
  130. G_warning("Wrong area calculation in N_get_geom_data_area_of_cell");
  131. sum++;
  132. }
  133. }
  134. return sum;
  135. }