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.db4ounit.jre11.soda;
022:
023: import com.db4o.query.*;
024:
025: import db4ounit.*;
026: import db4ounit.extensions.*;
027:
028: public class SodaNumberCoercionTestCase extends AbstractDb4oTestCase {
029: private static final String DOUBLEFIELD = "_doubleValue";
030: private static final String FLOATFIELD = "_floatValue";
031: private static final String LONGFIELD = "_longValue";
032: private static final String INTFIELD = "_intValue";
033: private static final String SHORTFIELD = "_shortValue";
034: private static final String BYTEFIELD = "_byteValue";
035: private static final Float ROUNDFLOATVALUE = new Float(100);
036: private static final Double ROUNDDOUBLEVALUE = new Double(100);
037: private static final Long LONGVALUE = new Long(100);
038: private static final Integer INTVALUE = new Integer(100);
039: private static final Short SHORTVALUE = new Short((short) 100);
040: private static final Byte BYTEVALUE = new Byte((byte) 100);
041:
042: public static class Thing {
043: public byte _byteValue;
044: public short _shortValue;
045: public int _intValue;
046: public long _longValue;
047: public float _floatValue;
048: public double _doubleValue;
049:
050: public Thing(byte byteValue, short shortValue, int intValue,
051: long longValue, float floatValue, double doubleValue) {
052: _byteValue = byteValue;
053: _shortValue = shortValue;
054: _intValue = intValue;
055: _longValue = longValue;
056: _floatValue = floatValue;
057: _doubleValue = doubleValue;
058: }
059: }
060:
061: protected void store() {
062: store(new Thing((byte) 10, (short) 10, 10, 10L, 10f, 10d));
063: store(new Thing((byte) 100, (short) 100, 100, 100L, 100f, 100d));
064: store(new Thing((byte) 42, (short) 42, 42, 42L, 42f, 42d));
065: store(new Thing((byte) 111, (short) 111, 111, 111L, 111.11f,
066: 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 = newQuery(Thing.class);
131: Constraint constr = q.descend(fieldName).constrain(value);
132: Assert.isNotNull(constr);
133: Assert.areEqual(expCount, q.execute().size());
134: }
135: }
|