/*
 * Copyright 2015-2019 the original author or authors.
 *
 * All rights reserved. This program and the accompanying materials are
 * made available under the terms of the Eclipse Public License v2.0 which
 * accompanies this distribution and is available at
 *
 * https://www.eclipse.org/legal/epl-v20.html
 */

package org.junit.platform.commons.util;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.entry;

import java.util.HashMap;
import java.util.stream.IntStream;

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;

/**
 * @since 1.6
 */
class LruCacheTests {

	@ParameterizedTest
	@MethodSource("smallInts")
	void evictsEldestEntryWhenMaxSizeIsReached(int maxSize) throws Exception {
		var cache = new LruCache<Integer, Integer>(maxSize);

		cache.put(0, 0);
		var initialCapacity = getCapacity(cache);
		assertThat(initialCapacity).isEqualTo(nextPowerOfTwo(maxSize));

		IntStream.rangeClosed(1, maxSize).forEach(i -> cache.put(i, i));

		assertThat(getCapacity(cache)).isEqualTo(initialCapacity);
		assertThat(cache) //
				.doesNotContain(entry(0, 0)) //
				.hasSize(maxSize);
	}

	private int nextPowerOfTwo(int n) {
		return Integer.highestOneBit(n) * 2;
	}

	private static IntStream smallInts() {
		return IntStream.rangeClosed(1, 64);
	}

	private int getCapacity(HashMap<?, ?> cache) throws Exception {
		var field = HashMap.class.getDeclaredField("table");
		var value = ReflectionUtils.tryToReadFieldValue(field, cache).get();
		return ((Object[]) value).length;
	}

}

@@ -473,167 +473,34 @@
 ort 
-java.util.HashMap;%0Aimport java.util.stream.IntStream;%0A%0Aimport org.junit.jupiter.params.ParameterizedTest;%0Aimport org.junit.jupiter.params.provider.MethodSource
+org.junit.jupiter.api.Test
 ;%0A%0A/
@@ -549,53 +549,12 @@
 %0A%0A%09@
-ParameterizedTest%0A%09@MethodSource(%22smallInts%22)
+Test
 %0A%09vo
@@ -598,37 +598,9 @@
 hed(
-int maxSize) throws Exception
+)
  %7B%0A%09
@@ -643,23 +643,17 @@
 nteger%3E(
-maxSize
+1
 );%0A%0A%09%09ca
@@ -673,720 +673,100 @@
 ;%0A%09%09
-var initialCapacity = getCapacity(cache);%0A%09%09assertThat(initialCapacity).isEqualTo(nextPowerOfTwo(maxSize));%0A%0A%09%09IntStream.rangeClosed(1, maxSize).forEach(i -%3E cache.put(i, i));%0A%0A%09%09assertThat(getCapacity(cache)).isEqualTo(initialCapacity);%0A%09%09assertThat(cache) //%0A%09%09%09%09.doesNotContain(entry(0, 0)) //%0A%09%09%09%09.hasSize(maxSize);%0A%09%7D%0A%0A%09private int nextPowerOfTwo(int n) %7B%0A%09%09return Integer.highestOneBit(n) * 2;%0A%09%7D%0A%0A%09private static IntStream smallInts() %7B%0A%09%09return IntStream.rangeClosed(1, 64);%0A%09%7D%0A%0A%09private int getCapacity(HashMap%3C?, ?%3E cache) throws Exception %7B%0A%09%09var field = HashMap.class.getDeclaredField(%22table%22);%0A%09%09var value = ReflectionUtils.tryToReadFieldValue(field, cache).get();%0A%09%09return ((Object%5B%5D) value).length
+cache.put(1, 1);%0A%0A%09%09assertThat(cache) //%0A%09%09%09%09.doesNotContain(entry(0, 0)) //%0A%09%09%09%09.hasSize(1)
 ;%0A%09%7D

Remove reflective hack from LruCacheTests