Package com.google.common.truth
Class PrimitiveDoubleArraySubject
- java.lang.Object
-
- com.google.common.truth.Subject
-
- com.google.common.truth.AbstractArraySubject
-
- com.google.common.truth.PrimitiveDoubleArraySubject
-
public final class PrimitiveDoubleArraySubject extends AbstractArraySubject
A Subject fordouble[]
.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
PrimitiveDoubleArraySubject.DoubleArrayAsIterable
A partially specified check for doing assertions on the array similar to the assertions supported forIterable
subjects, in which the elements of the array under test are compared to expected elements using either exact or tolerant double equality: seeusingExactEquality()
andusingTolerance(double)
.private class
PrimitiveDoubleArraySubject.IterableSubjectWithInheritedToString
-
Nested classes/interfaces inherited from class com.google.common.truth.Subject
Subject.EqualityCheck, Subject.Factory<SubjectT extends Subject,ActualT>
-
-
Field Summary
Fields Modifier and Type Field Description private double[]
actual
private static Correspondence<java.lang.Double,java.lang.Number>
EXACT_EQUALITY_CORRESPONDENCE
-
Constructor Summary
Constructors Constructor Description PrimitiveDoubleArraySubject(FailureMetadata metadata, double[] o, java.lang.String typeDescription)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private static double
checkedToDouble(java.lang.Number expected)
void
isEqualTo(java.lang.Object expected)
A check that the actual array andexpected
are arrays of the same length and type, containing elements such that each element inexpected
is equal to each element in the actual array, and in the same position, with element equality defined the same way thatArrays.equals(double[], double[])
andDouble.equals(Object)
define it (which is different to the way that the==
operator on primitivedouble
defines it).void
isNotEqualTo(java.lang.Object expected)
A check that the actual array andexpected
are not arrays of the same length and type, containing elements such that each element inexpected
is equal to each element in the actual array, and in the same position, with element equality defined the same way thatArrays.equals(double[], double[])
andDouble.equals(Object)
define it (which is different to the way that the==
operator on primitivedouble
defines it).private IterableSubject
iterableSubject()
private Subject.Factory<IterableSubject,java.lang.Iterable<?>>
iterablesWithCustomDoubleToString()
PrimitiveDoubleArraySubject.DoubleArrayAsIterable
usingExactEquality()
Starts a method chain for a check in which the actual values (i.e.PrimitiveDoubleArraySubject.DoubleArrayAsIterable
usingTolerance(double tolerance)
Starts a method chain for a check in which the actual values (i.e.-
Methods inherited from class com.google.common.truth.AbstractArraySubject
hasLength, isEmpty, isNotEmpty
-
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 Detail
-
actual
private final double[] actual
-
EXACT_EQUALITY_CORRESPONDENCE
private static final Correspondence<java.lang.Double,java.lang.Number> EXACT_EQUALITY_CORRESPONDENCE
-
-
Constructor Detail
-
PrimitiveDoubleArraySubject
PrimitiveDoubleArraySubject(FailureMetadata metadata, double[] o, java.lang.String typeDescription)
-
-
Method Detail
-
isEqualTo
public void isEqualTo(java.lang.Object expected)
A check that the actual array andexpected
are arrays of the same length and type, containing elements such that each element inexpected
is equal to each element in the actual array, and in the same position, with element equality defined the same way thatArrays.equals(double[], double[])
andDouble.equals(Object)
define it (which is different to the way that the==
operator on primitivedouble
defines 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-10).containsExactly(expectedArray).inOrder()
. (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 values without modification from its input or returning well-defined literal or constant values.- It considers
Double.POSITIVE_INFINITY
,Double.NEGATIVE_INFINITY
, andDouble.NaN
to be equal to themselves (contrast withusingTolerance(0.0)
which does not). - It does not consider
-0.0
to be equal to0.0
(contrast withusingTolerance(0.0)
which does).
- It considers
-
isNotEqualTo
public void isNotEqualTo(java.lang.Object expected)
A check that the actual array andexpected
are not arrays of the same length and type, containing elements such that each element inexpected
is equal to each element in the actual array, and in the same position, with element equality defined the same way thatArrays.equals(double[], double[])
andDouble.equals(Object)
define it (which is different to the way that the==
operator on primitivedouble
defines it). SeeisEqualTo(Object)
for advice on when exact equality is recommended.- It considers
Double.POSITIVE_INFINITY
,Double.NEGATIVE_INFINITY
, andDouble.NaN
to be equal to themselves. - It does not consider
-0.0
to be equal to0.0
.
- Overrides:
isNotEqualTo
in classSubject
- It considers
-
usingTolerance
public PrimitiveDoubleArraySubject.DoubleArrayAsIterable 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 aCorrespondence
which considers values to correspond if they are finite values withintolerance
of each other. The check is actually executed by continuing the method chain. For example:assertThat(actualDoubleArray).usingTolerance(1.0e-5).contains(3.14159);
- It does not consider values to correspond if either value is infinite or NaN.
- It considers
-0.0
to be within any tolerance of0.0
. - The expected values provided later in the chain will be
Number
instances which will be converted to doubles, which may result in a loss of precision for some numeric types. - The subsequent methods in the chain may throw a
NullPointerException
if any expectedNumber
instance is null.
- Parameters:
tolerance
- an inclusive upper bound on the difference between the double values of the actual and expected numbers, which must be a non-negative finite value, i.e. notDouble.NaN
,Double.POSITIVE_INFINITY
, or negative, including-0.0
-
checkedToDouble
private static double checkedToDouble(java.lang.Number expected)
-
usingExactEquality
public PrimitiveDoubleArraySubject.DoubleArrayAsIterable 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 aCorrespondence
which considers values to correspond if they are exactly equal, with equality defined byDouble.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) + 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. The check is actually executed by continuing the method chain. For example:assertThat(actualDoubleArray).usingExactEquality().contains(3.14159);
For convenience, some subsequent methods accept expected values as
Number
instances. These numbers must be either of typeDouble
,Float
,Integer
, orLong
, and if they areLong
then their absolute values must not exceed 2^53 which is just over 9e15. (This restriction ensures that the expected values have exactDouble
representations: using exact equality makes no sense if they do not.)- It considers
Double.POSITIVE_INFINITY
,Double.NEGATIVE_INFINITY
, andDouble.NaN
to be equal to themselves (contrast withusingTolerance(0.0)
which does not). - It does not consider
-0.0
to be equal to0.0
(contrast withusingTolerance(0.0)
which does not). - The subsequent methods in the chain may throw a
NullPointerException
if any expectedDouble
instance is null.
- It considers
-
iterableSubject
private IterableSubject iterableSubject()
-
iterablesWithCustomDoubleToString
private Subject.Factory<IterableSubject,java.lang.Iterable<?>> iterablesWithCustomDoubleToString()
-
-