001: /*
002: * $Id: TestFloatType.java,v 1.10 2005/05/02 22:32:03 ahimanikya Exp $
003: * =======================================================================
004: * Copyright (c) 2002-2005 Axion Development Team. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * 1. Redistributions of source code must retain the above
011: * copyright notice, this list of conditions and the following
012: * disclaimer.
013: *
014: * 2. Redistributions in binary form must reproduce the above copyright
015: * notice, this list of conditions and the following disclaimer in
016: * the documentation and/or other materials provided with the
017: * distribution.
018: *
019: * 3. The names "Tigris", "Axion", nor the names of its contributors may
020: * not be used to endorse or promote products derived from this
021: * software without specific prior written permission.
022: *
023: * 4. Products derived from this software may not be called "Axion", nor
024: * may "Tigris" or "Axion" appear in their names without specific prior
025: * written permission.
026: *
027: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
028: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
029: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
030: * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
031: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
032: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
033: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
034: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
035: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
036: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
037: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
038: * =======================================================================
039: */
040:
041: package org.axiondb.types;
042:
043: import java.io.ByteArrayInputStream;
044: import java.io.ByteArrayOutputStream;
045: import java.io.DataInputStream;
046: import java.io.DataOutputStream;
047: import java.sql.Timestamp;
048:
049: import junit.framework.Test;
050: import junit.framework.TestSuite;
051:
052: import org.axiondb.AxionException;
053: import org.axiondb.DataType;
054:
055: /**
056: * @version $Revision: 1.10 $ $Date: 2005/05/02 22:32:03 $
057: * @author Rodney Waldhoff
058: * @author Jonathan Giron
059: */
060: public class TestFloatType extends BaseNumericDataTypeTest {
061:
062: //------------------------------------------------------------ Conventional
063:
064: public TestFloatType(String testName) {
065: super (testName);
066: }
067:
068: public static Test suite() {
069: TestSuite suite = new TestSuite(TestFloatType.class);
070: return suite;
071: }
072:
073: //--------------------------------------------------------------- Lifecycle
074:
075: private DataType _type = null;
076:
077: public void setUp() throws Exception {
078: super .setUp();
079: _type = new FloatType();
080: }
081:
082: public void tearDown() throws Exception {
083: super .tearDown();
084: _type = null;
085: }
086:
087: //------------------------------------------------------------------- Super
088:
089: protected DataType getDataType() {
090: return _type;
091: }
092:
093: //------------------------------------------------------------------- Tests
094:
095: public void testSupportsSuccessor() throws Exception {
096: assertTrue(_type.supportsSuccessor());
097: }
098:
099: public void testGetPrecision() throws Exception {
100: assertEquals(12, getDataType().getPrecision());
101: assertEquals(10, getDataType().getPrecisionRadix());
102: }
103:
104: public void testConvertNonNumericString() throws Exception {
105: expectExceptionWhileConvertingNonNumericString("22018");
106: }
107:
108: public void testSuccessor() throws Exception {
109: assertTrue(_type.supportsSuccessor());
110:
111: // Test successor of negative value with largest magnitude.
112: Float f = new Float(-Float.MAX_VALUE);
113: Float result = (Float) _type.successor(f);
114: assertEquals(new Float(Float.intBitsToFloat(Float
115: .floatToIntBits(-Float.MAX_VALUE) - 1)), result);
116:
117: f = result;
118: result = (Float) _type.successor(f);
119: assertEquals(1, result.compareTo(f));
120:
121: // Test a few negative float values.
122: f = new Float(-1.512341235e10);
123: result = (Float) _type.successor(f);
124: assertEquals(1, result.compareTo(f)); // ensure successor is greater than source value
125:
126: f = new Float(-5.0);
127: result = (Float) _type.successor(f);
128: assertEquals(1, result.compareTo(f)); // ensure successor is greater than source value
129:
130: // Exercise the successor(float) method from just less then the smallest negative value
131: // to just greater than the smallest positive value.
132: f = new Float(Float.intBitsToFloat(Float
133: .floatToIntBits(-Float.MIN_VALUE) + 1));
134: result = (Float) _type.successor(f);
135: assertEquals(new Float(-Float.MIN_VALUE), result);
136:
137: f = result;
138: result = (Float) _type.successor(f);
139: assertEquals(new Float(-0.0), result);
140:
141: f = result;
142: result = (Float) _type.successor(f);
143: assertEquals(new Float(0.0), result);
144:
145: f = result;
146: result = (Float) _type.successor(f);
147: assertEquals(new Float(Float.MIN_VALUE), result);
148:
149: f = result;
150: result = (Float) _type.successor(f);
151: assertEquals(1, result.compareTo(f));
152:
153: // Test a few positive float values.
154: f = new Float(5.0);
155: result = (Float) _type.successor(f);
156: assertEquals(1, result.compareTo(f)); // ensure successor is greater than source value
157:
158: f = new Float(1.512341235e10);
159: result = (Float) _type.successor(f);
160: assertEquals(1, result.compareTo(f)); // ensure successor is greater than source value
161:
162: // Successor of [Float.MAX_VALUE - (min decrement)] should be Float.MAX_VALUE.
163: f = new Float(Float.intBitsToFloat(Float
164: .floatToIntBits(Float.MAX_VALUE) - 1));
165: result = (Float) _type.successor(f);
166: assertEquals(new Float(Float.MAX_VALUE), result);
167:
168: assertEquals(result, _type.successor(result));
169: }
170:
171: public void testRangeChecks() throws Exception {
172: final String msg = "Expected AxionException (22003)";
173:
174: final Double exceedsMaxPos = new Double(Float.MAX_VALUE + 1e38);
175: final Double exceedsMaxNeg = new Double(-Float.MAX_VALUE
176: - +1e38);
177:
178: try {
179: _type.convert(exceedsMaxPos);
180: fail(msg);
181: } catch (AxionException expected) {
182: assertEquals(msg, "22003", expected.getSQLState());
183: }
184:
185: try {
186: _type.convert(exceedsMaxNeg);
187: fail(msg);
188: } catch (AxionException expected) {
189: assertEquals(msg, "22003", expected.getSQLState());
190: }
191:
192: try {
193: _type.convert(exceedsMaxPos.toString());
194: fail(msg);
195: } catch (AxionException expected) {
196: assertEquals(msg, "22003", expected.getSQLState());
197: }
198:
199: try {
200: _type.convert(exceedsMaxNeg.toString());
201: fail(msg);
202: } catch (AxionException expected) {
203: assertEquals(msg, "22003", expected.getSQLState());
204: }
205: }
206:
207: public void testCompare() throws Exception {
208: compareEm(new Float[] { new Float(Float.NEGATIVE_INFINITY),
209: new Float(-1f * Float.MAX_VALUE), new Float(-1),
210: new Float(-1f * Float.MIN_VALUE), new Float(0),
211: new Float(Float.MIN_VALUE), new Float(1),
212: new Float(Float.MAX_VALUE),
213: new Float(Float.POSITIVE_INFINITY) });
214: }
215:
216: public void testAccepts() throws Exception {
217: assertTrue("Should accept Byte", _type.accepts(new Byte(
218: (byte) 3)));
219: assertTrue("Should accept Short", _type.accepts(new Short(
220: (short) 3)));
221: assertTrue("Should accept Integer", _type
222: .accepts(new Integer(3)));
223: assertTrue("Should accept Long", _type.accepts(new Long(3L)));
224:
225: assertTrue("Should accept Double", _type.accepts(new Double(
226: 3.14D)));
227: assertTrue("Should accept Float", _type
228: .accepts(new Float(3.14F)));
229:
230: assertTrue("Should accept integer String", _type.accepts("3"));
231: assertTrue("Should accept non-integer String", _type
232: .accepts("3.14159"));
233:
234: assertFalse("Should not accept Object", _type
235: .accepts(new Object()));
236: assertFalse("Should not accept Timestamp", _type
237: .accepts(new Timestamp(0L)));
238: }
239:
240: public void testConvertIntegerTypes() throws Exception {
241: assertEquals(new Float(17f), _type.convert(new Byte((byte) 17)));
242: assertEquals(new Float(17f), _type
243: .convert(new Short((short) 17)));
244: assertEquals(new Float(17f), _type.convert(new Integer(17)));
245: assertEquals(new Float(17f), _type.convert(new Long(17)));
246: }
247:
248: public void testConvertDecimalTypes() throws Exception {
249: assertEquals(new Float(17.99f), _type.convert(new Float(17.99)));
250: assertEquals(new Float(17.99f), _type
251: .convert(new Double(17.99)));
252: assertEquals(new Float(17.99f), _type.convert("17.99"));
253: }
254:
255: public void testConvertLiterals() throws Exception {
256: assertEquals(new Float(17f), _type.convert("17.0"));
257: assertEquals(new Float(-17f), _type.convert("-17.0"));
258:
259: assertEquals(new Float(17f), _type.convert(" 17"));
260: assertEquals(new Float(-17), _type.convert(" -17 "));
261:
262: assertEquals(new Float(17), _type.convert("17.0 "));
263: assertEquals(new Float(-17), _type.convert(" -17.0"));
264:
265: try {
266: _type.convert("- 150.0");
267: fail("Expected AxionException");
268: } catch (AxionException expected) {
269: assertEquals(
270: "Expected AxionException (22018) - invalid character value for cast",
271: "22018", expected.getSQLState());
272: }
273:
274: try {
275: _type.convert("abcd");
276: fail("Expected AxionException");
277: } catch (AxionException expected) {
278: assertEquals(
279: "Expected AxionException (22018) - invalid character value for cast",
280: "22018", expected.getSQLState());
281: }
282: }
283:
284: public void testWriteReadNonNull() throws Exception {
285: Float orig = new Float(17.3);
286: ByteArrayOutputStream buf = new ByteArrayOutputStream();
287: _type.write(orig, new DataOutputStream(buf));
288: Object read = _type.read(new DataInputStream(
289: new ByteArrayInputStream(buf.toByteArray())));
290: assertEquals(orig, read);
291: }
292:
293: public void testWriteReadSeveral() throws Exception {
294: Float[] data = { new Float(1.3), new Float(-1.3), null,
295: new Float(0.0), new Float(Float.MAX_VALUE), null,
296: new Float(Float.MIN_VALUE), new Float(17.0), null };
297: ByteArrayOutputStream out = new ByteArrayOutputStream();
298: for (int i = 0; i < data.length; i++) {
299: _type.write(data[i], new DataOutputStream(out));
300: }
301: DataInputStream in = new DataInputStream(
302: new ByteArrayInputStream(out.toByteArray()));
303: for (int i = 0; i < data.length; i++) {
304: Object read = _type.read(in);
305: if (null == data[i]) {
306: assertNull(read);
307: } else {
308: assertEquals(data[i], read);
309: }
310: }
311: }
312:
313: public void testInvalidWriteFails() throws Exception {
314: assertInvalidWriteFails(new Float(Float.POSITIVE_INFINITY));
315: assertInvalidWriteFails(new Float(Float.NEGATIVE_INFINITY));
316: assertInvalidWriteFails(new Float(Float.NaN));
317: }
318:
319: public void testGetColumnDisplaySize() {
320: assertEquals(String.valueOf(Float.MAX_VALUE).length(), _type
321: .getColumnDisplaySize());
322: }
323: }
|