File: base_test.ml

package info (click to toggle)
herdtools7 7.58-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 19,732 kB
  • sloc: ml: 128,583; ansic: 3,827; makefile: 670; python: 407; sh: 212; awk: 14
file content (134 lines) | stat: -rw-r--r-- 4,233 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
(****************************************************************************)
(*                           the diy toolsuite                              *)
(*                                                                          *)
(* Jade Alglave, University College London, UK.                             *)
(* Luc Maranget, INRIA Paris-Rocquencourt, France.                          *)
(*                                                                          *)
(* Copyright 2010-present Institut National de Recherche en Informatique et *)
(* en Automatique, ARM Ltd and the authors. All rights reserved.            *)
(*                                                                          *)
(* This software is governed by the CeCILL-B license under French law and   *)
(* abiding by the rules of distribution of free software. You can use,      *)
(* modify and/ or redistribute the software under the terms of the CeCILL-B *)
(* license as circulated by CEA, CNRS and INRIA at the following URL        *)
(* "http://www.cecill.info". We also give a copy in LICENSE.txt.            *)
(****************************************************************************)

(** Tests for the Base modules. *)

let tests = [
  "Base.Fun.protect calls both f and finally", (fun () ->
    let called_f = ref false in
    let called_finally = ref false in

    Base.Fun.protect
      ~finally:(fun () -> called_finally := true)
      (fun () -> called_f := true) ;

    if not !called_f then
      Test.fail "did not call f" ;

    if not !called_finally then
      Test.fail "did not call finally"
  );
  "Base.Fun.protect calls finally before re-raising exception", (fun () ->
    let called_finally = ref false in

    let raised_exception =
      try
        Base.Fun.protect
          ~finally:(fun () -> called_finally := true)
          (fun () -> if true then raise Not_found ; ()) ;
        false
      with Not_found -> true
    in

    if not raised_exception then
      Test.fail "did not re-raise exception" ;

    if not !called_finally then
      Test.fail "did not call finally"
  );
  "Base.Fun.protect wraps exceptions raised by finally", (fun () ->
    let raised_exception =
      try
        Base.Fun.protect
          ~finally:(fun () -> raise Not_found)
          (fun () -> ()) ;
        false
      with Base.Fun.Finally_raised Not_found -> true
    in

    if not raised_exception then
      Test.fail "did not wrap & re-raise exception" ;
  );

  "Base.List.compare", (fun () ->
    let tests = [
      [], [], 0 ;
      ["a"], [], 1 ;
      [], ["a"], -1 ;
      ["a"], ["a"], 0 ;
      ["a"], ["b"], -1 ;
    ] in

    List.iteri
      (fun i (xs, ys, expected) ->
        let actual = Base.List.compare Base.String.compare xs ys in
        if actual <> expected then
          Test.fail (Printf.sprintf "[%i] expected %i, got %i" i expected actual)
      )
      tests
  );

  "Base.List.to_ocaml_string", (fun () ->
    let tests = [
      [], "[]" ;
      ["a"], "[\"a\"]" ;
      ["a"; "b"], "[\"a\"; \"b\"]" ;
    ] in

    List.iter
      (fun (xs, expected) ->
        let actual = Base.List.to_ocaml_string Base.String.to_ocaml_string xs in
        if String.compare actual expected <> 0 then
          Test.fail (Printf.sprintf "expected %s, got %s" expected actual)
      )
      tests
  );

  "Base.Option.compare", (fun () ->
    let tests = [
      None, None, 0 ;
      Some "a", None, 1 ;
      None, Some "a", -1 ;
      Some "a", Some "a", 0 ;
      Some "a", Some "b", -1 ;
    ] in

    List.iteri
      (fun i (xs, ys, expected) ->
        let actual = Base.Option.compare Base.String.compare xs ys in
        if actual <> expected then
          Test.fail (Printf.sprintf "[%i] expected %i, got %i" i expected actual)
      )
      tests
  );

  "Base.Option.to_ocaml_string", (fun () ->
    let tests = [
      None, "None" ;
      Some "a", "Some (\"a\")" ;
    ] in

    List.iter
      (fun (xs, expected) ->
        let actual = Base.Option.to_ocaml_string Base.String.to_ocaml_string xs in
        if String.compare actual expected <> 0 then
          Test.fail (Printf.sprintf "expected %s, got %s" expected actual)
      )
      tests
  );
]

let () = Test.run tests