comparison src/test/java/jp/ac/u_ryukyu/ie/cr/jungle/core/treeeditor/TreeNodeChildrenTest.java @ 0:44465893e8b8

first Commit
author Kazuma
date Wed, 30 Nov 2016 01:47:55 +0900
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:44465893e8b8
1 /*package jp.ac.u_ryukyu.ie.cr.shoshi.jungle.core.treeeditor;
2
3 import java.nio.ByteBuffer;
4 import org.junit.Assert;
5 import fj.data.List;
6 import TreeNode;
7 import TreeNodeChildren;
8 import Either;
9 import Error;
10 import junit.framework.TestCase;
11
12 public abstract class TreeNodeChildrenTest<T extends TreeNode<T>> extends TestCase
13 {
14 public abstract TreeNode<T> instance();
15
16 public void testAddNewChildAtWithNode()
17 {
18 int count = 5;
19 for(Integer pos : List.range(0,5)){
20 _testAddNewChildAtWithNode(count,pos);
21 }
22 }
23
24 public void _testAddNewChildAtWithNode(int _count,int _pos)
25 {
26 TreeNode<T> instance = instance();
27
28 Either<Error, T> either;
29 for(int i = 0;i < _count;i ++){
30 either = instance.getChildren().addNewChildAt(0);
31 if(either.isA()){
32 Assert.fail();
33 }
34 instance = either.b();
35 }
36
37 TreeNode<T> newNode = instance.createNewNode();
38 String key = "KEY";
39 ByteBuffer value = ByteBuffer.wrap("VALUE".getBytes());
40
41 either = newNode.getAttributes().put(key,value);
42 if(either.isA()){
43 Assert.fail();
44 }
45 newNode = either.b();
46
47 either = instance.getChildren().addNewChildAt(_pos,newNode);
48 if(either.isA()){
49 Assert.fail();
50 }
51 instance = either.b();
52
53 // check
54 either = instance.getChildren().at(_pos);
55 if(either.isA()){
56 Assert.fail();
57 }
58
59 T checkTarget = either.b();
60 ByteBuffer actual = checkTarget.getAttributes().get(key);
61 Assert.assertEquals(0,value.compareTo(actual));
62 }
63
64 public void testReplaceAt()
65 {
66 int count = 5;
67 for(Integer pos : List.range(0,count)){
68 _testReplaceAt(count,pos);
69 }
70 }
71
72 public void _testReplaceAt(int _count,int _pos)
73 {
74 TreeNode<T> instance = instance();
75 String key = "KEY";
76 ByteBuffer value = ByteBuffer.wrap("VALUE".getBytes());
77
78 // prepare
79
80 for(int i = 0;i < _count;i ++){
81 TreeNode<T> newNode = instance.createNewNode();
82 Either<Error,T> either = newNode.getAttributes().put(key,value);
83 if(either.isA()){
84 Assert.fail("failed to put attributes to child");
85 }
86
87 newNode = either.b();
88 either = instance.getChildren().addNewChildAt(0,newNode);
89 if(either.isA()){
90 Assert.fail("failed to add child to instance");
91 }
92
93 instance = either.b();
94 }
95
96 int size = instance.getChildren().size();
97 Assert.assertEquals(_count,size);
98
99 // create node for replacement.
100
101 ByteBuffer replaceNodeValue = ByteBuffer.wrap("EULAV".getBytes());
102 TreeNode<T> replacement = instance.createNewNode();
103 Either<Error,T> either = replacement.getAttributes().put(key,replaceNodeValue);
104 if(either.isA()){
105 Assert.fail("failed to create replacement node");
106 }
107 replacement = either.b();
108
109 // replace
110
111 either = instance.getChildren().replaceNode(_pos,replacement);
112 if(either.isA()){
113 Assert.fail("failed to replace node.");
114 }
115 instance = either.b();
116
117 TreeNodeChildren<T> children = instance.getChildren();
118 for(Integer pos : List.range(0,_count)){
119 either = children.at(pos.intValue());
120 if(either.isA()){
121 Assert.fail("failed to get node.");
122 }
123
124 T ch = either.b();
125 ByteBuffer expect = (_pos != pos) ? value : replaceNodeValue;
126 ByteBuffer actual = ch.getAttributes().get(key);
127
128 Assert.assertEquals(0,expect.compareTo(actual));
129 }
130 }
131 }*/