You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TestCountFuncs.java 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.hssf.record.formula.functions;
  16. import junit.framework.AssertionFailedError;
  17. import junit.framework.TestCase;
  18. import org.apache.poi.hssf.HSSFTestDataSamples;
  19. import org.apache.poi.hssf.record.formula.eval.AreaEval;
  20. import org.apache.poi.hssf.record.formula.eval.BlankEval;
  21. import org.apache.poi.hssf.record.formula.eval.BoolEval;
  22. import org.apache.poi.hssf.record.formula.eval.ErrorEval;
  23. import org.apache.poi.hssf.record.formula.eval.NumberEval;
  24. import org.apache.poi.hssf.record.formula.eval.OperandResolver;
  25. import org.apache.poi.hssf.record.formula.eval.StringEval;
  26. import org.apache.poi.hssf.record.formula.eval.ValueEval;
  27. import org.apache.poi.hssf.record.formula.functions.CountUtils.I_MatchPredicate;
  28. import org.apache.poi.hssf.usermodel.HSSFCell;
  29. import org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator;
  30. import org.apache.poi.hssf.usermodel.HSSFRow;
  31. import org.apache.poi.hssf.usermodel.HSSFSheet;
  32. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  33. import org.apache.poi.ss.usermodel.CellValue;
  34. /**
  35. * Test cases for COUNT(), COUNTA() COUNTIF(), COUNTBLANK()
  36. *
  37. * @author Josh Micich
  38. */
  39. public final class TestCountFuncs extends TestCase {
  40. private static final String NULL = null;
  41. public void testCountBlank() {
  42. AreaEval range;
  43. ValueEval[] values;
  44. values = new ValueEval[] {
  45. new NumberEval(0),
  46. new StringEval(""), // note - does not match blank
  47. BoolEval.TRUE,
  48. BoolEval.FALSE,
  49. ErrorEval.DIV_ZERO,
  50. BlankEval.INSTANCE,
  51. };
  52. range = EvalFactory.createAreaEval("A1:B3", values);
  53. confirmCountBlank(1, range);
  54. values = new ValueEval[] {
  55. new NumberEval(0),
  56. new StringEval(""), // note - does not match blank
  57. BlankEval.INSTANCE,
  58. BoolEval.FALSE,
  59. BoolEval.TRUE,
  60. BlankEval.INSTANCE,
  61. };
  62. range = EvalFactory.createAreaEval("A1:B3", values);
  63. confirmCountBlank(2, range);
  64. }
  65. public void testCountA() {
  66. ValueEval[] args;
  67. args = new ValueEval[] {
  68. new NumberEval(0),
  69. };
  70. confirmCountA(1, args);
  71. args = new ValueEval[] {
  72. new NumberEval(0),
  73. new NumberEval(0),
  74. new StringEval(""),
  75. };
  76. confirmCountA(3, args);
  77. args = new ValueEval[] {
  78. EvalFactory.createAreaEval("D2:F5", new ValueEval[12]),
  79. };
  80. confirmCountA(12, args);
  81. args = new ValueEval[] {
  82. EvalFactory.createAreaEval("D1:F5", new ValueEval[15]),
  83. EvalFactory.createRefEval("A1"),
  84. EvalFactory.createAreaEval("A1:G6", new ValueEval[42]),
  85. new NumberEval(0),
  86. };
  87. confirmCountA(59, args);
  88. }
  89. public void testCountIf() {
  90. AreaEval range;
  91. ValueEval[] values;
  92. // when criteria is a boolean value
  93. values = new ValueEval[] {
  94. new NumberEval(0),
  95. new StringEval("TRUE"), // note - does not match boolean TRUE
  96. BoolEval.TRUE,
  97. BoolEval.FALSE,
  98. BoolEval.TRUE,
  99. BlankEval.INSTANCE,
  100. };
  101. range = EvalFactory.createAreaEval("A1:B3", values);
  102. confirmCountIf(2, range, BoolEval.TRUE);
  103. // when criteria is numeric
  104. values = new ValueEval[] {
  105. new NumberEval(0),
  106. new StringEval("2"),
  107. new StringEval("2.001"),
  108. new NumberEval(2),
  109. new NumberEval(2),
  110. BoolEval.TRUE,
  111. };
  112. range = EvalFactory.createAreaEval("A1:B3", values);
  113. confirmCountIf(3, range, new NumberEval(2));
  114. // note - same results when criteria is a string that parses as the number with the same value
  115. confirmCountIf(3, range, new StringEval("2.00"));
  116. // when criteria is an expression (starting with a comparison operator)
  117. confirmCountIf(2, range, new StringEval(">1"));
  118. // when criteria is an expression (starting with a comparison operator)
  119. confirmCountIf(2, range, new StringEval(">0.5"));
  120. }
  121. public void testCriteriaPredicateNe_Bug46647() {
  122. I_MatchPredicate mp = Countif.createCriteriaPredicate(new StringEval("<>aa"), 0, 0);
  123. StringEval seA = new StringEval("aa"); // this should not match the criteria '<>aa'
  124. StringEval seB = new StringEval("bb"); // this should match
  125. if (mp.matches(seA) && !mp.matches(seB)) {
  126. throw new AssertionFailedError("Identified bug 46647");
  127. }
  128. assertFalse(mp.matches(seA));
  129. assertTrue(mp.matches(seB));
  130. // general tests for not-equal (<>) operator
  131. AreaEval range;
  132. ValueEval[] values;
  133. values = new ValueEval[] {
  134. new StringEval("aa"),
  135. new StringEval("def"),
  136. new StringEval("aa"),
  137. new StringEval("ghi"),
  138. new StringEval("aa"),
  139. new StringEval("aa"),
  140. };
  141. range = EvalFactory.createAreaEval("A1:A6", values);
  142. confirmCountIf(2, range, new StringEval("<>aa"));
  143. values = new ValueEval[] {
  144. new StringEval("ab"),
  145. new StringEval("aabb"),
  146. new StringEval("aa"), // match
  147. new StringEval("abb"),
  148. new StringEval("aab"),
  149. new StringEval("ba"), // match
  150. };
  151. range = EvalFactory.createAreaEval("A1:A6", values);
  152. confirmCountIf(2, range, new StringEval("<>a*b"));
  153. values = new ValueEval[] {
  154. new NumberEval(222),
  155. new NumberEval(222),
  156. new NumberEval(111),
  157. new StringEval("aa"),
  158. new StringEval("111"),
  159. };
  160. range = EvalFactory.createAreaEval("A1:A5", values);
  161. confirmCountIf(4, range, new StringEval("<>111"));
  162. }
  163. /**
  164. * special case where the criteria argument is a cell reference
  165. */
  166. public void testCountIfWithCriteriaReference() {
  167. ValueEval[] values = {
  168. new NumberEval(22),
  169. new NumberEval(25),
  170. new NumberEval(21),
  171. new NumberEval(25),
  172. new NumberEval(25),
  173. new NumberEval(25),
  174. };
  175. AreaEval arg0 = EvalFactory.createAreaEval("C1:C6", values);
  176. ValueEval criteriaArg = EvalFactory.createRefEval("A1", new NumberEval(25));
  177. ValueEval[] args= { arg0, criteriaArg, };
  178. double actual = NumericFunctionInvoker.invoke(new Countif(), args);
  179. assertEquals(4, actual, 0D);
  180. }
  181. private static void confirmCountA(int expected, ValueEval[] args) {
  182. double result = NumericFunctionInvoker.invoke(new Counta(), args);
  183. assertEquals(expected, result, 0);
  184. }
  185. private static void confirmCountIf(int expected, AreaEval range, ValueEval criteria) {
  186. ValueEval[] args = { range, criteria, };
  187. double result = NumericFunctionInvoker.invoke(new Countif(), args);
  188. assertEquals(expected, result, 0);
  189. }
  190. private static void confirmCountBlank(int expected, AreaEval range) {
  191. ValueEval[] args = { range };
  192. double result = NumericFunctionInvoker.invoke(new Countblank(), args);
  193. assertEquals(expected, result, 0);
  194. }
  195. private static I_MatchPredicate createCriteriaPredicate(ValueEval ev) {
  196. return Countif.createCriteriaPredicate(ev, 0, 0);
  197. }
  198. /**
  199. * the criteria arg is mostly handled by {@link OperandResolver#getSingleValue(Eval, int, short)}
  200. */
  201. public void testCountifAreaCriteria() {
  202. int srcColIx = 2; // anything but column A
  203. ValueEval v0 = new NumberEval(2.0);
  204. ValueEval v1 = new StringEval("abc");
  205. ValueEval v2 = ErrorEval.DIV_ZERO;
  206. AreaEval ev = EvalFactory.createAreaEval("A10:A12", new ValueEval[] { v0, v1, v2, });
  207. I_MatchPredicate mp;
  208. mp = Countif.createCriteriaPredicate(ev, 9, srcColIx);
  209. confirmPredicate(true, mp, srcColIx);
  210. confirmPredicate(false, mp, "abc");
  211. confirmPredicate(false, mp, ErrorEval.DIV_ZERO);
  212. mp = Countif.createCriteriaPredicate(ev, 10, srcColIx);
  213. confirmPredicate(false, mp, srcColIx);
  214. confirmPredicate(true, mp, "abc");
  215. confirmPredicate(false, mp, ErrorEval.DIV_ZERO);
  216. mp = Countif.createCriteriaPredicate(ev, 11, srcColIx);
  217. confirmPredicate(false, mp, srcColIx);
  218. confirmPredicate(false, mp, "abc");
  219. confirmPredicate(true, mp, ErrorEval.DIV_ZERO);
  220. confirmPredicate(false, mp, ErrorEval.VALUE_INVALID);
  221. // tricky: indexing outside of A10:A12
  222. // even this #VALUE! error gets used by COUNTIF as valid criteria
  223. mp = Countif.createCriteriaPredicate(ev, 12, srcColIx);
  224. confirmPredicate(false, mp, srcColIx);
  225. confirmPredicate(false, mp, "abc");
  226. confirmPredicate(false, mp, ErrorEval.DIV_ZERO);
  227. confirmPredicate(true, mp, ErrorEval.VALUE_INVALID);
  228. }
  229. public void testCountifEmptyStringCriteria() {
  230. I_MatchPredicate mp;
  231. // pred '=' matches blank cell but not empty string
  232. mp = createCriteriaPredicate(new StringEval("="));
  233. confirmPredicate(false, mp, "");
  234. confirmPredicate(true, mp, NULL);
  235. // pred '' matches both blank cell but not empty string
  236. mp = createCriteriaPredicate(new StringEval(""));
  237. confirmPredicate(true, mp, "");
  238. confirmPredicate(true, mp, NULL);
  239. // pred '<>' matches empty string but not blank cell
  240. mp = createCriteriaPredicate(new StringEval("<>"));
  241. confirmPredicate(false, mp, NULL);
  242. confirmPredicate(true, mp, "");
  243. }
  244. public void testCountifComparisons() {
  245. I_MatchPredicate mp;
  246. mp = createCriteriaPredicate(new StringEval(">5"));
  247. confirmPredicate(false, mp, 4);
  248. confirmPredicate(false, mp, 5);
  249. confirmPredicate(true, mp, 6);
  250. mp = createCriteriaPredicate(new StringEval("<=5"));
  251. confirmPredicate(true, mp, 4);
  252. confirmPredicate(true, mp, 5);
  253. confirmPredicate(false, mp, 6);
  254. confirmPredicate(false, mp, "4.9");
  255. confirmPredicate(false, mp, "4.9t");
  256. confirmPredicate(false, mp, "5.1");
  257. confirmPredicate(false, mp, NULL);
  258. mp = createCriteriaPredicate(new StringEval("=abc"));
  259. confirmPredicate(true, mp, "abc");
  260. mp = createCriteriaPredicate(new StringEval("=42"));
  261. confirmPredicate(false, mp, 41);
  262. confirmPredicate(true, mp, 42);
  263. confirmPredicate(true, mp, "42");
  264. mp = createCriteriaPredicate(new StringEval(">abc"));
  265. confirmPredicate(false, mp, 4);
  266. confirmPredicate(false, mp, "abc");
  267. confirmPredicate(true, mp, "abd");
  268. mp = createCriteriaPredicate(new StringEval(">4t3"));
  269. confirmPredicate(false, mp, 4);
  270. confirmPredicate(false, mp, 500);
  271. confirmPredicate(true, mp, "500");
  272. confirmPredicate(true, mp, "4t4");
  273. }
  274. /**
  275. * the criteria arg value can be an error code (the error does not
  276. * propagate to the COUNTIF result).
  277. */
  278. public void testCountifErrorCriteria() {
  279. I_MatchPredicate mp;
  280. mp = createCriteriaPredicate(new StringEval("#REF!"));
  281. confirmPredicate(false, mp, 4);
  282. confirmPredicate(false, mp, "#REF!");
  283. confirmPredicate(true, mp, ErrorEval.REF_INVALID);
  284. mp = createCriteriaPredicate(new StringEval("<#VALUE!"));
  285. confirmPredicate(false, mp, 4);
  286. confirmPredicate(false, mp, "#DIV/0!");
  287. confirmPredicate(false, mp, "#REF!");
  288. confirmPredicate(true, mp, ErrorEval.DIV_ZERO);
  289. confirmPredicate(false, mp, ErrorEval.REF_INVALID);
  290. // not quite an error literal, should be treated as plain text
  291. mp = createCriteriaPredicate(new StringEval("<=#REF!a"));
  292. confirmPredicate(false, mp, 4);
  293. confirmPredicate(true, mp, "#DIV/0!");
  294. confirmPredicate(true, mp, "#REF!");
  295. confirmPredicate(false, mp, ErrorEval.DIV_ZERO);
  296. confirmPredicate(false, mp, ErrorEval.REF_INVALID);
  297. }
  298. public void testWildCards() {
  299. I_MatchPredicate mp;
  300. mp = createCriteriaPredicate(new StringEval("a*b"));
  301. confirmPredicate(false, mp, "abc");
  302. confirmPredicate(true, mp, "ab");
  303. confirmPredicate(true, mp, "axxb");
  304. confirmPredicate(false, mp, "xab");
  305. mp = createCriteriaPredicate(new StringEval("a?b"));
  306. confirmPredicate(false, mp, "abc");
  307. confirmPredicate(false, mp, "ab");
  308. confirmPredicate(false, mp, "axxb");
  309. confirmPredicate(false, mp, "xab");
  310. confirmPredicate(true, mp, "axb");
  311. mp = createCriteriaPredicate(new StringEval("a~?"));
  312. confirmPredicate(false, mp, "a~a");
  313. confirmPredicate(false, mp, "a~?");
  314. confirmPredicate(true, mp, "a?");
  315. mp = createCriteriaPredicate(new StringEval("~*a"));
  316. confirmPredicate(false, mp, "~aa");
  317. confirmPredicate(false, mp, "~*a");
  318. confirmPredicate(true, mp, "*a");
  319. mp = createCriteriaPredicate(new StringEval("12?12"));
  320. confirmPredicate(false, mp, 12812);
  321. confirmPredicate(true, mp, "12812");
  322. confirmPredicate(false, mp, "128812");
  323. }
  324. public void testNotQuiteWildCards() {
  325. I_MatchPredicate mp;
  326. // make sure special reg-ex chars are treated like normal chars
  327. mp = createCriteriaPredicate(new StringEval("a.b"));
  328. confirmPredicate(false, mp, "aab");
  329. confirmPredicate(true, mp, "a.b");
  330. mp = createCriteriaPredicate(new StringEval("a~b"));
  331. confirmPredicate(false, mp, "ab");
  332. confirmPredicate(false, mp, "axb");
  333. confirmPredicate(false, mp, "a~~b");
  334. confirmPredicate(true, mp, "a~b");
  335. mp = createCriteriaPredicate(new StringEval(">a*b"));
  336. confirmPredicate(false, mp, "a(b");
  337. confirmPredicate(true, mp, "aab");
  338. confirmPredicate(false, mp, "a*a");
  339. confirmPredicate(true, mp, "a*c");
  340. }
  341. private static void confirmPredicate(boolean expectedResult, I_MatchPredicate matchPredicate, int value) {
  342. assertEquals(expectedResult, matchPredicate.matches(new NumberEval(value)));
  343. }
  344. private static void confirmPredicate(boolean expectedResult, I_MatchPredicate matchPredicate, String value) {
  345. ValueEval ev = value == null ? BlankEval.INSTANCE : new StringEval(value);
  346. assertEquals(expectedResult, matchPredicate.matches(ev));
  347. }
  348. private static void confirmPredicate(boolean expectedResult, I_MatchPredicate matchPredicate, ErrorEval value) {
  349. assertEquals(expectedResult, matchPredicate.matches(value));
  350. }
  351. public void testCountifFromSpreadsheet() {
  352. testCountFunctionFromSpreadsheet("countifExamples.xls", 1, 2, 3, "countif");
  353. }
  354. public void testCountBlankFromSpreadsheet() {
  355. testCountFunctionFromSpreadsheet("countblankExamples.xls", 1, 3, 4, "countblank");
  356. }
  357. private static void testCountFunctionFromSpreadsheet(String FILE_NAME, int START_ROW_IX, int COL_IX_ACTUAL, int COL_IX_EXPECTED, String functionName) {
  358. int failureCount = 0;
  359. HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook(FILE_NAME);
  360. HSSFSheet sheet = wb.getSheetAt(0);
  361. HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(wb);
  362. int maxRow = sheet.getLastRowNum();
  363. for (int rowIx=START_ROW_IX; rowIx<maxRow; rowIx++) {
  364. HSSFRow row = sheet.getRow(rowIx);
  365. if(row == null) {
  366. continue;
  367. }
  368. HSSFCell cell = row.getCell(COL_IX_ACTUAL);
  369. CellValue cv = fe.evaluate(cell);
  370. double actualValue = cv.getNumberValue();
  371. double expectedValue = row.getCell(COL_IX_EXPECTED).getNumericCellValue();
  372. if (actualValue != expectedValue) {
  373. System.err.println("Problem with test case on row " + (rowIx+1) + " "
  374. + "Expected = (" + expectedValue + ") Actual=(" + actualValue + ") ");
  375. failureCount++;
  376. }
  377. }
  378. if (failureCount > 0) {
  379. throw new AssertionFailedError(failureCount + " " + functionName
  380. + " evaluations failed. See stderr for more details");
  381. }
  382. }
  383. }