125f08659ccb84ef104fefde72ea15d8d404db55
[pspp] / tests / language / data-io / match-files.at
1 dnl PSPP - a program for statistical analysis.
2 dnl Copyright (C) 2017 Free Software Foundation, Inc.
3 dnl 
4 dnl This program is free software: you can redistribute it and/or modify
5 dnl it under the terms of the GNU General Public License as published by
6 dnl the Free Software Foundation, either version 3 of the License, or
7 dnl (at your option) any later version.
8 dnl 
9 dnl This program is distributed in the hope that it will be useful,
10 dnl but WITHOUT ANY WARRANTY; without even the implied warranty of
11 dnl MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 dnl GNU General Public License for more details.
13 dnl 
14 dnl You should have received a copy of the GNU General Public License
15 dnl along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 dnl AT_BANNER([MATCH FILES])
17
18 m4_define([PREPARE_MATCH_FILES],
19   [AT_DATA([data1.txt], [dnl
20 1aB
21 8aM
22 3aE
23 5aG
24 0aA
25 5aH
26 6aI
27 7aJ
28 2aD
29 7aK
30 1aC
31 7aL
32 4aF
33 ])
34
35    AT_DATA([data2.txt], [dnl
36 1bN
37 3bO
38 4bP
39 6bQ
40 7bR
41 9bS
42 ])
43
44    AT_DATA([prepare.sps], [dnl
45 DATA LIST NOTABLE FILE='data1.txt' /a b c 1-3 (A).
46 SAVE OUTFILE='data1.sav'.
47 DATA LIST NOTABLE FILE='data2.txt' /a b c 1-3 (A).
48 SAVE OUTFILE='data2.sav'.
49 ])
50    AT_CHECK([pspp -O format=csv prepare.sps])
51    AT_CHECK([test -f data1.sav && test -f data2.sav])])
52
53 dnl CHECK_MATCH_FILES(TYPE2, SOURCE1, SOURCE2)
54 dnl
55 dnl Checks the MATCH FILES procedure with the specified combination of:
56 dnl
57 dnl - TYPE2: Either "file" or "table" for the type of matching used for
58 dnl   the second data source.  (The first data source is always "file").
59 dnl
60 dnl - SOURCE1: Either "system" or "active" for the source of data for
61 dnl   the first data source.
62 dnl
63 dnl - SOURCE2: Either "system" or "active" for the source of data for
64 dnl   the second data source.  (SOURCE1 and SOURCE2 may not both be
65 dnl   "active".)
66 m4_define([CHECK_MATCH_FILES], 
67   [AT_SETUP([MATCH FILES -- $2 file and $3 $1])
68    PREPARE_MATCH_FILES
69    AT_DATA([expout],
70     [m4_if([$1], [file], [dnl
71 Table: Data List
72 a,b,c,d,ina,inb,first,last
73 0,a,A,,1,0,1,1
74 1,a,B,N,1,1,1,0
75 1,a,C,,1,0,0,1
76 2,a,D,,1,0,1,1
77 3,a,E,O,1,1,1,1
78 4,a,F,P,1,1,1,1
79 5,a,G,,1,0,1,0
80 5,a,H,,1,0,0,1
81 6,a,I,Q,1,1,1,1
82 7,a,J,R,1,1,1,0
83 7,a,K,,1,0,0,0
84 7,a,L,,1,0,0,1
85 8,a,M,,1,0,1,1
86 9,b,,S,0,1,1,1
87 ], [dnl
88 Table: Data List
89 a,b,c,d,ina,inb,first,last
90 0,a,A,,1,0,1,1
91 1,a,B,N,1,1,1,0
92 1,a,C,N,1,1,0,1
93 2,a,D,,1,0,1,1
94 3,a,E,O,1,1,1,1
95 4,a,F,P,1,1,1,1
96 5,a,G,,1,0,1,0
97 5,a,H,,1,0,0,1
98 6,a,I,Q,1,1,1,1
99 7,a,J,R,1,1,1,0
100 7,a,K,R,1,1,0,0
101 7,a,L,R,1,1,0,1
102 8,a,M,,1,0,1,1
103 ])])
104
105    AT_DATA([match-files.sps], [dnl
106 m4_if([$2], [active], [GET FILE='data1.sav'.],
107       [$3], [active], [GET FILE='data2.sav'.],
108       [])
109 MATCH FILES
110         FILE=m4_if([$2], [active], [*], ['data1.sav']) /IN=ina /SORT
111         $1=m4_if([$3], [active], [*], ['data2.sav']) /in=inb /rename c=d
112         /BY a /FIRST=first /LAST=last.
113 LIST.
114 ])
115    AT_CHECK([pspp -o pspp.csv match-files.sps])
116    AT_CHECK([cat pspp.csv], [0], [expout])
117    AT_CLEANUP])
118
119 CHECK_MATCH_FILES([file], [system], [system])
120 CHECK_MATCH_FILES([file], [system], [active])
121 CHECK_MATCH_FILES([file], [active], [system])
122 CHECK_MATCH_FILES([table], [system], [system])
123 CHECK_MATCH_FILES([table], [system], [active])
124 CHECK_MATCH_FILES([table], [active], [system])
125
126 AT_SETUP([MATCH FILES parallel match])
127 PREPARE_MATCH_FILES
128 AT_DATA([match-files.sps], [dnl
129 MATCH FILES FILE='data1.sav' /FILE='data2.sav' /RENAME (a b c=d e f).
130 LIST.
131 ])
132 AT_CHECK([pspp -o pspp.csv match-files.sps])
133 AT_CHECK([cat pspp.csv], [0], [dnl
134 Table: Data List
135 a,b,c,d,e,f
136 1,a,B,1,b,N
137 8,a,M,3,b,O
138 3,a,E,4,b,P
139 5,a,G,6,b,Q
140 0,a,A,7,b,R
141 5,a,H,9,b,S
142 6,a,I,,,
143 7,a,J,,,
144 2,a,D,,,
145 7,a,K,,,
146 1,a,C,,,
147 7,a,L,,,
148 4,a,F,,,
149 ])
150 AT_CLEANUP
151
152 dnl Test bug handling TABLE from active dataset found by John Darrington.
153 AT_SETUP([MATCH FILES bug with TABLE from active dataset])
154 AT_DATA([match-files.sps], [dnl
155 DATA LIST LIST NOTABLE /x * y *.
156 BEGIN DATA
157 3 30
158 2 21
159 1 22
160 END DATA.
161
162 SAVE OUTFILE='bar.sav'.
163
164 DATA LIST LIST NOTABLE /x * z *.
165 BEGIN DATA
166 3 8
167 2 9
168 END DATA.
169
170 MATCH FILES TABLE=* /FILE='bar.sav' /BY=x.
171 LIST.
172 ])
173 AT_CHECK([pspp -o pspp.csv match-files.sps])
174 AT_CHECK([cat pspp.csv], [0], [dnl
175 Table: Data List
176 x,z,y
177 3.00,8.00,30.00
178 2.00,.  ,21.00
179 1.00,.  ,22.00
180 ])
181 AT_CLEANUP
182
183 dnl Tests for a bug which caused MATCH FILES to crash
184 dnl when used with scratch variables.
185 AT_SETUP([MATCH FILES bug with scratch variables])
186 AT_DATA([match-files.sps], [dnl
187 DATA LIST LIST /w * x * y * .
188 BEGIN DATA
189 4 5 6
190 1 2 3
191 END DATA.
192
193 COMPUTE j=0.
194 LOOP #k = 1 to 10.
195 COMPUTE j=#k + j.
196 END LOOP.
197
198 MATCH FILES FILE=* /DROP=w.
199 LIST.
200 FINISH.
201 ])
202 AT_CHECK([pspp -o pspp.csv match-files.sps])
203 AT_CHECK([cat pspp.csv], [0], [dnl
204 Table: Reading free-form data from INLINE.
205 Variable,Format
206 w,F8.0
207 x,F8.0
208 y,F8.0
209
210 Table: Data List
211 x,y,j
212 5.00,6.00,55.00
213 2.00,3.00,55.00
214 ])
215 AT_CLEANUP
216
217 dnl Tests for a bug that caused MATCH FILES to crash
218 dnl with incompatible variables, especially but not
219 dnl exclusively when one variable came from the active
220 dnl file.
221 AT_SETUP([MATCH FILES with incompatible variable types])
222 AT_DATA([match-files.sps], [dnl
223 DATA LIST LIST NOTABLE/name (A6) x.
224 BEGIN DATA.
225 al,7
226 brad,8
227 carl,9
228 END DATA.
229 SAVE OUTFILE='x.sav'.
230
231 DATA LIST LIST NOTABLE/name (A7) y.
232 BEGIN DATA.
233 al,1
234 carl,2
235 dan,3
236 END DATA.
237 MATCH FILES/FILE='x.sav'/FILE=*/BY name.
238 LIST.
239 ])
240 AT_CHECK([pspp -O format=csv match-files.sps], [1], [dnl
241 "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."
242
243 match-files.sps:16: error: Stopping syntax file processing here to avoid a cascade of dependent command failures.
244 ])
245 AT_CLEANUP