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.db4ounit.util.*;
024:
025: import db4ounit.*;
026:
027: public class DoubleHandlerUpdateTestCase extends
028: HandlerUpdateTestCaseBase {
029:
030: private static final double[] data = new double[] {
031: Double.MIN_VALUE, Double.MIN_VALUE + 1, -3.1415926535789,
032: -1, 0, Double.NaN, Double.NEGATIVE_INFINITY,
033: Double.POSITIVE_INFINITY, 1, 3.1415926535789,
034: Double.MAX_VALUE - 1, Double.MAX_VALUE, };
035:
036: public static class Item {
037:
038: public double _typedPrimitive;
039:
040: public Double _typedWrapper;
041:
042: public Object _untyped;
043: }
044:
045: public static class ItemArrays {
046:
047: public double[] _typedPrimitiveArray;
048:
049: public Double[] _typedWrapperArray;
050:
051: public Object[] _untypedObjectArray;
052:
053: public Object _primitiveArrayInObject;
054:
055: public Object _wrapperArrayInObject;
056:
057: }
058:
059: protected void assertArrays(Object obj) {
060: ItemArrays item = (ItemArrays) obj;
061: assertTypedPrimitiveArray(item);
062: assertTypedWrapperArray(item);
063:
064: // Will be removed for .NET by sharpen.
065: assertUntypedObjectArray(item);
066:
067: assertPrimitiveArrayInObject(item);
068: assertWrapperArrayInObject(item);
069:
070: }
071:
072: private void assertTypedPrimitiveArray(ItemArrays item) {
073: assertData(item._typedPrimitiveArray);
074: }
075:
076: private void assertTypedWrapperArray(ItemArrays item) {
077: assertWrapperData(item._typedWrapperArray);
078: }
079:
080: /**
081: * @sharpen.remove
082: */
083: protected void assertUntypedObjectArray(ItemArrays item) {
084: for (int i = 0; i < data.length; i++) {
085: assertAreEqual(new Double(data[i]),
086: item._untypedObjectArray[i]);
087: }
088: Assert
089: .isNull(item._untypedObjectArray[item._untypedObjectArray.length - 1]);
090: }
091:
092: private void assertPrimitiveArrayInObject(ItemArrays item) {
093: if (_db4oHeaderVersion == VersionServices.HEADER_30_40) {
094: // Bug in the oldest format: It accidentally double[] arrays to Double[] arrays.
095: assertWrapperData((Double[]) item._primitiveArrayInObject);
096: } else {
097: assertData((double[]) item._primitiveArrayInObject);
098: }
099: }
100:
101: private void assertWrapperArrayInObject(ItemArrays item) {
102: assertWrapperData((Double[]) item._wrapperArrayInObject);
103: }
104:
105: private void assertData(double[] values) {
106: for (int i = 0; i < data.length; i++) {
107: assertAreEqual(data[i], values[i]);
108: }
109: }
110:
111: private void assertWrapperData(Double[] values) {
112: for (int i = 0; i < data.length; i++) {
113: assertAreEqual(new Double(data[i]), values[i]);
114: }
115:
116: // FIXME: The following fails as is because of a deficiency
117: // in the storage format of arrays.
118:
119: // Arrays should also get a null Bitmap to fix.
120:
121: // Assert.isNull(values[values.length - 1]);
122: }
123:
124: protected void assertValues(Object[] values) {
125: for (int i = 0; i < data.length; i++) {
126: Item item = (Item) values[i];
127: assertAreEqual(data[i], item._typedPrimitive);
128: assertAreEqual(new Double(data[i]), item._typedWrapper);
129: assertAreEqual(new Double(data[i]), item._untyped);
130: }
131: Item nullItem = (Item) values[values.length - 1];
132: Assert.areEqual(0, nullItem._typedPrimitive);
133: assertPrimitiveWrapperIsNullJavaOnly(nullItem._typedWrapper);
134: Assert.isNull(nullItem._untyped);
135:
136: }
137:
138: protected Object[] createValues() {
139: Item[] values = new Item[data.length + 1];
140: for (int i = 0; i < data.length; i++) {
141: Item item = new Item();
142: values[i] = item;
143: item._typedPrimitive = data[i];
144: item._typedWrapper = new Double(data[i]);
145: item._untyped = new Double(data[i]);
146: }
147: values[values.length - 1] = new Item();
148: return values;
149: }
150:
151: protected Object createArrays() {
152: ItemArrays item = new ItemArrays();
153: createTypedPrimitiveArray(item);
154: createTypedWrapperArray(item);
155:
156: // Will be removed for .NET by sharpen.
157: createUntypedObjectArray(item);
158:
159: createPrimitiveArrayInObject(item);
160: createWrapperArrayInObject(item);
161: return item;
162: }
163:
164: /**
165: * @sharpen.remove
166: */
167: private void createUntypedObjectArray(ItemArrays item) {
168: item._untypedObjectArray = new Double[data.length + 1];
169: for (int i = 0; i < data.length; i++) {
170: item._untypedObjectArray[i] = new Double(data[i]);
171: }
172: }
173:
174: private void createTypedPrimitiveArray(ItemArrays item) {
175: item._typedPrimitiveArray = new double[data.length];
176: System.arraycopy(data, 0, item._typedPrimitiveArray, 0,
177: data.length);
178: }
179:
180: private void createTypedWrapperArray(ItemArrays item) {
181: item._typedWrapperArray = new Double[data.length + 1];
182: for (int i = 0; i < data.length; i++) {
183: item._typedWrapperArray[i] = new Double(data[i]);
184: }
185: }
186:
187: private void createPrimitiveArrayInObject(ItemArrays item) {
188: double[] arr = new double[data.length];
189: System.arraycopy(data, 0, arr, 0, data.length);
190: item._primitiveArrayInObject = arr;
191: }
192:
193: private void createWrapperArrayInObject(ItemArrays item) {
194: Double[] arr = new Double[data.length + 1];
195: for (int i = 0; i < data.length; i++) {
196: arr[i] = new Double(data[i]);
197: }
198: item._wrapperArrayInObject = arr;
199: }
200:
201: protected String typeName() {
202: return "double";
203: }
204:
205: private void assertAreEqual(double expected, double actual) {
206: if (Double.isNaN(expected) && _handlerVersion == 0) {
207: expected = 0;
208: }
209: if (Double.isNaN(expected) && Double.isNaN(actual)) {
210: return;
211: }
212: Assert.areEqual(expected, actual);
213: }
214:
215: private void assertAreEqual(Object expected, Object actual) {
216: if (((Double) expected).isNaN() && _handlerVersion == 0) {
217: expected = null;
218: }
219: if (expected != null && actual != null
220: && ((Double) expected).isNaN()
221: && ((Double) actual).isNaN()) {
222: return;
223: }
224: Assert.areEqual(expected, actual);
225: }
226:
227: }
|