startmacro s:func[""]
!
! Macro to test "SEDIT" intrinsic
!
if func NEQS "" then
  call test_^{func}
  stop
endif

call test_between
call test_bsearch
call test_clean
call test_element
call test_ends
call test_ext
call test_gsubstitute
call test_head
call test_join
call test_key
call test_length
call test_locase
call test_nelem
call test_nform
call test_nsearch
call test_pad
call test_parse
call test_parseall
call test_parseargs
call test_parsedindex
call test_range
call test_root
call test_search
call test_select
call test_split
call test_starts
call test_strim
call test_strip
call test_substitute
call test_tail
call test_trim
call test_upcase
call test_word
call test_multi_function
call test_force
call test_dr_1628

endmacro

!******************************************************************************!
procedure test_between
  call test_between_zerobased
  call test_between_onebased
return

!******************************************************************************!
procedure test_between_zerobased

result baseSwitch "/ZEROBASE"
result func "BETW"
res NumTests 9
do i 1 NumTests
 if i EQ 1 then
  res answer "This is"
  SEDIT^baseSwitch "This is a string" str "^func" 0 6

 elseif i EQ 2 then
  res answer "This is"
  SEDIT^baseSwitch "This is a string" str "^func" 6 0

 elseif i EQ 3 then
  res answer "string"
  res instr "This is a string"
  SEDIT^baseSwitch instr str "^func" ^instr.length -5

 elseif i EQ 4 then
  res answer ""
  SEDIT^baseSwitch "This is a string" str "^func" 100 200

 elseif i EQ 5 then
  res answer "This is a string"
  SEDIT^baseSwitch "This is a string" str "^func" 0 200

 elseif i EQ 6 then
  res answer "This "
  SEDIT^baseSwitch "This is a string" str "^func" 4 -100

 elseif i EQ 7 then
  res answer ""
  res instr "This is a string"
  SEDIT^baseSwitch instr str "^func" ^instr.length ^instr.length

 elseif i EQ 8 then
  res answer "g"
  res instr "This is a string"
  SEDIT^baseSwitch instr str "^func" ^instr.length-1 ^instr.length-1

 elseif i EQ 9 then
  res answer "T"
  SEDIT^baseSwitch instr str "^func" 0 0
 endif

 assert/text="SEDIT^baseSwitch: ^func test ^i" "^str" EQS "^answer"

enddo

return
!******************************************************************************!
procedure test_between_onebased

result baseSwitch "/ONEBASE"
result func "BETW"
res NumTests 8
do i 1 NumTests
 if i EQ 1 then
  res answer "This is"
  SEDIT^baseSwitch "This is a string" str "^func" 1 7

 elseif i EQ 2 then
  res answer "This is"
  SEDIT^baseSwitch "This is a string" str "^func" 7 1

 elseif i EQ 3 then
  res answer "string"
  res instr "This is a string"
  SEDIT^baseSwitch instr str "^func" ^instr.length -5

 elseif i EQ 4 then
  res answer ""
  SEDIT^baseSwitch "This is a string" str "^func" 100 200

 elseif i EQ 5 then
  res answer "This"
  SEDIT^baseSwitch "This is a string" str "^func" 4 -100

 elseif i EQ 6 then
  res answer "g"
  res instr "This is a string"
  SEDIT^baseSwitch instr str "^func" ^instr.length ^instr.length

 elseif i EQ 7 then
  res answer ""
  SEDIT^baseSwitch instr str "^func" 0 0

 else
   break
 endif
 assert/text="SEDIT^baseSwitch: ^func test ^i" "^str" EQS "^answer"

enddo

return

!******************************************************************************!
procedure test_bsearch
  call test_bsearch_zerobased
  call test_bsearch_onebased
return

!******************************************************************************!
procedure test_bsearch_zerobased

res switches "/ZB"
res i 1
res answer 10
result func "BSEA"
SEDIT^switches "This is a string" idx "^func" "string"
assert/text="SEDIT^switches: ^func test ^i" idx EQ answer

res i 2
res answer -1
SEDIT^switches "This is a string" idx "^func" "STRING"
assert/text="SEDIT^switches: ^func test ^i" idx EQ answer

return
!******************************************************************************!
procedure test_bsearch_onebased

res switches "/OB"
res i 1
res answer 11
result func "BSEA"
SEDIT^switches "This is a string" idx "^func" "string"
assert/text="SEDIT^switches: ^func test ^i" idx EQ answer

res i 2
res answer -1  !  0 will be DEPRECATED
SEDIT^switches "This is a string" idx "^func" "STRING"
assert/text="SEDIT^switches: ^func test ^i" idx EQ answer

return
!******************************************************************************!
procedure test_clean
  res answer "THIS,IS,A,STRING"
  res i 1
  result func "CLEAN"
  SEDIT  "This is a string" str "^func"
  assert/text="SEDIT: ^func test ^i" str EQS "^answer"
