Skip to main content
summaryrefslogblamecommitdiffstats
blob: 79beec4eb335498e84a2105f54ad72c0fc04a4a4 (plain) (tree)
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532



















































































































































































































































































































































































































































































































































                                                                                                                              
/*******************************************************************************
 * Copyright (c) 2015, 2016 Obeo and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     Obeo - initial API and implementation
 *     Alexandra Buzila - bug 478620
 *     Martin Fleck - bug 507177
 *******************************************************************************/
package org.eclipse.emf.compare.tests.utils;

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

import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;

import java.util.Iterator;
import java.util.Set;

import org.eclipse.emf.compare.graph.IGraph;
import org.eclipse.emf.compare.graph.PruningIterator;
import org.eclipse.emf.compare.internal.utils.Graph;
import org.junit.Test;

/**
 * We will use this to test the utility methods exposed by the {@link Graph}.
 * 
 * @author <a href="mailto:axel.richard@obeo.fr">Axel Richard</a>
 */
@SuppressWarnings({"nls", "unchecked" })
public abstract class AbstractGraphTest<E> {
	protected abstract E toType(final String name);

	protected abstract IGraph<E> createGraph();

	/**
	 * Test correct subgraph.
	 * 
	 * <pre>
	 *       _A_
	 *      / | \
	 *     B  C  D
	 *        |  |
	 *        E  F
	 * </pre>
	 */
	@Test
	public void testBuildSubGraph() {
		IGraph<E> graph = createGraph();
		graph.addChildren(toType("A"), ImmutableSet.of(toType("B"), toType("C"), toType("D")));
		graph.addChildren(toType("C"), ImmutableSet.of(toType("E")));
		graph.addChildren(toType("D"), ImmutableSet.of(toType("F")));

		Set<E> subgraph = graph.getSubgraphContaining(toType("D"), ImmutableSet.of(toType("C")));
		assertEquals(4, subgraph.size());
		assertTrue(
				subgraph.containsAll(Lists.newArrayList(toType("A"), toType("B"), toType("D"), toType("F"))));
	}

	/**
	 * Test avoidance of infinite loops in cyclic graph.
	 * 
	 * <pre>
	 *          C-+
	 *          | |
	 *          B-+
	 *          |
	 *          A
	 * </pre>
	 */
	@Test
	public void testPrune() {
		IGraph<E> graph = createGraph();

		graph.addChildren(toType("A"), ImmutableSet.of(toType("B")));
		graph.addChildren(toType("B"), ImmutableSet.of(toType("C")));
		graph.addChildren(toType("C"), ImmutableSet.of(toType("B")));
		PruningIterator<E> iterator = graph.breadthFirstIterator();
		while (iterator.hasNext()) {
			iterator.next();
			iterator.prune();
		}
	}

