-
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.
- Loading branch information
Showing
13 changed files
with
470 additions
and
100 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,110 @@ | ||
import BitSet from "../src/antlr4/misc/BitSet.js"; | ||
|
||
describe('test BitSet', () => { | ||
|
||
it("is empty", () => { | ||
const bs = new BitSet(); | ||
expect(bs.length).toEqual(0); | ||
}) | ||
|
||
it("sets 1 value", () => { | ||
const bs = new BitSet(); | ||
bs.set(67); | ||
expect(bs.length).toEqual(1); | ||
expect(bs.get(67)).toBeTrue(); | ||
}) | ||
|
||
it("clears 1 value", () => { | ||
const bs = new BitSet(); | ||
bs.set(67); | ||
bs.clear(67) | ||
expect(bs.length).toEqual(0); | ||
expect(bs.get(67)).toBeFalse(); | ||
}) | ||
|
||
it("sets 2 consecutive values", () => { | ||
const bs = new BitSet(); | ||
bs.set(67); | ||
bs.set(68); | ||
expect(bs.length).toEqual(2); | ||
expect(bs.get(67)).toBeTrue(); | ||
expect(bs.get(68)).toBeTrue(); | ||
}) | ||
|
||
it("sets 2 close values", () => { | ||
const bs = new BitSet(); | ||
bs.set(67); | ||
bs.set(70); | ||
expect(bs.length).toEqual(2); | ||
expect(bs.get(67)).toBeTrue(); | ||
expect(bs.get(70)).toBeTrue(); | ||
}) | ||
|
||
it("sets 2 distant values", () => { | ||
const bs = new BitSet(); | ||
bs.set(67); | ||
bs.set(241); | ||
expect(bs.length).toEqual(2); | ||
expect(bs.get(67)).toBeTrue(); | ||
expect(bs.get(241)).toBeTrue(); | ||
}) | ||
|
||
it("combines 2 identical sets", () => { | ||
const bs1 = new BitSet(); | ||
bs1.set(67); | ||
const bs2 = new BitSet(); | ||
bs2.set(67); | ||
bs1.or(bs2); | ||
expect(bs1.length).toEqual(1); | ||
expect(bs1.get(67)).toBeTrue(); | ||
}) | ||
|
||
it("combines 2 distinct sets", () => { | ||
const bs1 = new BitSet(); | ||
bs1.set(67); | ||
const bs2 = new BitSet(); | ||
bs2.set(69); | ||
bs1.or(bs2); | ||
expect(bs1.length).toEqual(2); | ||
expect(bs1.get(67)).toBeTrue(); | ||
expect(bs1.get(69)).toBeTrue(); | ||
}) | ||
|
||
it("combines 2 overlapping sets", () => { | ||
const bs1 = new BitSet(); | ||
bs1.set(67); | ||
bs1.set(69); | ||
const bs2 = new BitSet(); | ||
bs2.set(69); | ||
bs2.set(71); | ||
bs1.or(bs2); | ||
expect(bs1.length).toEqual(3); | ||
expect(bs1.get(67)).toBeTrue(); | ||
expect(bs1.get(69)).toBeTrue(); | ||
expect(bs1.get(71)).toBeTrue(); | ||
}) | ||
|
||
it("returns values", () => { | ||
const bs = new BitSet(); | ||
bs.set(67); | ||
bs.set(69); | ||
const values = bs.values(); | ||
expect(values).toEqual([67, 69]); | ||
}) | ||
|
||
it("counts bits", () => { | ||
for(let i= 0; i <= 0xFF; i++) { | ||
// count bits the slow but easy to understand way (Kernighan method) | ||
let count1 = 0; | ||
let value = i; | ||
while(value) { | ||
if(value & 1) | ||
count1++; | ||
value >>= 1; | ||
} | ||
// count bits the fast way | ||
const count2 = BitSet._bitCount(i); | ||
expect(count2).toEqual(count1); | ||
} | ||
}) | ||
}) |
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
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); | ||
}) | ||
|
||
}) |
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
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
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
Oops, something went wrong.