Skip to content

Commit

Permalink
Tests for CoordinateIterator;
Browse files Browse the repository at this point in the history
fix coding style.


git-svn-id: https://olap4j.svn.sourceforge.net/svnroot/olap4j/trunk@157 c6a108a4-781c-0410-a6c6-c2d559e19af0
  • Loading branch information
julianhyde committed Apr 19, 2009
1 parent 93fc515 commit fd57914
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/org/olap4j/query/RectangularCellSetFormatter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
39 changes: 29 additions & 10 deletions src/org/olap4j/query/TraditionalCellSetFormatter.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,34 +35,49 @@ 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<CellSetAxis> 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<Integer> pos = new ArrayList<Integer>(axes.size());
for (CellSetAxis axis : axes) {
List<Integer> pos = new ArrayList<Integer>(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<Integer> pos)
{
CellSetAxis _axis = axis < 0 ?
cellSet.getFilterAxis() :
cellSet.getAxes().get(axis);
List<Position> 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(", ");
Expand All @@ -71,15 +86,17 @@ 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);
if (axis == 0) {
pw.println();
}
}
i++;
}
}

Expand Down Expand Up @@ -119,3 +136,5 @@ private static void printCell(
pw.print(cell.getFormattedValue());
}
}

// End TraditionalCellSetFormatter.java
113 changes: 111 additions & 2 deletions testsrc/org/olap4j/impl/Olap4jUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
// 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.
*/
package org.olap4j.impl;

import junit.framework.TestCase;

import java.util.Arrays;
import java.util.*;

/**
* Tests for methods in {@link org.olap4j.impl.Olap4jUtil}.
Expand All @@ -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<Integer> expectedList = new ArrayList<Integer>();
for (int i : expected) {
expectedList.add(i);
}
List<Integer> actualList = new ArrayList<Integer>();
for (int i : actual) {
actualList.add(i);
}
assertEquals(expectedList, actualList);
}
}

//~ Tests follow ===========================================================

public void testCamel() {
assertEquals(
"FOO_BAR",
Expand Down Expand Up @@ -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

0 comments on commit fd57914

Please sign in to comment.