! The X-Midas side of the messaging tests when running NeXtMidas as an
! X-Midas option tree. See the X-Midas primitive of the same name for
! the other side of these tests.
!
!  /ID=999  - ID of the shared NeXtMidas session.
!  /ID=1    - This macro.
!  /ID=2    - Junk id this macro will send a message to.
!  /ID=3    - Junk id the nxm macro/primitive will send a message to.
!  /ID=4    - The NeXtMidas macro/primitive.
!  /ID=5    - Extra NeXtMidas macro when /MULTI is used.
!  /ID=6    - Extra NeXtMidas macro when /MULTI is used.
!  /ID=7    - X-Midas host primitive.
!
!  @author  J. Czechowski IV
!  @version $Id: test_xmmessage.txt,v 1.16 2011/01/06 23:00:38 ntn Exp $
!  @since   NeXtMidas 2.1.1
!
startmacro u:resTests["numTested"] u:resFails["numFailed"]
  local test_count,fail_count
  local name,id,info,args,type,val1,val2,val3,val4,val5,val6
  local item,longstr32,longstr36,xmid,expty
  local baseln       ! Baseline testing?
  local interAct     ! Interactive?

  switch BASELINE    baseln GET               ! Baseline testing?
  switch INTERACTIVE interAct GET             ! Interactive?
  if baseln   eq -1 then res baseln    1      ! switch not specified, default to enable Baseline testing
  if interAct eq -1 then res interAct ^baseln ! switch not specified, default to ^baseline

  res longstr12 "abcdefghijkl"
  res longstr32 "abcdefghijklmnopqrstuzwxyz012345"
  res longstr36 "abcdefghijklmnopqrstuzwxyz0123456789"

  res test_count 0
  res fail_count 0
  res L:xmid 1      ! X-Midas ID to send messages to from NeXtMidas.

if interAct le 0 then
  say "INFO: Run %test_xmmessage/baseline or with /interactive to enable additional tests that requires X11 display"
else

  ! make sure current messages queue is down before calling message up
  message down

  message up

  nm/on/bg/id=999
  nm debug on trace
  xpipe/controls on
    if /host gt 0
      res L:xmid 7
!!!      nxmoptmsg/id=7 4 2
    endif

    nm home test sys
    if /multi gt 0
      nm %test_xmmessage/id=4 XMID="XM.^xmid"
      nm %test_xmmessage/nosend/id=5 XMID="XM.^xmid"
      nm %test_xmmessage/nosend/id=6 XMID="XM.^xmid"
    else
      nm %test_xmmessage/id=4  XMID="XM.^xmid"
    endif

  xpipe off
  nm/off
  message down

endif

  res ^resTests test_count
  res ^resFails fail_count
endmacro

