You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by we...@apache.org on 2004/01/28 22:16:36 UTC

svn commit: rev 6339 - incubator/directory/snickers/trunk/ber/src/java/org/apache/snickers/test

Author: wesmckean
Date: Wed Jan 28 13:16:36 2004
New Revision: 6339

Added:
   incubator/directory/snickers/trunk/ber/src/java/org/apache/snickers/test/
   incubator/directory/snickers/trunk/ber/src/java/org/apache/snickers/test/TestBitString.java
Log:
initial load of test package and TestBitString class

Added: incubator/directory/snickers/trunk/ber/src/java/org/apache/snickers/test/TestBitString.java
==============================================================================
--- (empty file)
+++ incubator/directory/snickers/trunk/ber/src/java/org/apache/snickers/test/TestBitString.java	Wed Jan 28 13:16:36 2004
@@ -0,0 +1,233 @@
+/*
+
+ ============================================================================
+                   The Apache Software License, Version 1.1
+ ============================================================================
+
+ Copyright (C) 1999-2002 The Apache Software Foundation. All rights reserved.
+
+ Redistribution and use in source and binary forms, with or without modifica-
+ tion, are permitted provided that the following conditions are met:
+
+ 1. Redistributions of  source code must  retain the above copyright  notice,
+    this list of conditions and the following disclaimer.
+
+ 2. Redistributions in binary form must reproduce the above copyright notice,
+    this list of conditions and the following disclaimer in the documentation
+    and/or other materials provided with the distribution.
+
+ 3. The end-user documentation included with the redistribution, if any, must
+    include  the following  acknowledgment:  "This product includes  software
+    developed  by the  Apache Software Foundation  (http://www.apache.org/)."
+    Alternately, this  acknowledgment may  appear in the software itself,  if
+    and wherever such third-party acknowledgments normally appear.
+
+ 4. The names "Eve Directory Server", "Apache Directory Project", "Apache Eve" 
+    and "Apache Software Foundation"  must not be used to endorse or promote
+    products derived  from this  software without  prior written
+    permission. For written permission, please contact apache@apache.org.
+
+ 5. Products  derived from this software may not  be called "Apache", nor may
+    "Apache" appear  in their name,  without prior written permission  of the
+    Apache Software Foundation.
+
+ THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
+ INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
+ FITNESS  FOR A PARTICULAR  PURPOSE ARE  DISCLAIMED.  IN NO  EVENT SHALL  THE
+ APACHE SOFTWARE  FOUNDATION  OR ITS CONTRIBUTORS  BE LIABLE FOR  ANY DIRECT,
+ INDIRECT, INCIDENTAL, SPECIAL,  EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLU-
+ DING, BUT NOT LIMITED TO, PROCUREMENT  OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ OF USE, DATA, OR  PROFITS; OR BUSINESS  INTERRUPTION)  HOWEVER CAUSED AND ON
+ ANY  THEORY OF LIABILITY,  WHETHER  IN CONTRACT,  STRICT LIABILITY,  OR TORT
+ (INCLUDING  NEGLIGENCE OR  OTHERWISE) ARISING IN  ANY WAY OUT OF THE  USE OF
+ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+ This software  consists of voluntary contributions made  by many individuals
+ on  behalf of the Apache Software  Foundation. For more  information on the
+ Apache Software Foundation, please see <http://www.apache.org/>.
+
+*/
+
+package org.apache.snickers.test;
+
+import junit.framework.TestCase;
+import org.apache.commons.codec.binary.Binary;
+import org.apache.snickers.asn.BitString;
+
+/**
+ * @author Wes McKean
+ *
+ * Test the BitString class
+ */
+public class TestBitString extends TestCase {
+
+    String binary[] = {
+        "00000001",
+        "00000010",
+        "00000100",
+        "00001000",
+        "00010000",
+        "00100000",
+        "01000000",
+        "10000000"
+    };
+    
+	/**
+	 * @param name the test to run
+	 */
+	public TestBitString(String name) {
+		super(name);
+	}
+
+    /**
+     * Run a simple test of the bits in <code>Binary</code>
+     * 
+     * @throws Exception if an error occurs
+     */
+    public void testSimpleBinary() throws Exception {
+        int vals[] = {
+            Binary.BIT_0,
+            Binary.BIT_1,
+            Binary.BIT_2,
+            Binary.BIT_3,
+            Binary.BIT_4,
+            Binary.BIT_5,
+            Binary.BIT_6,
+            Binary.BIT_7
+        };
+        
+        for( int i = 0; i < vals.length; i++ ) {
+            int[] tmp = new int[1];
+            tmp[0] = vals[i];
+            BitString bs = new BitString( tmp,8 );
+            
+            assertTrue( "testSimpleBinary:" + binary[i], bs.toString().equals(binary[i]));
+        }
+    }
+    
+    /**
+     * A test for more complicated bit patterns.  Nothing fancy yet.
+     * 
+     * @throws Exception
+     */
+    public void testComplicated() throws Exception {
+        int []tmp;
+        
+        // First test.  1 int with various bits set
+        tmp = new int[1];
+        tmp[0] = (( Binary.BIT_7 | Binary.BIT_3 ) << 8 ) | (Binary.BIT_1 | Binary.BIT_2 | Binary.BIT_3 );
+        
+        BitString bs = new BitString( tmp, 16 ); 
+        //System.out.println( "\n" + bs.toString() );
+        assertTrue( "testComplicated:1", bs.toString().equals("1000100000001110") );
+        
+        // Lets do a 24 bit bit string. 
+        tmp[0] = (( Binary.BIT_7 | Binary.BIT_3 ) << 16 ) | ((Binary.BIT_1 | Binary.BIT_2 | Binary.BIT_3 )<<8) |
+                    (Binary.BIT_6 | Binary.BIT_0);
+        bs = new BitString( tmp, 24 ); 
+        //System.out.println( "\n" + bs.toString() );
+        assertTrue( "testComplicated:2", bs.toString().equals("100010000000111001000001") );
+        
+        // Lets do a 32 bit string
+        tmp[0] = (( Binary.BIT_7 | Binary.BIT_3 ) << 24 ) | ((Binary.BIT_1 | Binary.BIT_2 | Binary.BIT_3 )<<16) |
+                    ((Binary.BIT_6 | Binary.BIT_0) << 8 ) | (Binary.BIT_5 | Binary.BIT_0 | Binary.BIT_1 | Binary.BIT_3 );
+        bs = new BitString( tmp, 32 );
+        //System.out.println( "\n" + bs.toString() );
+        assertTrue( "testComplicated:3", bs.toString().equals("10001000000011100100000100101011") );
+        
+        // Now for the real test.  2 ints (64 bit strings)
+        tmp = new int[2];        
+        tmp[0] = (( Binary.BIT_7 | Binary.BIT_3 ) << 24 ) | ((Binary.BIT_1 | Binary.BIT_2 | Binary.BIT_3 )<<16) |
+                    ((Binary.BIT_6 | Binary.BIT_0) << 8 ) | (Binary.BIT_5 | Binary.BIT_0 | Binary.BIT_1 | Binary.BIT_3 );
+                    
+        tmp[1] = (( Binary.BIT_7 | Binary.BIT_3 ) << 8 ) | (Binary.BIT_1 | Binary.BIT_2 | Binary.BIT_3 );
+        bs = new BitString( tmp,48 );
+        //System.out.println( "\n" + bs.toString() );
+        assertTrue( "testComplicated:4", bs.toString().equals("100010000000111010001000000011100100000100101011") );
+    }
+
+    /**
+     *  Execute some simple 32-bit tests
+     */    
+    public void testAnd() throws Exception {
+        // First test.  1 int with various bits set
+        int[] tmp = new int[1];
+        tmp[0] = (( Binary.BIT_7 | Binary.BIT_3 ) << 8 ) | (Binary.BIT_1 | Binary.BIT_2 | Binary.BIT_3 );
+        
+        int[] tmp2 = new int[1];
+        tmp2[0] = (( Binary.BIT_7 ) << 8 ) | (Binary.BIT_1 | Binary.BIT_2 | Binary.BIT_5 );
+        
+        BitString bs1 = new BitString( tmp, 16 );
+        BitString bs2 = new BitString( tmp2, 16 );
+        BitString delta = bs1.and(bs2);
+        
+        //System.out.println( "\n" + bs1.toString() );
+        //System.out.println( bs2.toString() );
+        //System.out.println( delta.toString() );
+        
+        //     1000100000001110
+        // AND 1000000000000110
+        //     ----------------
+        //     1000000000000110        
+        
+        assertTrue( "testAnd", delta.toString().equals("1000000000000110"));
+    }
+
+    /**
+     *  Execute some simple 32-bit tests
+     */    
+    public void testOr() throws Exception {
+        // First test.  1 int with various bits set
+        int[] tmp = new int[1];
+        tmp[0] = (( Binary.BIT_7 | Binary.BIT_3 ) << 8 ) | (Binary.BIT_1 | Binary.BIT_2 | Binary.BIT_3 );
+        
+        int[] tmp2 = new int[1];
+        tmp2[0] = (( Binary.BIT_7 ) << 8 ) | (Binary.BIT_1 | Binary.BIT_2 | Binary.BIT_5 );
+        
+        BitString bs1 = new BitString( tmp, 16 );
+        BitString bs2 = new BitString( tmp2, 16 );
+        BitString delta = bs1.or(bs2);
+        
+        //System.out.println( "\n" + bs1.toString() );
+        //System.out.println( bs2.toString() );
+        //System.out.println( delta.toString() );
+        
+        //     1000100000001110
+        //  OR 1000000000100110
+        //     ----------------
+        //     1000100000101110        
+        
+        assertTrue( "testOr", delta.toString().equals("1000100000101110"));
+    }
+
+    /**
+     *  Execute some simple 32-bit tests
+     */    
+    public void testXor() throws Exception {
+        // First test.  1 int with various bits set
+        int[] tmp = new int[1];
+        tmp[0] = (( Binary.BIT_7 | Binary.BIT_3 ) << 8 ) | (Binary.BIT_1 | Binary.BIT_2 | Binary.BIT_3 );
+        
+        int[] tmp2 = new int[1];
+        tmp2[0] = (( Binary.BIT_7 ) << 8 ) | (Binary.BIT_1 | Binary.BIT_2 | Binary.BIT_5 );
+        
+        BitString bs1 = new BitString( tmp, 16 );
+        BitString bs2 = new BitString( tmp2, 16 );
+        BitString delta = bs1.xor(bs2);
+        
+        //System.out.println( "\n" + bs1.toString() );
+        //System.out.println( bs2.toString() );
+        //System.out.println( delta.toString() );
+        
+        //     1000100000001110
+        // XOR 1000000000100110
+        //     ----------------
+        //     0000100000101000        
+        
+        assertTrue( "testXor", delta.toString().equals("0000100000101000"));
+    }
+    
+	public static void main(String[] args) {
+        junit.textui.TestRunner.run( TestBitString.class );
+	}
+}