return
!******************************************************************************!
procedure test_element

  result func "ELEM"
  res answer "is"
  res i 1
  SEDIT "This/is/a//string" elm "^func" 2 "/"
  assert/text="SEDIT: ^func test ^i, delim '/'" "^elm" EQS "^answer"

  res answer "bcd 3456"
  res i 2
  SEDIT "a 12,bcd 3456" elm "^func" 2 ","
  assert/text="SEDIT: ^func test ^i, delim ','" "^elm" EQS "^answer"

  res answer "12,bcd"
  res i 3
  SEDIT "a 12,bcd 3456" elm "^func" 2 " "
  assert/text="SEDIT: ^func test ^i, delim ' '" "^elm" EQS "^answer"

  ! This is testing that a result name can be given instead of a
  ! number for the element number.
  res answer "is"
  res i 4
  SEDIT "This,is,a,string" elm "^func" 2
  assert/text="SEDIT: ^func test ^i, delim ','" "^elm" EQS "^answer"

  result func "ELEM"
  res answer "is"
  res i 5
  SEDIT "This:is:a::string" elm "^func" 2 ":"
  assert/text="SEDIT: ^func test ^i, delim ':'" "^elm" EQS "^answer"


  res i 6
  res answer "20:18:48.572"
  SEDIT "2005:06:10::20:18:48.572" elm "^func" 2 "::"
  assert/text="SEDIT: ^func test ^i, delim '::'" "^elm" EQS "^answer"

  res i 7
  res answer "2005:06:10"
  SEDIT "2005:06:10::20:18:48.572" elm "^func" 1 "::"
  assert/text="SEDIT: ^func test ^i, delim '::'" "^elm" EQS "^answer"

return
!******************************************************************************!
procedure test_ends
  res func "ENDS"
  res lowersuffix "suffix"
  res uppersuffix "SUFFIX"
  res lowerstring "prefix first, then suffix"
  res upperstring "PREFIX FIRST, THEN SUFFIX"
  
  res answer "true"
  SEDIT lowerstring  boolres "^func" lowersuffix
  assert/text="SEDIT: ^func" "^boolres" EQS "^answer"
  
  res answer "false"
  SEDIT upperstring  boolres "^func" lowersuffix
  assert/text="SEDIT: ^func" "^boolres" EQS "^answer"
  
  res answer "false"
  SEDIT lowerstring  boolres "^func" uppersuffix
  assert/text="SEDIT: ^func" "^boolres" EQS "^answer"
  
  res answer "true"
  SEDIT upperstring  boolres "^func" uppersuffix
  assert/text="SEDIT: ^func" "^boolres" EQS "^answer"
  
return
!******************************************************************************!
procedure test_ext
  result func "EXT"
  res answer "txt"
  res i 1
  SEDIT "/home/user/myfilename.txt" str "^func"
  assert/text="SEDIT: ^func test ^i" "^str" EQS "^answer"
return
!******************************************************************************!
procedure test_gsubstitute

  result func "GSUBS"

  res answer "That at a string"
  SEDIT "This is a string" str "^func" "is" "at"
  res i 1
  assert/text="SEDIT: ^func test ^i" "^str" EQS "^answer"

  res answer "Thisxxisxxaxxstring"
  res i 2
  SEDIT "This is a string" str "^func" " " "xx"
  assert/text="SEDIT: ^func test ^i" "^str" EQS "^answer"

  res answer "Th  a string"
  res i 3
  SEDIT "This is a string" str "^func" "is" ""
  assert/text="SEDIT: ^func test ^i" "^str" EQS "^answer"

  res answer "Th    a string"
  res i 4
  SEDIT "This is a string" str "^func" "is" " "
  assert/text="SEDIT: ^func test ^i" "^str" EQS "^answer"

return
!******************************************************************************!
procedure test_head
  result func "HEAD"

  res answer "/home/user/"
  res i 1
  SEDIT "/home/user/myfilename.txt" str "^func"
  assert/text="SEDIT: ^func test ^i" "^str" EQS "^answer"

return
!******************************************************************************!
procedure test_join

  result func "JOIN"

  SEDIT "A string to split" word SPLIT " "
  SEDIT word joinedString JOIN " "
  assert/text="SEDIT: ^func -- split then join" "^joinedString" EQS "A string to split"
return
!******************************************************************************!
procedure test_key
  result func "KEY"

  do i 1 3
    if i EQ 1 then
      ! Get last tag in a string
      result answer "Springfield"
      SEDIT "NAME=Homer CITY=Springfield" str "^func" "CITY"

    elseif i EQ 2 then
      ! Get a tag with a space after the =
      result answer ""
      SEDIT "NAME=Homer CITY= Springfield" str "^func" "CITY"

    elseif i EQ 3 then
      ! Get a tag with a a delimitter other than "="
      result answer "Homer"
      SEDIT "NAME/Homer CITY/Springfield" str "^func" "NAME" "/"

    endif
    assert/text="SEDIT: ^func test ^i" "^str" EQS "^answer"
  enddo
