! Test NeXtOpt (running NeXtMidas as an Option Tree inside X-Midas)
!
! @Author:  Neon Ngo
!
startmacro
  switch INTERACTIVE interAct   GET DEF=TRUE  SDEF=TRUE
  switch BASELINE    baseline   GET DEF=FALSE SDEF=TRUE
  switch ALTTESTENV  alttestenv GET DEF=FALSE SDEF=TRUE  ! alternate test environment

  call setDatDir

  if interAct then
    res inst "First follow the NeXtOpt Setup instructions.  Then click on each test cases &
              to see more detailed instructions on right.  After done make sure to cleanup &
              your X-Midas session by running 'xmend'."

    pipe on
      panel/msgid=MAIN/setup/controls=gc
      gcontrol label mainLabel "Main"
        gcontrol menu main "Exit with Status = " "PASS,FAIL" /tleft
        set gc.main.itemColors {PASS=GREEN,FAIL=RED}
        gcontrol text idtext "INSTRUCTIONS" inst 24 10 /title
        gcontrol menu testcase "NeXtOpt test cases" "1. baseline,&
                                                     2. <ctrl>+c and hung process"

      textdisplay/exit=NONE/id=tdisp0 text=""
      textdisplay/exit=NONE/id=tdisp  text="<First select a test case>"

      call display_setup_instructions tdisp0
    pipe off
  endif

endmacro

!******************************************************************************!
procedure processMessage m:msg
  if msg.name eqs "MAIN" then
    assert/text="^reg.main.name:  User indicated" msg.data eqss "PASS"
    pipe stop
  elseif msg.name eqs "testcase" then
    message send tdisp ,, "CLEAR"

    if     msg.data eqss "1. baseline" then
      call baseline_tests
    elseif msg.data eqss "2. <ctrl>+c and hung process" then
      call test_ctrl_c
    else
      message send tid ,, "APPENDL" ,, "WARN: Unknown test specified: ^msg.data"
    endif

  endif
return

!******************************************************************************!
! Set the dat directory
!******************************************************************************!
procedure setDatDir
  invoke nxmMajor env.nmversion.subString(0,1)
  invoke major java.lang.Integer.valueOf(nxmMajor)
  if major GE 4 then
    invoke s:datDir           nxm.sys.test.DatLocator.getDatDirLocation()
    invoke s:datDirSlash      nxm.sys.test.DatLocator.getDatDirSlashLocation()
    invoke z:sysDatFull       nxm.sys.test.DatLocator.sysDatFull()
    invoke z:sysTestDatExists nxm.sys.test.DatLocator.sysTestDatExists()
  else
    set s:datDir           "nxm.sys.dat"
    set s:datDirSlash      "nxm/sys/dat"
    set z:sysDatFull       true
    set z:sysTestDatExists false
  endif
return

!******************************************************************************!
! NeXtOpt setup instructions
!******************************************************************************!
procedure display_setup_instructions s:tid
  message send tid ,, "APPENDL" ,, " 0: Skip this test if X-Midas is not available on your platform (e.g. Windows)"
  message send tid ,, "APPENDL" ,, "    OR if NeXtMidas is being run from a jar file"
  message send tid ,, "APPENDL" ,, " 1. Start X-Midas and enter XM shell (in another OS terminal)"
  message send tid ,, "APPENDL" ,, "    $ xmstart "
  message send tid ,, "APPENDL" ,, "    $ xm "
  message send tid ,, "APPENDL" ,, " 2. Define NXM option tree"
  message send tid ,, "APPENDL" ,, "    X-Midas> xmopt nxm $NMROOT/nxm/sys "
  message send tid ,, "APPENDL" ,, " 3. Add NXM to the X-Midas path"
  message send tid ,, "APPENDL" ,, "    X-Midas> xmpath +nxm "
  message send tid ,, "APPENDL" ,, " 4. Build NXM (NeXtOpt)"
  message send tid ,, "APPENDL" ,, "    X-Midas> xmbopt nxm "
  message send tid ,, "APPENDL" ,, ""
return

!******************************************************************************!
! Run baseline/interactive confidence test for NeXtOpt
!******************************************************************************!
procedure baseline_tests s:tid[tdisp]
  res altinstr ""
  if alttestenv then
    res altinstr "/ALTTESTENV"
  endif
  message send tid ,, "APPENDL" ,, "NeXtOpt Baseline/Interactive Confidence Tests"
  message send tid ,, "APPENDL" ,, "---------------------------------------------"
  message send tid ,, "APPENDL" ,, " 1. Run baseline confidence tests for NeXtOpt"
  message send tid ,, "APPENDL" ,, "    X-Midas> home $NMROOT/nxm/sys/test "
  message send tid ,, "APPENDL" ,, "    X-Midas> mtranslate %confidence "
  message send tid ,, "APPENDL" ,, "    X-Midas> %confidence/baseline^{altinstr}"
  message send tid ,, "APPENDL" ,, ""
  message send tid ,, "APPENDL" ,, " 2. Follow instructions and look at output to see if there are any FAILures"
  message send tid ,, "APPENDL" ,, ""
return

!******************************************************************************!
procedure test_ctrl_c s:tid[tdisp]
  message send tid ,, "APPENDL" ,, " NeXtOpt tests for BUG 2274 and BUG 2679"
  message send tid ,, "APPENDL" ,, "----------------------------------------"
  message send tid ,, "APPENDL" ,, " 1. Launch NXM JVM"
  message send tid ,, "APPENDL" ,, "    X-Midas> nm/bg/pname=NXM_JVM/on/id=999 "
  message send tid ,, "APPENDL" ,, " 2. Launch NeXtMidas command (i.e. LIST2 apenny.prm)"
  message send tid ,, "APPENDL" ,, "    X-Midas> nm/bg/pname=NXM_LIST2 list2 ^{datDir}.apenny.prm "
  message send tid ,, "APPENDL" ,, " 3. Find NeXtOpt processes (there should be two NXM_ processes running)"
  message send tid ,, "APPENDL" ,, "    ps -ef | grep NXM_ | grep -v grep "
  message send tid ,, "APPENDL" ,, " 4. Kill the JVM via nm/off "
  message send tid ,, "APPENDL" ,, "    X-Midas> nm/off "
  message send tid ,, "APPENDL" ,, " 5. Find NeXtOpt processes (there should NOT be any NXM_ processes running)"
  message send tid ,, "APPENDL" ,, "    ps -ef | grep NXM_ | grep -v grep "
  message send tid ,, "APPENDL" ,, " 6. Re-launch NXM JVM"
  message send tid ,, "APPENDL" ,, "    X-Midas> nm/bg/pname=NXM_JVM/on/id=999 "
  message send tid ,, "APPENDL" ,, " 7. Again, launch NeXtMidas command (i.e. LIST2 apenny.prm)"
  message send tid ,, "APPENDL" ,, "    X-Midas> nm/bg/pname=NXM_LIST2 list2 ^{datDir}.apenny.prm "
  message send tid ,, "APPENDL" ,, " 8. Press CTRL+C in the X-Midas prompt"
  message send tid ,, "APPENDL" ,, "    X-Midas> <ctrl>+c"
  message send tid ,, "APPENDL" ,, " 9. Find NeXtOpt processes (there should NOT be any NXM_ processes running)"
  message send tid ,, "APPENDL" ,, "    ps -ef | grep NXM_ | grep -v grep "
  message send tid ,, "APPENDL" ,, ""
return

