dnl PSPP - a program for statistical analysis.
dnl Copyright (C) 2017 Free Software Foundation, Inc.
dnl
dnl This program is free software: you can redistribute it and/or modify
dnl it under the terms of the GNU General Public License as published by
dnl the Free Software Foundation, either version 3 of the License, or
dnl (at your option) any later version.
dnl
dnl This program is distributed in the hope that it will be useful,
dnl but WITHOUT ANY WARRANTY; without even the implied warranty of
dnl MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
dnl GNU General Public License for more details.
dnl
dnl You should have received a copy of the GNU General Public License
dnl along with this program.  If not, see <http://www.gnu.org/licenses/>.
dnl
AT_BANNER([CORRELATIONS])

AT_SETUP([CORRELATIONS -- unweighted])
AT_DATA([correlations.sps], [dnl
set format = F11.3.
data list notable list /foo * bar * wiz * bang *.
begin data.
1   0   3   1
3   9 -50   5
3   4   3 203
4  -9   0  -4
98 78 104   2
3  50 -49 200
.   4   4   4
5   3   0   .
end data.

correlations
	variables = foo bar wiz bang
	/print nosig
	/missing = listwise
	.

correlations
	variables = bar wiz
	/print nosig
	/missing = listwise
	.

correlations
	variables = foo bar wiz bang
	/print nosig
	/missing = pairwise
	.
])
AT_CHECK([pspp -o pspp.csv -o pspp.txt correlations.sps])
AT_CHECK([cat pspp.csv], [0],
[[Table: Correlations
,,foo,bar,wiz,bang
foo,Pearson Correlation,1.000,.802,.890[a],-.308
,Sig. (2-tailed),,.055,.017,.553
bar,Pearson Correlation,.802,1.000,.519,.118
,Sig. (2-tailed),.055,,.291,.824
wiz,Pearson Correlation,.890[a],.519,1.000,-.344
,Sig. (2-tailed),.017,.291,,.505
bang,Pearson Correlation,-.308,.118,-.344,1.000
,Sig. (2-tailed),.553,.824,.505,
Footnote: a. Significant at .05 level

Table: Correlations
,,bar,wiz
bar,Pearson Correlation,1.000,.497
,Sig. (2-tailed),,.210
wiz,Pearson Correlation,.497,1.000
,Sig. (2-tailed),.210,

Table: Correlations
,,foo,bar,wiz,bang
foo,Pearson Correlation,1.000,.805[a],.883[a],-.308
,Sig. (2-tailed),,.029,.008,.553
,N,7,7,7,6
bar,Pearson Correlation,.805[a],1.000,.497,.164
,Sig. (2-tailed),.029,,.210,.725
,N,7,8,8,7
wiz,Pearson Correlation,.883[a],.497,1.000,-.337
,Sig. (2-tailed),.008,.210,,.460
,N,7,8,8,7
bang,Pearson Correlation,-.308,.164,-.337,1.000
,Sig. (2-tailed),.553,.725,.460,
,N,6,7,7,7
Footnote: a. Significant at .05 level
]])
AT_CLEANUP

AT_SETUP([CORRELATIONS -- weighted])
AT_DATA([correlations1.sps], [dnl
set format = F11.3.
data list notable list /foo * bar * wiz * bang * w *.
begin data.
1   0   3   1  1
3   9 -50   5  2
3   4   3 203  1
4  -9   0  -4  1
98 78 104   2  3
3  50 -49 200  1
end data.

weight by w.

correlations
	variables = foo bar wiz bang
	/statistics=descriptives xprod
	.
])
AT_DATA([correlations2.sps], [dnl
set format = F11.3.
data list notable list /foo * bar * wiz * bang * w *.
begin data.
1   0   3   1  1
3   9 -50   5  1
3   9 -50   5  1
3   4   3 203  1
4  -9   0  -4  1
98 78 104   2  1
98 78 104   2  1
98 78 104   2  1
3  50 -49 200  1
end data.

weight by w.

correlations
	variables = foo bar wiz bang
	/statistics=descriptives xprod
	.
])
AT_CHECK([pspp -O format=csv correlations1.sps], [0], [stdout])
mv stdout expout
AT_CHECK([pspp -O format=csv correlations2.sps], [0], [expout])
AT_CLEANUP


AT_SETUP([CORRELATIONS -- non-square])
AT_DATA([corr-ns.sps], [dnl
set format = F11.3.
data list notable list /foo * bar * wiz *.
begin data.
1 1 6
2 2 5
3 3 4
4 4 3
5 5 2
6 6 1
end data.

correlations
	variables = foo with bar wiz
	.
])