return
!******************************************************************************!
procedure test_length
  call test_length_zerobased
  call test_length_onebased
return
!******************************************************************************!
procedure test_length_zerobased

result baseSwitch "/ZEROBASE"
result func "LEN"

do i 1 5
  if i EQ 1 then
    res answer 4
    SEDIT^baseSwitch "This is a string" lstr "^func" 0

  elseif i EQ 2 then
    res answer 9
    SEDIT^baseSwitch "This is a string" lstr "^func" 2

  elseif i EQ 3 then
    res answer 16
    SEDIT^baseSwitch "This is a string" lstr "^func" -1

  elseif i EQ 4 then
    res answer 16
    SEDIT^baseSwitch "This is a string" lstr "^func"
  else
    break
  endif
  assert/text="SEDIT^baseSwitch: ^func test ^i" lstr EQ answer

enddo
return
!******************************************************************************!
procedure test_length_onebased

result baseSwitch "/ONEBASE"
result func "LEN"

res i 1
res answer 16
SEDIT^baseSwitch "This is a string" lstr "^func" 0
assert/text="SEDIT^baseSwitch: ^func test ^i" lstr EQ answer

res answer 9
SEDIT^baseSwitch "This is a string" lstr "^func" 3
res i 2
assert/text="SEDIT^baseSwitch: ^func test ^i" lstr EQ answer

return
!******************************************************************************!
procedure test_locase

result func "LOCA"

res answer "this is a string"
res i 1
SEDIT "This is a string" str "^func"
assert/text="SEDIT: ^func test ^i" "^str" EQS "^answer"

return
!******************************************************************************!
procedure test_nelem
 result func "NELEM"

 res L:answer 0
 SEDIT "" elms "^func"
 assert/text="SEDIT: ^func, DELIM=, empty string, no commas" elms EQ answer

 res L:answer 0
 SEDIT "NULL" elms "^func"
 assert/text="SEDIT: ^func, DELIM=, NULL, no commas" elms EQ answer

 res L:answer 1
 SEDIT " " elms "^func"
 assert/text="SEDIT: ^func, space, no commas" elms EQ answer

 res L:answer 2
 SEDIT "," elms "^func"
 assert/text="SEDIT: ^func, no text, single comma" elms EQ answer

 res L:answer 2
 SEDIT " " elms "^func" " "
 assert/text="SEDIT: ^func, single blank, single blank delimiter" elms EQ answer

 res L:answer 2
 SEDIT "x,y" elms "^func"
 assert/text="SEDIT: ^func, text, single comma" elms EQ answer

 res L:answer 3
 SEDIT "blank separated string" elms "^func" " "
 assert/text="SEDIT: ^func, blank separated string" elms EQ answer

 res L:answer 4
 SEDIT "blanks and commas,separated string" elms "^func" " "
 assert/text="SEDIT: ^func, blanks and commas" elms EQ answer

 res L:answer 5
 SEDIT "    " elms "^func" " "
 assert/text="SEDIT: ^func, four(4) blanks" elms EQ answer

! Testing that a string can be given as a delimiter instead of a single character.
 res L:answer 7
 SEDIT "Kirk and Spock and McCoy and Scott and Sulu and Chekov and Uhura" elms "^func" " and "
 assert/text="SEDIT: ^func, string delimiter" elms EQ answer
 
return
!******************************************************************************!
procedure test_pad
!
! Relative pad RIGHT with default character
!
set S:answer "a string   "
sedit "a string" str PAD +3
assert/text="SEDIT: relative PAD RIGHT with three blanks" str EQS answer
!
! ABSOLUTE pad LEFT with dots
!
set S:answer "............a string"
sedit "a string" str PADL 20 .
assert/text="SEDIT: absolute PAD LEFT with dots" str EQS answer

!
! ABSOLUTE pad BOTH with dots. The extra dot should go on the RIGHT.
!
set S:answer ".....oddstring......"
sedit "oddstring" str PADB 20 .
assert/text="SEDIT: absolute PAD BOTH with dots" str EQS answer

return
!******************************************************************************!
procedure test_nform

result func "NFORM"

!
!  NFORM/JAVA - TEST 1
!
result D:dval 1.12345678
res answer "1.123"
res L:i 1
SEDIT dval str "^func" "#.###"
assert/text="SEDIT: ^func test ^i" "^str" EQS "^answer"

