db-derby-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kahat...@apache.org
Subject svn commit: r673057 - in /db/derby/code/trunk/java: engine/org/apache/derby/iapi/services/io/CompressedNumber.java testing/org/apache/derbyTesting/unitTests/junit/CompressedNumberTest.java
Date Tue, 01 Jul 2008 11:16:59 GMT
Author: kahatlen
Date: Tue Jul  1 04:16:59 2008
New Revision: 673057

URL: http://svn.apache.org/viewvc?rev=673057&view=rev
Log:
DERBY-3742: Move test code from CompressedNumber to a unit test

Added a JUnit test with the test code and removed it from CompressedNumber.

Contributed by Junjie Peng <pjj.ccce@gmail.com>.

Added:
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/unitTests/junit/CompressedNumberTest.java
  (with props)
Modified:
    db/derby/code/trunk/java/engine/org/apache/derby/iapi/services/io/CompressedNumber.java

Modified: db/derby/code/trunk/java/engine/org/apache/derby/iapi/services/io/CompressedNumber.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/iapi/services/io/CompressedNumber.java?rev=673057&r1=673056&r2=673057&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/iapi/services/io/CompressedNumber.java
(original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/iapi/services/io/CompressedNumber.java
Tue Jul  1 04:16:59 2008
@@ -674,280 +674,4 @@
 
 		return 8;
 	}
