Mercurial > hg > Members > shoshi > jungle > jungle-core
changeset 44:449d67be0886
added tests
author | Shoshi TAMAKI |
---|---|
date | Sun, 03 Feb 2013 19:46:04 +0900 |
parents | 1c91c4357228 |
children | fb00d7b147c8 |
files | src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/store/impl/DefaultNodePath.java src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/traverser/DefaultTraverser.java src/test/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/core/impl/treeeditor/DefaultTreeEditorTest.java src/test/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/traverse/TraverserTest.java |
diffstat | 4 files changed, 154 insertions(+), 16 deletions(-) [+] |
line wrap: on
line diff
--- 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<Integer> _path) @@ -38,7 +39,6 @@ public DefaultNodePath add(int _pos) { List<Integer> newPath = path.snoc(_pos); - return new DefaultNodePath(newPath); } @@ -62,4 +62,22 @@ { return path.length(); } + + public List<DefaultNodePath> inits() + { + List<List<Integer>> inits = path.inits(); + inits = inits.filter(new F<List<Integer>,Boolean>(){ + @Override + public Boolean f(List<Integer> _init){ + return _init.length() != 0; + } + }); + + return inits.map(new F<List<Integer>,DefaultNodePath>(){ + @Override + public DefaultNodePath f(List<Integer> _path){ + return new DefaultNodePath(_path); + } + }); + } }
--- 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<T> chs = _root.getChildren(); + //Children<T> chs = _root.getChildren(); + Children<T> chs = wrapper; - Either<Error,List<Direction<T>>> ret = _traverse(chs,_evaluator); + Either<Error,List<Direction<T>>> ret = _traverse(chs,_evaluator,-1); if(ret.isA()){ return DefaultEither.newA(ret.a()); } - final Iterable<Direction<T>> iterable = ret.b(); + List<Direction<T>> list = ret.b(); + + final Iterable<Direction<T>> iterable = list; final T destination = ret.b().last().getTarget(); Traversal<T> traversal = new Traversal<T>(){ @@ -58,9 +61,9 @@ return DefaultEither.newB(traversal); } - private <T extends TraversableNode<T>> Either<Error,List<Direction<T>>> _traverse(Children<T> _chs,Evaluator _evaluator) + private <T extends TraversableNode<T>> Either<Error,List<Direction<T>>> _traverse(Children<T> _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 <T extends TraversableNode<T>> Either<Error,List<Direction<T>>> _accept(final T _current,final int _pos,Evaluator _evaluator) { Children<T> chs = _current.getChildren(); - Either<Error,List<Direction<T>>> either = _traverse(chs,_evaluator); + Either<Error,List<Direction<T>>> either = _traverse(chs,_evaluator,0); if(either.isA()){ return either; }
--- /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<ClonableDefaultNode> instance() + { + DefaultTraverser traverser = new DefaultTraverser(); + ClonableDefaultNode root = new ClonableDefaultNode(new DefaultNode()); + return new DefaultTreeEditor<ClonableDefaultNode>(root,traverser); + } + + public void testEdittingDoesNotEffectToOtherTree() + { + + } + + public void testAppendChild() + { + DefaultTreeEditor<ClonableDefaultNode> instance = instance(); + DefaultNodePath path = new DefaultNodePath(); // root + + Either<Error, DefaultTreeEditor<ClonableDefaultNode>> 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<ClonableDefaultNode> instance = instance(); + DefaultNodePath path = new DefaultNodePath(); + + Either<Error,DefaultTreeEditor<ClonableDefaultNode>> 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<ClonableDefaultNode> testPutAttribute() + { + DefaultTreeEditor<ClonableDefaultNode> instance = instance(); + DefaultNodePath path = new DefaultNodePath(); + + String key = "KEY"; + ByteBuffer value = ByteBuffer.wrap(key.getBytes()); + + Either<Error,DefaultTreeEditor<ClonableDefaultNode>> 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<ClonableDefaultNode> instance = testPutAttribute(); + DefaultNodePath path = new DefaultNodePath(); + + String key = "KEY"; + + Either<Error, DefaultTreeEditor<ClonableDefaultNode>> 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<ClonableDefaultNode> instance = instance(); + DefaultNodePath path = new DefaultNodePath(); + + Either<Error, DefaultTreeEditor<ClonableDefaultNode>> 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()); + } +}
--- 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<DefaultNodePath> 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<Integer,NodePath> pop; - NodePath estimatedPath = path; - DefaultNodePath currentPath = new DefaultNodePath(); + List<DefaultNodePath> parts = path.inits(); for(Direction<TraversableNodeWrapper<ClonableDefaultNode>> 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); } }