Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (480)
Games in Android Showcase (110)
games submitted by our members
Games in WIP (547)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
  JavaGaming.org - Pastebin



Author: ClickerMonkey (posted 2013-02-15 12:02:23, viewed 218 times)

1   2   3   4   5   6   7   8   9   10   11   12   13   14   15   16   17   18   19   20   21   22   23   24   25   26   27   28   29   30   31   32   33   34   35   36   37   38   39   40   41   42   43   44   45   46   47   48   49   50   51   52   53   54   55   56   57   58   59   60   61   62   63   64   65   66   67   68   69   70   71   72   73   74   75   76   77   78   79   80   81   82   83   84   85   86   87   88   89   90   91   92   93   94   95   96   97   98   99   100   101   102   103   104   105   106   107   108   109   110   111   112   113   114   115   116   117   118   119   120   121   122   123   124   125   126   127   128   129   130   131   132   133   134   135   136   137   138   139   140   141   142   143   144   145   146   147   148   149   150   151   152   153   154   155   156   157   158   159   160   161   162   163   164   165   166   167   168   169   170   171   172   173   174   175   176   177   178   179   180   181   182   183   184   185   186   187   188   189   190   191   192   193   194   195   196   197   198   199   200   201   202   203   204   205   206   207   208   209   210   211   212   213   214   215   216   217   218   219   220   221   222   223   224   225   226   227   228   229   230   231   232   233   234   235   236   237   238   239   240   241   242   243   244   245   246   247   248   249   250   251   252   253   254   255   256   257   258   259   260   261   262   263   264   265   266   267   268   269   270   271   272   273   274   275   276   277   278   279   280   281   282   283   284   285   286   287   288   289   290   291   292   293   294   295   296   297   298   299   300   301   302   303   304   305   306   307   308   309   310   311   312   313   314   315   316   317   318   319   320   321   322   323   324   325   326   327   328   329   330   331   332   333   334   335   336   337   338   339   340   341   342   343   344   345   346   347   348   349   350   351   352   353   354   355   356   357   358   359   360   361   362   363   364   365   366   367   368   369   370   371   372   373   374   375   376   377   378   379   380   381   382   383   384   385   386   387   388   389   390   391   392   393   394   395   396   397   398   399   400   401   402   403   404   405   406   407   408   409   410   411   412   413   414   415   416   417   418   419   420   421   422   423   424   425   426   427   428   429   430   431   432   433   434   435   436   437   438   439   440   441   442   443   444   445   446   447   448   449   450   451   452   453   454   455   456   457   458   459   460   461   462   463   464   465   466   467   468   469   470   471   472   473   474   475   476   477   478   479   480   481   482   483   484   485   486   487   488   489   490   491   492   493   494   495   496   497   498   499   500   501   502   503   504   505   506   507   508   509   510   511   512   513   514   515   516   517   518   519   520   521   522   523   524   525   526   527   528   529   530   531   532   533   534   535   536   537   538   539   540   541   542   543   544   545   546   547   548   549   550   551   552   553   554   555   556   557   558   559   560   561   562   563   564   565   566   567   568   569   570   571   572   573   574   575   576   577   578   579   580   581   582   583   584   585  
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.junit.Test;

public class TestCyclicMap
{

   @Test
   public void testAddAndHas()
   {
      CyclicMap<String, Integer> map = createMap();
      
      map.add( "h", 2 );
      map.add( "w", 1 );
      
      assertEquals( 2, map.size() );
      assertFalse( map.isEmpty() );

      assertTrue( map.has( "h" ) );
      assertTrue( map.has( "w" ) );
      assertFalse( map.has( "o" ) );
      assertTrue( map.hasValue( 1 ) );
      assertTrue( map.hasValue( 2 ) );
      assertFalse( map.hasValue( 0 ) );
   }
   
   @Test
   public void testAddIfAbsent()
   {
      CyclicMap<String, Integer> map = createMap();
      
      assertTrue( map.addIfAbsent( "h", 1 ) );
      assertFalse( map.addIfAbsent( "h", 2 ) );
      
      assertEquals( (Integer)1, map.get( "h" ) );
   }
   
