Class PrimitiveDoubleArraySubject

java.lang.Object
com.google.common.truth.Subject
com.google.common.truth.PrimitiveDoubleArraySubject

public final class PrimitiveDoubleArraySubject extends Subject
A subject for double[].
Author:
Christian Gruber (cgruber@israfil.net)
  • Method Details

    • isEqualTo

      public void isEqualTo(@Nullable Object expected)
      Checks that the actual array and expected are arrays of the same length and type, containing elements such that each element in expected is equal to each element in the actual array, and in the same position, with element equality defined the same way that Arrays.equals(double[], double[]) and Double.equals(Object) define it (which is different to the way that the == operator on primitive double defines it). This method is not recommended when the code under test is doing any kind of arithmetic: use usingTolerance(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 with a + (b + c), and that unless strictfp 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.
      Overrides:
      isEqualTo in class Subject
    • isNotEqualTo

      public void isNotEqualTo(@Nullable Object expected)
      Checks that the actual array and expected are not arrays of the same length and type, containing elements such that each element in expected is equal to each element in the actual array, and in the same position, with element equality defined the same way that Arrays.equals(double[], double[]) and Double.equals(Object) define it (which is different to the way that the == operator on primitive double defines it). See isEqualTo(Object) for advice on when exact equality is recommended.
      Overrides:
      isNotEqualTo in class Subject
    • 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 a Correspondence which considers values to correspond if they are finite values within tolerance 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 of 0.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 expected Number 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. not Double.NaN, Double.POSITIVE_INFINITY, or negative, including -0.0
    • 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 a Correspondence which considers values to correspond if they are exactly equal, with equality defined by Double.equals(java.lang.Object). This method is not recommended when the code under test is doing any kind of arithmetic: use usingTolerance(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 with a + (b + c), and that unless strictfp 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 type Double, Float, Integer, or Long, and if they are Long then their absolute values must not exceed 2^53 which is just over 9e15. (This restriction ensures that the expected values have exact Double representations: using exact equality makes no sense if they do not.)

    • isEmpty

      public final void isEmpty()
      Checks that the actual array is empty (i.e., that array.length == 0).
    • isNotEmpty

      public final void isNotEmpty()
      Checks that the actual array is not empty (i.e., that array.length > 0).
    • hasLength

      public final void hasLength(int length)
      Checks that the actual array has the given length.
      Throws:
      IllegalArgumentException - if length < 0