! Include file with the assertion methods used when running X-Midas tests.
!
! @author  J. Czechowski IV / Neon Ngo
! @version $Id: xm_assert.txt,v 1.6 2009/03/13 19:54:04 ntn Exp $
!

!******************************************************************************!
! Assert that a boolean value is ge 1. This is usually only used by assert_str
! and assert_num.
! @param msg   The informational message.
! @param count Name of the result holding the number of tests run.
! @param fails Name of the result holding the number of tests failures.
! @param pass  Indicates if the test passes (1) or fails (0).
!
subroutine assert a:msg u:count u:fails l:pass
  local len,dots
  res a:dots "..................................................................."
  sedit msg len LENGTH 0
  sedit dots dots RANGE len 0

  calc/quiet ^count ^count 1 +
  if pass ge 1 then
    say "INFO: ^{msg} ^{dots}.PASS"
  else
    calc/quiet ^fails ^fails 1 +
    say "WARN: ^{msg} ^{dots}.FAIL <--"
  endif
endsubroutine

!******************************************************************************!
! Assert that two strings are equal.
! @param msg      The informational message.
! @param count    Name of the result holding the number of tests run.
! @param fails    Name of the result holding the number of tests failures.
! @param expected The expected result.
! @param actual   The actual result.
!
subroutine assert_str a:msg u:count u:fails a:expected a:actual
  if expected eqs actual then
    call assert msg ^count ^fails 1
  else
    !ORIG call assert msg ^count ^fails 0
    call assert "msg expect:^expected was:^actual" ^count ^fails 0
  endif
endsubroutine

!******************************************************************************!
! Assert that two numbers are equal (with specified absolute tolerance).
! @param msg       The informational message.
! @param count     Name of the result holding the number of tests run.
! @param fails     Name of the result holding the number of tests failures.
! @param expected  The expected result.
! @param actual    The actual result.
! @param tolerance The absolute tolerance between the expected and
!                  actual result value. [DEF=1e-6]
!
subroutine assert_num a:msg u:count u:fails n:expected n:actual n:tolerance[1e-6]
  local assert_num_diff

  if actual REXISTS then
    calc/quiet assert_num_diff expected actual - abs
  else
    result assert_num_diff 1e9 ! Fails if actual result not given
  endif

  if assert_num_diff le tolerance then
    call assert msg ^count ^fails 1
  else
    !ORIG call assert msg ^count ^fails 0
    call assert "^msg expect:^expected was:^actual" ^count ^fails 0
  endif
endsubroutine

!******************************************************************************!
! Assert that a given result exists.
! @param msg      The informational message.
! @param count    Name of the result holding the number of tests run.
! @param fails    Name of the result holding the number of tests failures.
! @param expected The name of the result.
!
subroutine assert_rexists a:msg u:count u:fails u:expected
  if expected rexists then
    call assert msg ^count ^fails 1
  else
    call assert msg ^count ^fails 0
  endif
endsubroutine
