From 23319fc23fd334a98e610edcfca4a1f255908e14 Mon Sep 17 00:00:00 2001 From: nharmata Date: Wed, 28 Feb 2018 13:03:12 -0800 Subject: Introduce an Extrema aggregator. RELNOTES: None PiperOrigin-RevId: 187370833 --- .../devtools/build/lib/collect/ExtremaTest.java | 84 ++++++++++++++++++++++ .../BazelPackageBuilderHelperForTesting.java | 3 +- 2 files changed, 86 insertions(+), 1 deletion(-) create mode 100644 src/test/java/com/google/devtools/build/lib/collect/ExtremaTest.java (limited to 'src/test/java/com/google/devtools/build') diff --git a/src/test/java/com/google/devtools/build/lib/collect/ExtremaTest.java b/src/test/java/com/google/devtools/build/lib/collect/ExtremaTest.java new file mode 100644 index 0000000000..1f86a042fe --- /dev/null +++ b/src/test/java/com/google/devtools/build/lib/collect/ExtremaTest.java @@ -0,0 +1,84 @@ +// Copyright 2018 The Bazel Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +package com.google.devtools.build.lib.collect; + +import static com.google.common.truth.Truth.assertThat; + +import com.google.common.collect.ImmutableList; +import java.util.Collections; +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.IntStream; +import java.util.stream.Stream; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** Tests for {@link Extrema}. */ +@RunWith(JUnit4.class) +public class ExtremaTest { + @Test + public void handlesDupes() { + Extrema extrema = Extrema.min(3); + extrema.aggregate(4); + extrema.aggregate(3); + extrema.aggregate(1); + extrema.aggregate(2); + extrema.aggregate(1); + extrema.aggregate(3); + extrema.aggregate(1); + assertThat(extrema.getExtremeElements()).containsExactly(1, 1, 1); + } + + @Test + public void minExtremaSmallK() { + runRangeTest(Extrema.min(5), 1, 100, ImmutableList.of(1, 2, 3, 4, 5)); + } + + @Test + public void minExtremaLargeK() { + runRangeTest(Extrema.min(10), 1, 5, ImmutableList.of(1, 2, 3, 4, 5)); + } + + @Test + public void maxExtremaSmallK() { + runRangeTest(Extrema.max(5), 1, 100, ImmutableList.of(100, 99, 98, 97, 96)); + } + + @Test + public void maxExtremaLargeK() { + runRangeTest(Extrema.max(10), 1, 5, ImmutableList.of(5, 4, 3, 2, 1)); + } + + private void runRangeTest( + Extrema extrema, + int leftEndpointInclusive, + int rightEndpointInclusive, + ImmutableList expected) { + assertThat(extrema.getExtremeElements()).isEmpty(); + closedRangeShuffled(leftEndpointInclusive, rightEndpointInclusive).forEach(extrema::aggregate); + assertThat(extrema.getExtremeElements()).containsExactlyElementsIn(expected).inOrder(); + extrema.clear(); + assertThat(extrema.getExtremeElements()).isEmpty(); + } + + private static Stream closedRangeShuffled( + int leftEndpointInclusive, int rightEndpointInclusive) { + List list = + IntStream.rangeClosed(leftEndpointInclusive, rightEndpointInclusive).boxed().collect( + Collectors.toList()); + Collections.shuffle(list); + return list.stream(); + } +} diff --git a/src/test/java/com/google/devtools/build/lib/testutil/BazelPackageBuilderHelperForTesting.java b/src/test/java/com/google/devtools/build/lib/testutil/BazelPackageBuilderHelperForTesting.java index b5522a0007..2bd6cf6d8e 100644 --- a/src/test/java/com/google/devtools/build/lib/testutil/BazelPackageBuilderHelperForTesting.java +++ b/src/test/java/com/google/devtools/build/lib/testutil/BazelPackageBuilderHelperForTesting.java @@ -47,7 +47,8 @@ public class BazelPackageBuilderHelperForTesting implements Package.Builder.Help } @Override - public void onLoadingComplete(Package pkg, SkylarkSemantics skylarkSemantics) { + public void onLoadingComplete( + Package pkg, SkylarkSemantics skylarkSemantics, long loadTimeNanos) { sanityCheckBazelPackageLoader(pkg, ruleClassProvider, skylarkSemantics); } -- cgit v1.2.3