# HG changeset patch # User shoshi # Date 1340384060 -32400 # Node ID 07b26b4b21e0607ee5911474d56b7b956f5b007a # Parent 761d04aecfcb90e711554395507ecfd31f7d3aaa modified AbstractVertexesTest diff -r 761d04aecfcb -r 07b26b4b21e0 memo.txt --- a/memo.txt Tue Jun 19 23:38:31 2012 +0900 +++ b/memo.txt Sat Jun 23 01:54:20 2012 +0900 @@ -40,4 +40,9 @@ ・Neo4j で言うと ・ GraphDB , Node , Iterator ・とりあえずこれで実装してみる。 - ・今日は、SimpleVertexes をほぼ完成させた、あとすこしとテストコードを追加すること。 \ No newline at end of file + ・今日は、SimpleVertexes をほぼ完成させた、あとすこしとテストコードを追加すること。 + +2012/06/22 + ・Vertexes のテストコードをほぼ仕上げた. + ・次は,テストコードが正しいか,SimpleVertexes を動作させてみる. + ・その次に,Jungle の実装を書く. \ No newline at end of file diff -r 761d04aecfcb -r 07b26b4b21e0 src/main/java/jungle/core/graph/Vertexes.java --- a/src/main/java/jungle/core/graph/Vertexes.java Tue Jun 19 23:38:31 2012 +0900 +++ b/src/main/java/jungle/core/graph/Vertexes.java Sat Jun 23 01:54:20 2012 +0900 @@ -12,13 +12,13 @@ public void add(Collection _vs); public void add(Vertexes _vs); - public void removeFirst(Vertex _v); - public void removeFirst(Collection _vs); - public void removeFirst(Vertexes _vs); - public void remove(int _i); + public boolean removeFirst(Vertex _v); + public boolean removeFirst(Collection _vs); + public boolean removeFirst(Vertexes _vs); + public Vertex remove(int _i); - public void replace(Vertex _v,int _i); - public void replaceFirst(Vertex _vertex,Vertex _newVertex); + public Vertex replace(Vertex _v,int _i); + public boolean replaceFirst(Vertex _vertex,Vertex _newVertex); public boolean compareAndSwap(int _index,Vertex _vertex,Vertex _newVertex); public boolean contains(Vertex _v); diff -r 761d04aecfcb -r 07b26b4b21e0 src/main/java/jungle/core/graph/simple/SimpleVertex.java --- a/src/main/java/jungle/core/graph/simple/SimpleVertex.java Tue Jun 19 23:38:31 2012 +0900 +++ b/src/main/java/jungle/core/graph/simple/SimpleVertex.java Sat Jun 23 01:54:20 2012 +0900 @@ -27,6 +27,12 @@ } @Override + public String toString() + { + return id; + } + + @Override public String getID() { return id; diff -r 761d04aecfcb -r 07b26b4b21e0 src/main/java/jungle/core/graph/simple/SimpleVertexes.java --- a/src/main/java/jungle/core/graph/simple/SimpleVertexes.java Tue Jun 19 23:38:31 2012 +0900 +++ b/src/main/java/jungle/core/graph/simple/SimpleVertexes.java Sat Jun 23 01:54:20 2012 +0900 @@ -1,5 +1,6 @@ package jungle.core.graph.simple; +import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.HashSet; @@ -29,45 +30,50 @@ vertexes = new AtomicReference>(nil); } + public static void main(String _args[]) + { + SimpleGraph g = new SimpleGraph(); + Vertex v = g.createVertex(); + Vertexes vs = v.createVertexes("test"); + Vertex a = g.createVertex("a"); + Vertex b = g.createVertex("b"); + Vertex c = g.createVertex("c"); + Vertex d = g.createVertex("d"); + vs.add(Arrays.asList(a,b,c,d)); + + System.out.println(vs); + + if(vs.compareAndSwap(2,c,b) == false){ + System.out.println("^o^..."); + } + + System.out.println(vs); + } + @Override - public void remove(final int _index) + public String toString() { + return vertexes.get().toString(); + } + + + @Override + public Vertex remove(final int _index) + { + SimpleVertex head = null; List current = null,update = null; do{ - final List list = vertexes.get(); - update = List.iterableList(new Iterable(){ - @Override - public Iterator iterator() { - return new Iterator(){ - int count = 0; - Iterator itr = list.iterator(); - - @Override - public boolean hasNext() - { - if(itr.hasNext() && count + 1 == _index){ - itr.next(); - } - - count ++; - return itr.hasNext(); - } - - @Override - public SimpleVertex next() - { - return itr.next(); - } - - @Override - public void remove() - { - throw new UnsupportedOperationException("removeing is not supported."); - } - }; - } - }); + current = vertexes.get(); + if(current.length() < _index){ + throw new IndexOutOfBoundsException("list.length() < _index"); + } + + P2,List> slice = current.splitAt(_index); + head = slice._2().head(); + update = slice._1().append(slice._2().drop(1)); }while(!vertexes.compareAndSet(current,update)); + + return head; } @Override @@ -184,7 +190,7 @@ }); @Override - public void removeFirst(final Vertex _v) + public boolean removeFirst(final Vertex _v) { if(_v == null){ throw new NullPointerException("_v is null"); @@ -204,6 +210,9 @@ list = vertexes.get(); update = list.delete((SimpleVertex)_v,VERTEX_EQUAL); }while(!vertexes.compareAndSet(list,update)); + + // list and update 's length are different when removing is succeeded + return list.length() != update.length(); } public Collection toCollection() @@ -213,16 +222,18 @@ } @Override - public void removeFirst(Vertexes _vs) + public boolean removeFirst(Vertexes _vs) { if(_vs instanceof SimpleVertexes){ Collection vs = ((SimpleVertexes)_vs).toCollection(); - removeFirst(vs); + return removeFirst(vs); } + + throw new IllegalArgumentException("_vs is not a instanceof SimpleVertexes"); } @Override - public void removeFirst(final Collection _vs) + public boolean removeFirst(final Collection _vs) { if(_vs == null){ throw new NullPointerException("_vs is null"); @@ -231,8 +242,7 @@ final HashSet tmp = new HashSet(_vs); F predicate = new F(){ @Override - public Boolean f(SimpleVertex _target) - { + public Boolean f(SimpleVertex _target){ return tmp.remove(_target); } }; @@ -242,6 +252,8 @@ list = vertexes.get(); update = list.removeAll(predicate); }while(!vertexes.compareAndSet(list,update)); + + return list.length() != update.length(); } @Override @@ -296,7 +308,7 @@ throw new IllegalArgumentException("_vs is including not a SimpleVertex instance."); } - if(v.getGraph() == parent){ + if(v.getGraph() != parent){ throw new IllegalArgumentException("_vs has vertex from other graph instance"); } return (SimpleVertex)v; @@ -319,7 +331,7 @@ } @Override - public void replace(Vertex _v, int _index) + public Vertex replace(Vertex _v, int _index) { if(_v == null){ throw new NullPointerException("_v is null"); @@ -329,6 +341,7 @@ throw new IllegalArgumentException("_v is not a instanceof SimpleVertex"); } + SimpleVertex head = null; List current = null,update = null; do{ current = vertexes.get(); @@ -337,12 +350,15 @@ } P2,List> slice = current.splitAt(_index); + head = slice._2().head(); update = slice._1().snoc((SimpleVertex)_v).append(slice._2().drop(1)); }while(!vertexes.compareAndSet(current,update)); + + return head; } @Override - public void replaceFirst(final Vertex _vertex, Vertex _newVertex) + public boolean replaceFirst(final Vertex _vertex, Vertex _newVertex) { if(_vertex == null || _newVertex == null){ throw new NullPointerException("_vertex or _newVertex is null"); @@ -359,18 +375,49 @@ } }; - List current = null,update = null; + List current = vertexes.get(),update = null; do{ P2,List> slice = current.span(predicate); - if(slice._2().length() != 0){ - //TODO , next day write here. + if(slice._2().length() == 0){ + // not found. + return false; } + + update = slice._1().append(slice._2().drop(1).cons((SimpleVertex)_newVertex)); }while(!vertexes.compareAndSet(current,update)); + + return true; } @Override public boolean compareAndSwap(int _index, Vertex _vertex, Vertex _newVertex) { - return false; + List current = vertexes.get(); + if(current.length() < _index || _index < 0){ + throw new IndexOutOfBoundsException("list.length() < _index or _index < 0"); + } + + final SimpleVertex v1 = (SimpleVertex)_vertex; + SimpleVertex v2 = (SimpleVertex)_newVertex; + + F predicate = new F(){ + @Override + public Boolean f(SimpleVertex _vertex2){ + return v1 != _vertex2; + } + }; + + List update = null; + do{ + P2,List> slice = current.span(predicate); + SimpleVertex head = slice._2().head(); + if(head != v1){ + return false; + } + + update = slice._1().append(slice._2().drop(1).snoc(v2)); + }while(!vertexes.compareAndSet(current,update)); + + return true; } -} +} \ No newline at end of file diff -r 761d04aecfcb -r 07b26b4b21e0 src/test/java/jungle/core/graph/AbstractVertexesTest.java --- a/src/test/java/jungle/core/graph/AbstractVertexesTest.java Tue Jun 19 23:38:31 2012 +0900 +++ b/src/test/java/jungle/core/graph/AbstractVertexesTest.java Sat Jun 23 01:54:20 2012 +0900 @@ -1,8 +1,11 @@ package jungle.core.graph; +import java.util.Arrays; import java.util.Collection; import java.util.HashSet; +import javax.swing.plaf.basic.BasicScrollPaneUI.VSBChangeListener; + import junit.framework.Assert; import junit.framework.TestCase; @@ -10,47 +13,60 @@ { public abstract Graph newInstance(); + public static final String RELATION = "RELATION"; + public void testSize() { Graph g = newInstance(); Vertex v = g.createVertex(); - Vertexes vs = v.createVertexes("RELATIONS"); + Vertexes vs = v.createVertexes(RELATION); + // first must be zero. Assert.assertEquals(0,vs.size()); - Vertex c1 = g.createVertex(); - vs.add(c1); + // add single , size() will be one. + vs.add(g.createVertex()); Assert.assertEquals(1,vs.size()); - Vertex c2 = g.createVertex(); - vs.add(c2); + // add single , size() will be two. + vs.add(g.createVertex()); Assert.assertEquals(2,vs.size()); Vertex c3 = g.createVertex(); vs.add(c3); Assert.assertEquals(3,vs.size()); - vs.remove(c3); + // remove , will be two + vs.removeFirst(c3); Assert.assertEquals(2,vs.size()); } - public void testAddingDoesNotAcceptOtherGraphVertex() + public void testAddFromOtherGraphExceptions() { Graph g1 = newInstance(); Graph g2 = newInstance(); Vertex v1 = g1.createVertex(); - Vertex v2 = g2.createVertex(); + Vertex v2 = g2.createVertex(); // belongs to g2 - Vertexes vs = v1.createVertexes("RELATIONS"); + Vertexes vs = v1.createVertexes(RELATION); try{ // v2 belongs to g2 , but v1 is not belongs to g2 vs.add(v2); // should be fail here. + Assert.fail("add(Vertex) must fail when vertex from other graph is supplied"); }catch(IllegalArgumentException _e){ - return; + // do nothing. } - Assert.fail("testing add() does not accept other graph vertex fail."); + try{ + Collection c = Arrays.asList(g2.createVertex(),g2.createVertex()); + vs.add(c); + Assert.fail("add(Collection) must fail when vertex from other graph is supplied"); + }catch(IllegalArgumentException _e){ + // do nothing. + } + + return; } public void testAddSingleVertex() @@ -58,7 +74,7 @@ Graph g = newInstance(); Vertex v = g.createVertex(); Vertex c = g.createVertex(); - Vertexes vs = v.createVertexes("RELATIONS"); + Vertexes vs = v.createVertexes(RELATION); vs.add(c); @@ -67,11 +83,11 @@ } } - public void testAddRefuseNull() + public void testAddExceptions() { Graph g = newInstance(); Vertex v = g.createVertex(); - Vertexes vs = v.createVertexes("RELATIONS"); + Vertexes vs = v.createVertexes(RELATION); try{ vs.add((Vertex)null); @@ -82,17 +98,17 @@ try{ vs.add((Collection)null); + Assert.fail("vs.add(Collection didnt throw null pointer exception"); }catch(NullPointerException _e){ return; } - Assert.fail("vs.add(Collection didnt throw null pointer exception"); } public void testAddVertexCollection() { Graph g = newInstance(); Vertex v = g.createVertex(); - Vertexes vs = v.createVertexes("RELATIONS"); + Vertexes vs = v.createVertexes(RELATION); HashSet set = new HashSet(); set.add(g.createVertex()); @@ -107,6 +123,197 @@ } } + public void testAt() + { + Graph g = newInstance(); + Vertex v = g.createVertex(); + Vertexes vs = v.createVertexes(RELATION); + + Vertex c = g.createVertex(); + + vs.add(g.createVertex()); // 0 + vs.add(g.createVertex()); // 1 + vs.add(c); // 2 + vs.add(g.createVertex()); // 3 + vs.add(g.createVertex()); // 4 + + Vertex two = vs.at(2); + Assert.assertEquals(c,two); + } + + public void testAtExceptions() + { + Graph g = newInstance(); + Vertex v = g.createVertex(); + Vertexes vs = v.createVertexes(RELATION); + + vs.add(g.createVertex()); // 0 + vs.add(g.createVertex()); // 1 + vs.add(g.createVertex()); // 2 + vs.add(g.createVertex()); // 3 + + try{ + // try muinus value + vs.at(-1); + Assert.fail("Vertexes.at() must throw IndexOutOfBoundsException when muinus value supplied."); + }catch(IndexOutOfBoundsException _e){ + // ??? + } + + try{ + // try large value , maximum index is 3 + vs.at(4); + Assert.fail("Vertexes.at() must throw IndexOutOfBoundsException when large value supplied."); + }catch(IndexOutOfBoundsException _e){ + // ??? + } + } + + public void testReplace() + { + Graph g = newInstance(); + Vertex v = g.createVertex(); + Vertexes vs = v.createVertexes(RELATION); + Vertex a = g.createVertex("a"); + Vertex c = g.createVertex("c"); + + vs.add(Arrays.asList(g.createVertex(),g.createVertex(),c,g.createVertex())); + Assert.assertEquals(c,vs.replace(a,2)); + Assert.assertEquals(a,vs.at(2)); + } + + public void testReplaceExceptions() + { + Graph g = newInstance(); + Vertex v = g.createVertex(); + Vertexes vs = v.createVertexes(RELATION); + + Vertex a = g.createVertex("a"); + vs.add(Arrays.asList(g.createVertex(),a,g.createVertex())); + + try{ + vs.replace(a,-1); + Assert.fail("Vertex.replate(Vertex,int) must throw IndexOutOfBoundsException when muinus value supplied."); + }catch(IndexOutOfBoundsException _e){ + // nothing + } + + try{ + vs.replace(a,3); + Assert.fail("Vertex.replate(Vertex,int) must throw IndexOutOfBoundsException when large value supplied."); + }catch(IndexOutOfBoundsException _e){ + // nothing + } + + try{ + vs.replace(null,0); + Assert.fail("Vertex.replate(Vertex,int) must throw NullPointerException when null value supplied."); + }catch(NullPointerException _e){ + // nothing + } + } + + public void testReplaceFirst() + { + Graph g = newInstance(); + Vertex v = g.createVertex(); + Vertexes vs = v.createVertexes(RELATION); + Vertex a = g.createVertex("a"); + Vertex c = g.createVertex("c"); + + vs.add(Arrays.asList(g.createVertex(),g.createVertex(),c,g.createVertex(),c,c)); + Assert.assertTrue(vs.replaceFirst(c,a)); + Assert.assertEquals(a,vs.at(2)); + Assert.assertEquals(c,vs.at(4)); + Assert.assertEquals(c,vs.at(5)); + } + + public void testReplaceFirstExceptions() + { + Graph g = newInstance(); + Vertex v = g.createVertex(); + Vertexes vs = v.createVertexes(RELATION); + Vertex a = g.createVertex("a"); + Vertex c = g.createVertex("c"); + + vs.add(Arrays.asList(g.createVertex(),g.createVertex(),c,g.createVertex(),c,c)); + + try{ + vs.replaceFirst(null,c); + Assert.fail("Vertexes.replaceFirst(Vertex,Vertex) must throw null when null value supplied."); + }catch(NullPointerException _e){ + // nothing. + } + + try{ + vs.replaceFirst(c,null); + Assert.fail("Vertexes.replaceFirst(Vertex,Vertex) must throw null when null value supplied."); + }catch(NullPointerException _e){ + // nothing. + } + } + + public void testCompareAndSwap() + { + Graph g = newInstance(); + Vertex v = g.createVertex(); + Vertexes vs = v.createVertexes(RELATION); + Vertex a = g.createVertex("a"); + Vertex c = g.createVertex("c"); + + vs.add(Arrays.asList(g.createVertex(),g.createVertex(),c,g.createVertex(),c,c)); + Assert.assertTrue(vs.compareAndSwap(2,c,a)); + Assert.assertEquals(a,vs.at(2)); + Assert.assertFalse(vs.compareAndSwap(2,c,a)); + } + + public void testCompareAndSwapExceptions() + { + Graph g = newInstance(); + Vertex v = g.createVertex(); + Vertexes vs = v.createVertexes(RELATION); + Vertex a = g.createVertex("a"); + Vertex c = g.createVertex("c"); + + vs.add(Arrays.asList(g.createVertex(),g.createVertex(),c,g.createVertex(),c,c)); + try{ + vs.compareAndSwap(-1,a,c); + Assert.fail("Vertexes.compareAndSwap(int,Vertex,Vertex) must throw IndexOutOfBoundsException when muinus value supplied."); + }catch(IndexOutOfBoundsException _e){ + // nothing. + } + + try{ + vs.compareAndSwap(6,a,c); + Assert.fail("Vertexes.compareAndSwap(int,Vertex,Vertex) must throw IndexOutOfBoundsException when large value supplied."); + }catch(IndexOutOfBoundsException _e){ + // nothing. + } + + try{ + vs.compareAndSwap(2,null,c); + Assert.fail("Vertexes.compareAndSwap(int,Vertex,Vertex) must throw NullPointerException when null value supplied."); + }catch(NullPointerException _e){ + // nothing. + } + + try{ + vs.compareAndSwap(2,c,null); + Assert.fail("Vertexes.compareAndSwap(int,Vertex,Vertex) must throw NullPointerException when null value supplied."); + }catch(NullPointerException _e){ + // nothing. + } + } + + public void testGetGraph() + { + Graph g = newInstance(); + Vertex v = g.createVertex(); + Vertexes vs = v.createVertexes(RELATION); + + Assert.assertEquals(g,vs.getGraph()); + } + public void testRemoveSingleVertex() { Graph g = newInstance(); @@ -122,7 +329,7 @@ vs.add(c); Assert.assertEquals(4,vs.size()); - vs.remove(c); + vs.removeFirst(c); Assert.assertEquals(3,vs.size()); for(Vertex x : vs){ @@ -132,24 +339,46 @@ Assert.assertEquals(0,set.size()); } - public void testRemoveVertexRefuseNull() + public void testRemoveVertexExceptions() { Graph g = newInstance(); Vertexes vs = g.createVertex().createVertexes("RELATIONS"); + vs.add(Arrays.asList(g.createVertex(),g.createVertex(),g.createVertex())); try{ - vs.remove((Vertex)null); + vs.remove(-1); + Assert.fail("Vertexes.remove(int) must throw IndexOutOfBoundsException when muinus value supplied."); + }catch(IndexOutOfBoundsException _e){ + // do nothing. + } + + try{ + vs.remove(10); + Assert.fail("Vertexes.remove(int) must throw IndexOutOfBoundsException when large value supplied."); + }catch(IndexOutOfBoundsException _e){ + // do nothing. + } + + try{ + vs.removeFirst((Vertex)null); Assert.fail("vs.remove(Vertex) didnt throw null pointer exception"); }catch(NullPointerException _e){ // do nothing. } try{ - vs.remove((Collection)null); + vs.removeFirst((Vertexes)null); + Assert.fail("vs.remove(Vertexes) didnt throw null pointer exception"); + }catch(NullPointerException _e){ + // do nothing. + } + + try{ + vs.removeFirst((Collection)null); + Assert.fail("vs.remove(Collection didnt throw null pointer exception"); }catch(NullPointerException _e){ return; } - Assert.fail("vs.remove(Collection didnt throw null pointer exception"); } public void testRemoveVertexCollection() @@ -165,7 +394,7 @@ vs.add(set); Assert.assertEquals(3,vs.size()); - vs.remove(set); + vs.removeFirst(set); Assert.assertEquals(0,vs.size()); } @@ -181,6 +410,19 @@ Assert.assertTrue(vs.contains(v)); } + public void testContainsExceptions() + { + Graph g = newInstance(); + Vertexes vs = g.createVertex().createVertexes("RELATIONS"); + + try{ + vs.contains(null); + Assert.fail("Vertexes.contains(Vertex) must throw NullPointerException when null value supplied."); + }catch(NullPointerException _e){ + return; + } + } + public void testVertexType() { Graph g = newInstance();