CR2RE Pipeline Reference Manual 1.6.7
irplib_hist-test.c
1/* *
2 * This file is part of the ESO IRPLIB package *
3 * Copyright (C) 2004,2005 European Southern Observatory *
4 * *
5 * This library is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the Free Software *
17 * Foundation, 51 Franklin St, Fifth Floor, Boston, MA 02111-1307 USA *
18 * */
19
20#ifdef HAVE_CONFIG_H
21#include <config.h>
22#endif
23
24/*----------------------------------------------------------------------------
25 Includes
26 ----------------------------------------------------------------------------*/
27
28
29#include <irplib_hist.h>
30
31#include <math.h>
32/*---------------------------------------------------------------------------*/
33/*
34 * @defgroup irplib_hist_test Testing of the IRPLIB utilities
35 */
36/*---------------------------------------------------------------------------*/
37
38
39/*----------------------------------------------------------------------------
40 Defines
41 ----------------------------------------------------------------------------*/
42
43#define NBINS 100
44
45/*----------------------------------------------------------------------------
46 Private Function prototypes
47 ----------------------------------------------------------------------------*/
48
49static void irplib_hist_tests(void);
50
51/*---------------------------------------------------------------------------*/
52/*
53 * @brief Unit tests of fit module
54 */
55/*---------------------------------------------------------------------------*/
56
57int main(void)
58{
59 /* Initialize CPL + IRPLIB */
60 cpl_test_init(PACKAGE_BUGREPORT, CPL_MSG_WARNING);
61
62 irplib_hist_tests();
63
64 return cpl_test_end(0);
65}
66
67static void irplib_hist_tests(void)
68{
69 irplib_hist * hist;
70 cpl_image * image;
71 cpl_error_code error;
72 int i, j;
73 float * data;
74
75 unsigned long max_where;
76
77 /* 1. trial: Create a right histogram */
78 hist = irplib_hist_new();
79 cpl_test_nonnull(hist);
80 cpl_test_error(CPL_ERROR_NONE);
81 irplib_hist_delete(hist);
82
83 /* 3. trial: Histogram for a uniform image */
84 image = cpl_image_new(100, 100, CPL_TYPE_FLOAT);
85 cpl_image_add_scalar(image, 202);
86
87 hist = irplib_hist_new();
88
89 error = irplib_hist_init(hist, NBINS, 0, 500);
90 cpl_test_zero(error);
91 error = irplib_hist_fill(hist, image);
92 cpl_test_zero(error);
93
94 for(i = 0; i < 40; i++) {
95 cpl_test_zero(irplib_hist_get_value(hist, i));
96 }
97
98 /* The following call retrieves the value of the 42-st bin */
99 /* When i = 41, 42-th is retrieved. 500 - 0 / 100 = 5; 202/5=40,xx
100 it should be in the 41-th bin but it is in the next one because
101 there is one before left empty for possible values out of range
102 0 (hinit) < 202 (image constant)
103 */
104
105 cpl_test_eq(irplib_hist_get_value(hist, 40), 10000);
106 for(i = 42; i < NBINS; i++) {
107 cpl_test_zero(irplib_hist_get_value(hist, i));
108 }
109
110 irplib_hist_delete(hist);
111 cpl_image_delete(image);
112
113 /* 4. trial: Histogram for a normal image: no checking of the output */
114 image = cpl_image_new(100, 100, CPL_TYPE_FLOAT);
115 cpl_image_fill_noise_uniform(image, 0, 200);
116
117 hist = irplib_hist_new();
118 error = irplib_hist_fill(hist,image);
119 cpl_test_zero(error);
120
121 irplib_hist_delete(hist);
122 cpl_image_delete(image);
123
124 /* 5. trial: Histogram */
125 image = cpl_image_new(100, 100, CPL_TYPE_FLOAT);
126 data = cpl_image_get_data_float(image);
127 for (i = 0; i < 100; i++) {
128 for (j = 0; j < 100; j++) {
129 *(data + 100*i + j) = i +j;
130 }
131 }
132
133 hist = irplib_hist_new();
134 error = irplib_hist_fill(hist, image);
135 cpl_test_eq_error(error, CPL_ERROR_NONE);
136
137 irplib_hist_get_max(hist, &max_where);
138
139 /* The following call retrieves the value of the 41-st bin */
140 /* cpl_test_eq(irplib_hist_get_value(hist, 40), 10000);
141 for(i = 42; i < NBINS; i++) {
142 cpl_test_zero(irplib_hist_get_value(hist, i));
143 }*/
144
145 /* 6. trial: all by default ( we use the same image) */
146
147 cpl_test_eq(max_where, irplib_hist_get_nbins(hist)/2);
148
149 irplib_hist_delete(hist);
150 cpl_image_delete(image);
151}