xboa
Testcore.c
Go to the documentation of this file.
1 //This file is a part of xboa
2 //
3 //xboa is free software: you can redistribute it and/or modify
4 //it under the terms of the GNU General Public License as published by
5 //the Free Software Foundation, either version 3 of the License, or
6 //(at your option) any later version.
7 //
8 //xboa is distributed in the hope that it will be useful,
9 //but WITHOUT ANY WARRANTY; without even the implied warranty of
10 //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 //GNU General Public License for more details.
12 //
13 //You should have received a copy of the GNU General Public License
14 //along with xboa in the doc folder. If not, see
15 //<http://www.gnu.org/licenses/>.
16 //
17 
18 /*
19 Testcore.c
20 
21 Excercise Hitcore functions in order to find stinking memory leak (in
22 combination with valgrind).
23 */
24 
25 #include <Python.h>
26 #include "Hitcore.c"
27 
28 void test(int test_condition, char* function_tested, char* test_name) {
29  printf("%s %s", function_tested, test_name);
30  if (!test_condition) {
31  puts(" fail\n");
32  exit(1);
33  }
34  printf(" pass\n");
35 }
36 
37 //typedef struct {void* data; size_t data_length; size_t allocd_length; size_t object_size; } vector;
38 
39 //static vector* vector_alloc (size_t object_size, int n_elements);
41  char* function_tested = "vector_alloc";
42  vector* vec_zero = vector_alloc(0, 1);
43  test(vec_zero == NULL, function_tested, "0 size object");
44 
45  vector* vec_no_data = vector_alloc(2, 0);
46  test(vec_no_data != NULL, function_tested, "0 length vector alloc");
47  test(vec_no_data->data != NULL, function_tested, "0 length vector data");
48  test(vec_no_data->data_length == 0, function_tested, "0 length vector data_length");
49  test(vec_no_data->allocd_length == 1, function_tested, "0 length vector allocd_length");
50  test(vec_no_data->object_size == 2, function_tested, "0 length vector object_size");
51  vector_free(vec_no_data);
52 
53  vector* vec_normal = vector_alloc(2, 3);
54  test(vec_normal != NULL, function_tested, "Standard vector alloc");
55  test(vec_normal->data != NULL, function_tested, "Standard vector data");
56  test(vec_normal->data_length == 6, function_tested, "Standard vector data_length");
57  test(vec_normal->allocd_length == 7, function_tested, "Standard vector allocd_length");
58  test(vec_normal->object_size == 2, function_tested, "Standard vector object_size");
59  vector_free(vec_normal);
60 }
61 
63  char* function_tested = "vector_realloc";
64  vector* vec_normal = vector_alloc(2, 3);
65  vector* vec_no_data = vector_alloc(2, 0);
66 
67  vector_realloc(vec_normal, 1); //try making it shorter
68  test(vec_normal != NULL, function_tested, "Normal vector");
69  test(vec_normal->data != NULL, function_tested, "Normal vector data");
70  test(vec_normal->data_length == 2, function_tested, "Normal vector data_length");
71  test(vec_normal->allocd_length == 3, function_tested, "Normal vector allocd_length");
72  test(vec_normal->object_size == 2, function_tested, "Normal vector object_size");
73 
74  vector_realloc(vec_normal, 5); //try making it longer
75  test(vec_normal != NULL, function_tested, "Normal vector");
76  test(vec_normal->data != NULL, function_tested, "Normal vector data");
77  test(vec_normal->data_length == 10, function_tested, "Normal vector data_length");
78  test(vec_normal->allocd_length == 11, function_tested, "Normal vector allocd_length");
79  test(vec_normal->object_size == 2, function_tested, "Normal vector object_size");
80  vector_free(vec_normal);
81 
82  vector_realloc(vec_no_data, 5); //try making it longer
83  test(vec_no_data != NULL, function_tested, "no_data vector");
84  test(vec_no_data->data != NULL, function_tested, "no_data vector data");
85  test(vec_no_data->data_length == 10, function_tested, "no_data vector data_length");
86  test(vec_no_data->allocd_length == 11, function_tested, "no_data vector allocd_length");
87  test(vec_no_data->object_size == 2, function_tested, "no_data vector object_size");
88  vector_free(vec_no_data);
89 }
90 
92  char* function_tested = "vector_init";
93  double* data = malloc(sizeof(double[3]));
94  vector* vec_init = vector_init(data, sizeof(double)*2, sizeof(double)*3, sizeof(double));
95  test(vec_init != NULL, function_tested, "vec_init vector");
96  test(vec_init->data != NULL, function_tested, "vec_init vector data");
97  test(vec_init->data_length == sizeof(double)*2, function_tested, "vec_init vector data_length");
98  test(vec_init->allocd_length == sizeof(double)*3, function_tested, "vec_init vector allocd_length");
99  test(vec_init->object_size == sizeof(double), function_tested, "vec_init vector object_size");
100  vector_free(vec_init);
101 
102  double* no_data = NULL;
103  vector* vec_no_length = vector_init(no_data, 0, 0, 0);
104  test(vec_no_length == NULL, function_tested, "vec_no_length vector");
105 
106  vector* vec_no_data = vector_init(no_data, 0, 0, sizeof(double));
107  test(vec_no_data != NULL, function_tested, "vec_no_data vector");
108  test(vec_no_data->data == NULL, function_tested, "vec_no_data vector data");
109  test(vec_no_data->data_length == 0, function_tested, "vec_no_data vector data_length");
110  test(vec_no_data->allocd_length == 0, function_tested, "vec_no_data vector allocd_length");
111  test(vec_no_data->object_size == sizeof(double), function_tested, "vec_no_data vector object_size");
112  vector_free(vec_no_data);
113 }
114 
116  char* function_tested = "vector_insert";
117  int ierr = 0;
118 
119  double* data_1 = malloc(sizeof(double[3]));
120  vector* vector_1 = vector_init(data_1, sizeof(double)*3, sizeof(double)*3, sizeof(double));
121  double insert_1[2] = {0.,1.};
122  vector_insert(vector_1, insert_1, insert_1+sizeof(insert_1), 0, &ierr);
123  double* p_1 = vector_el(vector_1, 0);
124  test(p_1[0] == 0., function_tested, "vec_insert data 0");
125  test(p_1[1] == 1., function_tested, "vec_insert data 1");
126  vector_free(vector_1);
127 
128  // crashes - not sure why
129  vector* vector_2 = vector_init(NULL, 0, 0, sizeof(double));
130  double insert_2[2] = {0., 1.};
131  double* p_2 = vector_el(vector_2, 0);
132  vector_insert(vector_2, insert_2, insert_2+sizeof(insert_2), 0, &ierr);
133  test(p_2[0] == 0., function_tested, "vec_insert data 0");
134  test(p_2[1] == 1., function_tested, "vec_insert data 1");
135  vector_free(vector_2);
136 
137  double* data_3 = malloc(sizeof(double[3]));
138  vector* vector_3 = vector_init(data_3, sizeof(double)*3, sizeof(double)*3, sizeof(double));
139  double insert_3[6] = {0., 1., 2., 3., 4., 5.};
140  vector_insert(vector_3, insert_3, insert_3+sizeof(insert_3), sizeof(double)*3, &ierr);
141  double* p_3 = vector_el(vector_3, 0);
142  test(p_3[3] == 0., function_tested, "vec_insert data 3");
143  test(p_3[4] == 1., function_tested, "vec_insert data 4");
144  test(p_3[8] == 5., function_tested, "vec_insert data 8");
145  vector_free(vector_3);
146 }
147 
148 /*
149 vector vec_non-zero = vector_alloc(2, 2);
150 //realloc the vec
151 static vector* vector_realloc (vector* vec, int n_elements);
152 //build a vector from allocated data - data must be allocated elsewhere
153 static vector* vector_init (void* data, size_t data_length, size_t allocd_length, size_t object_size);
154 //delete the vector
155 static void vector_free (vector* vec);
156 //insert elements found between target_start and target_end into vec at the point vec->data+insert_point
157 //if insert_start == NULL, inserts at end (appends)
158 //fail if runs out of memory
159 static void vector_insert (vector* vec, void* start, void* end, size_t insert_point, int* ierr);
160 //return vector element at point int
161 static void* vector_el (vector* vec, int element);
162 //return vector element at point int - with bound checking, returns NULL on out-of-bounds
163 static void* vector_el_bc (vector* vec, int element);
164 //return number of elements
165 static size_t vector_size (vector* vec)
166 */
167 
168 int main() {
173  return 0;
174 }
175 
176