!
!  NFORM/JAVA - TEST 2
!
result D:dval 1
res answer "1.00"
calc i i+1
SEDIT dval str "^func" "0.00"
assert/text="SEDIT: ^func test ^i" "^str" EQS "^answer"

!
!  NFORM/FORTRAN
!
result D:dval 1
res answer "1.00"
res formatStr "(F3.2)"
SEDIT dval str "^func" "^{formatStr}"
assert/text="SEDIT: ^func, format=^formatStr" "^str" EQS "^answer"


!        GEN    - X-Midas GENeral format (no exponent if between 1E-3, 1E15).
!        VIS    - X-Midas VISual  format (no exponent if between 1E-3, 1E7).
!        SCI    - SCIentific notation.
!        ENG    - ENGineering notation   (exponent is a multiple of 3).
!        MAN    - MANtissa notation      (no exponents).
!        DMS    - Deg-Min-Sec angular format.            ddd'mm'ss
!        LAT    - Deg-Min-Sec format for latitude.       ddd'mm'ssN
!        LON    - Deg-Min-Sec format for longitude.      ddd'mm'ssE
!        STD    - STanDard time code format.             [yy]yy:mm:dd::hh:mm:ss
!        ACQ    - ACQisition time code format.           [yy.ddd]:[hh:mm:ss]
!        EPOCH  - EPOCH quadwords for time code.         [yy]yy:sec_in_year
!        NORAD  - NORAD timecode format.                 yyddd.frac_of_day
!        TCR    - TimeCode Reader format.                ddd:hh:mm:ss
!        VAX    - VAX time format.                       dd-MMM-yy[yy]:hh:mm:ss
!        HMS    - Hour-Min-Sec time format.              hh:mm:ss.ffff
!        YMD    - Year-Month-Day format.                 yyyy:mm:dd
!        NET    - 32-bit integer formatted as URL.       127.0.0.1

set numFormatTable {}
set numFormatTable.GEN   {DVAL=123456789.123456789,ANSWER="123456789.1235"}
set numFormatTable.VIS   {DVAL=123456789.123456789,ANSWER="1.234568E+008"}
set numFormatTable.SCI   {DVAL=123456789.123456789,ANSWER="1.234568E+008"}
set numFormatTable.ENG   {DVAL=123456789.123456789,ANSWER="123.456789E+006"}
set numFormatTable.MAN   {DVAL=123456789.123456789,ANSWER="123456789"}
set numFormatTable.DMS   {DVAL=12.2,ANSWER="12'12'00"}
set numFormatTable.LAT   {DVAL=38.35123,ANSWER="38'21'04"}
set numFormatTable.LON   {DVAL=-179.35123,ANSWER="-179'21'04"}
set numFormatTable.STD   {DVAL=1.907462269777E9,ANSWER="2010:06:12::02:37:49.777"}
set numFormatTable.ACQ   {DVAL=1.907462269777E9,ANSWER="10.163:02:37:49.777"}
set numFormatTable.EPOCH {DVAL=1.907462269777E9,ANSWER="2010:14006269.777"}
set numFormatTable.NORAD {DVAL=1.907462269777E9,ANSWER="10163.10960390"}
set numFormatTable.TCR   {DVAL=1.907462269777E9,ANSWER="163:02:37:49.777"}
set numFormatTable.VAX   {DVAL=1.907462269777E9,ANSWER="12-JUN-2010:02:37:49.777"}
set numFormatTable.HMS   {DVAL=1.907462269777E9,ANSWER="02:37:49.777"}
set numFormatTable.YMD   {DVAL=1.907462269777E9,ANSWER="2010:06:12"}
set numFormatTable.NET   {DVAL=0x7f000001,ANSWER="127.0.0.1"}

res func "NFORM"
foreach key intable numFormatTable
  sedit numFormatTable.^{key}.DVAL str "^func" "^key"
  assert/text="SEDIT: ^func, format=^key" "^str" EQS "^numFormatTable.^{key}.ANSWER"
endfor
return
!******************************************************************************!
procedure test_nsearch

result func "NSEA"

res L:answer 14
!      0123456789012345678901234567890
SEDIT "1 and 2 and 3 and 4 and" index "^func" "and" 3
assert/text="SEDIT: ^func, existent occurrence" index EQ answer

res L:answer 12
!      0123456789012345678901234567890
SEDIT "and 1 and 2 and 3 and 4 and" index "^func" "and" 3
assert/text="SEDIT: ^func, first occurrence at 0" index EQ answer

res L:answer -1
SEDIT "1 and 2 and 3 and 4 and" index "^func" "and" 6
assert/text="SEDIT: ^func, nonexistent occurrence" index EQ answer

res L:answer -1
SEDIT "1 and 2 and 3 and 4 and" index "^func" "nothere" 6
assert/text="SEDIT: ^func, nonexistent sub string" index EQ answer

