Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

PrimitiveStack: Add 'void position(int)' to set new position. Add tes…

…t case w/ initialSizeElem:=0.
  • Loading branch information...
commit 8b3e5b94bcd3167278eb972b2be37086d3bd2996 1 parent ac16df0
@sgothel authored
View
10 src/java/com/jogamp/common/util/FloatStack.java
@@ -63,6 +63,14 @@
public final int position() { return position; }
@Override
+ public final void position(int newPosition) throws IndexOutOfBoundsException {
+ if( 0 > position || position >= buffer.length ) {
+ throw new IndexOutOfBoundsException("Invalid new position "+newPosition+", "+this.toString());
+ }
+ position = newPosition;
+ }
+
+ @Override
public final int remaining() { return buffer.length - position; }
@Override
@@ -72,7 +80,7 @@
public final void setGrowSize(int newGrowSize) { growSize = newGrowSize; }
public final String toString() {
- return "FloatStack[0..(top "+position+").."+buffer.length+", remaining "+remaining()+"]";
+ return "FloatStack[0..(pos "+position+").."+buffer.length+", remaining "+remaining()+"]";
}
public final /*value*/float/*value*/[] buffer() { return buffer; }
View
18 src/java/com/jogamp/common/util/PrimitiveStack.java
@@ -41,16 +41,24 @@
int capacity();
/**
- * Returns the current position where the next put operation will store the next element.
+ * Returns the current position of this stack.
* <p>
- * The position equals to the number of elements already stored.
+ * Position is in the range: 0 &le; position &lt; {@link #capacity()}.
* </p>
- * <p>
- * 0 denotes an empty stack.
+ * <p>
+ * The position equals to the number of elements stored.
* </p>
**/
int position();
+ /**
+ * Sets the position of this stack.
+ *
+ * @param newPosition the new position
+ * @throws IndexOutOfBoundsException if <code>newPosition</code> is outside of range: 0 &le; position &lt; {@link #capacity()}.
+ */
+ void position(int newPosition) throws IndexOutOfBoundsException;
+
/**
* Returns the remaining elements left before stack will grow about {@link #getGrowSize()}.
* <pre>
@@ -68,5 +76,5 @@
int getGrowSize();
/** Set new {@link #growSize(). */
- void setGrowSize(int newGrowSize);
+ void setGrowSize(int newGrowSize);
}
View
78 src/junit/com/jogamp/common/util/TestFloatStack01.java
@@ -51,15 +51,28 @@ static final boolean equals(/*value*/float/*value*/[] b, int bOffset,
@Test
- public void test01PrimitiveArray() {
+ public void test01PrimitiveArray_I32_G02() {
+ final int initialSizeElem = 32;
+ final int growSizeElem = 2;
+ testPrimitiveArrayImpl(initialSizeElem, growSizeElem);
+ }
+
+ @Test
+ public void test02PrimitiveArray_I00_G32() {
+ final int initialSizeElem = 0;
+ final int growSizeElem = 32;
+ testPrimitiveArrayImpl(initialSizeElem, growSizeElem);
+ }
+
+ static private final boolean VERBOSE = false;
+
+ private void testPrimitiveArrayImpl(int initialSizeElem, int growSizeElem) {
final int compNum = 3;
/*value*/float/*value*/[] e0 =
new /*value*/float/*value*/[] { 0, 1, 2 };
/*value*/float/*value*/[] e1 =
new /*value*/float/*value*/[] { 3, 4, 5 };
- final int initialSizeElem = 32;
- final int growSizeElem = 2;
final int totalSizeElem = initialSizeElem+2*growSizeElem;
final int initialSizeComp = initialSizeElem*compNum;
@@ -72,27 +85,38 @@ public void test01PrimitiveArray() {
//
// PUT
//
-
+ if(VERBOSE) {
+ System.err.println("0: "+fs0);
+ }
for(int i=0; i<totalSizeElem; i++) {
if(i < initialSizeElem) {
- Assert.assertTrue("Error "+fs0, fs0.remaining() == (initialSizeElem-i)*compNum);
+ Assert.assertTrue("Error #"+i+", "+fs0, fs0.remaining() == (initialSizeElem-i)*compNum);
} else {
- int j = ( i - initialSizeElem ) % growSizeElem ;
- Assert.assertTrue("Error "+fs0, fs0.remaining() == j*compNum);
+ final int j = ( i - initialSizeElem ) % growSizeElem ;
+ final int k = ( 0 < j && j < growSizeElem ) ? growSizeElem - j : 0;
+ Assert.assertTrue("Error #"+i+"("+j+", "+k+"), "+fs0, fs0.remaining() == k*compNum);
}
Assert.assertTrue("Error "+fs0, fs0.position() == i*compNum);
- // String s;
+ String s;
if( 0 == i % 2) {
- // s = Arrays.toString(e0);
+ if(VERBOSE) {
+ s = Arrays.toString(e0);
+ }
fs0.putOnTop(e0, 0, compNum);
} else {
- // s = Arrays.toString(e1);
+ if(VERBOSE) {
+ s = Arrays.toString(e1);
+ }
fs0.putOnTop(e1, 0, compNum);
}
- // System.err.println("#"+i+"/"+totalSizeElem+": "+fs0+" <- "+s);
+ if(VERBOSE) {
+ System.err.println("#"+i+"/"+totalSizeElem+": "+fs0+" <- "+s);
+ }
+ }
+ if(VERBOSE) {
+ System.err.println("X: "+fs0);
}
- // System.err.println("X: "+fs0);
Assert.assertTrue("Error "+fs0, fs0.remaining() == 0);
Assert.assertTrue("Error "+fs0, fs0.position() == totalSizeComp);
@@ -145,7 +169,20 @@ public void test01PrimitiveArray() {
}
@Test
- public void test02FloatBuffer() {
+ public void test11FloatBuffer_I32_G02() {
+ final int initialSizeElem = 32;
+ final int growSizeElem = 2;
+ testFloatBufferImpl(initialSizeElem, growSizeElem);
+ }
+
+ @Test
+ public void test12FloatBuffer_I00_G32() {
+ final int initialSizeElem = 0;
+ final int growSizeElem = 32;
+ testFloatBufferImpl(initialSizeElem, growSizeElem);
+ }
+
+ private void testFloatBufferImpl(int initialSizeElem, int growSizeElem) {
final int compNum = 3;
/*value2*/FloatBuffer/*value2*/ fb0 =
/*value2*/FloatBuffer/*value2*/.allocate(3*compNum);
@@ -161,8 +198,6 @@ public void test02FloatBuffer() {
fb0.put(e2);
fb0.position(0);
- final int initialSizeElem = 32;
- final int growSizeElem = 2;
final int totalSizeElem = initialSizeElem+2*growSizeElem;
final int initialSizeComp = initialSizeElem*compNum;
@@ -178,18 +213,19 @@ public void test02FloatBuffer() {
for(int i=0; i<totalSizeElem; i++) {
if( 0 == i ) {
- Assert.assertTrue("Error "+fs0+", "+fb0, fb0.position() == 0);
+ Assert.assertTrue("Error #"+i+", "+fs0+", "+fb0, fb0.position() == 0);
} else if( 0 == i % 2) {
- Assert.assertTrue("Error "+fs0+", "+fb0, fb0.position() == 2*compNum);
+ Assert.assertTrue("Error #"+i+", "+fs0+", "+fb0, fb0.position() == 2*compNum);
fb0.position(0);
} else {
- Assert.assertTrue("Error "+fs0+", "+fb0, fb0.position() == compNum);
+ Assert.assertTrue("Error #"+i+", "+fs0+", "+fb0, fb0.position() == compNum);
}
if(i < initialSizeElem) {
- Assert.assertTrue("Error "+fs0, fs0.remaining() == (initialSizeElem-i)*compNum);
+ Assert.assertTrue("Error #"+i+", "+fs0, fs0.remaining() == (initialSizeElem-i)*compNum);
} else {
- int j = ( i - initialSizeElem ) % growSizeElem ;
- Assert.assertTrue("Error "+fs0, fs0.remaining() == j*compNum);
+ final int j = ( i - initialSizeElem ) % growSizeElem ;
+ final int k = ( 0 < j && j < growSizeElem ) ? growSizeElem - j : 0;
+ Assert.assertTrue("Error #"+i+"("+j+", "+k+"), "+fs0, fs0.remaining() == k*compNum);
}
Assert.assertTrue("Error "+fs0, fs0.position() == i*compNum);
Please sign in to comment.
Something went wrong with that request. Please try again.