# HG changeset patch # User Shoshi TAMAKI # Date 1359888364 -32400 # Node ID 449d67be088623013bd9fdf06470570bb0e306c1 # Parent 1c91c4357228c6d6aa370e58b5c849e8d26651d6 added tests diff -r 1c91c4357228 -r 449d67be0886 src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/store/impl/DefaultNodePath.java --- a/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/store/impl/DefaultNodePath.java Fri Feb 01 00:55:02 2013 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/store/impl/DefaultNodePath.java Sun Feb 03 19:46:04 2013 +0900 @@ -2,6 +2,7 @@ import java.util.Iterator; +import fj.F; import fj.data.List; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.NodePath; @@ -20,7 +21,7 @@ public DefaultNodePath() { - path = List.nil(); + path = List.list(-1); } private DefaultNodePath(List _path) @@ -38,7 +39,6 @@ public DefaultNodePath add(int _pos) { List newPath = path.snoc(_pos); - return new DefaultNodePath(newPath); } @@ -62,4 +62,22 @@ { return path.length(); } + + public List inits() + { + List> inits = path.inits(); + inits = inits.filter(new F,Boolean>(){ + @Override + public Boolean f(List _init){ + return _init.length() != 0; + } + }); + + return inits.map(new F,DefaultNodePath>(){ + @Override + public DefaultNodePath f(List _path){ + return new DefaultNodePath(_path); + } + }); + } } diff -r 1c91c4357228 -r 449d67be0886 src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/traverser/DefaultTraverser.java --- a/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/traverser/DefaultTraverser.java Fri Feb 01 00:55:02 2013 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/traverser/DefaultTraverser.java Sun Feb 03 19:46:04 2013 +0900 @@ -33,14 +33,17 @@ } }; - Children chs = _root.getChildren(); + //Children chs = _root.getChildren(); + Children chs = wrapper; - Either>> ret = _traverse(chs,_evaluator); + Either>> ret = _traverse(chs,_evaluator,-1); if(ret.isA()){ return DefaultEither.newA(ret.a()); } - final Iterable> iterable = ret.b(); + List> list = ret.b(); + + final Iterable> iterable = list; final T destination = ret.b().last().getTarget(); Traversal traversal = new Traversal(){ @@ -58,9 +61,9 @@ return DefaultEither.newB(traversal); } - private > Either>> _traverse(Children _chs,Evaluator _evaluator) + private > Either>> _traverse(Children _chs,Evaluator _evaluator,int _pos) { - int pos = 0; + int pos = _pos; for(T ch : _chs){ Evaluation e = _evaluator.evaluate(ch,pos); Result r = e.result(); @@ -111,7 +114,7 @@ private > Either>> _accept(final T _current,final int _pos,Evaluator _evaluator) { Children chs = _current.getChildren(); - Either>> either = _traverse(chs,_evaluator); + Either>> either = _traverse(chs,_evaluator,0); if(either.isA()){ return either; } diff -r 1c91c4357228 -r 449d67be0886 src/test/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/core/impl/treeeditor/DefaultTreeEditorTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/test/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/core/impl/treeeditor/DefaultTreeEditorTest.java Sun Feb 03 19:46:04 2013 +0900 @@ -0,0 +1,120 @@ +package jp.ac.u_ryukyu.ie.cr.shoshi.jungle.core.impl.treeeditor; + +import java.nio.ByteBuffer; + +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.DefaultNode; +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.DefaultNodePath; +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.DefaultTreeEditor; +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.trasnformer.AppendChildAt; +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.transaction.ClonableDefaultNode; +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.traverser.DefaultTraverser; +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Either; +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Error; +import junit.framework.Assert; +import junit.framework.TestCase; + +public class DefaultTreeEditorTest extends TestCase +{ + public DefaultTreeEditor instance() + { + DefaultTraverser traverser = new DefaultTraverser(); + ClonableDefaultNode root = new ClonableDefaultNode(new DefaultNode()); + return new DefaultTreeEditor(root,traverser); + } + + public void testEdittingDoesNotEffectToOtherTree() + { + + } + + public void testAppendChild() + { + DefaultTreeEditor instance = instance(); + DefaultNodePath path = new DefaultNodePath(); // root + + Either> either = instance.appendChild(path,0); + if(either.isA()){ + Assert.fail(); + } + instance = either.b(); + + ClonableDefaultNode node = instance.getRootNode(); + Assert.assertEquals(1,node.getChildren().size()); + } + + public void testDeleteChild() + { + DefaultTreeEditor instance = instance(); + DefaultNodePath path = new DefaultNodePath(); + + Either> either = instance.appendChild(path,0); + if(either.isA()){ + Assert.fail(); + } + instance = either.b(); + ClonableDefaultNode node = instance.getRootNode(); + Assert.assertEquals(1,node.getChildren().size()); + + either = instance.deleteChild(path,0); + if(either.isA()){ + Assert.fail(); + } + instance = either.b(); + node = instance.getRootNode(); + Assert.assertEquals(0,node.getChildren().size()); + } + + public DefaultTreeEditor testPutAttribute() + { + DefaultTreeEditor instance = instance(); + DefaultNodePath path = new DefaultNodePath(); + + String key = "KEY"; + ByteBuffer value = ByteBuffer.wrap(key.getBytes()); + + Either> either = instance.putAttribute(path,key,value); + if(either.isA()){ + Assert.fail(); + } + instance = either.b(); + + ClonableDefaultNode root = instance.getRootNode(); + ByteBuffer actual = root.getAttributes().get(key); + + Assert.assertEquals(0,actual.compareTo(value)); + + return instance; + } + + public void testDeleteAttribute() + { + DefaultTreeEditor instance = testPutAttribute(); + DefaultNodePath path = new DefaultNodePath(); + + String key = "KEY"; + + Either> either = instance.deleteAttribute(path, key); + if(either.isA()){ + Assert.fail(); + } + instance = either.b(); + + ByteBuffer actual = instance.getRootNode().getAttributes().get(key); + Assert.assertNull(actual); + } + + public void testEdit() + { + DefaultTreeEditor instance = instance(); + DefaultNodePath path = new DefaultNodePath(); + + Either> either = instance.edit(path,new AppendChildAt(0)); + if(either.isA()){ + Assert.fail(); + } + instance = either.b(); + + ClonableDefaultNode node = instance.getRootNode(); + Assert.assertEquals(1,node.getChildren().size()); + } +} diff -r 1c91c4357228 -r 449d67be0886 src/test/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/traverse/TraverserTest.java --- a/src/test/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/traverse/TraverserTest.java Fri Feb 01 00:55:02 2013 +0900 +++ b/src/test/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/traverse/TraverserTest.java Sun Feb 03 19:46:04 2013 +0900 @@ -33,6 +33,7 @@ // generate all pattern. List paths = generatePathPattern(new DefaultNodePath(),0,maxHeight); + paths = paths.cons(new DefaultNodePath()); for(DefaultNodePath path : paths){ DefaultEvaluator evaluator = new DefaultEvaluator(path); @@ -48,18 +49,14 @@ String actual = new String(value.array()); Assert.assertEquals(expect,actual); - Pair pop; - NodePath estimatedPath = path; - DefaultNodePath currentPath = new DefaultNodePath(); + List parts = path.inits(); for(Direction> d : traversal){ - pop = estimatedPath.pop(); - estimatedPath = pop.right(); - currentPath = currentPath.add(pop.left()); - + DefaultNodePath part = parts.head(); + parts = parts.tail(); value = d.getTarget().getWrapped().getAttributes().get(key); String actualCurrentPathStr = new String(value.array()); - String expectCurrentPathStr = currentPath.toString(); + String expectCurrentPathStr = part.toString(); Assert.assertEquals(expectCurrentPathStr,actualCurrentPathStr); } }