res L:answer 15
SEDIT "1 and 2 and 3 and 4 and" index "^func" "and" 3 /OB
assert/text="SEDIT: ^func, existent occurrence, one based" index EQ answer

return
!******************************************************************************!
procedure test_parse

result func "PARSE"

res answer "IS"
SEDIT "This is a string" str "^func" 2
assert/text="SEDIT: ^func - space delimiter, NO /CLEAN switch" str EQS "^answer"

res answer "string"
SEDIT "This/is/a/string" str "^func" 4 "/" /clean=f
if/cs str EQS "^answer" then
  assert/text="SEDIT: ^func - '/' delimiter, /clean=f" true
else
  assert/text="SEDIT: ^func - '/' delimiter, /clean=f" false
endif

return
!******************************************************************************!
procedure test_parseall

result func "PARSEAL"

SEDIT "This is a string" word "^func" 4
assert/text="SEDIT: ^func test NO CLEAN SWITCH" &
     word(0) EQS "THIS" AND &
     word(1) EQS "IS"   AND &
     word(2) EQS "A"    AND &
     word(3) EQS "STRING"

SEDIT "This is a string" word "^func" 4 /clean=f
if/cs word(0) EQS "This" AND &
      word(1) EQS "is"   AND &
      word(2) EQS "a"    AND &
      word(3) EQS "string" then
  assert/text="SEDIT: ^func test /CLEAN=F" true
else
  assert/text="SEDIT: ^func test /CLEAN=F" false
endif

SEDIT "This-is-a-string" word "^func" 4 "-"
assert/text="SEDIT: ^func '-' delimiter NO CLEAN SWITCH" &
     word(0) EQS "THIS" AND &
     word(1) EQS "IS"   AND &
     word(2) EQS "A"    AND &
     word(3) EQS "STRING"


return
!******************************************************************************!
procedure test_parseargs

result func "PARSEAR"

sedit "PATH FUNC=SET SYS" args ^func
res L:size args.size
assert/text="SEDIT: ^func test ^i" size EQ 2

return

!******************************************************************************!
procedure test_parsedindex
  call test_parsedindex_zerobased
  call test_parsedindex_onebased
return

!******************************************************************************!
procedure test_parsedindex_zerobased

res switches "/ZEROBASE"
result func "PARSEDI"

res NumTests 2
do i 1 NumTests

  if i EQ 1
    res answer 1
    res p1 "is"
  elseif i EQ 2 then
    res answer -1
    res p1 "not"
  endif

  SEDIT^switches  "This is a string" idx "^func" "^p1"
  assert/text="SEDIT^switches: ^func test ^i" idx EQ answer

enddo
return
!******************************************************************************!
procedure test_parsedindex_onebased

res switches "/ONEBASE"
result func "PARSEDI"

res NumTests 2
do i 1 NumTests

  if i EQ 1
    res answer 2
    res p1 "is"
  elseif i EQ 2 then
    res answer -1
    res p1 "not"
  endif

  SEDIT^switches  "This is a string" idx "^func" "^p1"
  assert/text="SEDIT^switches: ^func test ^i" idx EQ answer

enddo
return
!******************************************************************************!
procedure test_range
  call test_range_onebased
  call test_range_zerobased
return
!******************************************************************************!
procedure test_range_onebased

res baseSwitch "/ONEBASE"
result func "RANGE"

res i 1
res answer "This is"
SEDIT^baseSwitch "This is a string" str ^func 1 7
assert/text="SEDIT^baseSwitch: ^func test ^i" "^str" EQS "^answer"

res i 2
res answer ""
SEDIT^baseSwitch "This is a string" str ^func 7 1
assert/text="SEDIT^baseSwitch: ^func test ^i" "^str" EQS "^answer"

res i 3
res answer "string"
res inStr "This is a string"
SEDIT^baseSwitch inStr str ^func -5 ^inStr.length
assert/text="SEDIT^baseSwitch: ^func test ^i" "^str" EQS "^answer"

res i 4
res answer ""
SEDIT^baseSwitch "This is a string" str ^func 100 200
assert/text="SEDIT^baseSwitch: ^func test ^i" "^str" EQS "^answer"

res i 5
res answer ""
SEDIT^baseSwitch "This is a string" str ^func 4 -100
assert/text="SEDIT^baseSwitch: ^func test ^i" "^str" EQS "^answer"

res i 6
res answer ""
SEDIT^baseSwitch "This is a string" str ^func -1 -2
assert/text="SEDIT^baseSwitch: ^func test ^i" "^str" EQS "^answer"

res i 7
res answer "in"
SEDIT^baseSwitch "This is a string" str ^func -2 -1
assert/text="SEDIT^baseSwitch: ^func test ^i" "^str" EQS "^answer"

