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.jre5.collections;
022:
023: import java.util.Collection;
024: import java.util.Map;
025: import java.util.Set;
026:
027: import com.db4o.collections.ArrayMap4;
028:
029: import db4ounit.Assert;
030:
031: public class ArrayMap4Asserter {
032:
033: public static int DATA_LENGTH = 10;
034:
035: private static int MULTIPLE = 100;
036:
037: public static void putData(Map<String, Integer> map) {
038: for (int i = 0; i < DATA_LENGTH; i++) {
039: map.put(String.valueOf(i), Integer.valueOf(i * MULTIPLE));
040: }
041: }
042:
043: public static void assertInitalStatus(Map<String, Integer> map) {
044: Assert.isNotNull(map);
045: Assert.areEqual(0, map.size());
046: Assert.isTrue(map.isEmpty());
047: }
048:
049: public static void assertClear(Map<String, Integer> map) {
050: Assert.areEqual(10, map.size());
051: Assert.isFalse(map.isEmpty());
052:
053: map.clear();
054:
055: checkClear(map);
056: }
057:
058: public static void checkClear(Map<String, Integer> map) {
059: Assert.areEqual(0, map.size());
060: Assert.isTrue(map.isEmpty());
061: }
062:
063: @SuppressWarnings("unchecked")
064: public static void assertClone(ArrayMap4<String, Integer> map) {
065: Assert.areEqual(DATA_LENGTH, map.size());
066: Assert.isFalse(map.isEmpty());
067:
068: ArrayMap4<String, Integer> clone = (ArrayMap4<String, Integer>) map
069: .clone();
070:
071: Assert.areEqual(DATA_LENGTH, clone.size());
072:
073: for (int i = 0; i < DATA_LENGTH; i++) {
074: Assert.areEqual(Integer.valueOf(i * MULTIPLE), clone
075: .get(String.valueOf(i)));
076: }
077: }
078:
079: public static void assertContainsKey(ArrayMap4<String, Integer> map) {
080: for (int i = 0; i < DATA_LENGTH; i++) {
081: Assert.isTrue(map.containsKey(String.valueOf(i)));
082: }
083:
084: Assert.isFalse(map.containsKey(String.valueOf(DATA_LENGTH)));
085: }
086:
087: public static void assertContainsValue(
088: ArrayMap4<String, Integer> map) {
089: for (int i = 0; i < DATA_LENGTH; i++) {
090: Assert.isTrue(map.containsValue(Integer.valueOf(i
091: * MULTIPLE)));
092: }
093:
094: Assert.isFalse(map.containsValue(Integer.valueOf(DATA_LENGTH)));
095: }
096:
097: public static void assertEntrySet(ArrayMap4<String, Integer> map) {
098: Set<Map.Entry<String, Integer>> set = map.entrySet();
099: Assert.areEqual(DATA_LENGTH, set.size());
100:
101: for (int i = 0; i < DATA_LENGTH; i++) {
102: ArrayMap4.MapEntry4<String, Integer> entry = new ArrayMap4.MapEntry4<String, Integer>(
103: String.valueOf(i), Integer.valueOf(i * MULTIPLE));
104: Assert.isTrue(set.contains(entry));
105: }
106: }
107:
108: public static void assertGet(ArrayMap4<String, Integer> map) {
109: for (int i = 0; i < DATA_LENGTH; i++) {
110: Integer value = map.get(String.valueOf(i));
111: Assert.areEqual(Integer.valueOf(i * MULTIPLE), value);
112: }
113: }
114:
115: public static void assertIsEmpty(ArrayMap4<String, Integer> map) {
116: Assert.isFalse(map.isEmpty());
117: map.clear();
118: Assert.isTrue(map.isEmpty());
119: }
120:
121: public static void assertKeySet(ArrayMap4<String, Integer> map) {
122: Set<String> set = map.keySet();
123: Assert.areEqual(DATA_LENGTH, set.size());
124: for (int i = 0; i < DATA_LENGTH; i++) {
125: set.contains(String.valueOf(i));
126: }
127: }
128:
129: public static void assertPut(ArrayMap4<String, Integer> map) {
130: map.put("one", Integer.valueOf(1));
131: map.put("two", Integer.valueOf(2));
132: map.put("three", Integer.valueOf(3));
133: Assert.areEqual(13, map.size());
134: Assert.areEqual(Integer.valueOf(1), map.get("one"));
135: Assert.areEqual(Integer.valueOf(2), map.get("two"));
136: Assert.areEqual(Integer.valueOf(3), map.get("three"));
137:
138: map.put("two", Integer.valueOf(-2));
139: checkPut(map);
140: }
141:
142: public static void checkPut(ArrayMap4<String, Integer> map) {
143: Assert.areEqual(Integer.valueOf(-2), map.get("two"));
144: }
145:
146: public static void assertPutAll(ArrayMap4<String, Integer> map) {
147: ArrayMap4<String, Integer> other = new ArrayMap4<String, Integer>();
148: for (int i = DATA_LENGTH; i < DATA_LENGTH * 2; i++) {
149: other.put(String.valueOf(i), Integer.valueOf(i * MULTIPLE));
150: }
151:
152: map.putAll(other);
153:
154: checkMap(map, 0, DATA_LENGTH * 2);
155: }
156:
157: public static void checkMap(ArrayMap4<String, Integer> map,
158: int start, int end) {
159: Assert.areEqual(end - start, map.size());
160: for (int i = start; i < end; i++) {
161: Assert.areEqual(Integer.valueOf(i * MULTIPLE), map
162: .get(String.valueOf(i)));
163: }
164: }
165:
166: public static void assertRemove_FromHead(
167: ArrayMap4<String, Integer> map) {
168: Integer value = map.remove("0");
169: Assert.areEqual(Integer.valueOf(0), value);
170:
171: checkRemove(map, 1, DATA_LENGTH, "0");
172: }
173:
174: public static void checkRemove(ArrayMap4<String, Integer> map,
175: int start, int end, String removedKey) {
176: checkMap(map, start, end);
177: Assert.isNull(map.get(removedKey));
178: }
179:
180: public static void assertRemove_FromEnd(
181: ArrayMap4<String, Integer> map) {
182: Integer value = map.remove("9");
183: Assert.areEqual(Integer.valueOf(900), value);
184:
185: checkRemove(map, 0, 9, "9");
186: }
187:
188: public static void assertRemove_FromMiddle(
189: ArrayMap4<String, Integer> map) {
190: Integer value = map.remove("5");
191: Assert.areEqual(Integer.valueOf(500), value);
192:
193: checkRemove_FromMiddle(map);
194: }
195:
196: public static void checkRemove_FromMiddle(
197: ArrayMap4<String, Integer> map) {
198: Assert.areEqual(9, map.size());
199: for (int i = 0; i < 5; i++) {
200: Assert.areEqual(Integer.valueOf(i * 100), map.get(String
201: .valueOf(i)));
202: }
203: Assert.isNull(map.get("5"));
204:
205: for (int i = 6; i < 9; i++) {
206: Assert.areEqual(Integer.valueOf(i * 100), map.get(String
207: .valueOf(i)));
208: }
209: }
210:
211: public static void assertSize(ArrayMap4<String, Integer> map) {
212: Assert.areEqual(DATA_LENGTH, map.size());
213: map.remove("1");
214: Assert.areEqual(9, map.size());
215: map.put("x", Integer.valueOf(1234));
216: Assert.areEqual(DATA_LENGTH, map.size());
217: }
218:
219: public static void assertValues(ArrayMap4<String, Integer> map) {
220: Collection<Integer> values = map.values();
221: Assert.areEqual(10, values.size());
222: for (int i = 0; i < DATA_LENGTH; i++) {
223: Assert.isTrue(values
224: .contains(Integer.valueOf(i * MULTIPLE)));
225: }
226: }
227:
228: public static void assertEquals(ArrayMap4<String, Integer> map) {
229: ArrayMap4<String, Integer> other = new ArrayMap4<String, Integer>();
230: for (int i = 0; i < DATA_LENGTH; i++) {
231: other.put(String.valueOf(i), Integer.valueOf(i * MULTIPLE));
232: }
233:
234: Assert.isTrue(map.equals(other));
235: Assert.isTrue(other.equals(map));
236: Assert.areEqual(map.hashCode(), other.hashCode());
237: Assert.isFalse(map.equals(null));
238:
239: other.remove("5");
240: Assert.isFalse(map.equals(other));
241: }
242:
243: public static void assertIncreaseSize(ArrayMap4<String, Integer> map) {
244: for (int i = DATA_LENGTH; i < DATA_LENGTH * 5; i++) {
245: map.put(String.valueOf(i), Integer.valueOf(i * MULTIPLE));
246: }
247:
248: checkMap(map, 0, DATA_LENGTH * 5);
249: }
250: }
|