AT_BANNER([MATCH FILES])

m4_define([PREPARE_MATCH_FILES],
  [AT_DATA([data1.txt], [dnl
1aB
8aM
3aE
5aG
0aA
5aH
6aI
7aJ
2aD
7aK
1aC
7aL
4aF
])

   AT_DATA([data2.txt], [dnl
1bN
3bO
4bP
6bQ
7bR
9bS
])

   AT_DATA([prepare.sps], [dnl
DATA LIST NOTABLE FILE='data1.txt' /a b c 1-3 (A).
SAVE OUTFILE='data1.sav'.
DATA LIST NOTABLE FILE='data2.txt' /a b c 1-3 (A).
SAVE OUTFILE='data2.sav'.
])
   AT_CHECK([pspp -O format=csv prepare.sps])
   AT_CHECK([test -f data1.sav && test -f data2.sav])])

dnl CHECK_MATCH_FILES(TYPE2, SOURCE1, SOURCE2)
dnl
dnl Checks the MATCH FILES procedure with the specified combination of:
dnl
dnl - TYPE2: Either "file" or "table" for the type of matching used for
dnl   the second data source.  (The first data source is always "file").
dnl
dnl - SOURCE1: Either "system" or "active" for the source of data for
dnl   the first data source.
dnl
dnl - SOURCE2: Either "system" or "active" for the source of data for
dnl   the second data source.  (SOURCE1 and SOURCE2 may not both be
dnl   "active".)
m4_define([CHECK_MATCH_FILES], 
  [AT_SETUP([MATCH FILES -- $2 file and $3 $1])
   PREPARE_MATCH_FILES
   AT_DATA([expout],
    [m4_if([$1], [file], [dnl
Table: Data List
a,b,c,d,ina,inb,first,last
0,a,A,,1,0,1,1
1,a,B,N,1,1,1,0
1,a,C,,1,0,0,1
2,a,D,,1,0,1,1
3,a,E,O,1,1,1,1
4,a,F,P,1,1,1,1
5,a,G,,1,0,1,0
5,a,H,,1,0,0,1
6,a,I,Q,1,1,1,1
7,a,J,R,1,1,1,0
7,a,K,,1,0,0,0
7,a,L,,1,0,0,1
8,a,M,,1,0,1,1
9,b,,S,0,1,1,1
], [dnl
Table: Data List
a,b,c,d,ina,inb,first,last
0,a,A,,1,0,1,1
1,a,B,N,1,1,1,0
1,a,C,N,1,1,0,1
2,a,D,,1,0,1,1
3,a,E,O,1,1,1,1
4,a,F,P,1,1,1,1
5,a,G,,1,0,1,0
5,a,H,,1,0,0,1
6,a,I,Q,1,1,1,1
7,a,J,R,1,1,1,0
7,a,K,R,1,1,0,0
7,a,L,R,1,1,0,1
8,a,M,,1,0,1,1
])])

   AT_DATA([match-files.sps], [dnl
m4_if([$2], [active], [GET FILE='data1.sav'.],
      [$3], [active], [GET FILE='data2.sav'.],
      [])
MATCH FILES
	FILE=m4_if([$2], [active], [*], ['data1.sav']) /IN=ina /SORT
	$1=m4_if([$3], [active], [*], ['data2.sav']) /in=inb /rename c=d
	/BY a /FIRST=first /LAST=last.
LIST.
])
   AT_CHECK([pspp -o pspp.csv match-files.sps])
   AT_CHECK([cat pspp.csv], [0], [expout])
   AT_CLEANUP])

CHECK_MATCH_FILES([file], [system], [system])
CHECK_MATCH_FILES([file], [system], [active])
CHECK_MATCH_FILES([file], [active], [system])
CHECK_MATCH_FILES([table], [system], [system])
CHECK_MATCH_FILES([table], [system], [active])
CHECK_MATCH_FILES([table], [active], [system])

AT_SETUP([MATCH FILES parallel match])
PREPARE_MATCH_FILES
AT_DATA([match-files.sps], [dnl
MATCH FILES FILE='data1.sav' /FILE='data2.sav' /RENAME (a b c=d e f).
LIST.
])
AT_CHECK([pspp -o pspp.csv match-files.sps])
AT_CHECK([cat pspp.csv], [0], [dnl
Table: Data List
a,b,c,d,e,f
1,a,B,1,b,N
8,a,M,3,b,O
3,a,E,4,b,P
5,a,G,6,b,Q
0,a,A,7,b,R
5,a,H,9,b,S
6,a,I,,,
7,a,J,,,
2,a,D,,,
7,a,K,,,
1,a,C,,,
7,a,L,,,
4,a,F,,,
])
AT_CLEANUP

dnl Test bug handling TABLE from active dataset found by John Darrington.
AT_SETUP([MATCH FILES bug with TABLE from active dataset])
AT_DATA([match-files.sps], [dnl
DATA LIST LIST NOTABLE /x * y *.
BEGIN DATA
3 30
2 21
1 22
END DATA.

SAVE OUTFILE='bar.sav'.

DATA LIST LIST NOTABLE /x * z *.
BEGIN DATA
3 8
2 9
END DATA.

MATCH FILES TABLE=* /FILE='bar.sav' /BY=x.
LIST.
])
AT_CHECK([pspp -o pspp.csv match-files.sps])
AT_CHECK([cat pspp.csv], [0], [dnl
Table: Data List
x,z,y
3.00,8.00,30.00
2.00,.  ,21.00
1.00,.  ,22.00
])
AT_CLEANUP

dnl Tests for a bug which caused MATCH FILES to crash
dnl when used with scratch variables.
AT_SETUP([MATCH FILES bug with scratch variables])
AT_DATA([match-files.sps], [dnl
DATA LIST LIST /w * x * y * .
BEGIN DATA
4 5 6
1 2 3
END DATA.

COMPUTE j=0.
LOOP #k = 1 to 10.
COMPUTE j=#k + j.
END LOOP.

MATCH FILES FILE=* /DROP=w.
LIST.
FINISH.
])
AT_CHECK([pspp -o pspp.csv match-files.sps])
AT_CHECK([cat pspp.csv], [0], [dnl
Table: Reading free-form data from INLINE.
Variable,Format
w,F8.0
x,F8.0
y,F8.0

Table: Data List
x,y,j
5.00,6.00,55.00
2.00,3.00,55.00
])
AT_CLEANUP
