okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
6ca60e9a0
main
1// --adapt-decay validation (include/strata/program/adapt_decay.hpp), CPU only, no model (#1284). 2#include "strata/program/adapt_decay.hpp" 3 4#include <cmath> 5#include <cstdio> 6#include <cstdlib> 7#include <initializer_list> 8#include <limits> 9 10using strata::program::adapt_decay_checked ; 11using strata::program::kAdaptDecayDefault ; 12 13namespace { 14int checks = 0 ; 15void check (bool ok ,const char * label ) { 16++ checks ; 17if (!ok ) { std::fprintf (stderr ,"FAIL: %s\n" ,label ); std::exit (1 ); } 18} 19}// namespace 20 21int main () { 22// inside the range: the value itself, no line (bit for bit what atof gave before, cast to float) 23for (double v : {0.0 ,0.25 ,0.5 ,0.7 ,0.9 ,0.97 ,0.999 ,1.0 }) { 24check (adapt_decay_checked (v ,nullptr )== (float )v ,"a value in 0..1 is kept" ); 25 } 26// every default and tune_knobs value of the engine is unchanged 27check (adapt_decay_checked (0.7 ,nullptr )== kAdaptDecayDefault ,"the default stays 0.7" ); 28// outside: the default 29for (double v : {1.0000001 ,1.5 ,2.0 ,1e30 ,-0.1 ,-1.0 ,-1e30 }) { 30check (adapt_decay_checked (v ,nullptr )== kAdaptDecayDefault ,"an out-of-range value falls back to the default" ); 31 } 32const double inf = std::numeric_limits < double > ::infinity (); 33const double nan = std::numeric_limits < double > ::quiet_NaN (); 34check (adapt_decay_checked (inf ,nullptr )== kAdaptDecayDefault ,"inf falls back" ); 35check (adapt_decay_checked (- inf ,nullptr )== kAdaptDecayDefault ,"-inf falls back" ); 36check (adapt_decay_checked (nan ,nullptr )== kAdaptDecayDefault ,"NaN falls back" ); 37// the result is always usable: finite and in 0..1 38for (double v : {-5.0 ,0.3 ,7.0 ,nan ,inf }) { 39const float r = adapt_decay_checked (v ,nullptr ); 40check (std::isfinite (r )&& r >=0.f && r <=1.f ,"the result is finite and in 0..1" ); 41 } 42// a counts vector driven by an accepted value stays finite over many adaptations (the bug: >1 overflowed to inf) 43float usage = 1.f ; 44const float d = adapt_decay_checked (1.5 ,nullptr ); 45for (int i = 0 ;i < 100000 ;++ i )usage = usage * d + 1.f ; 46check (std::isfinite (usage ),"counts stay finite with the value the engine ends up using" ); 47 std::printf ("adapt_decay_test: %d checks passed\n" ,checks ); 48return 0 ; 49}