001: /* Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com
002:
003: This file is part of the db4o open source object database.
004:
005: db4o is free software; you can redistribute it and/or modify it under
006: the terms of version 2 of the GNU General Public License as published
007: by the Free Software Foundation and as clarified by db4objects' GPL
008: interpretation policy, available at
009: http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
010: Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
011: Suite 350, San Mateo, CA 94403, USA.
012:
013: db4o is distributed in the hope that it will be useful, but WITHOUT ANY
014: WARRANTY; without even the implied warranty of MERCHANTABILITY or
015: FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
016: for more details.
017:
018: You should have received a copy of the GNU General Public License along
019: with this program; if not, write to the Free Software Foundation, Inc.,
020: 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
021: package com.db4o.test.legacy.soda;
022:
023: import com.db4o.query.*;
024: import com.db4o.test.*;
025:
026: public class SodaNumberCoercion {
027: private static final String DOUBLEFIELD = "_doubleValue";
028: private static final String FLOATFIELD = "_floatValue";
029: private static final String LONGFIELD = "_longValue";
030: private static final String INTFIELD = "_intValue";
031: private static final String SHORTFIELD = "_shortValue";
032: private static final String BYTEFIELD = "_byteValue";
033: private static final Float ROUNDFLOATVALUE = new Float(100);
034: private static final Double ROUNDDOUBLEVALUE = new Double(100);
035: private static final Long LONGVALUE = new Long(100);
036: private static final Integer INTVALUE = new Integer(100);
037: private static final Short SHORTVALUE = new Short((short) 100);
038: private static final Byte BYTEVALUE = new Byte((byte) 100);
039:
040: public static class Thing {
041: public byte _byteValue;
042: public short _shortValue;
043: public int _intValue;
044: public long _longValue;
045: public float _floatValue;
046: public double _doubleValue;
047:
048: public Thing(byte byteValue, short shortValue, int intValue,
049: long longValue, float floatValue, double doubleValue) {
050: _byteValue = byteValue;
051: _shortValue = shortValue;
052: _intValue = intValue;
053: _longValue = longValue;
054: _floatValue = floatValue;
055: _doubleValue = doubleValue;
056: }
057: }
058:
059: public void store() {
060: Test.deleteAllInstances(Thing.class);
061: Test.store(new Thing((byte) 10, (short) 10, 10, 10L, 10f, 10d));
062: Test.store(new Thing((byte) 100, (short) 100, 100, 100L, 100f,
063: 100d));
064: Test.store(new Thing((byte) 42, (short) 42, 42, 42L, 42f, 42d));
065: Test.store(new Thing((byte) 111, (short) 111, 111, 111L,
066: 111.11f, 111.11d));
067: }
068:
069: public void testIntegerTypes() {
070: assertSingleCoercionResult(BYTEFIELD, SHORTVALUE);
071: assertSingleCoercionResult(BYTEFIELD, INTVALUE);
072: assertSingleCoercionResult(BYTEFIELD, LONGVALUE);
073:
074: assertSingleCoercionResult(SHORTFIELD, BYTEVALUE);
075: assertSingleCoercionResult(SHORTFIELD, INTVALUE);
076: assertSingleCoercionResult(SHORTFIELD, LONGVALUE);
077:
078: assertSingleCoercionResult(INTFIELD, BYTEVALUE);
079: assertSingleCoercionResult(INTFIELD, SHORTVALUE);
080: assertSingleCoercionResult(INTFIELD, LONGVALUE);
081:
082: assertSingleCoercionResult(LONGFIELD, BYTEVALUE);
083: assertSingleCoercionResult(LONGFIELD, SHORTVALUE);
084: assertSingleCoercionResult(LONGFIELD, INTVALUE);
085: }
086:
087: public void testFloatingPointTypes() {
088: assertSingleCoercionResult(FLOATFIELD, ROUNDDOUBLEVALUE);
089: assertSingleCoercionResult(DOUBLEFIELD, ROUNDFLOATVALUE);
090: }
091:
092: public void testMixed() {
093: assertSingleCoercionResult(BYTEFIELD, ROUNDFLOATVALUE);
094: assertSingleCoercionResult(BYTEFIELD, ROUNDDOUBLEVALUE);
095: assertSingleCoercionResult(SHORTFIELD, ROUNDFLOATVALUE);
096: assertSingleCoercionResult(SHORTFIELD, ROUNDDOUBLEVALUE);
097: assertSingleCoercionResult(INTFIELD, ROUNDFLOATVALUE);
098: assertSingleCoercionResult(INTFIELD, ROUNDDOUBLEVALUE);
099: assertSingleCoercionResult(LONGFIELD, ROUNDFLOATVALUE);
100: assertSingleCoercionResult(LONGFIELD, ROUNDDOUBLEVALUE);
101: assertSingleCoercionResult(FLOATFIELD, BYTEVALUE);
102: assertSingleCoercionResult(FLOATFIELD, SHORTVALUE);
103: assertSingleCoercionResult(FLOATFIELD, INTVALUE);
104: assertSingleCoercionResult(FLOATFIELD, LONGVALUE);
105: assertSingleCoercionResult(DOUBLEFIELD, BYTEVALUE);
106: assertSingleCoercionResult(DOUBLEFIELD, SHORTVALUE);
107: assertSingleCoercionResult(DOUBLEFIELD, INTVALUE);
108: assertSingleCoercionResult(DOUBLEFIELD, LONGVALUE);
109: }
110:
111: public void testRounding() {
112: assertCoercionResult(FLOATFIELD, new Double(111.11), 0); // 111.11f!=111.11d
113: assertCoercionResult(FLOATFIELD, new Integer(111), 0);
114: assertCoercionResult(INTFIELD, new Float(111.11), 0);
115: }
116:
117: public void testOverflow() {
118: long cmpval = ((long) Integer.MAX_VALUE) - Integer.MIN_VALUE
119: + 1 + INTVALUE.intValue();
120: assertCoercionResult(INTFIELD, new Long(cmpval), 0);
121: }
122:
123: private void assertSingleCoercionResult(String fieldName,
124: Number value) {
125: assertCoercionResult(fieldName, value, 1);
126: }
127:
128: private void assertCoercionResult(String fieldName, Number value,
129: int expCount) {
130: Query q = Test.query();
131: q.constrain(Thing.class);
132: Constraint constr = q.descend(fieldName).constrain(value);
133: Test.ensure(constr != null);
134: Test.ensureEquals(expCount, q.execute().size());
135: }
136: }
|