GlueGen: src/junit/com/jogamp/common/util/TestIntObjectHashMap.java Source File

GlueGen v2.6.0-rc-20250721 GlueGen, Native Binding Generator for Java™ (public API).

TestIntObjectHashMap.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.HashMap;
36import java.util.Iterator;
37import java.util.Map.Entry;
38
39import org.junit.BeforeClass;
40import org.junit.Test;
41
42import com.jogamp.common.os.Platform;
43import com.jogamp.junit.util.SingletonJunitCase;
44
45import static org.junit.Assert.*;
46
47/**
48 *
49 * @author Michael Bien
50 * @author Sven Gothel
51 */
52import org.junit.FixMethodOrder;
53import org.junit.runners.MethodSorters;
54
55@FixMethodOrder(MethodSorters.NAME_ASCENDING)
56public class TestIntObjectHashMap extends SingletonJunitCase {
57
58 private static int iterations;
59 private static IntIntObjUniqueRndValues pairs;
60
61 @BeforeClass
62 public static void init() {
63 iterations = ( Platform.getCPUType().family == Platform.CPUFamily.ARM ) ? 20 : 10000;
64 pairs = new IntIntObjUniqueRndValues(iterations);
65 }
66
67 /**
68 * Test of put method, of class IntObjectHashMap.
69 */
70 @Test
71 public void testPutRemove() {
72
73 final IntObjectHashMap intmap = new IntObjectHashMap();
74 final HashMap<Integer, IntCloneable> map = new HashMap<Integer, IntCloneable>();
75
76 // put
77 for (int i = 0; i < iterations; i++) {
78 intmap.put(pairs.keys[i], pairs.values[i]);
79
80 assertTrue(intmap.containsValue(pairs.values[i]));
81 assertTrue(intmap.containsKey(pairs.keys[i]));
82 }
83
84 for (int i = 0; i < iterations; i++) {
85 map.put(pairs.keys[i], pairs.values[i]);
86 }
87
88 assertEquals(map.size(), intmap.size());
89
90 for (final Entry<Integer, IntCloneable> entry : map.entrySet()) {
91 assertTrue(intmap.containsKey(entry.getKey()));
92 assertTrue(intmap.containsValue(entry.getValue()));
93 }
94
95 int i = 0;
96 for (final Entry<Integer, IntCloneable> entry : map.entrySet()) {
97 assertEquals(entry.getValue(), intmap.remove(entry.getKey()));
98 assertEquals(map.size() - i - 1, intmap.size());
99 i++;
100 }
101
102 }
103
104 @Test
105 public void iteratorTest() {
106
107 final IntObjectHashMap intmap = new IntObjectHashMap(iterations);
108
109 for (int i = 0; i < iterations; i++) {
110 intmap.put(pairs.keys[i], pairs.values[i]);
111 }
112
113 final Iterator<IntObjectHashMap.Entry> iterator = intmap.iterator();
114 assertNotNull(iterator);
115 assertTrue(iterator.hasNext());
116
117 int n = 0;
118 while (iterator.hasNext()) {
119 final IntObjectHashMap.Entry entry = iterator.next();
120 assertNotNull(entry);
121 n++;
122 }
123 assertEquals(intmap.size(), n);
124
125// out.println(intmap);
126
127 }
128
129 @Test
130 public void cloneTest() {
131
132 final IntObjectHashMap intmap = new IntObjectHashMap(iterations);
133
134 for (int i = 0; i < iterations; i++) {
135 intmap.put(pairs.keys[i], pairs.values[i]);
136 }
137
138 final IntObjectHashMap intmapCopy = (IntObjectHashMap) intmap.clone();
139
140 assertEquals(intmap.size(), intmapCopy.size());
141 assertEquals(intmap.getKeyNotFoundValue(), intmapCopy.getKeyNotFoundValue());
142
143 final Iterator<IntObjectHashMap.Entry> iterator = intmap.iterator();
144 assertNotNull(iterator);
145 assertTrue(iterator.hasNext());
146
147 final Iterator<IntObjectHashMap.Entry> iteratorCopy = intmapCopy.iterator();
148 assertNotNull(iteratorCopy);
149 assertTrue(iteratorCopy.hasNext());
150
151 int n = 0;
152 while (iterator.hasNext()) {
153 assertTrue(iteratorCopy.hasNext());
154 final IntObjectHashMap.Entry entry = iterator.next();
155 final IntObjectHashMap.Entry entryCopy = iteratorCopy.next();
156 assertNotNull(entry);
157 assertNotNull(entryCopy);
158 assertEquals(entry.key, entryCopy.key);
159 assertEquals(entry.value, entryCopy.value);
160 n++;
161 }
162 assertTrue(!iteratorCopy.hasNext());
163
164 assertEquals(intmap.size(), n);
165 assertEquals(intmapCopy.size(), n);
166
167 for (int i = 0; i < iterations; i++) {
168 assertTrue(intmap.containsValue(pairs.values[i]));
169 assertTrue(intmap.containsKey(pairs.keys[i]));
170 assertTrue(intmapCopy.containsValue(pairs.values[i]));
171 assertTrue(intmapCopy.containsKey(pairs.keys[i]));
172 }
173
174// out.println(intmap);
175
176 }
177
178 public static void main(final String args[]) throws IOException {
179 org.junit.runner.JUnitCore.main(TestIntObjectHashMap.class.getName());
180 }
181
182}
com.jogamp.common.os.Platform
Utility class for querying platform specific properties.
Definition: Platform.java:58
com.jogamp.common.os.Platform.getCPUType
static CPUType getCPUType()
Returns the CPU architecture type.
Definition: Platform.java:415
com.jogamp.common.util.IntIntObjUniqueRndValues
Definition: IntIntObjUniqueRndValues.java:37
com.jogamp.common.util.IntIntObjUniqueRndValues.keys
int[] keys
Definition: IntIntObjUniqueRndValues.java:39
com.jogamp.common.util.IntIntObjUniqueRndValues.values
IntCloneable[] values
Definition: IntIntObjUniqueRndValues.java:40
com.jogamp.common.util.IntObjectHashMap.Entry
An entry mapping a key to a value.
Definition: IntObjectHashMap.java:449
com.jogamp.common.util.IntObjectHashMap
Fast HashMap for primitive data.
Definition: IntObjectHashMap.java:62
com.jogamp.common.util.IntObjectHashMap.iterator
Iterator< Entry > iterator()
Returns a new Iterator.
Definition: IntObjectHashMap.java:348
com.jogamp.common.util.IntObjectHashMap.put
Object put(final int key, final Object value)
Maps the key to the specified value.
Definition: IntObjectHashMap.java:242
com.jogamp.common.util.IntObjectHashMap.containsKey
boolean containsKey(final int key)
Definition: IntObjectHashMap.java:210
com.jogamp.common.util.IntObjectHashMap.remove
Object remove(final int key)
Removes the key-value mapping from this map.
Definition: IntObjectHashMap.java:298
com.jogamp.common.util.IntObjectHashMap.getKeyNotFoundValue
Object getKeyNotFoundValue()
Returns the value which is returned if no value has been found for the specified key.
Definition: IntObjectHashMap.java:372
com.jogamp.common.util.IntObjectHashMap.containsValue
boolean containsValue(final Object value)
Definition: IntObjectHashMap.java:190
com.jogamp.common.util.IntObjectHashMap.clone
Object clone()
Disclaimer: If the value type doesn't implement clone(), only the reference is copied.
Definition: IntObjectHashMap.java:158
com.jogamp.common.util.TestIntObjectHashMap
Definition: TestIntObjectHashMap.java:56
com.jogamp.common.util.TestIntObjectHashMap.iteratorTest
void iteratorTest()
Definition: TestIntObjectHashMap.java:105
com.jogamp.common.util.TestIntObjectHashMap.main
static void main(final String args[])
Definition: TestIntObjectHashMap.java:178
com.jogamp.common.util.TestIntObjectHashMap.cloneTest
void cloneTest()
Definition: TestIntObjectHashMap.java:130
com.jogamp.common.util.TestIntObjectHashMap.testPutRemove
void testPutRemove()
Test of put method, of class IntObjectHashMap.
Definition: TestIntObjectHashMap.java:71
com.jogamp.common.util.TestIntObjectHashMap.init
static void init()
Definition: TestIntObjectHashMap.java:62
com.jogamp.junit.util.SingletonJunitCase
Definition: SingletonJunitCase.java:39
com.jogamp.common.os.Platform.CPUType.family
final CPUFamily family
Definition: Platform.java:124