|
Java example source code file (FloatsTest.java)
The FloatsTest.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 static com.google.common.truth.Truth.assertThat; import static java.lang.Float.NaN; import com.google.common.annotations.GwtCompatible; import com.google.common.annotations.GwtIncompatible; import com.google.common.base.Converter; import com.google.common.collect.ImmutableList; 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 Floats}. * * @author Kevin Bourrillion */ @GwtCompatible(emulated = true) @SuppressWarnings("cast") // redundant casts are intentional and harmless public class FloatsTest extends TestCase { private static final float[] EMPTY = {}; private static final float[] ARRAY1 = {(float) 1}; private static final float[] ARRAY234 = {(float) 2, (float) 3, (float) 4}; private static final float LEAST = Float.NEGATIVE_INFINITY; private static final float GREATEST = Float.POSITIVE_INFINITY; private static final float[] NUMBERS = new float[] { LEAST, -Float.MAX_VALUE, -1f, -0f, 0f, 1f, Float.MAX_VALUE, GREATEST, Float.MIN_NORMAL, -Float.MIN_NORMAL, Float.MIN_VALUE, -Float.MIN_VALUE, Integer.MIN_VALUE, Integer.MAX_VALUE, Long.MIN_VALUE, Long.MAX_VALUE }; private static final float[] VALUES = Floats.concat(NUMBERS, new float[] {NaN}); public void testHashCode() { for (float value : VALUES) { assertEquals(((Float) value).hashCode(), Floats.hashCode(value)); } } public void testIsFinite() { for (float value : NUMBERS) { assertEquals(!(Float.isInfinite(value) || Float.isNaN(value)), Floats.isFinite(value)); } } public void testCompare() { for (float x : VALUES) { for (float y : VALUES) { // note: spec requires only that the sign is the same assertEquals(x + ", " + y, Float.valueOf(x).compareTo(y), Floats.compare(x, y)); } } } public void testContains() { assertFalse(Floats.contains(EMPTY, (float) 1)); assertFalse(Floats.contains(ARRAY1, (float) 2)); assertFalse(Floats.contains(ARRAY234, (float) 1)); assertTrue(Floats.contains(new float[] {(float) -1}, (float) -1)); assertTrue(Floats.contains(ARRAY234, (float) 2)); assertTrue(Floats.contains(ARRAY234, (float) 3)); assertTrue(Floats.contains(ARRAY234, (float) 4)); for (float value : NUMBERS) { assertTrue("" + value, Floats.contains(new float[] {5f, value}, value)); } assertFalse(Floats.contains(new float[] {5f, NaN}, NaN)); } public void testIndexOf() { assertEquals(-1, Floats.indexOf(EMPTY, (float) 1)); assertEquals(-1, Floats.indexOf(ARRAY1, (float) 2)); assertEquals(-1, Floats.indexOf(ARRAY234, (float) 1)); assertEquals(0, Floats.indexOf( new float[] {(float) -1}, (float) -1)); assertEquals(0, Floats.indexOf(ARRAY234, (float) 2)); assertEquals(1, Floats.indexOf(ARRAY234, (float) 3)); assertEquals(2, Floats.indexOf(ARRAY234, (float) 4)); assertEquals(1, Floats.indexOf( new float[] { (float) 2, (float) 3, (float) 2, (float) 3 }, (float) 3)); for (float value : NUMBERS) { assertEquals("" + value, 1, Floats.indexOf(new float[] {5f, value}, value)); } assertEquals(-1, Floats.indexOf(new float[] {5f, NaN}, NaN)); } public void testIndexOf_arrayTarget() { assertEquals(0, Floats.indexOf(EMPTY, EMPTY)); assertEquals(0, Floats.indexOf(ARRAY234, EMPTY)); assertEquals(-1, Floats.indexOf(EMPTY, ARRAY234)); assertEquals(-1, Floats.indexOf(ARRAY234, ARRAY1)); assertEquals(-1, Floats.indexOf(ARRAY1, ARRAY234)); assertEquals(0, Floats.indexOf(ARRAY1, ARRAY1)); assertEquals(0, Floats.indexOf(ARRAY234, ARRAY234)); assertEquals(0, Floats.indexOf( ARRAY234, new float[] { (float) 2, (float) 3 })); assertEquals(1, Floats.indexOf( ARRAY234, new float[] { (float) 3, (float) 4 })); assertEquals(1, Floats.indexOf(ARRAY234, new float[] { (float) 3 })); assertEquals(2, Floats.indexOf(ARRAY234, new float[] { (float) 4 })); assertEquals(1, Floats.indexOf(new float[] { (float) 2, (float) 3, (float) 3, (float) 3, (float) 3 }, new float[] { (float) 3 } )); assertEquals(2, Floats.indexOf( new float[] { (float) 2, (float) 3, (float) 2, (float) 3, (float) 4, (float) 2, (float) 3}, new float[] { (float) 2, (float) 3, (float) 4} )); assertEquals(1, Floats.indexOf( new float[] { (float) 2, (float) 2, (float) 3, (float) 4, (float) 2, (float) 3, (float) 4}, new float[] { (float) 2, (float) 3, (float) 4} )); assertEquals(-1, Floats.indexOf( new float[] { (float) 4, (float) 3, (float) 2}, new float[] { (float) 2, (float) 3, (float) 4} )); for (float value : NUMBERS) { assertEquals("" + value, 1, Floats.indexOf( new float[] {5f, value, value, 5f}, new float[] {value, value})); } assertEquals(-1, Floats.indexOf( new float[] {5f, NaN, NaN, 5f}, new float[] {NaN, NaN})); } public void testLastIndexOf() { assertEquals(-1, Floats.lastIndexOf(EMPTY, (float) 1)); assertEquals(-1, Floats.lastIndexOf(ARRAY1, (float) 2)); assertEquals(-1, Floats.lastIndexOf(ARRAY234, (float) 1)); assertEquals(0, Floats.lastIndexOf( new float[] {(float) -1}, (float) -1)); assertEquals(0, Floats.lastIndexOf(ARRAY234, (float) 2)); assertEquals(1, Floats.lastIndexOf(ARRAY234, (float) 3)); assertEquals(2, Floats.lastIndexOf(ARRAY234, (float) 4)); assertEquals(3, Floats.lastIndexOf( new float[] { (float) 2, (float) 3, (float) 2, (float) 3 }, (float) 3)); for (float value : NUMBERS) { assertEquals("" + value, 0, Floats.lastIndexOf(new float[] {value, 5f}, value)); } assertEquals(-1, Floats.lastIndexOf(new float[] {NaN, 5f}, NaN)); } public void testMax_noArgs() { try { Floats.max(); fail(); } catch (IllegalArgumentException expected) { } } public void testMax() { assertEquals(GREATEST, Floats.max(GREATEST)); assertEquals(LEAST, Floats.max(LEAST)); assertEquals((float) 9, Floats.max( (float) 8, (float) 6, (float) 7, (float) 5, (float) 3, (float) 0, (float) 9)); assertEquals(0f, Floats.max(-0f, 0f)); assertEquals(0f, Floats.max(0f, -0f)); assertEquals(GREATEST, Floats.max(NUMBERS)); assertTrue(Float.isNaN(Floats.max(VALUES))); } public void testMin_noArgs() { try { Floats.min(); fail(); } catch (IllegalArgumentException expected) { } } public void testMin() { assertEquals(LEAST, Floats.min(LEAST)); assertEquals(GREATEST, Floats.min(GREATEST)); assertEquals((float) 0, Floats.min( (float) 8, (float) 6, (float) 7, (float) 5, (float) 3, (float) 0, (float) 9)); assertEquals(-0f, Floats.min(-0f, 0f)); assertEquals(-0f, Floats.min(0f, -0f)); assertEquals(LEAST, Floats.min(NUMBERS)); assertTrue(Float.isNaN(Floats.min(VALUES))); } public void testConcat() { assertTrue(Arrays.equals(EMPTY, Floats.concat())); assertTrue(Arrays.equals(EMPTY, Floats.concat(EMPTY))); assertTrue(Arrays.equals(EMPTY, Floats.concat(EMPTY, EMPTY, EMPTY))); assertTrue(Arrays.equals(ARRAY1, Floats.concat(ARRAY1))); assertNotSame(ARRAY1, Floats.concat(ARRAY1)); assertTrue(Arrays.equals(ARRAY1, Floats.concat(EMPTY, ARRAY1, EMPTY))); assertTrue(Arrays.equals( new float[] {(float) 1, (float) 1, (float) 1}, Floats.concat(ARRAY1, ARRAY1, ARRAY1))); assertTrue(Arrays.equals( new float[] {(float) 1, (float) 2, (float) 3, (float) 4}, Floats.concat(ARRAY1, ARRAY234))); } public void testEnsureCapacity() { assertSame(EMPTY, Floats.ensureCapacity(EMPTY, 0, 1)); assertSame(ARRAY1, Floats.ensureCapacity(ARRAY1, 0, 1)); assertSame(ARRAY1, Floats.ensureCapacity(ARRAY1, 1, 1)); assertTrue(Arrays.equals( new float[] {(float) 1, (float) 0, (float) 0}, Floats.ensureCapacity(ARRAY1, 2, 1))); } public void testEnsureCapacity_fail() { try { Floats.ensureCapacity(ARRAY1, -1, 1); fail(); } catch (IllegalArgumentException expected) { } try { // notice that this should even fail when no growth was needed Floats.ensureCapacity(ARRAY1, 1, -1); fail(); } catch (IllegalArgumentException expected) { } } @GwtIncompatible // Float.toString returns different value in GWT. public void testJoin() { assertEquals("", Floats.join(",", EMPTY)); assertEquals("1.0", Floats.join(",", ARRAY1)); assertEquals("1.0,2.0", Floats.join(",", (float) 1, (float) 2)); assertEquals("1.02.03.0", Floats.join("", (float) 1, (float) 2, (float) 3)); } public void testLexicographicalComparator() { List<float[]> ordered = Arrays.asList( new float[] {}, new float[] {LEAST}, new float[] {LEAST, LEAST}, new float[] {LEAST, (float) 1}, new float[] {(float) 1}, new float[] {(float) 1, LEAST}, new float[] {GREATEST, Float.MAX_VALUE}, new float[] {GREATEST, GREATEST}, new float[] {GREATEST, GREATEST, GREATEST}); Comparator<float[]> comparator = Floats.lexicographicalComparator(); Helpers.testComparator(comparator, ordered); } @GwtIncompatible // SerializableTester public void testLexicographicalComparatorSerializable() { Comparator<float[]> comparator = Floats.lexicographicalComparator(); assertSame(comparator, SerializableTester.reserialize(comparator)); } @GwtIncompatible // SerializableTester public void testStringConverterSerialization() { SerializableTester.reserializeAndAssert(Floats.stringConverter()); } public void testToArray() { // need explicit type parameter to avoid javac warning!? List<Float> none = Arrays. Other Java examples (source code examples)Here is a short list of links related to this Java FloatsTest.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.