Browse souce code repository at: https://github.com/jjYBdx4IL/java-evaluation/blob/master/src/test/java/tests/java/util/CollectionsTest.java
/*
 * #%L
 * evaluation
 * %%
 * Copyright (C) 2016 Github jjYBdx4IL Projects
 * %%
 * #L%
 */
package tests.java.util;

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

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.junit.Test;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Locale;
import java.util.Random;

/**
 * @author Github jjYBdx4IL Projects
 *
 */
public class CollectionsTest {

    private static final Logger log = Logger.getLogger(CollectionsTest.class);
    private static final int LIST_LENGTH = 1000 * 1000;
    private static final long SLOW_ITERATION_MILLIS = 1000L;
    private static final int MAX_TRIES = 100;
    
    @Test
    public void testBinarySearch() {
        Random r = new Random(0);
        List<Double> list = new ArrayList<>(LIST_LENGTH);
        for (int i = 0; i < LIST_LENGTH; i++) {
            list.add(r.nextDouble());
        }
        Collections.sort(list);
        list = new ArrayList<>(list);

        int nTries = 0;
        double speedUp = 0.0;
        double minExpectedSpeedUp = 100.0;
        double slowFindsPerSec;
        double fastFindsPerSec;
        
        do {
            r = new Random(0);
            int nIterations = 0;
            long startTime = System.currentTimeMillis();
            do {
                int index = r.nextInt(LIST_LENGTH);
                assertEquals(index, list.indexOf(list.get(index)));
                nIterations++;
            } while (System.currentTimeMillis() < startTime + SLOW_ITERATION_MILLIS);
            long duration = System.currentTimeMillis() - startTime;
            slowFindsPerSec = (double) nIterations / duration * 1000;
            
            r = new Random(0);
            nIterations = 0;
            startTime = System.currentTimeMillis();
            do {
                int index = r.nextInt(LIST_LENGTH);
                assertEquals(index, Collections.binarySearch(list, list.get(index)));
                nIterations++;
            } while (System.currentTimeMillis() < startTime + SLOW_ITERATION_MILLIS);
            duration = System.currentTimeMillis() - startTime;
            fastFindsPerSec = (double) nIterations / duration * 1000;
            
            speedUp = fastFindsPerSec / slowFindsPerSec;
            nTries++;
        } while (speedUp < minExpectedSpeedUp && nTries < MAX_TRIES);
        
        log.info(String.format(
                Locale.ROOT,
                "binary search on ArrayList of size %d is %f times faster than ArrayList.indexOf()",
                list.size(),
                speedUp
                ));
        log.info(String.format(Locale.ROOT, "%f vs %f finds per second", fastFindsPerSec, slowFindsPerSec));
        
        assertTrue(speedUp >= minExpectedSpeedUp);
    }
        
    @Test
    public void testSortNatural() {
        List<Integer> list = new ArrayList<>();
        list.add(2);
        list.add(1);
        list.add(3);
        
        Collections.sort(list);
        assertEquals("1,2,3", StringUtils.join(list, ","));
    }
    
    @Test
    public void testSortComparator() {
        List<Integer> list = new ArrayList<>();
        list.add(2);
        list.add(1);
        list.add(3);
        
        Collections.sort(list, new Comparator<Integer>() {

            @Override
            public int compare(Integer o1, Integer o2) {
                return o1.compareTo(o2);
            }
        });
        assertEquals("1,2,3", StringUtils.join(list, ","));
    }
    
    @Test
    public void testSortComparator2() {
        List<Integer> list = new ArrayList<>();
        list.add(2);
        list.add(1);
        list.add(3);
        
        Collections.sort(list, new Comparator<Integer>() {

            @Override
            public int compare(Integer o1, Integer o2) {
                return Integer.compare(o1, o2);
            }
        });
        assertEquals("1,2,3", StringUtils.join(list, ","));
    }
}
-------------------------------------------------------------------------------
Test set: tests.java.util.CollectionsTest
-------------------------------------------------------------------------------
Tests run: 4, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 2.432 s - in tests.java.util.CollectionsTest
testSortNatural  Time elapsed: 0 s
testSortComparator  Time elapsed: 0 s
testBinarySearch  Time elapsed: 2.431 s
testSortComparator2  Time elapsed: 0.001 s