Skip to content

Commit

Permalink
Override hashCode where equals is overridden
Browse files Browse the repository at this point in the history
If two objects are equal according to the equals(Object), then calling
the hashCode method on each of the two objects must produce the same
integer result.

However we have classes in src/tests that overrides equals(Object)
without overriding the hashCode method. This overrides the missing
hashCode methods.

Fixes: #85
  • Loading branch information
gayanW committed Jun 15, 2018
1 parent b1f8b9d commit e39731f
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/tests/TypeNameTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ public boolean equals(Object other) {
return ((B) other).data == data;
}

@Override
public int hashCode() {
return Integer.hashCode(data);
}

@Override
public String toString() {
return "B {data=" + data + "}";
Expand Down
4 changes: 4 additions & 0 deletions src/tests/gov/nasa/jpf/test/mc/data/CGCreatorFactoryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ public boolean equals(Object o) {
return this.b == other.b;
}

@Override
public int hashCode() {
return Boolean.hashCode(b);
}
}

@Test
Expand Down
33 changes: 33 additions & 0 deletions src/tests/gov/nasa/jpf/test/mc/data/JSONTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@

import org.junit.Test;

import java.util.Arrays;
import java.util.Objects;


/**
* JPF regression test for JSON test object creation
Expand Down Expand Up @@ -350,6 +353,11 @@ public boolean equals(Object o) {
Bool bool = (Bool) o;
return this.b == bool.b;
}

@Override
public int hashCode() {
return Boolean.hashCode(b);
}
}

static void checkValue(Object[] expected, Object curVal) {
Expand Down Expand Up @@ -399,6 +407,11 @@ public boolean equals(Object o) {

return bs.b == b && bs.s == s && bs.i == i && bs.l == l;
}

@Override
public int hashCode() {
return Objects.hash(b, s, i, l);
}
}

@Test
Expand Down Expand Up @@ -457,6 +470,11 @@ public boolean equals(Object o) {

return outer.inner.i == this.inner.i;
}

@Override
public int hashCode() {
return inner.i;
}
}

@Test
Expand Down Expand Up @@ -498,6 +516,11 @@ public boolean equals(Object o) {
}
return true;
}

@Override
public int hashCode() {
return Arrays.hashCode(arr);
}
}

@Test
Expand Down Expand Up @@ -529,6 +552,11 @@ public boolean equals(Object obj) {
BoxedInteger bic = (BoxedInteger) obj;
return this.bi.equals(bic.bi);
}

@Override
public int hashCode() {
return bi;
}
}

@Test
Expand Down Expand Up @@ -565,6 +593,11 @@ boolean doublesEqual(double d1, double d2) {

return Math.abs(d1 - d2) <= diff;
}

@Override
public int hashCode() {
return Double.hashCode(d);
}
}

@Test
Expand Down
7 changes: 7 additions & 0 deletions src/tests/gov/nasa/jpf/util/SortedArrayObjectSetTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import gov.nasa.jpf.util.test.TestJPF;
import org.junit.Test;

import java.util.Objects;


/**
* regression test for SortedArrayObjectSet
Expand Down Expand Up @@ -59,6 +61,11 @@ public boolean equals(Object o){

return false;
}

@Override
public int hashCode() {
return Objects.hash(id, x);
}
}

@Test
Expand Down

0 comments on commit e39731f

Please sign in to comment.