Anonymous avatar Anonymous committed cdb7de7

Improved tests for Cf_lex module and Cf_regex module.

Comments (0)

Files changed (1)

             17, "seventeen";
             19, "nineteen";
         ] in
-        let m = M.of_list m in
+        let m = M.of_list_incr m in
         if nearest_succ 0 m <> (1, "one") then
             failwith "nearest_succ 0";
         if nearest_succ 1 m <> (1, "one") then
     
     module L2 = struct
         let p =
-            let q0 = Cf_scan_parser.scanf "%3u" (fun y -> y) in
-            let q1 = Cf_scan_parser.scanf "%c" (fun y -> y) in
-            let q2 = Cf_scan_parser.scanf "%3u" (fun y -> y) in
+            let q0 = Cf_scan_parser.scanfx "%3u" (fun y -> y) in
+            let q1 = Cf_scan_parser.scanfx "%c" (fun y -> y) in
+            let q2 = Cf_scan_parser.scanfx "%3u" (fun y -> y) in
             q0 >>= fun v0 ->
             q1 >>= fun v1 ->
             q2 >>= fun v2 ->
 end
 
 module T7 = struct
+    open Printf
+    open Cf_lex.Op
+    open Cf_parser.Op
+        
+    module L1 = struct        
+        let test1 lexer =
+            let token0 = "abaabaababbabb" in
+            let input = token0 (* ^ "jhw" *) in
+            let s = Cf_seq.of_substring input 0 in
+            let token, s' =
+                match lexer s with
+                | Some x -> x
+                | None -> failwith "No match! [L1.1]"
+            in
+            if Lazy.force s' <> Cf_seq.Z then
+                failwith "Unexpected trailer [1]!";
+            if token <> token0 then
+                failwith (sprintf "Bad match! [msg='%s']" token)
+        
+        let test2 lexer =
+            let token0 = "abaabaababbabb" in
+            let input = token0 ^ "jhw" in
+            let s = Cf_seq.of_substring input 0 in
+            let token, s' =
+                match lexer s with
+                | Some x -> x
+                | None -> failwith "No match! [L1.2]"
+            in
+            if Lazy.force s' = Cf_seq.Z then
+                failwith "Expected trailer [2]!";
+            if token <> token0 then
+                failwith (sprintf "Bad match! [msg='%s']" token)
+    end
+    
+    module L2 = struct
+        let p =
+            let q0 = Cf_scan_parser.scanf "%3u" (fun y -> y) in
+            let q1 = Cf_scan_parser.scanf "%c" (fun y -> y) in
+            let q2 = Cf_scan_parser.scanf "%3u" (fun y -> y) in
+            q0 >>= fun v0 ->
+            q1 >>= fun v1 ->
+            q2 >>= fun v2 ->
+            ~:(v0, v1, v2)
+                
+        let y s = p (Cf_seq.of_string s)
+        
+        let test () =
+            match y "1234567" with
+            | Some ((123, '4', 567), z) when Lazy.force z = Cf_seq.Z ->
+                ()
+            | Some ((123, '4', 567), z) ->
+                failwith (Cf_seq.to_string z)
+            | Some ((v0, v1, v2), _) ->
+                failwith (Printf.sprintf "%02u, '%c', %02u" v0 v1 v2)
+            | _ ->
+                failwith "No match! [L2]"
+    end
+    
+    module L3 = struct
+        let qlist_ = [
+            "abc", "abc";
+            "(a*b+c?.[`t +?]|d)", "`(a`*b`+c`?`.`[``t `+`?`]`|d`)";
+        ]
+        
+        let loop (a, b) =
+            let s = Cf_flow.commute_string Cf_regex.unquote b in
+            if s <> a then
+                failwith (Printf.sprintf "unquoting \"%s\" <> \"%s\"" s a);
+            let s = Cf_flow.commute_string Cf_regex.quote a in
+            if s <> b then
+                failwith (Printf.sprintf "quoting \"%s\" <> \"%s\"" s b);
+            ()
+        
+        let test () = List.iter loop qlist_
+    end
+    
+    module L4 = struct
+        let lex =
+            Cf_lex.create !@[
+                !$"`s+" $= None;
+                !$"`w+" $> (fun x -> Some x);
+            ]
+
+        let rec loop acc z =
+            match lex z with
+            | None ->
+                Some (List.rev acc, z)
+            | Some (None, z) ->
+                loop acc z
+            | Some (Some tok, z) ->
+                loop (tok :: acc) z
+        
+        let p =
+            loop [] >>= fun arr ->
+            Cf_parser.fin >>= fun () ->
+            ~:arr
+    
+        let input_ = "bim bam boom"
+    
+        let test () =
+            let s = Cf_seq.of_string input_ in
+            match p s with
+            | Some ([ "bim"; "bam"; "boom" ], s)
+              when Lazy.force s = Cf_seq.Z ->
+                ()
+            | _ ->
+                failwith "parse error!"
+    end
+    
+    module L5 = struct
+        let xlist = [
+            "a", [ "a" ], [ "x"; "ax"; "" ];
+
+            "abc", [ "abc" ], [ "xxxx" ];
+
+            "[abc]+", [ "abccabc" ], [ "abcdabcd" ];
+            
+            "ab+c*d?e", [ "abe"; "abbcde" ], [ "ae"; "acde"; "xxxx" ];
+            
+            "a(bc)+d", [ "abcd"; "abcbcd" ], [ "abc"; "abcabc"; "abcdd" ];
+
+            "(a|b)*abb", [ "abababb"; "ababbabb" ], [ "ababab"; "cde" ];
+            
+            "[_`a][_`i]*", [ "id0"; "_"; "long_id" ], [ "0"; "" ];
+            
+            "[ `t]*`w+", [ "foo"; "\t  foo" ], [ "\n"; " "; "" ];
+        ]
+        
+        let yes_ s x v =
+            if not (Cf_regex.test x v) then
+                jout#fail "Expected '%s' to match expression '%s'." v s
+        
+        let no_ s x v =
+            if Cf_regex.test x v then
+                jout#fail "Expected '%s' to match expression '%s'." v s
+        
+        let test1 (s, yes, no) =
+            let x = Cf_regex.of_string s in
+            List.iter (yes_ s x) yes;
+            List.iter (no_ s x) no
+        
+        let test () =
+            List.iter test1 xlist
+    end
+
+    let test () =
+        let lexer =
+            Cf_lex.create begin
+                (!*(!:'a' $| !:'b')) $& !$"abb" $> (fun x -> x)
+            end
+        in
+        L1.test1 lexer;
+        L1.test2 lexer;
+        L2.test ();
+        L3.test ();
+        L4.test ();
+        L5.test ()
+end
+
+module T8 = struct
     open Cf_parser.Op
     
     let digit =
         List.iter t_pos positive
 end
 