	/**
	 * Tests breadth first iteration with the following Graph:
	 * 
	 * <pre>
	 *     A       I    J
	 *    / \     /    / \
	 *   B   C   G    K   L
	 *  /   / \ / \      / \
	 * D   E   F   H    M   N
	 * 
	 * We expect our iteration to go in the following order:
	 * three first items, in unspecified order : A, I, J
	 * next five, in unspecified order :         B, C, G, K, L
	 * finally, still in unspecified order :     D, E, F, H, M, N
	 * </pre>
	 */
	@Test
	public void testBreadthFirstIteration() {
		IGraph<E> graph = createGraph();

		graph.addChildren(toType("A"), ImmutableSet.of(toType("B"), toType("C")));
		graph.addChildren(toType("B"), ImmutableSet.of(toType("D")));
		graph.addChildren(toType("C"), ImmutableSet.of(toType("E"), toType("F")));
		graph.addChildren(toType("I"), ImmutableSet.of(toType("G")));
		graph.addChildren(toType("G"), ImmutableSet.of(toType("F"), toType("H")));
		graph.addChildren(toType("J"), ImmutableSet.of(toType("K"), toType("L")));
		graph.addChildren(toType("L"), ImmutableSet.of(toType("M"), toType("N")));

		Set<E> firstThree = Sets.newHashSet(toType("A"), toType("I"), toType("J"));
		Set<E> nextFive = Sets.newHashSet(toType("B"), toType("C"), toType("G"), toType("K"), toType("L"));
		Set<E> finalSix = Sets.newHashSet(toType("D"), toType("E"), toType("F"), toType("H"), toType("M"),
				toType("N"));

		PruningIterator<E> iterator = graph.breadthFirstIterator();
		assertTrue(firstThree.remove(iterator.next()));
		assertTrue(firstThree.remove(iterator.next()));
		assertTrue(firstThree.remove(iterator.next()));
		assertTrue(firstThree.isEmpty());

		assertTrue(nextFive.remove(iterator.next()));
		assertTrue(nextFive.remove(iterator.next()));
		assertTrue(nextFive.remove(iterator.next()));
		assertTrue(nextFive.remove(iterator.next()));
		assertTrue(nextFive.remove(iterator.next()));
		assertTrue(nextFive.isEmpty());

		assertTrue(finalSix.remove(iterator.next()));
		assertTrue(finalSix.remove(iterator.next()));
		assertTrue(finalSix.remove(iterator.next()));
		assertTrue(finalSix.remove(iterator.next()));
		assertTrue(finalSix.remove(iterator.next()));
		assertTrue(finalSix.remove(iterator.next()));
		assertTrue(finalSix.isEmpty());
		assertFalse(iterator.hasNext());
	}

	/**
	 * This test case ensures that pruned elements are not returned in a scenario where the next iterator in
	 * the graph has already been prepared. This test uses the following graph:
	 * 
	 * <pre>
	 * A   B
	 *  \ /
	 *   C
	 * </pre>
	 */
	@Test
	public void testBreadthFirstIteration_MultipleParents() {
		IGraph<E> graph = createGraph();
		graph.addChildren(toType("A"), ImmutableSet.of(toType("C")));
		graph.addChildren(toType("B"), ImmutableSet.of(toType("C")));

		PruningIterator<E> breadthFirstIterator = graph.breadthFirstIterator();

		assertTrue(breadthFirstIterator.hasNext());
		assertEquals(toType("A"), breadthFirstIterator.next());

		assertTrue(breadthFirstIterator.hasNext());
		assertEquals(toType("B"), breadthFirstIterator.next());

		breadthFirstIterator.prune(); // prune all children of B: [C]

		assertFalse(breadthFirstIterator.hasNext());
	}

	/**
	 * This test case ensures that pruned elements are not returned in a scenario where the next iterator in
	 * the graph has already been prepared:
	 * 
	 * <pre>
	 *   _A_   B
	 *  / | \ /
	 * D  E  C
	 * </pre>
	 */
	@Test
	public void testBreadthFirstIteration_MultipleParentsMultipleChildren() {
		IGraph<E> graph = createGraph();
		graph.addChildren(toType("A"), ImmutableSet.of(toType("C"), toType("D"), toType("E")));
		graph.addChildren(toType("B"), ImmutableSet.of(toType("C"), toType("D")));

		PruningIterator<E> breadthFirstIterator = graph.breadthFirstIterator();

		assertTrue(breadthFirstIterator.hasNext());
		assertEquals(toType("A"), breadthFirstIterator.next());

		assertTrue(breadthFirstIterator.hasNext());
		assertEquals(toType("B"), breadthFirstIterator.next());

		breadthFirstIterator.prune(); // prune all children of B: [C, D]

		assertTrue(breadthFirstIterator.hasNext());
		assertEquals(toType("E"), breadthFirstIterator.next());

		assertFalse(breadthFirstIterator.hasNext());
	}

