From c051368fd7feae156d6a54b7e48ddc0dc16d5c47 Mon Sep 17 00:00:00 2001 From: Phoenix The Fallen Date: Thu, 6 May 2021 15:54:04 +0300 Subject: [PATCH] Fix tests and clean up --- ts/bi-map.ts | 159 +++++++++++++++------------------------- ts/tests/bi-map.test.ts | 84 ++++++++++----------- 2 files changed, 102 insertions(+), 141 deletions(-) diff --git a/ts/bi-map.ts b/ts/bi-map.ts index 33b8acf..15b5495 100644 --- a/ts/bi-map.ts +++ b/ts/bi-map.ts @@ -11,103 +11,64 @@ * @version v0.1.0 * @since v0.1.0 */ + export class BiMap { - - private primaryMap: Map; - - private secondaryMap: Map; - - public constructor() { - - this.primaryMap = new Map(); - this.secondaryMap = new Map(); - - } - - public get(key: K): V | undefined { - - return this.getFromKey(key); - - } - - public set(key: K, value: V): void { - - this.setFromKey(key, value); - - } - - public getFromKey(key: K): V | undefined { - - return this.primaryMap.get(key); - - } - - public getFromValue(value: V): K | undefined { - - return this.secondaryMap.get(value); - - } - - public setFromKey(key: K, value: V): void { - - this.primaryMap.set(key, value); - this.secondaryMap.set(value, key); - - } - - public setFromValue(value: V, key: K): void { - - this.setFromKey(key, value); - - } - - public removeByKey(key: K): V | undefined { - - if (this.primaryMap.has(key)) { - - let value: V = this.primaryMap.get(key) as V; - - this.primaryMap.delete(key); - this.secondaryMap.delete(value); - - return value; - - } else return undefined; - - } - - public removeByValue(value: V): K | undefined { - - if (this.secondaryMap.has(value)) { - - let key: K = this.secondaryMap.get(value) as K; - - this.primaryMap.delete(key); - this.secondaryMap.delete(value); - - return key; - - } else return undefined; - - } - - public hasKey(key: K): boolean { - - return this.primaryMap.has(key); - - } - - public hasValue(value: V): boolean { - - return this.secondaryMap.has(value); - - } - - public clear(): void { - - this.primaryMap.clear(); - this.secondaryMap.clear(); - - } - -} \ No newline at end of file + private primaryMap = new Map(); + private secondaryMap = new Map(); + + public get(key: K): V | undefined { + return this.getFromKey(key); + } + + public set(key: K, value: V): void { + const _val = this.primaryMap.get(key); + const _key = this.secondaryMap.get(value); + if (_val) this.secondaryMap.delete(_val); + if (_key) this.primaryMap.delete(_key); + this.primaryMap.set(key, value); + this.secondaryMap.set(value, key); + } + + public getFromKey(key: K): V | undefined { + return this.primaryMap.get(key); + } + + public getFromValue(value: V): K | undefined { + return this.secondaryMap.get(value); + } + + public removeByKey(key: K): V | undefined { + const value = this.primaryMap.get(key); + + if (value !== undefined) { + this.primaryMap.delete(key); + this.secondaryMap.delete(value); + } + + return value; + } + + public removeByValue(value: V): K | undefined { + const key = this.secondaryMap.get(value); + + if (key !== undefined) { + this.primaryMap.delete(key); + this.secondaryMap.delete(value); + } + + return key; + } + + public hasKey(key: K): boolean { + return this.primaryMap.has(key); + } + + public hasValue(value: V): boolean { + return this.secondaryMap.has(value); + } + + public clear(): void { + this.primaryMap.clear(); + this.secondaryMap.clear(); + } +} diff --git a/ts/tests/bi-map.test.ts b/ts/tests/bi-map.test.ts index f9ede74..bda3220 100644 --- a/ts/tests/bi-map.test.ts +++ b/ts/tests/bi-map.test.ts @@ -96,56 +96,56 @@ describe("Per-method Tests", () => { }); }); - - describe("#getFromValue", () => { - - test("", () => { - - + + describe("#removeByKey", () => { + test("Regular key --> returns deleted value.", () => { + bimap = new BiMap(); + bimap.set("one", 1); + + expect(bimap.hasKey("one")).toBe(true); + expect(bimap.hasKey("two")).toBe(false); + expect(bimap.removeByKey("one")).toBe(1); + expect(bimap.hasKey("one")).toBe(false); + expect(bimap.getFromKey("one")).toBeUndefined(); }); }); - - describe("#setFromKey", () => { - - - - }); - - describe("#setFromValue", () => { - - - - }); - - describe("#removeByKey", () => { - - - - }); - + describe("#removeByValue", () => { - - - - }); - - describe("#hasKey", () => { - - - - }); - - describe("#hasValue", () => { - - + + test("Regular key --> returns deleted value.", () => { + bimap = new BiMap(); + bimap.set("one", 1); + + expect(bimap.hasValue(1)).toBe(true); + expect(bimap.hasValue(2)).toBe(false); + expect(bimap.removeByValue(1)).toBe("one"); + expect(bimap.hasValue(1)).toBe(false); + expect(bimap.getFromValue(1)).toBeUndefined(); + }); }); - + describe("#clear", () => { - - + + test("nukes all assoc", () => { + bimap = new BiMap(); + bimap.set("one", 1); + bimap.set("two", 2); + + expect(bimap.hasKey("one")).toBe(true); + expect(bimap.hasKey("two")).toBe(true); + expect(bimap.hasValue(1)).toBe(true); + expect(bimap.hasValue(2)).toBe(true); + + bimap.clear() + + expect(bimap.hasKey("one")).toBe(false); + expect(bimap.hasKey("two")).toBe(false); + expect(bimap.hasValue(1)).toBe(false); + expect(bimap.hasValue(2)).toBe(false); + }); });