-
-// /* FOR TESTING
-// *****************************************************
-
-  	private static byte[] holder = new byte[8];
-	private static  ArrayOutputStream aos = new ArrayOutputStream(holder);
-	private static  DataOutput out = new DataOutputStream(aos);
-
-	private static  ArrayInputStream ais = new ArrayInputStream(holder);
-	private static  DataInput in = new DataInputStream(ais);
-	private static  InputStream in_stream = ais;
-
-
-    private static short checkInt(int i, short oldLength) throws IOException {
-
-		aos.setPosition(0);
-		int length = CompressedNumber.writeInt(out, i);
-		if (length != oldLength) {
-			System.out.println("changing length to " + length + " at value " + i + " 0x" + Integer.toHexString(i));
-
-			oldLength = (short) length;
-		}
-
-		int writtenBytes = aos.getPosition();
-		if (writtenBytes != length) {
-			System.out.println("MISMATCH written bytes  expected " + length + " got " + writtenBytes);
-			System.exit(1);
-		}
-
-		if (length != CompressedNumber.sizeInt(i)) {
-			System.out.println("MISMATCH sizeInt() bytes  expected " + length + " got " + CompressedNumber.sizeInt(i));
-			System.exit(1);
-		}
-
-		ais.setPosition(0);
-		int value = CompressedNumber.readInt(in);
-		if (value != i) {
-			System.out.println("MISMATCH value readInt(DataInput) expected " + i + " got " + value);
-			System.exit(1);
-		}
-
-		ais.setPosition(0);
-		value = ais.readCompressedInt();
-		if (value != i) {
-			System.out.println("MISMATCH value readInt(DataInput) expected " + i + " got " + value);
-			System.exit(1);
-		}
-
-		ais.setPosition(0);
-		value = CompressedNumber.readInt(in_stream);
-		if (value != i) {
-			System.out.println("MISMATCH value in readInt(InputStream) expected " + i + " got " +
value);
-			System.exit(1);
-		}
-
-
-		value = CompressedNumber.readInt(holder, 0);
-		if (value != i) {
-			System.out.println(
-                "MISMATCH frome readInt(byte[], offset) value expected " + 
-                i + " got " + value);
-			System.exit(1);
-		}
-
-		ais.setPosition(0);
-		int skipLength = CompressedNumber.skipInt(in);
-		if (skipLength != length) {
-			System.out.println("MISMATCH skip length expected " + length + " got " + skipLength);
-			System.exit(1);
-		}
-
-        int value_plus_int_length = readIntAndReturnIntPlusOverhead(holder, 0);
-        if (value_plus_int_length != (length + i + 1)) { 
-			System.out.println("MISMATCH readIntAndReturnIntPlusOverhead() return expected " + (length
+ i) + " got " + value_plus_int_length);
-			System.exit(1);
-        }
-
-		int skipPosition = ais.getPosition();
-		if (skipPosition != length) {
-			System.out.println("MISMATCH skip position expected " + length + " got " + skipPosition);
-			System.exit(1);
-		}
-
-		return oldLength;
-	}
-
-    private static short checkLong(long i, short oldLength) throws IOException {
-
-		aos.setPosition(0);
-		int length = CompressedNumber.writeLong(out, i);
-		if (length != oldLength) {
-			System.out.println("changing length to " + length + " at value " + i + " 0x" + Long.toHexString(i));
-			oldLength = (short) length;
-		}
-
-		int writtenBytes = aos.getPosition();
-		if (writtenBytes != length) {
-			System.out.println("MISMATCH written bytes  expected " + length + " got " + writtenBytes);
-			System.exit(1);
-		}
-
-		if (length != CompressedNumber.sizeLong(i)) {
-			System.out.println("MISMATCH sizeLong() bytes  expected " + length + " got " + CompressedNumber.sizeLong(i));
-			System.exit(1);
-		}
-
-		long value = CompressedNumber.readLong(holder, 0);
-		if (value != i) {
-			for (int j = 0; j < 8; j++) {
-
-				System.out.println(Integer.toHexString((int) holder[j]));
-			}
-
-			System.out.println(
-                "MISMATCH in readLong(byte[], offset) value expected " + 
-                Long.toHexString(i) + " got " + value);
-			System.exit(1);
-		}
-
-		ais.setPosition(0);
-		value = CompressedNumber.readLong(in_stream);
-		if (value != i) {
-			for (int j = 0; j < 8; j++) {
-
-				System.out.println(Integer.toHexString((int) holder[j]));
-			}
-			System.out.println("MISMATCH value in readLong(InputStream) expected " + Long.toHexString(i)
+ " got " + value);
-			System.exit(1);
-		}
-
-		ais.setPosition(0);
-		value = ais.readCompressedLong();
-		if (value != i) {
-			for (int j = 0; j < 8; j++) {
-
-				System.out.println(Integer.toHexString((int) holder[j]));
-			}
-			System.out.println("MISMATCH value in readLong(InputStream) expected " + Long.toHexString(i)
+ " got " + value);
-			System.exit(1);
-		}
-
-
-		ais.setPosition(0);
-		value = CompressedNumber.readLong(in);
-		if (value != i) {
-			for (int j = 0; j < 8; j++) {
-
-				System.out.println(Integer.toHexString((int) holder[j]));
-			}
-			System.out.println("MISMATCH value in readLong(DataInput) expected " + Long.toHexString(i)
+ " got " + value);
-			System.exit(1);
-		}
-
-		ais.setPosition(0);
-		int skipLength = CompressedNumber.skipLong(in);
-		if (skipLength != length) {
-			System.out.println("MISMATCH skip length expected " + length + " got " + skipLength);
-			System.exit(1);
-		}
-
-		int skipPosition = ais.getPosition();
-		if (skipPosition != length) {
-			System.out.println("MISMATCH skip position expected " + length + " got " + skipPosition);
-			System.exit(1);
-		}
-
-		return oldLength;
-	}
-
-	public static void main(String[] args) throws IOException {
-
-		short oldLength = -1;
-
-		System.out.println("** Testing Int");
-
-		oldLength = checkInt(0, oldLength);
-		oldLength = checkInt(1, oldLength);
-		oldLength = checkInt(2, oldLength);
-
-		oldLength = checkInt(0x3f - 4, oldLength);
-		oldLength = checkInt(0x3f - 3, oldLength);
-		oldLength = checkInt(0x3f - 2, oldLength);
-		oldLength = checkInt(0x3f - 1, oldLength);
-		oldLength = checkInt(0x3f    , oldLength);
-		oldLength = checkInt(0x3f + 1, oldLength);
-		oldLength = checkInt(0x3f + 2, oldLength);
-		oldLength = checkInt(0x3f + 3, oldLength);
-		oldLength = checkInt(0x3f + 4, oldLength);
-
-		oldLength = checkInt(0x3f80 - 4, oldLength);
-		oldLength = checkInt(0x3f80 - 3, oldLength);
-		oldLength = checkInt(0x3f80 - 2, oldLength);
-		oldLength = checkInt(0x3f80 - 1, oldLength);
-		oldLength = checkInt(0x3f80    , oldLength);
-		oldLength = checkInt(0x3f80 + 1, oldLength);
-		oldLength = checkInt(0x3f80 + 2, oldLength);
-		oldLength = checkInt(0x3f80 + 3, oldLength);
-		oldLength = checkInt(0x3f80 + 4, oldLength);
-
-		oldLength = checkInt(0x3fff - 4, oldLength);
-		oldLength = checkInt(0x3fff - 3, oldLength);
-		oldLength = checkInt(0x3fff - 2, oldLength);
-		oldLength = checkInt(0x3fff - 1, oldLength);
-		oldLength = checkInt(0x3fff    , oldLength);
-		oldLength = checkInt(0x3fff + 1, oldLength);
-		oldLength = checkInt(0x3fff + 2, oldLength);
-		oldLength = checkInt(0x3fff + 3, oldLength);
-		oldLength = checkInt(0x3fff + 4, oldLength);
-
-		oldLength = checkInt(Integer.MAX_VALUE - 4, oldLength);
-		oldLength = checkInt(Integer.MAX_VALUE - 3, oldLength);
-		oldLength = checkInt(Integer.MAX_VALUE - 2, oldLength);
-		oldLength = checkInt(Integer.MAX_VALUE - 1, oldLength);
-		oldLength = checkInt(Integer.MAX_VALUE    , oldLength);
-
-        oldLength = -1;
-        for (int i = 0; i < 0xf0000; i++)
-        {
-            oldLength = checkInt(i, oldLength);
-        }
-
-        // takes 30 minutes to run.
-        //
-        // for (int i = 0; i < Integer.MAX_VALUE; i++)
-        // {
-        // if (i % 0x00800000 == 0)
-        // System.out.println("checking: " + i);
-        //
-        // oldLength = checkInt(i, oldLength);
-        // }
-
-
-		System.out.println("** Testing Long");
-
-        oldLength = -1;
-        for (int i = 0; i < 0xf0000; i++)
-        {
-            oldLength = checkLong(i, oldLength);
-        }
-	
-		oldLength = -1;
-		
-		oldLength = checkLong(0, oldLength);
-		oldLength = checkLong(1, oldLength);
-		oldLength = checkLong(2, oldLength);
-
-		oldLength = checkLong(0x3fff - 2, oldLength);
-		oldLength = checkLong(0x3fff - 1, oldLength);
-		oldLength = checkLong(0x3fff    , oldLength);
-		oldLength = checkLong(0x3fff + 1, oldLength);
-		oldLength = checkLong(0x3fff + 2, oldLength);
-
-		oldLength = checkLong(0x3fffffff - 4, oldLength);
-		oldLength = checkLong(0x3fffffff - 3, oldLength);
-		oldLength = checkLong(0x3fffffff - 2, oldLength);
-		oldLength = checkLong(0x3fffffff - 1, oldLength);
-		oldLength = checkLong(0x3fffffff    , oldLength);
-		oldLength = checkLong(0x3fffffff + 1, oldLength);
-		oldLength = checkLong(0x3fffffff + 2, oldLength);
-		oldLength = checkLong(0x3fffffff + 3, oldLength);
-		oldLength = checkLong(0x3fffffff + 4, oldLength);
-
-		oldLength = checkLong(0x70000000 - 2, oldLength);
-		oldLength = checkLong(0x70000000 - 1, oldLength);
-		oldLength = checkLong(0x70000000    , oldLength);
-		oldLength = checkLong(0x70000000 + 1, oldLength);
-		oldLength = checkLong(0x70000000 + 2, oldLength);
-
-
-		oldLength = checkLong(Long.MAX_VALUE - 2, oldLength);
-		oldLength = checkLong(Long.MAX_VALUE - 1, oldLength);
-		oldLength = checkLong(Long.MAX_VALUE    , oldLength);
-
-
-	}
-// ********************************************************/
 }

