001 /*
002 * Created on Oct 25, 2010
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 @2010-2011 the original author or authors.
014 */
015 package org.fest.assertions.internal;
016
017 import static java.lang.Math.abs;
018
019 import static org.fest.assertions.error.ShouldBeEqualWithinOffset.shouldBeEqual;
020 import static org.fest.assertions.internal.CommonValidations.checkOffsetIsNotNull;
021
022 import org.fest.assertions.core.AssertionInfo;
023 import org.fest.assertions.data.Offset;
024 import org.fest.util.ComparisonStrategy;
025 import org.fest.util.Objects;
026 import org.fest.util.StandardComparisonStrategy;
027 import org.fest.util.VisibleForTesting;
028
029 /**
030 * Reusable assertions for <code>{@link Double}</code>s.
031 *
032 * @author Alex Ruiz
033 * @author Joel Costigliola
034 */
035 public class Doubles extends RealNumbers<Double> {
036
037 private static final Doubles INSTANCE = new Doubles();
038
039 /**
040 * Returns the singleton instance of this class based on {@link StandardComparisonStrategy}.
041 * @return the singleton instance of this class based on {@link StandardComparisonStrategy}.
042 */
043 public static Doubles instance() {
044 return INSTANCE;
045 }
046
047 @VisibleForTesting
048 Doubles() {
049 super();
050 }
051
052 public Doubles(ComparisonStrategy comparisonStrategy) {
053 super(comparisonStrategy);
054 }
055
056 @Override
057 protected Double zero() {
058 return 0.0d;
059 }
060
061 @Override
062 protected Double NaN() {
063 return Double.NaN;
064 }
065
066 /**
067 * Verifies that two floats are equal within a positive offset.<br>
068 * It does not rely on the custom comparisonStrategy (if one is set) because using an offset is already a specific
069 * comparison strategy.
070 * @param info contains information about the assertion.
071 * @param actual the actual value.
072 * @param expected the expected value.
073 * @param offset the given positive offset.
074 * @throws NullPointerException if the given offset is {@code null}.
075 * @throws AssertionError if the actual value is {@code null}.
076 * @throws AssertionError if the actual value is not equal to the expected one.
077 */
078 // can't be defined in RealNumbers because Offset parameter must inherits from Number
079 // while RealNumber parameter must inherits from Comparable (sadly Number is not Comparable)
080 public void assertEqual(AssertionInfo info, Double actual, Double expected, Offset<Double> offset) {
081 checkOffsetIsNotNull(offset);
082 assertNotNull(info, actual);
083 // doesn't use areEqual method relying on comparisonStrategy attribute
084 if (Objects.areEqual(actual, expected)) return;
085 if (expected != null && isEqualTo(actual, expected, offset)) return;
086 throw failures.failure(info, shouldBeEqual(actual, expected, offset));
087 }
088
089 @Override
090 protected boolean isEqualTo(Double actual, Double expected, Offset<?> offset) {
091 return abs(expected.doubleValue() - actual.doubleValue()) <= offset.value.doubleValue();
092 }
093
094 }