001 /* 002 * Created on Oct 24, 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 Float}</code>s. 031 * 032 * @author Alex Ruiz 033 * @author Joel Costigliola 034 */ 035 public class Floats extends RealNumbers<Float> { 036 037 private static final Floats INSTANCE = new Floats(); 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 Floats instance() { 044 return INSTANCE; 045 } 046 047 @VisibleForTesting 048 Floats() { 049 super(); 050 } 051 052 public Floats(ComparisonStrategy comparisonStrategy) { 053 super(comparisonStrategy); 054 } 055 056 @Override 057 protected Float zero() { 058 return 0.0f; 059 } 060 061 @Override 062 protected Float NaN() { 063 return Float.NaN; 064 } 065 066 @Override 067 protected boolean isEqualTo(Float actual, Float expected, Offset<?> offset) { 068 return abs(expected - actual.floatValue()) <= offset.value.floatValue(); 069 } 070 071 /** 072 * Verifies that two floats are equal within a positive offset.<br> 073 * It does not rely on the custom comparisonStrategy (if one is set) because using an offset is already a specific 074 * comparison strategy. 075 * @param info contains information about the assertion. 076 * @param actual the actual value. 077 * @param expected the expected value. 078 * @param offset the given positive offset. 079 * @throws NullPointerException if the given offset is {@code null}. 080 * @throws AssertionError if the actual value is not equal to the expected one. 081 */ 082 // can't be defined in RealNumbers because Offset parameter must inherits from Number 083 // while RealNumber parameter must inherits from Comparable (sadly Number is not Comparable) 084 public void assertEqual(AssertionInfo info, Float actual, Float expected, Offset<Float> offset) { 085 assertNotNull(info, actual); 086 checkOffsetIsNotNull(offset); 087 // doesn't use areEqual method relying on comparisonStrategy attribute 088 if (Objects.areEqual(actual, expected)) return; 089 if (expected != null && isEqualTo(actual, expected, offset)) return; 090 throw failures.failure(info, shouldBeEqual(actual, expected, offset)); 091 } 092 093 }