CR2RE Pipeline Reference Manual 1.6.8
hdrl_random-test.c
1/*
2 * This file is part of the HDRL
3 * Copyright (C) 2016 European Southern Observatory
4 *
5 * This program 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, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20
21#ifdef HAVE_CONFIG_H
22#include <config.h>
23#endif
24
25/*-----------------------------------------------------------------------------
26 Includes
27 -----------------------------------------------------------------------------*/
28
29#include "hdrl_random.h"
30
31#include <cpl.h>
32#include <stdint.h>
33#include <math.h>
34
35
36
37/*----------------------------------------------------------------------------*/
42/*----------------------------------------------------------------------------*/
43
44cpl_error_code test_basic(void)
45{
46 hdrl_random_state * state = hdrl_random_state_new(1, NULL);
47 const size_t N = 10000;
48
49 for (size_t i = 0; i < N; i++) {
50 int64_t r = hdrl_random_uniform_int64(state, 0, 100);
51 cpl_test_lt(r, 101);
52 r = hdrl_random_uniform_int64(state, 1000, 2000);
53 cpl_test_lt(r, 2001);
54 cpl_test_lt(999, r);
55
56 r = hdrl_random_uniform_int64(state, -(1ll<<55ll), 1ll<<55ll);
57 cpl_test_lt(r, (1ll<<55ll) + 1);
58 cpl_test_lt(-(1ll<<55ll) + 1ll, r);
59
60 r = hdrl_random_uniform_int64(state, -(1ll<<55ll), 0);
61 cpl_test_lt(r, 1);
62 cpl_test_lt(-(1ll<<55ll) + 1ll, r);
63
64 double rd = hdrl_random_uniform_double(state, -5., 2.);
65 cpl_test_lt(rd, nextafter(2., 1));
66 cpl_test_lt(nextafter(-5., -1), rd);
67 }
68
69 int buf[N];
70 double bufd[N];
71 for (size_t i = 0; i < N; i++) {
72 buf[i] = (int)hdrl_random_poisson(state, 300.);
73 bufd[i] = hdrl_random_normal(state, 3.5, 1.5);
74 }
75 cpl_image * iimg = cpl_image_wrap_int(N, 1, buf);
76 cpl_image * dimg = cpl_image_wrap_double(N, 1, bufd);
77 cpl_test_abs(cpl_image_get_mean(iimg), 300, 1);
78 cpl_test_abs(cpl_image_get_stdev(iimg), sqrt(300), 0.5);
79 cpl_test_abs(cpl_image_get_mean(dimg), 3.5, 0.1);
80 cpl_test_abs(cpl_image_get_stdev(dimg), 1.5, 0.1);
81
82 cpl_image_unwrap(iimg);
83 cpl_image_unwrap(dimg);
84 hdrl_random_state_delete(state);
85
86 uint64_t seed[2] = {1342, 232};
87 state = hdrl_random_state_new(1, seed);
88 hdrl_random_state_delete(state);
89
90 return cpl_error_get_code();
91}
92
93
94/*----------------------------------------------------------------------------*/
98/*----------------------------------------------------------------------------*/
99int main(void)
100{
101 cpl_test_init(PACKAGE_BUGREPORT, CPL_MSG_WARNING);
102
103 test_basic();
104
105 return cpl_test_end(0);
106}