001 /*
002 * Created on Jun 18, 2007
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 @2007-2011 the original author or authors.
014 */
015 package org.fest.assertions;
016
017 import static java.lang.Float.*;
018 import static java.lang.Math.abs;
019 import static org.fest.assertions.ErrorMessages.*;
020 import static org.fest.assertions.Formatting.format;
021
022 /**
023 * Assertions for {@code Float}s and {@code float}s.
024 * <p>
025 * To create a new instance of this class invoke either <code>{@link Assertions#assertThat(Float)}</code> or
026 * <code>{@link Assertions#assertThat(float)}</code>.
027 * </p>
028 *
029 * @author Yvonne Wang
030 * @author Alex Ruiz
031 * @author Ansgar Konermann
032 */
033 public class FloatAssert extends GenericAssert<FloatAssert, Float> implements NumberAssert {
034
035 private static final float ZERO = 0f;
036
037 /**
038 * Creates a new <code>{@link FloatAssert}</code>.
039 * @param actual the actual value to verify.
040 */
041 protected FloatAssert(float actual) {
042 super(FloatAssert.class, actual);
043 }
044
045 /**
046 * Creates a new <code>{@link FloatAssert}</code>.
047 * @param actual the actual value to verify.
048 */
049 protected FloatAssert(Float actual) {
050 super(FloatAssert.class, actual);
051 }
052
053 /**
054 * Verifies that the actual {@code Float} is equal to the given one.
055 * @param expected the value to compare the actual one to.
056 * @return this assertion object.
057 * @throws AssertionError if the actual {@code Float} is not equal to the given one.
058 */
059 public FloatAssert isEqualTo(float expected) {
060 return isEqualTo(valueOf(expected));
061 }
062
063 /**
064 * Verifies that the actual {@code Float} is equal to the given one, within a positive delta.
065 * @param expected the value to compare the actual one to.
066 * @param delta the given delta.
067 * @return this assertion object.
068 * @throws AssertionError if the actual {@code Float} is not equal to the given one.
069 * @deprecated use method <code>{@link #isEqualTo(float, org.fest.assertions.Delta)}</code> instead. This method will
070 * be removed in version 2.0.
071 */
072 @Deprecated
073 public FloatAssert isEqualTo(float expected, Delta delta) {
074 return isEqualTo(expected, delta.value);
075 }
076
077 /**
078 * Verifies that the actual {@code Float} is equal to the given one, within a positive delta.
079 * @param expected the value to compare the actual one to.
080 * @param delta the given delta.
081 * @return this assertion object.
082 * @throws AssertionError if the actual {@code Float} is not equal to the given one.
083 * @since 1.2
084 */
085 public FloatAssert isEqualTo(float expected, org.fest.assertions.Delta delta) {
086 return isEqualTo(expected, delta.floatValue());
087 }
088
089 private FloatAssert isEqualTo(float expected, float deltaValue) {
090 return isEqualTo(valueOf(expected), deltaValue);
091 }
092
093 /**
094 * Verifies that the actual {@code Float} is equal to the given one, within a positive delta.
095 * @param expected the value to compare the actual one to.
096 * @param delta the given delta.
097 * @return this assertion object.
098 * @throws AssertionError if the actual {@code Float} is not equal to the given one.
099 * @since 1.3
100 */
101 public FloatAssert isEqualTo(Float expected, org.fest.assertions.Delta delta) {
102 return isEqualTo(expected, delta.floatValue());
103 }
104
105 private FloatAssert isEqualTo(Float expected, float deltaValue) {
106 if (actual == null || expected == null) return isEqualTo(expected);
107 if (actual.compareTo(expected) == 0) return this;
108 if (abs(expected - actual) <= deltaValue) return this;
109 failIfCustomMessageIsSet();
110 throw failure(unexpectedNotEqual(actual, expected) + format(" using delta:<%s>", deltaValue));
111 }
112
113 /**
114 * Verifies that the actual {@code Float} is not equal to the given one.
115 * @param other the given value.
116 * @return this assertion object.
117 * @throws AssertionError if the actual {@code Float} is equal to the given one.
118 */
119 public FloatAssert isNotEqualTo(float other) {
120 if (compareTo(other) != 0) return this;
121 failIfCustomMessageIsSet();
122 throw failure(unexpectedEqual(actual, other));
123 }
124
125 /**
126 * Verifies that the actual {@code Float} is greater than the given one.
127 * @param other the given value.
128 * @return this assertion object.
129 * @throws AssertionError if the actual {@code Float} is not greater than the given one.
130 */
131 public FloatAssert isGreaterThan(float other) {
132 if (compareTo(other) > 0) return this;
133 failIfCustomMessageIsSet();
134 throw failure(unexpectedLessThanOrEqualTo(actual, other));
135 }
136
137 /**
138 * Verifies that the actual {@code Float} is less than the given one.
139 * @param other the given value.
140 * @return this assertion object.
141 * @throws AssertionError if the actual {@code Float} is not less than the given one.
142 */
143 public FloatAssert isLessThan(float other) {
144 if (compareTo(other) < 0) return this;
145 failIfCustomMessageIsSet();
146 throw failure(unexpectedGreaterThanOrEqualTo(actual, other));
147 }
148
149 /**
150 * Verifies that the actual {@code Float} is greater or equal to the given one.
151 * @param other the given value.
152 * @return this assertion object.
153 * @throws AssertionError if the actual {@code Float} is not greater than or equal to the given one.
154 */
155 public FloatAssert isGreaterThanOrEqualTo(float other) {
156 if (compareTo(other) >= 0) return this;
157 failIfCustomMessageIsSet();
158 throw failure(unexpectedLessThan(actual, other));
159 }
160
161 /**
162 * Verifies that the actual {@code Float} is less or equal to the given one.
163 * @param other the given value.
164 * @return this assertion object.
165 * @throws AssertionError if the actual {@code Float} is not less than or equal to the given one.
166 */
167 public FloatAssert isLessThanOrEqualTo(float other) {
168 if (compareTo(other) <= 0) return this;
169 failIfCustomMessageIsSet();
170 throw failure(unexpectedGreaterThan(actual, other));
171 }
172
173 private int compareTo(float other) {
174 return compare(actual, other);
175 }
176
177 /**
178 * Verifies that the actual {@code Float} is equal to <code>{@link Float#NaN}</code>.
179 * @return this assertion object.
180 * @throws AssertionError if the actual {@code Float} is not equal to <code>NaN</code>.
181 */
182 public FloatAssert isNaN() {
183 return isEqualTo(Float.NaN);
184 }
185
186 /**
187 * Verifies that the actual {@code Float} is equal to zero.
188 * @return this assertion object.
189 * @throws AssertionError if the actual {@code Float} is not equal to zero.
190 */
191 public FloatAssert isZero() {
192 return isEqualTo(ZERO);
193 }
194
195 /**
196 * Verifies that the actual {@code Float} is positive.
197 * @return this assertion object.
198 * @throws AssertionError if the actual {@code Float} is not positive.
199 */
200 public FloatAssert isPositive() {
201 return isGreaterThan(ZERO);
202 }
203
204 /**
205 * Verifies that the actual {@code Float} is negative.
206 * @return this assertion object.
207 * @throws AssertionError if the actual {@code Float} is not negative.
208 */
209 public FloatAssert isNegative() {
210 return isLessThan(ZERO);
211 }
212
213 /**
214 * Creates a new holder for a delta value to be used in
215 * <code>{@link FloatAssert#isEqualTo(float, org.fest.assertions.FloatAssert.Delta)}</code>.
216 * @param d the delta value.
217 * @return a new delta value holder.
218 * @deprecated use method <code>{@link org.fest.assertions.Delta#delta(double)}</code> instead. This method will be
219 * removed in version 2.0.
220 */
221 @Deprecated
222 public static Delta delta(float d) {
223 return new Delta(d);
224 }
225
226 /**
227 * Holds a delta value to be used in
228 * <code>{@link FloatAssert#isEqualTo(float, org.fest.assertions.FloatAssert.Delta)}</code>.
229 * @deprecated use top-level class <code>{@link org.fest.assertions.Delta}</code> instead. This class will be removed
230 * in version 2.0.
231 */
232 @Deprecated
233 public static class Delta {
234 final float value;
235
236 private Delta(float value) {
237 this.value = value;
238 }
239 }
240 }
241
242