summaryrefslogtreecommitdiffstats
path: root/test/Moretest
diff options
context:
space:
mode:
authorPierre Weis <Pierre.Weis@inria.fr>2008-09-09 06:39:26 +0000
committerPierre Weis <Pierre.Weis@inria.fr>2008-09-09 06:39:26 +0000
commit5631731def9e44cc6c1e14ff9cacf514db917af4 (patch)
treea1b418df33c1a0e6c25d1bdf559521b09d7c8afe /test/Moretest
parent5db36dd331934ae9bf48484f2b078c66a099c271 (diff)
Semantics of %n has changed.
git-svn-id: http://caml.inria.fr/svn/ocaml/trunk@9000 f963ae5c-01c2-4b8c-9fe0-0dff7051ff02
Diffstat (limited to 'test/Moretest')
-rw-r--r--test/Moretest/tscanf.ml406
1 files changed, 270 insertions, 136 deletions
diff --git a/test/Moretest/tscanf.ml b/test/Moretest/tscanf.ml
index 13e44787d..24bf40fac 100644
--- a/test/Moretest/tscanf.ml
+++ b/test/Moretest/tscanf.ml
@@ -18,9 +18,11 @@ let test0 () =
(sscanf " " " " id) 3 +
(sscanf "\t" " " id) 4 +
(sscanf "\n" " " id) 5 +
- (sscanf "\n\t 6" " %d" id);;
+ (sscanf "\n\t 6" " %d" id)
+;;
-test (test0 () = 21);;
+test (test0 () = 21)
+;;
(* Testing integer scanning %i and %d. *)
let test1 () =
@@ -28,24 +30,30 @@ let test1 () =
sscanf " 2" " %d" id +
sscanf " -2" " %d" id +
sscanf " +2" " %d" id +
- sscanf " 2a " " %da" id;;
+ sscanf " 2a " " %da" id
+;;
-test (test1 () = 5);;
+test (test1 () = 5)
+;;
let test2 () =
sscanf "123" "%2i" id +
sscanf "245" "%d" id +
- sscanf " 2a " " %1da" id;;
+ sscanf " 2a " " %1da" id
+;;
-test (test2 () = 259);;
+test (test2 () = 259)
+;;
let test3 () =
sscanf "0xff" "%3i" id +
sscanf "0XEF" "%3i" id +
sscanf "x=-245" " x = %d" id +
- sscanf " 2a " " %1da" id;;
+ sscanf " 2a " " %1da" id
+;;
-test (test3 () = -214);;
+test (test3 () = -214)
+;;
(* Testing float scanning. *)
(* f style. *)
@@ -79,8 +87,10 @@ let test4 () =
bscanf (Scanning.from_string "+000.113")
"%15f" (fun b0 -> b0 = 0.113) &&
bscanf (Scanning.from_string "-000.113")
- "%15f" (fun b0 -> b0 = -0.113);;
-test (test4 ());;
+ "%15f" (fun b0 -> b0 = -0.113)
+;;
+test (test4 ())
+;;
(* e style. *)
let test5 () =
@@ -99,9 +109,11 @@ let test5 () =
bscanf (Scanning.from_string "1 1.1 0e+1 1.3e-1")
"%g %g %g %g"
(fun b1 b2 b3 b4 ->
- b1 = 1.0 && b2 = 1.1 && b3 = 0.0 && b4 = 0.13);;
+ b1 = 1.0 && b2 = 1.1 && b3 = 0.0 && b4 = 0.13)
+;;
-test (test5 ());;
+test (test5 ())
+;;
(* Testing boolean scanning. *)
let test6 () =
@@ -114,9 +126,11 @@ let test6 () =
bscanf (Scanning.from_string "falsefalse") "%B%B"
(fun b1 b2 -> (b1, b2) = (false, false)) &&
bscanf (Scanning.from_string "true false") "%B %B"
- (fun b1 b2 -> (b1, b2) = (true, false));;
+ (fun b1 b2 -> (b1, b2) = (true, false))
+;;
-test (test6 ());;
+test (test6 ())
+;;
(* Testing char scanning. *)
@@ -130,28 +144,34 @@ let test7 () =
bscanf (Scanning.from_string "a \n \t \000 \032b")
"%c %c %c "
(fun c1 c2 c3 ->
- c1 = 'a' && c2 = '\000' && c3 = 'b');;
+ c1 = 'a' && c2 = '\000' && c3 = 'b')
+;;
-test (test7 ());;
+test (test7 ())
+;;
let verify_read c =
let s = Printf.sprintf "%C" c in
let ib = Scanning.from_string s in
- assert (bscanf ib "%C" id = c);;
+ assert (bscanf ib "%C" id = c)
+;;
let verify_scan_Chars () =
- for i = 0 to 255 do verify_read (char_of_int i) done;;
+ for i = 0 to 255 do verify_read (char_of_int i) done
+;;
let test8 () = verify_scan_Chars () = ();;
-test (test8 ());;
+test (test8 ())
+;;
(* Testing string scanning. *)
(* %S and %s styles. *)
let unit fmt s =
let ib = Scanning.from_string (Printf.sprintf "%S" s) in
- Scanf.bscanf ib fmt id;;
+ Scanf.bscanf ib fmt id
+;;
let test_fmt fmt s = unit fmt s = s;;
@@ -170,18 +190,22 @@ let test9 () =
\\\n\
b \\\n\
c\010\\\n\
- b";;
+ b"
+;;
-test (test9 ());;
+test (test9 ())
+;;
let test10 () =
let res =
sscanf "Une cha�ne: \"celle-ci\" et \"celle-l�\"!"
"%s %s %S %s %S %s"
(fun s1 s2 s3 s4 s5 s6 -> s1 ^ s2 ^ s3 ^ s4 ^ s5 ^ s6) in
- res = "Unecha�ne:celle-cietcelle-l�!";;
+ res = "Unecha�ne:celle-cietcelle-l�!"
+;;
-test (test10 ());;
+test (test10 ())
+;;
(* %[] style *)
let test11 () =
@@ -195,7 +219,8 @@ let test11 () =
&&
sscanf "Daniel de Rauglaudre 66" "%s@\t %s@\t %d"
(fun prenom nom poids ->
- prenom = "Daniel" && nom = "de Rauglaudre" && poids = 66);;
+ prenom = "Daniel" && nom = "de Rauglaudre" && poids = 66)
+;;
(* Empty string (end of input) testing. *)
let test110 () =
@@ -213,11 +238,13 @@ let test110 () =
sscanf " " " %s %s" (fun x y -> x = "" && x = y) &&
sscanf " " " %s@ %s" (fun x y -> x = "" && x = y) &&
sscanf " poi !" " %s@ %s@." (fun x y -> x = "poi" && y = "!") &&
- sscanf " poi !" "%s@ %s@." (fun x y -> x = "" && y = "poi !");;
+ sscanf " poi !" "%s@ %s@." (fun x y -> x = "" && y = "poi !")
+;;
let test111 () = sscanf "" "%[^\n]@\n" (fun x -> x = "");;
-test (test11 () && test110 () && test111 ());;
+test (test11 () && test110 () && test111 ())
+;;
(* Scanning lists. *)
let ib () = Scanning.from_string "[1;2;3;4; ]";;
@@ -225,38 +252,43 @@ let ib () = Scanning.from_string "[1;2;3;4; ]";;
(* Statically known lists can be scanned directly. *)
let f ib =
bscanf ib " [" ();
- bscanf ib " %i ;" (fun i ->
- bscanf ib " %i ;" (fun j ->
- bscanf ib " %i ;" (fun k ->
- bscanf ib " %i ;" (fun l ->
+ bscanf ib " %i;" (fun i ->
+ bscanf ib " %i;" (fun j ->
+ bscanf ib " %i;" (fun k ->
+ bscanf ib " %i;" (fun l ->
bscanf ib " ]" ();
[i; j; k; l]))));;
let test12 () = f (ib ()) = [1; 2; 3; 4];;
-test (test12 ());;
+test (test12 ())
+;;
(* A general list scanner that always fails to succeed. *)
let rec scan_elems ib accu =
- try bscanf ib " %i ;" (fun i -> scan_elems ib (i :: accu)) with
- | _ -> accu;;
+ try bscanf ib " %i;" (fun i -> scan_elems ib (i :: accu)) with
+ | _ -> accu
+;;
let g ib = bscanf ib "[ " (); List.rev (scan_elems ib []);;
let test13 () = g (ib ()) = [1; 2; 3; 4];;
-test (test13 ());;
+test (test13 ())
+;;
(* A general int list scanner. *)
let rec scan_int_list ib =
bscanf ib "[ " ();
let accu = scan_elems ib [] in
bscanf ib " ]" ();
- List.rev accu;;
+ List.rev accu
+;;
let test14 () = scan_int_list (ib ()) = [1; 2; 3; 4];;
-test (test14 ());;
+test (test14 ())
+;;
(* A general list scanner that always succeeds. *)
let rec scan_elems ib accu =
@@ -264,16 +296,19 @@ let rec scan_elems ib accu =
(fun i -> function
| ';' -> scan_elems ib (i :: accu)
| ']' -> List.rev (i :: accu)
- | c -> failwith "scan_elems");;
+ | c -> failwith "scan_elems")
+;;
let rec scan_int_list ib =
bscanf ib "[ " ();
- scan_elems ib [];;
+ scan_elems ib []
+;;
let test15 () =
scan_int_list (Scanning.from_string "[1;2;3;4]") = [1; 2; 3; 4];;
-test (test15 ());;
+test (test15 ())
+;;
let rec scan_elems ib accu =
try
@@ -286,7 +321,8 @@ let rec scan_elems ib accu =
| c -> prerr_endline (String.make 1 c); failwith "scan_elems")
with
| Scan_failure _ -> bscanf ib "]" (); accu
- | End_of_file -> accu;;
+ | End_of_file -> accu
+;;
let scan_int_list ib = scan_elems ib [];;
@@ -297,7 +333,8 @@ let test16 () =
(* Should fail but succeeds! *)
scan_int_list (Scanning.from_string "[1;2;3;4") = List.rev [1;2;3;4];;
-test (test16 ());;
+test (test16 ())
+;;
let rec scan_elems ib accu =
bscanf ib " %i%[]; \t\n\r]"
@@ -305,11 +342,13 @@ let rec scan_elems ib accu =
match s with
| ";" -> scan_elems ib (i :: accu)
| "]" -> List.rev (i :: accu)
- | s -> List.rev (i :: accu));;
+ | s -> List.rev (i :: accu))
+;;
let scan_int_list ib =
bscanf ib " [" ();
- scan_elems ib [];;
+ scan_elems ib []
+;;
let test17 () =
scan_int_list (Scanning.from_string "[1;2;3;4]") = [1;2;3;4] &&
@@ -317,7 +356,8 @@ let test17 () =
(* Should fail but succeeds! *)
scan_int_list (Scanning.from_string "[1;2;3;4 5]") = [1;2;3;4];;
-test (test17 ());;
+test (test17 ())
+;;
let rec scan_elems ib accu =
bscanf ib " %c " (fun c ->
@@ -345,7 +385,8 @@ and scan_rest ib accu =
bscanf ib " %i " (fun i ->
scan_rest ib (i :: accu)))
| ']' -> accu
- | _ -> failwith "scan_rest");;
+ | _ -> failwith "scan_rest")
+;;
let scan_int_list ib = List.rev (scan_elems ib []);;
@@ -355,28 +396,33 @@ let test18 () =
scan_int_list (Scanning.from_string "[1;2;3;4]") = [1;2;3;4] &&
scan_int_list (Scanning.from_string "[1;2;3;4; ]") = [1;2;3;4];;
-test (test18 ());;
+test (test18 ())
+;;
(* Those properly fail *)
let test19 () =
failure_test
scan_int_list (Scanning.from_string "[1;2;3;4 5]")
- "scan_rest";;
+ "scan_rest"
+;;
-(test19 ());;
+(test19 ())
+;;
let test20 () =
- scan_failure_test
- scan_int_list (Scanning.from_string "[1;2;3;4; ; 5]");;
+ scan_failure_test
+ scan_int_list (Scanning.from_string "[1;2;3;4;; 5]");;
-(test20 ());;
+(test20 ())
+;;
let test21 () =
scan_failure_test
scan_int_list (Scanning.from_string "[1;2;3;4;;");;
-(test21 ());;
+(test21 ())
+;;
let rec scan_elems ib accu =
bscanf ib "%1[];]" (function
@@ -394,11 +440,13 @@ and scan_rest ib accu =
| _ -> scan_elem ib accu)
and scan_elem ib accu =
- bscanf ib " %i " (fun i -> scan_elems ib (i :: accu));;
+ bscanf ib " %i " (fun i -> scan_elems ib (i :: accu))
+;;
let scan_int_list ib =
bscanf ib " [ " ();
- List.rev (scan_rest ib []);;
+ List.rev (scan_rest ib [])
+;;
let test22 () =
scan_int_list (Scanning.from_string "[]") = [] &&
@@ -407,41 +455,46 @@ let test22 () =
scan_int_list (Scanning.from_string "[1;2;3;4]") = [1;2;3;4] &&
scan_int_list (Scanning.from_string "[1;2;3;4;]") = [1;2;3;4];;
-test (test22 ());;
+test (test22 ())
+;;
(* Should work and does not with this version of scan_int_list!
scan_int_list (Scanning.from_string "[1;2;3;4; ]");;
(* Should lead to a bad input error. *)
scan_int_list (Scanning.from_string "[1;2;3;4 5]");;
scan_int_list (Scanning.from_string "[1;2;3;4;;");;
-scan_int_list (Scanning.from_string "[1;2;3;4; ; 5]");;
+scan_int_list (Scanning.from_string "[1;2;3;4;; 5]");;
scan_int_list (Scanning.from_string "[1;2;3;4;; 23]");;
*)
let rec scan_elems ib accu =
try bscanf ib " %i %1[;]" (fun i s ->
if s = "" then i :: accu else scan_elems ib (i :: accu)) with
- | Scan_failure _ -> accu;;
+ | Scan_failure _ -> accu
+;;
(* The general int list scanner. *)
let rec scan_int_list ib =
bscanf ib "[ " ();
let accu = scan_elems ib [] in
bscanf ib " ]" ();
- List.rev accu;;
+ List.rev accu
+;;
(* The general HO list scanner. *)
let rec scan_elems ib scan_elem accu =
try scan_elem ib (fun i s ->
let accu = i :: accu in
if s = "" then accu else scan_elems ib scan_elem accu) with
- | Scan_failure _ -> accu;;
+ | Scan_failure _ -> accu
+;;
let scan_list scan_elem ib =
bscanf ib "[ " ();
let accu = scan_elems ib scan_elem [] in
bscanf ib " ]" ();
- List.rev accu;;
+ List.rev accu
+;;
(* Deriving particular list scanners from the HO list scanner. *)
let scan_int_elem ib = bscanf ib " %i %1[;]";;
@@ -454,21 +507,23 @@ let test23 () =
scan_int_list (Scanning.from_string "[1;2;3;4]") = [1;2;3;4] &&
scan_int_list (Scanning.from_string "[1;2;3;4;]") = [1;2;3;4];;
-test (test23 ());;
+test (test23 ())
+;;
let test24 () =
scan_failure_test scan_int_list (Scanning.from_string "[1;2;3;4 5]")
and test25 () =
scan_failure_test scan_int_list (Scanning.from_string "[1;2;3;4;;")
and test26 () =
- scan_failure_test scan_int_list (Scanning.from_string "[1;2;3;4; ; 5]")
+ scan_failure_test scan_int_list (Scanning.from_string "[1;2;3;4;; 5]")
and test27 () =
scan_failure_test scan_int_list (Scanning.from_string "[1;2;3;4;; 23]");;
(test24 ()) &&
(test25 ()) &&
(test26 ()) &&
- (test27 ());;
+ (test27 ())
+;;
(* To scan a Caml string:
the format is "\"%s@\"".
@@ -499,7 +554,8 @@ let test28 () =
(Scanning.from_string "[\"Le\";\"langage\";\"Objective\";\"Caml\"; ]") =
["Le"; "langage"; "Objective"; "Caml"];;
-test (test28 ());;
+test (test28 ())
+;;
(* The general HO list scanner with continuations. *)
let rec scan_elems ib scan_elem accu =
@@ -507,13 +563,15 @@ let rec scan_elems ib scan_elem accu =
(fun i s ->
let accu = i :: accu in
if s = "" then accu else scan_elems ib scan_elem accu)
- (fun ib exc -> accu);;
+ (fun ib exc -> accu)
+;;
let scan_list scan_elem ib =
bscanf ib "[ " ();
let accu = scan_elems ib scan_elem [] in
bscanf ib " ]" ();
- List.rev accu;;
+ List.rev accu
+;;
(* Deriving particular list scanners from the HO list scanner. *)
let scan_int_elem ib f ek = kscanf ib ek " %i %1[;]" f;;
@@ -526,7 +584,8 @@ let test29 () =
scan_int_list (Scanning.from_string "[1;2;3;4]") = [1;2;3;4] &&
scan_int_list (Scanning.from_string "[1;2;3;4;]") = [1;2;3;4];;
-test (test29 ());;
+test (test29 ())
+;;
let scan_string_elem ib f ek = kscanf ib ek " %S %1[;]" f;;
let scan_string_list = scan_list scan_string_elem;;
@@ -542,7 +601,8 @@ let test30 () =
(Scanning.from_string "[\"1\"; \"2\"; \"3\"; \"4\";]") =
["1"; "2"; "3"; "4"];;
-test (test30 ());;
+test (test30 ())
+;;
(* A generic scan_elem, *)
let scan_elem fmt ib f ek = kscanf ib ek fmt f;;
@@ -562,13 +622,15 @@ let rec scan_elems ib scan_elem accu =
(fun ib exc -> accu)
" %1[;]"
(fun s -> if s = "" then accu else scan_elems ib scan_elem accu))
- (fun ib exc -> accu);;
+ (fun ib exc -> accu)
+;;
let scan_list scan_elem ib =
bscanf ib "[ " ();
let accu = scan_elems ib scan_elem [] in
bscanf ib " ]" ();
- List.rev accu;;
+ List.rev accu
+;;
let scan_int_list = scan_list (scan_elem " %i");;
let scan_string_list = scan_list (scan_elem " %S");;
@@ -583,7 +645,8 @@ let test31 () =
scan_int_list (Scanning.from_string "[1;2;3;4]") = [1;2;3;4] &&
scan_int_list (Scanning.from_string "[1;2;3;4;]") = [1;2;3;4];;
-test (test31 ());;
+test (test31 ())
+;;
let test32 () =
scan_string_list (Scanning.from_string "[]") = [] &&
@@ -596,7 +659,8 @@ let test32 () =
(Scanning.from_string "[\"1\"; \"2\"; \"3\"; \"4\";]") =
["1"; "2"; "3"; "4"];;
-test (test32 ());;
+test (test32 ())
+;;
(* Using kscanf only.
Using formats as ``functional'' specifications to scan elements of
@@ -611,13 +675,15 @@ let rec scan_elems ib scan_elem_fmt accu =
(function
| "" -> accu
| _ -> scan_elems ib scan_elem_fmt accu)
- );;
+ )
+;;
let scan_list scan_elem_fmt ib =
bscanf ib "[ " ();
let accu = scan_elems ib scan_elem_fmt [] in
bscanf ib " ]" ();
- List.rev accu;;
+ List.rev accu
+;;
let scan_int_list = scan_list "%i";;
let scan_string_list = scan_list "%S";;
@@ -629,10 +695,11 @@ let test33 () =
scan_int_list (Scanning.from_string "[]") = [] &&
scan_int_list (Scanning.from_string "[ ]") = [] &&
scan_int_list (Scanning.from_string "[ 1 ]") = [1] &&
- scan_int_list (Scanning.from_string "[ 1 ; 2 ; 3 ; 4 ]") = [1; 2; 3; 4] &&
- scan_int_list (Scanning.from_string "[1 ;2 ;3 ;4;]") = [1; 2; 3; 4];;
+ scan_int_list (Scanning.from_string "[ 1; 2; 3; 4 ]") = [1; 2; 3; 4] &&
+ scan_int_list (Scanning.from_string "[1;2;3;4;]") = [1; 2; 3; 4];;
-test (test33 ());;
+test (test33 ())
+;;
let test34 () =
scan_string_list (Scanning.from_string "[]") = [] &&
@@ -655,13 +722,15 @@ let rec scan_elems ib scan_elem accu =
" %1[;] "
(function
| "" -> accu
- | _ -> scan_elems ib scan_elem accu));;
+ | _ -> scan_elems ib scan_elem accu))
+;;
let scan_list scan_elem ib =
bscanf ib "[ " ();
let accu = scan_elems ib scan_elem [] in
bscanf ib " ]" ();
- List.rev accu;;
+ List.rev accu
+;;
let scan_float ib = Scanf.bscanf ib "%f";;
@@ -674,20 +743,24 @@ let scan_float_list = scan_list scan_float;;
(* Scanning list of lists of floats. *)
let scan_float_list_list =
scan_list
- (fun ib k -> k (scan_list (fun ib -> Scanf.bscanf ib "%f") ib));;
+ (fun ib k -> k (scan_list (fun ib -> Scanf.bscanf ib "%f") ib))
+;;
let scan_float_list_list =
scan_list
- (fun ib k -> k (scan_list scan_float ib));;
+ (fun ib k -> k (scan_list scan_float ib))
+;;
let scan_float_list_list =
scan_list
- (fun ib k -> k (scan_float_list ib));;
+ (fun ib k -> k (scan_float_list ib))
+;;
(* A general scan_list_list functional. *)
let scan_list_list scan_elems ib =
scan_list
- (fun ib k -> k (scan_elems ib)) ib;;
+ (fun ib k -> k (scan_elems ib)) ib
+;;
let scan_float_list_list = scan_list_list scan_float_list;;
@@ -696,33 +769,40 @@ let scan_float_item ib k = k (scan_float ib (fun x -> x));;
let scan_float_list ib k = k (scan_list scan_float_item ib);;
let scan_float_list_list ib k = k (scan_list scan_float_list ib);;
-test (test34 ());;
+test (test34 ())
+;;
(* Testing the %N format. *)
let test35 () =
sscanf "" "%N" (fun x -> x) = 0 &&
sscanf "456" "%N" (fun x -> x) = 0 &&
sscanf "456" "%d%N" (fun x y -> x, y) = (456, 1) &&
- sscanf " " "%N%s%N" (fun x s y -> x, s, y) = (0, "", 1);;
+ sscanf " " "%N%s%N" (fun x s y -> x, s, y) = (0, "", 1)
+;;
-test (test35 ());;
+test (test35 ())
+;;
(* Testing the %n format. *)
let test36 () =
sscanf "" "%n" (fun x -> x) = 0 &&
sscanf "456" "%n" (fun x -> x) = 0 &&
sscanf "456" "%d%n" (fun x y -> x, y) = (456, 3) &&
- sscanf " " "%n%s%n" (fun x s y -> x, s, y) = (0, "", 1);;
+ sscanf " " "%n%s%n" (fun x s y -> x, s, y) = (0, "", 0)
+;;
-test (test36 ());;
+test (test36 ())
+;;
(* Weird tests to empty strings or formats. *)
let test37 () =
sscanf "" "" true &&
sscanf "" "" (fun x -> x) 1 = 1 &&
- sscanf "123" "" (fun x -> x) 1 = 1;;
+ sscanf "123" "" (fun x -> x) 1 = 1
+;;
-test (test37 ());;
+test (test37 ())
+;;
(* Testing end of input condition. *)
let test38 () =
@@ -733,9 +813,11 @@ let test38 () =
sscanf "" "%!" true &&
sscanf " " " %!" true &&
sscanf "" " %!" true &&
- sscanf "" " %!%!" true;;
+ sscanf "" " %!%!" true
+;;
-test (test38 ());;
+test (test38 ())
+;;
(* Weird tests on empty buffers. *)
let test39 () =
@@ -747,24 +829,30 @@ let test39 () =
is_empty_buff ib &&
(* Do it twice since testing empty buff could incorrectly
thraw an exception or wrongly change the beginning_of_input condition. *)
- is_empty_buff ib;;
+ is_empty_buff ib
+;;
-test (test39 ());;
+test (test39 ())
+;;
(* Testing ranges. *)
let test40 () =
let s = "cba" in
let ib = Scanning.from_string s in
- bscanf ib "%[^ab]%s%!" (fun s1 s2 -> s1 = "c" && s2 = "ba");;
+ bscanf ib "%[^ab]%s%!" (fun s1 s2 -> s1 = "c" && s2 = "ba")
+;;
-test (test40 ());;
+test (test40 ())
+;;
let test41 () =
let s = "cba" in
let ib = Scanning.from_string s in
- bscanf ib "%[^abc]%[cba]%!" (fun s1 s2 -> s1 = "" && s2 = "cba");;
+ bscanf ib "%[^abc]%[cba]%!" (fun s1 s2 -> s1 = "" && s2 = "cba")
+;;
-test (test41 ());;
+test (test41 ())
+;;
let test42 () =
let s = "defcbaaghi" in
@@ -772,28 +860,34 @@ let test42 () =
bscanf ib "%[^abc]%[abc]%s%!" (fun s1 s2 s3 ->
s1 = "def" && s2 = "cbaa" && s3 = "ghi") &&
let ib = Scanning.from_string s in
- bscanf ib "%s@\t" (fun s -> s = "defcbaaghi");;
+ bscanf ib "%s@\t" (fun s -> s = "defcbaaghi")
+;;
-test (test42 ());;
+test (test42 ())
+;;
(* Testing end of file condition (bug found). *)
let test43, test44 =
let s = "" in
let ib = Scanning.from_string s in
(fun () -> bscanf ib "%i%!" (fun i -> i)),
- (fun () -> bscanf ib "%!%i" (fun i -> i));;
+ (fun () -> bscanf ib "%!%i" (fun i -> i))
+;;
test_raises_this_exc End_of_file test43 () &&
-test_raises_this_exc End_of_file test44 ();;
+test_raises_this_exc End_of_file test44 ()
+;;
(* Testing small range scanning (bug found once). *)
let test45 () =
let s = "12.2" in
let ib = Scanning.from_string s in
bscanf ib "%[0-9].%[0-9]%s%!" (fun s1 s2 s3 ->
- s1 = "12" && s2 = "2" && s3 = "");;
+ s1 = "12" && s2 = "2" && s3 = "")
+;;
-test (test45 ());;
+test (test45 ())
+;;
(* Testing printing of meta formats. *)
@@ -803,10 +897,13 @@ let test46, test47 =
1 "spells one, %s" "in english"),
(fun () ->
Printf.sprintf "%i ,%{%s%}, %s."
- 1 "spells one %s" "in english");;
+ 1 "spells one %s" "in english")
+;;
-test (test46 () = "1 spells one, in english.");;
-test (test47 () = "1 ,%s, in english.");;
+test (test46 () = "1 spells one, in english.")
+;;
+test (test47 () = "1 ,%s, in english.")
+;;
(* Testing scanning of meta formats. *)
let test48 () =
@@ -821,9 +918,11 @@ let test48 () =
(* Complex test of scanning a meta format specified in the scanner input
format string and extraction of its specification from a string. *)
sscanf "12 \"%i\"89 " "%i %{%d%}%s %!"
- (fun i f s -> i = 12 && f = "%i" && s = "89");;
+ (fun i f s -> i = 12 && f = "%i" && s = "89")
+;;
-test (test48 ());;
+test (test48 ())
+;;
(* Testing stoppers after ranges. *)
let test49 () =
@@ -836,11 +935,13 @@ let test49 () =
sscanf "as" "%[a-z]%s" (fun s t -> s = "as" && t = "") &&
sscanf "-as" "%[-a-z]" (fun s -> s = "-as") &&
sscanf "-as" "%[-a-z]@s" (fun s -> s = "-a") &&
- sscanf "-as" "-%[a]@s" (fun s -> s = "a") &&
+ sscanf "-as" "-%[a]@s" (fun s -> s = "a") &&
sscanf "-asb" "-%[a]@sb%!" (fun s -> s = "a") &&
- sscanf "-asb" "-%[a]@s%s" (fun s t -> s = "a" && t = "b");;
+ sscanf "-asb" "-%[a]@s%s" (fun s t -> s = "a" && t = "b")
+;;
-test (test49 ());;
+test (test49 ())
+;;
(* Testing buffers defined via functions
+ co-routines that read and write from the same buffers
@@ -853,7 +954,8 @@ let next_char ob () =
let c = s.[0] in
Buffer.clear ob;
Buffer.add_string ob (String.sub s 1 (len - 1));
- c;;
+ c
+;;
let send_string ob s =
Buffer.add_string ob s; Buffer.add_char ob '\n';;
@@ -885,11 +987,13 @@ and writer ib ob =
let go () =
let ob = Buffer.create 17 in
let ib = Scanf.Scanning.from_function (next_char ob) in
- reader ib ob;;
+ reader ib ob
+;;
let test50 () = go () = 100;;
-test (test50 ());;
+test (test50 ())
+;;
(* Simple tests may also fail! *)
let test51 () =
@@ -904,9 +1008,11 @@ let test51 () =
sscanf "Hello\n" "%s@\n%s" (fun s1 s2 ->
s1 = "Hello" && s2 = "") &&
sscanf "Hello \n" "%s@\n%s" (fun s1 s2 ->
- s1 = "Hello " && s2 = "");;
+ s1 = "Hello " && s2 = "")
+;;
-test (test51 ());;
+test (test51 ())
+;;
(* Tests that indeed %s@c format works properly.
Also tests the difference between \n and @\n.
@@ -925,9 +1031,11 @@ let test52 () =
sscanf "Hello\n" "%s@\n%s" (fun s1 s2 ->
s1 = "Hello" && s2 = "") &&
sscanf "Hello \n" "%s%s@\n" (fun s1 s2 ->
- s1 = "Hello" && s2 = " ");;
+ s1 = "Hello" && s2 = " ")
+;;
-test (test52 ());;
+test (test52 ())
+;;
(* Reading native, int32 and int64 numbers. *)
let test53 () =
@@ -938,9 +1046,11 @@ let test53 () =
sscanf "124" "%ld" (fun i -> Int32.succ i = 125l) &&
sscanf "123" "%Ld" id = 123L &&
- sscanf "124" "%Ld" (fun i -> Int64.pred i = 123L);;
+ sscanf "124" "%Ld" (fun i -> Int64.pred i = 123L)
+;;
-test (test53 ());;
+test (test53 ())
+;;
(* Routines to create the file that tscanf uses as a testbed case. *)
let create_tscanf_data ob lines =
@@ -949,23 +1059,27 @@ let create_tscanf_data ob lines =
Buffer.add_string ob " -> ";
Buffer.add_string ob (Printf.sprintf "%S" e);
Buffer.add_string ob ";\n" in
- List.iter add_line lines;;
+ List.iter add_line lines
+;;
let write_tscanf_data_file fname lines =
let oc = open_out fname in
let ob = Buffer.create 42 in
create_tscanf_data ob lines;
Buffer.output_buffer oc ob;
- close_out oc;;
+ close_out oc
+;;
(* The tscanf testbed case file name. *)
let tscanf_data_file = "tscanf_data";;
(* The contents of the tscanf testbed case file. *)
let tscanf_data_file_lines = [
"Objective", "Caml";
-];;
+]
+;;
(* We write the tscanf testbed case file. *)
-write_tscanf_data_file tscanf_data_file tscanf_data_file_lines;;
+write_tscanf_data_file tscanf_data_file tscanf_data_file_lines
+;;
(* Then we verify that its contents is indeed correct. *)
@@ -975,7 +1089,7 @@ let get_lines fname =
let l = ref [] in
try
while not (Scanf.Scanning.end_of_input ib) do
- Scanf.bscanf ib " %S -> %S ; " (fun x y ->
+ Scanf.bscanf ib " %S -> %S; " (fun x y ->
l := (x, y) :: !l)
done;
List.rev !l
@@ -983,12 +1097,15 @@ let get_lines fname =
| Scanf.Scan_failure s ->
failwith (Printf.sprintf "in file %s, %s" fname s)
| End_of_file ->
- failwith (Printf.sprintf "in file %s, unexpected end of file" fname);;
+ failwith (Printf.sprintf "in file %s, unexpected end of file" fname)
+;;
let test54 () =
- get_lines tscanf_data_file = tscanf_data_file_lines;;
+ get_lines tscanf_data_file = tscanf_data_file_lines
+;;
-test (test54 ());;
+test (test54 ())
+;;
(* Creating digests for files. *)
let add_digest_ib ob ib =
@@ -1000,13 +1117,15 @@ let add_digest_ib ob ib =
Buffer.add_char ob '\n' in
try
while true do scan_line ib output_line_digest done;
- with End_of_file -> ();;
+ with End_of_file -> ()
+;;
let digest_file fname =
let ib = Scanf.Scanning.from_file fname in
let ob = Buffer.create 42 in
add_digest_ib ob ib;
- Buffer.contents ob;;
+ Buffer.contents ob
+;;
let test55 () =
let ob = Buffer.create 42 in
@@ -1016,9 +1135,22 @@ let test55 () =
Buffer.clear ob;
Scanning.from_string s in
let tscanf_data_file_lines_digest = add_digest_ib ob ib; Buffer.contents ob in
- digest_file tscanf_data_file = tscanf_data_file_lines_digest;;
+ digest_file tscanf_data_file = tscanf_data_file_lines_digest
+;;
+
+test (test55 ())
+;;
+
+(* Testing the number of characters read. *)
+let test56 () =
+ let g s = Scanf.sscanf s "%d%n" (fun i n -> (i, n)) in
+ g "99" = (99, 2) &&
+ g "99 syntaxes all in a row" = (99, 2) &&
+ g "-20 degrees Celsius" = (-20, 3)
+;;
-test (test55 ());;
+test (test56 ())
+;;
(* To be continued ...
(* Trying to scan records. *)
@@ -1030,13 +1162,15 @@ let rec scan_fields ib scan_field accu =
kscanf ib (fun ib exc -> accu)
" %1[;] "
(fun s ->
- if s = "" then accu else scan_fields ib scan_field accu));;
+ if s = "" then accu else scan_fields ib scan_field accu))
+;;
let scan_record scan_field ib =
bscanf ib "{ " ();
let accu = scan_fields ib scan_field [] in
bscanf ib " }" ();
- List.rev accu;;
+ List.rev accu
+;;
let scan_field ib =
bscanf ib "%s = %[^;]" (fun finame ficont -> finame, ficont);;