   @Test
   public void testGet()
   {
      CyclicMap<String, Integer> map = createMap();
      
      map.add( "h", 2 );
      map.add( "w", 1 );

      assertEquals( (Integer)2, map.get( "h" ) );
      assertEquals( (Integer)1, map.get( "w" ) );
      assertEquals( "h", map.getKey( 2 ) );
      assertEquals( "w", map.getKey( 1 ) );
   }
   
   @Test
   public void testValues()
   {
      CyclicMap<String, Integer> map = createMap();
      
      map.add( "h", 2 );
      map.add( "w", 1 );
      
      Set<Integer> expected = createCollection( new HashSet<Integer>(), 2, 1 );
      Set<Integer> actual = createCollection( new HashSet<Integer>(), map.values() );
      assertEquals( expected, actual );
   }
   
   @Test
   public void testKeys()
   {
      CyclicMap<String, Integer> map = createMap();
      
      map.add( "h", 2 );
      map.add( "w", 1 );
      
      Set<String> expected = createCollection( new HashSet<String>(), "w", "h" );
      Set<String> actual = createCollection( new HashSet<String>(), map.keys() );
      assertEquals( expected, actual );
   }
   
   @Test
   public void testKeysMultiple()
   {
      CyclicMap<String, Integer> map = createMap();
      
      map.add( "a", 1 );
      map.add( "a", 2 );
      map.add( "b", 4 );
      map.add( "a", 3 );
      map.add( "c", 5 );
      map.add( "b", 4 );
      
      // reverse insertion order
      assertEquals( 3, map.countOf( "a" ) );
      List<Integer> expected = createCollection( new ArrayList<Integer>(), 3, 2, 1 );
      List<Integer> actual = map.getAll( "a", new ArrayList<Integer>() );
      assertEquals( expected, actual );
      
      // duplicate values
      assertEquals( 2, map.countOf( "b" ) );
      expected = createCollection( new ArrayList<Integer>(), 4, 4 );
      actual = map.getAll( "b", new ArrayList<Integer>() );
      assertEquals( expected, actual );
      
      // single value
      assertEquals( 1, map.countOf( "c" ) );
      expected = createCollection( new ArrayList<Integer>(), 5 );
      actual = map.getAll( "c", new ArrayList<Integer>() );
      assertEquals( expected, actual );
      
      // no value
      assertEquals( 0, map.countOf( "d" ) );
      expected = createCollection( new ArrayList<Integer>() );
      actual = map.getAll( "d", new ArrayList<Integer>() );
      assertEquals( expected, actual );
   }
   
   @Test
   public void testValuesMultiple()
   {
      CyclicMap<String, Integer> map = createMap();
      
      map.add( "a", 1 );
      map.add( "a", 1 );
      map.add( "b", 4 );
      map.add( "a", 3 );
      map.add( "c", 1 );
      map.add( "b", 4 );
      
      // hash + reverse insertion order
      List<String> expected = createCollection( new ArrayList<String>(), "a", "a", "c" );
      List<String> actual = map.getAllKeys( 1, new ArrayList<String>() );
      assertEquals( expected, actual );
      
      // duplicate values
      expected = createCollection( new ArrayList<String>(), "b", "b" );
      actual = map.getAllKeys( 4, new ArrayList<String>() );
      assertEquals( expected, actual );
      
      // single value
      expected = createCollection( new ArrayList<String>(), "a" );
      actual = map.getAllKeys( 3, new ArrayList<String>() );
      assertEquals( expected, actual );

      // no value
      assertEquals( 0, map.countOfValue( 2 ) );
      expected = createCollection( new ArrayList<String>() );
      actual = map.getAllKeys( 2, new ArrayList<String>() );
      assertEquals( expected, actual );
   }
   
   @Test
   public void testEmpty()
   {
      CyclicMap<String, Integer> map = createMap();
      
      assertNull( map.beforeKey( "meow" ) );
      assertNull( map.beforeValue( 3 ) );
      assertEquals( 0, map.countOf( "1" ) );
      assertEquals( 0, map.countOfValue( 3 ) );
      assertFalse( map.entries().iterator().hasNext() );
      assertFalse( map.entriesWithKey( "2" ).iterator().hasNext() );
      assertFalse( map.entriesWithValue( 2 ).iterator().hasNext() );
      assertNull( map.get( "a" ) );
      assertEquals( 0, map.getAll( "a", new ArrayList<Integer>() ).size() );
      assertEquals( 0, map.getAllKeys( 2, new ArrayList<String>() ).size() );
      assertNull( map.getKey( 4 ) );
      assertFalse( map.has( "a" ) );
      assertFalse( map.hasValue( 3 ) );
      assertTrue( map.isEmpty() );
      assertFalse( map.keys().iterator().hasNext() );
      assertNull( map.remove( "a" ) );
      assertNull( map.removeFirstEntry() );
      assertNull( map.removeFirstValue() );
      assertNull( map.removeValue( 1 ) );
      assertEquals( 0, map.size() );
      assertFalse( map.values().iterator().hasNext() );
   }

