Assertions are a way of breaking out of code when there is an error or an unexpected input.

Some languages throw exceptions and some treat it as a break point.



Task

Show an assertion in your language by asserting that an integer variable is equal to 42.




go =>
 
  %
  % Test predicates
  % 
  S = "ablewasiereisawelba",
  assert("test1",$is_palindrome(S)),
  assert_failure("test2",$not is_palindrome(S)),
 
  assert("test3",$member(1,1..10)), 
  assert_failure("test4",$member(1,1..10)), % bad test
  nl,
 
  % 
  % Test functions
  % 
  assert("test5",$2+2,4),
  assert_failure("test6",$2+2, 5),
 
  assert("test7",$to_lowercase("PICAT"),"picat"),
  assert_failure("test8",$sort([3,2,1]),[1,3,2]),
  nl.
 
is_palindrome(S) => 
  S == S.reverse().
 
% Test a predicate with call/1 
assert(Name, A) =>
  println(Name ++ ": " ++ cond(call(A), "ok", "not ok")).
assert_failure(Name, A) => 
  println(Name ++ ": " ++ cond(not call(A), "ok", "not ok")).
 
% Test a function with apply/1
assert(Name, A, Expected) => 
  println(Name ++ ": " ++ cond(apply(A) == Expected, "ok", "not ok")).
assert_failure(Name, A, Expected) => 
  println(Name ++ ": " ++ cond(apply(A) != Expected , "ok", "not ok")).