blob: 28f806538fa4d01b35f58b4c3d2d8faa5298c3e4 [file] [log] [blame]
Damien Martin-Guillerezf88f4d82015-09-25 13:56:55 +00001// Copyright 2014 The Bazel Authors. All rights reserved.
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +01002//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14package com.google.devtools.build.skyframe;
15
lberkiaea56b32017-05-30 12:35:33 +020016import static com.google.common.truth.Truth.assertThat;
jcaterecd2abd2019-04-30 13:31:13 -070017import static com.google.devtools.build.lib.testutil.MoreAsserts.assertThrows;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010018
19import com.google.common.collect.ImmutableList;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010020import org.junit.Test;
21import org.junit.runner.RunWith;
22import org.junit.runners.JUnit4;
23
24/** Simple tests for {@link CycleDeduper}. */
25@RunWith(JUnit4.class)
26public class CycleDeduperTest {
27
28 private CycleDeduper<String> cycleDeduper = new CycleDeduper<>();
29
30 @Test
31 public void simple() throws Exception {
lberkiaea56b32017-05-30 12:35:33 +020032 assertThat(cycleDeduper.seen(ImmutableList.of("a", "b"))).isTrue();
33 assertThat(cycleDeduper.seen(ImmutableList.of("a", "b"))).isFalse();
34 assertThat(cycleDeduper.seen(ImmutableList.of("b", "a"))).isFalse();
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010035
lberkiaea56b32017-05-30 12:35:33 +020036 assertThat(cycleDeduper.seen(ImmutableList.of("a", "b", "c"))).isTrue();
37 assertThat(cycleDeduper.seen(ImmutableList.of("b", "c", "a"))).isFalse();
38 assertThat(cycleDeduper.seen(ImmutableList.of("c", "a", "b"))).isFalse();
39 assertThat(cycleDeduper.seen(ImmutableList.of("b", "a", "c"))).isTrue();
40 assertThat(cycleDeduper.seen(ImmutableList.of("c", "b", "a"))).isFalse();
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010041 }
42
43 @Test
44 public void badCycle_Empty() throws Exception {
jcaterecd2abd2019-04-30 13:31:13 -070045 assertThrows(IllegalStateException.class, () -> cycleDeduper.seen(ImmutableList.<String>of()));
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010046 }
47
48 @Test
49 public void badCycle_NonUniqueMembers() throws Exception {
jcaterecd2abd2019-04-30 13:31:13 -070050 assertThrows(
51 IllegalStateException.class,
52 () -> cycleDeduper.seen(ImmutableList.<String>of("a", "b", "a")));
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010053 }
54}