Home | History | Annotate | Download | only in table
      1 /*******************************************************************************
      2  * Copyright (c) 2009, 2018 Mountainminds GmbH & Co. KG and Contributors
      3  * All rights reserved. This program and the accompanying materials
      4  * are made available under the terms of the Eclipse Public License v1.0
      5  * which accompanies this distribution, and is available at
      6  * http://www.eclipse.org/legal/epl-v10.html
      7  *
      8  * Contributors:
      9  *    Marc R. Hoffmann - initial API and implementation
     10  *
     11  *******************************************************************************/
     12 package org.jacoco.report.internal.html.table;
     13 
     14 import static org.junit.Assert.assertEquals;
     15 
     16 import java.util.ArrayList;
     17 import java.util.Arrays;
     18 import java.util.Collections;
     19 import java.util.Comparator;
     20 import java.util.List;
     21 
     22 import org.junit.Before;
     23 import org.junit.Test;
     24 
     25 /**
     26  * Unit tests for {@link SortIndex}.
     27  */
     28 public class SortIndexTest {
     29 
     30 	private SortIndex<Integer> index;
     31 
     32 	@Before
     33 	public void setup() {
     34 		index = new SortIndex<Integer>(new Comparator<Integer>() {
     35 			public int compare(Integer i1, Integer i2) {
     36 				return i1.compareTo(i2);
     37 			}
     38 		});
     39 	}
     40 
     41 	@Test
     42 	public void testEmptyList() {
     43 		index.init(Arrays.<Integer> asList());
     44 	}
     45 
     46 	@Test
     47 	public void testSingleton() {
     48 		final List<Integer> list = createList(1);
     49 		index.init(list);
     50 		assertSequence(list);
     51 	}
     52 
     53 	@Test
     54 	public void testSorted() {
     55 		final List<Integer> list = createList(20);
     56 		index.init(list);
     57 		assertSequence(list);
     58 	}
     59 
     60 	@Test
     61 	public void testIncreaseBuffer() {
     62 		index.init(createList(15));
     63 		final List<Integer> list = createList(20);
     64 		index.init(list);
     65 		assertSequence(list);
     66 	}
     67 
     68 	@Test
     69 	public void testReverse() {
     70 		final List<Integer> list = createList(57);
     71 		Collections.reverse(list);
     72 		index.init(list);
     73 		assertSequence(list);
     74 	}
     75 
     76 	@Test
     77 	public void testShuffle() {
     78 		final List<Integer> list = createList(71);
     79 		Collections.shuffle(list);
     80 		index.init(list);
     81 		assertSequence(list);
     82 	}
     83 
     84 	private List<Integer> createList(int length) {
     85 		List<Integer> list = new ArrayList<Integer>(length);
     86 		for (int i = 0; i < length; i++) {
     87 			list.add(Integer.valueOf(i));
     88 		}
     89 		return list;
     90 	}
     91 
     92 	private void assertSequence(List<Integer> list) {
     93 		int idx = 0;
     94 		for (Integer i : list) {
     95 			assertEquals(i.intValue(), index.getPosition(idx++));
     96 		}
     97 	}
     98 
     99 }
    100