Sha256: 0bd22e4a134a60964192e711a9e0d228614fac182248dfac1a619e6a35460618

Contents?: true

Size: 1.67 KB

Versions: 63

Compression:

Stored size: 1.67 KB

Contents

open Base
open OUnit2
open Dominoes

let print_dominoe (d1, d2) = Printf.sprintf "(%d,%d)" d1 d2

let dominoes_printer xs = "[" ^ String.concat ~sep:";" (List.map xs ~f:print_dominoe) ^ "]"
let option_printer = function
  | None -> "None"
  | Some xs -> "Some " ^ dominoes_printer xs

let rotate_1 xs = List.tl_exn xs @ [List.hd_exn xs]

let tuple_compare (x0, y0) (x1, y1) =
  if x0 < x1 then (-1)
  else if x0 > x1 then 1
  else Int.compare y0 y1

let norm l =
  let norm1 (x, y) = if x > y then (y, x) else (x, y) in
  List.map ~f:norm1 l |> List.sort ~cmp:tuple_compare

let check_chain (input: dominoe list) (chained: dominoe list) =
  assert_equal (norm input) (norm chained) ~printer:dominoes_printer ~msg:"chain doesn't use the same dominoes as the input";
  let assert_dominoes_match d1 d2 =
    if snd d1 <> fst d2 then failwith @@ Printf.sprintf "%s and %s cannot be chained together" (print_dominoe d1) (print_dominoe d2) else () in
  let consecutives = List.zip_exn chained (rotate_1 chained) in
  List.iter consecutives ~f:(fun (d1, d2) -> assert_dominoes_match d1 d2)

let assert_empty c = if List.is_empty c then () else failwith "Expected 0 length chain"

let assert_valid_chain input _ctxt =
  match chain input with
  | None -> failwith "Expecting a chain"
  | Some(c) -> (if List.is_empty input then assert_empty else check_chain input) c

let assert_no_chain input _ctxt =
  assert_equal None (chain input) ~printer:option_printer

let assert_chain input hasChain =
  if hasChain then assert_valid_chain input else assert_no_chain input

let tests = [
(* TEST
  "$description" >::
    assert_chain $dominoes $expected;
END TEST *)
]

let () =
  run_test_tt_main ("dominoes tests" >::: tests)

Version data entries

63 entries across 63 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/ocaml/tools/test-generator/templates/ocaml/dominoes/test.ml
trackler-2.2.1.179 tracks/ocaml/tools/test-generator/templates/ocaml/dominoes/test.ml
trackler-2.2.1.178 tracks/ocaml/tools/test-generator/templates/ocaml/dominoes/test.ml
trackler-2.2.1.177 tracks/ocaml/tools/test-generator/templates/ocaml/dominoes/test.ml
trackler-2.2.1.176 tracks/ocaml/tools/test-generator/templates/ocaml/dominoes/test.ml
trackler-2.2.1.175 tracks/ocaml/tools/test-generator/templates/ocaml/dominoes/test.ml
trackler-2.2.1.174 tracks/ocaml/tools/test-generator/templates/ocaml/dominoes/test.ml
trackler-2.2.1.173 tracks/ocaml/tools/test-generator/templates/ocaml/dominoes/test.ml
trackler-2.2.1.172 tracks/ocaml/tools/test-generator/templates/ocaml/dominoes/test.ml
trackler-2.2.1.171 tracks/ocaml/tools/test-generator/templates/ocaml/dominoes/test.ml
trackler-2.2.1.170 tracks/ocaml/tools/test-generator/templates/ocaml/dominoes/test.ml
trackler-2.2.1.169 tracks/ocaml/tools/test-generator/templates/ocaml/dominoes/test.ml
trackler-2.2.1.167 tracks/ocaml/tools/test-generator/templates/ocaml/dominoes/test.ml
trackler-2.2.1.166 tracks/ocaml/tools/test-generator/templates/ocaml/dominoes/test.ml
trackler-2.2.1.165 tracks/ocaml/tools/test-generator/templates/ocaml/dominoes/test.ml
trackler-2.2.1.164 tracks/ocaml/tools/test-generator/templates/ocaml/dominoes/test.ml
trackler-2.2.1.163 tracks/ocaml/tools/test-generator/templates/ocaml/dominoes/test.ml
trackler-2.2.1.162 tracks/ocaml/tools/test-generator/templates/ocaml/dominoes/test.ml
trackler-2.2.1.161 tracks/ocaml/tools/test-generator/templates/ocaml/dominoes/test.ml
trackler-2.2.1.160 tracks/ocaml/tools/test-generator/templates/ocaml/dominoes/test.ml