!
! Test various NEW commands
!
! @bug     2204
! @author  J Bauer
!
startmacro

  call setDatDir

  !
  !  Dependencies: TIMEX
  !
  timex now tstring
  set D:td ^tString
  new nxm.sys.lib.Time t {fromJ1950=^td}
  assert/text="^reg.main.name: creation of sys.lib.Time class" "^t" EQS "^tString" 

  !
  !  Get the current time from a Time class reference and from TIMEX
  !
  new nxm.sys.lib.Time tref
  invoke tCur1 tref.current()
  timex now tStr
  set D:tCur2 ^tStr
  calc tDiff tCur2 tCur1 -
  assert/text="^reg.main.name: current time from Time class reference" &
      tCur1 GT 0 AND tCur2 GT 0 AND tDiff LT 1

  ! Next test added with NeXtMidas 2.1.0
  new java.lang.Integer(4) newInteger
  assert/text="^reg.main.name: Constructor for java.lang.Integer(4)" &
       "^{newInteger.class}" eqs "class java.lang.Integer" and ^newInteger eq 4

  call test_newArray    ! Bug 2204
  call test_newTextFile ! BUG 2851
endmacro

!******************************************************************************!
! If an unexpected exception is caught, mark test as failed, unless it is expected
!******************************************************************************!
procedure processException m:emsg
  if expectedException REXISTS then
    assert/text="^reg.main.name: Expected ERROR starts with: ^{expectedException}" expectedException EQSS emsg.data.message
    remove expectedException
  else
    assert/text="^reg.main.name: Caught unexpected exception ^{emsg.data}" "FALSE" isTrue
    if "Trace" SUBS ENV.DEBUG then invoke ,, emsg.data.printStackTrace()
  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

!*****************************************************************************
! Test creation of an arry from the macro language using new (BUG 2204)
procedure test_newArray
  ! 1D array when given inital values
  new int[] array {1,2,3}
  set index 0
  assert/text="^reg.main.name: initialized array values: " array(0) eq 1 AND array(1) eq 2 AND array(2) eq 3
  assert/text="^reg.main.name: initialized array size: "   array.size eq 3
  
  ! 1D array and not given initial values
  new java.lang.String[5] array1D
  set array1d(2) "boom"
  assert/text="^reg.main.name: 1d array size: "   array1D.size eq 5
  assert/text="^reg.main.name: 1d array set String value: "   array1D(2) eqs "boom"
  
  ! 2D array
  new java.lang.Double[2][4] array2D
  res sub1 array2D(0)
  res sub2 array2D(1)
  new java.lang.Double(99.5) dVal
  set sub2(3) dVal  ! cannot use carat, since we need reference, not value
  assert/text="^reg.main.name: 2d array size: "   array2D.size eq 2
  assert/text="^reg.main.name: 2d dimension size: "   sub1.size eq 4
  assert/text="^reg.main.name: 2d array set Double value: "   ^{sub2(3)} eq 99.5
  
  ! 8D array
  new java.lang.String[3][6][9][2][5][7][6][15] array8D
  res sub3           array8D(2)   ! dim 1
  res sub32          sub3(1)      ! dim 2
  res sub326         sub32(5)     ! dim 3
  res sub3261        sub326(0)    ! dim 4
  res sub32614       sub3261(3)   ! dim 5
  res sub326143      sub32614(2)  ! dim 6
  res sub3261435     sub326143(4) ! dim 7
  set sub3261435(14) "Made it!"   ! set value in dim 8  
  assert/text="^reg.main.name: 8d array size: "   array8D.size eq 3
  assert/text="^reg.main.name: 8d dimension size: "   sub3261435.size eq 15
  assert/text="^reg.main.name: 8d array set String value: "   sub3261435(14) eqs "Made it!"

return

!*****************************************************************************
! test for BUG 2851
procedure test_newTextFile
  ! in 3.5.1+, Shell implements MidasReference so that will call new constructor
  new nxm.sys.lib.TextFile(reg.shell, ^{datDir}.cities.txt) tf
  assert/text="^reg.main.name: TextFile(ref,filename) constructor" tf rexists
  rem tmpstr
  res tmpstr tf.toString()
  assert/text="^reg.main.name: above TextFile object's toString() is ok" tmpstr rexists
  remove tf tmpstr

  ! use deprecated constructor by using bogus reference Object
  new java.lang.Object objRef
  new nxm.sys.lib.TextFile(objRef, ^{datDir}.cities.txt) tf2
  assert/text="^reg.main.name: TextFile(objectRef,filename) constructor" tf2 rexists
  res tmpstr tf2.toString()
  assert/text="^reg.main.name: above TextFile2 object's toString() is ok" tmpstr rexists
  remove objRef tf2 tmpstr

return
