Mercurial > hg > Members > shoshi > jungle > jungle-core
changeset 39:7d78bbf4a9cd
added some tests
line wrap: on
line diff
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/store/trasnformer/EditableAttributes.java Mon Jan 28 18:52:52 2013 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/store/trasnformer/EditableAttributes.java Tue Jan 29 01:11:17 2013 +0900 @@ -11,6 +11,4 @@ { public Either<Error,T> delete(String _key); public Either<Error,T> put(String _key,ByteBuffer _value); - - public static final Error KEY_NOT_FOUND = new DefaultError(); }
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/store/trasnformer/NodeEditorError.java Mon Jan 28 18:52:52 2013 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/store/trasnformer/NodeEditorError.java Tue Jan 29 01:11:17 2013 +0900 @@ -6,4 +6,5 @@ public class NodeEditorError { public static final Error INDEX_OUT_OF_BOUNDS = new DefaultError(); + public static final Error DELETE_KEY_NOT_FOUND = new DefaultError(); }
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/transaction/ClonableDefaultAttributes.java Mon Jan 28 18:52:52 2013 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/transaction/ClonableDefaultAttributes.java Tue Jan 29 01:11:17 2013 +0900 @@ -7,6 +7,7 @@ import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.ClonableAttributes; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.DefaultChildren; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.DefaultNode; +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.trasnformer.NodeEditorError; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.DefaultEither; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Either; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Error; @@ -27,6 +28,10 @@ DefaultChildren children = wrap.getChildren(); TreeMap<String,ByteBuffer> raw = attrs.getAttributesAsRawMap(); + if(!raw.contains(_key)){ + return DefaultEither.newA(NodeEditorError.DELETE_KEY_NOT_FOUND); + } + TreeMap<String,ByteBuffer> newMap = raw.delete(_key); DefaultNode newNode = new DefaultNode(children.getChildrenAsRawList(),newMap);
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/transaction/ClonableDefaultChildren.java Mon Jan 28 18:52:52 2013 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/transaction/ClonableDefaultChildren.java Tue Jan 29 01:11:17 2013 +0900 @@ -18,6 +18,15 @@ public class ClonableDefaultChildren implements ClonableChildren<ClonableDefaultNode> { + public static void main(String _args[]) + { + List<Integer> list = List.range(0,5); + P2<List<Integer>, List<Integer>> split = list.splitAt(3); + System.out.println(split._1().length()); + System.out.println(split._2().length()); + + } + private final DefaultNode node; public ClonableDefaultChildren(DefaultNode _node) @@ -28,7 +37,7 @@ private boolean boundaryCheck(int _pos) { int size = node.getChildren().size(); - if(size <= _pos){ + if(size < _pos){ return false; } @@ -38,7 +47,7 @@ @Override public Either<Error,ClonableDefaultNode> addNewChildAt(int _pos) { - if(!boundaryCheck(_pos)){ + if(!boundaryCheck(_pos) || _pos < 0){ return DefaultEither.newA(NodeEditorError.INDEX_OUT_OF_BOUNDS); } @@ -57,7 +66,7 @@ @Override public Either<Error,ClonableDefaultNode> deleteChildAt(int _pos) { - if(!boundaryCheck(_pos)){ + if(!boundaryCheck(_pos) || _pos < 0 || size() == 0){ return DefaultEither.newA(NodeEditorError.INDEX_OUT_OF_BOUNDS); } @@ -65,7 +74,8 @@ List<DefaultNode> raw = chs.getChildrenAsRawList(); P2<List<DefaultNode>,List<DefaultNode>> split = raw.splitAt(_pos); - List<DefaultNode> newChildren = split._1().init().append(split._2()); + //List<DefaultNode> newChildren = split._1().init().append(split._2()); + List<DefaultNode> newChildren = split._1().append(split._2().tail()); DefaultAttributes attrs = node.getAttributes(); TreeMap<String, ByteBuffer> map = attrs.getAttributesAsRawMap(); @@ -102,7 +112,15 @@ if(!boundaryCheck(_pos)){ return DefaultEither.newA(NodeEditorError.INDEX_OUT_OF_BOUNDS); } + DefaultNode replacement = _replacement.getWrapped(); - return null; + List<DefaultNode> rawList = node.getChildren().getChildrenAsRawList(); + P2<List<DefaultNode>,List<DefaultNode>> split = rawList.splitAt(_pos); + List<DefaultNode> init = split._1().init(); + List<DefaultNode> newInit = init.snoc(replacement); + List<DefaultNode> newList = newInit.append(split._2()); + TreeMap<String, ByteBuffer> rawMap = node.getAttributes().getAttributesAsRawMap(); + + return DefaultEither.newB(new ClonableDefaultNode(new DefaultNode(newList,rawMap))); } }
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/transaction/ClonableDefaultNode.java Mon Jan 28 18:52:52 2013 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/transaction/ClonableDefaultNode.java Tue Jan 29 01:11:17 2013 +0900 @@ -1,7 +1,5 @@ package jp.ac.u_ryukyu.ie.cr.shoshi.jungle.transaction; -import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.ClonableAttributes; -import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.ClonableChildren; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.ClonableNode; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.DefaultNode; @@ -15,13 +13,13 @@ } @Override - public ClonableChildren<ClonableDefaultNode> getChildren() + public ClonableDefaultChildren getChildren() { return new ClonableDefaultChildren(wrap); } @Override - public ClonableAttributes<ClonableDefaultNode> getAttributes() + public ClonableDefaultAttributes getAttributes() { return new ClonableDefaultAttributes(wrap); }
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/util/DefaultEither.java Mon Jan 28 18:52:52 2013 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/util/DefaultEither.java Tue Jan 29 01:11:17 2013 +0900 @@ -30,7 +30,7 @@ @Override public boolean isA() { - return (theA == null) ? true : false; + return theA != null; } @Override @@ -42,6 +42,6 @@ @Override public boolean isB() { - return (theB == null) ? true : false; + return theB != null; } }
--- a/src/test/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/core/AttributesTest.java Mon Jan 28 18:52:52 2013 +0900 +++ b/src/test/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/core/AttributesTest.java Tue Jan 29 01:11:17 2013 +0900 @@ -28,6 +28,7 @@ String key = entry._1(); ByteBuffer expect = entry._2(); ByteBuffer actual = attrs.get(key); + Assert.assertNotNull(actual); Assert.assertEquals(0,actual.compareTo(expect)); } }
--- a/src/test/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/impl/node/DefaultAttributesTest.java Mon Jan 28 18:52:52 2013 +0900 +++ b/src/test/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/impl/node/DefaultAttributesTest.java Tue Jan 29 01:11:17 2013 +0900 @@ -2,9 +2,12 @@ import java.nio.ByteBuffer; +import org.junit.Assert; + import fj.Ord; import fj.P2; import fj.data.List; +import fj.data.Option; import fj.data.TreeMap; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.core.AttributesTest; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.DefaultAttributes; @@ -26,4 +29,27 @@ DefaultNode node = new DefaultNode(rawList,rawMap); return node.getAttributes(); } + + public void testGetAttributesAsRawMap() + { + DefaultAttributes attrs = instance(); + TreeMap<String,ByteBuffer> rawMap = attrs.getAttributesAsRawMap(); + + // testing rawmap is not null. + Assert.assertNotNull(rawMap); + + // testing rawmap has correct key values. + for(P2<String,ByteBuffer> entry : ENTRIES){ + String key = entry._1(); + ByteBuffer value = entry._2(); + + Option<ByteBuffer> option = rawMap.get(key); + if(option.isNone()){ + Assert.fail(); + } + + ByteBuffer actual = option.some(); + Assert.assertEquals(0,actual.compareTo(value)); + } + } }
--- a/src/test/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/impl/node/DefaultChildrenTest.java Mon Jan 28 18:52:52 2013 +0900 +++ b/src/test/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/impl/node/DefaultChildrenTest.java Tue Jan 29 01:11:17 2013 +0900 @@ -2,6 +2,8 @@ import java.nio.ByteBuffer; +import org.junit.Assert; + import fj.Ord; import fj.data.List; import fj.data.TreeMap; @@ -31,4 +33,13 @@ { return 3; } + + public void testGetChildrenAsRawList() + { + DefaultChildren instance = instance(); + List<DefaultNode> rawList = instance.getChildrenAsRawList(); + + Assert.assertNotNull(rawList); + Assert.assertEquals(expectSize(),rawList.length()); + } } \ No newline at end of file