001 /*
002 * Created on Feb 14, 2008
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
005 * the License. You may obtain a copy of the License at
006 *
007 * http://www.apache.org/licenses/LICENSE-2.0
008 *
009 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
010 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
011 * specific language governing permissions and limitations under the License.
012 *
013 * Copyright @2008-2011 the original author or authors.
014 */
015 package org.fest.assertions;
016
017 import static java.lang.Math.abs;
018 import static org.fest.assertions.ArrayInspection.copy;
019 import static org.fest.assertions.ErrorMessages.*;
020 import static org.fest.assertions.Formatting.format;
021
022 import java.util.Arrays;
023
024 /**
025 * Assertions for {@code double} arrays.
026 * <p>
027 * To create a new instance of this class invoke <code>{@link Assertions#assertThat(double[])}</code>.
028 * </p>
029 *
030 * @author Yvonne Wang
031 * @author Alex Ruiz
032 */
033 public class DoubleArrayAssert extends ArrayAssert<DoubleArrayAssert, double[]> {
034
035 /**
036 * Creates a new </code>{@link DoubleArrayAssert}</code>.
037 * @param actual the target to verify.
038 */
039 protected DoubleArrayAssert(double... actual) {
040 super(DoubleArrayAssert.class, actual);
041 }
042
043 /**
044 * Verifies that the actual {@code double} array contains the given values.
045 * @param values the values to look for.
046 * @return this assertion object.
047 * @throws AssertionError if the actual {@code double} array is {@code null}.
048 * @throws NullPointerException if the given {@code double} array is {@code null}.
049 * @throws AssertionError if the actual {@code double} array does not contain the given values.
050 */
051 public DoubleArrayAssert contains(double... values) {
052 assertContains(copy(values));
053 return this;
054 }
055
056 /**
057 * Verifies that the actual {@code double} array contains the given values <strong>only</strong>.
058 * @param values the values to look for.
059 * @return this assertion object.
060 * @throws AssertionError if the actual {@code double} array is {@code null}.
061 * @throws NullPointerException if the given {@code double} array is {@code null}.
062 * @throws AssertionError if the actual {@code double} array does not contain the given objects, or if the actual
063 * {@code double} array contains elements other than the ones specified.
064 */
065 public DoubleArrayAssert containsOnly(double... values) {
066 assertContainsOnly(copy(values));
067 return this;
068 }
069
070 /**
071 * Verifies that the actual {@code double} array does not contain the given values.
072 * @param values the values the array should exclude.
073 * @return this assertion object.
074 * @throws AssertionError if the actual {@code double} array is {@code null}.
075 * @throws NullPointerException if the given {@code double} array is {@code null}.
076 * @throws AssertionError if the actual {@code double} array contains any of the given values.
077 */
078 public DoubleArrayAssert excludes(double... values) {
079 assertExcludes(copy(values));
080 return this;
081 }
082
083 /**
084 * Verifies that the actual {@code double} array is equal to the given array. Array equality is checked by
085 * <code>{@link Arrays#equals(double[], double[])}</code>.
086 * @param expected the given array to compare the actual array to.
087 * @return this assertion object.
088 * @throws AssertionError if the actual {@code double} array is not equal to the given one.
089 */
090 @Override public DoubleArrayAssert isEqualTo(double[] expected) {
091 if (Arrays.equals(actual, expected)) return this;
092 failIfCustomMessageIsSet();
093 throw failure(unexpectedNotEqual(actual, expected));
094 }
095
096 /**
097 * Verifies that the actual {@code double} array is equal to the given array, within a positive delta.
098 * @param expected the given array to compare the actual array to.
099 * @param delta the given delta.
100 * @return this assertion object.
101 * @throws AssertionError if the actual {@code double} array is not equal to the given one.
102 * @since 1.1
103 */
104 public DoubleArrayAssert isEqualTo(double[] expected, Delta delta) {
105 if (actual == expected) return this;
106 if (actual == null || expected == null) throw failureWhenNotEqual(expected, delta);
107 int length = expected.length;
108 if (actual.length != length) failureWhenNotEqual(expected, delta);
109 for (int i = 0; i < length; i++)
110 if (!equals(expected[i], actual[i], delta)) failureWhenNotEqual(expected, delta);
111 return this;
112 }
113
114 private AssertionError failureWhenNotEqual(double[] expected, Delta delta) {
115 failIfCustomMessageIsSet();
116 throw failure(unexpectedNotEqual(actual, expected) + format(" using delta:<%s>", delta.doubleValue()));
117 }
118
119 private boolean equals(double e, double a, Delta delta) {
120 if (Double.compare(e, a) == 0) return true;
121 return abs(e - a) <= delta.doubleValue();
122 }
123
124 /**
125 * Verifies that the actual {@code double} array is not equal to the given array. Array equality is checked by
126 * <code>{@link Arrays#equals(double[], double[])}</code>.
127 * @param array the given array to compare the actual array to.
128 * @return this assertion object.
129 * @throws AssertionError if the actual {@code double} array is equal to the given one.
130 */
131 @Override public DoubleArrayAssert isNotEqualTo(double[] array) {
132 if (!Arrays.equals(actual, array)) return this;
133 failIfCustomMessageIsSet();
134 throw failure(unexpectedEqual(actual, array));
135 }
136 }