From 7d4ad896f5bcceccc0b91e9bb1bb3943ec2ddd09 Mon Sep 17 00:00:00 2001 From: Eric Vergnaud Date: Fri, 8 Mar 2024 19:40:06 +0100 Subject: [PATCH] add test spec for HashSet Signed-off-by: Eric Vergnaud --- runtime/JavaScript/spec/HashSetSpec.js | 52 ++++++++++++++++++++++ runtime/JavaScript/spec/IntervalSetSpec.js | 5 +-- 2 files changed, 54 insertions(+), 3 deletions(-) create mode 100644 runtime/JavaScript/spec/HashSetSpec.js diff --git a/runtime/JavaScript/spec/HashSetSpec.js b/runtime/JavaScript/spec/HashSetSpec.js new file mode 100644 index 0000000000..542d4fb566 --- /dev/null +++ b/runtime/JavaScript/spec/HashSetSpec.js @@ -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); + }) + +}) diff --git a/runtime/JavaScript/spec/IntervalSetSpec.js b/runtime/JavaScript/spec/IntervalSetSpec.js index b40511e543..977aa45340 100644 --- a/runtime/JavaScript/spec/IntervalSetSpec.js +++ b/runtime/JavaScript/spec/IntervalSetSpec.js @@ -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);