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-2009 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.unexpectedEqual; 020 import static org.fest.assertions.ErrorMessages.unexpectedNotEqual; 021 import static org.fest.assertions.Formatting.inBrackets; 022 import static org.fest.util.Strings.concat; 023 024 import java.util.Arrays; 025 026 /** 027 * Understands assertion methods for <code>double</code> arrays. To create a new instance of this class use the method 028 * <code>{@link Assertions#assertThat(double[])}</code>. 029 * 030 * @author Yvonne Wang 031 * @author Alex Ruiz 032 */ 033 public class DoubleArrayAssert extends ArrayAssert<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(actual); 041 } 042 043 /** {@inheritDoc} */ 044 public DoubleArrayAssert as(String description) { 045 description(description); 046 return this; 047 } 048 049 /** {@inheritDoc} */ 050 public DoubleArrayAssert describedAs(String description) { 051 return as(description); 052 } 053 054 /** {@inheritDoc} */ 055 public DoubleArrayAssert as(Description description) { 056 description(description); 057 return this; 058 } 059 060 /** {@inheritDoc} */ 061 public DoubleArrayAssert describedAs(Description description) { 062 return as(description); 063 } 064 065 /** 066 * Verifies that the actual <code>double</code> array contains the given values. 067 * @param values the values to look for. 068 * @return this assertion object. 069 * @throws AssertionError if the actual <code>double</code> array is <code>null</code>. 070 * @throws NullPointerException if the given <code>double</code> array is <code>null</code>. 071 * @throws AssertionError if the actual <code>double</code> array does not contain the given values. 072 */ 073 public DoubleArrayAssert contains(double... values) { 074 isNotNull(); 075 validateIsNotNull(values); 076 assertContains(copy(values)); 077 return this; 078 } 079 080 /** 081 * Verifies that the actual <code>double</code> array contains the given values <strong>only</strong>. 082 * @param values the values to look for. 083 * @return this assertion object. 084 * @throws AssertionError if the actual <code>double</code> array is <code>null</code>. 085 * @throws NullPointerException if the given <code>double</code> array is <code>null</code>. 086 * @throws AssertionError if the actual <code>double</code> array does not contain the given objects, or if the actual 087 * <code>double</code> array contains elements other than the ones specified. 088 */ 089 public DoubleArrayAssert containsOnly(double... values) { 090 isNotNull(); 091 validateIsNotNull(values); 092 assertContainsOnly(copy(values)); 093 return this; 094 } 095 096 /** 097 * Verifies that the actual <code>double</code> array does not contain the given values. 098 * @param values the values the array should exclude. 099 * @return this assertion object. 100 * @throws AssertionError if the actual <code>double</code> array is <code>null</code>. 101 * @throws NullPointerException if the given <code>double</code> array is <code>null</code>. 102 * @throws AssertionError if the actual <code>double</code> array contains any of the given values. 103 */ 104 public DoubleArrayAssert excludes(double... values) { 105 isNotNull(); 106 validateIsNotNull(values); 107 assertExcludes(copy(values)); 108 return this; 109 } 110 111 private void validateIsNotNull(double[] values) { 112 if (values == null) 113 throw new NullPointerException(formattedErrorMessage("the given array of doubles should not be null")); 114 } 115 116 /** 117 * Verifies that the actual <code>double</code> array satisfies the given condition. 118 * @param condition the given condition. 119 * @return this assertion object. 120 * @throws NullPointerException if the given condition is <code>null</code>. 121 * @throws AssertionError if the actual <code>double</code> array does not satisfy the given condition. 122 * @see #is(Condition) 123 */ 124 public DoubleArrayAssert satisfies(Condition<double[]> condition) { 125 assertSatisfies(condition); 126 return this; 127 } 128 129 /** 130 * Verifies that the actual <code>double</code> array does not satisfy the given condition. 131 * @param condition the given condition. 132 * @return this assertion object. 133 * @throws NullPointerException if the given condition is <code>null</code>. 134 * @throws AssertionError if the actual <code>double</code> array satisfies the given condition. 135 * @see #isNot(Condition) 136 */ 137 public DoubleArrayAssert doesNotSatisfy(Condition<double[]> condition) { 138 assertDoesNotSatisfy(condition); 139 return this; 140 } 141 142 /** 143 * Alias for <code>{@link #satisfies(Condition)}</code>. 144 * @param condition the given condition. 145 * @return this assertion object. 146 * @throws NullPointerException if the given condition is <code>null</code>. 147 * @throws AssertionError if the actual <code>double</code> array does not satisfy the given condition. 148 * @since 1.2 149 */ 150 public DoubleArrayAssert is(Condition<double[]> condition) { 151 assertIs(condition); 152 return this; 153 } 154 155 /** 156 * Alias for <code>{@link #doesNotSatisfy(Condition)}</code>. 157 * @param condition the given condition. 158 * @return this assertion object. 159 * @throws NullPointerException if the given condition is <code>null</code>. 160 * @throws AssertionError if the actual <code>double</code> array satisfies the given condition. 161 * @since 1.2 162 */ 163 public DoubleArrayAssert isNot(Condition<double[]> condition) { 164 assertIsNot(condition); 165 return this; 166 } 167 168 /** 169 * Verifies that the actual <code>double</code> array is not <code>null</code>. 170 * @return this assertion object. 171 * @throws AssertionError if the actual <code>double</code> array is <code>null</code>. 172 */ 173 public DoubleArrayAssert isNotNull() { 174 assertThatActualIsNotNull(); 175 return this; 176 } 177 178 /** 179 * Verifies that the actual <code>double</code> array contains at least on element. 180 * @return this assertion object. 181 * @throws AssertionError if the actual <code>double</code> array is <code>null</code>. 182 * @throws AssertionError if the actual <code>double</code> array is empty. 183 */ 184 public DoubleArrayAssert isNotEmpty() { 185 assertThatActualIsNotEmpty(); 186 return this; 187 } 188 189 /** 190 * Verifies that the actual <code>double</code> array is equal to the given array. Array equality is checked by 191 * <code>{@link Arrays#equals(double[], double[])}</code>. 192 * @param expected the given array to compare the actual array to. 193 * @return this assertion object. 194 * @throws AssertionError if the actual <code>double</code> array is not equal to the given one. 195 */ 196 public DoubleArrayAssert isEqualTo(double[] expected) { 197 if (Arrays.equals(actual, expected)) return this; 198 failIfCustomMessageIsSet(); 199 throw failure(unexpectedNotEqual(actual, expected)); 200 } 201 202 /** 203 * Verifies that the actual <code>double</code> array is equal to the given array, within a positive delta. 204 * @param expected the given array to compare the actual array to. 205 * @param delta the given delta. 206 * @return this assertion object. 207 * @throws AssertionError if the actual <code>double</code> array is not equal to the given one. 208 * @since 1.1 209 */ 210 public DoubleArrayAssert isEqualTo(double[] expected, Delta delta) { 211 if (actual == expected) return this; 212 if (actual == null || expected == null) failNotEquals(expected, delta); 213 int length = expected.length; 214 if (actual.length != length) failNotEquals(expected, delta); 215 for (int i = 0; i < length; i++) 216 if (!equals(expected[i], actual[i], delta)) failNotEquals(expected, delta); 217 return this; 218 } 219 220 private void failNotEquals(double[] expected, Delta delta) { 221 failIfCustomMessageIsSet(); 222 fail(concat(unexpectedNotEqual(actual, expected), " using delta:", inBrackets(delta.doubleValue()))); 223 } 224 225 private boolean equals(double e, double a, Delta delta) { 226 if (Double.compare(e, a) == 0) return true; 227 return abs(e - a) <= delta.doubleValue(); 228 } 229 230 /** 231 * Verifies that the actual <code>double</code> array is not equal to the given array. Array equality is checked by 232 * <code>{@link Arrays#equals(double[], double[])}</code>. 233 * @param array the given array to compare the actual array to. 234 * @return this assertion object. 235 * @throws AssertionError if the actual <code>double</code> array is equal to the given one. 236 */ 237 public DoubleArrayAssert isNotEqualTo(double[] array) { 238 if (!Arrays.equals(actual, array)) return this; 239 failIfCustomMessageIsSet(); 240 throw failure(unexpectedEqual(actual, array)); 241 } 242 243 /** 244 * Verifies that the number of elements in the actual <code>double</code> array is equal to the given one. 245 * @param expected the expected number of elements in the actual <code>double</code> array. 246 * @return this assertion object. 247 * @throws AssertionError if the actual <code>double</code> array is <code>null</code>. 248 * @throws AssertionError if the number of elements in the actual <code>double</code> array is not equal to the given 249 * one. 250 */ 251 public DoubleArrayAssert hasSize(int expected) { 252 assertThatActualHasSize(expected); 253 return this; 254 } 255 256 /** 257 * Verifies that the actual <code>double</code> array is the same as the given array. 258 * @param expected the given array to compare the actual array to. 259 * @return this assertion object. 260 * @throws AssertionError if the actual <code>double</code> array is not the same as the given one. 261 */ 262 public DoubleArrayAssert isSameAs(double[] expected) { 263 assertSameAs(expected); 264 return this; 265 } 266 267 /** 268 * Verifies that the actual <code>double</code> array is not the same as the given array. 269 * @param expected the given array to compare the actual array to. 270 * @return this assertion object. 271 * @throws AssertionError if the actual <code>double</code> array is the same as the given one. 272 */ 273 public DoubleArrayAssert isNotSameAs(double[] expected) { 274 assertNotSameAs(expected); 275 return this; 276 } 277 278 /** {@inheritDoc} */ 279 public DoubleArrayAssert overridingErrorMessage(String message) { 280 replaceDefaultErrorMessagesWith(message); 281 return this; 282 } 283 }