Skip to content

Instantly share code, notes, and snippets.

@mauricio
Last active December 24, 2015 23:19
Show Gist options
  • Select an option

  • Save mauricio/6878736 to your computer and use it in GitHub Desktop.

Select an option

Save mauricio/6878736 to your computer and use it in GitHub Desktop.
exception AssertionFailure of string;
fun assert_true( result : bool, message : string ) =
if result
then result
else raise AssertionFailure message
fun assert_false( result : bool, message : string ) =
if result
then raise AssertionFailure message
else result
fun assert_equal( expected : int, result : int ) =
if expected = result
then true
else raise AssertionFailure ("expected " ^ Int.toString(result) ^ " to be " ^ Int.toString(expected) )
fun assert_equal_string( expected : string, result : string ) =
if expected = result
then true
else raise AssertionFailure ( "expected " ^ result ^ "to be " ^ expected )
fun to_string_date( date : int * int * int ) : string =
"(" ^ Int.toString(#1 date) ^ "," ^ Int.toString(#2 date) ^ "," ^ Int.toString(#3 date) ^ ")"
fun to_string( dates : (int * int * int) list) : string =
let
val mapped_dates = map to_string_date dates
val result = String.concatWith "," mapped_dates
in
"[" ^ result ^ "]"
end
fun assert_equal_dates( expected: (int * int * int) list, result : (int * int * int) list ) =
if expected = result
then true
else raise AssertionFailure ( "expected " ^ to_string(result) ^ " to be " ^ to_string(expected) )
fun int_list_to_string( items : int list ) =
let
val mapped_items = map Int.toString items
val result = String.concatWith "," mapped_items
in
"[" ^ result ^ "]"
end
fun assert_equal_ints( expected : int list, result : int list ) =
if expected = result
then true
else raise AssertionFailure (
"expected " ^ int_list_to_string(result) ^
" to be " ^ int_list_to_string(expected) )
fun assert_some_date( expected : int * int * int, result : (int * int * int) option ) =
if valOf(result) = expected
then true
else raise AssertionFailure ("expected " ^ to_string_date(valOf(result)) ^
" to be " ^ to_string_date(expected) )
val test = assert_true( is_older( (1,2,3), (2,3,4) ), "one is older than another" );
val test = assert_true( is_older( (1,2,3), (1,3,3) ), "older only on month" );
val test = assert_true( is_older( (1,2,3), (1,2,4) ), "older only on day" );
val test = assert_true( is_older( (2011,3,31), (2011,4,28) ), "march is before february" );
OS.Process.exit(OS.Process.success);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment