Skip to content

Commit

Permalink
Fixed addition bug in OrderedHashSet.
Browse files Browse the repository at this point in the history
Added unit tests for that class.

Signed-off-by: Mike Lischke <[email protected]>
  • Loading branch information
mike-lischke committed Nov 19, 2024
1 parent 0d1b951 commit 213870f
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 10 deletions.
10 changes: 0 additions & 10 deletions src/misc/OrderedHashSet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,6 @@ export class OrderedHashSet<T extends IComparable> extends HashSet<T> {
return super.equals(o);
}

public override add(element: T): boolean {
if (super.add(element)) {
this.#elements.push(element);

return true;
}

return false;
}

public override clear(): void {
super.clear();
this.#elements = [];
Expand Down
89 changes: 89 additions & 0 deletions tests/api/OrderedHashSet.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* Copyright (c) 2012-2017 The ANTLR Project. All rights reserved.
* Use of this file is governed by the BSD 3-clause license that
* can be found in the LICENSE.txt file in the project root.
*/

import { describe, expect, it } from "vitest";

import { OrderedHashSet, type IComparable } from "../../src/index.js";

class TestClass implements IComparable {
#value: number;

public constructor(value: number) {
this.#value = value;
}

public equals(o: TestClass): boolean {
return this.#value === o.#value;
}

public hashCode(): number {
return this.#value;
}
};

describe("TestOrderedHashSet", () => {
it("general", () => {
const set = new OrderedHashSet<TestClass>();
expect(set.size).toBe(0);

const obj1 = new TestClass(1);
const obj2 = new TestClass(2);
const obj3 = new TestClass(3);
const obj4 = new TestClass(4);

set.add(obj1);
set.add(obj2);
set.add(obj3);
set.add(obj4);

expect(set.size).toBe(4);
expect(set.toArray()).toEqual([obj1, obj2, obj3, obj4]);
});

it("duplicates and order", () => {
const set = new OrderedHashSet<TestClass>();
expect(set.size).toBe(0);

const obj1 = new TestClass(1);
const obj2 = new TestClass(2);
const obj3 = new TestClass(3);
const obj4 = new TestClass(4);
set.addAll([obj1, obj2, obj3, obj4, obj1, obj2, obj3, obj4]);

expect(set.size).toBe(4);
expect(set.toArray()).toEqual([obj1, obj2, obj3, obj4]);

set.clear();
expect(set.size).toBe(0);

set.addAll([obj4, obj3, obj2, obj1, obj1, obj2, obj2, obj1]);
expect(set.size).toBe(4);
expect(set.toArray()).toEqual([obj4, obj3, obj2, obj1]);
});

it("iterator", () => {
const set = new OrderedHashSet<TestClass>();
expect(set.size).toBe(0);

const obj1 = new TestClass(1);
const obj2 = new TestClass(2);
const obj3 = new TestClass(3);
const obj4 = new TestClass(4);

set.add(obj1);
set.add(obj2);
set.add(obj3);
set.add(obj4);

expect(set.size).toBe(4);
const iter = set[Symbol.iterator]();
expect(iter.next().value).toBe(obj1);
expect(iter.next().value).toBe(obj2);
expect(iter.next().value).toBe(obj3);
expect(iter.next().value).toBe(obj4);
expect(iter.next().done).toBe(true);
});
});

0 comments on commit 213870f

Please sign in to comment.