Commits

Paweł Wieczorek committed 234250a

Added pa_ounit

  • Participants
  • Parent commits d5f55f3
  • Branches 2014_03_16_cleaning

Comments (0)

Files changed (9)

 Tmp.byte
 Test.native
 Test.byte
+Unittests.native
+Unittests.byte
 source/Predefined/Autogen.ml
 distribution
 _build
 
 
 let define_contexts () =
-    populate_context "source"
+    populate_context "source";
+    populate_context "unittests"
 
 (****************************************************************************************************************
  * For-packs
  * Main
  ****************************************************************************************************************)
 
+let add_pa_deps () =
+    flag ["ocaml"; "pp"; "use_pa_ounit"] (A"unittests/pa_ounit.cmo");
+    dep  ["ocaml"; "ocamldep"; "use_pa_ounit"] ["unittests/pa_ounit.cmo"]
 ;;
 
 dispatch begin function
     ocaml_3_x_hack ();
     define_internal_libraries ();
     define_contexts ();
-    define_tags ()
+    define_tags ();
+    add_pa_deps ()
 | _ -> ()
 end;;
 
-#<Lib.cmxa>: use_util
-#<Lang.cmxa>: use_util, use_lib
-
 "Utilities": include
 "Predefined": include
 "Languages" : include

unittests/ExtOUnit.ml

+
+include OUnit
+
+let assert_true expr = 
+    assert_bool "expected true" expr
+
+let assert_false expr =
+    assert_bool "expected false" (not expr)
+
+

unittests/Unittests.ml

 open OUnit
 
 let tests = "Opifex" >:::
-    [ "Test_FrozenHashtbl.ml" >::: Test_FrozenHashtbl.tests
+    [ "Test_FrozenHashtbl"      >::: Test_FrozenHashtbl.tests
+    ; "Test_ManagedRef"         >::: Test_ManagedRef.tests
     ]
 
 let _ =

unittests/Utilities/Test_FrozenHashtbl.ml

 let create_frozenhashtbl () =
     FrozenHashtbl.freeze (create_hashtbl ())
 
+let create_empty_frozenhashtbl () =
+    FrozenHashtbl.freeze (Hashtbl.create 7)
+
 (*********************************************************************************************************************
  * Tests
  ********************************************************************************************************************)
         assert_raises Not_found (fun () -> FrozenHashtbl.find fhl 3)
     end
 
+let test_fold_empty = "fold_empty" >:: begin fun _ ->
+        let fhl = create_empty_frozenhashtbl () in
+        let initial_acc = 1 in
+        let y   = FrozenHashtbl.fold (fun _ v a -> v + a) fhl initial_acc in
+        assert_equal initial_acc y
+    end
+
+let test_fold_plus = "fold_plus" >:: begin fun _ ->
+        let hl      = create_hashtbl () in
+        let fhl     = FrozenHashtbl.freeze hl in
+        let f _ x y = x + y in
+
+        let y           = FrozenHashtbl.fold f fhl 0 in
+        let expected    = Hashtbl.fold f hl 0 in
+        assert_equal expected y
+    end
+
 (*********************************************************************************************************************
  * Test suite
  ********************************************************************************************************************)
     ; test_find_1
     ; test_find_2
     ; test_find_notfound
+    ; test_fold_empty
+    ; test_fold_plus
     ]

unittests/Utilities/Test_ManagedRef.ml

