~ubuntu-branches/ubuntu/wily/gs-collections/wily

« back to all changes in this revision

Viewing changes to unit-tests/src/test/java/com/gs/collections/impl/map/strategy/immutable/ImmutableEmptyMapWithHashingStrategyTest.java

  • Committer: Package Import Robot
  • Author(s): Emmanuel Bourg
  • Date: 2015-07-23 12:42:30 UTC
  • Revision ID: package-import@ubuntu.com-20150723124230-2rjvfv6elyn2m7d4
Tags: upstream-5.1.0
ImportĀ upstreamĀ versionĀ 5.1.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright 2014 Goldman Sachs.
 
3
 *
 
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
 
7
 *
 
8
 *     http://www.apache.org/licenses/LICENSE-2.0
 
9
 *
 
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.
 
15
 */
 
16
 
 
17
package com.gs.collections.impl.map.strategy.immutable;
 
18
 
 
19
import java.util.NoSuchElementException;
 
20
 
 
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;
 
30
 
 
31
/**
 
32
 * JUnit test for {@link ImmutableEmptyMapWithHashingStrategy}.
 
33
 */
 
34
public class ImmutableEmptyMapWithHashingStrategyTest extends ImmutableMemoryEfficientMapTestCase
 
35
{
 
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>()
 
38
    {
 
39
        public int computeHashCode(Integer object)
 
40
        {
 
41
            return object.hashCode();
 
42
        }
 
43
 
 
44
        public boolean equals(Integer object1, Integer object2)
 
45
        {
 
46
            return object1.equals(object2);
 
47
        }
 
48
    });
 
49
 
 
50
    @Override
 
51
    protected ImmutableMap<Integer, String> classUnderTest()
 
52
    {
 
53
        return new ImmutableEmptyMapWithHashingStrategy<Integer, String>(HASHING_STRATEGY);
 
54
    }
 
55
 
 
56
    @Override
 
57
    protected int size()
 
58
    {
 
59
        return 0;
 
60
    }
 
61
 
 
62
    @Override
 
63
    @Test
 
64
    public void testToString()
 
65
    {
 
66
        ImmutableMap<Integer, String> map = this.classUnderTest();
 
67
        Assert.assertEquals("{}", map.toString());
 
68
    }
 
69
 
 
70
    @Override
 
71
    @Test
 
72
    public void flipUniqueValues()
 
73
    {
 
74
        Verify.assertEmpty(this.classUnderTest().flipUniqueValues());
 
75
    }
 
76
 
 
77
    @Override
 
78
    @Test
 
79
    public void get()
 
80
    {
 
81
        Integer absentKey = this.size() + 1;
 
82
        String absentValue = String.valueOf(absentKey);
 
83
 
 
84
        // Absent key behavior
 
85
        ImmutableMap<Integer, String> classUnderTest = this.classUnderTest();
 
86
        Assert.assertNull(classUnderTest.get(absentKey));
 
87
        Assert.assertFalse(classUnderTest.containsValue(absentValue));
 
88
 
 
89
        // Still unchanged
 
90
        Assert.assertEquals(this.equalUnifiedMap(), classUnderTest);
 
91
    }
 
92
 
 
93
    @Override
 
94
    @Test
 
95
    public void getIfAbsent_function()
 
96
    {
 
97
        Integer absentKey = this.size() + 1;
 
98
        String absentValue = String.valueOf(absentKey);
 
99
 
 
100
        // Absent key behavior
 
101
        ImmutableMap<Integer, String> classUnderTest = this.classUnderTest();
 
102
        Assert.assertEquals(absentValue, classUnderTest.getIfAbsent(absentKey, new PassThruFunction0<String>(absentValue)));
 
103
 
 
104
        // Still unchanged
 
105
        Assert.assertEquals(this.equalUnifiedMap(), classUnderTest);
 
106
    }
 
107
 
 
108
    @Override
 
109
    @Test
 
110
    public void getIfAbsent()
 
111
    {
 
112
        Integer absentKey = this.size() + 1;
 
113
        String absentValue = String.valueOf(absentKey);
 
114
 
 
115
        // Absent key behavior
 
116
        ImmutableMap<Integer, String> classUnderTest = this.classUnderTest();
 
117
        Assert.assertEquals(absentValue, classUnderTest.getIfAbsentValue(absentKey, absentValue));
 
118
 
 
119
        // Still unchanged
 
120
        Assert.assertEquals(this.equalUnifiedMap(), classUnderTest);
 
121
    }
 
122
 
 
123
    @Override
 
124
    @Test
 
125
    public void getIfAbsentWith()
 
126
    {
 
127
        Integer absentKey = this.size() + 1;
 
128
        String absentValue = String.valueOf(absentKey);
 
129
 
 
130
        // Absent key behavior
 
131
        ImmutableMap<Integer, String> classUnderTest = this.classUnderTest();
 
132
        Assert.assertEquals(absentValue, classUnderTest.getIfAbsentWith(absentKey, String::valueOf, absentValue));
 
133
 
 
134
        // Still unchanged
 
135
        Assert.assertEquals(this.equalUnifiedMap(), classUnderTest);
 
136
    }
 
137
 
 
138
    @Override
 
139
    @Test
 
140
    public void ifPresentApply()
 