   @Test
   public void testRemove()
   {
      CyclicMap<String, Integer> map = createMap();
      
      map.add( "h", 2 );
      map.add( "w", 1 );

      assertEquals( 2, map.size() );
      assertEquals( (Integer)2, map.remove( "h" ) );
      assertEquals( null, map.remove( "h" ) );
      assertEquals( 1, map.size() );

      assertEquals( (Integer)1, map.remove( "w" ) );
      assertEquals( null, map.remove( "w" ) );
      assertEquals( 0, map.size() );
      
      map.add( "h", 2 );
      map.add( "w", 1 );
      
      assertEquals( 2, map.size() );
      
      assertEquals( "w", map.removeValue( 1 ) );
      assertEquals( null, map.removeValue( 1 ) );
      
      assertEquals( 1, map.size() );

      assertEquals( "h", map.removeValue( 2 ) );
      assertEquals( null, map.removeValue( 2 ) );
      
      assertEquals( 0, map.size() );
   }

   @Test
   public void testPerformancePut()
   {
      final int ENTRY_CACHE = 1024;
      final int INSERT_ENTRIES = 1024;
      final int ITERATIONS = 2048;
      final int ITERATIONS_START_MEASURING = 2000;
      
      CyclicMap.initPool( ENTRY_CACHE );
      
      Map<String, Integer> slow = new HashMap<String, Integer>();
      CyclicMap<String, Integer> fast = createMap();
      
      String[] keys = new String[INSERT_ENTRIES];

      long t0 = 0, t1 = 0, t2 = 0, t3 = 0;
      
      for (int i = 0; i < INSERT_ENTRIES; i++)
      {
         keys[i] = "Value " + i;
      }
      
      Integer[] values = new Integer[ INSERT_ENTRIES ];
      
      for ( int i = 0; i < INSERT_ENTRIES; i++ )
      {
         values[i] = Integer.valueOf( i );
      }
      
      long measures = 0;
      long measureSlow = 0;
      long measureFast = 0;
      
      for (int i = 0; i < ITERATIONS; i++)
      {
         t0 = System.nanoTime();

         for ( int k = 0; k < INSERT_ENTRIES; k++ )
         {
            slow.put( keys[k], values[k] );
         }
         
         t1 = System.nanoTime();
         
         slow.clear();
         
         t2 = System.nanoTime();
         
         for ( int k = 0; k < INSERT_ENTRIES; k++ )
         {
            fast.put( keys[k], values[k] );
         }
         
         t3 = System.nanoTime();
         
         fast.clear( false );
         
         if ( i > ITERATIONS_START_MEASURING )
         {
            measureSlow += (t1 - t0);
            measureFast += (t3 - t2);
            measures++;
         }
      }
      
      System.out.format( "HashMap.put:        %s per entry\n", getTimeString(measureSlow, measures, INSERT_ENTRIES ) );
      System.out.format( "CyclicMap.put:      %s per entry\n", getTimeString(measureFast, measures, INSERT_ENTRIES ) );

      CyclicMap.initPool( 0 );
   }
   