startcontrols
  label initialentry
    xcontrol/lab=domenu menu item ,, "Running;Cancel"

    ! Send messages to NeXtMidas macro.
    message send "TEST0" 2 4 0                           ! Message not sent to NeXtMidas macro

    if /host gt 0
      ! Host runs tests.
    else
      call send_messages 4
      if /multi gt 0 then
        call send_messages 5
        call send_messages 6
      endif
    endif

    ! Wait for the NeXtMidas macro to finish
    if /multi gt 0 then
      call waitfor 4
      call waitfor 5
      call waitfor 6
    else
      call waitfor 4
    endif

    remove/quiet name id info args type val1 val2 val3
    message/wait=5 getw name id info args type val1 val2 val3
    call assert_str "Got TESTN1 found name=TESTN1"       test_count fail_count "TESTN1"  name
    call assert_num "Got TESTN1 found id=4"              test_count fail_count 4         id
    call assert_num "Got TESTN1 found info=10"           test_count fail_count 10        info
    call assert_num "Got TESTN1 found args=0"            test_count fail_count 0         args
    call assert_str "Got TESTN1 found type=' '"          test_count fail_count ""        type

    remove/quiet name id info args type val1 val2 val3
    message/wait=5 getw name id info args type val1 val2 val3
    call assert_str "Got TESTN2 found name=TESTN2"       test_count fail_count "TESTN2"  name
    call assert_num "Got TESTN2 found id=4"              test_count fail_count 4         id
    call assert_num "Got TESTN2 found info=20"           test_count fail_count 20        info
    call assert_num "Got TESTN2 found args=3"            test_count fail_count 3         args
    call assert_str "Got TESTN2 found type=DAD"          test_count fail_count "DAD"     type
    call assert_num "Got TESTN2 found val1=10.0"         test_count fail_count 10.0      val1
    call assert_str "Got TESTN2 found val2=Twenty"       test_count fail_count "Twenty"  val2
    call assert_num "Got TESTN2 found val3=30.0"         test_count fail_count 30.0      val3

    remove/quiet name id info args type val1 val2 val3 val4
    message/wait=5 getw name id info args type val1 val2 val3 val4
    call assert_str "Got TESTN3 found name=TESTN3"       test_count fail_count "TESTN3"  name
    call assert_num "Got TESTN3 found id=4"              test_count fail_count 4         id
    call assert_num "Got TESTN3 found info=0"            test_count fail_count 0         info
    call assert_num "Got TESTN3 found args=4"            test_count fail_count 4         args
    call assert_str "Got TESTN3 found type=LS[12]S[32]L" test_count fail_count "LS[12]S[32]L" type
    call assert_num "Got TESTN3 found val1=1"            test_count fail_count 1         val1
    call assert_str "Got TESTN3 found val2=longstr12"    test_count fail_count longstr12 val2
    call assert_str "Got TESTN3 found val3=longstr32"    test_count fail_count longstr32 val3
    call assert_num "Got TESTN3 found val4=2"            test_count fail_count 2         val4

    remove/quiet name id info args type val1 val2 val3 val4 val5 val6
    message/wait=5 getw name id info args type val1 val2 val3 val4 val5 val6
    call assert_str "Got TESTN4 found name=TESTN4"       test_count fail_count "TESTN4"  name
    call assert_num "Got TESTN4 found id=4"              test_count fail_count 4         id
    call assert_num "Got TESTN4 found info=0"            test_count fail_count 0         info
    call assert_num "Got TESTN4 found args=6"            test_count fail_count 6         args
    call assert_str "Got TESTN4 found type=2L2DFF"       test_count fail_count "2L2DFF"  type
    call assert_num "Got TESTN4 found val1=1"            test_count fail_count 1         val1
    call assert_num "Got TESTN4 found val2=2"            test_count fail_count 2         val2
    call assert_num "Got TESTN4 found val3=3.3"          test_count fail_count 3.3       val3
    call assert_num "Got TESTN4 found val4=4.4"          test_count fail_count 4.4       val4
    call assert_num "Got TESTN4 found val5=5.5"          test_count fail_count 5.5       val5
    call assert_num "Got TESTN4 found val6=6.6"          test_count fail_count 6.6       val6

    remove/quiet name id info args type val1 val2 val3
    message/wait=5 getw name id info args type val1 val2 val3
    call assert_str "Got SETL found name=SETL"           test_count fail_count "SETL"    name
    call assert_num "Got SETL found id=4"                test_count fail_count 4         id
    call assert_num "Got SETL found info=30"             test_count fail_count 30        info
    call assert_num "Got SETL found args=3"              test_count fail_count 3         args
    call assert_str "Got SETL found type=A2L"            test_count fail_count "A2L"     type
    call assert_str "Got SETL found val1=NmObj"          test_count fail_count "NmObj"   val1
    call assert_num "Got SETL found val2=20.0"           test_count fail_count 20.0      val2
    call assert_num "Got SETL found val3=30.0"           test_count fail_count 30.0      val3

    remove/quiet name id info args type val1 val2 val3
    message/rid=2  getr name id info args type val1 val2 val3
    call assert_str "TEST0 found for /ID=2 name=TEST0"   test_count fail_count "TEST0" name
    call assert_num "TEST0 found for /ID=2 id=1"         test_count fail_count 1       id
    call assert_num "TEST0 found for /ID=2 info=4"       test_count fail_count 4       info
    call assert_num "TEST0 found for /ID=2 args=0"       test_count fail_count 0       args
    call assert_str "TEST0 found for /ID=2 type=' '"     test_count fail_count ""      type

    remove/quiet name id info args type val1 val2 val3
    message/rid=3  getr name id info args type val1 val2 val3
    call assert_str "TESTN0 found for /ID=3 name=TESTN0" test_count fail_count "TESTN0" name
    call assert_num "TESTN0 found for /ID=3 id=4"        test_count fail_count 4        id
    call assert_num "TESTN0 found for /ID=3 info=0"      test_count fail_count 0        info
    call assert_num "TESTN0 found for /ID=3 args=0"      test_count fail_count 0        args
    call assert_str "TESTN0 found for /ID=3 type=' '"    test_count fail_count ""       type

    ! This next message is a test for Bug 431 (type field length >8 chars)
    remove/quiet name id info args type val1 val2 val3 val4
    message/rid=3  getr name id info args type val1 val2 val3 val4
    res expty "S[80]S[40]S[8]S[128]"
    call assert_str "RERR found for /ID=3 name=RERR"   test_count fail_count "RERR"   name
    call assert_num "RERR found for /ID=3 id=4"        test_count fail_count 4        id
    call assert_num "RERR found for /ID=3 info=0"      test_count fail_count 0        info
    call assert_num "RERR found for /ID=3 args=4"      test_count fail_count 4        args
    call assert_str "RERR found for /ID=3 type=^expty" test_count fail_count "^expty" type
    call assert_str "RERR found for /ID=3 val1=MYTEXT" test_count fail_count "MYTEXT" val1
    call assert_str "RERR found for /ID=3 val2=MYADDR" test_count fail_count "MYADDR" val2
    call assert_str "RERR found for /ID=3 val3=MYFUNC" test_count fail_count "MYFUNC" val3
    call assert_str "RERR found for /ID=3 val4=MYDATA" test_count fail_count "MYDATA" val4

    ! Check to see that all tests on the NeXtMidas side passed.

    call assert_str "NeXtMidas messages tests for /ID=4" test_count fail_count "PASS" status_4
    rem/quiet done_4 status_4
    if /multi gt 0 then
      call assert_str "NeXtMidas messages tests for /ID=5" test_count fail_count "PASS" status_5
      rem/quiet done_5 status_5
      call assert_str "NeXtMidas messages tests for /ID=6" test_count fail_count "PASS" status_6
      rem/quiet done_6 status_6
    endif

    stop
  return

  label domenu
    if item eq 1 then
      stop
    endif
  return
