File: AbstractInt2IntMapTest.java

package info (click to toggle)
libfastutil-java 8.5.16%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,208 kB
  • sloc: java: 19,706; sh: 1,188; makefile: 473; xml: 354
file content (163 lines) | stat: -rw-r--r-- 4,148 bytes parent folder | download | duplicates (3)
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*
 * Copyright (C) 2017-2022 Sebastiano Vigna
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package it.unimi.dsi.fastutil.ints;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import java.util.Collections;
import java.util.EnumSet;
import java.util.function.Supplier;

import org.junit.Test;
import org.junit.runners.Parameterized.Parameters;

import it.unimi.dsi.fastutil.objects.ObjectIterator;
import it.unimi.dsi.fastutil.objects.ObjectSet;
import it.unimi.dsi.fastutil.objects.ObjectSets;

public class AbstractInt2IntMapTest extends Int2IntMapGenericTest<AbstractInt2IntMap> {

	@Parameters
	public static Iterable<Object[]> data() {
		return Collections.singletonList(new Object[] {(Supplier<Int2IntMap>) SimpleInt2IntMap::new, EnumSet.allOf(Capability.class)});
	}

	// A basic implementation of AbstractInt2IntMap that contains as little methods implemented
	// possible to make a mutable map, so we can test as many of the AbstractInt2IntMap default
	// implementations as possible.
	static final class SimpleInt2IntMap extends AbstractInt2IntMap implements Int2IntMap {
		private static final long serialVersionUID = 1L;

		private final IntList keys = new IntArrayList();
		private final IntList values = new IntArrayList();
		@Override
		public int get(final int key) {
			final int index = keys.indexOf(key);
			if (index == -1) {
				return defaultReturnValue();
			}
			return values.getInt(index);
		}
		@Override
		public int size() {
			return keys.size();
		}
		@Override
		public int put(final int key, final int value) {
			final int index = keys.indexOf(key);
			if (index == -1) {
				keys.add(key);
				values.add(value);
				return defaultReturnValue();
			}
			return values.set(index, value);
		}
		@Override
		public int remove(final int key) {
			final int index = keys.indexOf(key);
			if (index == -1) {
				return defaultReturnValue();
			}
			keys.removeInt(index);
			return values.removeInt(index);
		}
		@Override
		public void clear() {
			keys.clear();
			values.clear();
		}

		@Override
		public ObjectSet<Entry> int2IntEntrySet() {
			return new AbstractInt2IntMap.BasicEntrySet(this) {
				@Override
				public ObjectIterator<Entry> iterator() {
					return new ObjectIterator<Entry>() {
					final IntIterator keyIter = keys.iterator();
					final IntIterator valueIter = values.iterator();

					@Override
					public boolean hasNext() {
						return keyIter.hasNext();
					}

						@Override
						public Entry next() {
							return new AbstractInt2IntMap.BasicEntry(keyIter.nextInt(), valueIter.nextInt());
						}

					@Override
					public void remove() {
						keyIter.remove();
						valueIter.remove();
					}
					};
				}
			};
		}
	}

	@Test
	public void testContainsKeyEmptySet() {
		m = new AbstractInt2IntMap() {
			private static final long serialVersionUID = 0L;

			@Override
			public int size() {
				return 0;
			}

			@Override
			public int get(final int key) {
				return -1;
			}

			@Override
			public ObjectSet<Entry> int2IntEntrySet() {
				return ObjectSets.EMPTY_SET;
			}
		};

		assertFalse(m.containsKey(0));
	}

	@Test
	public void testContainsKeySingleton() {
		m = new AbstractInt2IntMap() {
			private static final long serialVersionUID = 0L;

			@Override
			public int size() {
				return 1;
			}

			@Override
			public int get(final int key) {
				return key == 0 ? 0 : -1;
			}

			@Override
			public ObjectSet<Entry> int2IntEntrySet() {
				return ObjectSets.singleton((Entry)new AbstractInt2IntMap.BasicEntry(0,0));
			}
		};

		assertTrue(m.containsKey(0));
		assertFalse(m.containsKey(1));
	}
}