	/**
	 * This test case ensures that elements over multiple lvels are pruned correctly and are not returned
	 * through another path in the graph:
	 * 
	 * <pre>
	 *   A   B
	 *  / \ /
	 * D   C
	 *  \ /
	 *   E
	 * </pre>
	 */
	@Test
	public void testBreadthFirstIteration_PruneMultipleLevels() {
		IGraph<E> graph = new Graph<E>();
		// first level
		graph.addChildren(toType("A"), ImmutableSet.of(toType("D"), toType("C")));
		graph.addChildren(toType("B"), ImmutableSet.of(toType("C")));

		// second level
		graph.addChildren(toType("D"), ImmutableSet.of(toType("E")));
		graph.addChildren(toType("C"), ImmutableSet.of(toType("E")));

		PruningIterator<E> breadthFirstIterator = graph.breadthFirstIterator();

		// iterate over first level
		assertTrue(breadthFirstIterator.hasNext());
		assertEquals(toType("A"), breadthFirstIterator.next());

		assertTrue(breadthFirstIterator.hasNext());
		assertEquals(toType("B"), breadthFirstIterator.next());

		// prune all children of B: [C and also E from the level below]
		breadthFirstIterator.prune();

		assertTrue(breadthFirstIterator.hasNext());
		assertEquals(toType("D"), breadthFirstIterator.next());

		// no more children as C and E have been pruned
		assertFalse(breadthFirstIterator.hasNext());
	}

	@Test
	public void testTreeIteration_1() {
		IGraph<E> graph = getAcyclicGraph();

		Iterator<E> iteratorOnA = graph.depthFirstIterator(toType("A"));
		assertEquals(toType("A"), iteratorOnA.next());
		assertEquals(toType("B"), iteratorOnA.next());
		assertEquals(toType("D"), iteratorOnA.next());
		assertEquals(toType("C"), iteratorOnA.next());
		assertEquals(toType("E"), iteratorOnA.next());
		assertEquals(toType("F"), iteratorOnA.next());
		assertFalse(iteratorOnA.hasNext());
	}

	@Test
	public void testTreeIteration_2() {
		IGraph<E> graph = getAcyclicGraph();

		Iterator<E> iteratorOnC = graph.depthFirstIterator(toType("C"));
		assertEquals(toType("C"), iteratorOnC.next());
		assertEquals(toType("E"), iteratorOnC.next());
		assertEquals(toType("F"), iteratorOnC.next());
		assertFalse(iteratorOnC.hasNext());
	}

	@Test
	public void testTreeIteration_3() {
		IGraph<E> graph = getAcyclicGraph();

		Iterator<E> iteratorOnI = graph.depthFirstIterator(toType("I"));
		assertEquals(toType("I"), iteratorOnI.next());
		assertEquals(toType("G"), iteratorOnI.next());
		assertEquals(toType("F"), iteratorOnI.next());
		assertEquals(toType("H"), iteratorOnI.next());
		assertFalse(iteratorOnI.hasNext());
	}

	@Test
	public void testTreeIteration_4() {
		IGraph<E> graph = getAcyclicGraph();

		Iterator<E> iteratorOnJ = graph.depthFirstIterator(toType("J"));
		assertEquals(toType("J"), iteratorOnJ.next());
		assertEquals(toType("K"), iteratorOnJ.next());
		assertEquals(toType("L"), iteratorOnJ.next());
		assertEquals(toType("M"), iteratorOnJ.next());
		assertEquals(toType("N"), iteratorOnJ.next());
		assertFalse(iteratorOnJ.hasNext());
	}

	@Test
	public void testDepthIterationWithCycles_1() {
		IGraph<E> graph = getGraphWithCycles();

		Iterator<E> iteratorOnA = graph.depthFirstIterator(toType("A"));
		assertEquals(toType("A"), iteratorOnA.next());
		assertEquals(toType("B"), iteratorOnA.next());
		assertEquals(toType("D"), iteratorOnA.next());
		assertEquals(toType("E"), iteratorOnA.next());
		assertEquals(toType("C"), iteratorOnA.next());
		assertEquals(toType("F"), iteratorOnA.next());
		assertEquals(toType("H"), iteratorOnA.next());

		assertFalse(iteratorOnA.hasNext());
	}

