test_ssp_ran_circle.h (2545B)
1 /* Copyright (C) 2015-2025 |Méso|Star> (contact@meso-star.com) 2 * 3 * This program is free software: you can redistribute it and/or modify 4 * it under the terms of the GNU General Public License as published by 5 * the Free Software Foundation, either version 3 of the License, or 6 * (at your option) any later version. 7 * 8 * This program is distributed in the hope that it will be useful, 9 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * GNU General Public License for more details. 12 * 13 * You should have received a copy of the GNU General Public License 14 * along with this program. If not, see <http://www.gnu.org/licenses/>. */ 15 16 #ifndef TEST_SSP_RAN_CIRCLE_H 17 #define TEST_SSP_RAN_CIRCLE_H 18 19 #include "ssp.h" 20 #include "test_ssp_utils.h" 21 22 #define NSAMPS 128 23 24 #endif /* TEST_SSP_RAN_CIRCLE_H */ 25 26 #if TYPE_FLOAT == 0 27 #include <rsys/double2.h> 28 29 #define REAL double 30 #define TEST test_double 31 #define RAN_CIRCLE_UNIFORM ssp_ran_circle_uniform 32 #define RAN_CIRCLE_UNIFORM_PDF ssp_ran_circle_uniform_pdf 33 #define EQ_EPS eq_eps 34 #define R2_EQ_EPS d2_eq_eps 35 #define R2_IS_NORMALIZED d2_is_normalized 36 37 #elif TYPE_FLOAT == 1 38 #include <rsys/float2.h> 39 40 #define REAL float 41 #define TEST test_float 42 #define RAN_CIRCLE_UNIFORM ssp_ran_circle_uniform_float 43 #define RAN_CIRCLE_UNIFORM_PDF ssp_ran_circle_uniform_float_pdf 44 #define EQ_EPS eq_epsf 45 #define R2_EQ_EPS f2_eq_eps 46 #define R2_IS_NORMALIZED f2_is_normalized 47 #else 48 #error "TYPE_FLOAT must be defined either 0 or 1" 49 #endif 50 51 static void 52 TEST(void) 53 { 54 struct ssp_rng* rng; 55 struct mem_allocator allocator; 56 REAL samps[NSAMPS][4]; 57 int i = 0, j = 9; 58 59 CHK(mem_init_proxy_allocator(&allocator, &mem_default_allocator) == RES_OK); 60 CHK(ssp_rng_create(&allocator, SSP_RNG_MT19937_64, &rng) == RES_OK); 61 CHK(EQ_EPS(RAN_CIRCLE_UNIFORM_PDF(), 1/(2*(REAL)PI), (REAL)1.e-6)); 62 63 FOR_EACH(i, 0, NSAMPS) { 64 CHK(RAN_CIRCLE_UNIFORM(rng, samps[i], &samps[i][3]) == samps[i]); 65 CHK(R2_IS_NORMALIZED(samps[i])); 66 CHK(EQ_EPS(samps[i][3], 1/(2*(REAL)PI), (REAL)1.e-6)); 67 FOR_EACH(j, 0, i) { 68 CHK(!R2_EQ_EPS(samps[j], samps[i], (REAL)1.e-6)); 69 } 70 printf("%g %g\n", SPLIT2(samps[i])); 71 } 72 73 ssp_rng_ref_put(rng); 74 check_memory_allocator(&allocator); 75 mem_shutdown_proxy_allocator(&allocator); 76 77 CHK(mem_allocated_size() == 0); 78 } 79 80 #undef REAL 81 #undef TEST 82 #undef RAN_CIRCLE_UNIFORM 83 #undef RAN_CIRCLE_UNIFORM_PDF 84 #undef EQ_EPS 85 #undef R2_EQ_EPS 86 #undef R2_IS_NORMALIZED 87 #undef TYPE_FLOAT 88