File: util.ml

package info (click to toggle)
extlib 1.7.7-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 632 kB
  • sloc: ml: 6,980; makefile: 128; sh: 42; ansic: 31
file content (108 lines) | stat: -rw-r--r-- 3,118 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
(*
 * ExtLib Testing Suite
 * Copyright (C) 2004 Janne Hellsten
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version,
 * with the special exception on linking described in file LICENSE.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *)

module P = Printf

let log s = 
  P.printf "%s\n" s;
  flush stdout

let random_char () = 
  char_of_int (Random.int 256)

let random_string () = 
  let len = Random.int 256 in
  let str = String.create len in
  if len > 0 then
    for i = 0 to (len-1) do
      str.[i] <- random_char ()
    done;
  Bytes.unsafe_to_string str


let random_string_len len = 
  let len = len in
  let str = String.create len in
  if len > 0 then
    for i = 0 to (len-1) do
      str.[i] <- random_char ()
    done;
  Bytes.unsafe_to_string str

(* For  counting the success ratio *)
let test_run_count = ref 0 
let test_success_count = ref 0 
let g_test_run_count = ref 0 
let g_test_success_count = ref 0 

let test_module name f = 
  P.printf "%s\n" name;
  flush stdout;
  test_run_count := 0;
  test_success_count := 0;
  f ();
  if !test_run_count <> 0 then
    P.printf "  %i/%i tests succeeded.\n" 
      !test_success_count !test_run_count

let run_test ~test_name f = 
  try
    incr g_test_run_count;
    incr test_run_count;
    P.printf "  %s" test_name;
    flush stdout;
    let () = f () in
    incr g_test_success_count;
    incr test_success_count;
    P.printf " - OK\n"
  with 
    Assert_failure (file,line,column) ->
      P.printf " - FAILED\n    reason: ";
      P.printf " %s:%i:%i\n" file line column;
      flush stdout

let all_tests = Hashtbl.create 10

let register modname l =
  let existing = try Hashtbl.find all_tests modname with Not_found -> [] in
  Hashtbl.replace all_tests modname (l @ existing)

let register1 modname name f = register modname [name,f]

let run_all filter =
  let allowed name =
    match filter with
    | None -> true
    | Some l -> List.mem (String.lowercase name) l
  in
  g_test_run_count := 0;
  g_test_success_count := 0;
  Hashtbl.iter begin fun modname tests ->
    let allowed_module = allowed modname in
    test_module modname begin fun () ->
      List.iter begin fun (test_name,f) ->
        if allowed_module || allowed (modname^"."^test_name) then run_test ~test_name f
      end tests
    end
  end all_tests;
  if !g_test_run_count <> 0 then
    P.printf "\nOverall %i/%i tests succeeded.\n" 
      !g_test_success_count !g_test_run_count;
  !g_test_run_count = !g_test_success_count