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.Iterator4;
24: import com.db4o.foundation.Tree;
25: import com.db4o.foundation.TreeNodeIterator;
26: import com.db4o.foundation.Visitor4;
27: import com.db4o.internal.TreeInt;
28:
29: import db4ounit.Assert;
30: import db4ounit.TestCase;
31: import db4ounit.TestRunner;
32:
33: public class TreeNodeIteratorTestCase implements TestCase {
34:
35: public static void main(String[] args) {
36: new TestRunner(TreeNodeIteratorTestCase.class).run();
37: }
38:
39: private static int[] VALUES = new int[] { 1, 3, 5, 7, 9, 10, 11,
40: 13, 24, 76 };
41:
42: public void testIterate() {
43: for (int i = 1; i <= VALUES.length; i++) {
44: assertIterateValues(VALUES, i);
45: }
46: }
47:
48: public void testMoveNextAfterCompletion() {
49: Iterator4 i = new TreeNodeIterator(createTree(VALUES));
50: while (i.moveNext()) {
51:
52: }
53: Assert.isFalse(i.moveNext());
54: }
55:
56: private void assertIterateValues(int[] values, int count) {
57: int[] testValues = new int[count];
58: System.arraycopy(values, 0, testValues, 0, count);
59: assertIterateValues(testValues);
60: }
61:
62: private void assertIterateValues(int[] values) {
63: Tree tree = createTree(VALUES);
64: final Iterator4 i = new TreeNodeIterator(tree);
65: tree.traverse(new Visitor4() {
66: public void visit(Object obj) {
67: i.moveNext();
68: Assert.areSame(obj, i.current());
69: }
70: });
71: }
72:
73: private Tree createTree(int[] values) {
74: Tree tree = new TreeInt(values[0]);
75: for (int i = 1; i < values.length; i++) {
76: tree = tree.add(new TreeInt(values[i]));
77: }
78: return tree;
79: }
80:
81: public void testEmpty() {
82: Iterator4 i = new TreeNodeIterator(null);
83: Assert.isFalse(i.moveNext());
84: }
85:
86: }
|