1dnl PSPP - a program for statistical analysis.
2dnl Copyright (C) 2017 Free Software Foundation, Inc.
3dnl
4dnl This program is free software: you can redistribute it and/or modify
5dnl it under the terms of the GNU General Public License as published by
6dnl the Free Software Foundation, either version 3 of the License, or
7dnl (at your option) any later version.
8dnl
9dnl This program is distributed in the hope that it will be useful,
10dnl but WITHOUT ANY WARRANTY; without even the implied warranty of
11dnl MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12dnl GNU General Public License for more details.
13dnl
14dnl You should have received a copy of the GNU General Public License
15dnl along with this program.  If not, see <http://www.gnu.org/licenses/>.
16dnl
17AT_BANNER([MATCH FILES])
18
19m4_define([PREPARE_MATCH_FILES],
20  [AT_DATA([data1.txt], [dnl
211aB
228aM
233aE
245aG
250aA
265aH
276aI
287aJ
292aD
307aK
311aC
327aL
334aF
34])
35
36   AT_DATA([data2.txt], [dnl
371bN
383bO
394bP
406bQ
417bR
429bS
43])
44
45   AT_DATA([prepare.sps], [dnl
46DATA LIST NOTABLE FILE='data1.txt' /a b c 1-3 (A).
47SAVE OUTFILE='data1.sav'.
48DATA LIST NOTABLE FILE='data2.txt' /a b c 1-3 (A).
49SAVE OUTFILE='data2.sav'.
50])
51   AT_CHECK([pspp -O format=csv prepare.sps])
52   AT_CHECK([test -f data1.sav && test -f data2.sav])])
53
54dnl CHECK_MATCH_FILES(TYPE2, SOURCE1, SOURCE2)
55dnl
56dnl Checks the MATCH FILES procedure with the specified combination of:
57dnl
58dnl - TYPE2: Either "file" or "table" for the type of matching used for
59dnl   the second data source.  (The first data source is always "file").
60dnl
61dnl - SOURCE1: Either "system" or "active" for the source of data for
62dnl   the first data source.
63dnl
64dnl - SOURCE2: Either "system" or "active" for the source of data for
65dnl   the second data source.  (SOURCE1 and SOURCE2 may not both be
66dnl   "active".)
67m4_define([CHECK_MATCH_FILES],
68  [AT_SETUP([MATCH FILES -- $2 file and $3 $1])
69   PREPARE_MATCH_FILES
70   AT_DATA([expout],
71    [m4_if([$1], [file], [dnl
72Table: Data List
73a,b,c,d,ina,inb,first,last
740,a,A,,1,0,1,1
751,a,B,N,1,1,1,0
761,a,C,,1,0,0,1
772,a,D,,1,0,1,1
783,a,E,O,1,1,1,1
794,a,F,P,1,1,1,1
805,a,G,,1,0,1,0
815,a,H,,1,0,0,1
826,a,I,Q,1,1,1,1
837,a,J,R,1,1,1,0
847,a,K,,1,0,0,0
857,a,L,,1,0,0,1
868,a,M,,1,0,1,1
879,b,,S,0,1,1,1
88], [dnl
89Table: Data List
90a,b,c,d,ina,inb,first,last
910,a,A,,1,0,1,1
921,a,B,N,1,1,1,0
931,a,C,N,1,1,0,1
942,a,D,,1,0,1,1
953,a,E,O,1,1,1,1
964,a,F,P,1,1,1,1
975,a,G,,1,0,1,0
985,a,H,,1,0,0,1
996,a,I,Q,1,1,1,1
1007,a,J,R,1,1,1,0
1017,a,K,R,1,1,0,0
1027,a,L,R,1,1,0,1
1038,a,M,,1,0,1,1
104])])
105
106   AT_DATA([match-files.sps], [dnl
107m4_if([$2], [active], [GET FILE='data1.sav'.],
108      [$3], [active], [GET FILE='data2.sav'.],
109      [])
110MATCH FILES
111	FILE=m4_if([$2], [active], [*], ['data1.sav']) /IN=ina /SORT
112	$1=m4_if([$3], [active], [*], ['data2.sav']) /in=inb /rename c=d
113	/BY a /FIRST=first /LAST=last.
114LIST.
115])
116   AT_CHECK([pspp -o pspp.csv match-files.sps])
117   AT_CHECK([cat pspp.csv], [0], [expout])
118   AT_CLEANUP])
119
120CHECK_MATCH_FILES([file], [system], [system])
121CHECK_MATCH_FILES([file], [system], [active])
122CHECK_MATCH_FILES([file], [active], [system])
123CHECK_MATCH_FILES([table], [system], [system])
124CHECK_MATCH_FILES([table], [system], [active])
125CHECK_MATCH_FILES([table], [active], [system])
126
127AT_SETUP([MATCH FILES parallel match])
128PREPARE_MATCH_FILES
129AT_DATA([match-files.sps], [dnl
130MATCH FILES FILE='data1.sav' /FILE='data2.sav' /RENAME (a b c=d e f).
131LIST.
132])
133AT_CHECK([pspp -o pspp.csv match-files.sps])
134AT_CHECK([cat pspp.csv], [0], [dnl
135Table: Data List
136a,b,c,d,e,f
1371,a,B,1,b,N
1388,a,M,3,b,O
1393,a,E,4,b,P
1405,a,G,6,b,Q
1410,a,A,7,b,R
1425,a,H,9,b,S
1436,a,I,,,
1447,a,J,,,
1452,a,D,,,
1467,a,K,,,
1471,a,C,,,
1487,a,L,,,
1494,a,F,,,
150])
151AT_CLEANUP
152
153dnl Test bug handling TABLE from active dataset found by John Darrington.
154AT_SETUP([MATCH FILES bug with TABLE from active dataset])
155AT_DATA([match-files.sps], [dnl
156DATA LIST LIST NOTABLE /x * y *.
157BEGIN DATA
1583 30
1592 21
1601 22
161END DATA.
162
163SAVE OUTFILE='bar.sav'.
164
165DATA LIST LIST NOTABLE /x * z *.
166BEGIN DATA
1673 8
1682 9
169END DATA.
170
171MATCH FILES TABLE=* /FILE='bar.sav' /BY=x.
172LIST.
173])
174AT_CHECK([pspp -o pspp.csv match-files.sps])
175AT_CHECK([cat pspp.csv], [0], [dnl
176Table: Data List
177x,z,y
1783.00,8.00,30.00
1792.00,.  ,21.00
1801.00,.  ,22.00
181])
182AT_CLEANUP
183
184dnl Tests for a bug which caused MATCH FILES to crash
185dnl when used with scratch variables.
186AT_SETUP([MATCH FILES bug with scratch variables])
187AT_DATA([match-files.sps], [dnl
188DATA LIST LIST /w * x * y * .
189BEGIN DATA
1904 5 6
1911 2 3
192END DATA.
193
194COMPUTE j=0.
195LOOP #k = 1 to 10.
196COMPUTE j=#k + j.
197END LOOP.
198
199MATCH FILES FILE=* /DROP=w.
200LIST.
201FINISH.
202])
203AT_CHECK([pspp -o pspp.csv match-files.sps])
204AT_CHECK([cat pspp.csv], [0], [dnl
205Table: Reading free-form data from INLINE.
206Variable,Format
207w,F8.0
208x,F8.0
209y,F8.0
210
211Table: Data List
212x,y,j
2135.00,6.00,55.00
2142.00,3.00,55.00
215])
216AT_CLEANUP
217
218dnl Tests for a bug that caused MATCH FILES to crash
219dnl with incompatible variables, especially but not
220dnl exclusively when one variable came from the active
221dnl file.
222AT_SETUP([MATCH FILES with incompatible variable types])
223AT_DATA([match-files.sps], [dnl
224DATA LIST LIST NOTABLE/name (A6) x.
225BEGIN DATA.
226al,7
227brad,8
228carl,9
229END DATA.
230SAVE OUTFILE='x.sav'.
231
232DATA LIST LIST NOTABLE/name (A7) y.
233BEGIN DATA.
234al,1
235carl,2
236dan,3
237END DATA.
238MATCH FILES/FILE='x.sav'/FILE=*/BY name.
239LIST.
240])
241AT_CHECK([pspp -O format=csv match-files.sps], [1], [dnl
242"match-files.sps:15: error: MATCH FILES: Variable name in file * has different type or width from the same variable in earlier file.  In file *, name is a string variable with width 7.  In an earlier file, name was a string variable with width 6."
243
244match-files.sps:16: error: Stopping syntax file processing here to avoid a cascade of dependent command failures.
245])
246AT_CLEANUP
247