Commits

camlspotter  committed ea32545

added --code-test

  • Participants
  • Parent commits 62f167f
  • Branches 4.01.0.2.2.0+win

Comments (0)

Files changed (9)

 COMPFLAGS= -g $(INCLUDES_DEP) -I +unix
 
 MODULES= utils checksum fileident copiedfilename filepath dotfile compdir xset treeset command typeexpand \
-	xlongident name xident xpath locident typeFix xprinttyp ext cmt spot spoteval spotconfig_intf spotconfig spotfile ocamlspot # pathreparse 
+	xlongident name xident xpath locident typeFix xprinttyp ext cmt spot spoteval spotconfig_intf spotconfig spotfile test ocamlspot # pathreparse 
 
 OBJS=		$(addsuffix .cmo, $(MODULES))
 
   let is_network_drive _ = false
 end
 
-let () =
-  assert (String.sub' "hello" 0 4 = "hell");
-  assert (String.sub' "hello" 0 5 = "hello");
-  assert (String.sub' "hello" 0 6 = "hello");
-  assert (String.sub' "hello" 0 7 = "hello");
-  assert (String.sub' "hello" 3 2 = "lo");
-  assert (String.sub' "hello" 3 3 = "lo");
-  assert (String.sub' "hello" 3 4 = "lo");
-  assert (String.sub' "hello" 5 5 = "")
-
 module Win32 : Filename = struct
   include Filename.Win32
     
     match drive_and_path s with
     | "", s ->
         begin match String.sub' s 0 2 with
-        | ("//" | "\\\\" as s) -> s, String.sub s 2 (String.length s - 2)
+        | ("//" | "\\\\" as p) -> p, String.sub s 2 (String.length s - 2)
         | _ -> "", s
         end
     | res -> res
   let f = of_os os in
   let drive, p = f#drive_and_path s in
   let drive = if drive = "" then None else Some drive in
-  let abs = drive <> None || f#is_absolute p in
+  let abs = match drive with
+    | None -> f#is_absolute p 
+    | Some d when f#is_network_drive d -> true
+    | _ -> f#is_absolute p 
+  in
   let rec splits st s =
     let d, b = f#dir_and_base s in
     if f#is_dir_sep d 0 && f#is_dir_sep b 0 then
       if s = d then s :: st
       else splits (b::st) d
   in
-  let revs = List.rev (splits [] s) in
+  let revs = List.rev (splits [] p) in
   { os; 
     op = f;
     drive;
 let to_string t =
   let compos = List.rev t.revs in
   let concats = String.concat t.op#sep in
-  match t.drive, t.abs with
-  | None, true -> concats ("" :: compos)
-  | None, false -> concats compos
-  | Some d, true when t.op#is_network_drive d -> d ^ concats compos
-  | Some d, false when t.op#is_network_drive d -> assert false
-  | Some d, true -> d ^ concats ("" :: compos)
-  | Some d, false -> d ^ concats compos
+  match t.drive, t.abs, compos with
+  | None, true, [] -> t.op#sep
+  | None, true, _ -> concats ("" :: compos)
+  | None, false, [] -> t.op#current
+  | None, false, _ -> concats compos
+  | Some d, true, _ when t.op#is_network_drive d -> d ^ concats compos
+  | Some d, false, _ when t.op#is_network_drive d -> assert false
+  | Some d, true, [] -> d ^ t.op#sep
+  | Some d, true, _ -> d ^ concats ("" :: compos)
+  | Some d, false, [] -> d ^ t.op#current
+  | Some d, false, _ -> d ^ concats compos
       
 let is_absolute t = t.abs
 let is_relative t = not t.abs
     is_prefix (List.rev x.revs) (List.rev y.revs)
   else None
 
+let test () =
+  let norm os s eq = 
+    let res = wrap os (fun x -> x) s in
+    if res <> eq then begin
+      Format.eprintf "Filepath.test failed: %S => %S => %S@." s res eq;
+      assert false
+    end
+  in
+  List.iter (fun (os, s, eq) -> norm os s eq) 
+    [ Unix, "/a/b/c", "/a/b/c";
+
+      Unix, "a/b/c", "a/b/c";
+      
+      Unix, "//a/b/c", "/a/b/c";
+      
+      Unix, "///a/b/c", "/a/b/c";
+
+      Unix, "/", "/";
+
+      Unix, "//", "/";
+
+      Unix, "///", "/";
+
+      Unix, ".", ".";
+
+      Unix, "./", ".";
+
+      Unix, "/.", "/";
+
+      Unix, "/a/./b/./c/", "/a/b/c";
+
+      Unix, "/a/../b/../c/", "/c"; 
+
+      Unix, "../../a/../b", "../../b";
+
+      Unix, "..", "..";
+
+      Unix, "/..", "/";
+
+      Unix, "a/.", "a";
+
+      Unix, "a//b/.", "a/b";
+
+      Unix, "", "."; (* ??? *)
+      
+      Win32, "\\a\\b\\c", "\\a\\b\\c";
+
+      Win32, "c:\\a\\b\\c", "C:\\a\\b\\c";
+
+      Win32, "c:/a/b/c", "C:\\a\\b\\c";
+
+      Win32, "c:a/b/c", "C:a\\b\\c";
+
+      Win32, "c:", "C:.";
+
+      Win32, "//a/b", "\\\\a\\b";
+    ]
+  
 (*
 let get_component : string -> string = Hashtbl.memoize (Hashtbl.create 1023) (fun x -> x)
 
 let dotdot = get_component (parent_dir_name)
 
-let test s = 
-  let ss = split s in
-  (* Format.eprintf "%s => %s@." s (concats ss); *)
-  ss
-
-let () =
-  assert (test "" = ["."]);
-  assert (test "." = ["."]);
-  assert (test "a/b/c" = ["."; "a"; "b"; "c"]);
-  assert (test "/a/b/c" = ["/"; "a"; "b"; "c"]);
-  assert (test "//a//b//c" = ["/"; "a"; "b"; "c"]);
-  assert (test "/a/./b/./c/" = ["/"; "a"; "."; "b"; "."; "c"]);
-  
-  if Sys.os_type = "Win32" then begin
-    assert (test "\\a\\b\\c" = ["\\"; "a"; "b"; "c"]);
-    assert (test "c:\\a\\b\\c" = ["C:\\"; "a"; "b"; "c"]);
-    assert (test "c:/a/b/c" = ["C:\\"; "a"; "b"; "c"]);
-  end
-
 let hashcons_list = 
   let cache = Hashtbl.create 1023 in
   let rec f xs = Hashtbl.memoize cache (function
     | x::xs -> x :: f xs) xs
   in
   f
-
-let rec normalize' rev = function
-  | [] -> hashcons_list (List.rev rev)
-  | x::xs when x = current_dir_name -> normalize' rev xs
-  | x::xs when x = parent_dir_name -> 
-      begin match rev with
-      | r::_ when not (is_relative r) -> normalize' rev xs
-      | r::_ when r = parent_dir_name || r = current_dir_name -> normalize' (x::rev) xs
-      | _::rev -> normalize' rev xs
-      | [] -> normalize' [x] xs
-      end
-  | x::xs -> normalize' (x::rev) xs
-          
-let normalize = normalize' []
-
-let () =
-  let test s = 
-    let ss = split s in
-    let nss = normalize ss in
-    Format.eprintf "%s => %s => %s@." s (concats ss) (concats nss);
-    nss
-  in
-
-  assert (test "a/b/c" = ["a"; "b"; "c"]);
-  assert (test "/a/b/c" = ["/"; "a"; "b"; "c"]);
-  assert (test "//a//b//c" = ["/"; "a"; "b"; "c"]);
-  assert (test "/a/./b/./c/" = ["/"; "a"; "b"; "c"]);
-  assert (test "/a/../b/../c/" = ["/"; "c"]);
-  assert (test "../../a/../b" = [".."; ".."; "b"]);
-    
-      assert (test ""       = []);
-      assert (test "."      = []);
-      assert (test "/"      = ["/"]);
-      assert (test "/."     = ["/"]);
-      assert (test "./"     = [""]);
-      assert (test ".."     = [".."]);
-      assert (test "/.."    = ["/"]);
-      assert (test "//"     = ["/"]);
-      assert (test "///"    = ["/"]);
-      assert (test "a/"     = ["a"]);
-      assert (test "a/."    = ["a"]);
-      assert (test "a/b"    = ["a"; "b"]);
-      assert (test "a/b/"   = ["a"; "b"]);
-      assert (test "a/b/."  = ["a"; "b"]);
-      assert (test "a//b/." = ["a"; "b"]);
-
-  if Sys.os_type = "Win32" then begin
-    assert (test "\\a\\b\\c" = ["\\"; "a"; "b"; "c"]);
-    assert (test "c:\\a\\b\\c" = ["c:\\"; "a"; "b"; "c"]);
-    assert (test "c:/a/b/c" = ["c:/"; "a"; "b"; "c"]);
-    assert (test "\\a\\..\\\\b\\\\..\\c\\" = ["\\"; "c"]);
-    assert (test "c:a/../b" = ["c:."; "b"]);
-  end
-
-type t = string list
-
-let () = assert (is_prefix ["a"; "b"; "c"] ["a"; "b"; "c"; "d"; "e"] = Some ["d"; "e"])
-
-let of_string path = normalize (split path)
-
-let to_string = concats
-
-
 *)

File filepath.mli

 val parent : t -> t
 val wrap : os -> (t -> t) -> string -> string
 
+val test : unit -> unit

File ocamlspot.ml

     | `Typecheck _ | `Recheck _ -> assert false
 end
 
-let _ = Main.main ()
+let () = if C.code_test then Test.test ()
 
+let () = Main.main ()
+

File spotconfig.ml

 let type_expand            = ref false
 let rest_args_rev          = ref []
 let use_spot               = ref false
+let code_test              = ref false
 
 let _ = 
   Arg.parse (Arg.align
 
       "--use-spot", 
       Arg.Set use_spot, " : use spot files instead of cmt, if exists";
+
+      "--code-test",
+      Arg.Set code_test, " : test some library functions";
     ])
     (fun s -> rev_anonargs := s :: !rev_anonargs)
     (Printf.sprintf 
 let print_interface        = !print_interface
 let type_expand            = !type_expand
 let use_spot               = !use_spot
+let code_test              = !code_test
 
 let dump_any = 
   dump_file || dump_rannots || dump_tree || dump_top || dump_flat

File spotconfig_intf.ml

   val dump_flat              : bool
   val dump_any               : bool
   val eager_dump             : bool
-  
+
+  val code_test              : bool
+
   val no_definition_analysis : bool
   
   val strict_time_stamp      : bool
+let test () =
+  Utils.String.test ();
+  Filepath.test ()
   (** Same as [String.sub] but even if the string shorter for [len] 
       the function succeeds and returns a shorter substring. 
   *)
-  (* CR jfuruse: need tests *)
   let sub' s pos len =
     let orig_len = length s in
     let len = max (min (pos + len) orig_len - pos) 0 in
     sub s pos len
 
+  let test () =
+    assert (sub' "hello" 0 4 = "hell");
+    assert (sub' "hello" 0 5 = "hello");
+    assert (sub' "hello" 0 6 = "hello");
+    assert (sub' "hello" 0 7 = "hello");
+    assert (sub' "hello" 3 2 = "lo");
+    assert (sub' "hello" 3 3 = "lo");
+    assert (sub' "hello" 3 4 = "lo");
+    assert (sub' "hello" 5 5 = "")
+    
   let find s pos f =
     let len = length s in
     let rec scan pos =
   let replace_chars from to_ s =
     let s' = copy s in
     iteri (fun p -> function
-      | c when c = from -> unsafe_set s p to_
+      | c when c = from -> unsafe_set s' p to_
       | _ -> ()) s';
     s'
 end
   val find : string -> int -> (char -> bool) -> int option
 
   val replace_chars : char -> char -> string -> string
+
+  val test : unit -> unit
 end
 
 module Filename : sig