22#include "../hdrl_cat_utils_sort.h"
24#include "hdrl_random.h"
25#include "hdrl_types.h"
33#define RANDOM_UNIFORM_DOBULE 1
34#define RANDOM_NORMAL 2
35#define RANDOM_POISSON 3
40int comparation_asc(
const void * a,
const void * b)
42 const double a_d = *(
const double *)a;
43 const double b_d = *(
const double *)b;
45 return a_d < b_d ? -1 : (a_d > b_d ? 1 : 0);
49int comparation_des(
const void * a,
const void * b)
51 const double a_d = *(
const double *)a;
52 const double b_d = *(
const double *)b;
54 return a_d > b_d ? -1 : (a_d < b_d ? 1 : 0);
57void test_sort(
double *a,
double *b, cpl_vector *c_vec, cpl_size random_type, cpl_sort_direction dir) {
62 hdrl_random_state *state = hdrl_random_state_new(1, NULL);
65 for (cpl_size i = 0; i < N; i++) {
68 case RANDOM_UNIFORM_DOBULE : rd = hdrl_random_uniform_double(state, 10., 20.);
break;
69 case RANDOM_NORMAL : rd = hdrl_random_normal(state, 3.5, 1.5);
break;
70 case RANDOM_POISSON : rd = hdrl_random_poisson(state, 100.);
break;
76 cpl_vector_set(c_vec, i, rd);
78 hdrl_random_state_delete(state);
81 if(dir == CPL_SORT_ASCENDING) {
82 qsort(a, N,
sizeof(*a), comparation_asc);
84 qsort(a, N,
sizeof(*a), comparation_des);
86 sort_array(b, N,
sizeof(*b), HDRL_SORT_DOUBLE, dir);
87 cpl_vector_sort(c_vec, dir);
90 for (cpl_size i = 0; i < N; i++) {
91 cpl_test_eq(a[i], b[i]);
92 cpl_test_eq(a[i], cpl_vector_get(c_vec, i));
93 cpl_test_eq(b[i], cpl_vector_get(c_vec, i));
97void test_cmp_sort_func(cpl_sort_direction dir)
99 double *a = cpl_malloc(N *
sizeof(
double));
100 double *b = cpl_malloc(N *
sizeof(
double));
101 double *c = cpl_malloc(N *
sizeof(
double));
102 cpl_vector *c_vec = cpl_vector_wrap(N, c);
105 for(cpl_size i = RANDOM_INIT + 1; i < RANDOM_END; i++) {
106 test_sort(a, b, c_vec, i, dir);
109 cpl_vector_unwrap(c_vec);
115void test_sort_arrays_random(
void)
117 double *a_asc = cpl_malloc(N *
sizeof(
double));
118 double *a_des = cpl_malloc(N *
sizeof(
double));
119 double *a1 = cpl_malloc(N *
sizeof(
double));
120 double *a2 = cpl_malloc(N *
sizeof(
double));
121 double *a3 = cpl_malloc(N *
sizeof(
double));
122 double *a4 = cpl_malloc(N *
sizeof(
double));
123 double *a6 = cpl_malloc(N *
sizeof(
double));
124 double *a7 = cpl_malloc(N *
sizeof(
double));
125 double *a8 = cpl_malloc(N *
sizeof(
double));
126 double *a9 = cpl_malloc(N *
sizeof(
double));
128 int *b_asc = cpl_malloc(N *
sizeof(
int));
129 int *b_des = cpl_malloc(N *
sizeof(
int));
130 int *b1 = cpl_malloc(N *
sizeof(
int));
131 int *b6 = cpl_malloc(N *
sizeof(
int));
133 double *c_asc = cpl_malloc(N *
sizeof(
double));
134 double *c_des = cpl_malloc(N *
sizeof(
double));
135 double *c2 = cpl_malloc(N *
sizeof(
double));
136 double *c7 = cpl_malloc(N *
sizeof(
double));
138 cpl_size *d_asc = cpl_malloc(N *
sizeof(cpl_size));
139 cpl_size *d_des = cpl_malloc(N *
sizeof(cpl_size));
140 cpl_size *d3 = cpl_malloc(N *
sizeof(cpl_size));
141 cpl_size *d8 = cpl_malloc(N *
sizeof(cpl_size));
143 hdrl_value *e_asc = cpl_malloc(N *
sizeof(hdrl_value));
144 hdrl_value *e_des = cpl_malloc(N *
sizeof(hdrl_value));
145 hdrl_value *e4 = cpl_malloc(N *
sizeof(hdrl_value));
146 hdrl_value *e9 = cpl_malloc(N *
sizeof(hdrl_value));
150 hdrl_random_state *state = hdrl_random_state_new(1, NULL);
151 for (cpl_size i = 0; i < N; i++) {
153 a_asc[i] = hdrl_random_normal(state, 5.5, 0.5);
164 b_asc[i] = hdrl_random_poisson(state, 100.);
169 c_asc[i] = hdrl_random_uniform_double(state, 10., 20.);
174 d_asc[i] = hdrl_random_poisson(state, 300.);
179 e_asc[i] = (hdrl_value){hdrl_random_normal(state, 3.5, 1.5),
180 hdrl_random_normal(state, 2.5, 1.0) };
181 e4[i] = (hdrl_value){e_asc[i].data, e_asc[i].error};
182 e_des[i] = (hdrl_value){e_asc[i].data, e_asc[i].error};
183 e9[i] = (hdrl_value){e_des[i].data, e_des[i].error};
185 hdrl_random_state_delete(state);
188 void * arrs_asc[ 4] = { b_asc, c_asc, d_asc, e_asc};
189 hdrl_sort_type types_asc[4] = { HDRL_SORT_INT, HDRL_SORT_DOUBLE, HDRL_SORT_CPL_SIZE, HDRL_SORT_HDRL_VALUE};
192 void * arrs_des[ 4] = { b_des, c_des, d_des, e_des};
193 hdrl_sort_type types_des[4] = { HDRL_SORT_INT, HDRL_SORT_DOUBLE, HDRL_SORT_CPL_SIZE, HDRL_SORT_HDRL_VALUE};
198 cpl_test_error(CPL_ERROR_NONE);
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);
219 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);
270void test_sort_arrays_fixed(
void)
274 double x_asc[10] = {-0.5, 0.33, 2.66, -3.5, 5.1, 5.2, 5.3, 5.4, 5.5, 4.4};
275 double x_des[10] = {-0.5, 0.33, 2.66, -3.5, 5.1, 5.2, 5.3, 5.4, 5.5, 4.4};
276 double x1[10] = {-0.5, 0.33, 2.66, -3.5, 5.1, 5.2, 5.3, 5.4, 5.5, 4.4};
277 double x2[10] = {-0.5, 0.33, 2.66, -3.5, 5.1, 5.2, 5.3, 5.4, 5.5, 4.4};
278 double x3[10] = {-0.5, 0.33, 2.66, -3.5, 5.1, 5.2, 5.3, 5.4, 5.5, 4.4};
279 double x4[10] = {-0.5, 0.33, 2.66, -3.5, 5.1, 5.2, 5.3, 5.4, 5.5, 4.4};
280 double x6[10] = {-0.5, 0.33, 2.66, -3.5, 5.1, 5.2, 5.3, 5.4, 5.5, 4.4};
281 double x7[10] = {-0.5, 0.33, 2.66, -3.5, 5.1, 5.2, 5.3, 5.4, 5.5, 4.4};
282 double x8[10] = {-0.5, 0.33, 2.66, -3.5, 5.1, 5.2, 5.3, 5.4, 5.5, 4.4};
283 double x9[10] = {-0.5, 0.33, 2.66, -3.5, 5.1, 5.2, 5.3, 5.4, 5.5, 4.4};
284 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};
285 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};
287 int i_asc[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
288 int i_des[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
289 int i_1[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
290 int i_6[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
291 int i_corr_asc[10] = { 3, 0, 1, 2, 9, 4, 5, 6, 7, 8};
292 int i_corr_des[10] = { 8, 7, 6, 5, 4, 9, 2, 1, 0, 3};
294 double db_asc[10] = { 0.,-1.,-2.,-3.,-4., 5., 6., 7., 8., 9.};
295 double db_des[10] = { 0.,-1.,-2.,-3.,-4., 5., 6., 7., 8., 9.};
296 double db_2[10] = { 0.,-1.,-2.,-3.,-4., 5., 6., 7., 8., 9.};
297 double db_7[10] = { 0.,-1.,-2.,-3.,-4., 5., 6., 7., 8., 9.};
298 double db_corr_asc[10] = {-3., 0.,-1.,-2., 9.,-4., 5., 6., 7., 8.};
299 double db_corr_des[10] = { 8., 7., 6., 5.,-4., 9.,-2.,-1., 0.,-3.};
301 cpl_size cp_asc[10] = { 0,-1,-2,-3,-4,-5,-6,-7,-8,-9};
302 cpl_size cp_des[10] = { 0,-1,-2,-3,-4,-5,-6,-7,-8,-9};
303 cpl_size cp_3[10] = { 0,-1,-2,-3,-4,-5,-6,-7,-8,-9};
304 cpl_size cp_8[10] = { 0,-1,-2,-3,-4,-5,-6,-7,-8,-9};
305 cpl_size cp_corr_asc[10] = {-3, 0,-1,-2,-9,-4,-5,-6,-7,-8};
306 cpl_size cp_corr_des[10] = {-8,-7,-6,-5,-4,-9,-2,-1, 0,-3};
308 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},
309 (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} };
310 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},
311 (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} };
312 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},
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_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},
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_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},
317 (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} };
318 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},
319 (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} };
322 void * arrs_asc[ 4] = { i_asc, db_asc, cp_asc, hv_asc};
323 hdrl_sort_type types_asc[4] = { HDRL_SORT_INT, HDRL_SORT_DOUBLE, HDRL_SORT_CPL_SIZE, HDRL_SORT_HDRL_VALUE};
326 void * arrs_des[ 4] = { i_des, db_des, cp_des, hv_des};
327 hdrl_sort_type types_des[4] = { HDRL_SORT_INT, HDRL_SORT_DOUBLE, HDRL_SORT_CPL_SIZE, HDRL_SORT_HDRL_VALUE};
332 cpl_test_error(CPL_ERROR_NONE);
333 for (cpl_size ind = 0; ind < 10; ind++) {
334 cpl_test_eq(x1[ ind], x_corr_asc[ind]);
335 cpl_test_eq(i_1[ind], i_corr_asc[ind]);
340 cpl_test_error(CPL_ERROR_NONE);
341 for (cpl_size ind = 0; ind < 10; ind++) {
342 cpl_test_eq(x2[ ind], x_corr_asc[ ind]);
343 cpl_test_eq(db_2[ind], db_corr_asc[ind]);
348 cpl_test_error(CPL_ERROR_NONE);
349 for (cpl_size ind = 0; ind < 10; ind++) {
350 cpl_test_eq(x3[ ind], x_corr_asc[ ind]);
351 cpl_test_eq(cp_3[ind], cp_corr_asc[ind]);
356 cpl_test_error(CPL_ERROR_NONE);
357 for (cpl_size ind = 0; ind < 10; ind++) {
358 cpl_test_eq(x4[ ind], x_corr_asc[ ind]);
359 cpl_test_eq(hv_4[ind].data, hv_corr_asc[ind].data);
360 cpl_test_eq(hv_4[ind].error, hv_corr_asc[ind].error);
365 cpl_test_error(CPL_ERROR_NONE);
366 for (cpl_size ind = 0; ind < 10; ind++) {
367 cpl_test_eq(x_asc[ ind], x_corr_asc[ ind]);
368 cpl_test_eq(i_asc[ ind], i_corr_asc[ ind]);
369 cpl_test_eq(db_asc[ind], db_corr_asc[ind]);
370 cpl_test_eq(cp_asc[ind], cp_corr_asc[ind]);
371 cpl_test_eq(hv_asc[ind].data, hv_corr_asc[ind].data);
372 cpl_test_eq(hv_asc[ind].error, hv_corr_asc[ind].error);
378 cpl_test_error(CPL_ERROR_NONE);
379 for (cpl_size ind = 0; ind < 10; ind++) {
380 cpl_test_eq(x6[ ind], x_corr_des[ind]);
381 cpl_test_eq(i_6[ind], i_corr_des[ind]);
386 cpl_test_error(CPL_ERROR_NONE);
387 for (cpl_size ind = 0; ind < 10; ind++) {
388 cpl_test_eq(x7[ ind], x_corr_des[ ind]);
389 cpl_test_eq(db_7[ind], db_corr_des[ind]);
394 cpl_test_error(CPL_ERROR_NONE);
395 for (cpl_size ind = 0; ind < 10; ind++) {
396 cpl_test_eq(x8[ ind], x_corr_des[ ind]);
397 cpl_test_eq(cp_8[ind], cp_corr_des[ind]);
402 cpl_test_error(CPL_ERROR_NONE);
403 for (cpl_size ind = 0; ind < 10; ind++) {
404 cpl_test_eq(x9[ ind], x_corr_des[ ind]);
405 cpl_test_eq(hv_9[ind].data, hv_corr_des[ind].data);
406 cpl_test_eq(hv_9[ind].error, hv_corr_des[ind].error);
411 cpl_test_error(CPL_ERROR_NONE);
412 for (cpl_size ind = 0; ind < 10; ind++) {
413 cpl_test_eq(x_des[ ind], x_corr_des[ ind]);
414 cpl_test_eq(i_des[ ind], i_corr_des[ ind]);
415 cpl_test_eq(db_des[ind], db_corr_des[ind]);
416 cpl_test_eq(cp_des[ind], cp_corr_des[ind]);
417 cpl_test_eq(hv_des[ind].data, hv_corr_des[ind].data);
418 cpl_test_eq(hv_des[ind].error, hv_corr_des[ind].error);
424 cpl_test_init(PACKAGE_BUGREPORT,CPL_MSG_WARNING);
428 test_cmp_sort_func(CPL_SORT_ASCENDING);
429 test_cmp_sort_func(CPL_SORT_DESCENDING);
432 test_sort_arrays_random();
435 test_sort_arrays_fixed();
437 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...