Added: directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/digester/TagTreeTest.java
URL: http://svn.apache.org/viewcvs/directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/digester/TagTreeTest.java?rev=234226&view=auto
==============================================================================
--- directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/digester/TagTreeTest.java (added)
+++ directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/digester/TagTreeTest.java Sun Aug 21 08:03:46 2005
@@ -0,0 +1,771 @@
+/*
+ * Copyright 2004 The Apache Software Foundation
+ *
+ * Licensed 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.asn1.ber.digester ;
+
+
+import org.apache.commons.collections.primitives.IntStack;
+import org.apache.commons.test.PrivateTestCase;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Stack;
+
+
+/**
+ * Tests the TagTree class.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">
+ * Apache Directory Project</a>
+ * @version $Rev: 158144 $
+ */
+public class TagTreeTest extends PrivateTestCase
+{
+ /**
+ * Constructor for TagTreeTest.
+ * @param name the name of the test case
+ */
+ public TagTreeTest( String name )
+ {
+ super( name ) ;
+ }
+
+
+ /**
+ * Accesses the private wildNodes member to extract the TagNode
+ * with the int tag argument as a value.
+ *
+ * @param tree the tree to access
+ * @param tag the tag value to get the TagNode for
+ * @return the tag node accessed or null
+ */
+ private TagNode getWildNode( TagTree tree, int tag )
+ {
+ HashMap nodes = ( HashMap ) getMember( "wildNodes", tree ) ;
+ return ( TagNode ) nodes.get( new Integer( tag ) ) ;
+ }
+
+
+ /**
+ * Accesses the private normNodes member to extract the TagNode
+ * with the int tag argument as a value.
+ *
+ * @param tree the tree to access
+ * @param tag the tag value to get the TagNode for
+ * @return the tag node accessed or null
+ */
+ private TagNode getNormalNode( TagTree tree, int tag )
+ {
+ HashMap nodes = ( HashMap ) getMember( "normNodes", tree ) ;
+ return ( TagNode ) nodes.get( new Integer( tag ) ) ;
+ }
+
+
+ /**
+ * Used to white box test the private isTailMatch() method of the TagTree.
+ *
+ * @param tree the tree instance to test
+ * @param pattern the pattern arg
+ * @param stack the stack arg
+ * @return the resultant true or false return value
+ */
+ private boolean isTailMatch( TagTree tree, int[] pattern, Stack stack )
+ {
+ Class[] argClasses = { pattern.getClass(), Stack.class } ;
+ Object[] args = { pattern, stack } ;
+ Object result = invoke( tree, TagTree.class, "isTailMatch", argClasses,
+ args ) ;
+ return ( ( Boolean ) result ).booleanValue() ;
+ }
+
+
+ /**
+ * Used to white box test the private isReverseTailMatch() method of the
+ * TagTree.
+ *
+ * @param tree the tree instance to test
+ * @param pattern the pattern arg
+ * @param stack the stack arg
+ * @return the resultant true or false return value
+ */
+ private boolean isReverseTailMatch( TagTree tree, int[] pattern,
+ Stack stack )
+ {
+ Class[] argClasses = { pattern.getClass(), Stack.class } ;
+ Object[] args = { pattern, stack } ;
+ Object result = invoke( tree, TagTree.class, "isReverseTailMatch",
+ argClasses, args ) ;
+ return ( ( Boolean ) result ).booleanValue() ;
+ }
+
+
+ /**
+ * Tests the private isTailMatch() method.
+ */
+ public void testIsTailMatch()
+ {
+ TagTree tree = new TagTree() ;
+ int[] pattern = {TagTree.WILDCARD,1,2,3} ;
+ Stack stack = new Stack() ;
+ assertFalse( isTailMatch( tree, pattern, stack ) ) ;
+ stack.push( new Integer( 5 ) ) ;
+ assertFalse( isTailMatch( tree, pattern, stack ) ) ;
+ stack.push( new Integer( 6 ) ) ;
+ assertFalse( isTailMatch( tree, pattern, stack ) ) ;
+ stack.push( new Integer( 1 ) ) ;
+ assertFalse( isTailMatch( tree, pattern, stack ) ) ;
+ stack.push( new Integer( 2 ) ) ;
+ assertFalse( isTailMatch( tree, pattern, stack ) ) ;
+ stack.push( new Integer( 3 ) ) ;
+ assertTrue( isTailMatch( tree, pattern, stack ) ) ;
+
+ tree = new TagTree() ;
+ pattern = new int[]{TagTree.WILDCARD} ;
+ stack = new Stack() ;
+ assertTrue( isTailMatch( tree, pattern, stack ) ) ;
+
+ tree = new TagTree() ;
+ pattern = new int[]{} ;
+ stack = new Stack() ;
+ assertTrue( isTailMatch( tree, pattern, stack ) ) ;
+
+ tree = new TagTree() ;
+ pattern = new int[]{TagTree.WILDCARD, 1} ;
+ stack = new Stack() ;
+ assertFalse( isTailMatch( tree, pattern, stack ) ) ;
+ stack.push( new Integer( 2 ) ) ;
+ assertFalse( isTailMatch( tree, pattern, stack ) ) ;
+ stack.push( new Integer( 1 ) ) ;
+ assertTrue( isTailMatch( tree, pattern, stack ) ) ;
+ stack.push( new Integer( 1 ) ) ;
+ assertTrue( isTailMatch( tree, pattern, stack ) ) ;
+ stack.push( new Integer( 2 ) ) ;
+ assertFalse( isTailMatch( tree, pattern, stack ) ) ;
+ }
+
+
+ /**
+ * Tests the private isTailMatch() method.
+ */
+ public void testIsReverseTailMatch()
+ {
+ TagTree tree = new TagTree() ;
+ int[] pattern = {TagTree.WILDCARD,1,2,3} ;
+ Stack stack = new Stack() ;
+ stack.push( new Integer( 3 ) ) ;
+ assertFalse( isReverseTailMatch( tree, pattern, stack ) ) ;
+ stack.push( new Integer( 2 ) ) ;
+ assertFalse( isReverseTailMatch( tree, pattern, stack ) ) ;
+ stack.push( new Integer( 1 ) ) ;
+ assertTrue( isReverseTailMatch( tree, pattern, stack ) ) ;
+
+ tree = new TagTree() ;
+ pattern = new int[]{TagTree.WILDCARD} ;
+ stack = new Stack() ;
+ assertTrue( isReverseTailMatch( tree, pattern, stack ) ) ;
+
+ tree = new TagTree() ;
+ pattern = new int[]{} ;
+ stack = new Stack() ;
+ assertTrue( isReverseTailMatch( tree, pattern, stack ) ) ;
+
+ tree = new TagTree() ;
+ pattern = new int[]{TagTree.WILDCARD, 1, 1} ;
+ stack = new Stack() ;
+ assertFalse( isReverseTailMatch( tree, pattern, stack ) ) ;
+ stack.push( new Integer( 1 ) ) ;
+ assertFalse( isReverseTailMatch( tree, pattern, stack ) ) ;
+ stack.push( new Integer( 1 ) ) ;
+ assertTrue( isReverseTailMatch( tree, pattern, stack ) ) ;
+ stack.push( new Integer( 2 ) ) ;
+ assertTrue( isReverseTailMatch( tree, pattern, stack ) ) ;
+ stack.push( new Integer( 3 ) ) ;
+ assertTrue( isReverseTailMatch( tree, pattern, stack ) ) ;
+
+ tree = new TagTree() ;
+ pattern = new int[]{TagTree.WILDCARD, 1, 2} ;
+ stack = new Stack() ;
+ assertFalse( isReverseTailMatch( tree, pattern, stack ) ) ;
+ stack.push( new Integer( 1 ) ) ;
+ assertFalse( isReverseTailMatch( tree, pattern, stack ) ) ;
+ stack.push( new Integer( 1 ) ) ;
+ assertFalse( isReverseTailMatch( tree, pattern, stack ) ) ;
+ stack.push( new Integer( 2 ) ) ;
+ assertFalse( isReverseTailMatch( tree, pattern, stack ) ) ;
+ }
+
+
+ /**
+ * Used to white box test the private addWildRuleToNormalTree() method of
+ * the TagTree class.
+ *
+ * @param tree the tree instance to test
+ * @param pattern the 1st int[] pattern arg
+ * @param rule the 2nd Rule arg
+ * @param stack the 3rd Stack arg
+ * @param node the last TagNode arg
+ */
+ public void addWildRuleToNormalTree( TagTree tree, int[] pattern,
+ Rule rule, Stack stack, TagNode node )
+ {
+ Class[] argClasses = {
+ pattern.getClass(), Rule.class, Stack.class, TagNode.class
+ } ;
+
+ Object[] args = { pattern, rule, stack, node } ;
+ invoke( tree, TagTree.class, "addWildRuleToNormalTree",
+ argClasses, args ) ;
+ }
+
+
+ /**
+ * Tests various combinations of wild card patterns and trees to make sure
+ * all is working with addWildRuleToNormalTree().
+ */
+ public void testAddWildRuleToNormalTree2()
+ {
+ TagTree tree = new TagTree() ;
+ int[] pattern = {1,2,3} ;
+ Rule r0 = new MockRule() ;
+ tree.addRule( pattern, r0 ) ;
+
+ // Walk the branch of tag nodes and validate contents along the way
+ TagNode node = getNormalNode( tree, 1 ) ;
+ assertEquals( 0, node.getRules().size() ) ;
+ node = node.getChild( new Integer( 2 ) ) ;
+ assertEquals( 0, node.getRules().size() ) ;
+ node = node.getChild( new Integer( 3 ) ) ;
+ assertEquals( 1, node.getRules().size() ) ;
+ assertEquals( r0, node.getRules().get( 0 ) ) ;
+
+ // setup the first wild card addition but it should not match
+ int[] wildpat = { TagTree.WILDCARD,1,2} ;
+ Rule r1 = new MockRule() ;
+ addWildRuleToNormalTree( tree, wildpat, r1, new Stack(), node ) ;
+
+ // the pattern *,1,2 should NOT match 1,2,3
+ // walk of the branch of tag nodes and validate contents along the way
+ node = getNormalNode( tree, 1 ) ;
+ assertEquals( 0, node.getRules().size() ) ;
+ node = node.getChild( new Integer( 2 ) ) ;
+ assertEquals( 0, node.getRules().size() ) ;
+ node = node.getChild( new Integer( 3 ) ) ;
+ assertEquals( 1, node.getRules().size() ) ;
+ assertEquals( r0, node.getRules().get( 0 ) ) ;
+
+ // now try a matching the pattern with *,1,2,3 so r1 should be present
+ wildpat = new int[]{TagTree.WILDCARD, 1, 2, 3} ;
+ r1 = new MockRule() ;
+ node = getNormalNode( tree, 1 ) ;
+ addWildRuleToNormalTree( tree, wildpat, r1, new Stack(), node ) ;
+
+ // the pattern *,1,2,3 should match 1,2,3 now
+ // walk of the branch of tag nodes and validate contents along the way
+ assertEquals( 0, node.getRules().size() ) ;
+ node = node.getChild( new Integer( 2 ) ) ;
+ assertEquals( 0, node.getRules().size() ) ;
+ node = node.getChild( new Integer( 3 ) ) ;
+ assertEquals( 2, node.getRules().size() ) ;
+ assertEquals( r0, node.getRules().get( 0 ) ) ;
+ assertEquals( r1, node.getRules().get( 1 ) ) ;
+
+ // now try a less specific matching the pattern *,2,3
+ wildpat = new int[]{TagTree.WILDCARD, 2, 3} ;
+ Rule r2 = new MockRule() ;
+ node = getNormalNode( tree, 1 ) ;
+ addWildRuleToNormalTree( tree, wildpat, r2, new Stack(), node ) ;
+
+ // the pattern *,2,3 should match 1,2,3 now
+ // walk of the branch of tag nodes and validate contents along the way
+ assertEquals( 0, node.getRules().size() ) ;
+ node = node.getChild( new Integer( 2 ) ) ;
+ assertEquals( 0, node.getRules().size() ) ;
+ node = node.getChild( new Integer( 3 ) ) ;
+ assertEquals( 3, node.getRules().size() ) ;
+ assertEquals( r0, node.getRules().get( 0 ) ) ;
+ assertEquals( r1, node.getRules().get( 1 ) ) ;
+ assertEquals( r2, node.getRules().get( 2 ) ) ;
+
+ // now try least specific matching using pattern *,3
+ wildpat = new int[]{TagTree.WILDCARD, 3} ;
+ Rule r3 = new MockRule() ;
+ node = getNormalNode( tree, 1 ) ;
+ addWildRuleToNormalTree( tree, wildpat, r3, new Stack(), node ) ;
+
+ // the pattern *,3 should match 1,2,3 now
+ // walk of the branch of tag nodes and validate contents along the way
+ assertEquals( 0, node.getRules().size() ) ;
+ node = node.getChild( new Integer( 2 ) ) ;
+ assertEquals( 0, node.getRules().size() ) ;
+ node = node.getChild( new Integer( 3 ) ) ;
+ assertEquals( 4, node.getRules().size() ) ;
+ assertEquals( r0, node.getRules().get( 0 ) ) ;
+ assertEquals( r1, node.getRules().get( 1 ) ) ;
+ assertEquals( r2, node.getRules().get( 2 ) ) ;
+ assertEquals( r3, node.getRules().get( 3 ) ) ;
+ }
+
+
+ /**
+ * Tests to see that we do not add the rule with wild card pattern more
+ * than once to a node in the normal tree by first checking if it already
+ * contains that rule before a rule add operation to the node.
+ */
+ public void testAddWildRuleToNormalTree1()
+ {
+ TagTree tree = new TagTree() ;
+ int[] pattern = {1,2,3} ;
+ Rule r0 = new MockRule() ;
+ tree.addRule( pattern, r0 ) ;
+
+ int[] wildpat = { TagTree.WILDCARD,1,2,3} ;
+ TagNode node = getNormalNode( tree, 1 ) ;
+
+ // see if the rule r0 is added - should not be!
+ addWildRuleToNormalTree( tree, wildpat, r0, new Stack(), node ) ;
+ node = node.getChild( new Integer( 2 ) ).getChild( new Integer( 3 ) ) ;
+ List rules = node.getRules() ;
+
+ // only one copy of r0 should exist
+ assertEquals( 1, rules.size() ) ;
+ assertEquals( r0, rules.get( 0 ) ) ;
+ }
+
+
+ /**
+ * Used to white box test the private addWildRuleToNormalTree() method of
+ * the TagTree class.
+ *
+ * @param tree the tree instance to test
+ * @param pattern the 1st int[] pattern arg
+ * @param rule the 2nd Rule arg
+ * @param stack the 3rd Stack arg
+ * @param node the last TagNode arg
+ */
+ public void addWildRuleToWildTree( TagTree tree, int[] pattern,
+ Rule rule, Stack stack, TagNode node )
+ {
+ Class[] argClasses = {
+ pattern.getClass(), Rule.class, Stack.class, TagNode.class
+ } ;
+
+ Object[] args = { pattern, rule, stack, node } ;
+ invoke( tree, TagTree.class, "addWildRuleToWildTree", argClasses,
+ args ) ;
+ }
+
+
+
+ /**
+ * Tests various combinations of wild card patterns and trees to make sure
+ * all is working with addWildRuleToWildTree().
+ */
+ public void testAddWildRuleToWildTree2()
+ {
+ TagTree tree = new TagTree() ;
+ TagNode n0 = new TagNode( new Integer( 2 ) ) ;
+ TagNode n1 = new TagNode( new Integer( 1 ) ) ;
+ TagNode n2 = new TagNode( new Integer( 3 ) ) ;
+ n0.addNode( n1 ) ;
+ n1.addNode( n2 ) ;
+ HashMap wildNodes = ( HashMap ) getMember( "wildNodes", tree ) ;
+ wildNodes.put( new Integer( 2 ), n0 ) ;
+
+ int[] pattern = {TagTree.WILDCARD,1,2} ;
+ Rule r0 = new MockRule() ;
+ addWildRuleToWildTree( tree, pattern, r0, new Stack(), n0 ) ;
+
+ // the pattern *,1,2 should match at nodes 2-1 and 2-1-3
+ assertEquals( 0, n0.getRules().size() ) ;
+ assertEquals( 1, n1.getRules().size() ) ;
+ assertEquals( r0, n1.getRules().get( 0 ) ) ;
+ assertEquals( 1, n2.getRules().size() ) ;
+ assertEquals( r0, n2.getRules().get( 0 ) ) ;
+ }
+
+ /**
+ * Tests various combinations of wild card patterns and trees to make sure
+ * all is working with addWildRuleToWildTree().
+ */
+ public void testAddWildRuleToWildTree1()
+ {
+ TagTree tree = new TagTree() ;
+ TagNode n0 = new TagNode( new Integer( 2 ) ) ;
+ TagNode n1 = new TagNode( new Integer( 1 ) ) ;
+ TagNode n2 = new TagNode( new Integer( 3 ) ) ;
+ n0.addNode( n1 ) ;
+ n1.addNode( n2 ) ;
+ HashMap wildNodes = ( HashMap ) getMember( "wildNodes", tree ) ;
+ wildNodes.put( new Integer( 2 ), n0 ) ;
+
+ int[] pattern = {TagTree.WILDCARD,1,2} ;
+ Rule r0 = new MockRule() ;
+ n1.addRule( r0 );
+ n2.addRule( r0 );
+ addWildRuleToWildTree( tree, pattern, r0, new Stack(), n0 ) ;
+
+ // the pattern *,1,2 should match at nodes 2-1 and 2-1-3
+ assertEquals( 0, n0.getRules().size() ) ;
+ assertEquals( 1, n1.getRules().size() ) ;
+ assertEquals( r0, n1.getRules().get( 0 ) ) ;
+ assertEquals( 1, n2.getRules().size() ) ;
+ assertEquals( r0, n2.getRules().get( 0 ) ) ;
+ }
+
+
+ /**
+ * Tests the addRule method of the tree.
+ */
+ public void testAddRuleNormal()
+ {
+ TagTree tree = new TagTree() ;
+ int[] pattern = {1,2,3} ;
+ tree.addRule( pattern, new MockRule() ) ;
+ assertNull( getNormalNode( tree, 4 ) ) ;
+
+ TagNode node = getNormalNode( tree, 1 ) ;
+ assertNotNull( node ) ;
+ node = node.getChild( new Integer(2) ) ;
+ assertNotNull( node ) ;
+ node = node.getChild( new Integer(3) ) ;
+ assertNotNull( node ) ;
+ node = node.getChild( new Integer(4) ) ;
+ assertNull( node ) ;
+
+ tree.addRule( pattern, new MockRule() ) ;
+ }
+
+
+ /**
+ * Used to white box test the private addWildRule() method of the
+ * TagTree.
+ *
+ * @param tree the tree instance to test
+ * @param pattern the pattern arg
+ * @param rule the Rule arg
+ */
+ private void addWildRule( TagTree tree, int[] pattern, Rule rule )
+ {
+ Class[] argClasses = { pattern.getClass(), Rule.class } ;
+ Object[] args = { pattern, rule } ;
+ invoke( tree, TagTree.class, "addWildRule", argClasses, args ) ;
+ }
+
+
+ /**
+ * Used to white box test the private addNormalRule() method of the
+ * TagTree.
+ *
+ * @param tree the tree instance to test
+ * @param pattern the pattern arg
+ * @param rule the Rule arg
+ */
+ private void addNormalRule( TagTree tree, int[] pattern, Rule rule )
+ {
+ Class[] argClasses = { pattern.getClass(), Rule.class } ;
+ Object[] args = { pattern, rule } ;
+ invoke( tree, TagTree.class, "addNormalRule",
+ argClasses, args ) ;
+ }
+
+
+ /**
+ * Tests the addRule method of the tree.
+ */
+ public void testAddWildRule1()
+ {
+ TagTree tree = new TagTree() ;
+ int[] pattern = {TagTree.WILDCARD,1,2,3} ;
+ Rule r0 = new MockRule() ;
+ addWildRule( tree, pattern, r0 );
+ assertNull( getWildNode( tree, 4 ) ) ;
+
+ TagNode node = getWildNode( tree, 3 ) ;
+ assertNotNull( node ) ;
+ node = node.getChild( new Integer(2) ) ;
+ assertNotNull( node ) ;
+ node = node.getChild( new Integer(1) ) ;
+ assertNotNull( node ) ;
+ assertEquals( 1, node.getRules().size() ) ;
+ assertEquals( r0, node.getRules().get( 0 ) ) ;
+ node = node.getChild( new Integer(4) ) ;
+ assertNull( node ) ;
+ }
+
+
+ /**
+ * Tests the addRule method of the tree.
+ */
+ public void testAddWildRule2()
+ {
+ TagTree tree = new TagTree() ;
+ int[] pattern0 = {TagTree.WILDCARD,1,2} ;
+ int[] pattern1 = {3,1,2} ;
+ int[] pattern2 = {TagTree.WILDCARD,2} ;
+ Rule r0 = new MockRule() ;
+ Rule r1 = new MockRule() ;
+ Rule r2 = new MockRule() ;
+ addNormalRule( tree, pattern1, r1 ) ;
+ addWildRule( tree, pattern0, r0 );
+ assertNull( getWildNode( tree, 4 ) ) ;
+
+ // now test that we have made the addition to the wild tree
+ TagNode node = getWildNode( tree, 2 ) ;
+ assertNotNull( node ) ;
+ node = node.getChild( new Integer(1) ) ;
+ assertNotNull( node ) ;
+ assertEquals( 1, node.getRules().size() ) ;
+ assertEquals( r0, node.getRules().get( 0 ) ) ;
+
+ node = node.getChild( new Integer(4) ) ;
+ assertNull( node ) ;
+
+ // now test the normal tree
+ HashMap normNodes = ( HashMap ) getMember( "normNodes", tree ) ;
+ assertEquals( 1, normNodes.size() ) ;
+ node = getNormalNode( tree, 3 ) ;
+ assertNotNull( node ) ;
+ assertEquals( 0, node.getRules().size() ) ;
+ node = node.getChild( new Integer( 1 ) ) ;
+ assertNotNull( node ) ;
+ assertEquals( 0, node.getRules().size() ) ;
+ node = node.getChild( new Integer( 2 ) ) ;
+ assertNotNull( node ) ;
+ assertEquals( 2, node.getRules().size() ) ;
+ assertEquals( r1, node.getRules().get( 0 ) ) ;
+ assertEquals( r0, node.getRules().get( 1 ) ) ;
+
+ // now we'll add the other wild card and see if it adds correctly
+ addWildRule( tree, pattern2, r2 ) ;
+
+ // test that we have made the r2 addition to the places in wild tree
+ node = getWildNode( tree, 2 ) ;
+ assertNotNull( node ) ;
+ assertEquals( 1, node.getRules().size() ) ;
+ assertEquals( r2, node.getRules().get( 0 ) ) ;
+ node = node.getChild( new Integer(1) ) ;
+ assertNotNull( node ) ;
+ assertEquals( 2, node.getRules().size() ) ;
+ assertEquals( r0, node.getRules().get( 0 ) ) ;
+ assertEquals( r2, node.getRules().get( 1 ) ) ;
+ assertTrue( node.isLeaf() ) ;
+
+ // test that we have added r2 to the normal tree
+ assertEquals( 1, normNodes.size() ) ;
+ node = getNormalNode( tree, 3 ) ;
+ assertNotNull( node ) ;
+ assertEquals( 0, node.getRules().size() ) ;
+ node = node.getChild( new Integer( 1 ) ) ;
+ assertNotNull( node ) ;
+ assertEquals( 0, node.getRules().size() ) ;
+ node = node.getChild( new Integer( 2 ) ) ;
+ assertNotNull( node ) ;
+ assertEquals( 3, node.getRules().size() ) ;
+ assertEquals( r1, node.getRules().get( 0 ) ) ;
+ assertEquals( r0, node.getRules().get( 1 ) ) ;
+ assertEquals( r2, node.getRules().get( 2 ) ) ;
+
+ // control
+ node = node.getChild( new Integer(4) ) ;
+ assertNull( node ) ;
+ }
+
+
+ /**
+ * Tests the addNormalRule method of the tree after registering
+ * rule patterns using wild cards.
+ */
+ public void testAddNormalRule1()
+ {
+ TagTree tree = new TagTree() ;
+ int[] pattern0 = {TagTree.WILDCARD,1,2} ;
+ int[] pattern1 = {3,1,2} ;
+ int[] pattern2 = {TagTree.WILDCARD,2} ;
+ Rule r0 = new MockRule() ;
+ Rule r1 = new MockRule() ;
+ Rule r2 = new MockRule() ;
+ addNormalRule( tree, pattern1, r1 ) ;
+ addWildRule( tree, pattern0, r0 );
+ assertNull( getWildNode( tree, 4 ) ) ;
+
+ // now test that we have made the addition to the wild tree
+ TagNode node = getWildNode( tree, 2 ) ;
+ assertNotNull( node ) ;
+ node = node.getChild( new Integer(1) ) ;
+ assertNotNull( node ) ;
+ assertEquals( 1, node.getRules().size() ) ;
+ assertEquals( r0, node.getRules().get( 0 ) ) ;
+
+ node = node.getChild( new Integer(4) ) ;
+ assertNull( node ) ;
+
+ // now test the normal tree
+ HashMap normNodes = ( HashMap ) getMember( "normNodes", tree ) ;
+ assertEquals( 1, normNodes.size() ) ;
+ node = getNormalNode( tree, 3 ) ;
+ assertNotNull( node ) ;
+ assertEquals( 0, node.getRules().size() ) ;
+ node = node.getChild( new Integer( 1 ) ) ;
+ assertNotNull( node ) ;
+ assertEquals( 0, node.getRules().size() ) ;
+ node = node.getChild( new Integer( 2 ) ) ;
+ assertNotNull( node ) ;
+ assertEquals( 2, node.getRules().size() ) ;
+ assertEquals( r1, node.getRules().get( 0 ) ) ;
+ assertEquals( r0, node.getRules().get( 1 ) ) ;
+
+ // now we'll add the other wild card and see if it adds correctly
+ addWildRule( tree, pattern2, r2 ) ;
+
+ // test that we have made the r2 addition to the places in wild tree
+ node = getWildNode( tree, 2 ) ;
+ assertNotNull( node ) ;
+ assertEquals( 1, node.getRules().size() ) ;
+ assertEquals( r2, node.getRules().get( 0 ) ) ;
+ node = node.getChild( new Integer(1) ) ;
+ assertNotNull( node ) ;
+ assertEquals( 2, node.getRules().size() ) ;
+ assertEquals( r0, node.getRules().get( 0 ) ) ;
+ assertEquals( r2, node.getRules().get( 1 ) ) ;
+ assertTrue( node.isLeaf() ) ;
+
+ // test that we have added r2 to the normal tree
+ assertEquals( 1, normNodes.size() ) ;
+ node = getNormalNode( tree, 3 ) ;
+ assertNotNull( node ) ;
+ assertEquals( 0, node.getRules().size() ) ;
+ node = node.getChild( new Integer( 1 ) ) ;
+ assertNotNull( node ) ;
+ assertEquals( 0, node.getRules().size() ) ;
+ node = node.getChild( new Integer( 2 ) ) ;
+ assertNotNull( node ) ;
+ assertEquals( 3, node.getRules().size() ) ;
+ assertEquals( r1, node.getRules().get( 0 ) ) ;
+ assertEquals( r0, node.getRules().get( 1 ) ) ;
+ assertEquals( r2, node.getRules().get( 2 ) ) ;
+
+ // control
+ node = node.getChild( new Integer(4) ) ;
+ assertNull( node ) ;
+
+ // lets add a normal node to the normal tree matching these patterns
+ int[] pattern3 = {8,1,2} ;
+ Rule r3 = new MockRule() ;
+ List wildRegistrations = ( List )
+ getMember( "wildRegistrations", tree ) ;
+ wildRegistrations.add( new RuleRegistration( pattern0, r0 ) ) ;
+ wildRegistrations.add( new RuleRegistration( pattern2, r2 ) ) ;
+ addNormalRule( tree, pattern3, r3 ) ;
+
+ // test if we have added the rules and nodes correctly
+ assertEquals( 2, normNodes.size() ) ;
+ node = getNormalNode( tree, 8 ) ;
+ assertNotNull( node ) ;
+ assertEquals( 0, node.getRules().size() ) ;
+ node = node.getChild( new Integer( 1 ) ) ;
+ assertNotNull( node ) ;
+ assertEquals( 0, node.getRules().size() ) ;
+ node = node.getChild( new Integer( 2 ) ) ;
+ assertNotNull( node ) ;
+ assertEquals( 3, node.getRules().size() ) ;
+ assertEquals( r0, node.getRules().get( 0 ) ) ;
+ assertEquals( r2, node.getRules().get( 1 ) ) ;
+ assertEquals( r3, node.getRules().get( 2 ) ) ;
+ }
+
+
+ /**
+ * Tests the TagTree.match(int[]) method.
+ */
+ public void testMatchintArray()
+ {
+ TagTree tree = new TagTree() ;
+ int[] pattern0 = {1,2,3} ;
+ int[] pattern1 = {4,2,7} ;
+ int[] pattern2 = {1,5,3} ;
+ Rule rule0 = new MockRule() ;
+ Rule rule1 = new MockRule() ;
+ Rule rule2 = new MockRule() ;
+ tree.addRule( pattern0, rule0 ) ;
+ tree.addRule( pattern1, rule1 ) ;
+ tree.addRule( pattern2, rule2 ) ;
+ assertEquals( rule0, ( (List) tree.match( pattern0 ) ).get(0) ) ;
+ assertEquals( rule1, ( (List) tree.match( pattern1 ) ).get(0) ) ;
+ assertEquals( rule2, ( (List) tree.match( pattern2 ) ).get(0) ) ;
+
+ assertNotSame( rule0, ( (List) tree.match( pattern1 ) ).get(0) ) ;
+ assertNotSame( rule1, ( (List) tree.match( pattern2 ) ).get(0) ) ;
+ assertNotSame( rule2, ( (List) tree.match( pattern0 ) ).get(0) ) ;
+
+ int[] pattern3 = { 12 } ;
+ tree.match( pattern3 ) ;
+ }
+
+
+ /**
+ * Tests the TagTree.match(IntStack) method.
+ */
+ public void testMatchIntStack()
+ {
+ TagTree tree = new TagTree() ;
+ int[] pattern0 = {1,2,3} ;
+ int[] pattern1 = {4,2,7} ;
+ int[] pattern2 = {1,5,3} ;
+ Rule rule0 = new MockRule() ;
+ Rule rule1 = new MockRule() ;
+ Rule rule2 = new MockRule() ;
+ tree.addRule( pattern0, rule0 ) ;
+ tree.addRule( pattern1, rule1 ) ;
+ tree.addRule( pattern2, rule2 ) ;
+
+ assertEquals( rule0, ( (List) tree.match( new IntStack( pattern0 ) ) )
+ .get(0) ) ;
+ assertEquals( rule1, ( (List) tree.match( new IntStack( pattern1 ) ) )
+ .get(0) ) ;
+ assertEquals( rule2, ( (List) tree.match( new IntStack( pattern2 ) ) )
+ .get(0) ) ;
+ assertNotSame( rule0, ( (List) tree.match( new IntStack( pattern1 ) ) )
+ .get(0) ) ;
+ assertNotSame( rule1, ( (List) tree.match( new IntStack( pattern2 ) ) )
+ .get(0) ) ;
+ assertNotSame( rule2, ( (List) tree.match( new IntStack( pattern0 ) ) )
+ .get(0) ) ;
+ }
+
+
+ /**
+ * Tests the TagTree.getNormalNode(int[]) method.
+ */
+ public void testGetNodeintArray()
+ {
+ TagTree tree = new TagTree() ;
+ int[] pattern0 = {1,2,3} ;
+ int[] pattern1 = {4,2,7} ;
+ int[] pattern2 = {1,5,3} ;
+ Rule rule0 = new MockRule() ;
+ Rule rule1 = new MockRule() ;
+ Rule rule2 = new MockRule() ;
+ tree.addRule( pattern0, rule0 ) ;
+ tree.addRule( pattern1, rule1 ) ;
+ tree.addRule( pattern2, rule2 ) ;
+
+ int[] pattern3 = { 1, 23 } ;
+ assertNull( tree.getNode( pattern3 ) ) ;
+ }
+
+
+ class MockRule extends AbstractRule { }
+}
Added: directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/digester/rules/ByteAccumulatorTest.java
URL: http://svn.apache.org/viewcvs/directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/digester/rules/ByteAccumulatorTest.java?rev=234226&view=auto
==============================================================================
--- directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/digester/rules/ByteAccumulatorTest.java (added)
+++ directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/digester/rules/ByteAccumulatorTest.java Sun Aug 21 08:03:46 2005
@@ -0,0 +1,210 @@
+/*
+ * Copyright 2004 The Apache Software Foundation
+ *
+ * Licensed 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.asn1.ber.digester.rules ;
+
+
+import junit.framework.TestCase ;
+
+import java.nio.ByteBuffer ;
+
+import org.apache.asn1.ber.digester.rules.ByteAccumulator;
+
+
+/**
+ * Tests the ByteAccumulator class.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev: 157644 $
+ */
+public class ByteAccumulatorTest extends TestCase
+{
+ ByteAccumulator accumulator = null ;
+
+
+ protected void setUp() throws Exception
+ {
+ super.setUp();
+ accumulator = new ByteAccumulator() ;
+ }
+
+
+ protected void tearDown() throws Exception
+ {
+ super.tearDown();
+ accumulator = null ;
+ }
+
+
+ public void testByteAccumulatorInt()
+ {
+ ByteAccumulator ba = new ByteAccumulator( 72 ) ;
+ assertEquals( ba.getInitialSize(), ba.getCapacity() ) ;
+ assertEquals( ba.getInitialSize(), ba.getRemainingSpace() ) ;
+ assertEquals( 0, ba.getPosition() ) ;
+
+ // shuts up clover
+ ba = new ByteAccumulator( -1 ) ;
+ assertEquals( ba.getInitialSize(), ba.getCapacity() ) ;
+ assertEquals( ba.getInitialSize(), ba.getRemainingSpace() ) ;
+ assertEquals( 0, ba.getPosition() ) ;
+ }
+
+
+ /**
+ * Fills the buffer by varying amounts so that either the remaining
+ * amount is used for growth increment or the set increment value is
+ * used. Basically tests the growth behavior and byte accounting.
+ */
+ public void testFill()
+ {
+ assertEquals( accumulator.getInitialSize(),
+ accumulator.getCapacity() ) ;
+ assertEquals( accumulator.getInitialSize(),
+ accumulator.getRemainingSpace() ) ;
+ assertEquals( 0, accumulator.getPosition() ) ;
+
+ ByteBuffer buf = ByteBuffer.allocate( 70 ) ;
+ buf.position( 70 ).flip() ;
+ accumulator.fill( buf ) ;
+ assertEquals( 100, accumulator.getCapacity() ) ;
+ assertEquals( 30, accumulator.getRemainingSpace() ) ;
+ assertEquals( 70, accumulator.getPosition() ) ;
+
+ buf = ByteBuffer.allocate( 70 ) ;
+ buf.position( 70 ).flip() ;
+ accumulator.fill( buf ) ;
+ assertEquals( 200, accumulator.getCapacity() ) ;
+ assertEquals( 60, accumulator.getRemainingSpace() ) ;
+
+ buf = ByteBuffer.allocate( 160 ) ;
+ buf.position( 160 ).flip() ;
+ accumulator.fill( buf ) ;
+ assertEquals( 300, accumulator.getCapacity() ) ;
+ assertEquals( 0, accumulator.getRemainingSpace() ) ;
+
+ buf = ByteBuffer.allocate( 110 ) ;
+ buf.position( 110 ).flip() ;
+ accumulator.fill( buf ) ;
+ assertEquals( 410, accumulator.getCapacity() ) ;
+
+ // test the trivial case
+ accumulator.fill( buf ) ;
+ assertEquals( 410, accumulator.getCapacity() ) ;
+ assertEquals( 0, accumulator.getRemainingSpace() ) ;
+ }
+
+
+ /**
+ * Tests to make sure drains reset the backing store to the initial state
+ * and the right amount of compacted buffer is returned. Make sure the
+ * inputs are the same as the outputs.
+ */
+ public void testDrain()
+ {
+ ByteBuffer buf = ByteBuffer.allocate( 200 ) ;
+ buf.position( 200 ).flip() ;
+ accumulator.fill( buf ) ;
+ assertEquals( 200, accumulator.getCapacity() ) ;
+ ByteBuffer total = accumulator.drain() ;
+ assertEquals( 200, total.remaining() ) ;
+ assertEquals( 0, accumulator.getCapacity() ) ;
+ assertEquals( 0, accumulator.getRemainingSpace() ) ;
+
+ buf = ByteBuffer.allocate( 30 ) ;
+ buf.position( 30 ).flip() ;
+ accumulator.fill( buf ) ;
+ assertEquals( 100, accumulator.getCapacity() ) ;
+ assertEquals( 70, accumulator.getRemainingSpace() ) ;
+ total = accumulator.drain() ;
+ assertEquals( 30, total.remaining() ) ;
+ assertEquals( 0, accumulator.getCapacity() ) ;
+ assertEquals( 0, accumulator.getRemainingSpace() ) ;
+
+ buf = ByteBuffer.allocate( 3 ) ;
+ buf.put( (byte) 0x01 ) ;
+ buf.put( (byte) 0x02 ) ;
+ buf.put( (byte) 0x03 ) ;
+ buf.flip() ;
+ accumulator.fill( buf ) ;
+ assertEquals( 100, accumulator.getCapacity() ) ;
+ assertEquals( 97, accumulator.getRemainingSpace() ) ;
+ buf = accumulator.drain() ;
+ assertEquals( 0, accumulator.getCapacity() ) ;
+ assertEquals( 0, accumulator.getRemainingSpace() ) ;
+ assertEquals( 0x01, buf.get() ) ;
+ assertEquals( 0x02, buf.get() ) ;
+ assertEquals( 0x03, buf.get() ) ;
+ assertFalse( buf.hasRemaining() ) ;
+
+ ByteAccumulator ba = new ByteAccumulator( 1 ) ;
+ ba.fill( ByteBuffer.allocate( 1 ) ) ;
+ buf = ba.drain() ;
+ buf.get() ;
+ assertFalse( buf.hasRemaining() ) ;
+ }
+
+
+ public void testDrainInt0()
+ {
+ accumulator.fill( ByteBuffer.allocate( 200 ) ) ;
+ accumulator.drain( -1 ) ;
+ assertEquals( 0, accumulator.getCapacity() ) ;
+ assertEquals( 0, accumulator.getRemainingSpace() ) ;
+ }
+
+
+ public void testDrainInt1()
+ {
+ accumulator.fill( ByteBuffer.allocate( 200 ) ) ;
+ accumulator.drain( 10 ) ;
+ assertEquals( 10, accumulator.getCapacity() ) ;
+ assertEquals( 10, accumulator.getRemainingSpace() ) ;
+ }
+
+
+ public void testDrainInt2()
+ {
+ accumulator.fill( ByteBuffer.allocate( 20 ) ) ;
+ accumulator.drain( 10 ) ;
+ assertEquals( 10, accumulator.getCapacity() ) ;
+ assertEquals( 10, accumulator.getRemainingSpace() ) ;
+ }
+
+
+ public void testEnsureCapacity()
+ {
+ accumulator.ensureCapacity( 150 ) ;
+ accumulator.ensureCapacity( 10 ) ;
+ assertEquals( 150, accumulator.getCapacity() ) ;
+ assertEquals( 150, accumulator.getRemainingSpace() ) ;
+ accumulator.ensureCapacity( 222 ) ;
+ assertEquals( 222, accumulator.getCapacity() ) ;
+ assertEquals( 222, accumulator.getRemainingSpace() ) ;
+ }
+
+
+ public void testGrowthIncrement()
+ {
+ int increment = accumulator.getGrowthIncrement() ;
+ ByteBuffer buf = ByteBuffer.allocate(
+ accumulator.getInitialSize() + 1 ) ;
+ buf.position( buf.capacity() ).flip() ;
+ accumulator.fill( buf ) ;
+ assertEquals( increment + accumulator.getInitialSize(),
+ accumulator.getCapacity() ) ;
+ }
+}
\ No newline at end of file
Added: directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/digester/rules/PrimitiveIntDecodeRuleTest.java
URL: http://svn.apache.org/viewcvs/directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/digester/rules/PrimitiveIntDecodeRuleTest.java?rev=234226&view=auto
==============================================================================
--- directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/digester/rules/PrimitiveIntDecodeRuleTest.java (added)
+++ directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/digester/rules/PrimitiveIntDecodeRuleTest.java Sun Aug 21 08:03:46 2005
@@ -0,0 +1,459 @@
+/*
+ * Copyright 2004 The Apache Software Foundation
+ *
+ * Licensed 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.asn1.ber.digester.rules ;
+
+
+import junit.framework.TestCase ;
+
+import org.apache.asn1.ber.TypeClass ;
+import org.apache.asn1.ber.digester.BERDigester ;
+import org.apache.asn1.ber.primitives.UniversalTag ;
+import org.apache.asn1.ber.primitives.UniversalTag;
+import org.apache.asn1.ber.digester.rules.PrimitiveIntDecodeRule;
+import org.apache.asn1.ber.digester.BERDigester;
+import org.apache.asn1.ber.TypeClass;
+
+import java.nio.ByteBuffer;
+import java.math.BigInteger;
+
+
+/**
+ * Tests the PrimitiveIntDecodeRule.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev: 157644 $
+ */
+public class PrimitiveIntDecodeRuleTest extends TestCase
+{
+ PrimitiveIntDecodeRule rule ;
+ BERDigester digester ;
+
+
+ protected void setUp() throws Exception
+ {
+ super.setUp() ;
+ rule = new PrimitiveIntDecodeRule() ;
+ digester = new BERDigester() ;
+ rule.setDigester( digester ) ;
+ int[] pattern = { 0x10000000, 0x02000000 } ;
+ digester.addRule( pattern, rule ) ;
+ }
+
+
+ protected void tearDown() throws Exception
+ {
+ super.tearDown() ;
+ rule.setDigester( null ) ;
+ rule = null ;
+ digester = null ;
+ }
+
+
+ public void testTag()
+ {
+ rule.tag( UniversalTag.INTEGER.getTagId(), true,
+ TypeClass.UNIVERSAL ) ;
+
+ try
+ {
+ rule.tag( 0, false, null ) ;
+ fail( "should never get here" ) ;
+ }
+ catch ( IllegalArgumentException e )
+ {
+ }
+ }
+
+
+ public void testLength()
+ {
+ rule.tag( UniversalTag.INTEGER.getTagId(), true,
+ TypeClass.UNIVERSAL ) ;
+
+ rule.length( 0 ) ;
+ rule.length( 1 ) ;
+ rule.length( 2 ) ;
+ rule.length( 3 ) ;
+ rule.length( 4 ) ;
+
+ try
+ {
+ rule.length( -1 ) ;
+ fail( "should never get here due to exception" ) ;
+ }
+ catch ( IllegalArgumentException e )
+ {
+ }
+
+ try
+ {
+ rule.length( 5 ) ;
+ fail( "should never get here due to exception" ) ;
+ }
+ catch ( IllegalArgumentException e )
+ {
+ }
+ }
+
+
+ public void testValue0()
+ {
+ rule.tag( UniversalTag.INTEGER.getTagId(), true,
+ TypeClass.UNIVERSAL ) ;
+ rule.length( 0 ) ;
+ rule.value( null ) ;
+ }
+
+
+ public void testValue1()
+ {
+ rule.tag( UniversalTag.INTEGER.getTagId(), true,
+ TypeClass.UNIVERSAL ) ;
+ rule.length( 1 ) ;
+ byte[] bites = { 0x45 } ;
+ ByteBuffer buf = ByteBuffer.wrap( bites ) ;
+ rule.value( buf ) ;
+ assertTrue( rule.equals( (byte) 0x45, 0 ) ) ;
+ assertTrue( rule.equals( (byte) 0x00, 1 ) ) ;
+ assertTrue( rule.equals( (byte) 0x00, 2 ) ) ;
+ assertTrue( rule.equals( (byte) 0x00, 3 ) ) ;
+ buf.clear() ;
+ rule.value( buf ) ;
+ assertTrue( rule.equals( (byte) 0x45, 0 ) ) ;
+ assertTrue( rule.equals( (byte) 0x00, 1 ) ) ;
+ assertTrue( rule.equals( (byte) 0x00, 2 ) ) ;
+ assertTrue( rule.equals( (byte) 0x00, 3 ) ) ;
+ }
+
+
+ public void testValue2()
+ {
+ rule.tag( UniversalTag.INTEGER.getTagId(), true,
+ TypeClass.UNIVERSAL ) ;
+ rule.length( 2 ) ;
+ byte[] bites = { 0x45, 0x23 } ;
+ ByteBuffer buf = ByteBuffer.wrap( bites ) ;
+ rule.value( buf ) ;
+ assertTrue( rule.equals( (byte) 0x45, 0 ) ) ;
+ assertTrue( rule.equals( (byte) 0x23, 1 ) ) ;
+ assertTrue( rule.equals( (byte) 0x00, 2 ) ) ;
+ assertTrue( rule.equals( (byte) 0x00, 3 ) ) ;
+ buf.clear() ;
+ rule.value( buf ) ;
+ assertTrue( rule.equals( (byte) 0x45, 0 ) ) ;
+ assertTrue( rule.equals( (byte) 0x23, 1 ) ) ;
+ assertTrue( rule.equals( (byte) 0x00, 2 ) ) ;
+ assertTrue( rule.equals( (byte) 0x00, 3 ) ) ;
+ }
+
+
+ public void testValue2Fragmented()
+ {
+ rule.tag( UniversalTag.INTEGER.getTagId(), true,
+ TypeClass.UNIVERSAL ) ;
+ rule.length( 2 ) ;
+ byte[] bites0 = { 0x45 } ;
+ byte[] bites1 = { 0x23 } ;
+ ByteBuffer buf = ByteBuffer.wrap( bites0 ) ;
+ rule.value( buf ) ;
+ assertTrue( rule.equals( (byte) 0x45, 0 ) ) ;
+ assertTrue( rule.equals( (byte) 0x00, 1 ) ) ;
+ assertTrue( rule.equals( (byte) 0x00, 2 ) ) ;
+ assertTrue( rule.equals( (byte) 0x00, 3 ) ) ;
+ buf = ByteBuffer.wrap( bites1 ) ;
+ rule.value( buf ) ;
+ assertTrue( rule.equals( (byte) 0x45, 0 ) ) ;
+ assertTrue( rule.equals( (byte) 0x23, 1 ) ) ;
+ assertTrue( rule.equals( (byte) 0x00, 2 ) ) ;
+ assertTrue( rule.equals( (byte) 0x00, 3 ) ) ;
+ buf.clear() ;
+ rule.value( buf ) ;
+ assertTrue( rule.equals( (byte) 0x45, 0 ) ) ;
+ assertTrue( rule.equals( (byte) 0x23, 1 ) ) ;
+ assertTrue( rule.equals( (byte) 0x00, 2 ) ) ;
+ assertTrue( rule.equals( (byte) 0x00, 3 ) ) ;
+ }
+
+
+ public void testValue3()
+ {
+ rule.tag( UniversalTag.INTEGER.getTagId(), true,
+ TypeClass.UNIVERSAL ) ;
+ rule.length( 3 ) ;
+ byte[] bites = { 0x45, 0x23, 0x12 } ;
+ ByteBuffer buf = ByteBuffer.wrap( bites ) ;
+ rule.value( buf ) ;
+ assertTrue( rule.equals( (byte) 0x45, 0 ) ) ;
+ assertTrue( rule.equals( (byte) 0x23, 1 ) ) ;
+ assertTrue( rule.equals( (byte) 0x12, 2 ) ) ;
+ assertTrue( rule.equals( (byte) 0x00, 3 ) ) ;
+ buf.clear() ;
+ rule.value( buf ) ;
+ assertTrue( rule.equals( (byte) 0x45, 0 ) ) ;
+ assertTrue( rule.equals( (byte) 0x23, 1 ) ) ;
+ assertTrue( rule.equals( (byte) 0x12, 2 ) ) ;
+ assertTrue( rule.equals( (byte) 0x00, 3 ) ) ;
+ }
+
+
+ public void testValue4()
+ {
+ rule.tag( UniversalTag.INTEGER.getTagId(), true,
+ TypeClass.UNIVERSAL ) ;
+ rule.length( 4 ) ;
+ byte[] bites = { 0x45, 0x23, 0x12, 0x01 } ;
+ ByteBuffer buf = ByteBuffer.wrap( bites ) ;
+ rule.value( buf ) ;
+ assertTrue( rule.equals( (byte) 0x45, 0 ) ) ;
+ assertTrue( rule.equals( (byte) 0x23, 1 ) ) ;
+ assertTrue( rule.equals( (byte) 0x12, 2 ) ) ;
+ assertTrue( rule.equals( (byte) 0x01, 3 ) ) ;
+ buf.clear() ;
+ rule.value( buf ) ;
+ assertTrue( rule.equals( (byte) 0x45, 0 ) ) ;
+ assertTrue( rule.equals( (byte) 0x23, 1 ) ) ;
+ assertTrue( rule.equals( (byte) 0x12, 2 ) ) ;
+ assertTrue( rule.equals( (byte) 0x01, 3 ) ) ;
+ }
+
+
+ public void testValue5()
+ {
+ rule.tag( UniversalTag.INTEGER.getTagId(), true,
+ TypeClass.UNIVERSAL ) ;
+ rule.length( 4 ) ;
+ byte[] bites = { 0x45, 0x23, 0x12, 0x01, 0x07 } ;
+ ByteBuffer buf = ByteBuffer.wrap( bites ) ;
+ rule.value( buf ) ;
+ assertTrue( rule.equals( (byte) 0x45, 0 ) ) ;
+ assertTrue( rule.equals( (byte) 0x23, 1 ) ) ;
+ assertTrue( rule.equals( (byte) 0x12, 2 ) ) ;
+ assertTrue( rule.equals( (byte) 0x01, 3 ) ) ;
+ buf.clear() ;
+ rule.value( buf ) ;
+ assertTrue( rule.equals( (byte) 0x45, 0 ) ) ;
+ assertTrue( rule.equals( (byte) 0x23, 1 ) ) ;
+ assertTrue( rule.equals( (byte) 0x12, 2 ) ) ;
+ assertTrue( rule.equals( (byte) 0x01, 3 ) ) ;
+ }
+
+
+ public void testFinishNullDigester()
+ {
+ rule.setDigester( null ) ;
+ rule.tag( UniversalTag.INTEGER.getTagId(), true,
+ TypeClass.UNIVERSAL ) ;
+ rule.length( 0 ) ;
+ rule.value( null ) ;
+ rule.finish() ;
+
+ assertEquals( 0, digester.getIntCount() ) ;
+ }
+
+
+ public void testFinish0()
+ {
+ rule.tag( UniversalTag.INTEGER.getTagId(), true,
+ TypeClass.UNIVERSAL ) ;
+ rule.length( 0 ) ;
+ rule.value( null ) ;
+ rule.finish() ;
+
+ assertEquals( 1, digester.getIntCount() ) ;
+ assertEquals( 0, digester.popInt() ) ;
+ assertEquals( 0, digester.getIntCount() ) ;
+ }
+
+
+ public void testFinish1()
+ {
+ rule.tag( UniversalTag.INTEGER.getTagId(), true,
+ TypeClass.UNIVERSAL ) ;
+ rule.length( 1 ) ;
+ byte[] bites = { 0x45 } ;
+ ByteBuffer buf = ByteBuffer.wrap( bites ) ;
+ rule.value( buf ) ;
+ assertTrue( rule.equals( (byte) 0x45, 0 ) ) ;
+ assertTrue( rule.equals( (byte) 0x00, 1 ) ) ;
+ assertTrue( rule.equals( (byte) 0x00, 2 ) ) ;
+ assertTrue( rule.equals( (byte) 0x00, 3 ) ) ;
+ buf.clear() ;
+ rule.value( buf ) ;
+ assertTrue( rule.equals( (byte) 0x45, 0 ) ) ;
+ assertTrue( rule.equals( (byte) 0x00, 1 ) ) ;
+ assertTrue( rule.equals( (byte) 0x00, 2 ) ) ;
+ assertTrue( rule.equals( (byte) 0x00, 3 ) ) ;
+
+ rule.finish() ;
+ assertEquals( 1, digester.getIntCount() ) ;
+ assertEquals( 0x45, digester.popInt() ) ;
+ assertEquals( 0, digester.getIntCount() ) ;
+ }
+
+
+ public void testFinish2()
+ {
+ rule.tag( UniversalTag.INTEGER.getTagId(), true,
+ TypeClass.UNIVERSAL ) ;
+ rule.length( 2 ) ;
+ byte[] bites = { 0x45, 0x23 } ;
+ ByteBuffer buf = ByteBuffer.wrap( bites ) ;
+ rule.value( buf ) ;
+ assertTrue( rule.equals( (byte) 0x45, 0 ) ) ;
+ assertTrue( rule.equals( (byte) 0x23, 1 ) ) ;
+ assertTrue( rule.equals( (byte) 0x00, 2 ) ) ;
+ assertTrue( rule.equals( (byte) 0x00, 3 ) ) ;
+ buf.clear() ;
+ rule.value( buf ) ;
+ assertTrue( rule.equals( (byte) 0x45, 0 ) ) ;
+ assertTrue( rule.equals( (byte) 0x23, 1 ) ) ;
+ assertTrue( rule.equals( (byte) 0x00, 2 ) ) ;
+ assertTrue( rule.equals( (byte) 0x00, 3 ) ) ;
+
+ rule.finish() ;
+ BigInteger big = new BigInteger( bites ) ;
+ assertEquals( 1, digester.getIntCount() ) ;
+ assertEquals( big.intValue(), digester.popInt() ) ;
+ assertEquals( 0, digester.getIntCount() ) ;
+ }
+
+
+ public void testFinish2Fragmented()
+ {
+ rule.tag( UniversalTag.INTEGER.getTagId(), true,
+ TypeClass.UNIVERSAL ) ;
+ rule.length( 2 ) ;
+ byte[] bites0 = { 0x45 } ;
+ byte[] bites1 = { 0x23 } ;
+ ByteBuffer buf = ByteBuffer.wrap( bites0 ) ;
+ rule.value( buf ) ;
+ assertTrue( rule.equals( (byte) 0x45, 0 ) ) ;
+ assertTrue( rule.equals( (byte) 0x00, 1 ) ) ;
+ assertTrue( rule.equals( (byte) 0x00, 2 ) ) ;
+ assertTrue( rule.equals( (byte) 0x00, 3 ) ) ;
+ buf = ByteBuffer.wrap( bites1 ) ;
+ rule.value( buf ) ;
+ assertTrue( rule.equals( (byte) 0x45, 0 ) ) ;
+ assertTrue( rule.equals( (byte) 0x23, 1 ) ) ;
+ assertTrue( rule.equals( (byte) 0x00, 2 ) ) ;
+ assertTrue( rule.equals( (byte) 0x00, 3 ) ) ;
+ buf.clear() ;
+ rule.value( buf ) ;
+ assertTrue( rule.equals( (byte) 0x45, 0 ) ) ;
+ assertTrue( rule.equals( (byte) 0x23, 1 ) ) ;
+ assertTrue( rule.equals( (byte) 0x00, 2 ) ) ;
+ assertTrue( rule.equals( (byte) 0x00, 3 ) ) ;
+ rule.finish() ;
+
+ assertEquals( 1, digester.getIntCount() ) ;
+ byte[] bitesUsed = { 0x45, 0x23 } ;
+ BigInteger big = new BigInteger( bitesUsed ) ;
+ assertEquals( big.intValue(), digester.popInt() ) ;
+ assertEquals( 0, digester.getIntCount() ) ;
+ }
+
+
+ public void testFinish3()
+ {
+ rule.tag( UniversalTag.INTEGER.getTagId(), true,
+ TypeClass.UNIVERSAL ) ;
+ rule.length( 3 ) ;
+ byte[] bites = { 0x45, 0x23, 0x12 } ;
+ ByteBuffer buf = ByteBuffer.wrap( bites ) ;
+ rule.value( buf ) ;
+ assertTrue( rule.equals( (byte) 0x45, 0 ) ) ;
+ assertTrue( rule.equals( (byte) 0x23, 1 ) ) ;
+ assertTrue( rule.equals( (byte) 0x12, 2 ) ) ;
+ assertTrue( rule.equals( (byte) 0x00, 3 ) ) ;
+ buf.clear() ;
+ rule.value( buf ) ;
+ assertTrue( rule.equals( (byte) 0x45, 0 ) ) ;
+ assertTrue( rule.equals( (byte) 0x23, 1 ) ) ;
+ assertTrue( rule.equals( (byte) 0x12, 2 ) ) ;
+ assertTrue( rule.equals( (byte) 0x00, 3 ) ) ;
+ rule.finish() ;
+
+ assertEquals( 1, digester.getIntCount() ) ;
+ BigInteger big = new BigInteger( bites ) ;
+ assertEquals( big.intValue(), digester.popInt() ) ;
+ assertEquals( 0, digester.getIntCount() ) ;
+ }
+
+
+ public void testFinish4()
+ {
+ rule.tag( UniversalTag.INTEGER.getTagId(), true,
+ TypeClass.UNIVERSAL ) ;
+ rule.length( 4 ) ;
+ byte[] bites = { 0x45, 0x23, 0x12, 0x01 } ;
+ ByteBuffer buf = ByteBuffer.wrap( bites ) ;
+ rule.value( buf ) ;
+ assertTrue( rule.equals( (byte) 0x45, 0 ) ) ;
+ assertTrue( rule.equals( (byte) 0x23, 1 ) ) ;
+ assertTrue( rule.equals( (byte) 0x12, 2 ) ) ;
+ assertTrue( rule.equals( (byte) 0x01, 3 ) ) ;
+ buf.clear() ;
+ rule.value( buf ) ;
+ assertTrue( rule.equals( (byte) 0x45, 0 ) ) ;
+ assertTrue( rule.equals( (byte) 0x23, 1 ) ) ;
+ assertTrue( rule.equals( (byte) 0x12, 2 ) ) ;
+ assertTrue( rule.equals( (byte) 0x01, 3 ) ) ;
+ rule.finish() ;
+
+ assertEquals( 1, digester.getIntCount() ) ;
+ BigInteger big = new BigInteger( bites ) ;
+ assertEquals( big.intValue(), digester.popInt() ) ;
+ assertEquals( 0, digester.getIntCount() ) ;
+ }
+
+
+ public void testFinish5()
+ {
+ rule.tag( UniversalTag.INTEGER.getTagId(), true,
+ TypeClass.UNIVERSAL ) ;
+ rule.length( 4 ) ;
+ byte[] bites = { 0x45, 0x23, 0x12, 0x01, 0x07 } ;
+ ByteBuffer buf = ByteBuffer.wrap( bites ) ;
+ rule.value( buf ) ;
+ assertTrue( rule.equals( (byte) 0x45, 0 ) ) ;
+ assertTrue( rule.equals( (byte) 0x23, 1 ) ) ;
+ assertTrue( rule.equals( (byte) 0x12, 2 ) ) ;
+ assertTrue( rule.equals( (byte) 0x01, 3 ) ) ;
+ buf.clear() ;
+ rule.value( buf ) ;
+ assertTrue( rule.equals( (byte) 0x45, 0 ) ) ;
+ assertTrue( rule.equals( (byte) 0x23, 1 ) ) ;
+ assertTrue( rule.equals( (byte) 0x12, 2 ) ) ;
+ assertTrue( rule.equals( (byte) 0x01, 3 ) ) ;
+ rule.finish() ;
+
+ assertEquals( 1, digester.getIntCount() ) ;
+ byte[] bitesUsed = { 0x45, 0x23, 0x12, 0x01 } ;
+ BigInteger big = new BigInteger( bitesUsed ) ;
+ assertEquals( big.intValue(), digester.popInt() ) ;
+ assertEquals( 0, digester.getIntCount() ) ;
+ }
+
+
+ public void testByDecoding() throws Exception
+ {
+ byte[] data = { 0x30, 0x03, 0x02, 0x01, 0x0f } ;
+ ByteBuffer buf = ByteBuffer.wrap( data ) ;
+ digester.decode( buf ) ;
+ assertEquals( 0x0f, digester.peekInt() ) ;
+ }
+}
\ No newline at end of file
Added: directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/digester/rules/PrimitiveOctetStringRuleTest.java
URL: http://svn.apache.org/viewcvs/directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/digester/rules/PrimitiveOctetStringRuleTest.java?rev=234226&view=auto
==============================================================================
--- directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/digester/rules/PrimitiveOctetStringRuleTest.java (added)
+++ directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/digester/rules/PrimitiveOctetStringRuleTest.java Sun Aug 21 08:03:46 2005
@@ -0,0 +1,143 @@
+/*
+ * Copyright 2004-2005 The Apache Software Foundation
+ *
+ * Licensed 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.asn1.ber.digester.rules ;
+
+
+import junit.framework.TestCase ;
+
+import org.apache.asn1.ber.digester.BERDigester ;
+import org.apache.asn1.ber.primitives.UniversalTag;
+import org.apache.asn1.ber.Length;
+import org.apache.asn1.ber.primitives.UniversalTag;
+import org.apache.asn1.ber.digester.rules.PrimitiveOctetStringRule;
+import org.apache.asn1.ber.digester.BERDigester;
+
+import java.nio.ByteBuffer;
+
+
+/**
+ * Tests the operation of the PrimitiveOctetStringRule.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev: 157644 $
+ */
+public class PrimitiveOctetStringRuleTest extends TestCase
+{
+ private BERDigester digester ;
+ private PrimitiveOctetStringRule rule ;
+
+
+ protected void setUp() throws Exception
+ {
+ super.setUp() ;
+
+ rule = new PrimitiveOctetStringRule() ;
+ digester = new BERDigester() ;
+ rule.setDigester( digester ) ;
+ }
+
+
+ protected void tearDown() throws Exception
+ {
+ super.tearDown() ;
+
+ rule = null ;
+ digester = null ;
+ }
+
+
+ /**
+ * Tests for correct behavior when the OCTET STRING is constructed.
+ */
+ public void testConstructedOctetString()
+ {
+ rule.tag( UniversalTag.OCTET_STRING.getTagId(), false, null ) ;
+ rule.length( 2 ) ;
+ byte[] bites = { 0x07, 0x1 } ;
+ ByteBuffer buf = ByteBuffer.wrap( bites ) ;
+ rule.value( buf ) ;
+ rule.finish() ;
+
+ // we should have nothing in the object stack
+ assertEquals( 0, digester.getCount() ) ;
+ }
+
+
+ /**
+ * Tests for correct behavior when the OCTET STRING is primitive.
+ */
+ public void testPrimitiveOctetString()
+ {
+ rule.tag( UniversalTag.OCTET_STRING.getTagId(), true, null ) ;
+ rule.length( 2 ) ;
+ byte[] bites = { 0x07, 0x1 } ;
+ ByteBuffer buf = ByteBuffer.wrap( bites ) ;
+ rule.value( buf ) ;
+ rule.finish() ;
+
+ // we should have nothing in the object stack
+ assertEquals( 1, digester.getCount() ) ;
+ ByteBuffer pushed = ( ByteBuffer ) digester.pop() ;
+ assertEquals( bites[0], pushed.get() ) ;
+ assertEquals( bites[1], pushed.get() ) ;
+ assertEquals( 0, digester.getCount() ) ;
+ }
+
+
+ /**
+ * Tests when the length is indefinite.
+ */
+ public void testIndefiniteLength()
+ {
+ rule.tag( UniversalTag.OCTET_STRING.getTagId(), true, null ) ;
+ rule.length( Length.INDEFINITE ) ;
+ rule.finish() ;
+ ByteBuffer buf = ( ByteBuffer ) digester.pop() ;
+ assertFalse( buf.hasRemaining() ) ;
+ }
+
+
+ /**
+ * Tests when the length is indefinite.
+ */
+ public void testNullValue()
+ {
+ rule.tag( UniversalTag.OCTET_STRING.getTagId(), true, null ) ;
+ rule.length( Length.INDEFINITE ) ;
+ rule.value( null ) ;
+ rule.value( ByteBuffer.allocate( 0 ) ) ;
+ rule.finish() ;
+ ByteBuffer buf = ( ByteBuffer ) digester.pop() ;
+ assertFalse( buf.hasRemaining() ) ;
+ }
+
+
+ /**
+ * Tests when the wrong tag is supplied.
+ */
+ public void testWrongTag()
+ {
+ try
+ {
+ rule.tag( UniversalTag.OBJECT_IDENTIFIER.getTagId(), true, null ) ;
+ fail( "should never get here due to an exception" ) ;
+ }
+ catch ( IllegalArgumentException e )
+ {
+ }
+ }
+}
\ No newline at end of file
Added: directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/primitives/PrimitiveUtilsTest.java
URL: http://svn.apache.org/viewcvs/directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/primitives/PrimitiveUtilsTest.java?rev=234226&view=auto
==============================================================================
--- directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/primitives/PrimitiveUtilsTest.java (added)
+++ directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/primitives/PrimitiveUtilsTest.java Sun Aug 21 08:03:46 2005
@@ -0,0 +1,272 @@
+/*
+ * Copyright 2004 The Apache Software Foundation
+ *
+ * Licensed 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.asn1.ber.primitives ;
+
+
+import junit.framework.TestCase ;
+
+import java.math.BigInteger ;
+
+import org.apache.commons.lang.ArrayUtils;
+import org.apache.asn1.ber.primitives.PrimitiveUtils;
+
+
+/**
+ * Tests the PrimitiveUtil methods.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev: 157644 $
+ */
+public class PrimitiveUtilsTest extends TestCase
+{
+ public static byte[] bites0 = { ( byte ) 0x96, 0x46 } ;
+ public static byte[] bites1 = { ( byte ) 0x76, 0x46 } ;
+ public static byte[] bites2 = { ( byte ) 0x96 } ;
+ public static byte[] bites3 = { ( byte ) 0x46 } ;
+ public static byte[] bites4 = { ( byte ) 0x46, 0x34, 0x12 } ;
+ public static byte[] bites5 = { ( byte ) 0x96, 0x34, 0x12 } ;
+ public static byte[] bites6 = { ( byte ) 0x4F, 0x46, 0x34, 0x12 } ;
+ public static byte[] bites7 = { ( byte ) 0xFF, 0x26, 0x34, 0x12 } ;
+
+ public static byte[][] byteArrays = {
+ bites0, bites1, bites2, bites3, bites4, bites5, bites6, bites7
+ } ;
+
+ public static int[] values = {
+ ( new BigInteger( bites0 ) ).intValue(),
+ ( new BigInteger( bites1 ) ).intValue(),
+ ( new BigInteger( bites2 ) ).intValue(),
+ ( new BigInteger( bites3 ) ).intValue(),
+ ( new BigInteger( bites4 ) ).intValue(),
+ ( new BigInteger( bites5 ) ).intValue(),
+ ( new BigInteger( bites6 ) ).intValue(),
+ ( new BigInteger( bites7 ) ).intValue()
+ };
+
+
+ /**
+ * Tests the PrimitiveUtils.decodeInt(byte[], int, int) method.
+ * Uses the BigInteger class to verify correct encoding because
+ * that's what a BigInteger uses.
+ */
+ public void testDecodeInt()
+ {
+ byte[] bites = new byte[1];
+ bites[0] = (byte)0x80;
+ assertEquals( -128, PrimitiveUtils.decodeInt( bites, 0, 1 ) );
+
+ bites = new byte[2];
+ bites[0] = 0;
+ bites[1] = (byte)0x80;
+ assertEquals( 128, PrimitiveUtils.decodeInt( bites, 0, 2 ) );
+
+ bites = new byte[1];
+ bites[0] = (byte)0x80;
+ assertEquals( -128, PrimitiveUtils.decodeInt( bites, 0, 1 ) );
+
+ assertEquals( 0, PrimitiveUtils.decodeInt( null, 0, 0 ) ) ;
+
+ for ( int ii = 0; ii < byteArrays.length; ii++ )
+ {
+ int value = PrimitiveUtils.decodeInt( byteArrays[ii], 0,
+ byteArrays[ii].length ) ;
+ assertEquals( values[ii], value ) ;
+ }
+
+ try
+ {
+ PrimitiveUtils.decodeInt( bites7, 0, -1 ) ;
+ fail( "should never get here due to an exception" ) ;
+ }
+ catch( IllegalArgumentException e )
+ {
+ assertNotNull( e ) ;
+ }
+ }
+
+
+ public void testEncodeInt()
+ {
+ byte[] encoded = PrimitiveUtils.encodeInt( 0 );
+ byte[] actual = new BigInteger( "0" ).toByteArray();
+ assertTrue( ArrayUtils.isEquals( actual, encoded ) );
+ assertEquals( 0, PrimitiveUtils.decodeInt( encoded, 0, 1 ) );
+
+ encoded = PrimitiveUtils.encodeInt( -1 );
+ actual = new BigInteger( "-1" ).toByteArray();
+ assertTrue( ArrayUtils.isEquals( actual, encoded ) );
+ assertEquals( -1, PrimitiveUtils.decodeInt( encoded, 0, 1 ) );
+
+ encoded = PrimitiveUtils.encodeInt( 1 );
+ actual = new BigInteger( "1" ).toByteArray();
+ assertTrue( ArrayUtils.isEquals( actual, encoded ) );
+ assertEquals( 1, PrimitiveUtils.decodeInt( encoded, 0, 1 ) );
+
+ encoded = PrimitiveUtils.encodeInt( -100 );
+ actual = new BigInteger( "-100" ).toByteArray();
+ assertTrue( ArrayUtils.isEquals( actual, encoded ) );
+ assertEquals( -100, PrimitiveUtils.decodeInt( encoded, 0, 1 ) );
+
+ encoded = PrimitiveUtils.encodeInt( 100 );
+ actual = new BigInteger( "100" ).toByteArray();
+ assertTrue( ArrayUtils.isEquals( actual, encoded ) );
+ assertEquals( 100, PrimitiveUtils.decodeInt( encoded, 0, 1 ) );
+
+ encoded = PrimitiveUtils.encodeInt( -128 );
+ actual = new BigInteger( "-128" ).toByteArray();
+ assertTrue( ArrayUtils.isEquals( actual, encoded ) );
+ assertEquals( -128, PrimitiveUtils.decodeInt( encoded, 0, 1 ) );
+
+ encoded = PrimitiveUtils.encodeInt( 127 );
+ actual = new BigInteger( "127" ).toByteArray();
+ assertTrue( ArrayUtils.isEquals( actual, encoded ) );
+ assertEquals( 127, PrimitiveUtils.decodeInt( encoded, 0, 1 ) );
+
+ // --------------------------------------------------------------------
+ // TWO BYTES
+ // --------------------------------------------------------------------
+
+ encoded = PrimitiveUtils.encodeInt( 128 );
+ actual = new BigInteger( "128" ).toByteArray();
+ assertTrue( ArrayUtils.isEquals( actual, encoded ) );
+ assertEquals( 128, PrimitiveUtils.decodeInt( encoded, 0, 2 ) );
+
+ encoded = PrimitiveUtils.encodeInt( -129 );
+ actual = new BigInteger( "-129" ).toByteArray();
+ assertTrue( ArrayUtils.isEquals( actual, encoded ) );
+ assertEquals( -129, PrimitiveUtils.decodeInt( encoded, 0, 2 ) );
+
+ encoded = PrimitiveUtils.encodeInt( 129 );
+ actual = new BigInteger( "129" ).toByteArray();
+ assertTrue( ArrayUtils.isEquals( actual, encoded ) );
+ assertEquals( 129, PrimitiveUtils.decodeInt( encoded, 0, 2 ) );
+
+ encoded = PrimitiveUtils.encodeInt( -1000 );
+ actual = new BigInteger( "-1000" ).toByteArray();
+ assertTrue( ArrayUtils.isEquals( actual, encoded ) );
+ assertEquals( -1000, PrimitiveUtils.decodeInt( encoded, 0, 2 ) );
+
+ encoded = PrimitiveUtils.encodeInt( 1000 );
+ actual = new BigInteger( "1000" ).toByteArray();
+ assertTrue( ArrayUtils.isEquals( actual, encoded ) );
+ assertEquals( 1000, PrimitiveUtils.decodeInt( encoded, 0, 2 ) );
+
+ encoded = PrimitiveUtils.encodeInt( -(1<<15) );
+ actual = new BigInteger( new Integer(-(1<<15))
+ .toString() ).toByteArray();
+ assertTrue( ArrayUtils.isEquals( actual, encoded ) );
+ assertEquals( -(1<<15), PrimitiveUtils.decodeInt( encoded, 0, 2 ) );
+
+ encoded = PrimitiveUtils.encodeInt( (1<<15)-1 );
+ actual = new BigInteger( new Integer((1<<15)-1)
+ .toString() ).toByteArray();
+ assertTrue( ArrayUtils.isEquals( actual, encoded ) );
+ assertEquals( (1<<15)-1, PrimitiveUtils.decodeInt( encoded, 0, 2 ) );
+
+
+ // --------------------------------------------------------------------
+ // THREE BYTES
+ // --------------------------------------------------------------------
+
+ encoded = PrimitiveUtils.encodeInt( (1<<15) );
+ actual = new BigInteger( new Integer((1<<15))
+ .toString() ).toByteArray();
+ assertTrue( ArrayUtils.isEquals( actual, encoded ) );
+ assertEquals( (1<<15), PrimitiveUtils.decodeInt( encoded, 0, 3 ) );
+
+ encoded = PrimitiveUtils.encodeInt( (-(1<<15))-1 );
+ actual = new BigInteger( new Integer((-(1<<15))-1)
+ .toString() ).toByteArray();
+ assertTrue( ArrayUtils.isEquals( actual, encoded ) );
+ assertEquals( (-(1<<15))-1, PrimitiveUtils.decodeInt( encoded, 0, 3 ) );
+
+ encoded = PrimitiveUtils.encodeInt( (1<<15)+1000 );
+ actual = new BigInteger( new Integer((1<<15)+1000)
+ .toString() ).toByteArray();
+ assertTrue( ArrayUtils.isEquals( actual, encoded ) );
+ assertEquals( (1<<15)+1000, PrimitiveUtils.decodeInt( encoded, 0, 3 ) );
+
+ encoded = PrimitiveUtils.encodeInt( (-(1<<15))-1000 );
+ actual = new BigInteger( new Integer((-(1<<15))-1000)
+ .toString() ).toByteArray();
+ assertTrue( ArrayUtils.isEquals( actual, encoded ) );
+ assertEquals( (-(1<<15))-1000, PrimitiveUtils
+ .decodeInt( encoded, 0, 3 ) );
+
+ encoded = PrimitiveUtils.encodeInt( (1<<23)-1 );
+ actual = new BigInteger( new Integer((1<<23)-1)
+ .toString() ).toByteArray();
+ assertTrue( ArrayUtils.isEquals( actual, encoded ) );
+ assertEquals( (1<<23)-1, PrimitiveUtils.decodeInt( encoded, 0, 3 ) );
+
+ encoded = PrimitiveUtils.encodeInt( (-(1<<23) ));
+ actual = new BigInteger( new Integer(-(1<<23))
+ .toString() ).toByteArray();
+ assertTrue( ArrayUtils.isEquals( actual, encoded ) );
+ assertEquals( -(1<<23), PrimitiveUtils.decodeInt( encoded, 0, 3 ) );
+
+ // --------------------------------------------------------------------
+ // FOUR BYTES
+ // --------------------------------------------------------------------
+
+ encoded = PrimitiveUtils.encodeInt( (1<<23) );
+ actual = new BigInteger( new Integer((1<<23))
+ .toString() ).toByteArray();
+ assertTrue( ArrayUtils.isEquals( actual, encoded ) );
+ assertEquals( (1<<23), PrimitiveUtils.decodeInt( encoded, 0, 4 ) );
+
+ encoded = PrimitiveUtils.encodeInt( (-(1<<23))-1 );
+ actual = new BigInteger( new Integer((-(1<<23))-1)
+ .toString() ).toByteArray();
+ assertTrue( ArrayUtils.isEquals( actual, encoded ) );
+ assertEquals( (-(1<<23))-1, PrimitiveUtils.decodeInt( encoded, 0, 4 ) );
+
+ encoded = PrimitiveUtils.encodeInt( (1<<23) + 10000 );
+ actual = new BigInteger( new Integer((1<<23) + 10000 )
+ .toString() ).toByteArray();
+ assertTrue( ArrayUtils.isEquals( actual, encoded ) );
+ assertEquals( (1<<23) + 10000,
+ PrimitiveUtils.decodeInt( encoded, 0, 4 ) );
+
+ encoded = PrimitiveUtils.encodeInt( (-(1<<23))-10000 );
+ actual = new BigInteger( new Integer((-(1<<23))-10000 )
+ .toString() ).toByteArray();
+ assertTrue( ArrayUtils.isEquals( actual, encoded ) );
+ assertEquals( (-(1<<23))-10000,
+ PrimitiveUtils.decodeInt( encoded, 0, 4 ) );
+
+ encoded = PrimitiveUtils.encodeInt( Integer.MAX_VALUE );
+ actual = new BigInteger( new Integer( Integer.MAX_VALUE )
+ .toString() ).toByteArray();
+ assertTrue( ArrayUtils.isEquals( actual, encoded ) );
+ assertEquals( Integer.MAX_VALUE,
+ PrimitiveUtils.decodeInt( encoded, 0, 4 ) );
+
+ encoded = PrimitiveUtils.encodeInt( Integer.MIN_VALUE );
+ actual = new BigInteger( new Integer( Integer.MIN_VALUE )
+ .toString() ).toByteArray();
+ assertTrue( ArrayUtils.isEquals( actual, encoded ) );
+ assertEquals( Integer.MIN_VALUE,
+ PrimitiveUtils.decodeInt( encoded, 0, 4 ) );
+
+ for ( int ii = 0; ii < values.length; ii++ )
+ {
+ encoded = PrimitiveUtils.encodeInt( values[ii] ) ;
+ assertTrue( ArrayUtils.isEquals( byteArrays[ii], encoded ) );
+ }
+ }
+}
\ No newline at end of file
|