|
Java example source code file (BooleansTest.java)
The BooleansTest.java Java example source code
/*
* Copyright (C) 2008 The Guava Authors
*
* 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.common.primitives;
import com.google.common.annotations.GwtCompatible;
import com.google.common.annotations.GwtIncompatible;
import com.google.common.collect.testing.Helpers;
import com.google.common.testing.NullPointerTester;
import com.google.common.testing.SerializableTester;
import junit.framework.TestCase;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
/**
* Unit test for {@link Booleans}.
*
* @author Kevin Bourrillion
*/
@GwtCompatible(emulated = true)
public class BooleansTest extends TestCase {
private static final boolean[] EMPTY = {};
private static final boolean[] ARRAY_FALSE = {false};
private static final boolean[] ARRAY_TRUE = {true};
private static final boolean[] ARRAY_FALSE_FALSE = {false, false};
private static final boolean[] ARRAY_FALSE_TRUE = {false, true};
private static final boolean[] VALUES = {false, true};
public void testHashCode() {
assertEquals(Boolean.TRUE.hashCode(), Booleans.hashCode(true));
assertEquals(Boolean.FALSE.hashCode(), Booleans.hashCode(false));
}
public void testCompare() {
for (boolean x : VALUES) {
for (boolean y : VALUES) {
// note: spec requires only that the sign is the same
assertEquals(x + ", " + y,
Boolean.valueOf(x).compareTo(y),
Booleans.compare(x, y));
}
}
}
public void testContains() {
assertFalse(Booleans.contains(EMPTY, false));
assertFalse(Booleans.contains(ARRAY_FALSE, true));
assertTrue(Booleans.contains(ARRAY_FALSE, false));
assertTrue(Booleans.contains(ARRAY_FALSE_TRUE, false));
assertTrue(Booleans.contains(ARRAY_FALSE_TRUE, true));
}
public void testIndexOf() {
assertEquals(-1, Booleans.indexOf(EMPTY, ARRAY_FALSE));
assertEquals(-1, Booleans.indexOf(ARRAY_FALSE, ARRAY_FALSE_TRUE));
assertEquals(0, Booleans.indexOf(ARRAY_FALSE_FALSE, ARRAY_FALSE));
assertEquals(0, Booleans.indexOf(ARRAY_FALSE, ARRAY_FALSE));
assertEquals(0, Booleans.indexOf(ARRAY_FALSE_TRUE, ARRAY_FALSE));
assertEquals(1, Booleans.indexOf(ARRAY_FALSE_TRUE, ARRAY_TRUE));
assertEquals(0, Booleans.indexOf(ARRAY_TRUE, new boolean[0]));
}
public void testIndexOf_arrays() {
assertEquals(-1, Booleans.indexOf(EMPTY, false));
assertEquals(-1, Booleans.indexOf(ARRAY_FALSE, true));
assertEquals(-1, Booleans.indexOf(ARRAY_FALSE_FALSE, true));
assertEquals(0, Booleans.indexOf(ARRAY_FALSE, false));
assertEquals(0, Booleans.indexOf(ARRAY_FALSE_TRUE, false));
assertEquals(1, Booleans.indexOf(ARRAY_FALSE_TRUE, true));
assertEquals(2, Booleans.indexOf(new boolean[] {false, false, true}, true));
}
public void testLastIndexOf() {
assertEquals(-1, Booleans.lastIndexOf(EMPTY, false));
assertEquals(-1, Booleans.lastIndexOf(ARRAY_FALSE, true));
assertEquals(-1, Booleans.lastIndexOf(ARRAY_FALSE_FALSE, true));
assertEquals(0, Booleans.lastIndexOf(ARRAY_FALSE, false));
assertEquals(0, Booleans.lastIndexOf(ARRAY_FALSE_TRUE, false));
assertEquals(1, Booleans.lastIndexOf(ARRAY_FALSE_TRUE, true));
assertEquals(2, Booleans.lastIndexOf(new boolean[] {false, true, true}, true));
}
public void testConcat() {
assertTrue(Arrays.equals(EMPTY, Booleans.concat()));
assertTrue(Arrays.equals(EMPTY, Booleans.concat(EMPTY)));
assertTrue(Arrays.equals(EMPTY, Booleans.concat(EMPTY, EMPTY, EMPTY)));
assertTrue(Arrays.equals(ARRAY_FALSE, Booleans.concat(ARRAY_FALSE)));
assertNotSame(ARRAY_FALSE, Booleans.concat(ARRAY_FALSE));
assertTrue(Arrays.equals(ARRAY_FALSE, Booleans.concat(EMPTY, ARRAY_FALSE, EMPTY)));
assertTrue(Arrays.equals(
new boolean[] {false, false, false},
Booleans.concat(ARRAY_FALSE, ARRAY_FALSE, ARRAY_FALSE)));
assertTrue(Arrays.equals(
new boolean[] {false, false, true},
Booleans.concat(ARRAY_FALSE, ARRAY_FALSE_TRUE)));
}
public void testEnsureCapacity() {
assertSame(EMPTY, Booleans.ensureCapacity(EMPTY, 0, 1));
assertSame(ARRAY_FALSE, Booleans.ensureCapacity(ARRAY_FALSE, 0, 1));
assertSame(ARRAY_FALSE, Booleans.ensureCapacity(ARRAY_FALSE, 1, 1));
assertTrue(Arrays.equals(
new boolean[] {true, false, false},
Booleans.ensureCapacity(new boolean[] {true}, 2, 1)));
}
public void testEnsureCapacity_fail() {
try {
Booleans.ensureCapacity(ARRAY_FALSE, -1, 1);
fail();
} catch (IllegalArgumentException expected) {
}
try {
// notice that this should even fail when no growth was needed
Booleans.ensureCapacity(ARRAY_FALSE, 1, -1);
fail();
} catch (IllegalArgumentException expected) {
}
}
public void testJoin() {
assertEquals("", Booleans.join(",", EMPTY));
assertEquals("false", Booleans.join(",", ARRAY_FALSE));
assertEquals("false,true", Booleans.join(",", false, true));
assertEquals("falsetruefalse",
Booleans.join("", false, true, false));
}
public void testLexicographicalComparator() {
List<boolean[]> ordered = Arrays.asList(
new boolean[] {},
new boolean[] {false},
new boolean[] {false, false},
new boolean[] {false, true},
new boolean[] {true},
new boolean[] {true, false},
new boolean[] {true, true},
new boolean[] {true, true, true});
Comparator<boolean[]> comparator = Booleans.lexicographicalComparator();
Helpers.testComparator(comparator, ordered);
}
@GwtIncompatible // SerializableTester
public void testLexicographicalComparatorSerializable() {
Comparator<boolean[]> comparator = Booleans.lexicographicalComparator();
assertSame(comparator, SerializableTester.reserialize(comparator));
}
public void testToArray() {
// need explicit type parameter to avoid javac warning!?
List<Boolean> none = Arrays.
Other Java examples (source code examples)Here is a short list of links related to this Java BooleansTest.java source code file: |
| ... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.