141
    {
 
142
        Integer absentKey = this.size() + 1;
 
143
 
 
144
        ImmutableMap<Integer, String> classUnderTest = this.classUnderTest();
 
145
        Assert.assertNull(classUnderTest.ifPresentApply(absentKey, Functions.<String>getPassThru()));
 
146
    }
 
147
 
 
148
    @Override
 
149
    @Test
 
150
    public void notEmpty()
 
151
    {
 
152
        Assert.assertFalse(this.classUnderTest().notEmpty());
 
153
    }
 
154
 
 
155
    @Override
 
156
    @Test
 
157
    public void allSatisfy()
 
158
    {
 
159
        ImmutableMap<Integer, String> map = this.classUnderTest();
 
160
 
 
161
        Assert.assertTrue(map.allSatisfy(String.class::isInstance));
 
162
        Assert.assertTrue(map.allSatisfy("Monkey"::equals));
 
163
    }
 
164
 
 
165
    @Override
 
166
    @Test
 
167
    public void noneSatisfy()
 
168
    {
 
169
        ImmutableMap<Integer, String> map = this.classUnderTest();
 
170
 
 
171
        Assert.assertTrue(map.noneSatisfy(Integer.class::isInstance));
 
172
        Assert.assertTrue(map.noneSatisfy("Monkey"::equals));
 
173
    }
 
174
 
 
175
    @Override
 
176
    @Test
 
177
    public void anySatisfy()
 
178
    {
 
179
        ImmutableMap<Integer, String> map = this.classUnderTest();
 
180
 
 
181
        Assert.assertFalse(map.anySatisfy(String.class::isInstance));
 
182
        Assert.assertFalse(map.anySatisfy("Monkey"::equals));
 
183
    }
 
184
 
 
185
    @Override
 
186
    @Test(expected = NoSuchElementException.class)
 
187
    public void max()
 
188
    {
 
189
        ImmutableMap<Integer, String> map = this.classUnderTest();
 
190
 
 
191
        map.max();
 
192
    }
 
193
 
 
194
    @Override
 
195
    @Test(expected = NoSuchElementException.class)
 
196
    public void maxBy()
 
197
    {
 
198
        ImmutableMap<Integer, String> map = this.classUnderTest();
 
199
 
 
200
        map.maxBy(Functions.getStringPassThru());
 
201
    }
 
202
 
 
203
    @Override
 
204
    @Test(expected = NoSuchElementException.class)
 
205
    public void min()
 
206
    {
 
207
        ImmutableMap<Integer, String> map = this.classUnderTest();
 
208
 
 
209
        map.min();
 
210
    }
 
211
 
 
212
    @Override
 
213
    @Test(expected = NoSuchElementException.class)
 
214
    public void minBy()
 
215
    {
 
216
        ImmutableMap<Integer, String> map = this.classUnderTest();
 
217
 
 
218
        map.minBy(Functions.getStringPassThru());
 
219
    }
 
220
 
 
221
    @Override
 
222
    public void select()
 
223
    {
 
224
        ImmutableMap<Integer, String> map = this.classUnderTest();
 
225
        ImmutableMap<Integer, String> actual = map.select((ignored1, ignored2) -> true);
 
226
        Verify.assertInstanceOf(ImmutableEmptyMapWithHashingStrategy.class, actual);
 
227
    }
 
228
 
 
229
    @Override
 
230
    public void reject()
 
231
    {
 
232
        ImmutableMap<Integer, String> map = this.classUnderTest();
 
233
        ImmutableMap<Integer, String> actual = map.reject((ignored1, ignored2) -> false);
 
234
        Verify.assertInstanceOf(ImmutableEmptyMapWithHashingStrategy.class, actual);
 
235
    }
 
236
 
 
237
    @Override
 
238
    public void detect()
 
239
    {
 
240
        ImmutableMap<Integer, String> map = this.classUnderTest();
 
241
        Assert.assertNull(map.detect((ignored1, ignored2) -> true));
 
242
    }
 
243
 
 
244
    @Override
 
245
    protected <K, V> ImmutableMap<K, V> newMapWithKeysValues(K key1, V value1, K key2, V value2)
 
246
    {
 
247
        return new ImmutableEmptyMapWithHashingStrategy<K, V>(HashingStrategies.nullSafeHashingStrategy(
 
248
                HashingStrategies.<K>defaultStrategy()));
 
249
    }
 
250
 
 
251
    @Override
 
252
    protected <K, V> ImmutableMap<K, V> newMapWithKeysValues(K key1, V value1, K key2, V value2, K key3, V value3)
 
253
    {
 
254
        return new ImmutableEmptyMapWithHashingStrategy<K, V>(HashingStrategies.nullSafeHashingStrategy(
 
255
                HashingStrategies.<K>defaultStrategy()));
 
256
    }
 
257
 
 
258
    @Override
 
259
    protected <K, V> ImmutableMap<K, V> newMapWithKeysValues(K key1, V value1, K key2, V value2, K key3, V value3, K key4, V value4)
 
260
    {
 
261
        return new ImmutableEmptyMapWithHashingStrategy<K, V>(HashingStrategies.nullSafeHashingStrategy(
 
262
                HashingStrategies.<K>defaultStrategy()));
 
263
    }
 
264
}