-
Notifications
You must be signed in to change notification settings - Fork 583
/
BiMapTestCase.java
141 lines (119 loc) · 5.4 KB
/
BiMapTestCase.java
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
/*
* Copyright (c) 2022 Goldman Sachs and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v. 1.0 which accompany this distribution.
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*/
package org.eclipse.collections.test.bimap;
import org.eclipse.collections.api.bimap.BiMap;
import org.eclipse.collections.api.collection.MutableCollection;
import org.eclipse.collections.api.factory.Lists;
import org.eclipse.collections.api.list.MutableList;
import org.eclipse.collections.test.RichIterableUniqueTestCase;
import org.eclipse.collections.test.map.MapIterableTestCase;
import org.junit.Assert;
import org.junit.Test;
import static org.eclipse.collections.test.IterableTestCase.assertEquals;
public interface BiMapTestCase extends RichIterableUniqueTestCase, MapIterableTestCase
{
@Override
<T> BiMap<Object, T> newWith(T... elements);
@Override
<K, V> BiMap<K, V> newWithKeysValues(Object... elements);
@Override
default boolean allowsDuplicates()
{
return false;
}
@Test
default void Iterable_sanity_check()
{
// Intentionally blank
}
@Override
default void Iterable_toString()
{
RichIterableUniqueTestCase.super.Iterable_toString();
BiMap<String, Integer> bimap = this.newWithKeysValues("Two", 2, "One", 1);
Assert.assertEquals("{Two=2, One=1}", bimap.toString());
Assert.assertEquals("[Two, One]", bimap.keysView().toString());
Assert.assertEquals("[2, 1]", bimap.valuesView().toString());
Assert.assertEquals("[Two:2, One:1]", bimap.keyValuesView().toString());
Assert.assertEquals("[2, 1]", bimap.asLazy().toString());
Assert.assertEquals(
"{10=4, 9=4, 8=4, 7=4, 6=3, 5=3, 4=3, 3=2, 2=2, 1=1}",
this.newWith(4, 4, 4, 4, 3, 3, 3, 2, 2, 1).toString());
Assert.assertEquals(
"[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]",
this.newWith(4, 4, 4, 4, 3, 3, 3, 2, 2, 1).keysView().toString());
Assert.assertEquals(
"[4, 4, 4, 4, 3, 3, 3, 2, 2, 1]",
this.newWith(4, 4, 4, 4, 3, 3, 3, 2, 2, 1).valuesView().toString());
Assert.assertEquals(
"[10:4, 9:4, 8:4, 7:4, 6:3, 5:3, 4:3, 3:2, 2:2, 1:1]",
this.newWith(4, 4, 4, 4, 3, 3, 3, 2, 2, 1).keyValuesView().toString());
}
@Override
default void MapIterable_forEachKeyValue()
{
BiMap<Object, Integer> bimap = this.newWith(3, 2, 1);
MutableCollection<Integer> forEachKeyValue = this.newMutableForFilter();
bimap.forEachKeyValue((key, value) -> forEachKeyValue.add(value + 10));
assertEquals(this.newMutableForFilter(13, 13, 13, 12, 12, 11), forEachKeyValue);
BiMap<Integer, String> bimap2 = this.newWithKeysValues(3, "Three", 2, "Two", 1, "One");
MutableCollection<String> forEachKeyValue2 = this.newMutableForFilter();
bimap2.forEachKeyValue((key, value) -> forEachKeyValue2.add(key + value));
assertEquals(this.newMutableForFilter("3Three", "2Two", "1One"), forEachKeyValue2);
MutableCollection<Integer> forEachValue = this.newMutableForFilter();
bimap.forEachValue(value -> forEachValue.add(value + 10));
assertEquals(this.newMutableForFilter(13, 13, 13, 12, 12, 11), forEachValue);
MutableCollection<Object> forEachKey = this.newMutableForFilter();
bimap2.forEachKey(key -> forEachKey.add(key + 1));
assertEquals(this.newMutableForFilter(4, 3, 2), forEachKey);
}
@Override
default void MapIterable_flipUniqueValues()
{
BiMap<String, Integer> bimap = this.newWithKeysValues("Three", 3, "Two", 2, "One", 1);
BiMap<Integer, String> result = bimap.flipUniqueValues();
assertEquals(
this.newWithKeysValues(3, "Three", 2, "Two", 1, "One"),
result);
}
@Override
default void RichIterable_size()
{
RichIterableUniqueTestCase.super.RichIterable_size();
}
default void BiMap_toList()
{
BiMap<Object, Integer> iterable = this.newWith(4, 3, 2, 1);
{
MutableList<Integer> target = Lists.mutable.empty();
iterable.forEachValue(target::add);
assertEquals(
target,
iterable.toList());
}
MutableList<Integer> target = Lists.mutable.empty();
iterable.forEachKeyValue((key, value) -> target.add(value));
assertEquals(
target,
iterable.toList());
}
@Override
default void MapIterable_forEach()
{
BiMap<Object, Integer> bimap = this.newWith(3, 2, 1);
MutableCollection<Integer> forEach = this.newMutableForFilter();
bimap.forEach((key, value) -> forEach.add(value + 10));
assertEquals(this.newMutableForFilter(13, 13, 13, 12, 12, 11), forEach);
BiMap<Integer, String> bimap2 = this.newWithKeysValues(3, "Three", 2, "Two", 1, "One");
MutableCollection<String> forEach2 = this.newMutableForFilter();
bimap2.forEach((key, value) -> forEach2.add(key + value));
assertEquals(this.newMutableForFilter("3Three", "2Two", "1One"), forEach2);
}
}