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.lib.collect; |
| 15 | |
Ulf Adams | 795895a | 2015-03-06 15:58:35 +0000 | [diff] [blame] | 16 | import static com.google.common.truth.Truth.assertThat; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 17 | import static org.junit.Assert.fail; |
| 18 | |
| 19 | import com.google.common.collect.ImmutableList; |
| 20 | import com.google.common.collect.ImmutableSet; |
| 21 | import com.google.common.collect.Iterables; |
| 22 | import com.google.common.collect.Lists; |
| 23 | import com.google.common.collect.Sets; |
| 24 | import com.google.devtools.build.lib.collect.nestedset.NestedSet; |
| 25 | import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 26 | import java.util.EnumSet; |
| 27 | import java.util.HashSet; |
| 28 | import java.util.List; |
| 29 | import java.util.Set; |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 30 | import org.junit.Test; |
| 31 | import org.junit.runner.RunWith; |
| 32 | import org.junit.runners.JUnit4; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 33 | |
| 34 | /** |
| 35 | * Tests for {@link CollectionUtils}. |
| 36 | */ |
| 37 | |
| 38 | @RunWith(JUnit4.class) |
| 39 | public class CollectionUtilsTest { |
| 40 | |
| 41 | @Test |
| 42 | public void testDuplicatedElementsOf() { |
| 43 | assertDups(ImmutableList.<Integer>of(), ImmutableSet.<Integer>of()); |
| 44 | assertDups(ImmutableList.of(0), ImmutableSet.<Integer>of()); |
| 45 | assertDups(ImmutableList.of(0, 0, 0), ImmutableSet.of(0)); |
| 46 | assertDups(ImmutableList.of(1, 2, 3, 1, 2, 3), ImmutableSet.of(1, 2, 3)); |
| 47 | assertDups(ImmutableList.of(1, 2, 3, 1, 2, 3, 4), ImmutableSet.of(1, 2, 3)); |
| 48 | assertDups(ImmutableList.of(1, 2, 3, 4), ImmutableSet.<Integer>of()); |
| 49 | } |
| 50 | |
| 51 | private static void assertDups(List<Integer> collection, Set<Integer> dups) { |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 52 | assertThat(CollectionUtils.duplicatedElementsOf(collection)).isEqualTo(dups); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | @Test |
| 56 | public void testIsImmutable() throws Exception { |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 57 | assertThat(CollectionUtils.isImmutable(ImmutableList.of(1, 2, 3))).isTrue(); |
| 58 | assertThat(CollectionUtils.isImmutable(ImmutableSet.of(1, 2, 3))).isTrue(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 59 | |
| 60 | NestedSet<Integer> ns = NestedSetBuilder.<Integer>compileOrder() |
| 61 | .add(1).add(2).add(3).build(); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 62 | assertThat(CollectionUtils.isImmutable(ns)).isTrue(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 63 | |
| 64 | NestedSet<Integer> ns2 = NestedSetBuilder.<Integer>linkOrder().add(1).add(2).add(3).build(); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 65 | assertThat(CollectionUtils.isImmutable(ns2)).isTrue(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 66 | |
tomlu | 641569a | 2018-04-18 08:41:55 -0700 | [diff] [blame] | 67 | Iterable<Integer> chain = IterablesChain.<Integer>builder().addElement(1).build(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 68 | |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 69 | assertThat(CollectionUtils.isImmutable(chain)).isTrue(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 70 | |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 71 | assertThat(CollectionUtils.isImmutable(Lists.newArrayList())).isFalse(); |
| 72 | assertThat(CollectionUtils.isImmutable(Lists.newLinkedList())).isFalse(); |
| 73 | assertThat(CollectionUtils.isImmutable(Sets.newHashSet())).isFalse(); |
| 74 | assertThat(CollectionUtils.isImmutable(Sets.newLinkedHashSet())).isFalse(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 75 | |
| 76 | // The result of Iterables.concat() actually is immutable, but we have no way of checking if |
| 77 | // a given Iterable comes from concat(). |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 78 | assertThat(CollectionUtils.isImmutable(Iterables.concat(ns, ns2))).isFalse(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 79 | |
| 80 | // We can override the check by using the ImmutableIterable wrapper. |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 81 | assertThat(CollectionUtils.isImmutable(ImmutableIterable.from(Iterables.concat(ns, ns2)))) |
| 82 | .isTrue(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | @Test |
| 86 | public void testCheckImmutable() throws Exception { |
| 87 | CollectionUtils.checkImmutable(ImmutableList.of(1, 2, 3)); |
| 88 | CollectionUtils.checkImmutable(ImmutableSet.of(1, 2, 3)); |
| 89 | |
| 90 | try { |
| 91 | CollectionUtils.checkImmutable(Lists.newArrayList(1, 2, 3)); |
| 92 | } catch (IllegalStateException e) { |
| 93 | return; |
| 94 | } |
| 95 | fail(); |
| 96 | } |
| 97 | |
| 98 | @Test |
| 99 | public void testMakeImmutable() throws Exception { |
| 100 | Iterable<Integer> immutableList = ImmutableList.of(1, 2, 3); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 101 | assertThat(CollectionUtils.makeImmutable(immutableList)).isSameAs(immutableList); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 102 | |
| 103 | Iterable<Integer> mutableList = Lists.newArrayList(1, 2, 3); |
| 104 | Iterable<Integer> converted = CollectionUtils.makeImmutable(mutableList); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 105 | assertThat(converted).isNotSameAs(mutableList); |
| 106 | assertThat(ImmutableList.copyOf(converted)).isEqualTo(mutableList); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | private static enum Small { ALPHA, BRAVO } |
| 110 | private static enum Large { |
| 111 | L0, L1, L2, L3, L4, L5, L6, L7, L8, L9, |
| 112 | L10, L11, L12, L13, L14, L15, L16, L17, L18, L19, |
| 113 | L20, L21, L22, L23, L24, L25, L26, L27, L28, L29, |
| 114 | L30, L31, |
| 115 | } |
| 116 | |
| 117 | private static enum TooLarge { |
| 118 | T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, |
| 119 | T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, |
| 120 | T20, T21, T22, T23, T24, T25, T26, T27, T28, T29, |
| 121 | T30, T31, T32, |
| 122 | } |
| 123 | |
| 124 | private static enum Medium { |
| 125 | ONE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, |
| 126 | } |
| 127 | |
| 128 | private <T extends Enum<T>> void assertAllDifferent(Class<T> clazz) throws Exception { |
| 129 | Set<EnumSet<T>> allSets = new HashSet<>(); |
| 130 | |
| 131 | int maxBits = 1 << clazz.getEnumConstants().length; |
| 132 | for (int i = 0; i < maxBits; i++) { |
| 133 | EnumSet<T> set = CollectionUtils.fromBits(i, clazz); |
| 134 | int back = CollectionUtils.toBits(set); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 135 | assertThat(i).isEqualTo(back); // Assert that a roundtrip is idempotent |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 136 | allSets.add(set); |
| 137 | } |
| 138 | |
Ulf Adams | 795895a | 2015-03-06 15:58:35 +0000 | [diff] [blame] | 139 | assertThat(allSets).hasSize(maxBits); // Assert that every decoded value is different |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | @Test |
| 143 | public void testEnumBitfields() throws Exception { |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 144 | assertThat(CollectionUtils.<Small>toBits()).isEqualTo(0); |
| 145 | assertThat(CollectionUtils.fromBits(0, Small.class)).isEqualTo(EnumSet.noneOf(Small.class)); |
| 146 | assertThat(CollectionUtils.toBits(Small.ALPHA, Small.BRAVO)).isEqualTo(3); |
| 147 | assertThat(CollectionUtils.toBits(Medium.TWO, Medium.FOUR)).isEqualTo(10); |
| 148 | assertThat(CollectionUtils.fromBits(192, Medium.class)) |
| 149 | .isEqualTo(EnumSet.of(Medium.SEVEN, Medium.EIGHT)); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 150 | |
| 151 | assertAllDifferent(Small.class); |
| 152 | assertAllDifferent(Medium.class); |
| 153 | assertAllDifferent(Large.class); |
| 154 | |
| 155 | try { |
| 156 | CollectionUtils.toBits(TooLarge.T32); |
| 157 | fail(); |
| 158 | } catch (IllegalArgumentException e) { |
| 159 | // good |
| 160 | } |
| 161 | |
| 162 | try { |
| 163 | CollectionUtils.fromBits(0, TooLarge.class); |
| 164 | fail(); |
| 165 | } catch (IllegalArgumentException e) { |
| 166 | // good |
| 167 | } |
| 168 | } |
| 169 | } |