endcontrols

! Sends the messages.
subroutine send_messages l:id
  local longstr12, longstr32
  res longstr12 "abcdefghijkl"
  res longstr32 "abcdefghijklmnopqrstuzwxyz012345"

  message send "TEST1" ^id 99 0                             ! Unconfigured message no data
  message send "TEST2" ^id  7 3 DAD     1.0 "Two" 3.0       ! Unconfigured message with data
!!!  message send "TEST3" ^id  0 3 S[32]LL "^longstr32" 1 2    ! Unconfigured message with string
  message send "TEST3" ^id  0 4 LS[12]S[32]L 1 "^longstr12" "^longstr32" 2    ! Unconfigured message with string
  message send "TEST4" ^id  0 6 2L2DFF  1 2 3.3 4.4 5.5 6.6 ! Unconfigured message type multipliers
  message send "SETL"  ^id 42 3 ALL     "MyObj" 1 2         ! Matches configured message
endsubroutine


! Waits for a given NeXtMidas macro to finish
subroutine waitfor l:id
  local count
  local check

  calc/quiet count 0
  while done_^{id} eqs "FALSE" and count lt 100
    calc/quiet check count 25 mod
    if count gt 0 and check eq 0 then
      say "Waiting for NeXtMidas macro to complete"
    endif
    pause/quiet 0.1
    calc/quiet count count 1 +
  endwhile
endsubroutine

include "%xm_assert" ! Includes the assertion subroutines
