okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge f-027-gh-eng: GitHub engine items for 0.2.76ca60e9a0

main
2.2 KiB49 linesraw
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;
17    if (!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)
23    for (double v : {0.0, 0.25, 0.5, 0.7, 0.9, 0.97, 0.999, 1.0}) {
24        check(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
27    check(adapt_decay_checked(0.7, nullptr) == kAdaptDecayDefault, "the default stays 0.7");
28    // outside: the default
29    for (double v : {1.0000001, 1.5, 2.0, 1e30, -0.1, -1.0, -1e30}) {
30        check(adapt_decay_checked(v, nullptr) == kAdaptDecayDefault, "an out-of-range value falls back to the default");
31    }
32    const double inf = std::numeric_limits<double>::infinity();
33    const double nan = std::numeric_limits<double>::quiet_NaN();
34    check(adapt_decay_checked(inf, nullptr) == kAdaptDecayDefault, "inf falls back");
35    check(adapt_decay_checked(-inf, nullptr) == kAdaptDecayDefault, "-inf falls back");
36    check(adapt_decay_checked(nan, nullptr) == kAdaptDecayDefault, "NaN falls back");
37    // the result is always usable: finite and in 0..1
38    for (double v : {-5.0, 0.3, 7.0, nan, inf}) {
39        const float r = adapt_decay_checked(v, nullptr);
40        check(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)
43    float usage = 1.f;
44    const float d = adapt_decay_checked(1.5, nullptr);
45    for (int i = 0; i < 100000; ++i) usage = usage * d + 1.f;
46    check(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);
48    return 0;
49}