Skip to content

Commit

Permalink
add test spec for HashSet
Browse files Browse the repository at this point in the history
Signed-off-by: Eric Vergnaud <eric.vergnaud@wanadoo.fr>
  • Loading branch information
ericvergnaud committed Mar 11, 2024
1 parent 2865844 commit 7d4ad89
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 3 deletions.
52 changes: 52 additions & 0 deletions runtime/JavaScript/spec/HashSetSpec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import HashSet from "../src/antlr4/misc/HashSet.js";
import HashCode from "../src/antlr4/misc/HashCode.js";

class Thing {

value1 = Math.random();
value2 = Math.random();

hashCode() {
return HashCode.hashStuff(this.value1);
}

equals(other) {
return other instanceof Thing
&& other.value1 === this.value1
&& other.value2 === this.value2;
}
}
describe('test HashSet', () => {

it("adds a thing", () => {
const t1 = new Thing();
const t2 = new Thing();
const set = new HashSet();
set.add(t1);
expect(set.has(t1)).toBeTrue();
expect(set.has(t2)).toBeFalse();
expect(set.length).toEqual(1);
})

it("adds a thing once only", () => {
const t1 = new Thing();
const set = new HashSet();
set.add(t1);
set.add(t1);
expect(set.has(t1)).toBeTrue();
expect(set.length).toEqual(1);
})

it("adds 2 things with same hash code", () => {
const t1 = new Thing();
const t2 = new Thing();
t2.value1 = t1.value1;
const set = new HashSet();
set.add(t1);
set.add(t2);
expect(set.has(t1)).toBeTrue();
expect(set.has(t2)).toBeTrue();
expect(set.length).toEqual(2);
})

})
5 changes: 2 additions & 3 deletions runtime/JavaScript/spec/IntervalSetSpec.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import antlr4 from "../src/antlr4/index.node.js";
const IntervalSet = antlr4.IntervalSet;
import IntervalSet from "../src/antlr4/misc/IntervalSet.js";

describe('IntervalSet', () => {
describe('test IntervalSet', () => {
it("computes interval set length", () => {
const s1 = new IntervalSet();
s1.addOne(20);
Expand Down

0 comments on commit 7d4ad89

Please sign in to comment.