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 FloatHandlerUpdateTestCase extends
028: HandlerUpdateTestCaseBase {
029:
030: public static class Item {
031: public float _typedPrimitive;
032:
033: public Float _typedWrapper;
034:
035: public Object _untyped;
036: }
037:
038: public static class ItemArrays {
039: public float[] _typedPrimitiveArray;
040:
041: public Float[] _typedWrapperArray;
042:
043: public Object[] _untypedObjectArray;
044:
045: public Object _primitiveArrayInObject;
046:
047: public Object _wrapperArrayInObject;
048: }
049:
050: private static final float[] data = new float[] {
051: Float.NEGATIVE_INFINITY, Float.MIN_VALUE,
052: Float.MIN_VALUE + 1, -5, -1, 0, 1, 5, Float.MAX_VALUE - 1,
053: Float.MAX_VALUE, Float.POSITIVE_INFINITY, Float.NaN, };
054:
055: public static void main(String[] args) {
056: new TestRunner(FloatHandlerUpdateTestCase.class).run();
057: }
058:
059: protected void assertArrays(Object obj) {
060: ItemArrays itemArrays = (ItemArrays) obj;
061:
062: assertPrimitiveArray(itemArrays._typedPrimitiveArray);
063: if (_db4oHeaderVersion == VersionServices.HEADER_30_40) {
064: // Bug in the oldest format: It accidentally float[] arrays to Float[] arrays.
065: assertWrapperArray((Float[]) itemArrays._primitiveArrayInObject);
066: } else {
067: assertPrimitiveArray((float[]) itemArrays._primitiveArrayInObject);
068: }
069: assertWrapperArray(itemArrays._typedWrapperArray);
070: assertUntypedObjectArray(itemArrays);
071: assertWrapperArray((Float[]) itemArrays._wrapperArrayInObject);
072: }
073:
074: /**
075: * @sharpen.remove Cannot convert 'object[]' to 'Float[]' in .net
076: */
077: private void assertUntypedObjectArray(ItemArrays itemArrays) {
078: assertWrapperArray((Float[]) itemArrays._untypedObjectArray);
079: }
080:
081: private void assertPrimitiveArray(float[] primitiveArray) {
082: for (int i = 0; i < data.length; i++) {
083: assertAreEqual(data[i], primitiveArray[i]);
084: }
085: }
086:
087: private void assertWrapperArray(Float[] wrapperArray) {
088: for (int i = 0; i < data.length; i++) {
089: assertAreEqual(new Float(data[i]), wrapperArray[i]);
090: }
091: //FIXME: Arrays should also get a null Bitmap to fix.
092: //Assert.isNull(wrapperArray[wrapperArray.length - 1]);
093: }
094:
095: protected void assertValues(Object[] values) {
096: for (int i = 0; i < data.length; i++) {
097: Item item = (Item) values[i];
098: assertAreEqual(data[i], item._typedPrimitive);
099: assertAreEqual(new Float(data[i]), item._typedWrapper);
100: assertAreEqual(new Float(data[i]), item._untyped);
101: }
102:
103: Item nullItem = (Item) values[values.length - 1];
104: assertAreEqual((float) 0, nullItem._typedPrimitive);
105: assertPrimitiveWrapperIsNullJavaOnly(nullItem._typedWrapper);
106: Assert.isNull(nullItem._untyped);
107: }
108:
109: private void assertAreEqual(float expected, float actual) {
110: if (Float.isNaN(expected) && _handlerVersion == 0) {
111: expected = 0;
112: }
113: if (Float.isNaN(expected) && Float.isNaN(actual)) {
114: return;
115: }
116: Assert.areEqual(expected, actual);
117: }
118:
119: private void assertAreEqual(Object expected, Object actual) {
120: if (((Float) expected).isNaN() && _handlerVersion == 0) {
121: expected = null;
122: }
123: Assert.areEqual(expected, actual);
124: }
125:
126: protected Object createArrays() {
127: ItemArrays itemArrays = new ItemArrays();
128: itemArrays._typedPrimitiveArray = new float[data.length];
129: System.arraycopy(data, 0, itemArrays._typedPrimitiveArray, 0,
130: data.length);
131:
132: Float[] dataWrapper = new Float[data.length];
133: for (int i = 0; i < data.length; i++) {
134: dataWrapper[i] = new Float(data[i]);
135: }
136:
137: itemArrays._typedWrapperArray = new Float[data.length + 1];
138: System.arraycopy(dataWrapper, 0, itemArrays._typedWrapperArray,
139: 0, dataWrapper.length);
140:
141: initializeUntypedObjectArray(itemArrays, dataWrapper);
142:
143: float[] primitiveArray = new float[data.length];
144: System.arraycopy(data, 0, primitiveArray, 0, data.length);
145: itemArrays._primitiveArrayInObject = primitiveArray;
146:
147: Float[] wrapperArray = new Float[data.length + 1];
148: System.arraycopy(dataWrapper, 0, wrapperArray, 0,
149: dataWrapper.length);
150: itemArrays._wrapperArrayInObject = wrapperArray;
151: return itemArrays;
152: }
153:
154: /**
155: * @sharpen.remove Cannot convert 'Float[]' to 'object[]'
156: */
157: private void initializeUntypedObjectArray(ItemArrays itemArrays,
158: Float[] dataWrapper) {
159: itemArrays._untypedObjectArray = new Float[data.length + 1];
160: System.arraycopy(dataWrapper, 0,
161: itemArrays._untypedObjectArray, 0, dataWrapper.length);
162: }
163:
164: protected Object[] createValues() {
165: Item[] values = new Item[data.length + 1];
166: for (int i = 0; i < data.length; i++) {
167: Item item = new Item();
168: item._typedPrimitive = data[i];
169: item._typedWrapper = new Float(data[i]);
170: item._untyped = new Float(data[i]);
171: values[i] = item;
172: }
173: values[values.length - 1] = new Item();
174: return values;
175: }
176:
177: protected String typeName() {
178: return "float";
179: }
180:
181: }
|