Added: db/derby/code/trunk/java/testing/org/apache/derbyTesting/unitTests/junit/CompressedNumberTest.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/unitTests/junit/CompressedNumberTest.java?rev=673057&view=auto
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/unitTests/junit/CompressedNumberTest.java
(added)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/unitTests/junit/CompressedNumberTest.java
Tue Jul  1 04:16:59 2008
@@ -0,0 +1,197 @@
+/*
+
+   Derby - Class org.apache.derbyTesting.unitTests.CompressedNumberTest
+
+   Licensed to the Apache Software Foundation (ASF) under one or more
+   contributor license agreements.  See the NOTICE file distributed with
+   this work for additional information regarding copyright ownership.
+   The ASF licenses this file to you under the Apache License, Version 2.0
+   (the "License"); you may not use this file except in compliance with
+   the License.  You may obtain a copy of the License at
+
+      http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+
+ */
+
+package org.apache.derbyTesting.unitTests.junit;
+
+import java.io.DataInput;
+import java.io.DataInputStream;
+import java.io.DataOutput;
+import java.io.DataOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+
+import org.apache.derby.iapi.services.io.ArrayInputStream;
+import org.apache.derby.iapi.services.io.ArrayOutputStream;
+import org.apache.derby.iapi.services.io.CompressedNumber;
+
+import junit.framework.TestCase;
+
+/**
+ * Test case for CompressedNumber.
+ */
+public class CompressedNumberTest extends TestCase {
+    private static byte[] holder = new byte[8];
+    private static  ArrayOutputStream aos = new ArrayOutputStream(holder);
+    private static  DataOutput out = new DataOutputStream(aos);
+
+    private static  ArrayInputStream ais = new ArrayInputStream(holder);
+    private static  DataInput in = new DataInputStream(ais);
+    private static  InputStream in_stream = ais;
+
+    /**
+     * Public constructor required for running test as stand alone JUnit.
+     *
+     * @param name
+     *            name to present this test case.
+     */
+    public CompressedNumberTest(String name) {
+        super(name);
+    }
+
+    public void testLong() throws IOException{
+        long[] dataToTest = {0, 1, 2,
+                0x3fff - 2, 0x3fff - 1,
+                0x3fff,
+                0x3fff + 1, 0x3fff + 2,
+                0x3fffffff - 4, 0x3fffffff - 3, 0x3fffffff - 2, 0x3fffffff - 1,
+                0x3fffffff,
+                0x3fffffff + 1, 0x3fffffff + 2, 0x3fffffff + 3, 0x3fffffff + 4,
+                0x70000000 - 2, 0x70000000 - 1,
+                0x70000000,
+                0x70000000 + 1, 0x70000000 + 2,
+                Long.MAX_VALUE - 2, Long.MAX_VALUE - 1,
+                Long.MAX_VALUE,
+                };
+
+        for(int i = 0; i < dataToTest.length; i++){
+            checkLong(dataToTest[i]);
+        }
+    }
+
+    public void testLongWidely() throws IOException{
+        for (int i = 0; i < 0xf0000; i++){
+            checkLong(i);
+        }
+    }
+
+    public void testInt() throws IOException{
+        int[] dataToTest = {0, 1, 2,
+                0x3f - 4, 0x3f - 3, 0x3f - 2, 0x3f - 1,
+                0x3f,
+                0x3f + 1, 0x3f + 2, 0x3f + 3, 0x3f + 4,
+                0x3f80 - 4, 0x3f80 - 3, 0x3f80 - 2, 0x3f80 - 1,
+                0x3f80,
+                0x3f80 + 1, 0x3f80 + 2, 0x3f80 + 3, 0x3f80 + 4,
+                0x3fff - 4, 0x3fff - 3, 0x3fff - 2, 0x3fff - 1,
+                0x3fff,
+                0x3fff + 1, 0x3fff + 2, 0x3fff + 3, 0x3fff + 4,
+                Integer.MAX_VALUE - 4, Integer.MAX_VALUE - 3,
+                Integer.MAX_VALUE - 2, Integer.MAX_VALUE - 1,
+                Integer.MAX_VALUE,
+                };
+
+        for(int i = 0; i < dataToTest.length; i++){
+            checkInt(dataToTest[i]);
+        }
+    }
+
+    public void testIntWidely() throws IOException{
+        for (int i = 0; i < 0xf0000; i++){
+             checkInt(i);
+        }
+
+         //takes 30 minutes to run.
+//         for (int i = 0; i < Integer.MAX_VALUE; i++) {
+//             checkInt(i);
+//         }
+    }
+
+    /**
+     * Check whether CompressedNumber can work well on integer passed.
+     *
+     * @param i
+     *            the integer to be checked.
+     * @throws IOException
+     */
+    private void checkInt(int i) throws IOException {
+        aos.setPosition(0);
+        int length = CompressedNumber.writeInt(out, i);
+
+        assertEquals("MISMATCH written bytes", length, aos.getPosition());
+
+        assertEquals("MISMATCH sizeInt() bytes", length,
+                     CompressedNumber.sizeInt(i));
+
+        ais.setPosition(0);
+        assertEquals("MISMATCH value readInt(DataInput)", i,
+                     CompressedNumber.readInt(in));
+
+        ais.setPosition(0);
+        assertEquals("MISMATCH value readInt(DataInput)", i,
+                     ais.readCompressedInt());
+
+        ais.setPosition(0);
+        assertEquals("MISMATCH value in readInt(InputStream)", i,
+                     CompressedNumber.readInt(in_stream));
+
+        assertEquals("MISMATCH frome readInt(byte[], offset)", i,
+                     CompressedNumber.readInt(holder, 0));
+
+        ais.setPosition(0);
+        assertEquals("MISMATCH skip length", length,
+                     CompressedNumber.skipInt(in));
+
+        assertEquals("MISMATCH readIntAndReturnIntPlusOverhead() return",
+                     length + i + 1,
+                CompressedNumber.readIntAndReturnIntPlusOverhead(holder, 0));
+
+        assertEquals("MISMATCH skip position", length, ais.getPosition());
+    }
+
+
+    /**
+     * Check whether CompressedNumber can work well on long number passed.
+     *
+     * @param l
+     *            the long number to be checked.
+     * @throws IOException
+     */
+    private void checkLong(long l) throws IOException {
+        aos.setPosition(0);
+        int length = CompressedNumber.writeLong(out, l);
+
+        assertEquals("MISMATCH written bytes", length, aos.getPosition());
+
+        assertEquals("MISMATCH sizeLong() bytes", length,
+                     CompressedNumber.sizeLong(l));
+
+        assertEquals("MISMATCH in readLong(byte[], offset) value", l,
+                     CompressedNumber.readLong(holder, 0));
+
+        ais.setPosition(0);
+        assertEquals("MISMATCH value in readLong(InputStream)", l,
+                     CompressedNumber.readLong(in_stream));
+
+        ais.setPosition(0);
+        assertEquals("MISMATCH value in readLong(InputStream)", l, ais
+                .readCompressedLong());
+
+        ais.setPosition(0);
+        assertEquals("MISMATCH value in readLong(DataInput)", l,
+                     CompressedNumber.readLong(in));
+
+        ais.setPosition(0);
+        assertEquals("MISMATCH skip length", length,
+                     CompressedNumber.skipLong(in));
+
+        assertEquals("MISMATCH skip position", length, ais.getPosition());
+    }
+}

Propchange: db/derby/code/trunk/java/testing/org/apache/derbyTesting/unitTests/junit/CompressedNumberTest.java
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message