Line 0
Link Here
|
|
|
1 |
/* |
2 |
* ==================================================================== |
3 |
* Licensed to the Apache Software Foundation (ASF) under one or more |
4 |
* contributor license agreements. See the NOTICE file distributed with |
5 |
* this work for additional information regarding copyright ownership. |
6 |
* The ASF licenses this file to You under the Apache License, Version 2.0 |
7 |
* (the "License"); you may not use this file except in compliance with |
8 |
* the License. You may obtain a copy of the License at |
9 |
* |
10 |
* http://www.apache.org/licenses/LICENSE-2.0 |
11 |
* |
12 |
* Unless required by applicable law or agreed to in writing, software |
13 |
* distributed under the License is distributed on an "AS IS" BASIS, |
14 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
15 |
* See the License for the specific language governing permissions and |
16 |
* limitations under the License. |
17 |
* ==================================================================== |
18 |
*/ |
19 |
|
20 |
package org.apache.poi.ss.formula.functions; |
21 |
|
22 |
import junit.framework.AssertionFailedError; |
23 |
import junit.framework.TestCase; |
24 |
import org.apache.poi.ss.formula.OperationEvaluationContext; |
25 |
import org.apache.poi.ss.formula.eval.*; |
26 |
|
27 |
/** |
28 |
* Test cases for SUMIFS() |
29 |
* |
30 |
* @author Yegor Kozlov |
31 |
*/ |
32 |
public final class TestSumifs extends TestCase { |
33 |
|
34 |
private static final OperationEvaluationContext EC = new OperationEvaluationContext(null, null, 0, 1, 0, null); |
35 |
|
36 |
private static ValueEval invokeSumifs(ValueEval[] args, OperationEvaluationContext ec) { |
37 |
return new Sumifs().evaluate(args, EC); |
38 |
} |
39 |
private static void confirmDouble(double expected, ValueEval actualEval) { |
40 |
if(!(actualEval instanceof NumericValueEval)) { |
41 |
throw new AssertionFailedError("Expected numeric result"); |
42 |
} |
43 |
NumericValueEval nve = (NumericValueEval)actualEval; |
44 |
assertEquals(expected, nve.getNumberValue(), 0); |
45 |
} |
46 |
|
47 |
private static void confirm(double expectedResult, ValueEval[] args) { |
48 |
confirmDouble(expectedResult, invokeSumifs(args, EC)); |
49 |
} |
50 |
|
51 |
/** |
52 |
* Example 1 from |
53 |
* http://office.microsoft.com/en-us/excel-help/sumifs-function-HA010047504.aspx |
54 |
*/ |
55 |
public void testExample1() { |
56 |
// mimic test sample from http://office.microsoft.com/en-us/excel-help/sumifs-function-HA010047504.aspx |
57 |
ValueEval[] a2a9 = new ValueEval[] { |
58 |
new NumberEval(5), |
59 |
new NumberEval(4), |
60 |
new NumberEval(15), |
61 |
new NumberEval(3), |
62 |
new NumberEval(22), |
63 |
new NumberEval(12), |
64 |
new NumberEval(10), |
65 |
new NumberEval(33) |
66 |
}; |
67 |
|
68 |
ValueEval[] b2b9 = new ValueEval[] { |
69 |
new StringEval("Apples"), |
70 |
new StringEval("Apples"), |
71 |
new StringEval("Artichokes"), |
72 |
new StringEval("Artichokes"), |
73 |
new StringEval("Bananas"), |
74 |
new StringEval("Bananas"), |
75 |
new StringEval("Carrots"), |
76 |
new StringEval("Carrots"), |
77 |
}; |
78 |
|
79 |
ValueEval[] c2c9 = new ValueEval[] { |
80 |
new NumberEval(1), |
81 |
new NumberEval(2), |
82 |
new NumberEval(1), |
83 |
new NumberEval(2), |
84 |
new NumberEval(1), |
85 |
new NumberEval(2), |
86 |
new NumberEval(1), |
87 |
new NumberEval(2) |
88 |
}; |
89 |
|
90 |
ValueEval[] args; |
91 |
// "=SUMIFS(A2:A9, B2:B9, "=A*", C2:C9, 1)" |
92 |
args = new ValueEval[]{ |
93 |
EvalFactory.createAreaEval("A2:A9", a2a9), |
94 |
EvalFactory.createAreaEval("B2:B9", b2b9), |
95 |
new StringEval("A*"), |
96 |
EvalFactory.createAreaEval("C2:C9", c2c9), |
97 |
new NumberEval(1), |
98 |
}; |
99 |
confirm(20.0, args); |
100 |
|
101 |
// "=SUMIFS(A2:A9, B2:B9, "<>Bananas", C2:C9, 1)" |
102 |
args = new ValueEval[]{ |
103 |
EvalFactory.createAreaEval("A2:A9", a2a9), |
104 |
EvalFactory.createAreaEval("B2:B9", b2b9), |
105 |
new StringEval("<>Bananas"), |
106 |
EvalFactory.createAreaEval("C2:C9", c2c9), |
107 |
new NumberEval(1), |
108 |
}; |
109 |
confirm(30.0, args); |
110 |
|
111 |
} |
112 |
|
113 |
/** |
114 |
* Example 2 from |
115 |
* http://office.microsoft.com/en-us/excel-help/sumifs-function-HA010047504.aspx |
116 |
*/ |
117 |
public void testExample2() { |
118 |
} |
119 |
|
120 |
/** |
121 |
* Example 3 from |
122 |
* http://office.microsoft.com/en-us/excel-help/sumifs-function-HA010047504.aspx |
123 |
*/ |
124 |
public void testExample3() { |
125 |
} |
126 |
|
127 |
/** |
128 |
* Example 4 from |
129 |
* http://office.microsoft.com/en-us/excel-help/sumifs-function-HA010047504.aspx |
130 |
*/ |
131 |
public void testExample4() { |
132 |
} |
133 |
|
134 |
/** |
135 |
* Example 5 from |
136 |
* http://office.microsoft.com/en-us/excel-help/sumifs-function-HA010047504.aspx |
137 |
*/ |
138 |
public void testExample5() { |
139 |
} |
140 |
|
141 |
/** |
142 |
* Test passing invalid or wrong arguments |
143 |
*/ |
144 |
public void testInvalidArguments() { |
145 |
} |
146 |
} |