2
* Copyright 2014 Goldman Sachs.
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
8
* http://www.apache.org/licenses/LICENSE-2.0
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
17
package com.gs.collections.impl.map.strategy.immutable;
19
import java.util.NoSuchElementException;
21
import com.gs.collections.api.block.HashingStrategy;
22
import com.gs.collections.api.map.ImmutableMap;
23
import com.gs.collections.impl.block.factory.Functions;
24
import com.gs.collections.impl.block.factory.HashingStrategies;
25
import com.gs.collections.impl.block.function.PassThruFunction0;
26
import com.gs.collections.impl.map.immutable.ImmutableMemoryEfficientMapTestCase;
27
import com.gs.collections.impl.test.Verify;
28
import org.junit.Assert;
29
import org.junit.Test;
32
* JUnit test for {@link ImmutableEmptyMapWithHashingStrategy}.
34
public class ImmutableEmptyMapWithHashingStrategyTest extends ImmutableMemoryEfficientMapTestCase
36
//Not using the static factor method in order to have concrete types for test cases
37
private static final HashingStrategy<Integer> HASHING_STRATEGY = HashingStrategies.nullSafeHashingStrategy(new HashingStrategy<Integer>()
39
public int computeHashCode(Integer object)
41
return object.hashCode();
44
public boolean equals(Integer object1, Integer object2)
46
return object1.equals(object2);
51
protected ImmutableMap<Integer, String> classUnderTest()
53
return new ImmutableEmptyMapWithHashingStrategy<Integer, String>(HASHING_STRATEGY);
64
public void testToString()
66
ImmutableMap<Integer, String> map = this.classUnderTest();
67
Assert.assertEquals("{}", map.toString());
72
public void flipUniqueValues()
74
Verify.assertEmpty(this.classUnderTest().flipUniqueValues());
81
Integer absentKey = this.size() + 1;
82
String absentValue = String.valueOf(absentKey);
84
// Absent key behavior
85
ImmutableMap<Integer, String> classUnderTest = this.classUnderTest();
86
Assert.assertNull(classUnderTest.get(absentKey));
87
Assert.assertFalse(classUnderTest.containsValue(absentValue));
90
Assert.assertEquals(this.equalUnifiedMap(), classUnderTest);
95
public void getIfAbsent_function()
97
Integer absentKey = this.size() + 1;
98
String absentValue = String.valueOf(absentKey);
100
// Absent key behavior
101
ImmutableMap<Integer, String> classUnderTest = this.classUnderTest();
102
Assert.assertEquals(absentValue, classUnderTest.getIfAbsent(absentKey, new PassThruFunction0<String>(absentValue)));
105
Assert.assertEquals(this.equalUnifiedMap(), classUnderTest);
110
public void getIfAbsent()
112
Integer absentKey = this.size() + 1;
113
String absentValue = String.valueOf(absentKey);
115
// Absent key behavior
116
ImmutableMap<Integer, String> classUnderTest = this.classUnderTest();
117
Assert.assertEquals(absentValue, classUnderTest.getIfAbsentValue(absentKey, absentValue));
120
Assert.assertEquals(this.equalUnifiedMap(), classUnderTest);
125
public void getIfAbsentWith()
127
Integer absentKey = this.size() + 1;
128
String absentValue = String.valueOf(absentKey);
130
// Absent key behavior
131
ImmutableMap<Integer, String> classUnderTest = this.classUnderTest();
132
Assert.assertEquals(absentValue, classUnderTest.getIfAbsentWith(absentKey, String::valueOf, absentValue));
135
Assert.assertEquals(this.equalUnifiedMap(), classUnderTest);
140
public void ifPresentApply()
142
Integer absentKey = this.size() + 1;
144
ImmutableMap<Integer, String> classUnderTest = this.classUnderTest();
145
Assert.assertNull(classUnderTest.ifPresentApply(absentKey, Functions.<String>getPassThru()));
150
public void notEmpty()
152
Assert.assertFalse(this.classUnderTest().notEmpty());
157
public void allSatisfy()
159
ImmutableMap<Integer, String> map = this.classUnderTest();
161
Assert.assertTrue(map.allSatisfy(String.class::isInstance));
162
Assert.assertTrue(map.allSatisfy("Monkey"::equals));
167
public void noneSatisfy()
169
ImmutableMap<Integer, String> map = this.classUnderTest();
171
Assert.assertTrue(map.noneSatisfy(Integer.class::isInstance));
172
Assert.assertTrue(map.noneSatisfy("Monkey"::equals));
177
public void anySatisfy()
179
ImmutableMap<Integer, String> map = this.classUnderTest();
181
Assert.assertFalse(map.anySatisfy(String.class::isInstance));
182
Assert.assertFalse(map.anySatisfy("Monkey"::equals));
186
@Test(expected = NoSuchElementException.class)
189
ImmutableMap<Integer, String> map = this.classUnderTest();
195
@Test(expected = NoSuchElementException.class)
198
ImmutableMap<Integer, String> map = this.classUnderTest();
200
map.maxBy(Functions.getStringPassThru());
204
@Test(expected = NoSuchElementException.class)
207
ImmutableMap<Integer, String> map = this.classUnderTest();
213
@Test(expected = NoSuchElementException.class)
216
ImmutableMap<Integer, String> map = this.classUnderTest();
218
map.minBy(Functions.getStringPassThru());
224
ImmutableMap<Integer, String> map = this.classUnderTest();
225
ImmutableMap<Integer, String> actual = map.select((ignored1, ignored2) -> true);
226
Verify.assertInstanceOf(ImmutableEmptyMapWithHashingStrategy.class, actual);
232
ImmutableMap<Integer, String> map = this.classUnderTest();
233
ImmutableMap<Integer, String> actual = map.reject((ignored1, ignored2) -> false);
234
Verify.assertInstanceOf(ImmutableEmptyMapWithHashingStrategy.class, actual);
240
ImmutableMap<Integer, String> map = this.classUnderTest();
241
Assert.assertNull(map.detect((ignored1, ignored2) -> true));
245
protected <K, V> ImmutableMap<K, V> newMapWithKeysValues(K key1, V value1, K key2, V value2)
247
return new ImmutableEmptyMapWithHashingStrategy<K, V>(HashingStrategies.nullSafeHashingStrategy(
248
HashingStrategies.<K>defaultStrategy()));
252
protected <K, V> ImmutableMap<K, V> newMapWithKeysValues(K key1, V value1, K key2, V value2, K key3, V value3)
254
return new ImmutableEmptyMapWithHashingStrategy<K, V>(HashingStrategies.nullSafeHashingStrategy(
255
HashingStrategies.<K>defaultStrategy()));
259
protected <K, V> ImmutableMap<K, V> newMapWithKeysValues(K key1, V value1, K key2, V value2, K key3, V value3, K key4, V value4)
261
return new ImmutableEmptyMapWithHashingStrategy<K, V>(HashingStrategies.nullSafeHashingStrategy(
262
HashingStrategies.<K>defaultStrategy()));