0
|
1 package jp.ac.u_ryukyu.ie.cr.jungle.traverse;
|
|
2
|
|
3
|
|
4 import jp.ac.u_ryukyu.ie.cr.jungle.data.list.List;
|
|
5 import jp.ac.u_ryukyu.ie.cr.jungle.store.NodePath;
|
|
6 import jp.ac.u_ryukyu.ie.cr.jungle.store.impl.DefaultNodePath;
|
|
7 import jp.ac.u_ryukyu.ie.cr.jungle.store.impl.TreeNode;
|
|
8 import jp.ac.u_ryukyu.ie.cr.jungle.transaction.DefaultTreeNode;
|
|
9 import jp.ac.u_ryukyu.ie.cr.jungle.traverser.*;
|
|
10 import jp.ac.u_ryukyu.ie.cr.jungle.util.Either;
|
|
11 import jp.ac.u_ryukyu.ie.cr.jungle.util.Error;
|
|
12 import junit.framework.Assert;
|
|
13 import junit.framework.TestCase;
|
|
14
|
|
15 import java.nio.ByteBuffer;
|
|
16
|
|
17 public class TraverserTest extends TestCase
|
|
18 {
|
|
19
|
|
20 public void testTraverse()
|
|
21 {
|
|
22 int maxHeight = 3;
|
|
23
|
|
24 TreeNode root = createTree(0,maxHeight,new DefaultNodePath());
|
|
25 Traverser traverser = new DefaultTraverser();
|
|
26
|
|
27 List<DefaultNodePath> paths = generatePathPattern(new DefaultNodePath(),0,maxHeight);
|
|
28 paths = paths.addLast(new DefaultNodePath());
|
|
29
|
|
30 for(DefaultNodePath path : paths){
|
|
31 DefaultEvaluator evaluator = new DefaultEvaluator(path);
|
|
32 Either<Error,Traversal> ret = traverser.traverse(root,evaluator);
|
|
33 if(ret.isA()){
|
|
34 Assert.fail();
|
|
35 }
|
|
36
|
|
37 Traversal traversal = ret.b();
|
|
38 TreeNode target = traversal.destination();
|
|
39 String expect = path.toString();
|
|
40 ByteBuffer value = target.getAttributes().get(key);
|
|
41 String actual = new String(value.array());
|
|
42 Assert.assertEquals(expect,actual);
|
|
43
|
|
44 List<DefaultNodePath> parts = path.inits();
|
|
45
|
|
46 for(Direction<TreeNode> d : traversal){
|
|
47 DefaultNodePath part = parts.tail();
|
|
48 parts = parts.delete(parts.length() - 1);
|
|
49 value = d.getTarget().getAttributes().get(key);
|
|
50 String actualCurrentPathStr = new String(value.array());
|
|
51 String expectCurrentPathStr = part.toString();
|
|
52 Assert.assertEquals(expectCurrentPathStr,actualCurrentPathStr);
|
|
53 }
|
|
54 }
|
|
55 }
|
|
56
|
|
57 public List<DefaultNodePath> generatePathPattern(DefaultNodePath _cur,int _curHeight,int _maxHeight)
|
|
58 {
|
|
59 List<DefaultNodePath> paths = new List();
|
|
60 for(int p = 0;p <= _curHeight;p ++){
|
|
61 DefaultNodePath path = _cur.add(p);
|
|
62 if(_curHeight != _maxHeight - 1){
|
|
63 List<DefaultNodePath> newpaths = generatePathPattern(path,_curHeight+1,_maxHeight);
|
|
64 paths = paths.append(newpaths);
|
|
65 }
|
|
66 paths = paths.addLast(path);
|
|
67 }
|
|
68
|
|
69 return paths;
|
|
70 }
|
|
71
|
|
72 public static String key = "KEY";
|
|
73 public static DefaultTreeNode factory = new DefaultTreeNode();
|
|
74
|
|
75 public TreeNode createTree(int _curY,int _maxHeight,NodePath _address)
|
|
76 {
|
|
77 TreeNode parent = factory.createNewNode();
|
|
78 Either<Error,TreeNode> either = parent.getAttributes().put(key,ByteBuffer.wrap(_address.toString().getBytes()));
|
|
79 if(either.isA()){
|
|
80 Assert.fail();
|
|
81 }
|
|
82 parent = either.b();
|
|
83
|
|
84 if(_curY == _maxHeight){
|
|
85 return parent;
|
|
86 }
|
|
87
|
|
88 for(int i = 0;i < _curY + 1;i ++){
|
|
89 TreeNode ch = createTree(_curY + 1,_maxHeight,_address.add(i));
|
|
90 either = parent.getChildren().addNewChildAt(i,ch);
|
|
91 if(either.isA()){
|
|
92 Assert.fail();
|
|
93 }
|
|
94
|
|
95 parent = either.b();
|
|
96 }
|
|
97
|
|
98 return parent;
|
|
99 }
|
|
100 }
|