Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Fixed a bug in checking the type of edges in EdgeTypeIterator #852

Merged
merged 1 commit into from

2 participants

@prathamesh-sonpatki
  • While checking for type of edges, equality is not sufficient as we are comparing two objects
  • Added specs for this change
@prathamesh-sonpatki prathamesh-sonpatki Fixed a bug in checking the type of edges in EdgeTypeIterator
- While checking for type of edges, equality is not sufficient as we
  are comparing two objects
- Added specs for this change
389bcda
@enebo enebo merged commit 908153f into from
@prathamesh-sonpatki prathamesh-sonpatki deleted the branch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jul 9, 2013
  1. @prathamesh-sonpatki

    Fixed a bug in checking the type of edges in EdgeTypeIterator

    prathamesh-sonpatki authored
    - While checking for type of edges, equality is not sufficient as we
      are comparing two objects
    - Added specs for this change
This page is out of date. Refresh to see the latest.
View
33 core/src/main/java/org/jruby/ir/util/EdgeTypeIterator.java
@@ -1,7 +1,3 @@
-/*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
package org.jruby.ir.util;
import java.util.Iterator;
@@ -16,7 +12,7 @@
private Object type;
private Edge nextEdge = null;
private boolean negate;
-
+
public EdgeTypeIterator(Set<Edge<T>> edges, Object type, boolean negate) {
this.internalIterator = edges.iterator();
this.type = type;
@@ -26,21 +22,36 @@ public EdgeTypeIterator(Set<Edge<T>> edges, Object type, boolean negate) {
public boolean hasNext() {
// Multiple hasNext calls with no next...hasNext still true
if (nextEdge != null) return true;
-
+
while (internalIterator.hasNext()) {
Edge edge = internalIterator.next();
+ Object edgeType = edge.getType();
if (negate) {
- if (edge.getType() != type) {
+ if (type != null && edgeType != null && edgeType.equals(type) == false) {
+ nextEdge = edge;
+ return true;
+ }
+ else if (type == null || edgeType == null) {
+ if (edgeType != type) {
+ nextEdge = edge;
+ return true;
+ }
+ }
+ }
+ else {
+ if (type != null && edgeType != null && edgeType.equals(type)) {
nextEdge = edge;
return true;
}
- } else if (edge.getType() == type) {
- nextEdge = edge;
- return true;
+ else if (type == null || edgeType == null) {
+ if (edgeType == type) {
+ nextEdge = edge;
+ return true;
+ }
+ }
}
}
-
return false;
}
View
129 spec/ir/directed_graph/edge_type_iterator_spec.rb
@@ -0,0 +1,129 @@
+import 'org.jruby.ir.util.DirectedGraph'
+import 'org.jruby.ir.util.EdgeTypeIterator'
+import 'java.util.NoSuchElementException'
+
+describe "EdgeTypeIterable" do
+
+ before do
+ @graph = DirectedGraph.new
+ @graph.addEdge(1, 2, "foo")
+ @graph.addEdge(2, 3, "foo")
+ end
+
+ describe "hasNext" do
+
+ context "edges of given type" do
+
+ it "returns true if the iterator contains an edge of given type" do
+ iterator = EdgeTypeIterator.new(@graph.edges(), "foo", false)
+ expect(iterator.hasNext).to eq true
+ end
+
+ it "returns false if the iterator does not contain any edge of given type" do
+ iterator = EdgeTypeIterator.new(@graph.edges(), "bar", false)
+ expect(iterator.hasNext).to eq false
+ end
+
+ end
+
+ context "edges not of given type" do
+
+ it "returns true if the iterator contains an edge not of given type" do
+ iterator = EdgeTypeIterator.new(@graph.edges(), "bar", true)
+ expect(iterator.hasNext).to eq true
+ end
+
+ it "returns false if the iterator contains an edge of given type" do
+ iterator = EdgeTypeIterator.new(@graph.edges(), "foo", true)
+ expect(iterator.hasNext).to eq false
+ end
+
+ end
+
+ context "when iterator type is null" do
+
+ context "edges of given type" do
+
+ it "returns true if the iterator contains an edge of type nil" do
+ # add an edge of type nil
+ @graph.addEdge(4,1,nil)
+ iterator = EdgeTypeIterator.new(@graph.edges(), nil, false)
+ expect(iterator.hasNext).to eq true
+ end
+
+ it "returns false if the iterator does not contain any edge of type nil" do
+ iterator = EdgeTypeIterator.new(@graph.edges(), nil, false)
+ expect(iterator.hasNext).to eq false
+ end
+
+ end
+
+ context "edges not of given type" do
+
+ it "returns true if the iterator contains an edge not of type nil" do
+ iterator = EdgeTypeIterator.new(@graph.edges(), nil, true)
+ expect(iterator.hasNext).to eq true
+ end
+
+ it "returns false if the iterator contains all edges of type nil" do
+ # remove existing edges not of type nil
+ @graph.removeEdge(1,2)
+ @graph.removeEdge(2,3)
+ # add an edge of type nil
+ @graph.addEdge(4,1,nil)
+ iterator = EdgeTypeIterator.new(@graph.edges(), nil, true)
+ expect(iterator.hasNext).to eq false
+ end
+
+ end
+
+ end
+
+ context "when edge type is nil and iterator type is not nil" do
+
+ it "returns true if the iterator contains an edge not of type nil" do
+ # remove existing edges not of type nil
+ @graph.removeEdge(1,2)
+ @graph.removeEdge(2,3)
+ # add an edge of type nil
+ @graph.addEdge(4,1,nil)
+ iterator = EdgeTypeIterator.new(@graph.edges(), "foo", true)
+ expect(iterator.hasNext).to eq true
+ end
+
+ it "returns false if the iterator contains all edges not of type nil" do
+ iterator = EdgeTypeIterator.new(@graph.edges(), "foo", true)
+ expect(iterator.hasNext).to eq false
+ end
+ end
+
+ end
+
+ describe "next" do
+
+ context "when the iterator has next edge" do
+
+ it "returns the next edge" do
+ iterator = EdgeTypeIterator.new(@graph.edges(), "foo", false)
+ expect(iterator.next.getType).to eq "foo"
+ end
+ end
+
+ context "when the iterator does not have next edge" do
+ it "throws NoSuchElementException" do
+ empty_graph = DirectedGraph.new
+ iterator = EdgeTypeIterator.new(empty_graph.edges(), "foo", false)
+ expect { iterator.next }.to raise_error NoSuchElementException
+ end
+ end
+ end
+
+ describe "remove" do
+
+ it "throws UnsupportedOperationException exception" do
+ iterator = EdgeTypeIterator.new(@graph.edges(), "foo", false)
+ expect { iterator.remove }.to raise_error Java::JavaLang::UnsupportedOperationException
+ end
+ end
+
+end
Something went wrong with that request. Please try again.