AT_CHECK([pspp -O format=csv corr-ns.sps], [0], [dnl
Table: Correlations
,,bar,wiz
foo,Pearson Correlation,1.000,-1.000
,Sig. (2-tailed),.000,.000
,N,6,6
])

AT_CLEANUP

dnl Checks for bug #38661.
AT_SETUP([CORRELATIONS -- crash with WITH keyword])
AT_DATA([correlations.sps], [dnl
DATA LIST LIST NOTABLE /a b c d e f g h i.
.
BEGIN DATA.
20 21 17 28 23 4.35 24 19 25
28 18 29 30 23 4.55 17 23 28
47 18 30 30 29 4.35 26 31 31
20 7 19 22 22 4.80 24 16 27
19 12 17 27 22 . 22 14 25
22 9 19 30 33 5 29 30 27
41 16 22 32 23 3.90 26 27 23
18 18 20 26 22 5.80 17 20 39
18 24 25 25 31 5.15 27 27 34
19 22 26 23 37 6 41 32 27
23 12 15 29 25 4.10 21 27 20
21 4 28 37 31 5.65 27 18 42
19 5 17 17 29 3.10 19 16 19
21 17 20 35 31 . 28 30 22
END DATA.

CORRELATIONS VARIABLE=a f b WITH c g h i e d/STATISTICS=DESCRIPTIVES.
])
AT_CHECK([pspp -o pspp.csv correlations.sps])
# Check the output, ignoring the actual correlations values since
# they look pretty nonsensical to me for this input (they include NaNs).
AT_CHECK([sed '/a,Pearson/,$s/,\([[^,]]*\),.*/,\1,.../' pspp.csv], [0], [dnl
Table: Descriptive Statistics
,Mean,Std. Deviation,N
a,24.00,8.93,14
f,4.73,.85,12
b,14.50,6.41,14
c,21.71,4.98,14
g,24.86,6.09,14
h,23.57,6.30,14
i,27.79,6.73,14
e,27.21,4.95,14
d,27.93,5.23,14

Table: Correlations
,,c,g,h,i,e,d
a,Pearson Correlation,...
,Sig. (2-tailed),...
,N,...
f,Pearson Correlation,...
,Sig. (2-tailed),...
,N,...
b,Pearson Correlation,...
,Sig. (2-tailed),...
,N,...
])
AT_CLEANUP



dnl Checks for bug #40661
AT_SETUP([CORRELATIONS -- incorrect subtable selection])
AT_DATA([correlations.sps], [dnl
set format = F12.4.
OUTPUT MODIFY /SELECT TABLES /TABLECELLS SELECT = [[CORRELATIONS]] FORMAT=F12.4.
set decimal = dot.
data list notable list /var1 var2 var3 var4 var5 *.
begin data.
7,6,9,2,3
9,12,8,5,8
8,9,7,8,6
8,8,9,10,8
7,6,4,5,3
7,9,8,2,1
9,8,11,,10
8,7,6,,5
6,7,6,,8
6,,3,,4
6,,7,3,3
5,4,2,7,8
9,8,6,11,10
5,6,2,2,4
8,7,6,8,7
10,13,8,12,10
7,8,7,11,2
8,7,7,9,6
10,11,11,8,1
5,8,6,9,9
8,7,5,5,6
5,7,2,1,8
9,8,8,13,6
5,8,5,6,4
,7,5,4,5
,8,4,4,3
,6,4,9,5
8,11,9,12,3
9,11,8,10,6
10,10,7,8,1
6,6,3,8,9
10,9,7,12,2
6,8,,7,4
6,8,3,2,9
7,8,8,2,9
5,6,5,5,5
9,9,7,7,5
9,10,11,7,8
8,11,9,3,3
5,4,4,0,5
9,9,11,14,2
5,6,2,4,4
8,8,7,4,1
9,9,8,14,
6,8,7,2,
10,9,9,6,
8,8,10,9,
7,8,4,12,
6,6,6,7,1
5,7,7,4,10
9,10,10,13,4
9,11,9,8,7
10,13,12,6,8
8,11,6,8,5
7,8,7,12,2
6,7,4,1,10
5,4,5,6,10
7,8,6,12,10
6,5,3,9,2
7,8,8,7,2
5,4,4,9,8
5,7,6,3,9
10,10,9,13,1
8,10,9,5,4
8,9,8,8,7
7,9,9,6,7
10,9,7,12,6
10,13,12,12,4
7,10,9,7,2
6,8,7,11,6
8,11,5,13,2
7,10,6,12,8
10,10,9,7,9
9,12,6,7,10
6,6,8,2,9
10,9,12,13,10
8,9,8,3,6
8,7,6,4,10
8,7,10,12,2
7,6,8,2,7
8,11,6,9,4
6,6,7,8,2
6,7,3,11,4
5,6,3,0,5
10,10,11,15,6
5,4,7,6,8
5,4,4,1,3
6,9,8,1,6
10,11,10,15,8
7,10,4,11,7
9,12,8,6,3
10,10,11,15,2
10,9,9,15,3
6,6,8,5,1
5,7,7,0,3
9,8,10,6,8
9,8,11,11,4
8,10,7,3,4
7,8,7,3,3
8,9,10,13,8
end data.

CORRELATION
	/VARIABLES =  var1 var2 var3 WITH var4 var5
	/PRINT = TWOTAIL NOSIG.

CORRELATION
	/VARIABLES =  var3 var4 var5 WITH var1 var2
	/PRINT = TWOTAIL NOSIG.

])

