GlueGen v2.6.0-rc-20250712
GlueGen, Native Binding Generator for Java™ (public API).
TestLongIntHashMap.java
Go to the documentation of this file.
1/**
2 * Copyright 2010 JogAmp Community. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification, are
5 * permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice, this list of
8 * conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
11 * of conditions and the following disclaimer in the documentation and/or other materials
12 * provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED
15 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
21 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
22 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 *
24 * The views and conclusions contained in the software and documentation are those of the
25 * authors and should not be interpreted as representing official policies, either expressed
26 * or implied, of JogAmp Community.
27 */
28
29/**
30 * Created on Sunday, March 28 2010 21:01
31 */
32package com.jogamp.common.util;
33
34import java.io.IOException;
35import java.util.Iterator;
36import java.util.HashMap;
37import java.util.Map.Entry;
38
39import org.junit.BeforeClass;
40import org.junit.Test;
41
42import com.jogamp.common.os.Clock;
43import com.jogamp.common.os.Platform;
44import com.jogamp.junit.util.SingletonJunitCase;
45
46import static org.junit.Assert.*;
47import static java.lang.System.*;
48
49/**
50 *
51 * @author Michael Bien
52 * @author Simon Goller
53 */
54import org.junit.FixMethodOrder;
55import org.junit.runners.MethodSorters;
56
57@FixMethodOrder(MethodSorters.NAME_ASCENDING)
59
60 private static int iterations;
61 private static LongIntUniqueRndValues pairs;
62
63 @BeforeClass
64 public static void init() {
65 iterations = ( Platform.getCPUFamily() == Platform.CPUFamily.ARM ) ? 20 : 10000;
66 pairs = new LongIntUniqueRndValues(iterations);
67 }
68
69 /**
70 * Test of put method, of class LongIntHashMap.
71 */
72 @Test
73 public void testPutRemove() {
74
75 final LongIntHashMap intmap = new LongIntHashMap();
76 final HashMap<Long, Integer> map = new HashMap<Long, Integer>();
77
78 // put
79 for (int i = 0; i < iterations; i++) {
80 intmap.put(pairs.keys[i], pairs.values[i]);
81
82 assertTrue(intmap.containsValue(pairs.values[i]));
83 assertTrue(intmap.containsKey(pairs.keys[i]));
84 }
85
86 for (int i = 0; i < iterations; i++) {
87 map.put(pairs.keys[i], pairs.values[i]);
88 }
89
90 assertEquals(map.size(), intmap.size());
91
92 for (final Entry<Long, Integer> entry : map.entrySet()) {
93 assertTrue(intmap.containsKey(entry.getKey()));
94 assertTrue(intmap.containsValue(entry.getValue()));
95 }
96
97 int i = 0;
98 for (final Entry<Long, Integer> entry : map.entrySet()) {
99 assertEquals((int)entry.getValue(), intmap.remove(entry.getKey()));
100 assertEquals(map.size() - i - 1, intmap.size());
101 i++;
102 }
103
104 }
105
106 @Test
107 public void iteratorTest() {
108
109 final LongIntHashMap map = new LongIntHashMap(iterations);
110
111 for (int i = 0; i < iterations; i++) {
112 map.put(pairs.keys[i], pairs.values[i]);
113 }
114
115 final Iterator<LongIntHashMap.Entry> iterator = map.iterator();
116 assertNotNull(iterator);
117 assertTrue(iterator.hasNext());
118
119 int n = 0;
120 while (iterator.hasNext()) {
121 final LongIntHashMap.Entry entry = iterator.next();
122 assertNotNull(entry);
123 n++;
124 }
125 assertEquals(map.size(), n);
126
127// out.println(intmap);
128
129 }
130
131 @Test
132 public void benchmark() {
133 benchmark(true);
134 benchmark(false);
135 }
136
137 void benchmark(final boolean warmup) {
138
139 // simple benchmark
140 final LongIntHashMap intmap = new LongIntHashMap(1024);
141 final HashMap<Long, Integer> map = new HashMap<Long, Integer>(1024);
142
143 out.println(intmap.getClass().getName()+" vs "+map.getClass().getName()+
144 " warmup: " + warmup);
145
146 out.println("put");
147 long time = Clock.currentNanos();
148 for (int i = 0; i < iterations; i++) {
149 intmap.put(pairs.keys[i], pairs.values[i]);
150 }
151 final long intmapPutTime = (Clock.currentNanos() - time);
152 out.println(" iimap: " + intmapPutTime/1000000.0f+"ms");
153
154
155 time = Clock.currentNanos();
156 for (int i = 0; i < iterations; i++) {
157 map.put(pairs.keys[i], pairs.values[i]);
158 }
159 final long mapPutTime = (Clock.currentNanos() - time);
160 out.println(" map: " + mapPutTime/1000000.0f+"ms");
161
162
163 System.out.println();
164 System.out.println("get");
165 time = Clock.currentNanos();
166 for (int i = 0; i < iterations; i++) {
167 intmap.get(pairs.keys[i]);
168 }
169 final long intmapGetTime = (Clock.currentNanos() - time);
170 out.println(" iimap: " + intmapGetTime/1000000.0f+"ms");
171
172 time = Clock.currentNanos();
173 for (int i = 0; i < iterations; i++) {
174 map.get(pairs.keys[i]);
175 }
176 final long mapGetTime = (Clock.currentNanos() - time);
177 out.println(" map: " + mapGetTime/1000000.0f+"ms");
178
179
180 out.println();
181 out.println("remove");
182 time = Clock.currentNanos();
183 for (int i = 0; i < iterations; i++) {
184 intmap.remove(pairs.keys[i]);
185 }
186 assertEquals(0, intmap.size());
187 final long intmapRemoveTime = (Clock.currentNanos() - time);
188 out.println(" iimap: " + intmapRemoveTime/1000000.0f+"ms");
189
190 time = Clock.currentNanos();
191 for (int i = 0; i < iterations; i++) {
192 map.remove(pairs.keys[i]);
193 }
194 assertEquals(0, map.size());
195 final long mapRemoveTime = (Clock.currentNanos() - time);
196 out.println(" map: " + mapRemoveTime/1000000.0f+"ms");
197
198 if(!warmup) {
199 // In case the 1st class map magically improves
200 // we add a tolerance around 50% since this would be hardly a bug.
201 // The main goal of this primitve map is memory efficiency.
202 // high and not O(1) assertTrue("'put' too slow", intmapPutTime <= mapPutTime + mapPutTime/4 );
203 assertTrue("'get' too slow", intmapGetTime <= mapGetTime + mapGetTime/2 );
204 assertTrue("'remove' too slow", intmapRemoveTime <= mapRemoveTime + mapRemoveTime/2 );
205 }
206 }
207
208 public static void main(final String args[]) throws IOException {
209 org.junit.runner.JUnitCore.main(TestLongIntHashMap.class.getName());
210 }
211
212}
static native long currentNanos()
Returns current monotonic nanoseconds since start of this application.
Utility class for querying platform specific properties.
Definition: Platform.java:58
static CPUFamily getCPUFamily()
Returns the CPU family.
Definition: Platform.java:408
An entry mapping a key to a value.
Fast HashMap for primitive data.
int get(final long key)
Returns the value to which the specified key is mapped, or getKeyNotFoundValue if this map contains n...
Iterator< Entry > iterator()
Returns a new Iterator.
boolean containsValue(final int value)
int remove(final long key)
Removes the key-value mapping from this map.
int put(final long key, final int value)
Maps the key to the specified value.
void testPutRemove()
Test of put method, of class LongIntHashMap.
static void main(final String args[])