okami.mom/strata

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

Niko1221Merge f-028-eng-p1: engine P1 fixes77287f13e

main
2.6 KiB43 linesraw
1#include "strata/core/auto_cache.hpp"
2
3#include <cstdio>
4#include <cstdlib>
5
6using namespace strata::core;
7namespace {
8int checks = 0;
9void check(bool ok, const char* label) {
10    ++checks;
11    if (!ok) { std::fprintf(stderr, "FAIL: %s\n", label); std::exit(1); }
12}
13constexpr int64_t MiB = 1ll << 20;
14}
15
16int main() {
17    const int64_t blob = 2560ll * 1024;   // 2.5 MiB
18    const int64_t min_slots = 300;
19    // the A4000 of #4474: 9951 MiB free after the weights, 700 reserved + 184 draft head
20    const int64_t free_b = 9951 * MiB, reserve_b = (700 + 184) * MiB;
21    const AutoCacheSlots a = auto_cache_slots(free_b, reserve_b, blob, min_slots);
22    const AutoCacheSlots plain = auto_cache_slots(free_b, reserve_b, blob, min_slots, 0);
23    check(a.late_priced && !plain.late_priced, "the late allocation is priced by default and off with late_mib 0");
24    check(plain.slots == (free_b - reserve_b) / blob, "late_mib 0 is the old sizing");
25    check(a.slots == (free_b - reserve_b - kLateAllocMib * MiB) / blob, "the cache leaves reserve + late allocation");
26    check(plain.slots - a.slots == kLateAllocMib * MiB / blob || plain.slots - a.slots == kLateAllocMib * MiB / blob + 1,
27          "256 MiB fewer bytes: about 100 slots of 2.5 MiB");
28    // the card ends with the reserve: free - slots*blob - priced - 244 MiB unpriced >= reserve
29    check(free_b - a.slots * blob - (184 + 244) * MiB >= 700 * MiB - blob, "the end free is the reserve, not 241 MiB under it");
30    // a small card: not enough for min_slots after the late allocation -> the old sizing (#496 lowers the reserve elsewhere)
31    const AutoCacheSlots small = auto_cache_slots(1200 * MiB, 700 * MiB, blob, min_slots);
32    check(!small.late_priced && small.slots == (500 * MiB) / blob, "a card that cannot spare it keeps the old number");
33    const AutoCacheSlots tiny = auto_cache_slots(900 * MiB, 700 * MiB, blob, min_slots);
34    check(!tiny.late_priced && tiny.slots == 80, "less than min_slots stays as it was");
35    check(auto_cache_slots(500 * MiB, 700 * MiB, blob, min_slots).slots == 0, "a negative room is no slots");
36    check(auto_cache_slots(1000 * MiB, 700 * MiB, 0, min_slots).slots == 0, "no blob size: no slots, no division");
37    // exactly at the limit: min_slots after the late allocation
38    const int64_t edge_free = reserve_b + (kLateAllocMib * MiB) + min_slots * blob;
39    check(auto_cache_slots(edge_free, reserve_b, blob, min_slots).late_priced, "exactly min_slots after the late allocation: priced");
40    check(!auto_cache_slots(edge_free - 1, reserve_b, blob, min_slots).late_priced, "one byte less: the old sizing");
41    std::printf("auto_cache_test: %d checks passed\n", checks);
42    return 0;
43}