Essentially rewrite MATCH FILES to support FIRST and LAST subcommands.
[pspp-builds.git] / tests / command / match-files.sh
1 #!/bin/sh
2
3 # This program tests the MATCH FILES procedure
4
5 TEMPDIR=/tmp/pspp-tst-$$
6 TESTFILE=$TEMPDIR/match-files.pspp
7
8
9 # ensure that top_builddir  are absolute
10 if [ -z "$top_builddir" ] ; then top_builddir=. ; fi
11 if [ -z "$top_srcdir" ] ; then top_srcdir=. ; fi
12 top_builddir=`cd $top_builddir; pwd`
13 PSPP=$top_builddir/src/ui/terminal/pspp
14
15 # ensure that top_srcdir is absolute
16 top_srcdir=`cd $top_srcdir; pwd`
17
18
19 STAT_CONFIG_PATH=$top_srcdir/config
20 export STAT_CONFIG_PATH
21
22 cleanup()
23 {
24     cd /
25     rm -rf $TEMPDIR
26     :
27 }
28
29
30 fail()
31 {
32     echo $activity
33     echo FAILED
34     cleanup;
35     exit 1;
36 }
37
38
39 no_result()
40 {
41     echo $activity
42     echo NO RESULT;
43     cleanup;
44     exit 2;
45 }
46
47 pass()
48 {
49     cleanup;
50     exit 0;
51 }
52
53 mkdir -p $TEMPDIR
54
55 cd $TEMPDIR
56
57 activity="data create"
58 cat > a.data <<EOF
59 0aA
60 1aB
61 1aC
62 2aD
63 3aE
64 4aF
65 5aG
66 5aH
67 6aI
68 7aJ
69 7aK
70 7aL
71 8aM
72 EOF
73 if [ $? -ne 0 ] ; then no_result ; fi
74 cat > b.data <<EOF
75 1bN
76 3bO
77 4bP
78 6bQ
79 7bR
80 9bS
81 EOF
82 if [ $? -ne 0 ] ; then no_result ; fi
83
84 cat > ff.out <<EOF
85 A B C D INA INB FIRST LAST
86 - - - - --- --- ----- ----
87 0 a A     1   0     1    1
88 1 a B N   1   1     1    0
89 1 a C     1   0     0    1
90 2 a D     1   0     1    1
91 3 a E O   1   1     1    1
92 4 a F P   1   1     1    1
93 5 a G     1   0     1    0
94 5 a H     1   0     0    1
95 6 a I Q   1   1     1    1
96 7 a J R   1   1     1    0
97 7 a K     1   0     0    0
98 7 a L     1   0     0    1
99 8 a M     1   0     1    1
100 9 b   S   0   1     1    1
101 EOF
102
103 cat > ft.out <<EOF
104 A B C D INA INB FIRST LAST
105 - - - - --- --- ----- ----
106 0 a A     1   0     1    1
107 1 a B N   1   1     1    0
108 1 a C N   1   1     0    1
109 2 a D     1   0     1    1
110 3 a E O   1   1     1    1
111 4 a F P   1   1     1    1
112 5 a G     1   0     1    0
113 5 a H     1   0     0    1
114 6 a I Q   1   1     1    1
115 7 a J R   1   1     1    0
116 7 a K R   1   1     0    0
117 7 a L R   1   1     0    1
118 8 a M     1   0     1    1
119 EOF
120
121 # Test nonparallel match and table lookup.
122 dla="data list notable file='a.data' /A B C 1-3 (a)."
123 sa="save outfile='a.sys'."
124 dlb="data list notable file='b.data' /A B C 1-3 (a)."
125 sb="save outfile='b.sys'."
126 for types in ff ft; do
127     type1=file
128     if [ $types = ff ]; then 
129         type2=file
130     else
131         type2=table
132     fi
133     for sources in ss sa as; do
134         name="$types-$sources"
135         activity="create $name.pspp"
136         {
137             if [ $sources = ss ]; then
138                 cat <<EOF
139 $dla
140 $sa
141 $dlb
142 $sb
143 match files $type1='a.sys' /in=INA /$type2='b.sys' /in=INB /rename c=D /by a
144             /first=FIRST /last=LAST.
145 EOF
146             elif [ $sources = sa ]; then
147                 cat <<EOF
148 $dla
149 $sa
150 $dlb
151 match files $type1='a.sys' /in=INA /$type2=* /in=INB /rename c=D /by a
152             /first=FIRST /last=LAST.
153 EOF
154             elif [ $sources = as ]; then
155                 cat <<EOF
156 $dlb
157 $sb
158 $dla
159 match files $type1=* /in=INA /$type2='b.sys' /in=INB /rename c=D /by a
160             /first=FIRST /last=LAST.
161 EOF
162             else
163                 activity="internal error"
164                 no_result
165             fi
166             echo 'list.'
167         } > $name.pspp
168         if [ $? -ne 0 ] ; then no_result ; fi
169
170         activity="run $name.pspp"
171         $SUPERVISOR $PSPP -o raw-ascii -e /dev/null $name.pspp 
172         if [ $? -ne 0 ] ; then no_result ; fi
173
174         activity="check $name output"
175         perl -pi -e 's/^\s*$//g' pspp.list
176         perl -pi -e 's/^\s*$//g' $types.out
177         diff -b -w pspp.list $types.out
178         if [ $? -ne 0 ] ; then fail ; fi
179     done
180 done
181
182 # Test parallel match.  
183 name="parallel"
184 activity="create $name.pspp"
185 cat > $name.pspp <<EOF
186 $dla
187 $sa
188 $dlb
189 $sb
190 match files file='a.sys' /file='b.sys' /rename (a b c=D E F).
191 list.
192 EOF
193 if [ $? -ne 0 ] ; then no_result ; fi
194
195 activity="run $name.pspp"
196 $SUPERVISOR $PSPP -o raw-ascii -e /dev/null $name.pspp 
197 if [ $? -ne 0 ] ; then no_result ; fi
198
199 activity="check $name output"
200 perl -pi -e 's/^\s*$//g' pspp.list
201 diff -b -w - pspp.list <<EOF
202 A B C D E F
203 - - - - - -
204 0 a A 1 b N
205 1 a B 3 b O
206 1 a C 4 b P
207 2 a D 6 b Q
208 3 a E 7 b R
209 4 a F 9 b S
210 5 a G
211 5 a H
212 6 a I
213 7 a J
214 7 a K
215 7 a L
216 8 a M
217 EOF
218 if [ $? -ne 0 ] ; then fail ; fi
219
220 # Test bug handling TABLE from active file found by John Darrington.
221 name="active-table"
222 activity="create $name.pspp"
223 cat > $name.pspp <<EOF
224 DATA LIST LIST NOTABLE /x * y *.
225 BEGIN DATA
226 3 30
227 2 21
228 1 22
229 END DATA.
230
231 SAVE OUTFILE='bar.sav'.
232
233 DATA LIST LIST NOTABLE /x * z *.
234 BEGIN DATA
235 3 8
236 2 9
237 END DATA.
238
239 MATCH FILES TABLE=* /FILE='bar.sav' /BY=x.
240 LIST.
241 EOF
242 if [ $? -ne 0 ] ; then no_result ; fi
243
244 activity="run $name.pspp"
245 $SUPERVISOR $PSPP -o raw-ascii -e /dev/null $name.pspp 
246 if [ $? -ne 0 ] ; then no_result ; fi
247
248 activity="check $name output"
249 perl -pi -e 's/^\s*$//g' pspp.list
250 diff -b -w - pspp.list <<EOF | perl -e 's/^\s*$//g'
251         x        z        y
252  -------- -------- --------
253      3.00     8.00    30.00 
254      2.00      .      21.00 
255      1.00      .      22.00 
256 EOF
257 if [ $? -ne 0 ] ; then fail ; fi
258
259
260 pass;