!******************************************************************************!
! Tests to see if the SORT primitive is functioning properly.
!
! @author  J. Hallam / J. Czechowski IV
! @version $Id: test_sort.mm,v 1.5 2011/06/08 18:04:57 ntn Exp $
!******************************************************************************!
startmacro

  set t:fileData1 {0={NAME=AAA,DBLE=1.0,SLNG=9,SINT=1},&
                   1={NAME=BBB,DBLE=1.1,SLNG=8,SINT=2},&
                   2={NAME=CCC,DBLE=1.2,SLNG=7,SINT=3},&
                   3={NAME=AAA,DBLE=1.3,SLNG=6,SINT=3},&
                   4={NAME=BBB,DBLE=1.4,SLNG=5,SINT=2},&
                   5={NAME=CCC,DBLE=1.5,SLNG=4,SINT=1},&
                   6={NAME=CCC,DBLE=1.6,SLNG=3,SINT=2},&
                   7={NAME=BBB,DBLE=1.7,SLNG=2,SINT=3},&
                   8={NAME=AAA,DBLE=1.8,SLNG=1,SINT=1},&
                   9={NAME=DDD,DBLE=1.9,SLNG=0,SINT=2}}

  set t:fileData2 {0=^{fileData1.0},1=^{fileData1.3},&
                   2=^{fileData1.8},3=^{fileData1.1},&
                   4=^{fileData1.4},5=^{fileData1.7},&
                   6=^{fileData1.2},7=^{fileData1.5},&
                   8=^{fileData1.6},9=^{fileData1.9}}

  set t:fileData3 {0=^{fileData1.8},1=^{fileData1.3},&
                   2=^{fileData1.0},3=^{fileData1.7},&
                   4=^{fileData1.4},5=^{fileData1.1},&
                   6=^{fileData1.6},7=^{fileData1.5},&
                   8=^{fileData1.2},9=^{fileData1.9}}

  set t:fileData4 {0=^{fileData1.2},1=^{fileData1.7},&
                   2=^{fileData1.3},3=^{fileData1.9},&
                   4=^{fileData1.6},5=^{fileData1.4},&
                   6=^{fileData1.1},7=^{fileData1.5},&
                   8=^{fileData1.8},9=^{fileData1.0}}

  header/create ram:testfile1 SR=(NAME/2A,DBLE/SD,SLNG/SL,SINT/SI)
  file open/d/w tag ram:testfile1
  foreach key intable fileData1
    file write tag ^{fileData1.^{key}}
  endfor
  file close tag

  sort ram:testfile1 ram:testfile2 "+NAME|+DBLE"
  sort ram:testfile1 ram:testfile3 "+NAME|-DBLE"
  sort ram:testfile1 ram:testfile4 "-SINT|-NAME|+SLNG"

  call compareFile ram:testfile2 fileData2
  call compareFile ram:testfile3 fileData3
  call compareFile ram:testfile4 fileData4

endmacro

!******************************************************************************!
procedure compareFile s:fileName t:rowValues
  file open/d/w tag ^fileName
  foreach key intable rowValues
    invoke actual   tag.getDataTable(^key)
    set    expected ^{rowValues.^{key}}
    set    ok       "true"

    foreach ekey intable expected
      if "^{expected.^{ekey}}" neqs "^{actual.^{ekey}}"
        set ok "false"
      endif
    endfor
    if ok isFalse then
        warn "    --> row ^{key}: expected ^{expected}"
        warn "                    got ^{actual}"
    endif
    assert/text="Checking row ^key in ^fileName " ok isTrue
  endfor
  file close tag
return
