|
| 1 | +open OUnit2 |
| 2 | +open Crashcourse.Basics |
| 3 | + |
| 4 | +let test_inc _ = |
| 5 | + let assert_equal = assert_equal ~printer:string_of_int in |
| 6 | + assert_equal 5 (inc 4); |
| 7 | + assert_equal 1 (inc 0) |
| 8 | + |
| 9 | +let test_inc' _ = |
| 10 | + let assert_equal = assert_equal ~printer:string_of_int in |
| 11 | + assert_equal 5 (inc' 4); |
| 12 | + assert_equal 1 (inc' 0) |
| 13 | + |
| 14 | +let test_fact _ = |
| 15 | + let assert_equal = assert_equal ~printer:string_of_int in |
| 16 | + assert_equal 1 (fact 0); |
| 17 | + assert_equal 1 (fact 1); |
| 18 | + assert_equal 2 (fact 2); |
| 19 | + assert_equal 6 (fact 3); |
| 20 | + assert_equal 24 (fact 4); |
| 21 | + assert_equal 120 (fact 5) |
| 22 | + |
| 23 | +let test_fact' _ = |
| 24 | + let assert_equal = assert_equal ~printer:string_of_int in |
| 25 | + assert_equal 1 (fact' 0); |
| 26 | + assert_equal 1 (fact' 1); |
| 27 | + assert_equal 2 (fact' 2); |
| 28 | + assert_equal 6 (fact' 3); |
| 29 | + assert_equal 24 (fact' 4); |
| 30 | + assert_equal 120 (fact' 5) |
| 31 | + |
| 32 | +let test_gcd _ = |
| 33 | + let assert_equal = assert_equal ~printer:string_of_int in |
| 34 | + assert_equal 1 (gcd 3 5); |
| 35 | + assert_equal 3 (gcd 3 9); |
| 36 | + assert_equal 3 (gcd 9 6); |
| 37 | + assert_equal 6 (gcd 12 18) |
| 38 | + |
| 39 | +let test_implies _ = |
| 40 | + let assert_equal = assert_equal ~printer:string_of_bool in |
| 41 | + assert_equal true (implies false false); |
| 42 | + assert_equal true (implies false true); |
| 43 | + assert_equal false (implies true false); |
| 44 | + assert_equal true (implies true true) |
| 45 | + |
| 46 | +let test_inc3 _ = |
| 47 | + let assert_equal = assert_equal ~printer:string_of_int in |
| 48 | + assert_equal 7 (inc3 4); |
| 49 | + assert_equal 3 (inc3 0) |
| 50 | + |
| 51 | +let test_triangular _ = |
| 52 | + let assert_equal = assert_equal ~printer:string_of_int in |
| 53 | + assert_equal 0 (triangular 0); |
| 54 | + assert_equal 1 (triangular 1); |
| 55 | + assert_equal 3 (triangular 2); |
| 56 | + assert_equal 6 (triangular 3); |
| 57 | + assert_equal 10 (triangular 4) |
| 58 | + |
| 59 | +let test_pow _ = |
| 60 | + let assert_equal = assert_equal ~printer:string_of_int in |
| 61 | + assert_equal 1 (pow 2 0); |
| 62 | + assert_equal 2 (pow 2 1); |
| 63 | + assert_equal 4 (pow 2 2); |
| 64 | + assert_equal 8 (pow 2 3); |
| 65 | + assert_equal 16 (pow 2 4); |
| 66 | + assert_equal 9 (pow 3 2) |
| 67 | + |
| 68 | +let test_is_vowel _ = |
| 69 | + let assert_equal = assert_equal ~printer:string_of_bool in |
| 70 | + assert_equal true (is_vowel 'a'); |
| 71 | + assert_equal true (is_vowel 'e'); |
| 72 | + assert_equal true (is_vowel 'i'); |
| 73 | + assert_equal true (is_vowel 'o'); |
| 74 | + assert_equal true (is_vowel 'u'); |
| 75 | + assert_equal false (is_vowel 'x'); |
| 76 | + assert_equal false (is_vowel 'm'); |
| 77 | + assert_equal false (is_vowel 'b') |
| 78 | + |
| 79 | +let test_is_vowel' _ = |
| 80 | + let assert_equal = assert_equal ~printer:string_of_bool in |
| 81 | + assert_equal true (is_vowel' 'a'); |
| 82 | + assert_equal true (is_vowel' 'e'); |
| 83 | + assert_equal true (is_vowel' 'i'); |
| 84 | + assert_equal true (is_vowel' 'o'); |
| 85 | + assert_equal true (is_vowel' 'u'); |
| 86 | + assert_equal false (is_vowel' 'x'); |
| 87 | + assert_equal false (is_vowel' 'm'); |
| 88 | + assert_equal false (is_vowel' 'b') |
| 89 | + |
| 90 | +let test_xor _ = |
| 91 | + let assert_equal = assert_equal ~printer:string_of_bool in |
| 92 | + assert_equal false (xor false false); |
| 93 | + assert_equal true (xor false true); |
| 94 | + assert_equal true (xor true false); |
| 95 | + assert_equal false (xor true true) |
| 96 | + |
| 97 | +let tests = |
| 98 | + "basics" >::: [ |
| 99 | + "examples" >::: [ |
| 100 | + "inc" >:: test_inc; |
| 101 | + "inc'" >:: test_inc'; |
| 102 | + "fact" >:: test_fact; |
| 103 | + "fact'" >:: test_fact'; |
| 104 | + "gcd" >:: test_gcd; |
| 105 | + "implies" >:: test_implies; |
| 106 | + ]; |
| 107 | + "problems" >::: [ |
| 108 | + "inc3" >:: test_inc3; |
| 109 | + "triangular" >:: test_triangular; |
| 110 | + "pow" >:: test_pow; |
| 111 | + "is_vowel" >:: test_is_vowel; |
| 112 | + "is_vowel'" >:: test_is_vowel'; |
| 113 | + "xor" >:: test_xor; |
| 114 | + ]; |
| 115 | + ] |
0 commit comments