26#include "../hdrl_cat_utils_sort.h"
28#include "hdrl_random.h"
29#include "hdrl_types.h"
37#define RANDOM_UNIFORM_DOBULE 1
38#define RANDOM_NORMAL 2
39#define RANDOM_POISSON 3
44int comparation_asc(
const void * a,
const void * b)
46 const double a_d = *(
const double *)a;
47 const double b_d = *(
const double *)b;
49 return a_d < b_d ? -1 : (a_d > b_d ? 1 : 0);
53int comparation_des(
const void * a,
const void * b)
55 const double a_d = *(
const double *)a;
56 const double b_d = *(
const double *)b;
58 return a_d > b_d ? -1 : (a_d < b_d ? 1 : 0);
61void test_sort(
double *a,
double *b, cpl_vector *c_vec, cpl_size random_type, cpl_sort_direction dir) {
66 hdrl_random_state *state = hdrl_random_state_new(1, NULL);
69 for (cpl_size i = 0; i < N; i++) {
72 case RANDOM_UNIFORM_DOBULE : rd = hdrl_random_uniform_double(state, 10., 20.);
break;
73 case RANDOM_NORMAL : rd = hdrl_random_normal(state, 3.5, 1.5);
break;
74 case RANDOM_POISSON : rd = hdrl_random_poisson(state, 100.);
break;
80 cpl_vector_set(c_vec, i, rd);
82 hdrl_random_state_delete(state);
85 if(dir == CPL_SORT_ASCENDING) {
86 qsort(a, N,
sizeof(*a), comparation_asc);
88 qsort(a, N,
sizeof(*a), comparation_des);
90 sort_array(b, N,
sizeof(*b), HDRL_SORT_DOUBLE, dir);
91 cpl_vector_sort(c_vec, dir);
94 for (cpl_size i = 0; i < N; i++) {
95 cpl_test_eq(a[i], b[i]);
96 cpl_test_eq(a[i], cpl_vector_get(c_vec, i));
97 cpl_test_eq(b[i], cpl_vector_get(c_vec, i));
101void test_cmp_sort_func(cpl_sort_direction dir)
103 double *a = cpl_malloc(N *
sizeof(
double));
104 double *b = cpl_malloc(N *
sizeof(
double));
105 double *c = cpl_malloc(N *
sizeof(
double));
106 cpl_vector *c_vec = cpl_vector_wrap(N, c);
109 for(cpl_size i = RANDOM_INIT + 1; i < RANDOM_END; i++) {
110 test_sort(a, b, c_vec, i, dir);
113 cpl_vector_unwrap(c_vec);
119void test_sort_arrays_random(
void)
121 double *a_asc = cpl_malloc(N *
sizeof(
double));
122 double *a_des = cpl_malloc(N *
sizeof(
double));
123 double *a1 = cpl_malloc(N *
sizeof(
double));
124 double *a2 = cpl_malloc(N *
sizeof(
double));
125 double *a3 = cpl_malloc(N *
sizeof(
double));
126 double *a4 = cpl_malloc(N *
sizeof(
double));
127 double *a6 = cpl_malloc(N *
sizeof(
double));
128 double *a7 = cpl_malloc(N *
sizeof(
double));
129 double *a8 = cpl_malloc(N *
sizeof(
double));
130 double *a9 = cpl_malloc(N *
sizeof(
double));
132 int *b_asc = cpl_malloc(N *
sizeof(
int));
133 int *b_des = cpl_malloc(N *
sizeof(
int));
134 int *b1 = cpl_malloc(N *
sizeof(
int));
135 int *b6 = cpl_malloc(N *
sizeof(
int));
137 double *c_asc = cpl_malloc(N *
sizeof(
double));
138 double *c_des = cpl_malloc(N *
sizeof(
double));
139 double *c2 = cpl_malloc(N *
sizeof(
double));
140 double *c7 = cpl_malloc(N *
sizeof(
double));
142 cpl_size *d_asc = cpl_malloc(N *
sizeof(cpl_size));
143 cpl_size *d_des = cpl_malloc(N *
sizeof(cpl_size));
144 cpl_size *d3 = cpl_malloc(N *
sizeof(cpl_size));
145 cpl_size *d8 = cpl_malloc(N *
sizeof(cpl_size));
147 hdrl_value *e_asc = cpl_malloc(N *
sizeof(hdrl_value));
148 hdrl_value *e_des = cpl_malloc(N *
sizeof(hdrl_value));
149 hdrl_value *e4 = cpl_malloc(N *
sizeof(hdrl_value));
150 hdrl_value *e9 = cpl_malloc(N *
sizeof(hdrl_value));
154 hdrl_random_state *state = hdrl_random_state_new(1, NULL);
155 for (cpl_size i = 0; i < N; i++) {
157 a_asc[i] = hdrl_random_normal(state, 5.5, 0.5);
168 b_asc[i] = hdrl_random_poisson(state, 100.);
173 c_asc[i] = hdrl_random_uniform_double(state, 10., 20.);
178 d_asc[i] = hdrl_random_poisson(state, 300.);
183 e_asc[i] = (hdrl_value){hdrl_random_normal(state, 3.5, 1.5),
184 hdrl_random_normal(state, 2.5, 1.0) };
185 e4[i] = (hdrl_value){e_asc[i].data, e_asc[i].error};
186 e_des[i] = (hdrl_value){e_asc[i].data, e_asc[i].error};
187 e9[i] = (hdrl_value){e_des[i].data, e_des[i].error};
189 hdrl_random_state_delete(state);
192 void * arrs_asc[ 4] = { b_asc, c_asc, d_asc, e_asc};
193 hdrl_sort_type types_asc[4] = { HDRL_SORT_INT, HDRL_SORT_DOUBLE, HDRL_SORT_CPL_SIZE, HDRL_SORT_HDRL_VALUE};
196 void * arrs_des[ 4] = { b_des, c_des, d_des, e_des};
197 hdrl_sort_type types_des[4] = { HDRL_SORT_INT, HDRL_SORT_DOUBLE, HDRL_SORT_CPL_SIZE, HDRL_SORT_HDRL_VALUE};
202 cpl_test_error(CPL_ERROR_NONE);
206 cpl_test_error(CPL_ERROR_NONE);
210 cpl_test_error(CPL_ERROR_NONE);
214 cpl_test_error(CPL_ERROR_NONE);
218 cpl_test_error(CPL_ERROR_NONE);
223 cpl_test_error(CPL_ERROR_NONE);
227 cpl_test_error(CPL_ERROR_NONE);
231 cpl_test_error(CPL_ERROR_NONE);
235 cpl_test_error(CPL_ERROR_NONE);
239 cpl_test_error(CPL_ERROR_NONE);
274void test_sort_arrays_fixed(
void)
278 double x_asc[10] = {-0.5, 0.33, 2.66, -3.5, 5.1, 5.2, 5.3, 5.4, 5.5, 4.4};
279 double x_des[10] = {-0.5, 0.33, 2.66, -3.5, 5.1, 5.2, 5.3, 5.4, 5.5, 4.4};
280 double x1[10] = {-0.5, 0.33, 2.66, -3.5, 5.1, 5.2, 5.3, 5.4, 5.5, 4.4};
281 double x2[10] = {-0.5, 0.33, 2.66, -3.5, 5.1, 5.2, 5.3, 5.4, 5.5, 4.4};
282 double x3[10] = {-0.5, 0.33, 2.66, -3.5, 5.1, 5.2, 5.3, 5.4, 5.5, 4.4};
283 double x4[10] = {-0.5, 0.33, 2.66, -3.5, 5.1, 5.2, 5.3, 5.4, 5.5, 4.4};
284 double x6[10] = {-0.5, 0.33, 2.66, -3.5, 5.1, 5.2, 5.3, 5.4, 5.5, 4.4};
285 double x7[10] = {-0.5, 0.33, 2.66, -3.5, 5.1, 5.2, 5.3, 5.4, 5.5, 4.4};
286 double x8[10] = {-0.5, 0.33, 2.66, -3.5, 5.1, 5.2, 5.3, 5.4, 5.5, 4.4};
287 double x9[10] = {-0.5, 0.33, 2.66, -3.5, 5.1, 5.2, 5.3, 5.4, 5.5, 4.4};
288 double x_corr_asc[10] = {-3.5, -0.5, 0.33, 2.66, 4.4, 5.1, 5.2, 5.3, 5.4, 5.5};
289 double x_corr_des[10] = { 5.5, 5.4, 5.3, 5.2, 5.1, 4.4, 2.66, 0.33,-0.5,-3.5};
291 int i_asc[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
292 int i_des[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
293 int i_1[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
294 int i_6[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
295 int i_corr_asc[10] = { 3, 0, 1, 2, 9, 4, 5, 6, 7, 8};
296 int i_corr_des[10] = { 8, 7, 6, 5, 4, 9, 2, 1, 0, 3};
298 double db_asc[10] = { 0.,-1.,-2.,-3.,-4., 5., 6., 7., 8., 9.};
299 double db_des[10] = { 0.,-1.,-2.,-3.,-4., 5., 6., 7., 8., 9.};
300 double db_2[10] = { 0.,-1.,-2.,-3.,-4., 5., 6., 7., 8., 9.};
301 double db_7[10] = { 0.,-1.,-2.,-3.,-4., 5., 6., 7., 8., 9.};
302 double db_corr_asc[10] = {-3., 0.,-1.,-2., 9.,-4., 5., 6., 7., 8.};
303 double db_corr_des[10] = { 8., 7., 6., 5.,-4., 9.,-2.,-1., 0.,-3.};
305 cpl_size cp_asc[10] = { 0,-1,-2,-3,-4,-5,-6,-7,-8,-9};
306 cpl_size cp_des[10] = { 0,-1,-2,-3,-4,-5,-6,-7,-8,-9};
307 cpl_size cp_3[10] = { 0,-1,-2,-3,-4,-5,-6,-7,-8,-9};
308 cpl_size cp_8[10] = { 0,-1,-2,-3,-4,-5,-6,-7,-8,-9};
309 cpl_size cp_corr_asc[10] = {-3, 0,-1,-2,-9,-4,-5,-6,-7,-8};
310 cpl_size cp_corr_des[10] = {-8,-7,-6,-5,-4,-9,-2,-1, 0,-3};
312 hdrl_value hv_asc[10] = {(hdrl_value){-3.5, 0.1}, (hdrl_value){-0.5, 0.2}, (hdrl_value){ 0.001, 0.3}, (hdrl_value){ 1.1, 0.4}, (hdrl_value){1.2, 0.5},
313 (hdrl_value){ 5.0, 0.6}, (hdrl_value){ 6.0, 0.7}, (hdrl_value){ 7.0, 0.8}, (hdrl_value){ 8.0, 0.9}, (hdrl_value){9.0, 1.0} };
314 hdrl_value hv_des[10] = {(hdrl_value){-3.5, 0.1}, (hdrl_value){-0.5, 0.2}, (hdrl_value){ 0.001, 0.3}, (hdrl_value){ 1.1, 0.4}, (hdrl_value){1.2, 0.5},
315 (hdrl_value){ 5.0, 0.6}, (hdrl_value){ 6.0, 0.7}, (hdrl_value){ 7.0, 0.8}, (hdrl_value){ 8.0, 0.9}, (hdrl_value){9.0, 1.0} };
316 hdrl_value hv_4[10] = {(hdrl_value){-3.5, 0.1}, (hdrl_value){-0.5, 0.2}, (hdrl_value){ 0.001, 0.3}, (hdrl_value){ 1.1, 0.4}, (hdrl_value){1.2, 0.5},
317 (hdrl_value){ 5.0, 0.6}, (hdrl_value){ 6.0, 0.7}, (hdrl_value){ 7.0, 0.8}, (hdrl_value){ 8.0, 0.9}, (hdrl_value){9.0, 1.0} };
318 hdrl_value hv_9[10] = {(hdrl_value){-3.5, 0.1}, (hdrl_value){-0.5, 0.2}, (hdrl_value){ 0.001, 0.3}, (hdrl_value){ 1.1, 0.4}, (hdrl_value){1.2, 0.5},
319 (hdrl_value){ 5.0, 0.6}, (hdrl_value){ 6.0, 0.7}, (hdrl_value){ 7.0, 0.8}, (hdrl_value){ 8.0, 0.9}, (hdrl_value){9.0, 1.0} };
320 hdrl_value hv_corr_asc[10] = {(hdrl_value){ 1.1, 0.4}, (hdrl_value){-3.5, 0.1}, (hdrl_value){-0.5, 0.2}, (hdrl_value){ 0.001, 0.3}, (hdrl_value){9.0, 1.0},
321 (hdrl_value){ 1.2, 0.5}, (hdrl_value){ 5.0, 0.6}, (hdrl_value){ 6.0, 0.7}, (hdrl_value){ 7.0, 0.8}, (hdrl_value){8.0, 0.9} };
322 hdrl_value hv_corr_des[10] = {(hdrl_value){ 8.0, 0.9}, (hdrl_value){ 7.0, 0.8}, (hdrl_value){ 6.0, 0.7}, (hdrl_value){ 5.0, 0.6}, (hdrl_value){1.2, 0.5},
323 (hdrl_value){ 9.0, 1.0}, (hdrl_value){ 0.001, 0.3}, (hdrl_value){-0.5, 0.2}, (hdrl_value){-3.5, 0.1}, (hdrl_value){1.1, 0.4} };
326 void * arrs_asc[ 4] = { i_asc, db_asc, cp_asc, hv_asc};
327 hdrl_sort_type types_asc[4] = { HDRL_SORT_INT, HDRL_SORT_DOUBLE, HDRL_SORT_CPL_SIZE, HDRL_SORT_HDRL_VALUE};
330 void * arrs_des[ 4] = { i_des, db_des, cp_des, hv_des};
331 hdrl_sort_type types_des[4] = { HDRL_SORT_INT, HDRL_SORT_DOUBLE, HDRL_SORT_CPL_SIZE, HDRL_SORT_HDRL_VALUE};
336 cpl_test_error(CPL_ERROR_NONE);
337 for (cpl_size ind = 0; ind < 10; ind++) {
338 cpl_test_eq(x1[ ind], x_corr_asc[ind]);
339 cpl_test_eq(i_1[ind], i_corr_asc[ind]);
344 cpl_test_error(CPL_ERROR_NONE);
345 for (cpl_size ind = 0; ind < 10; ind++) {
346 cpl_test_eq(x2[ ind], x_corr_asc[ ind]);
347 cpl_test_eq(db_2[ind], db_corr_asc[ind]);
352 cpl_test_error(CPL_ERROR_NONE);
353 for (cpl_size ind = 0; ind < 10; ind++) {
354 cpl_test_eq(x3[ ind], x_corr_asc[ ind]);
355 cpl_test_eq(cp_3[ind], cp_corr_asc[ind]);
360 cpl_test_error(CPL_ERROR_NONE);
361 for (cpl_size ind = 0; ind < 10; ind++) {
362 cpl_test_eq(x4[ ind], x_corr_asc[ ind]);
363 cpl_test_eq(hv_4[ind].data, hv_corr_asc[ind].data);
364 cpl_test_eq(hv_4[ind].error, hv_corr_asc[ind].error);
369 cpl_test_error(CPL_ERROR_NONE);
370 for (cpl_size ind = 0; ind < 10; ind++) {
371 cpl_test_eq(x_asc[ ind], x_corr_asc[ ind]);
372 cpl_test_eq(i_asc[ ind], i_corr_asc[ ind]);
373 cpl_test_eq(db_asc[ind], db_corr_asc[ind]);
374 cpl_test_eq(cp_asc[ind], cp_corr_asc[ind]);
375 cpl_test_eq(hv_asc[ind].data, hv_corr_asc[ind].data);
376 cpl_test_eq(hv_asc[ind].error, hv_corr_asc[ind].error);
382 cpl_test_error(CPL_ERROR_NONE);
383 for (cpl_size ind = 0; ind < 10; ind++) {
384 cpl_test_eq(x6[ ind], x_corr_des[ind]);
385 cpl_test_eq(i_6[ind], i_corr_des[ind]);
390 cpl_test_error(CPL_ERROR_NONE);
391 for (cpl_size ind = 0; ind < 10; ind++) {
392 cpl_test_eq(x7[ ind], x_corr_des[ ind]);
393 cpl_test_eq(db_7[ind], db_corr_des[ind]);
398 cpl_test_error(CPL_ERROR_NONE);
399 for (cpl_size ind = 0; ind < 10; ind++) {
400 cpl_test_eq(x8[ ind], x_corr_des[ ind]);
401 cpl_test_eq(cp_8[ind], cp_corr_des[ind]);
406 cpl_test_error(CPL_ERROR_NONE);
407 for (cpl_size ind = 0; ind < 10; ind++) {
408 cpl_test_eq(x9[ ind], x_corr_des[ ind]);
409 cpl_test_eq(hv_9[ind].data, hv_corr_des[ind].data);
410 cpl_test_eq(hv_9[ind].error, hv_corr_des[ind].error);
415 cpl_test_error(CPL_ERROR_NONE);
416 for (cpl_size ind = 0; ind < 10; ind++) {
417 cpl_test_eq(x_des[ ind], x_corr_des[ ind]);
418 cpl_test_eq(i_des[ ind], i_corr_des[ ind]);
419 cpl_test_eq(db_des[ind], db_corr_des[ind]);
420 cpl_test_eq(cp_des[ind], cp_corr_des[ind]);
421 cpl_test_eq(hv_des[ind].data, hv_corr_des[ind].data);
422 cpl_test_eq(hv_des[ind].error, hv_corr_des[ind].error);
428 cpl_test_init(PACKAGE_BUGREPORT,CPL_MSG_WARNING);
432 test_cmp_sort_func(CPL_SORT_ASCENDING);
433 test_cmp_sort_func(CPL_SORT_DESCENDING);
436 test_sort_arrays_random();
439 test_sort_arrays_fixed();
441 return cpl_test_end(0);
cpl_error_code sort_array(void *a, cpl_size nE, cpl_size sE, hdrl_sort_type type, cpl_sort_direction dir)
sort_array hdrl function for order arrays with know types. Using the type parameter for select the co...
cpl_error_code sort_arrays_index(double *a, cpl_size nE, void **bs, cpl_size nA, hdrl_sort_type *types, cpl_sort_direction dir)
sort_arrays_index hdrl function for sort several arrays The alghorithm sort 'a' and in the same way s...
cpl_error_code sort_array_index(double *a, cpl_size nE, void *b, hdrl_sort_type type, cpl_sort_direction dir)
sort_array_index hdrl function for sort two arrays The alghorithm sort 'a' and in the same way sort t...