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 17 package com.google.common.primitives; 18 19 import com.google.common.annotations.GwtCompatible; 20 import com.google.common.annotations.GwtIncompatible; 21 import com.google.common.collect.testing.Helpers; 22 import com.google.common.testing.NullPointerTester; 23 import com.google.common.testing.SerializableTester; 24 import java.util.Arrays; 25 import java.util.Collection; 26 import java.util.Collections; 27 import java.util.Comparator; 28 import java.util.List; 29 import junit.framework.TestCase; 30 31 /** 32 * Unit test for {@link Booleans}. 33 * 34 * @author Kevin Bourrillion 35 */ 36 @GwtCompatible(emulated = true) 37 public class BooleansTest extends TestCase { 38 private static final boolean[] EMPTY = {}; 39 private static final boolean[] ARRAY_FALSE = {false}; 40 private static final boolean[] ARRAY_TRUE = {true}; 41 private static final boolean[] ARRAY_FALSE_FALSE = {false, false}; 42 private static final boolean[] ARRAY_FALSE_TRUE = {false, true}; 43 44 private static final boolean[] VALUES = {false, true}; 45 testHashCode()46 public void testHashCode() { 47 assertEquals(Boolean.TRUE.hashCode(), Booleans.hashCode(true)); 48 assertEquals(Boolean.FALSE.hashCode(), Booleans.hashCode(false)); 49 } 50 testTrueFirst()51 public void testTrueFirst() { 52 assertEquals(0, Booleans.trueFirst().compare(true, true)); 53 assertEquals(0, Booleans.trueFirst().compare(false, false)); 54 assertTrue(Booleans.trueFirst().compare(true, false) < 0); 55 assertTrue(Booleans.trueFirst().compare(false, true) > 0); 56 } 57 testFalseFirst()58 public void testFalseFirst() { 59 assertEquals(0, Booleans.falseFirst().compare(true, true)); 60 assertEquals(0, Booleans.falseFirst().compare(false, false)); 61 assertTrue(Booleans.falseFirst().compare(false, true) < 0); 62 assertTrue(Booleans.falseFirst().compare(true, false) > 0); 63 } 64 testCompare()65 public void testCompare() { 66 for (boolean x : VALUES) { 67 for (boolean y : VALUES) { 68 // note: spec requires only that the sign is the same 69 assertEquals(x + ", " + y, Boolean.valueOf(x).compareTo(y), Booleans.compare(x, y)); 70 } 71 } 72 } 73 testContains()74 public void testContains() { 75 assertFalse(Booleans.contains(EMPTY, false)); 76 assertFalse(Booleans.contains(ARRAY_FALSE, true)); 77 assertTrue(Booleans.contains(ARRAY_FALSE, false)); 78 assertTrue(Booleans.contains(ARRAY_FALSE_TRUE, false)); 79 assertTrue(Booleans.contains(ARRAY_FALSE_TRUE, true)); 80 } 81 testIndexOf()82 public void testIndexOf() { 83 assertEquals(-1, Booleans.indexOf(EMPTY, ARRAY_FALSE)); 84 assertEquals(-1, Booleans.indexOf(ARRAY_FALSE, ARRAY_FALSE_TRUE)); 85 assertEquals(0, Booleans.indexOf(ARRAY_FALSE_FALSE, ARRAY_FALSE)); 86 assertEquals(0, Booleans.indexOf(ARRAY_FALSE, ARRAY_FALSE)); 87 assertEquals(0, Booleans.indexOf(ARRAY_FALSE_TRUE, ARRAY_FALSE)); 88 assertEquals(1, Booleans.indexOf(ARRAY_FALSE_TRUE, ARRAY_TRUE)); 89 assertEquals(0, Booleans.indexOf(ARRAY_TRUE, new boolean[0])); 90 } 91 testIndexOf_arrays()92 public void testIndexOf_arrays() { 93 assertEquals(-1, Booleans.indexOf(EMPTY, false)); 94 assertEquals(-1, Booleans.indexOf(ARRAY_FALSE, true)); 95 assertEquals(-1, Booleans.indexOf(ARRAY_FALSE_FALSE, true)); 96 assertEquals(0, Booleans.indexOf(ARRAY_FALSE, false)); 97 assertEquals(0, Booleans.indexOf(ARRAY_FALSE_TRUE, false)); 98 assertEquals(1, Booleans.indexOf(ARRAY_FALSE_TRUE, true)); 99 assertEquals(2, Booleans.indexOf(new boolean[] {false, false, true}, true)); 100 } 101 testLastIndexOf()102 public void testLastIndexOf() { 103 assertEquals(-1, Booleans.lastIndexOf(EMPTY, false)); 104 assertEquals(-1, Booleans.lastIndexOf(ARRAY_FALSE, true)); 105 assertEquals(-1, Booleans.lastIndexOf(ARRAY_FALSE_FALSE, true)); 106 assertEquals(0, Booleans.lastIndexOf(ARRAY_FALSE, false)); 107 assertEquals(0, Booleans.lastIndexOf(ARRAY_FALSE_TRUE, false)); 108 assertEquals(1, Booleans.lastIndexOf(ARRAY_FALSE_TRUE, true)); 109 assertEquals(2, Booleans.lastIndexOf(new boolean[] {false, true, true}, true)); 110 } 111 testConcat()112 public void testConcat() { 113 assertTrue(Arrays.equals(EMPTY, Booleans.concat())); 114 assertTrue(Arrays.equals(EMPTY, Booleans.concat(EMPTY))); 115 assertTrue(Arrays.equals(EMPTY, Booleans.concat(EMPTY, EMPTY, EMPTY))); 116 assertTrue(Arrays.equals(ARRAY_FALSE, Booleans.concat(ARRAY_FALSE))); 117 assertNotSame(ARRAY_FALSE, Booleans.concat(ARRAY_FALSE)); 118 assertTrue(Arrays.equals(ARRAY_FALSE, Booleans.concat(EMPTY, ARRAY_FALSE, EMPTY))); 119 assertTrue( 120 Arrays.equals( 121 new boolean[] {false, false, false}, 122 Booleans.concat(ARRAY_FALSE, ARRAY_FALSE, ARRAY_FALSE))); 123 assertTrue( 124 Arrays.equals( 125 new boolean[] {false, false, true}, Booleans.concat(ARRAY_FALSE, ARRAY_FALSE_TRUE))); 126 } 127 testEnsureCapacity()128 public void testEnsureCapacity() { 129 assertSame(EMPTY, Booleans.ensureCapacity(EMPTY, 0, 1)); 130 assertSame(ARRAY_FALSE, Booleans.ensureCapacity(ARRAY_FALSE, 0, 1)); 131 assertSame(ARRAY_FALSE, Booleans.ensureCapacity(ARRAY_FALSE, 1, 1)); 132 assertTrue( 133 Arrays.equals( 134 new boolean[] {true, false, false}, 135 Booleans.ensureCapacity(new boolean[] {true}, 2, 1))); 136 } 137 testEnsureCapacity_fail()138 public void testEnsureCapacity_fail() { 139 try { 140 Booleans.ensureCapacity(ARRAY_FALSE, -1, 1); 141 fail(); 142 } catch (IllegalArgumentException expected) { 143 } 144 try { 145 // notice that this should even fail when no growth was needed 146 Booleans.ensureCapacity(ARRAY_FALSE, 1, -1); 147 fail(); 148 } catch (IllegalArgumentException expected) { 149 } 150 } 151 testJoin()152 public void testJoin() { 153 assertEquals("", Booleans.join(",", EMPTY)); 154 assertEquals("false", Booleans.join(",", ARRAY_FALSE)); 155 assertEquals("false,true", Booleans.join(",", false, true)); 156 assertEquals("falsetruefalse", Booleans.join("", false, true, false)); 157 } 158 testLexicographicalComparator()159 public void testLexicographicalComparator() { 160 List<boolean[]> ordered = 161 Arrays.asList( 162 new boolean[] {}, 163 new boolean[] {false}, 164 new boolean[] {false, false}, 165 new boolean[] {false, true}, 166 new boolean[] {true}, 167 new boolean[] {true, false}, 168 new boolean[] {true, true}, 169 new boolean[] {true, true, true}); 170 171 Comparator<boolean[]> comparator = Booleans.lexicographicalComparator(); 172 Helpers.testComparator(comparator, ordered); 173 } 174 175 @GwtIncompatible // SerializableTester testLexicographicalComparatorSerializable()176 public void testLexicographicalComparatorSerializable() { 177 Comparator<boolean[]> comparator = Booleans.lexicographicalComparator(); 178 assertSame(comparator, SerializableTester.reserialize(comparator)); 179 } 180 testReverse()181 public void testReverse() { 182 testReverse(new boolean[] {}, new boolean[] {}); 183 testReverse(new boolean[] {true}, new boolean[] {true}); 184 testReverse(new boolean[] {false, true}, new boolean[] {true, false}); 185 testReverse(new boolean[] {true, false, false}, new boolean[] {false, false, true}); 186 testReverse(new boolean[] {true, true, false, false}, new boolean[] {false, false, true, true}); 187 } 188 testReverse(boolean[] input, boolean[] expectedOutput)189 private static void testReverse(boolean[] input, boolean[] expectedOutput) { 190 input = Arrays.copyOf(input, input.length); 191 Booleans.reverse(input); 192 assertTrue(Arrays.equals(expectedOutput, input)); 193 } 194 testReverse( boolean[] input, int fromIndex, int toIndex, boolean[] expectedOutput)195 private static void testReverse( 196 boolean[] input, int fromIndex, int toIndex, boolean[] expectedOutput) { 197 input = Arrays.copyOf(input, input.length); 198 Booleans.reverse(input, fromIndex, toIndex); 199 assertTrue(Arrays.equals(expectedOutput, input)); 200 } 201 testReverseIndexed()202 public void testReverseIndexed() { 203 testReverse(new boolean[] {}, 0, 0, new boolean[] {}); 204 testReverse(new boolean[] {true}, 0, 1, new boolean[] {true}); 205 testReverse(new boolean[] {false, true}, 0, 2, new boolean[] {true, false}); 206 testReverse(new boolean[] {true, false, false}, 0, 2, new boolean[] {false, true, false}); 207 testReverse(new boolean[] {true, false, false}, 0, 1, new boolean[] {true, false, false}); 208 testReverse( 209 new boolean[] {true, true, false, false}, 1, 3, new boolean[] {true, false, true, false}); 210 } 211 testToArray()212 public void testToArray() { 213 // need explicit type parameter to avoid javac warning!? 214 List<Boolean> none = Arrays.<Boolean>asList(); 215 assertTrue(Arrays.equals(EMPTY, Booleans.toArray(none))); 216 217 List<Boolean> one = Arrays.asList(false); 218 assertTrue(Arrays.equals(ARRAY_FALSE, Booleans.toArray(one))); 219 220 boolean[] array = {false, false, true}; 221 222 List<Boolean> three = Arrays.asList(false, false, true); 223 assertTrue(Arrays.equals(array, Booleans.toArray(three))); 224 225 assertTrue(Arrays.equals(array, Booleans.toArray(Booleans.asList(array)))); 226 } 227 testToArray_threadSafe()228 public void testToArray_threadSafe() { 229 // Only for booleans, we lengthen VALUES 230 boolean[] VALUES = BooleansTest.VALUES; 231 VALUES = Booleans.concat(VALUES, VALUES); 232 233 for (int delta : new int[] {+1, 0, -1}) { 234 for (int i = 0; i < VALUES.length; i++) { 235 List<Boolean> list = Booleans.asList(VALUES).subList(0, i); 236 Collection<Boolean> misleadingSize = Helpers.misleadingSizeCollection(delta); 237 misleadingSize.addAll(list); 238 boolean[] arr = Booleans.toArray(misleadingSize); 239 assertEquals(i, arr.length); 240 for (int j = 0; j < i; j++) { 241 assertEquals(VALUES[j], arr[j]); 242 } 243 } 244 } 245 } 246 testToArray_withNull()247 public void testToArray_withNull() { 248 List<Boolean> list = Arrays.asList(false, true, null); 249 try { 250 Booleans.toArray(list); 251 fail(); 252 } catch (NullPointerException expected) { 253 } 254 } 255 testAsListIsEmpty()256 public void testAsListIsEmpty() { 257 assertTrue(Booleans.asList(EMPTY).isEmpty()); 258 assertFalse(Booleans.asList(ARRAY_FALSE).isEmpty()); 259 } 260 testAsListSize()261 public void testAsListSize() { 262 assertEquals(0, Booleans.asList(EMPTY).size()); 263 assertEquals(1, Booleans.asList(ARRAY_FALSE).size()); 264 assertEquals(2, Booleans.asList(ARRAY_FALSE_TRUE).size()); 265 } 266 testAsListIndexOf()267 public void testAsListIndexOf() { 268 assertEquals(-1, Booleans.asList(EMPTY).indexOf((Object) "wrong type")); 269 assertEquals(-1, Booleans.asList(EMPTY).indexOf(true)); 270 assertEquals(-1, Booleans.asList(ARRAY_FALSE).indexOf(true)); 271 assertEquals(0, Booleans.asList(ARRAY_FALSE).indexOf(false)); 272 assertEquals(1, Booleans.asList(ARRAY_FALSE_TRUE).indexOf(true)); 273 } 274 testAsListLastIndexOf()275 public void testAsListLastIndexOf() { 276 assertEquals(-1, Booleans.asList(EMPTY).lastIndexOf((Object) "wrong type")); 277 assertEquals(-1, Booleans.asList(EMPTY).lastIndexOf(true)); 278 assertEquals(-1, Booleans.asList(ARRAY_FALSE).lastIndexOf(true)); 279 assertEquals(1, Booleans.asList(ARRAY_FALSE_TRUE).lastIndexOf(true)); 280 assertEquals(1, Booleans.asList(ARRAY_FALSE_FALSE).lastIndexOf(false)); 281 } 282 testAsListContains()283 public void testAsListContains() { 284 assertFalse(Booleans.asList(EMPTY).contains((Object) "wrong type")); 285 assertFalse(Booleans.asList(EMPTY).contains(true)); 286 assertFalse(Booleans.asList(ARRAY_FALSE).contains(true)); 287 assertTrue(Booleans.asList(ARRAY_TRUE).contains(true)); 288 assertTrue(Booleans.asList(ARRAY_FALSE_TRUE).contains(false)); 289 assertTrue(Booleans.asList(ARRAY_FALSE_TRUE).contains(true)); 290 } 291 testAsListEquals()292 public void testAsListEquals() { 293 assertEquals(Booleans.asList(EMPTY), Collections.emptyList()); 294 assertEquals(Booleans.asList(ARRAY_FALSE), Booleans.asList(ARRAY_FALSE)); 295 assertFalse(Booleans.asList(ARRAY_FALSE).equals(ARRAY_FALSE)); 296 assertFalse(Booleans.asList(ARRAY_FALSE).equals(null)); 297 assertFalse(Booleans.asList(ARRAY_FALSE).equals(Booleans.asList(ARRAY_FALSE_TRUE))); 298 assertFalse(Booleans.asList(ARRAY_FALSE_FALSE).equals(Booleans.asList(ARRAY_FALSE_TRUE))); 299 assertEquals(1, Booleans.asList(ARRAY_FALSE_TRUE).lastIndexOf(true)); 300 List<Boolean> reference = Booleans.asList(ARRAY_FALSE); 301 assertEquals(Booleans.asList(ARRAY_FALSE), reference); 302 assertEquals(reference, reference); 303 } 304 testAsListHashcode()305 public void testAsListHashcode() { 306 assertEquals(1, Booleans.asList(EMPTY).hashCode()); 307 assertEquals(Booleans.asList(ARRAY_FALSE).hashCode(), Booleans.asList(ARRAY_FALSE).hashCode()); 308 List<Boolean> reference = Booleans.asList(ARRAY_FALSE); 309 assertEquals(Booleans.asList(ARRAY_FALSE).hashCode(), reference.hashCode()); 310 } 311 testAsListToString()312 public void testAsListToString() { 313 assertEquals("[false]", Booleans.asList(ARRAY_FALSE).toString()); 314 assertEquals("[false, true]", Booleans.asList(ARRAY_FALSE_TRUE).toString()); 315 } 316 testAsListSet()317 public void testAsListSet() { 318 List<Boolean> list = Booleans.asList(ARRAY_FALSE); 319 assertFalse(list.set(0, true)); 320 assertTrue(list.set(0, false)); 321 try { 322 list.set(0, null); 323 fail(); 324 } catch (NullPointerException expected) { 325 } 326 try { 327 list.set(1, true); 328 fail(); 329 } catch (IndexOutOfBoundsException expected) { 330 } 331 } 332 testCountTrue()333 public void testCountTrue() { 334 assertEquals(0, Booleans.countTrue()); 335 assertEquals(0, Booleans.countTrue(false)); 336 assertEquals(1, Booleans.countTrue(true)); 337 assertEquals(3, Booleans.countTrue(false, true, false, true, false, true)); 338 assertEquals(1, Booleans.countTrue(false, false, true, false, false)); 339 } 340 341 @GwtIncompatible // NullPointerTester testNulls()342 public void testNulls() { 343 new NullPointerTester().testAllPublicStaticMethods(Booleans.class); 344 } 345 } 346