Package com.google.common.truth
Class PrimitiveFloatArraySubject
- java.lang.Object
-
- com.google.common.truth.Subject
-
- com.google.common.truth.PrimitiveFloatArraySubject
-
public final class PrimitiveFloatArraySubject extends Subject
A Subject forfloat[].- Author:
- Christian Gruber (cgruber@israfil.net)
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classPrimitiveFloatArraySubject.FloatArrayAsIterableA partially specified check for doing assertions on the array similar to the assertions supported forIterablesubjects, in which the elements of the array under test are compared to expected elements using either exact or tolerant float equality: seeusingExactEquality()andusingTolerance(double).-
Nested classes/interfaces inherited from class com.google.common.truth.Subject
Subject.Factory<SubjectT extends Subject,ActualT>
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidhasLength(int length)Fails if the array does not have the given length.voidisEmpty()Fails if the array is not empty (i.e.voidisEqualTo(Object expected)A check that the actual array andexpectedare arrays of the same length and type, containing elements such that each element inexpectedis equal to each element in the actual array, and in the same position, with element equality defined the same way thatArrays.equals(float[], float[])andFloat.equals(Object)define it (which is different to the way that the==operator on primitivefloatdefines it).voidisNotEmpty()Fails if the array is empty (i.e.voidisNotEqualTo(Object expected)A check that the actual array andexpectedare not arrays of the same length and type, containing elements such that each element inexpectedis equal to each element in the actual array, and in the same position, with element equality defined the same way thatArrays.equals(float[], float[])andFloat.equals(Object)define it (which is different to the way that the==operator on primitivefloatdefines it).PrimitiveFloatArraySubject.FloatArrayAsIterableusingExactEquality()Starts a method chain for a check in which the actual values (i.e.PrimitiveFloatArraySubject.FloatArrayAsIterableusingTolerance(double tolerance)Starts a method chain for a check in which the actual values (i.e.-
Methods inherited from class com.google.common.truth.Subject
actualCustomStringRepresentation, check, equals, failWithActual, failWithActual, failWithoutActual, hashCode, ignoreCheck, isAnyOf, isIn, isInstanceOf, isNoneOf, isNotIn, isNotInstanceOf, isNotNull, isNotSameInstanceAs, isNull, isSameInstanceAs, toString
-
-
-
-
Method Detail
-
isEqualTo
public void isEqualTo(Object expected)
A check that the actual array andexpectedare arrays of the same length and type, containing elements such that each element inexpectedis equal to each element in the actual array, and in the same position, with element equality defined the same way thatArrays.equals(float[], float[])andFloat.equals(Object)define it (which is different to the way that the==operator on primitivefloatdefines it). This method is not recommended when the code under test is doing any kind of arithmetic: useusingTolerance(double)with a suitable tolerance in that case, e.g.assertThat(actualArray).usingTolerance(1.0e-5).containsExactly(expectedArray).inOrder(). (Remember that the exact result of floating point arithmetic is sensitive to apparently trivial changes such as replacing(a + b) + cwitha + (b + c), and that unlessstrictfpis in force even the result of(a + b) + cis sensitive to the JVM's choice of precision for the intermediate result.) This method is recommended when the code under test is specified as either copying values without modification from its input or returning well-defined literal or constant values.- It considers
Float.POSITIVE_INFINITY,Float.NEGATIVE_INFINITY, andFloat.NaNto be equal to themselves (contrast withusingTolerance(0.0)which does not). - It does not consider
-0.0fto be equal to0.0f(contrast withusingTolerance(0.0)which does).
- It considers
-
isNotEqualTo
public void isNotEqualTo(Object expected)
A check that the actual array andexpectedare not arrays of the same length and type, containing elements such that each element inexpectedis equal to each element in the actual array, and in the same position, with element equality defined the same way thatArrays.equals(float[], float[])andFloat.equals(Object)define it (which is different to the way that the==operator on primitivefloatdefines it). SeeisEqualTo(Object)for advice on when exact equality is recommended.- It considers
Float.POSITIVE_INFINITY,Float.NEGATIVE_INFINITY, andFloat.NaNto be equal to themselves. - It does not consider
-0.0to be equal to0.0.
- Overrides:
isNotEqualToin classSubject
- It considers
-
usingTolerance
public PrimitiveFloatArraySubject.FloatArrayAsIterable usingTolerance(double tolerance)
Starts a method chain for a check in which the actual values (i.e. the elements of the array under test) are compared to expected elements using aCorrespondencewhich considers values to correspond if they are finite values withintoleranceof each other. The check is actually executed by continuing the method chain. For example:assertThat(actualFloatArray).usingTolerance(1.0e-5f).contains(3.14159f);- It does not consider values to correspond if either value is infinite or NaN.
- It considers
-0.0fto be within any tolerance of0.0f. - The expected values provided later in the chain will be
Numberinstances which will be converted to floats, which may result in a loss of precision for some numeric types. - The subsequent methods in the chain may throw a
NullPointerExceptionif any expectedNumberinstance is null.
- Parameters:
tolerance- an inclusive upper bound on the difference between the float values of the actual and expected numbers, which must be a non-negative finite value, i.e. notFloat.NaN,Float.POSITIVE_INFINITY, or negative, including-0.0f
-
usingExactEquality
public PrimitiveFloatArraySubject.FloatArrayAsIterable usingExactEquality()
Starts a method chain for a check in which the actual values (i.e. the elements of the array under test) are compared to expected elements using aCorrespondencewhich considers values to correspond if they are exactly equal, with equality defined byFloat.equals(java.lang.Object). This method is not recommended when the code under test is doing any kind of arithmetic: useusingTolerance(double)with a suitable tolerance in that case. (Remember that the exact result of floating point arithmetic is sensitive to apparently trivial changes such as replacing(a + b) + cwitha + (b + c), and that unlessstrictfpis in force even the result of(a + b) + cis sensitive to the JVM's choice of precision for the intermediate result.) This method is recommended when the code under test is specified as either copying a value without modification from its input or returning a well-defined literal or constant value. The check is actually executed by continuing the method chain. For example:assertThat(actualFloatArray).usingExactEquality().contains(3.14159f);For convenience, some subsequent methods accept expected values as
Numberinstances. These numbers must be either of typeFloat,Integer, orLong, and if they areIntegerorLongthen their absolute values must not exceed 2^24 which is 16,777,216. (This restriction ensures that the expected values have exactFloatrepresentations: using exact equality makes no sense if they do not.)- It considers
Float.POSITIVE_INFINITY,Float.NEGATIVE_INFINITY, andFloat.NaNto be equal to themselves (contrast withusingTolerance(0.0)which does not). - It does not consider
-0.0fto be equal to0.0f(contrast withusingTolerance(0.0)which does). - The subsequent methods in the chain may throw a
NullPointerExceptionif any expectedFloatinstance is null.
- It considers
-
isEmpty
public final void isEmpty()
Fails if the array is not empty (i.e.array.length > 0).
-
isNotEmpty
public final void isNotEmpty()
Fails if the array is empty (i.e.array.length == 0).
-
hasLength
public final void hasLength(int length)
Fails if the array does not have the given length.- Throws:
IllegalArgumentException- iflength < 0
-
-