Class DoubleSubject
Double
values.- Author:
- Kurt Alfred Kluever
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final class
A partially specified check about an approximate relationship to adouble
value using a tolerance.Nested classes/interfaces inherited from class com.google.common.truth.Subject
Subject.Factory<SubjectT extends Subject, ActualT>
-
Method Summary
Modifier and TypeMethodDescriptionvoid
isAtLeast
(int other) Checks that the actual value is greater than or equal toother
.void
isAtMost
(int other) Checks that the actual value is less than or equal toother
.void
Asserts that the actual value is exactly equal to the given value, with equality defined as byDouble#equals
.void
Deprecated.void
isFinite()
Asserts that the actual value is finite, i.e. notDouble.POSITIVE_INFINITY
,Double.NEGATIVE_INFINITY
, orDouble.NaN
.void
isGreaterThan
(int other) Checks that the actual value is greater thanother
.void
isLessThan
(int other) Checks that the actual value is less thanother
.void
isNaN()
Asserts that the actual value isDouble.NaN
.void
Asserts that the actual value isDouble.NEGATIVE_INFINITY
.void
Asserts that the actual value is a non-null value other than zero (i.e. it is not0.0
,-0.0
ornull
).void
isNotEqualTo
(@Nullable Object other) Asserts that the actual value is not exactly equal to the given value, with equality defined as byDouble#equals
.void
isNotNaN()
Asserts that the actual value is a non-null value other thanDouble.NaN
(but it may beDouble.POSITIVE_INFINITY
orDouble.NEGATIVE_INFINITY
).isNotWithin
(double tolerance) Prepares for a check that the actual value 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.void
Asserts that the actual value isDouble.POSITIVE_INFINITY
.isWithin
(double tolerance) Prepares for a check that the actual value is a finite number within the given tolerance of an expected value that will be provided in the next call in the fluent chain.void
isZero()
Asserts that the actual value is zero (i.e. it is either0.0
or-0.0
).Methods inherited from class com.google.common.truth.ComparableSubject
isAtLeast, isAtMost, isGreaterThan, isIn, isLessThan, isNotIn
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 Details
-
isWithin
Prepares for a check that the actual value 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 actual value or the expected value is
Double.POSITIVE_INFINITY
,Double.NEGATIVE_INFINITY
, orDouble.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.0
and the other is-0.0
. UseisEqualTo(java.lang.Object)
to assert that a value is exactly0.0
or that it is exactly-0.0
.You can use a tolerance of
0.0
to assert the exact equality of finite doubles, but oftenisEqualTo(java.lang.Object)
is preferable (note the different behaviours around non-finite values and-0.0
). 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 actual value and expected value allowed by the check, which must be a non-negative finite value, i.e. notDouble.NaN
,Double.POSITIVE_INFINITY
, or negative, including-0.0
-
isNotWithin
Prepares for a check that the actual value 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 actual value or the expected value is
Double.POSITIVE_INFINITY
,Double.NEGATIVE_INFINITY
, orDouble.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.0
and the other is-0.0
. UseisNotEqualTo(java.lang.Object)
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.0
to assert the exact non-equality of finite doubles, but sometimesisNotEqualTo(java.lang.Object)
is preferable (note the different behaviours around non-finite values and-0.0
).- Parameters:
tolerance
- an exclusive lower bound on the difference between the actual value and expected value allowed by the check, which must be a non-negative finite value, i.e. notDouble.NaN
,Double.POSITIVE_INFINITY
, or negative, including-0.0
-
isEqualTo
Asserts that the actual value is exactly equal to the given value, with equality defined as byDouble#equals
. This method is not recommended when the code under test is doing any kind of arithmetic: useisWithin(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) + 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.0)
fails for an input of-0.0
, and vice versa. For an assertion that passes for either0.0
or-0.0
, useisZero()
. -
isNotEqualTo
Asserts that the actual value is not exactly equal to the given value, with equality defined as byDouble#equals
. SeeisEqualTo(java.lang.Object)
for advice on when exact equality is recommended. UseisNotWithin(double)
for an assertion with a tolerance.Note: The assertion
isNotEqualTo(0.0)
passes for-0.0
, and vice versa. For an assertion that fails for either0.0
or-0.0
, useisNonZero()
.- Overrides:
isNotEqualTo
in classSubject
-
isEquivalentAccordingToCompareTo
Deprecated.UseisWithin(double)
orisEqualTo(java.lang.Object)
instead (see documentation for advice).Description copied from class:ComparableSubject
Checks that the actual value 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<Double>
-
isZero
public void isZero()Asserts that the actual value is zero (i.e. it is either0.0
or-0.0
). -
isNonZero
public void isNonZero()Asserts that the actual value is a non-null value other than zero (i.e. it is not0.0
,-0.0
ornull
). -
isPositiveInfinity
public void isPositiveInfinity()Asserts that the actual value isDouble.POSITIVE_INFINITY
. -
isNegativeInfinity
public void isNegativeInfinity()Asserts that the actual value isDouble.NEGATIVE_INFINITY
. -
isNaN
public void isNaN()Asserts that the actual value isDouble.NaN
. -
isFinite
public void isFinite()Asserts that the actual value is finite, i.e. notDouble.POSITIVE_INFINITY
,Double.NEGATIVE_INFINITY
, orDouble.NaN
. -
isNotNaN
public void isNotNaN()Asserts that the actual value is a non-null value other thanDouble.NaN
(but it may beDouble.POSITIVE_INFINITY
orDouble.NEGATIVE_INFINITY
). -
isGreaterThan
public void isGreaterThan(int other) Checks that the actual value is greater thanother
.To check that the actual value is greater than or equal to
other
, useisAtLeast(int)
. -
isLessThan
public void isLessThan(int other) Checks that the actual value is less thanother
.To check that the actual value is less than or equal to
other
, useisAtMost(int)
. -
isAtMost
public void isAtMost(int other) Checks that the actual value is less than or equal toother
.To check that the actual value is strictly less than
other
, useisLessThan(int)
. -
isAtLeast
public void isAtLeast(int other) Checks that the actual value is greater than or equal toother
.To check that the actual value is strictly greater than
other
, useisGreaterThan(int)
.
-
isWithin(double)
orisEqualTo(java.lang.Object)
instead (see documentation for advice).