res i 8
res answer "g"
res inStr "This is a string"
SEDIT^baseSwitch inStr str ^func  ^inStr.length ^inStr.length
assert/text="SEDIT^baseSwitch: ^func test ^i" "^str" EQS "^answer"

! DEPRECATED
!res i 9
!res answer "g"
!res inStr "This is a string"
!SEDIT^baseSwitch inStr str ^func 0 0
!assert/text="SEDIT^baseSwitch: ^func test ^i" "^str" EQS "^answer"

!res i 10
res i 9
res answer "This is a string"
res inStr "This is a string"
SEDIT^baseSwitch inStr str ^func 1
assert/text="SEDIT^baseSwitch: ^func test ^i" "^str" EQS "^answer"


return
!******************************************************************************!
procedure test_range_zerobased

res baseSwitch "/ZEROBASE"
result func "RANGE"

res i 1
res answer "This is"
SEDIT^baseSwitch "This is a string" str ^func 0 6
assert/text="SEDIT^baseSwitch: ^func test ^i" "^str" EQS "^answer"

res i 2
res answer ""
SEDIT^baseSwitch "This is a string" str ^func 6 0
assert/text="SEDIT^baseSwitch: ^func test ^i" "^str" EQS "^answer"

res i 3
res answer "string"
res inStr "This is a string"
SEDIT^baseSwitch inStr str ^func -5 ^inStr.length-1
assert/text="SEDIT^baseSwitch: ^func test ^i" "^str" EQS "^answer"

res i 4
res answer ""
SEDIT^baseSwitch "This is a string" str ^func 100 200
assert/text="SEDIT^baseSwitch: ^func test ^i" "^str" EQS "^answer"

res i 5
res answer ""
SEDIT^baseSwitch "This is a string" str ^func 4 -100
assert/text="SEDIT^baseSwitch: ^func test ^i" "^str" EQS "^answer"

res i 6
res answer ""
SEDIT^baseSwitch "This is a string" str ^func -1 -2
assert/text="SEDIT^baseSwitch: ^func test ^i" "^str" EQS "^answer"

res i 7
res answer "in"
SEDIT^baseSwitch "This is a string" str ^func -2 -1
assert/text="SEDIT^baseSwitch: ^func test ^i" "^str" EQS "^answer"

res i 8
res answer "g"
res inStr "This is a string"
SEDIT^baseSwitch inStr str ^func  ^inStr.length-1 ^inStr.length-1
assert/text="SEDIT^baseSwitch: ^func test ^i" "^str" EQS "^answer"

return
!******************************************************************************!
procedure test_root

  result func "ROOT"

  res i 1
  res answer "myfilename"
  SEDIT "/home/user/myfilename.txt" str ^func
  assert/text="SEDIT: ^func test ^i" "^str" EQS "^answer"

return

!******************************************************************************!
procedure test_search
  call test_search_zerobased
  call test_search_onebased
return

!******************************************************************************!
procedure test_search_zerobased

  result func "SEAR"
  res baseSwitch "/ZEROBASE"
  result inStr "This is a string"
  do i 1 6
    if i EQ 1 then
      result p1 "string"
      result answer 10

    elseif i EQ 2 then
      result p1 "STRING"
      result answer -1
    endif
    SEDIT^baseSwitch "^inStr" idx SEARCH "^p1"
    assert/text="SEDIT^baseSwitch: ^func test ^i" idx EQ answer

  enddo
return
!******************************************************************************!
procedure test_search_onebased

  result func "SEAR"
  res baseSwitch "/ONEBASE"
  result inStr "This is a string"
  do i 1 6
    if i EQ 1 then
      result p1 "string"
      result answer 11

    elseif i EQ 2 then
      result p1 "STRING"
      result answer -1
    endif
    SEDIT^baseSwitch "^inStr" idx ^func "^p1"
    assert/text="SEDIT^baseSwitch: ^func test ^i" idx EQ answer

  enddo
return

!******************************************************************************!
procedure test_select
  call test_select_zerobased
  call test_select_onebased
return

!******************************************************************************!
procedure test_select_zerobased
  result func "SEL"
  res baseSwitch "/ZEROBASE"
  do i 1 3
    if i eq 1 then
      ! Look for an EXISTING substring in a string
      res instr "This,is,a,string"
      res answer 3
      res substr "string"
      sedit^baseSwitch "^inStr" idx ^func "^subStr"
    elseif i eq 2 then
      ! Look for first instance of an EXISTING substring in a string
      ! (since it is a perfect match there will not be a warning for 'ambiguous items match...' NXM-2588)
      res instr "This,is,a,string,with,two,is,substrings"
      res answer 1
      res substr "is"
      sedit^baseSwitch "^inStr" idx ^func "^subStr"
    elseif i eq 3 then
      ! Look for a NON-EXISTING substring in a string
      res instr "This,is,a,string,with,two,is,substrings"
      res answer -1
      res substr "jksdfjkhfj"
      sedit^baseSwitch "^inStr" idx ^func "^subStr"
    else
      warning "^macroName: loop through more ^func tests than are coded"
    endif
    assert/text="SEDIT^baseSwitch: ^func test ^i; expected ^answer got ^idx" idx EQ answer
  enddo
