OKlibrary  0.2.1.6
Asserts.mac
Go to the documentation of this file.
00001 /* Oliver Kullmann, 27.1.2008 (Swansea) */
00002 /* Copyright 2008, 2010, 2012 Oliver Kullmann
00003 This file is part of the OKlibrary. OKlibrary is free software; you can redistribute
00004 it and/or modify it under the terms of the GNU General Public License as published by
00005 the Free Software Foundation and included in this library; either version 3 of the
00006 License, or any later version. */
00007 
00022 /* Use "assert(expr)" to state that expr shall evaluate to true. */
00023 assert('expr) := block([error_size : error_size, val], 
00024   error_size : 1000,
00025   val : errcatch(ev(expr)),
00026   if val = [] then (
00027     if not oklib_automatic_test then
00028       error("ASSERT: Evaluation of expression \"expr\" yields an error.")
00029     else (
00030       with_stdout(test_output_okl, 
00031         print("ASSERT: Evaluation of expression \"expr\" yields an error.")),
00032       error("ASSERT: Evaluation of expression \"expr\" yields an error.")
00033     )
00034   )
00035   else block([val2],
00036     val : val[1],
00037     val2 : is(val),
00038     if val2=true then true 
00039     elseif not oklib_automatic_test then
00040       error("ASSERT: Expression \"", val, "\" does not evaluate to true.")
00041     else (
00042       with_stdout(test_output_okl, print("ASSERT: Expression \"", val, "\" does not evaluate to true.")),
00043       error("ASSERT: Expression \"", val, "\" does not evaluate to true.")
00044   )))$
00045 
00046 /* Helper functions to handle matters of precision. */
00047 no_digits(x) := if x = 0 then 0 else entier(log(x)/log(10)) + 1$
00048 
00049 /* Use "assert_float_equal(a,b)" to state that a,b shall be equal within
00050    the floating-point precision: */
00051 assert_float_equal(expr1,expr2) := ev(
00052   buildq([expr1,expr2], 
00053     assert(abs(expr1-expr2) < oklib_float_comparison_factor * 10^(no_digits(max(abs(expr1), abs(expr2)))-oklib_float_comparison_exponent))),
00054  eval)$
00055 
00056 /* Use "assert_bfloat_equal(a,b,d)" to state that a,b shall be equal as
00057    big-float-numbers with a precision of d digits: */
00058 assert_bfloat_equal(expr1,expr2,d) := ev(
00059   buildq([expr1,expr2], 
00060     assert(abs(expr1-expr2) < 2 * 10^(no_digits(max(abs(expr1), abs(expr2)))-d))),
00061  eval)$
00062 
00063 
00064 /* *********
00065    * Tools *
00066    *********
00067 */
00068 
00069 /* Case distinction regarding oklib_test_level; for example, cokltl(4,6)
00070    replaces "if oklib_test_level=0 then 4 else 6": */
00071 cokltl([L]) := L[min(length(L),oklib_test_level+1)]$
00072 
00073