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