return
!******************************************************************************!
procedure test_select_onebased
  result func "SEL"
  res baseSwitch "/ONEBASE"
  do i 1 3
    if i eq 1 then
      ! Look for an EXISTING substring in a string
      res instr "This,is,a,string"
      res answer 4
      res substr "string"
      sedit^baseSwitch "^inStr" idx ^func "^subStr"
    elseif i eq 2 then
      ! Look for first instance of an EXISTING substring in a string
      ! (since it is a perfect match there will not be a warning for 'ambiguous items match...' NXM-2588)
      res instr "This,is,a,string,with,two,is,substrings"
      res answer 2
      res substr "is"
      sedit^baseSwitch "^inStr" idx ^func "^subStr"
    elseif i eq 3 then
      ! Look for a NON-EXISTING substring in a string
      res instr "This,is,a,string,with,two,is,substrings"
      res answer -1
      res substr "jksdfjkhfj"
      sedit^baseSwitch "^inStr" idx ^func "^subStr"
    else
      warning "^macroName: loop through more ^func tests than are coded"
    endif
    assert/text="SEDIT^baseSwitch: ^func test ^i; expected ^answer got ^idx" idx EQ answer
  enddo
return
!******************************************************************************!
procedure test_split

  result func "SPLIT"

  do i 1 2
    if i EQ 1 then
      SEDIT "A string to split" word ^func " "
      assert/text="SEDIT: ^func test ^i" &
               "^word(0)" EQS "A"      AND &
               "^word(1)" EQS "string" AND &
               "^word(2)" EQS "to"     AND &
               "^word(3)" EQS "split"
    elseif i EQ 2 then
      SEDIT "A,string,to,split" word ^func ","
      assert/text="SEDIT: ^func test ^i" &
               "^word(0)" EQS "A"      AND &
               "^word(1)" EQS "string" AND &
               "^word(2)" EQS "to"     AND &
               "^word(3)" EQS "split"
    else
      warning "^macroName: func ^func; More tests to be coded"
    endif
  enddo
return
!******************************************************************************!
procedure test_starts
  res func "STARTS"
  res lowerprefix "prefix"
  res upperprefix "PREFIX"
  res lowerstring "prefix first, then suffix"
  res upperstring "PREFIX FIRST, THEN SUFFIX"
  
  res answer "true"
  SEDIT lowerstring  boolres "^func" lowerprefix
  assert/text="SEDIT: ^func" "^boolres" EQS "^answer"
  
  res answer "false"
  SEDIT upperstring  boolres "^func" lowerprefix
  assert/text="SEDIT: ^func" "^boolres" EQS "^answer"
  
  res answer "false"
  SEDIT lowerstring  boolres "^func" upperprefix
  assert/text="SEDIT: ^func" "^boolres" EQS "^answer"
  
  res answer "true"
  SEDIT upperstring  boolres "^func" upperprefix
  assert/text="SEDIT: ^func" "^boolres" EQS "^answer"

return
!******************************************************************************!
procedure test_strim

  result func "STRIM"

  res i 1
  res answer "stranded"
  SEDIT "    stranded   " str ^func
  assert/text="SEDIT: ^func test ^i" "^str" EQS "^answer"

return
!******************************************************************************!
procedure test_strip
  ! Test 1
    res   str X"WORD"X

    sedit str str GSUBS "X" ""
    sedit str str STRIP
    result func "STRIP"

    res i 1
    res answer "WORD"
    assert/text="SEDIT: ^func test ^i" "^str" EQS "^answer"

  ! Test 2
    res   str X"WORD

    sedit str str GSUBS "X" ""
    sedit str str STRIP
    result func "STRIP"

    res i 2
    res answer "WORD "
    assert/text="SEDIT: ^func test ^i" "^str" EQS "^answer"


  ! Test 3
    res i 3
    res answer ""
    sedit "" str STRIP
    assert/text="SEDIT: ^func test ^i" "^str" EQS "^answer"

  ! Test 4
    res i 4
    res answer "abc"
    sedit "abc" str STRIP
    assert/text="SEDIT: ^func test ^i" "^str" EQS "^answer"

