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.common.handlers;
022:
023: import com.db4o.internal.Buffer;
024: import com.db4o.internal.Indexable4;
025: import com.db4o.internal.handlers.DoubleHandler;
026:
027: import db4ounit.Assert;
028:
029: /**
030: * @exclude
031: */
032: public class DoubleHandlerTestCase extends TypeHandlerTestCaseBase {
033:
034: private Indexable4 _handler;
035:
036: public static void main(String[] args) {
037: new DoubleHandlerTestCase().runSolo();
038: }
039:
040: protected void db4oSetupBeforeStore() throws Exception {
041: _handler = new DoubleHandler(stream());
042: }
043:
044: public void testMarshalling() {
045: final Double expected = new Double(1.1);
046:
047: Buffer buffer = new Buffer(_handler.linkLength());
048: _handler.writeIndexEntry(buffer, expected);
049:
050: buffer.seek(0);
051: final Object actual = _handler.readIndexEntry(buffer);
052: Assert.areEqual(expected, actual);
053: }
054:
055: public void testComparison() {
056: assertComparison(0, 1.1, 1.1);
057: assertComparison(1, 1.0, 1.1);
058: assertComparison(-1, 1.1, 0.5);
059: }
060:
061: private void assertComparison(final int expected,
062: final double prepareWith, final double compareTo) {
063: _handler.prepareComparison(new Double(prepareWith));
064: final Double doubleCompareTo = new Double(compareTo);
065: Assert.areEqual(expected, _handler.compareTo(doubleCompareTo));
066: }
067:
068: public void testReadWrite() {
069: MockWriteContext writeContext = new MockWriteContext(db());
070: DoubleHandler doubleHandler = (DoubleHandler) _handler;
071: Double expected = new Double(1.23456789);
072: doubleHandler.write(writeContext, expected);
073:
074: MockReadContext readContext = new MockReadContext(writeContext);
075: Double d = (Double) doubleHandler.read(readContext);
076:
077: Assert.areEqual(expected, d);
078: }
079:
080: public void testStoreObject() {
081: Item storedItem = new Item(1.023456789, new Double(1.023456789));
082: doTestStoreObject(storedItem);
083: }
084:
085: public static class Item {
086: public double _double;
087: public Double _doubleWrapper;
088:
089: public Item(double d, Double wrapper) {
090: _double = d;
091: _doubleWrapper = wrapper;
092: }
093:
094: public boolean equals(Object obj) {
095: if (obj == this ) {
096: return true;
097: }
098: if (!(obj instanceof Item)) {
099: return false;
100: }
101: Item other = (Item) obj;
102: return (other._double == this ._double)
103: && this ._doubleWrapper.equals(other._doubleWrapper);
104: }
105:
106: public String toString() {
107: return "[" + _double + "," + _doubleWrapper + "]";
108: }
109: }
110: }
|