-module T8 = struct
+module T9 = struct
     module L1 = struct
         module C = Cf_cmonad
         open C.Op
         assert true
 end
 
-module T9 = struct
+module T10 = struct
     (*
     let rec logflow w =
         match Lazy.force w with
         if not (Cf_seq.equal x o) then failwith "Transform failed!"
 end
 
-module T10 = struct
+module T11 = struct
     module V6 = Cf_ip6_addr
     open Printf
     
         v6_unicast_test ()
 end
 
-module T11 = struct
+module T12 = struct
     let len = 100
 
     let test () =
                 (Unix.error_message e) fn)
 end
 
-module T12 = struct
+module T13 = struct
     let len = 100
 
     let test () =
                 (Unix.error_message e) fn)
 end
 
-module T13 = struct
+module T14 = struct
     open Cf_uri
     
     module L1 = struct
         L2.test ();
 end
 
-module T14 = struct
+module T15 = struct
 (*
 3fffe33e1b840309 365  Sun -1000000-12-31 23:59:59 +0000
 3fffe33e1b84030a 000  Mon -999999-01-01 00:00:00 +0000
     let test () = List.iter L1.y L1.x
 end
 
-module T15 = struct
+module T16 = struct
     open Cf_poll
     
     class clock n dt =
         ignore (Unix.sigprocmask Unix.SIG_SETMASK save)
 end
 
-module T16 = struct
+module T17 = struct
     open Cf_scmonad.Op
     
     module String_set = Cf_rbtree.Set(String)
         if s2 <> s2' then failwith "Error in uniq!"
 end
 
-module T17 = struct
+module T18 = struct
     open Cf_gadget
     open Cf_cmonad.Op
     
         T1.test; T2.test; T3.test; T4.test; T5.test;
         T6.test; T7.test; T8.test; T9.test; T10.test;
         T11.test; T12.test; T13.test; T14.test; T15.test;
-        T16.test; T17.test
+        T16.test; T17.test; T18.test
     ] in
     Printf.printf "1..%d\n" (List.length tests);
     flush stdout;
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.