Damien Martin-Guillerez | f88f4d8 | 2015-09-25 13:56:55 +0000 | [diff] [blame] | 1 | // Copyright 2014 The Bazel Authors. All rights reserved. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 2 | // |
| 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. |
| 14 | package com.google.devtools.build.skyframe; |
| 15 | |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 16 | import static com.google.common.truth.Truth.assertThat; |
jcater | ecd2abd | 2019-04-30 13:31:13 -0700 | [diff] [blame] | 17 | import static com.google.devtools.build.lib.testutil.MoreAsserts.assertThrows; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 18 | |
| 19 | import com.google.common.collect.ImmutableList; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 20 | import org.junit.Test; |
| 21 | import org.junit.runner.RunWith; |
| 22 | import org.junit.runners.JUnit4; |
| 23 | |
| 24 | /** Simple tests for {@link CycleDeduper}. */ |
| 25 | @RunWith(JUnit4.class) |
| 26 | public class CycleDeduperTest { |
| 27 | |
| 28 | private CycleDeduper<String> cycleDeduper = new CycleDeduper<>(); |
| 29 | |
| 30 | @Test |
| 31 | public void simple() throws Exception { |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 32 | 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 Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 35 | |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 36 | 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 Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | @Test |
| 44 | public void badCycle_Empty() throws Exception { |
jcater | ecd2abd | 2019-04-30 13:31:13 -0700 | [diff] [blame] | 45 | assertThrows(IllegalStateException.class, () -> cycleDeduper.seen(ImmutableList.<String>of())); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | @Test |
| 49 | public void badCycle_NonUniqueMembers() throws Exception { |
jcater | ecd2abd | 2019-04-30 13:31:13 -0700 | [diff] [blame] | 50 | assertThrows( |
| 51 | IllegalStateException.class, |
| 52 | () -> cycleDeduper.seen(ImmutableList.<String>of("a", "b", "a"))); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 53 | } |
| 54 | } |