GRASS Programmer's Manual  6.4.2(2012)
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
test_geom.c
Go to the documentation of this file.
1 
2 /*****************************************************************************
3 *
4 * MODULE: Grass PDE Numerical Library
5 * AUTHOR(S): Soeren Gebbert, Berlin (GER) Dec 2006
6 * soerengebbert <at> gmx <dot> de
7 *
8 * PURPOSE: Unit tests for geometry calculations
9 *
10 * COPYRIGHT: (C) 2000 by the GRASS Development Team
11 *
12 * This program is free software under the GNU General Public
13 * License (>=v2). Read the file COPYING that comes with GRASS
14 * for details.
15 *
16 *****************************************************************************/
17 
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <grass/glocale.h>
22 #include <grass/N_pde.h>
23 #include <grass/G3d.h>
24 #include "test_gpde_lib.h"
25 
26 /* prototypes */
27 static int test_geom_data(void);
28 
29 /* ************************************************************************* */
30 /* Performe the geom_data unit tests *************************************** */
31 /* ************************************************************************* */
33 {
34  int sum = 0;
35 
36  G_message(_("\n++ Running geom_data unit tests ++"));
37 
38  sum += test_geom_data();
39 
40  if (sum > 0)
41  G_warning(_("\n-- geom_data unit tests failure --"));
42  else
43  G_message(_("\n-- geom_data unit tests finished successfully --"));
44 
45  return sum;
46 }
47 
48 
49 /* ************************************************************************* */
50 /* ************************************************************************* */
51 /* ************************************************************************* */
52 int test_geom_data(void)
53 {
54  struct Cell_head region2d;
55  G3D_Region region3d;
56  N_geom_data *geom = NULL;
57  int sum = 0, i;
58  double area = 0;
59 
60  G_get_set_window(&region2d);
61 
62  /*Set the defaults */
64 
65  /*get the current region */
66  G3d_getWindow(&region3d);
67 
68  geom = N_alloc_geom_data();
69  if (!geom) {
70  G_warning("error in N_alloc_geom_data");
71  return 1;
72  }
73  N_free_geom_data(geom);
74  geom = NULL;
75 
76  /* ************ 2d region *************** */
77  geom = N_init_geom_data_2d(&region2d, geom);
78  if (!geom) {
79  G_warning("error in N_init_geom_data_2d");
80  return 2;
81  }
82 
83  geom = N_init_geom_data_2d(&region2d, geom);
84  if (!geom) {
85  G_warning("error in N_init_geom_data_2d");
86  return 3;
87  }
88 
89  if (geom->dim != 2)
90  sum++;
91  if (geom->planimetric == 0 && geom->area == NULL)
92  sum++;
93  if (geom->planimetric == 1 && geom->area != NULL)
94  sum++;
95 
96  /*get areas */
97  area = 0.0;
98  if (geom->planimetric == 0) {
99  for (i = 0; i < geom->rows; i++)
100  area += N_get_geom_data_area_of_cell(geom, i);
101 
102  if (area == 0) {
103  G_warning("Wrong area calculation in N_init_geom_data_2d");
104  sum++;
105  }
106  }
107 
108  area = 0.0;
109  if (geom->planimetric == 1) {
110  for (i = 0; i < geom->rows; i++)
111  area += N_get_geom_data_area_of_cell(geom, i);
112 
113  if (area == 0) {
114  G_warning
115  ("Wrong area calculation in N_get_geom_data_area_of_cell");
116  sum++;
117  }
118  }
119 
120 
121  N_free_geom_data(geom);
122  geom = NULL;
123 
124  /* ************ 3d region *************** */
125  geom = N_init_geom_data_3d(&region3d, geom);
126  if (!geom) {
127  G_warning("error in N_init_geom_data_3d");
128  return 2;
129  }
130 
131  geom = N_init_geom_data_3d(&region3d, geom);
132  if (!geom) {
133  G_warning("error in N_init_geom_data_3d");
134  return 3;
135  }
136 
137  if (geom->dim != 3)
138  sum++;
139  if (geom->planimetric == 0 && geom->area == NULL)
140  sum++;
141 
142  if (geom->planimetric == 1 && geom->area != NULL)
143  sum++;
144 
145  /*get areas */
146  area = 0.0;
147  if (geom->planimetric == 0) {
148  for (i = 0; i < geom->rows; i++)
149  area += N_get_geom_data_area_of_cell(geom, i);
150 
151  if (area == 0) {
152  G_warning
153  ("Wrong area calculation in N_get_geom_data_area_of_cell");
154  sum++;
155  }
156  }
157 
158  area = 0.0;
159  if (geom->planimetric == 1) {
160  for (i = 0; i < geom->rows; i++)
161  area += N_get_geom_data_area_of_cell(geom, i);
162 
163  if (area == 0) {
164  G_warning
165  ("Wrong area calculation in N_get_geom_data_area_of_cell");
166  sum++;
167  }
168  }
169 
170  return sum;
171 
172 }