   @Test
   public void testPerformanceGet()
   {
      final int ENTRY_CACHE = 1024;
      final int INSERT_ENTRIES = 1024;
      final int ITERATIONS = 2048;
      final int ITERATIONS_START_MEASURING = 2000;
      
      CyclicMap.initPool( ENTRY_CACHE );
      
      Map<String, Integer> slow = new HashMap<String, Integer>();
      CyclicMap<String, Integer> fast = createMap();
      
      String[] keys = new String[INSERT_ENTRIES];

      long t0 = 0, t1 = 0, t2 = 0, t3 = 0;
      
      for (int i = 0; i < INSERT_ENTRIES; i++)
      {
         keys[i] = "Value " + i;
      }
      
      for (int i = 0; i < INSERT_ENTRIES; i++)
      {
         fast.put( keys[i], i );
         slow.put( keys[i], i );
      }
      
      long measures = 0;
      long measureSlow = 0;
      long measureFast = 0;
      
      for (int i = 0; i < ITERATIONS; i++)
      {
         t0 = System.nanoTime();

         for ( int k = 0; k < INSERT_ENTRIES; k++ )
         {
            slow.get( keys[k] );
         }
         
         t1 = System.nanoTime();
         
         slow.clear();
         
         t2 = System.nanoTime();
         
         for ( int k = 0; k < INSERT_ENTRIES; k++ )
         {
            fast.get( keys[k] );
         }
         
         t3 = System.nanoTime();
         
         fast.clear( false );
         
         if ( i > ITERATIONS_START_MEASURING )
         {
            measureSlow += (t1 - t0);
            measureFast += (t3 - t2);
            measures++;
         }
      }
      
      System.out.format( "HashMap.get:        %s per entry\n", getTimeString(measureSlow, measures, INSERT_ENTRIES ) );
      System.out.format( "CyclicMap.get:      %s per entry\n", getTimeString(measureFast, measures, INSERT_ENTRIES ) );

      CyclicMap.initPool( 0 );
   }
   
   @Test
   @SuppressWarnings("unused")
   public void testPerformanceEntryIteration()
   {
      final int ENTRY_CACHE = 1024;
      final int INSERT_ENTRIES = 1024;
      final int ITERATIONS = 2048;
      final int ITERATIONS_START_MEASURING = 2000;
      
      CyclicMap.initPool( ENTRY_CACHE );
      
      Map<String, Integer> slow = new HashMap<String, Integer>();
      CyclicMap<String, Integer> fast = createMap();
      
      long t0 = 0, t1 = 0, t2 = 0, t3 = 0;
      
      for (int i = 0; i < INSERT_ENTRIES; i++)
      {
         fast.put( "Value " + i, i );
         slow.put( "Value " + i, i );
      }
      
      long measures = 0;
      long measureSlow = 0;
      long measureFast = 0;
      
      for (int i = 0; i < ITERATIONS; i++)
      {
         t0 = System.nanoTime();

         for (Map.Entry<String, Integer> entry : slow.entrySet() )
         {
         }
         
         t1 = System.nanoTime();
         
         slow.clear();
         
         t2 = System.nanoTime();
         
         for (CyclicMap.Entry<String, Integer> entry : fast.entries())
         {
         }
         
         t3 = System.nanoTime();
         
         fast.clear( false );
         
         if ( i > ITERATIONS_START_MEASURING )
         {
            measureSlow += (t1 - t0);
            measureFast += (t3 - t2);
            measures++;
         }
      }
      
      System.out.format( "HashMap.entrySet:   %s per entry\n", getTimeString(measureSlow, measures, INSERT_ENTRIES ) );
      System.out.format( "CyclicMap.entries:  %s per entry\n", getTimeString(measureFast, measures, INSERT_ENTRIES ) );

      CyclicMap.initPool( 0 );
   }
   
   @Test
   @SuppressWarnings("unused")
   public void testPerformanceKeyIteration()
   {
      final int ENTRY_CACHE = 1024;
      final int INSERT_ENTRIES = 1024;
      final int ITERATIONS = 2048;
      final int ITERATIONS_START_MEASURING = 2000;
      
      CyclicMap.initPool( ENTRY_CACHE );
      
      Map<String, Integer> slow = new HashMap<String, Integer>();
      CyclicMap<String, Integer> fast = createMap();
      
      long t0 = 0, t1 = 0, t2 = 0, t3 = 0;
      
      for (int i = 0; i < INSERT_ENTRIES; i++)
      {
         fast.put( "Value " + i, i );
         slow.put( "Value " + i, i );
      }
      
      long measures = 0;
      long measureSlow = 0;
      long measureFast = 0;
      
      for (int i = 0; i < ITERATIONS; i++)
      {
         t0 = System.nanoTime();

         for (String key : slow.keySet())
         {
         }
         
         t1 = System.nanoTime();
         
         slow.clear();
         
         t2 = System.nanoTime();
         
         for (String key : fast.keys())
         {
         }
         
         t3 = System.nanoTime();
         
         fast.clear( false );
         
         if ( i > ITERATIONS_START_MEASURING )
         {
            measureSlow += (t1 - t0);
            measureFast += (t3 - t2);
            measures++;
         }
      }
      
      System.out.format( "HashMap.keySet:     %s per entry\n", getTimeString(measureSlow, measures, INSERT_ENTRIES ) );
      System.out.format( "CyclicMap.keys:     %s per entry\n", getTimeString(measureFast, measures, INSERT_ENTRIES ) );

      CyclicMap.initPool( 0 );
   }
   