AT_CHECK([pspp -o pspp.csv -o pspp.txt correlations.sps])
AT_CHECK([cat pspp.csv], [0],
[[Table: Correlations
,,var4,var5
var1,Pearson Correlation,.5693[a],-.0519
,Sig. (2-tailed),.000,.623
,N,93,92
var2,Pearson Correlation,.3792[a],-.0407
,Sig. (2-tailed),.000,.698
,N,95,93
var3,Pearson Correlation,.3699[a],-.0543
,Sig. (2-tailed),.000,.603
,N,95,94
Footnote: a. Significant at .05 level

Table: Correlations
,,var1,var2
var3,Pearson Correlation,.6964[a],.5615[a]
,Sig. (2-tailed),.000,.000
,N,96,97
var4,Pearson Correlation,.5693[a],.3792[a]
,Sig. (2-tailed),.000,.000
,N,93,95
var5,Pearson Correlation,-.0519,-.0407
,Sig. (2-tailed),.623,.698
,N,92,93
Footnote: a. Significant at .05 level
]])

AT_CLEANUP


dnl Crash found by zzuf
AT_SETUP([CORRELATIONS -- empty dataset])

AT_DATA([correlations.sps], [dnl
data list list /a b c q g *.
CORRELATIONS 'VARIABLES = a b.]
)

AT_CHECK([pspp -o pspp.csv correlations.sps], [1], [ignore])

AT_CLEANUP

dnl Another Crash found by zzuf
AT_SETUP([CORRELATIONS -- empty dataset 2])

AT_DATA([correlations.sps], [dnl
data list notable list /foo * bar * wiz bang *.
begin data.
 1     00      3           .
 3     9     -50           .
98    78     104           .
 .     4       4           .
 5     3       0           .
end data.

correlations
        variables = foo bar wiz bang
        /missing = listwise
        .
])

AT_CHECK([pspp -O format=csv correlations.sps], [1], [dnl
correlations.sps:13: error: CORRELATIONS: The data for the chosen variables are all missing or empty.
])

AT_CLEANUP

AT_SETUP([CORRELATIONS -- syntax errors])
AT_DATA([correlations.sps], [dnl
DATA LIST LIST NOTABLE /x y z.
CORRELATIONS MISSING=**.
CORRELATIONS PRINT=**.
CORRELATIONS STATISTICS=**.
CORRELATIONS **.
CORRELATIONS x y z WITH **.
CORRELATIONS.
])
AT_CHECK([pspp -O format=csv correlations.sps], [1], [dnl
"correlations.sps:2.22-2.23: error: CORRELATIONS: Syntax error expecting PAIRWISE, LISTWISE, INCLUDE, or EXCLUDE.
    2 | CORRELATIONS MISSING=**.
      |                      ^~"

"correlations.sps:3.20-3.21: error: CORRELATIONS: Syntax error expecting TWOTAIL, ONETAIL, SIG, or NOSIG.
    3 | CORRELATIONS PRINT=**.
      |                    ^~"

"correlations.sps:4.25-4.26: error: CORRELATIONS: Syntax error expecting DESCRIPTIVES, XPROD, or ALL.
    4 | CORRELATIONS STATISTICS=**.
      |                         ^~"

"correlations.sps:5.14-5.15: error: CORRELATIONS: Syntax error expecting variable name.
    5 | CORRELATIONS **.
      |              ^~"

"correlations.sps:6.25-6.26: error: CORRELATIONS: Syntax error expecting variable name.
    6 | CORRELATIONS x y z WITH **.
      |                         ^~"

"correlations.sps:7.1-7.12: error: CORRELATIONS: No variables specified.
    7 | CORRELATIONS.
      | ^~~~~~~~~~~~"
])
AT_CLEANUP