-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement faster HashMap and test spec
Signed-off-by: Robert Einhorn <[email protected]>
- Loading branch information
1 parent
1f5e64b
commit fcd47de
Showing
3 changed files
with
129 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import HashMap from "../src/antlr4/misc/HashMap.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 HashMap', () => { | ||
|
||
it("sets a thing", () => { | ||
const t1 = new Thing(); | ||
const map = new HashMap(); | ||
map.set("abc", t1); | ||
expect(map.containsKey("abc")).toBeTrue(); | ||
expect(map.containsKey("def")).toBeFalse(); | ||
expect(map.length).toEqual(1); | ||
}) | ||
|
||
it("gets a thing", () => { | ||
const t1 = new Thing(); | ||
const map = new HashMap(); | ||
map.set("abc", t1); | ||
const t2 = map.get("abc"); | ||
expect(t2).toEqual(t1); | ||
}) | ||
|
||
it("replaces a thing", () => { | ||
const t1 = new Thing(); | ||
const t2 = new Thing(); | ||
const map = new HashMap(); | ||
map.set("abc", t1); | ||
map.set("abc", t2); | ||
const t3 = map.get("abc"); | ||
expect(t3).toEqual(t2); | ||
}) | ||
|
||
it("returns correct length", () => { | ||
const t1 = new Thing(); | ||
const t2 = new Thing(); | ||
const map = new HashMap(); | ||
expect(map.length).toEqual(0); | ||
map.set("abc", t1); | ||
expect(map.length).toEqual(1); | ||
map.set("def", t2); | ||
expect(map.length).toEqual(2); | ||
}) | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters