From fd57914bc4c669d86f35c4a5ed12ec72c42cecd4 Mon Sep 17 00:00:00 2001 From: Julian Hyde Date: Sun, 19 Apr 2009 01:33:03 +0000 Subject: [PATCH] Tests for CoordinateIterator; fix coding style. git-svn-id: https://olap4j.svn.sourceforge.net/svnroot/olap4j/trunk@157 c6a108a4-781c-0410-a6c6-c2d559e19af0 --- .../query/RectangularCellSetFormatter.java | 2 +- .../query/TraditionalCellSetFormatter.java | 39 ++++-- testsrc/org/olap4j/impl/Olap4jUtilTest.java | 113 +++++++++++++++++- 3 files changed, 141 insertions(+), 13 deletions(-) diff --git a/src/org/olap4j/query/RectangularCellSetFormatter.java b/src/org/olap4j/query/RectangularCellSetFormatter.java index 8bf8a04..631b28a 100644 --- a/src/org/olap4j/query/RectangularCellSetFormatter.java +++ b/src/org/olap4j/query/RectangularCellSetFormatter.java @@ -226,7 +226,7 @@ private void formatPage( for (int y = 0; y < matrix.height; y++) { for (int x = 0; x < matrix.width; x++) { if (x > 0) { - pw.print(' '); + pw.print(' '); } final MatrixCell cell = matrix.get(x, y); final int len; diff --git a/src/org/olap4j/query/TraditionalCellSetFormatter.java b/src/org/olap4j/query/TraditionalCellSetFormatter.java index eef59fe..32fcd1c 100644 --- a/src/org/olap4j/query/TraditionalCellSetFormatter.java +++ b/src/org/olap4j/query/TraditionalCellSetFormatter.java @@ -35,25 +35,40 @@ public void format( print(cellSet, pw); } + /** + * Prints a cell set. + * + * @param cellSet Cell set + * @param pw Writer + */ private static void print(CellSet cellSet, PrintWriter pw) { pw.println("Axis #0:"); printAxis(pw, cellSet.getFilterAxis()); final List axes = cellSet.getAxes(); - int i = 0; - for (CellSetAxis axis : axes) { - pw.println("Axis #" + (++i) + ":"); + final int axisCount = axes.size(); + for (int i = 0; i < axisCount; i++) { + CellSetAxis axis = axes.get(i); + pw.println("Axis #" + (i + 1) + ":"); printAxis(pw, axis); } // Usually there are 3 axes: {filter, columns, rows}. Position is a // {column, row} pair. We call printRows with axis=2. When it // recurses to axis=-1, it prints. - List pos = new ArrayList(axes.size()); - for (CellSetAxis axis : axes) { + List pos = new ArrayList(axisCount); + for (int i = 0; i < axisCount; i++) { pos.add(-1); } - printRows(cellSet, pw, axes.size() - 1, pos); + printRows(cellSet, pw, axisCount - 1, pos); } + /** + * Prints the rows of cell set. + * + * @param cellSet Cell set + * @param pw Writer + * @param axis Axis ordinal + * @param pos Partial coordinate + */ private static void printRows( CellSet cellSet, PrintWriter pw, int axis, List pos) { @@ -61,8 +76,8 @@ private static void printRows( cellSet.getFilterAxis() : cellSet.getAxes().get(axis); List positions = _axis.getPositions(); - int i = 0; - for (Position position : positions) { + final int positionCount = positions.size(); + for (int i = 0; i < positionCount; i++) { if (axis < 0) { if (i > 0) { pw.print(", "); @@ -71,7 +86,10 @@ private static void printRows( } else { pos.set(axis, i); if (axis == 0) { - int row = axis + 1 < pos.size() ? pos.get(axis + 1) : 0; + int row = + axis + 1 < pos.size() + ? pos.get(axis + 1) + : 0; pw.print("Row #" + row + ": "); } printRows(cellSet, pw, axis - 1, pos); @@ -79,7 +97,6 @@ private static void printRows( pw.println(); } } - i++; } } @@ -119,3 +136,5 @@ private static void printCell( pw.print(cell.getFormattedValue()); } } + +// End TraditionalCellSetFormatter.java diff --git a/testsrc/org/olap4j/impl/Olap4jUtilTest.java b/testsrc/org/olap4j/impl/Olap4jUtilTest.java index 50367a8..6208045 100644 --- a/testsrc/org/olap4j/impl/Olap4jUtilTest.java +++ b/testsrc/org/olap4j/impl/Olap4jUtilTest.java @@ -2,7 +2,7 @@ // This software is subject to the terms of the Common Public License // Agreement, available at the following URL: // http://www.opensource.org/licenses/cpl.html. -// Copyright (C) 2007-2008 Julian Hyde +// Copyright (C) 2007-2009 Julian Hyde // All Rights Reserved. // You must accept the terms of that agreement to use this software. */ @@ -10,7 +10,7 @@ import junit.framework.TestCase; -import java.util.Arrays; +import java.util.*; /** * Tests for methods in {@link org.olap4j.impl.Olap4jUtil}. @@ -20,6 +20,33 @@ * @since Dec 12, 2007 */ public class Olap4jUtilTest extends TestCase { + + //~ Helper methods ========================================================= + + /** + * Asserts that two integer arrays have equal length and contents. + * + * @param expected Expected integer array + * @param actual Actual integer array + */ + public void assertEqualsArray(int[] expected, int[] actual) { + if (expected == null) { + assertEquals(expected, actual); + } else { + List expectedList = new ArrayList(); + for (int i : expected) { + expectedList.add(i); + } + List actualList = new ArrayList(); + for (int i : actual) { + actualList.add(i); + } + assertEquals(expectedList, actualList); + } + } + + //~ Tests follow =========================================================== + public void testCamel() { assertEquals( "FOO_BAR", @@ -55,6 +82,88 @@ public void testWildcard() { Olap4jUtil.wildcardToRegexp( Arrays.asList("_Foo_", "Bar%BAZ"))); } + + /** + * Tests {@link org.olap4j.impl.CoordinateIterator}. + */ + public void testCoordinateIterator() { + // no axes, should produce one result + CoordinateIterator iter = new CoordinateIterator(new int[]{}); + assertTrue(iter.hasNext()); + assertEqualsArray(iter.next(), new int[] {}); + + // one axis of length n, should produce n elements + iter = new CoordinateIterator(new int[]{2}); + assertTrue(iter.hasNext()); + assertEqualsArray(iter.next(), new int[] {0}); + assertTrue(iter.hasNext()); + assertEqualsArray(iter.next(), new int[] {1}); + assertFalse(iter.hasNext()); + + // one axis of length 0, should produce 0 elements + iter = new CoordinateIterator(new int[]{0}); + assertFalse(iter.hasNext()); + + // two axes of length 0, should produce 0 elements + iter = new CoordinateIterator(new int[]{0, 0}); + assertFalse(iter.hasNext()); + + // five axes of length 0, should produce 0 elements + iter = new CoordinateIterator(new int[]{0, 0, 0, 0, 0}); + assertFalse(iter.hasNext()); + + // two axes, neither empty + iter = new CoordinateIterator(new int[]{2, 3}); + assertTrue(iter.hasNext()); + assertEqualsArray(iter.next(), new int[] {0, 0}); + assertTrue(iter.hasNext()); + assertEqualsArray(iter.next(), new int[] {0, 1}); + assertTrue(iter.hasNext()); + assertEqualsArray(iter.next(), new int[] {0, 2}); + assertTrue(iter.hasNext()); + assertEqualsArray(iter.next(), new int[] {1, 0}); + assertTrue(iter.hasNext()); + assertEqualsArray(iter.next(), new int[] {1, 1}); + assertTrue(iter.hasNext()); + assertEqualsArray(iter.next(), new int[] {1, 2}); + assertFalse(iter.hasNext()); + + // three axes, one of length 0, should produce 0 elements + iter = new CoordinateIterator(new int[]{10, 0, 2}); + assertFalse(iter.hasNext()); + iter = new CoordinateIterator(new int[]{0, 10, 2}); + assertFalse(iter.hasNext()); + + // if any axis has negative length, produces 0 elements + iter = new CoordinateIterator(new int[]{3, 4, 5, -6, 7}); + assertFalse(iter.hasNext()); + iter = new CoordinateIterator(new int[]{3, 4, 5, 6, -7}); + assertFalse(iter.hasNext()); + iter = new CoordinateIterator(new int[]{-3, 4, 5, 6, 7}); + assertFalse(iter.hasNext()); + } + + /** + * Tests a little-endian {@link org.olap4j.impl.CoordinateIterator}. + */ + public void testCoordinateIteratorLittleEndian() { + // two axes, neither empty + CoordinateIterator iter = + new CoordinateIterator(new int[]{2, 3}, true); + assertTrue(iter.hasNext()); + assertEqualsArray(iter.next(), new int[] {0, 0}); + assertTrue(iter.hasNext()); + assertEqualsArray(iter.next(), new int[] {1, 0}); + assertTrue(iter.hasNext()); + assertEqualsArray(iter.next(), new int[] {0, 1}); + assertTrue(iter.hasNext()); + assertEqualsArray(iter.next(), new int[] {1, 1}); + assertTrue(iter.hasNext()); + assertEqualsArray(iter.next(), new int[] {0, 2}); + assertTrue(iter.hasNext()); + assertEqualsArray(iter.next(), new int[] {1, 2}); + assertFalse(iter.hasNext()); + } } // End Olap4jUtilTest.java