aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/test/java/com/google/devtools/build/lib/collect/CollectionUtilsTest.java
blob: 40d4db2cddab1212d13cf1115ced9c1c9fca2ee8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// Copyright 2014 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 static org.junit.Assert.fail;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.google.devtools.build.lib.collect.nestedset.NestedSet;
import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder;
import java.util.EnumSet;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

/**
 * Tests for {@link CollectionUtils}.
 */

@RunWith(JUnit4.class)
public class CollectionUtilsTest {

  @Test
  public void testDuplicatedElementsOf() {
    assertDups(ImmutableList.<Integer>of(), ImmutableSet.<Integer>of());
    assertDups(ImmutableList.of(0), ImmutableSet.<Integer>of());
    assertDups(ImmutableList.of(0, 0, 0), ImmutableSet.of(0));
    assertDups(ImmutableList.of(1, 2, 3, 1, 2, 3), ImmutableSet.of(1, 2, 3));
    assertDups(ImmutableList.of(1, 2, 3, 1, 2, 3, 4), ImmutableSet.of(1, 2, 3));
    assertDups(ImmutableList.of(1, 2, 3, 4), ImmutableSet.<Integer>of());
  }

  private static void assertDups(List<Integer> collection, Set<Integer> dups) {
    assertThat(CollectionUtils.duplicatedElementsOf(collection)).isEqualTo(dups);
  }

  @Test
  public void testIsImmutable() throws Exception {
    assertThat(CollectionUtils.isImmutable(ImmutableList.of(1, 2, 3))).isTrue();
    assertThat(CollectionUtils.isImmutable(ImmutableSet.of(1, 2, 3))).isTrue();

    NestedSet<Integer> ns = NestedSetBuilder.<Integer>compileOrder()
        .add(1).add(2).add(3).build();
    assertThat(CollectionUtils.isImmutable(ns)).isTrue();

    NestedSet<Integer> ns2 = NestedSetBuilder.<Integer>linkOrder().add(1).add(2).add(3).build();
    assertThat(CollectionUtils.isImmutable(ns2)).isTrue();

    Iterable<Integer> chain = IterablesChain.<Integer>builder().addElement(1).build();

    assertThat(CollectionUtils.isImmutable(chain)).isTrue();

    assertThat(CollectionUtils.isImmutable(Lists.newArrayList())).isFalse();
    assertThat(CollectionUtils.isImmutable(Lists.newLinkedList())).isFalse();
    assertThat(CollectionUtils.isImmutable(Sets.newHashSet())).isFalse();
    assertThat(CollectionUtils.isImmutable(Sets.newLinkedHashSet())).isFalse();

    // The result of Iterables.concat() actually is immutable, but we have no way of checking if
    // a given Iterable comes from concat().
    assertThat(CollectionUtils.isImmutable(Iterables.concat(ns, ns2))).isFalse();

    // We can override the check by using the ImmutableIterable wrapper.
    assertThat(CollectionUtils.isImmutable(ImmutableIterable.from(Iterables.concat(ns, ns2))))
        .isTrue();
  }

  @Test
  public void testCheckImmutable() throws Exception {
    CollectionUtils.checkImmutable(ImmutableList.of(1, 2, 3));
    CollectionUtils.checkImmutable(ImmutableSet.of(1, 2, 3));

    try {
      CollectionUtils.checkImmutable(Lists.newArrayList(1, 2, 3));
    } catch (IllegalStateException e) {
      return;
    }
    fail();
  }

  @Test
  public void testMakeImmutable() throws Exception {
    Iterable<Integer> immutableList = ImmutableList.of(1, 2, 3);
    assertThat(CollectionUtils.makeImmutable(immutableList)).isSameAs(immutableList);

    Iterable<Integer> mutableList = Lists.newArrayList(1, 2, 3);
    Iterable<Integer> converted = CollectionUtils.makeImmutable(mutableList);
    assertThat(converted).isNotSameAs(mutableList);
    assertThat(ImmutableList.copyOf(converted)).isEqualTo(mutableList);
  }

  private static enum Small { ALPHA, BRAVO }
  private static enum Large {
    L0, L1, L2, L3, L4, L5, L6, L7, L8, L9,
    L10, L11, L12, L13, L14, L15, L16, L17, L18, L19,
    L20, L21, L22, L23, L24, L25, L26, L27, L28, L29,
    L30, L31,
  }

  private static enum TooLarge {
    T0, T1, T2, T3, T4, T5, T6, T7, T8, T9,
    T10, T11, T12, T13, T14, T15, T16, T17, T18, T19,
    T20, T21, T22, T23, T24, T25, T26, T27, T28, T29,
    T30, T31, T32,
  }

  private static enum Medium {
    ONE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT,
  }

  private <T extends Enum<T>> void assertAllDifferent(Class<T> clazz) throws Exception {
    Set<EnumSet<T>> allSets = new HashSet<>();

    int maxBits = 1 << clazz.getEnumConstants().length;
    for (int i = 0; i < maxBits; i++) {
      EnumSet<T> set = CollectionUtils.fromBits(i, clazz);
      int back = CollectionUtils.toBits(set);
      assertThat(i).isEqualTo(back); // Assert that a roundtrip is idempotent
      allSets.add(set);
    }

    assertThat(allSets).hasSize(maxBits); // Assert that every decoded value is different
  }

  @Test
  public void testEnumBitfields() throws Exception {
    assertThat(CollectionUtils.<Small>toBits()).isEqualTo(0);
    assertThat(CollectionUtils.fromBits(0, Small.class)).isEqualTo(EnumSet.noneOf(Small.class));
    assertThat(CollectionUtils.toBits(Small.ALPHA, Small.BRAVO)).isEqualTo(3);
    assertThat(CollectionUtils.toBits(Medium.TWO, Medium.FOUR)).isEqualTo(10);
    assertThat(CollectionUtils.fromBits(192, Medium.class))
        .isEqualTo(EnumSet.of(Medium.SEVEN, Medium.EIGHT));

    assertAllDifferent(Small.class);
    assertAllDifferent(Medium.class);
    assertAllDifferent(Large.class);

    try {
      CollectionUtils.toBits(TooLarge.T32);
      fail();
    } catch (IllegalArgumentException e) {
      // good
    }

    try {
      CollectionUtils.fromBits(0, TooLarge.class);
      fail();
    } catch (IllegalArgumentException e) {
      // good
    }
  }
}