return
!******************************************************************************!
procedure test_substitute

   result func "SUBS"
   do i 1 6
     if i eq 1
       ! Replace a substring that EXISTS in the string
       res instr "This is a string"
       res answer "That is a string"
       res substr "is"
       res replacestr "at"

     elseif i eq 2
       ! Replace a substring that EXISTS in the string with a space
       res instr "This is a string"
       res answer "Th  is a string"
       res substr "is"
       res replacestr " "

     elseif i eq 3
       ! Replace a substring that EXISTS in the string with a NULL string
       res instr "This is a string"
       res answer "Th is a string"
       res substr "is"
       res replacestr ""

     elseif i eq 4
       ! Replace a substring that DOES NOT EXIST in the string
       res instr "This is a string"
       res answer "This is a string"
       res s:substr "//"
       res replacestr ""

     elseif i eq 5
       ! Replace first substring in a string with only 1 instance of substring
       res instr "//This is a string"
       res answer "This is a string"
       res s:substr "//"
       res replacestr ""

     elseif i eq 6
       ! Replace first substring in a string with multiple instances of
       ! substring
       res answer "This//is//a//string"
       res instr "//This//is//a//string"
       res s:substr "//"
       res replacestr ""
     endif

     SEDIT "^instr" str "^func" "^substr" "^replacestr"
     assert/text="SEDIT: ^func test ^i" "^str" EQS "^answer"
   enddo


return
!******************************************************************************!
procedure test_tail

  result func "TAIL"

  res i 1
  res answer "myfilename.txt"
  SEDIT "/home/user/myfilename.txt" str "^func"
  assert/text="SEDIT: ^func test ^i" "^str" EQS "^answer"

return
!******************************************************************************!
procedure test_trim

  result func "TRIM"

  res i 1
  res answer "FRAME;INDEX"
  SEDIT "ARRAY(FRAME;INDEX)+5" str "^func" "(" ")"
  assert/text="SEDIT: ^func test ^i" "^str" EQS "^answer"

  res i 2
  res answer "EXT"
  SEDIT "FILENAME.EXT" str "^func" "."
  assert/text="SEDIT: ^func test ^i" "^str" EQS "^answer"

  res i 3
  res answer "FILENAME"
  SEDIT "FILENAME.EXT" str "^func" ,, "."
  assert/text="SEDIT: ^func test ^i" "^str" EQS "^answer"

return
!******************************************************************************!
procedure test_upcase

  result func "UPCA"

  res i 1
  res answer "THIS IS A STRING"
  SEDIT "This is a string" str "^func"
  assert/text="SEDIT: ^func test ^i" "^str" EQS "^answer"

return
!******************************************************************************!
procedure test_word

  result func "WORD"
  res i 1
  res answer "string"
  SEDIT "This is a string" str "^func" "ri"
  assert/text="SEDIT: ^func test ^i" "^str" EQS "^answer"

return
!******************************************************************************!
procedure test_multi_function

  ! Make sure PARSE works as part of a multiple function statement with
  ! OPTIONAL second argument
  result func1 "PARSE"
  result func2 "LOCASE"
  res answer "string"
  sedit "This is a string" str "PARSE" 4 "," LOCASE
  if/cs "^str" EQS "^answer" then
    assert/text="SEDIT: MULTI FUNCTION ^func1 / ^func2 answer = ^answer" TRUE
  else
    assert/text="SEDIT: MULTI FUNCTION ^func1 / ^func2 answer = ^answer" FALSE
  endif

  ! DR #967 -- Make sure PARSE works as part of a multiple function statement
  !            without OPTIONAL second argument
  ! NOTE: Before NXM 2.5.0 this would not work
  result func1 "PARSE"
  result func2 "LOCASE"
  res answer "is"
  sedit "This is a string" str "PARSE" 2 LOCASE
  if/cs "^str" EQS "^answer" then
    assert/text="SEDIT: MULTI FUNCTION ^func1 / ^func2 answer = ^answer" TRUE
  else
    assert/text="SEDIT: MULTI FUNCTION ^func1 / ^func2 answer = ^answer" FALSE
  endif

return

!******************************************************************************!
! test /force switch to allow writing to a results in a read-only table (BUG 2415)
! also test APPEND function
procedure test_force
  set istr "Hello "
  set astr env.user
  sedit istr expStr APPEND astr
  assert/text="^reg.main.name: APPEND FUNCTION" expStr eqs "Hello ^env.user"

  ! ENV Table should be read-only
  sedit/force istr env.test_sedit APPEND astr
  assert/text="^reg.main.name: /force switch to read-only table" expStr eqs env.test_sedit

  remove/force env.test_sedit
return

!******************************************************************************!
procedure test_dr_1628

  ! Make sure formatted numbers are not "adjusted"
  result d:doubleval 905.50000006333
  result s:answer "0905.500000063330"
  sedit doubleval doublestring NFORM "0000.000000000000"

  assert/text="SEDIT: DR 1628 ^doublestring = ^answer"  ^doublestring EQS ^answer

return
