From ca53a08a9017362fc53b4e71735d17edd667d0fb Mon Sep 17 00:00:00 2001 From: Javen O'Neal Date: Mon, 6 Nov 2017 22:19:06 +0000 Subject: [PATCH] bug 61727: make TestCellRangeUtil unit tests more explicit git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1814444 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/poi/ss/util/TestCellRangeUtil.java | 61 +++++++++++-------- 1 file changed, 34 insertions(+), 27 deletions(-) diff --git a/src/testcases/org/apache/poi/ss/util/TestCellRangeUtil.java b/src/testcases/org/apache/poi/ss/util/TestCellRangeUtil.java index fd52a29a4b..8bac4b580e 100644 --- a/src/testcases/org/apache/poi/ss/util/TestCellRangeUtil.java +++ b/src/testcases/org/apache/poi/ss/util/TestCellRangeUtil.java @@ -19,6 +19,7 @@ package org.apache.poi.ss.util; import org.junit.Test; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertArrayEquals; import static org.junit.Assume.assumeTrue; /** @@ -32,38 +33,44 @@ public final class TestCellRangeUtil { private static final CellRangeAddress B1 = new CellRangeAddress(0, 0, 1, 1); private static final CellRangeAddress A2 = new CellRangeAddress(1, 1, 0, 0); private static final CellRangeAddress B2 = new CellRangeAddress(1, 1, 1, 1); + private static final CellRangeAddress A1_B2 = new CellRangeAddress(0, 1, 0, 1); + private static final CellRangeAddress A1_B1 = new CellRangeAddress(0, 0, 0, 1); + private static final CellRangeAddress A1_A2 = new CellRangeAddress(0, 1, 0, 0); @Test public void testMergeCellRanges() { - testMergeCellRange(asArray(A1, B1, A2, B2), 1); - // suboptimal result for permuted arguments (2 ranges instead of one) - testMergeCellRange(asArray(A1, B2, A2, A1), 2); - - testMergeCellRange(asArray(A1, B1, A2), 2); - testMergeCellRange(asArray(A2, A1, B1), 2); - testMergeCellRange(asArray(B1, A2, A1), 2); - - testMergeCellRange(asArray(A1, B2), 2); - testMergeCellRange(asArray(B2, A1), 2); + // Note that the order of the output array elements does not matter + // And that there may be more than one valid outputs for a given input. Any valid output is accepted. + // POI should use a strategy that is consistent and predictable (it currently is not). + + // Fully mergeable + // A B + // 1 x x A1,A2,B1,B2 --> A1:B2 + // 2 x x + assertArrayEquals(asArray(A1_B2), merge(A1, B1, A2, B2)); + assertArrayEquals(asArray(A1_B2), merge(A1, B2, A2, B1)); + + // Partially mergeable: multiple possible mergings + // A B + // 1 x x A1,A2,B1 --> A1:B1,A2 or A1:A2,B1 + // 2 x + assertArrayEquals(asArray(A1_B1, A2), merge(A1, B1, A2)); + assertArrayEquals(asArray(A1_A2, B1), merge(A2, A1, B1)); + assertArrayEquals(asArray(A1_B1, A2), merge(B1, A2, A1)); + + // Not mergeable + // A B + // 1 x A1,B2 --> A1,B2 + // 2 x + assertArrayEquals(asArray(A1, B2), merge(A1, B2)); + assertArrayEquals(asArray(B2, A1), merge(B2, A1)); } - - private void testMergeCellRange(CellRangeAddress[] input, int expectedResultRangeLength) { - CellRangeAddress[] result = CellRangeUtil.mergeCellRanges( input ); - assertEquals( expectedResultRangeLength, result.length ); - assertResultExactlyContainsInput(result, result); - } - - private static void assertResultExactlyContainsInput(CellRangeAddress[] result, CellRangeAddress[] input) { - for(CellRangeAddress inputEntry: input) { - boolean isInResultRange = false; - for(CellRangeAddress resultEntry: result) { - isInResultRange |= resultEntry.intersects(inputEntry); - } - assumeTrue(isInResultRange); - } - } - + private static T[] asArray(T...ts) { return ts; } + + private static CellRangeAddress[] merge(CellRangeAddress... ranges) { + return CellRangeUtil.mergeCellRanges(ranges); + } }