	@Test
	public void testDepthIterationWithCycles_2() {
		IGraph<E> graph = getGraphWithCycles();

		Iterator<E> iteratorOnC = graph.depthFirstIterator(toType("C"));
		assertEquals(toType("C"), iteratorOnC.next());
		assertEquals(toType("A"), iteratorOnC.next());
		assertEquals(toType("B"), iteratorOnC.next());
		assertEquals(toType("D"), iteratorOnC.next());
		assertEquals(toType("E"), iteratorOnC.next());
		assertEquals(toType("F"), iteratorOnC.next());
		assertEquals(toType("H"), iteratorOnC.next());

		assertFalse(iteratorOnC.hasNext());
	}

	@Test
	public void testDepthIterationWithCycles_3() {
		IGraph<E> graph = getGraphWithCycles();

		Iterator<E> iteratorOnI = graph.depthFirstIterator(toType("I"));
		assertEquals(toType("I"), iteratorOnI.next());
		assertEquals(toType("G"), iteratorOnI.next());
		assertEquals(toType("F"), iteratorOnI.next());
		assertEquals(toType("H"), iteratorOnI.next());

		assertFalse(iteratorOnI.hasNext());
	}

	@Test
	public void testDepthIterationWithCycles_4() {
		IGraph<E> graph = getGraphWithCycles();

		Iterator<E> iteratorOnJ = graph.depthFirstIterator(toType("J"));
		assertEquals(toType("J"), iteratorOnJ.next());
		assertEquals(toType("K"), iteratorOnJ.next());
		assertEquals(toType("M"), iteratorOnJ.next());
		assertEquals(toType("L"), iteratorOnJ.next());
		assertEquals(toType("N"), iteratorOnJ.next());

		assertFalse(iteratorOnJ.hasNext());
	}

	/**
	 * Test the BreadthFirstIterator with the following cyclic graph:
	 * 
	 * <pre>
	 *          A
	 *         / \
	 *        B = C
	 * </pre>
	 */
	@Test
	public void testBug503035_1() {
		IGraph<E> graph = createGraph();

		graph.addChildren(toType("A"), ImmutableSet.of(toType("B"), toType("C")));
		graph.addChildren(toType("B"), ImmutableSet.of(toType("C")));
		graph.addChildren(toType("C"), ImmutableSet.of(toType("B")));

		Iterator<E> it = graph.breadthFirstIterator();
		assertTrue(it.hasNext());
		assertEquals(toType("A"), it.next());
		assertTrue(it.hasNext());
		assertEquals(toType("B"), it.next());
		assertTrue(it.hasNext());
		assertEquals(toType("C"), it.next());
		assertFalse(it.hasNext());
	}

	/**
	 * Test the BreadthFirstIterator with the following cyclic graph:
	 * 
	 * <pre>
	 *          A
	 *         / \
	 *        B = C
	 *       /
	 *      D
	 * </pre>
	 */
	@Test
	public void testBug503035_2() {
		IGraph<E> graph = createGraph();

		graph.addChildren(toType("A"), ImmutableSet.of(toType("B"), toType("C")));
		graph.addChildren(toType("B"), ImmutableSet.of(toType("D"), toType("C")));
		graph.addChildren(toType("C"), ImmutableSet.of(toType("B")));

		Iterator<E> it = graph.breadthFirstIterator();
		assertTrue(it.hasNext());
		assertEquals(toType("A"), it.next());
		assertTrue(it.hasNext());
		assertEquals(toType("B"), it.next());
		assertTrue(it.hasNext());
		assertEquals(toType("C"), it.next());
		assertTrue(it.hasNext());
		assertEquals(toType("D"), it.next());
		assertFalse(it.hasNext());
	}

	/**
	 * Test the BreadthFirstIterator with the following cyclic graph:
	 * 
	 * <pre>
	 *        A   B 
	 *        |   |
	 *        C = D
	 * </pre>
	 */
	@Test
	public void testBug503035_3() {
		IGraph<E> graph = createGraph();

		graph.addChildren(toType("A"), ImmutableSet.of(toType("C")));
		graph.addChildren(toType("B"), ImmutableSet.of(toType("D")));
		graph.addChildren(toType("C"), ImmutableSet.of(toType("D")));
		graph.addChildren(toType("D"), ImmutableSet.of(toType("C")));

		Iterator<E> it = graph.breadthFirstIterator();
		assertTrue(it.hasNext());
		assertEquals(toType("A"), it.next());
		assertTrue(it.hasNext());
		assertEquals(toType("B"), it.next());
		assertTrue(it.hasNext());
		assertEquals(toType("C"), it.next());
		assertTrue(it.hasNext());
		assertEquals(toType("D"), it.next());
		assertFalse(it.hasNext());
	}

