test_ssp_ran_sphere.h (2435B)
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_SPHERE_H 17 #define TEST_SSP_RAN_SPHERE_H 18 19 #include "ssp.h" 20 #include "test_ssp_utils.h" 21 22 #define NSAMPS 128 23 24 #endif /* TEST_SSP_RAN_SPHERE_H */ 25 26 #if TYPE_FLOAT==0 27 #define REAL double 28 #define TEST test_double 29 #define RAN_SPHERE_UNIFORM ssp_ran_sphere_uniform 30 #define RAN_SPHERE_UNIFORM_PDF ssp_ran_sphere_uniform_pdf 31 #define EQ_EPS eq_eps 32 #define R3_EQ_EPS d3_eq_eps 33 #define R3_IS_NORMALIZED d3_is_normalized 34 35 #elif TYPE_FLOAT==1 36 #define REAL float 37 #define TEST test_float 38 #define RAN_SPHERE_UNIFORM ssp_ran_sphere_uniform_float 39 #define RAN_SPHERE_UNIFORM_PDF ssp_ran_sphere_uniform_float_pdf 40 #define EQ_EPS eq_epsf 41 #define R3_EQ_EPS f3_eq_eps 42 #define R3_IS_NORMALIZED f3_is_normalized 43 44 #else 45 #error "TYPE_FLOAT must be defined either 0 or 1" 46 #endif 47 48 static void 49 TEST() 50 { 51 struct ssp_rng* rng; 52 struct mem_allocator allocator; 53 REAL samps[NSAMPS][4]; 54 REAL* f = NULL; 55 int i = 0, j = 0; 56 57 mem_init_proxy_allocator(&allocator, &mem_default_allocator); 58 59 CHK(ssp_rng_create(&allocator, SSP_RNG_MT19937_64, &rng) == RES_OK); 60 61 FOR_EACH(i, 0, NSAMPS) { 62 f = RAN_SPHERE_UNIFORM(rng, samps[i], &samps[i][3]); 63 CHK(f == samps[i]); 64 CHK(R3_IS_NORMALIZED(f) == 1); 65 CHK(EQ_EPS(samps[i][3], 1/(4*(REAL)PI), (REAL)1.e-6) == 1); 66 CHK(EQ_EPS(samps[i][3], RAN_SPHERE_UNIFORM_PDF(), (REAL)1.e-6) == 1); 67 FOR_EACH(j, 0, i) { 68 CHK(R3_EQ_EPS(samps[j], samps[i], (REAL)1.e-6) == 0); 69 } 70 } 71 72 ssp_rng_ref_put(rng); 73 check_memory_allocator(&allocator); 74 mem_shutdown_proxy_allocator(&allocator); 75 76 CHK(mem_allocated_size() == 0); 77 } 78 79 #undef REAL 80 #undef TEST 81 #undef RAN_SPHERE_UNIFORM 82 #undef RAN_SPHERE_UNIFORM_PDF 83 #undef EQ_EPS 84 #undef R3_EQ_EPS 85 #undef R3_IS_NORMALIZED 86 #undef TYPE_FLOAT