File: hdrl_random-test.c

package info (click to toggle)
cpl-plugin-uves 6.1.3%2Bdfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 23,136 kB
  • sloc: ansic: 171,056; sh: 4,368; python: 3,002; makefile: 1,319
file content (106 lines) | stat: -rw-r--r-- 3,323 bytes parent folder | download | duplicates (16)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*
 * This file is part of the HDRL
 * Copyright (C) 2016 European Southern Observatory
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */


#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

/*-----------------------------------------------------------------------------
                                    Includes
 -----------------------------------------------------------------------------*/

#include "hdrl_random.h"

#include <cpl.h>
#include <stdint.h>
#include <math.h>



/*----------------------------------------------------------------------------*/
/**
 * @defgroup hdrl_random_test   
            Testing of hdrl_random module
 */
/*----------------------------------------------------------------------------*/

cpl_error_code test_basic(void)
{
    hdrl_random_state * state = hdrl_random_state_new(1, NULL);
    const size_t N = 10000;

    for (size_t i = 0; i < N; i++) {
        int64_t r = hdrl_random_uniform_int64(state, 0, 100);
        cpl_test_lt(r, 101);
        r = hdrl_random_uniform_int64(state, 1000, 2000);
        cpl_test_lt(r, 2001);
        cpl_test_lt(999, r);

        r = hdrl_random_uniform_int64(state, -(1ll<<55ll), 1ll<<55ll);
        cpl_test_lt(r, (1ll<<55ll) + 1);
        cpl_test_lt(-(1ll<<55ll) + 1ll, r);

        r = hdrl_random_uniform_int64(state, -(1ll<<55ll), 0);
        cpl_test_lt(r, 1);
        cpl_test_lt(-(1ll<<55ll) + 1ll, r);

        double rd = hdrl_random_uniform_double(state, -5., 2.);
        cpl_test_lt(rd, nextafter(2., 1));
        cpl_test_lt(nextafter(-5., -1), rd);
    }

    int buf[N];
    double bufd[N];
    for (size_t i = 0; i < N; i++) {
        buf[i] = (int)hdrl_random_poisson(state, 300.);
        bufd[i] = hdrl_random_normal(state, 3.5, 1.5);
    }
    cpl_image * iimg = cpl_image_wrap_int(N, 1, buf);
    cpl_image * dimg = cpl_image_wrap_double(N, 1, bufd);
    cpl_test_abs(cpl_image_get_mean(iimg), 300, 1);
    cpl_test_abs(cpl_image_get_stdev(iimg), sqrt(300), 0.5);
    cpl_test_abs(cpl_image_get_mean(dimg), 3.5, 0.1);
    cpl_test_abs(cpl_image_get_stdev(dimg), 1.5, 0.1);

    cpl_image_unwrap(iimg);
    cpl_image_unwrap(dimg);
    hdrl_random_state_delete(state);

    uint64_t seed[2] = {1342, 232};
    state = hdrl_random_state_new(1, seed);
    hdrl_random_state_delete(state);

    return cpl_error_get_code();
}


/*----------------------------------------------------------------------------*/
/**
 @brief   Unit tests of hdrl_image
 **/
/*----------------------------------------------------------------------------*/
int main(void)
{
    cpl_test_init(PACKAGE_BUGREPORT, CPL_MSG_WARNING);

    test_basic();

    return cpl_test_end(0);
}