Constant Story "ReplaceRecurseTest"; Constant Headline "Not a game.^"; Release 1; #ifdef TARGET_ZCODE; Constant HDR_GAMERELEASE = $02; ! word Constant HDR_GAMESERIAL = $12; ! six ASCII characters #ifnot; Global gg_mainwin; Constant HDR_GLULXVERSION $04; ! long word Constant ROM_GAMERELEASE $34; ! short word Constant ROM_GAMESERIAL $36; ! six ASCII characters #endif; ! TARGET_GLULX Global failures; [ Main; #ifdef TARGET_GLULX; @setiosys 2 0; @push 201; @push 3; @push 0; @push 0; @push 0; @glk $0023 5 gg_mainwin; @push gg_mainwin; @glk $002F 1 0; #endif; ! TARGET_GLULX Banner(); new_line; RunTest(); ]; [ Banner ix; if (Story ~= 0) { #ifdef TARGET_ZCODE; #ifV5; style bold; #Endif; print (string) Story; #ifV5; style roman; #Endif; #ifnot; ! TARGET_GLULX; glk($0086, 3); ! set header style print (string) Story; glk($0086, 0); ! set normal style #Endif; ! TARGET_ } if (Headline ~= 0) print ": ", (string) Headline; #ifdef TARGET_ZCODE; print "Release ", (HDR_GAMERELEASE-->0) & $03ff, " / Serial number "; for (ix=0 : ix<6 : ix++) print (char) HDR_GAMESERIAL->ix; #ifnot; ! TARGET_GLULX; print "Release "; @aloads ROM_GAMERELEASE 0 ix; print ix; print " / Serial number "; for (ix=0 : ix<6 : ix++) print (char) ROM_GAMESERIAL->ix; #Endif; ! TARGET_ print " / Inform v"; inversion; print ", compiler options "; ix = false; #ifdef STRICT_MODE; print "S"; ix++; #Endif; ! STRICT_MODE #ifdef INFIX; print "X"; ix++; #ifnot; #ifdef DEBUG; print "D"; ix++; #Endif; ! DEBUG #Endif; ! INFIX if (~~ix) print "(none)"; new_line; #ifdef TARGET_GLULX; @gestalt 1 0 ix; print "Interpreter version ", ix / $10000, ".", (ix & $FF00) / $100, ".", ix & $FF, " / "; @gestalt 0 0 ix; print "VM ", ix / $10000, ".", (ix & $FF00) / $100, ".", ix & $FF, " / "; ix = HDR_GLULXVERSION-->0; print "game file format ", ix / $10000, ".", (ix & $FF00) / $100, ".", ix & $FF, "^"; #Endif; ! TARGET_GLULX ]; [ check_value val1 val2; print val1; if (val1 ~= val2) { failures++; print " (ERROR, should be ", val2, ")"; } ]; Global testglob; [ RunTest; print "Calls to replaced function:^"; testglob = 0; call0(); call1(); call2(); call3(); print " ...called "; check_value(testglob, 4); print " times.^^"; print "Calls to recursive replaced function:^"; testglob = 0; recurse(4); print " ...called "; check_value(testglob, 5); print " times.^^"; print "Calls to double-replaced function:^"; testglob = 0; tricall0(); tricall1(); tricall2(); tricall3(); tricall4(); print " ...called "; check_value(testglob, 5); print " times.^^"; print "Calls to never-replaced function:^"; testglob = 0; unicall0(); print " ...called "; check_value(testglob, 1); print " times.^"; new_line; if (failures == 0) print "All passed.^"; else print failures, " errors!^"; ]; ! ----------------------------- [ call0; print "Call0: "; origfunc(); ]; Replace origfunc; [ call1; print "Call1: "; origfunc(); ]; [ origfunc; print "Original (ERROR).^"; failures++; ! Should not be called. ]; [ call2; print "Call2: "; origfunc(); ]; [ origfunc; print "Replacement.^"; testglob++; ]; [ call3; print "Call3: "; origfunc(); ]; ! ----------------------------- [ tricall0; print "Tricall0: "; tripfunc(); ]; Replace tripfunc; [ tricall1; print "Tricall1: "; tripfunc(); ]; [ tripfunc; print "Original (ERROR).^"; failures++; ! Should not be called. ]; [ tricall2; print "Tricall2: "; tripfunc(); ]; [ tripfunc; print "Replacement 1 (ERROR).^"; failures++; ! Should not be called. ]; [ tricall3; print "Tricall3: "; tripfunc(); ]; [ tripfunc; print "Replacement 2.^"; testglob++; ]; [ tricall4; print "Tricall4: "; tripfunc(); ]; ! ----------------------------- Replace unifunc; [ unifunc; print "Unreplaced original.^"; testglob++; ]; [ unicall0; print "Unicall0: "; unifunc(); ]; ! ----------------------------- Replace recurse; [ recurse val; print "Recurse orig (ERROR): ", val, "^"; failures++; ! Should not be called. if (val) recurse(val-1); ]; [ recurse val; print "Recurse replacement: ", val, "^"; testglob++; if (val) recurse(val-1); ];