	/**
	 * Test the BreadthFirstIterator with the following cyclic graph:
	 * 
	 * <pre>
	 *          A
	 *         /|\
	 *        / | \
	 *       B  C  D
	 *       \\===//
	 * </pre>
	 */
	@Test
	public void testBug503035_4() {
		IGraph<E> graph = createGraph();

		graph.addChildren(toType("A"), ImmutableSet.of(toType("B"), toType("C"), toType("D")));
		graph.addChildren(toType("B"), ImmutableSet.of(toType("D")));
		graph.addChildren(toType("D"), ImmutableSet.of(toType("B")));

		Iterator<E> it = graph.breadthFirstIterator();
		assertTrue(it.hasNext());
		assertEquals(toType("A"), it.next());
		assertTrue(it.hasNext());
		assertEquals(toType("B"), it.next());
		assertTrue(it.hasNext());
		assertEquals(toType("C"), it.next());
		assertTrue(it.hasNext());
		assertEquals(toType("D"), it.next());
		assertFalse(it.hasNext());
	}

	/**
	 * @return The following acyclic graph:
	 * 
	 *         <pre>
	 *     A       I    J
	 *    / \     /    / \
	 *   B   C   G    K   L
	 *  /   / \ / \      / \
	 * D   E   F   H    M   N
	 *         </pre>
	 */
	protected IGraph<E> getAcyclicGraph() {
		IGraph<E> graph = createGraph();

		graph.addChildren(toType("A"), ImmutableSet.of(toType("B"), toType("C")));
		graph.addChildren(toType("B"), ImmutableSet.of(toType("D")));
		graph.addChildren(toType("C"), ImmutableSet.of(toType("E"), toType("F")));
		graph.addChildren(toType("I"), ImmutableSet.of(toType("G")));
		graph.addChildren(toType("G"), ImmutableSet.of(toType("F"), toType("H")));
		graph.addChildren(toType("J"), ImmutableSet.of(toType("K"), toType("L")));
		graph.addChildren(toType("L"), ImmutableSet.of(toType("M"), toType("N")));

		return graph;
	}

	/**
	 * @return The following cyclic graph:
	 * 
	 *         <pre>
	 *     A       I    J
	 *    / \\    /    /  \
	 *   B   C   G    K    L
	 *  /   / \ / \    \ // \
	 * D - E   F = H    M    N
	 *         </pre>
	 */
	protected IGraph<E> getGraphWithCycles() {
		IGraph<E> graph = createGraph();

		graph.addChildren(toType("A"), ImmutableSet.of(toType("B"), toType("C")));
		graph.addChildren(toType("B"), ImmutableSet.of(toType("D")));
		graph.addChildren(toType("D"), ImmutableSet.of(toType("E")));
		graph.addChildren(toType("C"), ImmutableSet.of(toType("A"), toType("E"), toType("F")));
		graph.addChildren(toType("I"), ImmutableSet.of(toType("G")));
		graph.addChildren(toType("G"), ImmutableSet.of(toType("F"), toType("H")));
		graph.addChildren(toType("J"), ImmutableSet.of(toType("K"), toType("L")));
		graph.addChildren(toType("K"), ImmutableSet.of(toType("M")));
		graph.addChildren(toType("M"), ImmutableSet.of(toType("L")));
		graph.addChildren(toType("L"), ImmutableSet.of(toType("M"), toType("N")));
		graph.addChildren(toType("F"), ImmutableSet.of(toType("H")));
		graph.addChildren(toType("H"), ImmutableSet.of(toType("F")));

		return graph;
	}
}

Back to the top