1
exercism fetch ocaml run-length-encoding

test.ml

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
(* Test/exercise version: "1.0.0" *)

open Core
open OUnit2
open Run_length_encoding

let ae exp got _test_ctxt = assert_equal exp got ~printer:Fn.id

let encode_tests = [
   "empty string" >::
     ae "" (encode "");
   "single characters only are encoded without count" >::
     ae "XYZ" (encode "XYZ");
   "string with no single characters" >::
     ae "2A3B4C" (encode "AABBBCCCC");
   "single characters mixed with repeated characters" >::
     ae "12WB12W3B24WB" (encode "WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWB");
   "multiple whitespace mixed in string" >::
     ae "2 hs2q q2w2 " (encode "  hsqq qww  ");
   "lowercase characters" >::
     ae "2a3b4c" (encode "aabbbcccc");
]


let decode_tests = [
   "empty string" >::
     ae "" (decode "");
   "single characters only" >::
     ae "XYZ" (decode "XYZ");
   "string with no single characters" >::
     ae "AABBBCCCC" (decode "2A3B4C");
   "single characters with repeated characters" >::
     ae "WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWB" (decode "12WB12W3B24WB");
   "multiple whitespace mixed in string" >::
     ae "  hsqq qww  " (decode "2 hs2q q2w2 ");
   "lower case string" >::
     ae "aabbbcccc" (decode "2a3b4c");
]


let encode_and_then_decode_tests = [
   "encode followed by decode gives original string" >::
     ae "zzz ZZ  zZ" (encode "zzz ZZ  zZ" |> decode);
]

let () =
  run_test_tt_main (
    "run length encoding tests" >:::
      List.concat [encode_tests; decode_tests; encode_and_then_decode_tests]
  )