! Runs the Fortran/C++ MIDAS API tests for XBC
!
! author:  J. Czechowski IV / Neon Ngo
! version: $Id: test_xbc.mm,v 1.12 2015/12/16 19:28:28 paq Exp $
!
! @param test_name test set name to run:
!            ALL       - run all tests (DEF=ALL)
!            BASIc     - run basic API test cases
!            UTILities - run utilities API test cases
!******************************************************************************!
startmacro S:test_names["ALL"]
  switch INTERACTIVE interAct GET DEF=TRUE  SDEF=TRUE
  switch BASELINE    baseline GET DEF=FALSE SDEF=TRUE
  switch DEBUG       debug    GET DEF=FALSE SDEF=TRUE

  set xbc_test_list "BASIC,UTILITIES"

  if test_names eqs "ALL" then
    res xbc_tests xbc_test_list
  else
    sedit test_names xbc_tests GSUBstitute "|" ","
  endif

  ! --- run specified test cases ---
  for test_case inlist xbc_tests
    if     test_case eqss "BASI"
      call test_basic
    elseif test_case eqss "UTIL"
      call test_utilities
    endif
  endfor
endmacro

!******************************************************************************!
! test BASIC/common MIDAS APIs
!******************************************************************************!
procedure test_basic
  set L:TEST_RES_1 4
  ! TEST_RES_2 should not exist until test_xbc sets it
  set S:TEST_RES_3 "abc"
  ! TEST_RES_4 should not exist until test_xbc sets it
  ! This is used to test M$GET_USWITCH
  set ten 10

  ! Copy files to ram
  file copy nxm.sys.dat.world.prm         ram:world
  file copy nxm.sys.dat.apenny.prm        ram:apenny
  file copy nxm.sys.dat.apenny.prm        ram:apenny2 ! This file gets modified as part of the tests.
  file copy nxm.sys.dat.cone5000.prm      ram:cone5000
  file copy nxm.sys.dat.test3000_ieee.prm ram:test3000_ieee
  file copy nxm.sys.dat.test3000_eeei.prm ram:test3000_eeei
  
  file unprotect ram:world
  file unprotect ram:apenny
  file unprotect ram:apenny2
  file unprotect ram:test3000_ieee
  file unprotect ram:test3000_eeei

  nxm.xbc.host.test_xbc/foo/bar=2/verbose/stat=status/uswitch=ten

  assert/text="^{reg.main.name}: Fortran M$ API BASIC tests."  ^status eqs "PASS"

  assert/text="^{reg.main.name}: Result TEST_RES_2 exists."            TEST_RES_2 rexists
  assert/text="^{reg.main.name}: Result TEST_RES_2 is set to 'Hello'." TEST_RES_2 eqs "Hello"
  assert/text="^{reg.main.name}: Result TEST_RES_4 exists."            TEST_RES_4 rexists
  assert/text="^{reg.main.name}: Result TEST_RES_4 is set to 3.14."    TEST_RES_4 eqt 3.14

  file open tag ram:apenny2
  invoke row tag.getDataTable(128)
  assert/text="^{reg.main.name}: File updated after M$EXISTENCE size updated" tag.getSize() eq 129
  assert/text="^{reg.main.name}: File updated after M$EXISTENCE row updated"  row.F3 eq 2
  file close tag

  ! Make sure messages.tbl exists.  This should exist if developer did a make on XBC
  if nxm.xbc.cfg.messages.tbl FEXISTS then
    assert/text="^{reg.main.name}: messages.tbl exists. Run 'make code xbc' if this fails" true
  else
    assert/text="^{reg.main.name}: messages.tbl exists: Run 'make code xbc' if this fails" false
    warning "^{reg.main.name}: messages.tbl missing - Run 'make code xbc' to generate"
  endif

  ! test case for BUG 2375 MUNIQUE.okio field
  nxm.xbc.host.test_mu_okio/STATUS="TESTRESULT0"
  assert/text="^{reg.main.name}: Mu.okio test results" ^testresult0 eqs "PASS"

return  ! end test_basic

!******************************************************************************!
! test MIDAS UTILITIES APIs
!******************************************************************************!
procedure test_utilities
  nxm.xbc.host.test_utilities/STATUS="TESTRESULT"
  assert/text="^{reg.main.name}: UTILITIES test results" ^testresult eqs "PASS"
return  ! end test_utilities
