65 lines
1.3 KiB
TypeScript
65 lines
1.3 KiB
TypeScript
import { expect, test } from "vitest";
|
|
import {
|
|
createDictionary,
|
|
type Dictionary,
|
|
deleteEntrySave,
|
|
forEntry,
|
|
getEntry,
|
|
setEntrySave,
|
|
} from "./Dictionary.ts";
|
|
|
|
test("Dictionary", () => {
|
|
const dict: Dictionary<string, number> = createDictionary([
|
|
["a", 1],
|
|
["b", 2],
|
|
["c", 3],
|
|
]);
|
|
setEntrySave(dict, "foo", 42);
|
|
setEntrySave(dict, "bar", 0);
|
|
deleteEntrySave(dict, "bar");
|
|
|
|
expect(getEntry(dict, "foo")).toBe(42);
|
|
expect(getEntry(dict, "bar")).toBe(undefined);
|
|
expect(dict.indices.size).toBe(dict.entries.length);
|
|
|
|
let sum = 0;
|
|
forEntry(dict, (value) => {
|
|
sum += value;
|
|
});
|
|
expect(sum).toBe(48);
|
|
});
|
|
|
|
test("Dictionary don't mutate, when used 'save', while iterating", () => {
|
|
const dict: Dictionary<string, number> = createDictionary([
|
|
["a", 1],
|
|
["b", 2],
|
|
["c", 3],
|
|
]);
|
|
|
|
let sum = 0;
|
|
forEntry(dict, (value, _, index) => {
|
|
if (index < 1) {
|
|
setEntrySave(dict, "d", 4);
|
|
setEntrySave(dict, "e", 5);
|
|
}
|
|
|
|
sum += value;
|
|
});
|
|
|
|
expect(sum).toBe(6);
|
|
expect(dict.indices.size === 5).toEqual(dict.entries.length === 5);
|
|
|
|
sum = 0;
|
|
forEntry(dict, (value, _, index) => {
|
|
if (index < 1) {
|
|
deleteEntrySave(dict, "d");
|
|
deleteEntrySave(dict, "e");
|
|
}
|
|
|
|
sum += value;
|
|
});
|
|
|
|
expect(sum).toBe(15);
|
|
expect(dict.indices.size === 3).toEqual(dict.entries.length === 3);
|
|
});
|