fast_lexer.unit_test.anubis 5.05 KB

   
read tools/unit_test.anubis

   
define String
   format
     (
       AtEndOfInput a
     )  =
   if a is
     {
       not_at_end_of_input then "not_at_end_of_input", 
       at_end_of_input then "at_end_of_input"
     }.
   
   
define FastLexerOutput
   test_fast_lexer
     (
        List(FastLexerState)        lexer,
        ByteArray                   input, 
        FastLexerLastAccepted       last_accepted,
        Int                         position,         
        Int                         start, 
        Word16                      state
     ) =
   if make_fast_lexer(lexer) is 
     {
       unknown_state(n) then print("fast lexer unknown state: "+to_decimal(n)+"\n"); should_not_happen(ignored(0,0)), 
       too_many_states  then print("too many states.\n"); should_not_happen(ignored(0,0)),
       ok(fl) then with result = fl(input,last_accepted,position,start,state),
         print("Result = "); 
         if result is 
           {
             rejected(w,eoi,s,e) then 
               print("rejected("+to_decimal(w)+","
                                  +format(eoi)+","
                                  +abs_to_decimal(s)+","
                                  +abs_to_decimal(e)
                                  +")\n"),
             accepted(w,eoi,s,e) then 
               print("accepted("+to_decimal(w)+","
                                  +format(eoi)+","
                                  +abs_to_decimal(s)+","
                                  +abs_to_decimal(e)
                                  +")\n"),
             ignored(w,s) then 
               print("ignored("+to_decimal(w)+","
                                  +abs_to_decimal(s)
                                  +")\n")       
           };
         result
     }. 
   

   
   A lexer accepting "g+abu", "g+abuzo", and ignoring blanks
   
define List(FastLexerState)   
   lexer_1 
     =
   [
     /* state 0  */ rejecting([transition('g',1),transition(' ',7)]),
     /* state 1  */ rejecting([transition('a',2),transition('g',1)]),
     /* state 2  */ rejecting([transition('b',3)]),
     /* state 3  */ rejecting([transition('u',4)]),
     /* state 4  */ accepting([transition('z',5)]),
     /* state 5  */ rejecting([transition('o',6)]),
     /* state 6  */ accepting([ ]),
     /* state 7  */ ignoring([ ])
   ]. 
   
   
define One
   fast_lexer_test
     (
       UnitTestContext ut
     ) =
   
   /* starting in state 0, position 0 */
   assertIsSame(ut,
                test_fast_lexer(lexer_1,to_byte_array("gab"),none,0,0,0),
                rejected(3,at_end_of_input,0,3),    "1");
   assertIsSame(ut,
                test_fast_lexer(lexer_1,to_byte_array("gabu"),none,0,0,0),
                accepted(4,at_end_of_input,0,4),    "2");
   assertIsSame(ut,
                test_fast_lexer(lexer_1,to_byte_array("ggggabu"),none,0,0,0),
                accepted(4,at_end_of_input,0,7),    "2bis");
   assertIsSame(ut,
                test_fast_lexer(lexer_1,to_byte_array("gabuz"),none,0,0,0),
                accepted(5,at_end_of_input,0,4),    "3");
   assertIsSame(ut,
                test_fast_lexer(lexer_1,to_byte_array("gmbuz"),none,0,0,0),
                rejected(1,not_at_end_of_input,0,2),"4");
   assertIsSame(ut,
                test_fast_lexer(lexer_1,to_byte_array("gabuzo"),none,0,0,0),
                accepted(6,at_end_of_input,0,6),    "5"); 
   assertIsSame(ut,
                test_fast_lexer(lexer_1,to_byte_array("gabuzobof"),none,0,0,0),
                accepted(6,not_at_end_of_input,0,6),    "6"); 
   assertIsSame(ut,
                test_fast_lexer(lexer_1,to_byte_array("gggggabuzobof"),none,0,0,0),
                accepted(6,not_at_end_of_input,0,10),    "6bis"); 
   assertIsSame(ut,
                test_fast_lexer(lexer_1,to_byte_array("  gggabuzobof"),none,0,0,0),
                accepted(6,not_at_end_of_input,2,10),    "6ter"); 
   assertIsSame(ut,
                test_fast_lexer(lexer_1,to_byte_array("   "),none,0,0,0),
                ignored(7,3),    "7"); 

   unique. 
   

   
   /* restarting from some other state (with or without an already accepted position) */ 
   
   assertIsSame(ut,
                test_fast_lexer(lexer_1,to_byte_array("bu"),none,bol,neol,0,2),
                accepted(4,2,at_end_of_input),    "7"); 
   assertIsSame(ut,
                test_fast_lexer(lexer_1,to_byte_array("gabuzfff"),last(4,4),bol,neol,5,5),
                accepted(4,4,not_at_end_of_input),    "8"); 
   assertIsSame(ut,
                test_fast_lexer(lexer_1,to_byte_array("gabuzomeu"),last(4,4),bol,neol,5,5),
                accepted(6,6,not_at_end_of_input),    "9"); 

   /* testing bol and eol */ 
   assertIsSame(ut,
                test_fast_lexer(lexer_1,to_byte_array("meu"),none,bol,neol,0,0),
                accepted(6,6,not_at_end_of_input),    "9"); 
   
   
   unique. 
   


   
   
   
public define UnitTestSuite 
   make_fast_lexer_test_suite
      =
   ut_suite("predefined.fast_lexer",
     [
       ut_fixture("fast lexer",  fast_lexer_test)
     ]).
   
   
global define One
   fast_lexer_tests
      (List(String) args) = 
   execute_tests([make_fast_lexer_test_suite],args).