okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
ef8071303
main
1#include "strata/core/conversation_cache.hpp" 2#include <cstdlib> 3#include <cstdio> 4#include <new> 5static long fail_after = -1 ; 6void * operatornew (std::size_t n ) { 7if (fail_after == 0 )throw std::bad_alloc (); 8if (fail_after > 0 )-- fail_after ; 9if (void * p = std::malloc (n ?n :1 ))return p ; 10throw std::bad_alloc (); 11} 12void operatordelete (void * p )noexcept { std::free (p ); } 13void operatordelete (void * p ,std::size_t )noexcept { std::free (p ); } 14using strata::core::ConversationCheckpoint ; 15bool equal (const ConversationCheckpoint & a ,const ConversationCheckpoint & b ) { 16if (a .ids != b .ids || a .imgs != b .imgs || a .gdn != b .gdn || a .ple != b .ple || a .tails != b .tails || a .dead != b .dead || a .block_pos != b .block_pos || a .used != b .used || a .stage_parts .size ()!= b .stage_parts .size ())return false; 17for (size_t i = 0 ;i < a .stage_parts .size ();++ i )if (!equal (a .stage_parts [i ],b .stage_parts [i ]))return false; 18return true; 19} 20int main () { 21// #752: every stage count a layer split runs (1-3), with one checkpoint that has no stage parts (it stays in `rest`) 22long total_faults = 0 ; 23for (size_t stages = 1 ;stages <=3 ;++ stages ) { 24int faults = 0 ; 25bool finished = false; 26for (int n = 0 ;n < 256 && !finished ;++ n ) { 27 std::vector < ConversationCheckpoint > checks (4 ); 28for (size_t i = 0 ;i < checks .size ();++ i ) { 29auto & c = checks [i ];c .ids = {1 ,2 ,(int )i + 3 };c .gdn = {1 ,2 ,3 };c .ple = {4 };c .used = i ; 30if (i == 2 )continue ;// incomplete: no stage parts 31c .stage_parts .resize (stages ); 32for (size_t k = 0 ;k < stages ;++ k ) {c .stage_parts [k ].gdn = {5 ,6 ,static_cast < uint8_t > (7 + k )};c .stage_parts [k ].tails = {static_cast < uint8_t > (k )}; } 33 } 34const auto backup = checks ; 35fail_after = n ; 36try { 37auto split = strata::core::conversation_checkpoints_split (std::move (checks ),stages ); 38fail_after = 0 ;// Merge-back after a successful split must not allocate. 39if (!strata::core::conversation_checkpoints_merge (std::move (split ),checks ))return 2 ; 40fail_after = -1 ; 41if (checks .size ()!= backup .size ())return 6 ; 42finished = true;// the split went through: the merge gives the running state back, in order 43for (size_t i = 0 ;i < checks .size ();++ i ) { 44const auto & x = checks [i ];const auto & y = backup [i ]; 45// (each stage part's identity is its parent's by design, so only the state is compared) 46if (x .ids != y .ids || x .gdn != y .gdn || x .ple != y .ple || x .used != y .used || x .stage_parts .size ()!= y .stage_parts .size ())return 7 ; 47for (size_t k = 0 ;k < x .stage_parts .size ();++ k )if (x .stage_parts [k ].gdn != y .stage_parts [k ].gdn || x .stage_parts [k ].tails != y .stage_parts [k ].tails )return 8 ; 48 } 49 }catch (const std::bad_alloc & ) { 50fail_after = -1 ;++ faults ; 51if (checks .size ()!= backup .size ())return 4 ; 52for (size_t i = 0 ;i < checks .size ();++ i )if (!equal (checks [i ],backup [i ])) { 53 std::fprintf (stderr ,"stages %zu: allocation failure %d partially moved checkpoint %zu\n" ,stages ,n ,i );return 1 ; 54 } 55 } 56 } 57if (!finished || faults == 0 )return 5 ; 58 std::printf ("stages %zu: allocation-failure cases preserved: %d\n" ,stages ,faults ); 59total_faults += faults ; 60 } 61return total_faults > 0 ?0 :3 ; 62}