From 213870f87f1f4fbe6c1bf374a36bbaaa12208056 Mon Sep 17 00:00:00 2001 From: Mike Lischke Date: Tue, 19 Nov 2024 09:48:59 +0100 Subject: [PATCH] Fixed addition bug in OrderedHashSet. Added unit tests for that class. Signed-off-by: Mike Lischke --- src/misc/OrderedHashSet.ts | 10 ---- tests/api/OrderedHashSet.spec.ts | 89 ++++++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+), 10 deletions(-) create mode 100644 tests/api/OrderedHashSet.spec.ts diff --git a/src/misc/OrderedHashSet.ts b/src/misc/OrderedHashSet.ts index dcfa04e..524c6a3 100644 --- a/src/misc/OrderedHashSet.ts +++ b/src/misc/OrderedHashSet.ts @@ -28,16 +28,6 @@ export class OrderedHashSet extends HashSet { 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 = []; diff --git a/tests/api/OrderedHashSet.spec.ts b/tests/api/OrderedHashSet.spec.ts new file mode 100644 index 0000000..a5edeb3 --- /dev/null +++ b/tests/api/OrderedHashSet.spec.ts @@ -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(); + 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(); + 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(); + 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); + }); +});