You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@avalon.apache.org by bl...@apache.org on 2002/02/27 14:31:41 UTC

cvs commit: jakarta-avalon-excalibur/src/test/org/apache/avalon/excalibur/collections/test BucketMapTestCase.java

bloritsch    02/02/27 05:31:40

  Modified:    src/java/org/apache/avalon/excalibur/collections
                        BucketMap.java
  Added:       src/test/org/apache/avalon/excalibur/collections/test
                        BucketMapTestCase.java
  Log:
  Speed up BucketMap power of two test and provide TestCase from Vadim Gritsenko
  
  Revision  Changes    Path
  1.10      +3 -13     jakarta-avalon-excalibur/src/java/org/apache/avalon/excalibur/collections/BucketMap.java
  
  Index: BucketMap.java
  ===================================================================
  RCS file: /home/cvs/jakarta-avalon-excalibur/src/java/org/apache/avalon/excalibur/collections/BucketMap.java,v
  retrieving revision 1.9
  retrieving revision 1.10
  diff -u -r1.9 -r1.10
  --- BucketMap.java	26 Feb 2002 22:12:45 -0000	1.9
  +++ BucketMap.java	27 Feb 2002 13:31:40 -0000	1.10
  @@ -19,7 +19,7 @@
    *
    * @author  <a href="bloritsch@apache.org">Berin Loritsch</a>
    * @author  <a href="g-froehlich@gmx.de">Gerhard Froehlich</a>
  - * @version CVS $Revision: 1.9 $ $Date: 2002/02/26 22:12:45 $
  + * @version CVS $Revision: 1.10 $ $Date: 2002/02/27 13:31:40 $
    * @since 4.0
    */
   public final class BucketMap implements Map
  @@ -40,19 +40,9 @@
           int size = Math.max( 17, numBuckets );
   
           // Ensure that bucketSize is never a power of 2 (to ensure maximal distribution)
  -        for ( int i = 0; i < 32; i++ )
  +        if ( size % 2 == 0 )
           {
  -            if ( ( 2 ^ i ) < size )
  -            {
  -                continue;
  -            }
  -
  -            if ( ( 2 ^ i ) == size )
  -            {
  -                size  = ( 2 ^ i ) - 1;
  -            }
  -
  -            break;
  +            size--;
           }
   
           m_buckets = new Node[size];
  
  
  
  1.1                  jakarta-avalon-excalibur/src/test/org/apache/avalon/excalibur/collections/test/BucketMapTestCase.java
  
  Index: BucketMapTestCase.java
  ===================================================================
  /*
   * Copyright (C) The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software License
   * version 1.1, a copy of which has been included with this distribution in
   * the LICENSE.txt file.
   */
  package org.apache.avalon.excalibur.collections.test;
  
  import junit.framework.TestCase;
  import org.apache.avalon.excalibur.collections.BucketMap;
  
  /**
   *
   * @author <a href="mailto:vgritsenko@apache.org">Vadim Gritsenko</a>
   */
  public final class BucketMapTestCase
      extends TestCase
  {
  
      private static class TestInteger
      {
          int i;
  
          public TestInteger ( int i )
          {
              this.i = i ;
          }
  
          public boolean equals (Object o)
          {
              return this == o;
          }
  
          public int hashCode ()
          {
              return i;
          }
  
          public String toString ()
          {
              return "TestInteger " + i + " @" + System.identityHashCode(this);
          }
      }
  
      private final static TestInteger VAL1 = new TestInteger( 5 );
      private final static TestInteger VAL2 = new TestInteger( 5 );
      private final static TestInteger VAL3 = new TestInteger( 5 );
      private final static TestInteger VAL4 = new TestInteger( 5 );
      private final static TestInteger VAL5 = new TestInteger( 5 );
      private final static TestInteger VAL6 = new TestInteger( 5 );
      private final static TestInteger VAL7 = new TestInteger( 5 );
  
      public BucketMapTestCase()
      {
          this("Bucket Map Test Case");
      }
  
      public BucketMapTestCase( String name )
      {
          super( name );
      }
  
  
      public void testBucket()
      {
          final BucketMap map = new BucketMap();
  
          map.put( VAL1, VAL1 );
          assertTrue( map.size() == 1 );
          assertTrue( VAL1 == map.get( VAL1 ) );
  
          map.put( VAL2, VAL2 );
          assertTrue( map.size() == 2 );
          assertTrue( VAL1 == map.get( VAL1 ) );
          assertTrue( VAL2 == map.get( VAL2 ) );
  
          map.put( VAL3, VAL3 );
          assertTrue( map.size() == 3 );
          assertTrue( VAL1 == map.get( VAL1 ) );
          assertTrue( VAL2 == map.get( VAL2 ) );
          assertTrue( VAL3 == map.get( VAL3 ) );
  
          map.put( VAL4, VAL4 );
          assertTrue( map.size() == 4 );
          assertTrue( VAL1 == map.get( VAL1 ) );
          assertTrue( VAL2 == map.get( VAL2 ) );
          assertTrue( VAL3 == map.get( VAL3 ) );
          assertTrue( VAL4 == map.get( VAL4 ) );
  
          map.put( VAL5, VAL5 );
          assertTrue( map.size() == 5 );
          assertTrue( VAL1 == map.get( VAL1 ) );
          assertTrue( VAL2 == map.get( VAL2 ) );
          assertTrue( VAL3 == map.get( VAL3 ) );
          assertTrue( VAL4 == map.get( VAL4 ) );
          assertTrue( VAL5 == map.get( VAL5 ) );
  
          map.put( VAL6, VAL6 );
          assertTrue( map.size() == 6 );
          assertTrue( VAL1 == map.get( VAL1 ) );
          assertTrue( VAL2 == map.get( VAL2 ) );
          assertTrue( VAL3 == map.get( VAL3 ) );
          assertTrue( VAL4 == map.get( VAL4 ) );
          assertTrue( VAL5 == map.get( VAL5 ) );
          assertTrue( VAL6 == map.get( VAL6 ) );
  
          map.put( VAL7, VAL7 );
          assertTrue( map.size() == 7 );
          assertTrue( VAL1 == map.get( VAL1 ) );
          assertTrue( VAL2 == map.get( VAL2 ) );
          assertTrue( VAL3 == map.get( VAL3 ) );
          assertTrue( VAL4 == map.get( VAL4 ) );
          assertTrue( VAL5 == map.get( VAL5 ) );
          assertTrue( VAL6 == map.get( VAL6 ) );
          assertTrue( VAL7 == map.get( VAL7 ) );
  
          map.remove( VAL1 );
          assertTrue( map.size() == 6 );
          assertTrue( map.get( VAL1 ) == null );
  
          map.remove( VAL7 );
          assertTrue( map.size() == 5 );
          assertTrue( map.get( VAL7 ) == null );
  
          map.remove( VAL4 );
          assertTrue( map.size() == 4 );
          assertTrue( map.get( VAL4 ) == null );
      }
  }
  
  
  

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>