! $Id: 10_jumps.inf 17096 2007-02-20 20:20:48Z paultcochrane $

Include "declare.inf"; ! variable declarations
Include "assert.inf"; ! p/f/pt (ok/not_ok) routines

! ----------------------------------------------------------------------

[ test_jumps i;
   print "# jump^";
   jump j2; ! Using "@jump" with label name crashes
   .j1;
   @print "Bail out! Quitting tests because jump doesn't work!";
   @quit;   
   .j2; p();

   ! Note that some of these jumps are > 63 bytes away, some less,
   ! so we'll test short and long form of branching.
   print "# je^";
   ! TODO test "je sp a b c" to make sure not multi-popping stack, etc.
   @je  5  5 ?~bad; p();
   @je  5 n5 ?bad; p();
   @je n5  5 ?bad; p();
   @je n5 n5 ?~bad; p();
   @je  32767 n32768 ?bad; p();
   @je n32768 n32768 ?~bad; p();
!   @je 5 4 5 ?~bad; p();
!   @je 5 4 3 5 ?~bad; p();
!   @je 5 4 5 3 ?~bad; p();
!   @je 5 4 3 2 ?bad; p();
   
   print "# jg^";
   @jg  5  5 ?bad; p();
   @jg  1  0 ?~bad; p();
   @jg  0  1 ?bad; p();
   @jg n1 n2 ?~bad; p();
   @jg n2 n1 ?bad; p();
   @jg  1 n1 ?~bad; p();
   @jg n1  1 ?bad; p();
   
   print "# jl^";
   @jl  5  5 ?bad; p();
   @jl  1  0 ?bad; p();
   @jl  0  1 ?~bad; p();
   @jl n1 n2 ?bad; p();
   @jl n2 n1 ?~bad; p();
   @jl  1 n1 ?bad; p();
   @jl n1  1 ?~bad; p();
   
   print "# jz^";
   @jz 0 ?~bad; p();
   @jz 1 ?bad; p();
   @jz n4 ?bad; p();

   print "# offsets^";
   i = do_jump_return(0);
   assert0(i, 0, "branch 0");
   i = do_jump_return(1);
   assert0(i, 1, "branch 1");
   rtrue;

.bad;
   print "^bad [", Testnum, "]!^";
   @print "Bail out! Quitting tests because jumps don't work!";
   @quit;   

];

! Test that offset of 0/1 returns instead of branching.
! TODO in theory we should test all jump opcodes to make sure they can
! return false/true
[ do_jump_return i;
   @je i 0 ?~j1;
   @jz 0 ?rfalse;
   return 97;
   .j1;
   @je i 1 ?~j2;
   @jz 0 ?rtrue;
   return 98;
   .j2;
   return 99;
];

!---------------------- MAIN

[ Main;

   Testnum = 0; Passed = 0; Failed = 0; Print_Tests = 0;

   test_jumps();

   print "1..", Testnum,"^";
   @quit;
   .bad_quit;
   print "not ok Quit didn't work!^";
   rtrue;
];

! vim: tw=78 sw=3
