File: IntArrayIndirectPriorityQueueTest.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 (363 lines) | stat: -rw-r--r-- 12,187 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/*
 * 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.assertEquals;
import static org.junit.Assert.assertTrue;

import java.util.Random;

import org.junit.Test;

public class IntArrayIndirectPriorityQueueTest {

	@Test
	public void testFront() {
		final int refArray[] = { 4, 3, 2, 1, 0, 3, 2, 1, 0, 2, 1, 0, 1, 0, 0 };
		final int tops[] = new int[refArray.length];
		final IntArrayIndirectPriorityQueue queue = new IntArrayIndirectPriorityQueue(refArray);
		for (int i = refArray.length; i-- != 0;)
			queue.enqueue(i);

		assertEquals(5, queue.front(tops));
		assertEquals(new IntOpenHashSet(new int[] { 4, 8, 11, 13, 14 }), new IntOpenHashSet(tops, 0, 5));
		for (int i = 4; i-- != 0;) {
			queue.dequeue();
			assertEquals(i + 1, queue.front(tops));
		}
		queue.dequeue();

		assertEquals(4, queue.front(tops));
		assertEquals(new IntOpenHashSet(new int[] { 3, 7, 10, 12 }), new IntOpenHashSet(tops, 0, 4));
		for (int i = 3; i-- != 0;) {
			queue.dequeue();
			assertEquals(i + 1, queue.front(tops));
		}
		queue.dequeue();

		assertEquals(3, queue.front(tops));
		assertEquals(new IntOpenHashSet(new int[] { 2, 6, 9 }), new IntOpenHashSet(tops, 0, 3));
		for (int i = 2; i-- != 0;) {
			queue.dequeue();
			assertEquals(i + 1, queue.front(tops));
		}
		queue.dequeue();

		assertEquals(2, queue.front(tops));
		assertEquals(new IntOpenHashSet(new int[] { 1, 5 }), new IntOpenHashSet(tops, 0, 2));
		queue.dequeue();
		assertEquals(1, queue.front(tops));
		queue.dequeue();

		assertEquals(1, queue.front(tops));
	}


	private int[] ref;

	private boolean heapEqual(final int[] a, final int[] b, int sizea, final int sizeb) {
		if (sizea != sizeb) return false;
		final int[] aa = new int[sizea];
		final int[] bb = new int[sizea];
		for (int i = 0; i < sizea; i++) {
			aa[i] = ref[a[i]];
			bb[i] = ref[b[i]];
		}
		java.util.Arrays.sort(aa);
		java.util.Arrays.sort(bb);
		while (sizea-- != 0)
			if (!((aa[sizea]) == (bb[sizea]))) return false;
		return true;
	}

	public void test(final int n, final IntComparator comparator) {
		Exception mThrowsIllegal, tThrowsIllegal, mThrowsOutOfBounds, tThrowsOutOfBounds, mThrowsNoElement, tThrowsNoElement;
		int rm = 0, rt = 0;
		final Random r = new Random(0);
		ref = new int[n];

		for (int i = 0; i < n; i++) ref[i] = r.nextInt();

		IntArrayIndirectPriorityQueue m = new IntArrayIndirectPriorityQueue(ref, comparator);
		final IntHeapIndirectPriorityQueue t = new IntHeapIndirectPriorityQueue(ref, comparator);

		/* We add pairs to t. */
		for (int i = 0; i < n / 2; i++) {
			t.enqueue(i);
			m.enqueue(i);
		}

		assertTrue("Error: m and t differ after creation (" + m + ", " + t + ")", heapEqual(m.array, t.heap, m.size(), t.size()));

		/* Now we add and remove random data in m and t, checking that the result is the same. */

		for (int i = 0; i < 2 * n; i++) {
			if (r.nextDouble() < 0.01) {
				t.clear();
				m.clear();
				for (int j = 0; j < n / 2; j++) {
					t.enqueue(j);
					m.enqueue(j);
				}
			}

			final int T = r.nextInt(2 * n);

			mThrowsNoElement = tThrowsNoElement = mThrowsOutOfBounds = tThrowsOutOfBounds = mThrowsIllegal = tThrowsIllegal = null;

			try {
				t.enqueue(T);
			}
			catch (final IndexOutOfBoundsException e) {
				tThrowsOutOfBounds = e;
			}
			catch (final IllegalArgumentException e) {
				tThrowsIllegal = e;
			}

			if (tThrowsIllegal == null) { // To skip duplicates
				try {
					m.enqueue(T);
				}
				catch (final IndexOutOfBoundsException e) {
					mThrowsOutOfBounds = e;
				}
				catch (final IllegalArgumentException e) {
					mThrowsIllegal = e;
				}
			}

			mThrowsIllegal = tThrowsIllegal = null; // To skip duplicates

			assertTrue("Error: enqueue() divergence in IndexOutOfBoundsException for " + T + " (" + mThrowsOutOfBounds + ", " + tThrowsOutOfBounds + ")",
					(mThrowsOutOfBounds == null) == (tThrowsOutOfBounds == null));
			assertTrue("Error: enqueue() divergence in IllegalArgumentException for " + T + " (" + mThrowsIllegal + ", " + tThrowsIllegal + ")",
					(mThrowsIllegal == null) == (tThrowsIllegal == null));

			assertTrue("Error: m and t differ after enqueue (" + m + ", " + t + ")", heapEqual(m.array, t.heap, m.size(), t.size()));

			if (m.size() != 0) {
				assertTrue("Error: m and t differ in first element after enqueue (" + m.first() + "->" + ref[m.first()] + ", " + t.first() + "->" + ref[t.first()] + ")",
						((ref[m.first()]) == (ref[t.first()])));
			}

			mThrowsOutOfBounds = tThrowsOutOfBounds = null;

			try {
				rm = m.dequeue();
				while (!m.isEmpty() && ((ref[m.first()]) == (ref[rm])))	m.dequeue();
			}
			catch (final IndexOutOfBoundsException e) {
				mThrowsOutOfBounds = e;
			}
			catch (final IllegalArgumentException e) {
				mThrowsIllegal = e;
			}
			catch (final java.util.NoSuchElementException e) {
				mThrowsNoElement = e;
			}

			try {
				rt = t.dequeue();
				while (!t.isEmpty() && ((ref[t.first()]) == (ref[rt])))
					t.dequeue();
			}
			catch (final IndexOutOfBoundsException e) {
				tThrowsOutOfBounds = e;
			}
			catch (final IllegalArgumentException e) {
				tThrowsIllegal = e;
			}
			catch (final java.util.NoSuchElementException e) {
				tThrowsNoElement = e;
			}

			assertTrue("Error: dequeue() divergence in IndexOutOfBoundsException (" + mThrowsOutOfBounds + ", " + tThrowsOutOfBounds + ")",
					(mThrowsOutOfBounds == null) == (tThrowsOutOfBounds == null));
			assertTrue("Error: dequeue() divergence in IllegalArgumentException  (" + mThrowsIllegal + ", " + tThrowsIllegal + ")", (mThrowsIllegal == null) == (tThrowsIllegal == null));
			assertTrue("Error: dequeue() divergence in java.util.NoSuchElementException  (" + mThrowsNoElement + ", " + tThrowsNoElement + ")",
					(mThrowsNoElement == null) == (tThrowsNoElement == null));
			if (mThrowsOutOfBounds == null) assertTrue("Error: divergence in dequeue() between m and t (" + rm + "->" + ref[rm] + ", " + rt + "->" + ref[rt] + ")",
					((ref[rt]) == (ref[rm])));


			assertTrue("Error: m and t differ after dequeue (" + m + ", " + t + ")", heapEqual(m.array, t.heap, m.size(), t.size()));

			if (m.size() != 0) {
				assertTrue("Error: m and t differ in first element after dequeue (" + m.first() + "->" + ref[m.first()] + ", " + t.first() + "->" + ref[t.first()] + ")",
						((ref[m.first()]) == (ref[t.first()])));
			}

			mThrowsNoElement = tThrowsNoElement = mThrowsOutOfBounds = tThrowsOutOfBounds = mThrowsIllegal = tThrowsIllegal = null;


			int pos = r.nextInt(n * 2);

			try {
				m.remove(pos);
			}
			catch (final IndexOutOfBoundsException e) {
				mThrowsOutOfBounds = e;
			}
			catch (final IllegalArgumentException e) {
				mThrowsIllegal = e;
			}
			catch (final java.util.NoSuchElementException e) {
				mThrowsNoElement = e;
			}

			try {
				t.remove(pos);
			}
			catch (final IndexOutOfBoundsException e) {
				tThrowsOutOfBounds = e;
			}
			catch (final IllegalArgumentException e) {
				tThrowsIllegal = e;
			}
			catch (final java.util.NoSuchElementException e) {
				tThrowsNoElement = e;
			}

			assertTrue("Error: remove(int) divergence in IndexOutOfBoundsException (" + mThrowsOutOfBounds + ", " + tThrowsOutOfBounds + ")",
					(mThrowsOutOfBounds == null) == (tThrowsOutOfBounds == null));
			assertTrue("Error: remove(int) divergence in IllegalArgumentException  (" + mThrowsIllegal + ", " + tThrowsIllegal + ")", (mThrowsIllegal == null) == (tThrowsIllegal == null));
			assertTrue("Error: remove(int) divergence in java.util.NoSuchElementException  (" + mThrowsNoElement + ", " + tThrowsNoElement + ")",
					(mThrowsNoElement == null) == (tThrowsNoElement == null));

			assertTrue("Error: m and t differ after remove(int) (" + m + ", " + t + ")", heapEqual(m.array, t.heap, m.size(), t.size()));

			if (m.size() != 0) {
				assertTrue("Error: m and t differ in first element after remove(int) (" + m.first() + "->" + ref[m.first()] + ", " + t.first() + "->" + ref[t.first()] + ")",
						((ref[m.first()]) == (ref[t.first()])));
			}


			mThrowsNoElement = tThrowsNoElement = mThrowsOutOfBounds = tThrowsOutOfBounds = mThrowsIllegal = tThrowsIllegal = null;

			pos = r.nextInt(n);

			try {
				t.changed(pos);
			}
			catch (final IndexOutOfBoundsException e) {
				tThrowsOutOfBounds = e;
			}
			catch (final IllegalArgumentException e) {
				tThrowsIllegal = e;
			}
			catch (final java.util.NoSuchElementException e) {
				tThrowsNoElement = e;
			}

			if (tThrowsIllegal == null) {
				try {
					m.changed(pos);
				}
				catch (final IndexOutOfBoundsException e) {
					mThrowsOutOfBounds = e;
				}
				catch (final IllegalArgumentException e) {
					mThrowsIllegal = e;
				}
				catch (final java.util.NoSuchElementException e) {
					mThrowsNoElement = e;
				}
			}

			assertTrue("Error: change(int) divergence in IndexOutOfBoundsException (" + mThrowsOutOfBounds + ", " + tThrowsOutOfBounds + ")",
					(mThrowsOutOfBounds == null) == (tThrowsOutOfBounds == null));
			// assertTrue("Error: change(int) divergence in IllegalArgumentException  (" +
			// mThrowsIllegal + ", " + tThrowsIllegal + ")" , (mThrowsIllegal == null) == (
			// tThrowsIllegal == null));
			assertTrue("Error: change(int) divergence in java.util.NoSuchElementException  (" + mThrowsNoElement + ", " + tThrowsNoElement + ")",
					(mThrowsNoElement == null) == (tThrowsNoElement == null));

			assertTrue("Error: m and t differ after change(int) (" + m + ", " + t + ")", heapEqual(m.array, t.heap, m.size(), t.size()));

			if (m.size() != 0) {
				assertTrue("Error: m and t differ in first element after change(int) (" + m.first() + "->" + ref[m.first()] + ", " + t.first() + "->" + ref[t.first()] + ")",
						((ref[m.first()]) == (ref[t.first()])));
			}

			final int[] temp = t.heap.clone();
			IntArrays.quickSort(temp, 0, t.size()); // To scramble a bit
			m = new IntArrayIndirectPriorityQueue(m.refArray, temp, t.size(), comparator);

			assertTrue("Error: m and t differ after wrap (" + m + ", " + t + ")", heapEqual(m.array, t.heap, m.size(), t.size()));

			if (m.size() != 0) {
				assertTrue("Error: m and t differ in first element after wrap (" + m.first() + "->" + ref[m.first()] + ", " + t.first() + "->" + ref[t.first()] + ")",
						((ref[m.first()]) == (ref[t.first()])));
			}

			if (m.size() != 0 && ((new it.unimi.dsi.fastutil.ints.IntOpenHashSet(m.array, 0, m.size)).size() == m.size())) {

				final int first = m.first();
				ref[first] = r.nextInt();

				// System.err.println("Pre-change m: " +m);
				// System.err.println("Pre-change t: " +t);
				m.changed();
				t.changed(first);

				// System.err.println("Post-change m: " +m);
				// System.err.println("Post-change t: " +t);

				assertTrue("Error: m and t differ after change (" + m + ", " + t + ")", heapEqual(m.array, t.heap, m.size(), t.size()));

				if (m.size() != 0) {
					assertTrue("Error: m and t differ in first element after change (" + m.first() + "->" + ref[m.first()] + ", " + t.first() + "->" + ref[t.first()] + ")",
							((ref[m.first()]) == (ref[t.first()])));
				}
			}
		}


		/* Now we check that m actually holds the same data. */

		m.clear();
		assertTrue("Error: m is not empty after clear()", m.isEmpty());
	}


	@Test
	public void test1() {
		test(1, null);
		test(1, IntComparators.OPPOSITE_COMPARATOR);

	}

	@Test
	public void test10() {
		test(10, null);
		test(10, IntComparators.OPPOSITE_COMPARATOR);
	}

	@Test
	public void test100() {
		test(100, null);
		test(100, IntComparators.OPPOSITE_COMPARATOR);
	}

	@Test
	public void test1000() {
		test(1000, null);
		test(1000, IntComparators.OPPOSITE_COMPARATOR);
	}

}