aboutsummaryrefslogtreecommitdiff
path: root/org.jacoco.report.test/src/org/jacoco/report/internal/html/table/SortIndexTest.java
blob: 7ca4722657a0109c914f880f74f8b7fcd38d4a71 (plain)
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
/*******************************************************************************
 * Copyright (c) 2009, 2019 Mountainminds GmbH & Co. KG and Contributors
 * 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:
 *    Marc R. Hoffmann - initial API and implementation
 *
 *******************************************************************************/
package org.jacoco.report.internal.html.table;

import static org.junit.Assert.assertEquals;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import org.junit.Before;
import org.junit.Test;

/**
 * Unit tests for {@link SortIndex}.
 */
public class SortIndexTest {

	private SortIndex<Integer> index;

	@Before
	public void setup() {
		index = new SortIndex<Integer>(new Comparator<Integer>() {
			public int compare(Integer i1, Integer i2) {
				return i1.compareTo(i2);
			}
		});
	}

	@Test
	public void testEmptyList() {
		index.init(Arrays.<Integer> asList());
	}

	@Test
	public void testSingleton() {
		final List<Integer> list = createList(1);
		index.init(list);
		assertSequence(list);
	}

	@Test
	public void testSorted() {
		final List<Integer> list = createList(20);
		index.init(list);
		assertSequence(list);
	}

	@Test
	public void testIncreaseBuffer() {
		index.init(createList(15));
		final List<Integer> list = createList(20);
		index.init(list);
		assertSequence(list);
	}

	@Test
	public void testReverse() {
		final List<Integer> list = createList(57);
		Collections.reverse(list);
		index.init(list);
		assertSequence(list);
	}

	@Test
	public void testShuffle() {
		final List<Integer> list = createList(71);
		Collections.shuffle(list);
		index.init(list);
		assertSequence(list);
	}

	private List<Integer> createList(int length) {
		List<Integer> list = new ArrayList<Integer>(length);
		for (int i = 0; i < length; i++) {
			list.add(Integer.valueOf(i));
		}
		return list;
	}

	private void assertSequence(List<Integer> list) {
		int idx = 0;
		for (Integer i : list) {
			assertEquals(i.intValue(), index.getPosition(idx++));
		}
	}

}