+(*
+ * Opifex
+ *
+ * Copyrights(C) 2012,2013 by Pawel Wieczorek <wieczyk at gmail>
+ *)
+
+open ExtOUnit
+
+(*********************************************************************************************************************
+ * Helpers
+ ********************************************************************************************************************)
+
+
+(*********************************************************************************************************************
+ * Tests
+ ********************************************************************************************************************)
+
+let test_create = "create" >:: begin fun () ->
+        ignore (ManagedRef.create ())
+    end
+
+let test_is_empty_on_empty = "is_empty_on_empty" >:: begin fun () ->
+        let mr = ManagedRef.create () in
+        assert_true (ManagedRef.is_empty mr)
+    end
+
+let test_is_initialized_on_empty = "is_initialized_on_empty" >:: begin fun () ->
+        let mr = ManagedRef.create () in
+        assert_false (ManagedRef.is_initialized mr)
+    end
+
+
+let test_is_empty_on_initialized = "is_empty_on_initialized" >:: begin fun () ->
+        let mr = ManagedRef.create () in
+        ManagedRef.replace mr 5;
+        assert_false (ManagedRef.is_empty mr)
+    end
+
+
+let test_is_initialized_on_initialized = "is_initialized_on_initialized" >:: begin fun () ->
+        let mr = ManagedRef.create () in
+        ManagedRef.replace mr "5";
+        assert_true (ManagedRef.is_initialized mr)
+    end
+
+
+let test_release = "release" >:: begin fun () ->
+        let mr = ManagedRef.create () in
+        ManagedRef.replace mr true;
+        let y = ManagedRef.release mr in
+        assert_true y
+    end
+
+let test_is_empty_on_released = "is_empty_on_released" >:: begin fun () ->
+        let mr = ManagedRef.create () in
+        ManagedRef.replace mr 'a';
+        ignore (ManagedRef.release mr);
+        assert_true (ManagedRef.is_empty mr)
+    end
+
+
+Testlist tests begin
+
+test helo2 =
+    assert_true true
+
+test helo3 =
+    assert_true true
+
+end
+
+
+
+(*********************************************************************************************************************
+ * Test suite
+ ********************************************************************************************************************)
+
+(*
+let tests =
+    [ test_create
+    ; test_is_empty_on_empty
+    ; test_is_initialized_on_empty
+    ; test_is_empty_on_initialized
+    ; test_is_initialized_on_initialized
+    ; test_release
+    ; test_is_empty_on_released 
+    ]
+*)
+# <**/Test_*>: use_pa_ounit
+
+<Utilities/Test_ManagedRef*>: camlp4o, use_pa_ounit
+
+"pa_ounit.ml": use_camlp4, pp(camlp4of)
+
 "Utilities": include
 "Languages" : include
 "Libraries" : include
+
 "Machines" : include

unittests/pa_ounit.ml

+open Camlp4
+open Camlp4.PreCast
+open Syntax
+
+
+let create_test_suite _loc identifier test_body =
+    let test_func_name  = identifier in
+    let test_case_name  = <:expr<$str:identifier$>> in
+    let test_func_patt  = <:patt<$lid:test_func_name$>> in
+    let test_definition =
+        <:str_item<
+            let $test_func_patt$ = $test_case_name$ >:: begin fun () ->
+                $test_body$
+            end
+        >> in
+
+    (test_func_name, test_definition)
+
+
+let create_ocaml_list _loc exprs =
+    let add_to_list expr list_expr = <:expr< $expr$ :: $list_expr$ >> in
+    List.fold_right add_to_list exprs <:expr<[]>> 
+
+let create_test_list _loc identifier test_names =
+    let test_name_expr n = <:expr<$lid:n$>> in
+    let test_names_expr = List.map test_name_expr test_names in
+    <:str_item<
+        let $lid:identifier$ = $create_ocaml_list _loc test_names_expr$
+    >>
+
+let create_test_list_and_tests _loc identifier test_suites = 
+    let test_names          = List.map fst test_suites in
+    let test_definitions    = List.map snd test_suites in
+
+    let test_list   = create_test_list _loc identifier test_names in
+
+    Ast.stSem_of_list (test_definitions @ [test_list])
+
+
+EXTEND Gram
+  GLOBAL: str_item;
+
+    one_test:
+      [ [ "test"; identifier = a_ident; "="; test_body = expr -> begin
+        create_test_suite _loc identifier test_body
+        end
+      
+        ]];
+
+    testlist:
+    [[ "Testlist"; identifier = a_ident; "begin"; tests = LIST1 one_test;  "end"  -> begin
+        create_test_list_and_tests _loc identifier tests
+        end
+    ]];
+
+    str_item:
+        [[ testlist = testlist ->
+            testlist
+        ]];
+
+END
+