BooleansTest.java revision 1d580d0f6ee4f21eb309ba7b509d2c6d671c4044
1/*
2 * Copyright (C) 2008 The Guava Authors
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.google.common.primitives;
18
19import com.google.common.annotations.GwtCompatible;
20import com.google.common.annotations.GwtIncompatible;
21import com.google.common.collect.testing.Helpers;
22import com.google.common.testing.NullPointerTester;
23import com.google.common.testing.SerializableTester;
24
25import junit.framework.TestCase;
26
27import java.util.Arrays;
28import java.util.Collection;
29import java.util.Collections;
30import java.util.Comparator;
31import java.util.List;
32
33/**
34 * Unit test for {@link Booleans}.
35 *
36 * @author Kevin Bourrillion
37 */
38@GwtCompatible(emulated = true)
39public class BooleansTest extends TestCase {
40  private static final boolean[] EMPTY = {};
41  private static final boolean[] ARRAY_FALSE = {false};
42  private static final boolean[] ARRAY_FALSE_FALSE = {false, false};
43  private static final boolean[] ARRAY_FALSE_TRUE = {false, true};
44
45  private static final boolean[] VALUES = {false, true};
46
47  public void testHashCode() {
48    assertEquals(Boolean.TRUE.hashCode(), Booleans.hashCode(true));
49    assertEquals(Boolean.FALSE.hashCode(), Booleans.hashCode(false));
50  }
51
52  public void testCompare() {
53    for (boolean x : VALUES) {
54      for (boolean y : VALUES) {
55        // note: spec requires only that the sign is the same
56        assertEquals(x + ", " + y,
57                     Boolean.valueOf(x).compareTo(y),
58                     Booleans.compare(x, y));
59      }
60    }
61  }
62
63  public void testContains() {
64    assertFalse(Booleans.contains(EMPTY, false));
65    assertFalse(Booleans.contains(ARRAY_FALSE, true));
66    assertTrue(Booleans.contains(ARRAY_FALSE, false));
67    assertTrue(Booleans.contains(ARRAY_FALSE_TRUE, false));
68    assertTrue(Booleans.contains(ARRAY_FALSE_TRUE, true));
69  }
70
71  public void testIndexOf() {
72    assertEquals(-1, Booleans.indexOf(EMPTY, false));
73    assertEquals(-1, Booleans.indexOf(ARRAY_FALSE, true));
74    assertEquals(-1, Booleans.indexOf(ARRAY_FALSE_FALSE, true));
75    assertEquals(0, Booleans.indexOf(ARRAY_FALSE, false));
76    assertEquals(0, Booleans.indexOf(ARRAY_FALSE_TRUE, false));
77    assertEquals(1, Booleans.indexOf(ARRAY_FALSE_TRUE, true));
78    assertEquals(2, Booleans.indexOf(new boolean[] {false, false, true}, true));
79  }
80
81  public void testLastIndexOf() {
82    assertEquals(-1, Booleans.lastIndexOf(EMPTY, false));
83    assertEquals(-1, Booleans.lastIndexOf(ARRAY_FALSE, true));
84    assertEquals(-1, Booleans.lastIndexOf(ARRAY_FALSE_FALSE, true));
85    assertEquals(0, Booleans.lastIndexOf(ARRAY_FALSE, false));
86    assertEquals(0, Booleans.lastIndexOf(ARRAY_FALSE_TRUE, false));
87    assertEquals(1, Booleans.lastIndexOf(ARRAY_FALSE_TRUE, true));
88    assertEquals(2, Booleans.lastIndexOf(new boolean[] {false, true, true}, true));
89  }
90
91  public void testConcat() {
92    assertTrue(Arrays.equals(EMPTY, Booleans.concat()));
93    assertTrue(Arrays.equals(EMPTY, Booleans.concat(EMPTY)));
94    assertTrue(Arrays.equals(EMPTY, Booleans.concat(EMPTY, EMPTY, EMPTY)));
95    assertTrue(Arrays.equals(ARRAY_FALSE, Booleans.concat(ARRAY_FALSE)));
96    assertNotSame(ARRAY_FALSE, Booleans.concat(ARRAY_FALSE));
97    assertTrue(Arrays.equals(ARRAY_FALSE, Booleans.concat(EMPTY, ARRAY_FALSE, EMPTY)));
98    assertTrue(Arrays.equals(
99        new boolean[] {false, false, false},
100        Booleans.concat(ARRAY_FALSE, ARRAY_FALSE, ARRAY_FALSE)));
101    assertTrue(Arrays.equals(
102        new boolean[] {false, false, true},
103        Booleans.concat(ARRAY_FALSE, ARRAY_FALSE_TRUE)));
104  }
105
106  public void testEnsureCapacity() {
107    assertSame(EMPTY, Booleans.ensureCapacity(EMPTY, 0, 1));
108    assertSame(ARRAY_FALSE, Booleans.ensureCapacity(ARRAY_FALSE, 0, 1));
109    assertSame(ARRAY_FALSE, Booleans.ensureCapacity(ARRAY_FALSE, 1, 1));
110    assertTrue(Arrays.equals(
111        new boolean[] {true, false, false},
112        Booleans.ensureCapacity(new boolean[] {true}, 2, 1)));
113  }
114
115  public void testEnsureCapacity_fail() {
116    try {
117      Booleans.ensureCapacity(ARRAY_FALSE, -1, 1);
118      fail();
119    } catch (IllegalArgumentException expected) {
120    }
121    try {
122      // notice that this should even fail when no growth was needed
123      Booleans.ensureCapacity(ARRAY_FALSE, 1, -1);
124      fail();
125    } catch (IllegalArgumentException expected) {
126    }
127  }
128
129  public void testJoin() {
130    assertEquals("", Booleans.join(",", EMPTY));
131    assertEquals("false", Booleans.join(",", ARRAY_FALSE));
132    assertEquals("false,true", Booleans.join(",", false, true));
133    assertEquals("falsetruefalse",
134        Booleans.join("", false, true, false));
135  }
136
137  public void testLexicographicalComparator() {
138    List<boolean[]> ordered = Arrays.asList(
139        new boolean[] {},
140        new boolean[] {false},
141        new boolean[] {false, false},
142        new boolean[] {false, true},
143        new boolean[] {true},
144        new boolean[] {true, false},
145        new boolean[] {true, true},
146        new boolean[] {true, true, true});
147
148    Comparator<boolean[]> comparator = Booleans.lexicographicalComparator();
149    Helpers.testComparator(comparator, ordered);
150  }
151
152  @GwtIncompatible("SerializableTester")
153  public void testLexicographicalComparatorSerializable() {
154    Comparator<boolean[]> comparator = Booleans.lexicographicalComparator();
155    assertSame(comparator, SerializableTester.reserialize(comparator));
156  }
157
158  public void testToArray() {
159    // need explicit type parameter to avoid javac warning!?
160    List<Boolean> none = Arrays.<Boolean>asList();
161    assertTrue(Arrays.equals(EMPTY, Booleans.toArray(none)));
162
163    List<Boolean> one = Arrays.asList(false);
164    assertTrue(Arrays.equals(ARRAY_FALSE, Booleans.toArray(one)));
165
166    boolean[] array = {false, false, true};
167
168    List<Boolean> three = Arrays.asList(false, false, true);
169    assertTrue(Arrays.equals(array, Booleans.toArray(three)));
170
171    assertTrue(Arrays.equals(array, Booleans.toArray(Booleans.asList(array))));
172  }
173
174  public void testToArray_threadSafe() {
175    // Only for booleans, we lengthen VALUES
176    boolean[] VALUES = BooleansTest.VALUES;
177    VALUES = Booleans.concat(VALUES, VALUES);
178
179    for (int delta : new int[] { +1, 0, -1 }) {
180      for (int i = 0; i < VALUES.length; i++) {
181        List<Boolean> list = Booleans.asList(VALUES).subList(0, i);
182        Collection<Boolean> misleadingSize =
183            Helpers.misleadingSizeCollection(delta);
184        misleadingSize.addAll(list);
185        boolean[] arr = Booleans.toArray(misleadingSize);
186        assertEquals(i, arr.length);
187        for (int j = 0; j < i; j++) {
188          assertEquals(VALUES[j], arr[j]);
189        }
190      }
191    }
192  }
193
194  public void testToArray_withNull() {
195    List<Boolean> list = Arrays.asList(false, true, null);
196    try {
197      Booleans.toArray(list);
198      fail();
199    } catch (NullPointerException expected) {
200    }
201  }
202
203  public void testAsListEmpty() {
204    assertSame(Collections.emptyList(), Booleans.asList(EMPTY));
205  }
206
207  @GwtIncompatible("NullPointerTester")
208  public void testNulls() throws Exception {
209    NullPointerTester tester = new NullPointerTester();
210    tester.setDefault(boolean[].class, new boolean[0]);
211    tester.testAllPublicStaticMethods(Booleans.class);
212  }
213}
214