okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
536db01af
main
1// read_expert_profile refuses a profile whose header version is not 1, naming the version it found, the one it 2// reads, and the file - before the geometry check, since the fields after the version are only known to be a 3// version 1 layout. A version 1 file still loads. CPU only: no device is touched. 4#include "strata/core/expert_cache.hpp" 5 6#include <cstdint> 7#include <cstdio> 8#include <filesystem> 9#include <string> 10#include <utility> 11#include <vector> 12 13namespace { 14int fails = 0 ; 15void check (bool ok ,const char * what ) { 16if (!ok ) { 17 std::fprintf (stderr ,"FAIL: %s\n" ,what ); 18++ fails ; 19 } 20} 21// overwrite the little-endian uint32 `version` field at byte 4 (after "STRP") in place 22bool set_version (const std::string & path ,uint32_t v ) { 23 std::FILE * f = std::fopen (path .c_str (),"r+b" ); 24if (f == nullptr )return false; 25uint8_t le [4 ]; 26for (int b = 0 ;b < 4 ;++ b )le [b ]= (uint8_t ) (v >> (8 * b )); 27const bool ok = std::fseek (f ,4 ,SEEK_SET )== 0 && std::fwrite (le ,1 ,4 ,f )== 4 ; 28return (std::fclose (f )== 0 )&& ok ; 29} 30bool has (const std::string & s ,const std::string & what ) {return s .find (what )!= std::string::npos ; } 31}// namespace 32 33int main () { 34using Pair = std::pair < int32_t ,int32_t > ; 35const int64_t L = 2 ,E = 3 ; 36const std::vector < Pair > ranked = {{1 ,2 }, {0 ,0 }, {1 ,0 }}; 37const std::filesystem::path dir = std::filesystem::temp_directory_path () /"strata_profile_version_test" ; 38 std::filesystem::create_directories (dir ); 39const std::string path = (dir /"profile.bin" ).string (); 40 std::string err ; 41 std::vector < Pair > back ; 42int64_t slots = 0 ; 43 44// version 1, as --expert-profile-save and tools/make_profile.py write it: loads 45check (strata::core::write_expert_profile (path ,L ,E ,ranked ,err ),"written" ); 46check (strata::core::read_expert_profile (path ,L ,E ,back ,slots ,err )&& back == ranked && slots == 3 , 47"a version 1 profile loads" ); 48 49// the same bytes with the version field at 2: refused, naming 2, 1 and the file 50check (set_version (path ,2 ),"version field rewritten to 2" ); 51err .clear (); 52check (!strata::core::read_expert_profile (path ,L ,E ,back ,slots ,err ),"a version 2 profile is refused" ); 53check (has (err ,"read_expert_profile: " )&& has (err ,path ),"the refusal names the file" ); 54check (has (err ,"version 2" )&& has (err ,"version 1" ),"the refusal names the version found and the one read" ); 55 56// version 0 (a zeroed header) is not version 1 either 57check (set_version (path ,0 ),"version field rewritten to 0" ); 58check (!strata::core::read_expert_profile (path ,L ,E ,back ,slots ,err )&& has (err ,"version 0" ), 59"a version 0 profile is refused by its number" ); 60 61// the version is checked before the geometry: a version 2 file for another model is refused as version 2 62check (set_version (path ,2 ),"version field rewritten to 2 again" ); 63check (!strata::core::read_expert_profile (path ,L + 1 ,E ,back ,slots ,err )&& has (err ,"version 2" ), 64"a version 2 profile of another geometry is refused by its version, not its geometry" ); 65 66// and back to 1: it loads again, so the refusal was the version field and nothing else 67check (set_version (path ,1 ),"version field rewritten to 1" ); 68check (strata::core::read_expert_profile (path ,L ,E ,back ,slots ,err )&& back == ranked ,"version 1 loads again" ); 69 70 std::filesystem::remove_all (dir ); 71if (fails == 0 ) std::puts ("expert_profile_version_test: OK" ); 72return fails == 0 ?0 :1 ; 73}