   @Test
   @SuppressWarnings("unused")
   public void testPerformanceValueIteration()
   {
      final int ENTRY_CACHE = 1024;
      final int INSERT_ENTRIES = 1024;
      final int ITERATIONS = 2048;
      final int ITERATIONS_START_MEASURING = 2000;
      
      CyclicMap.initPool( ENTRY_CACHE );
      
      Map<String, Integer> slow = new HashMap<String, Integer>();
      CyclicMap<String, Integer> fast = createMap();
      
      long t0 = 0, t1 = 0, t2 = 0, t3 = 0;
      
      for (int i = 0; i < INSERT_ENTRIES; i++)
      {
         fast.put( "Value " + i, i );
         slow.put( "Value " + i, i );
      }
      
      long measures = 0;
      long measureSlow = 0;
      long measureFast = 0;
      
      for (int i = 0; i < ITERATIONS; i++)
      {
         t0 = System.nanoTime();

         for (Integer value : slow.values())
         {
         }
         
         t1 = System.nanoTime();
         
         slow.clear();
         
         t2 = System.nanoTime();
         
         for (Integer value : fast.values())
         {
         }
         
         t3 = System.nanoTime();
         
         fast.clear( false );
         
         if ( i > ITERATIONS_START_MEASURING )
         {
            measureSlow += (t1 - t0);
            measureFast += (t3 - t2);
            measures++;
         }
      }
      
      System.out.format( "HashMap.values:     %s per entry\n", getTimeString(measureSlow, measures, INSERT_ENTRIES ) );
      System.out.format( "CyclicMap.values:   %s per entry\n", getTimeString(measureFast, measures, INSERT_ENTRIES ) );

      CyclicMap.initPool( 0 );
   }

   public String getTimeString(long totalTime, long times, int entries)
   {
      double entryBatch = (double)totalTime / (double)times;
      double entrySingle = entryBatch / entries;
      long nanoTime = (long)entrySingle;
      double nanosExtra = entrySingle - nanoTime;
      long nanos = (nanoTime % 1000);
      long micros = (nanoTime / 1000) % 1000;
      
      return String.format( "%d us  %03d.%03d ns", micros, nanos, (int)(nanosExtra * 1000) );
   }
   
   public CyclicMap<String, Integer> createMap()
   {
      return new CyclicMap<String, Integer>( CyclicMap.SIZE_1024 );
   }
   
   public <E, C extends Collection<E>> C createCollection( C destintation, Iterable<E> iterable )
   {
      for ( E element : iterable )
      {
         destintation.add( element );
      }
      
      return destintation;
   }
   
   public <E, C extends Collection<E>> C createCollection( C destintation, E ... elementArray )
   {
      for ( E element : elementArray )
      {
         destintation.add( element );
      }
      
      return destintation;
   }
   
}





Dump your java code here :



Special syntax:
  • To highlight a line (yellow background), prefix it with '@@'
  • To indicate that a line should be removed (red background), prefix it with '-'
  • To indicate that a line should be added (green background), prefix it with '+'
  • To post multiple snippets, seperate them by '~~~~'
  EOF
 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

atombrot (23 views)
2014-08-19 09:29:53

Tekkerue (22 views)
2014-08-16 06:45:27

Tekkerue (21 views)
2014-08-16 06:22:17

Tekkerue (12 views)
2014-08-16 06:20:21

Tekkerue (19 views)
2014-08-16 06:12:11

Rayexar (57 views)
2014-08-11 02:49:23

BurntPizza (37 views)
2014-08-09 21:09:32

BurntPizza (29 views)
2014-08-08 02:01:56

Norakomi (36 views)
2014-08-06 19:49:38

BurntPizza (66 views)
2014-08-03 02:57:17
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!