program dc_string_test
  use dc_types, only: STRING
  use dc_string, only: Split, StoA, CPrintf, StriEq, StrInclude
  use dc_trace, only: SetDebug
  use dc_test, only: AssertEqual
  implicit none

  character(STRING), pointer :: carray(:) =>null()
  character(STRING) :: char
  logical :: strieq_check
continue

  call SetDebug

  !
  ! Test for "Split"
  !
  call Split('time=0.0', carray, '=')
  call AssertEqual('Split Test 1', (/'time', '0.0 '/), carray)
  deallocate(carray)

  call Split('  time = 0.0  ', carray, '')
  call AssertEqual('Split Test 2', (/'    ', 'time', '=   ', '0.0 '/), carray)
  deallocate(carray)

  call Split('time=0.0 ||$ x=1.0 ||$ y=hogehoge ', carray, '||$')
  call AssertEqual('Split Test 3', &
    & (/'time=0.0   ', &
    &   ' x=1.0     ', &
    &   ' y=hogehoge'/), &
    & carray)
  deallocate(carray)

  call Split('  time = 0.0  ', carray, '', 2)
  call AssertEqual('Split Test 4', &
    & (/'          ', &
    &   'time = 0.0'/), &
    & carray)
  deallocate(carray)

  call Split('time=0.0,x=1.0,y=0.1,z=10,m=8,l=9 ', carray, ',', 3)
  call AssertEqual('Split Test 5', &
    & (/'time=0.0          ', &
    &   'x=1.0             ', &
    &   'y=0.1,z=10,m=8,l=9'/), &
    & carray)
  deallocate(carray)



  !
  ! Test for "StoA"
  !

  allocate(carray(3))
  carray = StoA('hogehoge', 'foo', 'hero')
  call AssertEqual('StoA Test', &
    & (/'hogehoge', &
    &   'foo     ', &
    &   'hero    '/), &
    & carray)
  deallocate(carray)

  char = CPrintf('%a, %a, %a, %a, %a, %a', &
    & ca=StoA("hogehoge", "foo", "hero", "uni"))
  call AssertEqual('CPrintf Test 1', 'hogehoge, foo, hero, uni, ,', char)

  char = CPrintf('%*a', &
    & ca=StoA("hogehoge", "foo", "hero", "uni"), n=(/4/))
  call AssertEqual('CPrintf Test 2', 'hogehoge, foo, hero, uni', char)

  char = CPrintf('%*d', i=(/1, 2, 4/), n=(/2/))
  call AssertEqual('CPrintf Test 3', '1, 2', char)

  char = CPrintf('%*r', r=(/(/1.0, 2.0, 4.0/)/), n=(/5/))
  call AssertEqual('CPrintf Test 4', '1., 2., 4.,', char)

  char = CPrintf('%*r %*a', r=(/1.0, 2.0, 4.0/), &
    & ca=StoA('hogehoge', 'foo', 'hero', 'uni'), n=(/2,3/))
  call AssertEqual('CPrintf Test 5', '1., 2. hogehoge, foo, hero', char)

  char = CPrintf('%d %5d %05d %4d %*02d', &
    & i=(/123, -987000, -360, 91, 1, 9/), &
    & n=(/2/))
  call AssertEqual('CPrintf Test 6', '123 -987000 -0360   91 01, 09', char)

  !
  ! Test for "StriEq"
  !
  strieq_check = StriEq('hogehoge', 'HogeHoge')
  call AssertEqual('StriEq Test 1', .true., strieq_check)

  strieq_check = StriEq('foo', 'HogeHoge')
  call AssertEqual('StriEq Test 2', .false., strieq_check)

  !
  ! Test for "StrInclude"
  !
  strieq_check = StrInclude(StoA('aaa', 'BBB', 'ccc'), 'bbb')
  call AssertEqual('StrInclude Test 1', .true., strieq_check)

  strieq_check = StrInclude(StoA('aaa', 'BBB', 'ccc'), 'DDD')
  call AssertEqual('StrInclude Test 2', .false., strieq_check)

end program dc_string_test
