01: /* Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com
02:
03: This file is part of the db4o open source object database.
04:
05: db4o is free software; you can redistribute it and/or modify it under
06: the terms of version 2 of the GNU General Public License as published
07: by the Free Software Foundation and as clarified by db4objects' GPL
08: interpretation policy, available at
09: http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
10: Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
11: Suite 350, San Mateo, CA 94403, USA.
12:
13: db4o is distributed in the hope that it will be useful, but WITHOUT ANY
14: WARRANTY; without even the implied warranty of MERCHANTABILITY or
15: FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16: for more details.
17:
18: You should have received a copy of the GNU General Public License along
19: with this program; if not, write to the Free Software Foundation, Inc.,
20: 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21: package com.db4o.db4ounit.common.foundation;
22:
23: import com.db4o.foundation.*;
24:
25: import db4ounit.*;
26:
27: /**
28: * @exclude
29: */
30: public class IteratorsTestCase implements TestCase {
31:
32: public void testFilter() {
33: assertFilter(new String[] { "bar", "baz" }, new String[] {
34: "foo", "bar", "baz", "zong" }, new Predicate4() {
35: public boolean match(Object candidate) {
36: return ((String) candidate).startsWith("b");
37: }
38: });
39:
40: assertFilter(new String[] { "foo", "bar" }, new String[] {
41: "foo", "bar" }, new Predicate4() {
42: public boolean match(Object candidate) {
43: return true;
44: }
45: });
46:
47: assertFilter(new String[0], new String[] { "foo", "bar" },
48: new Predicate4() {
49: public boolean match(Object candidate) {
50: return false;
51: }
52: });
53: }
54:
55: private void assertFilter(String[] expected, String[] actual,
56: Predicate4 filter) {
57: Iterator4Assert.areEqual(expected, Iterators.filter(actual,
58: filter));
59: }
60:
61: public void testMap() {
62: final int[] array = new int[] { 1, 2, 3 };
63: final Collection4 args = new Collection4();
64: final Iterator4 iterator = Iterators.map(IntArrays4
65: .newIterator(array), new Function4() {
66: public Object apply(Object arg) {
67: args.add(arg);
68: return new Integer(((Integer) arg).intValue() * 2);
69: }
70: });
71: Assert.isNotNull(iterator);
72: Assert.areEqual(0, args.size());
73:
74: for (int i = 0; i < array.length; ++i) {
75: Assert.isTrue(iterator.moveNext());
76: Assert.areEqual(i + 1, args.size());
77: Assert.areEqual(new Integer(array[i] * 2), iterator
78: .current());
79: }
80: }
81:
82: }
|