Class FloatSubject
Float
subjects.-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic class
A partially specified check about an approximate relationship to afloat
subject using a tolerance.Nested classes/interfaces inherited from class com.google.common.truth.Subject
Subject.EqualityCheck, Subject.Factory<SubjectT extends Subject,
ActualT> -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final Float
private final DoubleSubject
private static final int
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescription(package private) static void
checkTolerance
(float tolerance) Ensures that the given tolerance is a non-negative finite value, i.e.final void
isAtLeast
(int other) Checks that the subject is greater than or equal toother
.final void
isAtMost
(int other) Checks that the subject is less than or equal toother
.final void
Asserts that the subject is exactly equal to the given value, with equality defined as byFloat#equals
.final void
Deprecated.final void
isFinite()
Asserts that the subject is finite, i.e.final void
isGreaterThan
(int other) Checks that the subject is greater thanother
.final void
isLessThan
(int other) Checks that the subject is less thanother
.final void
isNaN()
Asserts that the subject isFloat.NaN
.final void
Asserts that the subject isFloat.NEGATIVE_INFINITY
.final void
Asserts that the subject is a non-null value other than zero (i.e.final void
isNotEqualTo
(Object other) Asserts that the subject is not exactly equal to the given value, with equality defined as byFloat#equals
.final void
isNotNaN()
Asserts that the subject is a non-null value other thanFloat.NaN
(but it may beFloat.POSITIVE_INFINITY
orFloat.NEGATIVE_INFINITY
).isNotWithin
(float tolerance) Prepares for a check that the subject is a finite number not within the given tolerance of an expected value that will be provided in the next call in the fluent chain.final void
Asserts that the subject isFloat.POSITIVE_INFINITY
.isWithin
(float tolerance) Prepares for a check that the subject is a finite number within the given tolerance of an expected value that will be provided in the next call in the fluent chain.final void
isZero()
Asserts that the subject is zero (i.e.Methods inherited from class com.google.common.truth.ComparableSubject
isAtLeast, isAtMost, isGreaterThan, isIn, isLessThan, isNotIn
Methods inherited from class com.google.common.truth.Subject
actual, actualCustomStringRepresentation, actualCustomStringRepresentationForPackageMembersToCall, butWas, check, check, checkNoNeedToDisplayBothValues, equals, fail, fail, fail, failEqualityCheckForEqualsWithoutDescription, failWithActual, failWithActual, failWithActual, failWithActual, failWithBadResults, failWithCustomSubject, failWithoutActual, failWithoutActual, failWithoutActual, failWithoutActual, failWithoutSubject, hashCode, ignoreCheck, isAnyOf, isIn, isInstanceOf, isNoneOf, isNotIn, isNotInstanceOf, isNotNull, isNotSameInstanceAs, isNull, isSameInstanceAs, toString, typeDescription
-
Field Details
-
NEG_ZERO_BITS
private static final int NEG_ZERO_BITS -
actual
-
asDouble
-
-
Constructor Details
-
FloatSubject
FloatSubject(FailureMetadata metadata, Float actual)
-
-
Method Details
-
isWithin
Prepares for a check that the subject is a finite number within the given tolerance of an expected value that will be provided in the next call in the fluent chain.The check will fail if either the subject or the object is
Float.POSITIVE_INFINITY
,Float.NEGATIVE_INFINITY
, orFloat.NaN
. To check for those values, useisPositiveInfinity()
,isNegativeInfinity()
,isNaN()
, or (with more generality)isEqualTo(java.lang.Object)
.The check will pass if both values are zero, even if one is
0.0f
and the other is-0.0f
. Use#isEqualTo
to assert that a value is exactly0.0f
or that it is exactly-0.0f
.You can use a tolerance of
0.0f
to assert the exact equality of finite floats, but oftenisEqualTo(java.lang.Object)
is preferable (note the different behaviours around non-finite values and-0.0f
). See the documentation onisEqualTo(java.lang.Object)
for advice on when exact equality assertions are appropriate.- Parameters:
tolerance
- an inclusive upper bound on the difference between the subject and object allowed by the check, which must be a non-negative finite value, i.e. notFloat.NaN
,Float.POSITIVE_INFINITY
, or negative, including-0.0f
-
isNotWithin
Prepares for a check that the subject is a finite number not within the given tolerance of an expected value that will be provided in the next call in the fluent chain.The check will fail if either the subject or the object is
Float.POSITIVE_INFINITY
,Float.NEGATIVE_INFINITY
, orFloat.NaN
. SeeisFinite()
,isNotNaN()
, orisNotEqualTo(java.lang.Object)
for checks with other behaviours.The check will fail if both values are zero, even if one is
0.0f
and the other is-0.0f
. Use#isNotEqualTo
for a test which fails for a value of exactly zero with one sign but passes for zero with the opposite sign.You can use a tolerance of
0.0f
to assert the exact non-equality of finite floats, but sometimesisNotEqualTo(java.lang.Object)
is preferable (note the different behaviours around non-finite values and-0.0f
).- Parameters:
tolerance
- an exclusive lower bound on the difference between the subject and object allowed by the check, which must be a non-negative finite value, i.e. notFloat.NaN
,Float.POSITIVE_INFINITY
, or negative, including-0.0f
-
isEqualTo
Asserts that the subject is exactly equal to the given value, with equality defined as byFloat#equals
. This method is not recommended when the code under test is doing any kind of arithmetic: useisWithin(float)
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) + c
witha + (b + c)
, and that unlessstrictfp
is in force even the result of(a + b) + c
is 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.Note: The assertion
isEqualTo(0.0f)
fails for an input of-0.0f
, and vice versa. For an assertion that passes for either0.0f
or-0.0f
, useisZero()
. -
isNotEqualTo
Asserts that the subject is not exactly equal to the given value, with equality defined as byFloat#equals
. SeeisEqualTo(java.lang.Object)
for advice on when exact equality is recommended. UseisNotWithin(float)
for an assertion with a tolerance.Note: The assertion
isNotEqualTo(0.0f)
passes for-0.0f
, and vice versa. For an assertion that fails for either0.0f
or-0.0f
, useisNonZero()
.- Overrides:
isNotEqualTo
in classSubject
-
isEquivalentAccordingToCompareTo
Deprecated.UseisWithin(float)
orisEqualTo(java.lang.Object)
instead (see documentation for advice).Description copied from class:ComparableSubject
Checks that the subject is equivalent toother
according toComparable.compareTo(T)
, (i.e., checks thata.comparesTo(b) == 0
).Note: Do not use this method for checking object equality. Instead, use
Subject.isEqualTo(Object)
.- Overrides:
isEquivalentAccordingToCompareTo
in classComparableSubject<Float>
-
checkTolerance
static void checkTolerance(float tolerance) Ensures that the given tolerance is a non-negative finite value, i.e. notFloat.NaN
,Float.POSITIVE_INFINITY
, or negative, including-0.0f
. -
isZero
public final void isZero()Asserts that the subject is zero (i.e. it is either0.0f
or-0.0f
). -
isNonZero
public final void isNonZero()Asserts that the subject is a non-null value other than zero (i.e. it is not0.0f
,-0.0f
ornull
). -
isPositiveInfinity
public final void isPositiveInfinity()Asserts that the subject isFloat.POSITIVE_INFINITY
. -
isNegativeInfinity
public final void isNegativeInfinity()Asserts that the subject isFloat.NEGATIVE_INFINITY
. -
isNaN
public final void isNaN()Asserts that the subject isFloat.NaN
. -
isFinite
public final void isFinite()Asserts that the subject is finite, i.e. notFloat.POSITIVE_INFINITY
,Float.NEGATIVE_INFINITY
, orFloat.NaN
. -
isNotNaN
public final void isNotNaN()Asserts that the subject is a non-null value other thanFloat.NaN
(but it may beFloat.POSITIVE_INFINITY
orFloat.NEGATIVE_INFINITY
). -
isGreaterThan
public final void isGreaterThan(int other) Checks that the subject is greater thanother
.To check that the subject is greater than or equal to
other
, useisAtLeast(int)
. -
isLessThan
public final void isLessThan(int other) Checks that the subject is less thanother
.To check that the subject is less than or equal to
other
, useisAtMost(int)
. -
isAtMost
public final void isAtMost(int other) Checks that the subject is less than or equal toother
.To check that the subject is strictly less than
other
, useisLessThan(int)
. -
isAtLeast
public final void isAtLeast(int other) Checks that the subject is greater than or equal toother
.To check that the subject is strictly greater than
other
, useisGreaterThan(int)
.
-
isWithin(float)
orisEqualTo(java.lang.Object)
instead (see documentation for advice).