all files / spec/ weighted-dictionary.spec.1.ts

100% Statements 76/76
100% Branches 0/0
100% Functions 21/21
100% Lines 72/72
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94                                     11×        
import { WeightedDictionary } from "../src/weighted-dictionary";
 
describe("WeightedDictionary<T>", () => {
    describe("constructor", () => {
        it("Empty dictionary has weight of zero", function () {
            const dict = new WeightedDictionary<string>();
            expect(dict.totalWeight).toBe(0);
        });
    });
 
    describe("setValue", () => {
        it("Adding new value increments weight", function () {
            const dict = new WeightedDictionary<string>();
            dict.setValue("test", 4);
            expect(dict.totalWeight).toBe(4);
        });
        it("Adding multiple values increments weight", function () {
            const dict = new WeightedDictionary<string>();
            dict.setValue("test1", 4);
            dict.setValue("test2", 2);
            dict.setValue("test3", 3);
            expect(dict.totalWeight).toBe(9);
        });
        it("Resetting existing value has correct weight", function () {
            const dict = new WeightedDictionary<string>();
            dict.setValue("test1", 4);
            dict.setValue("test2", 2);
            dict.setValue("test3", 3);
 
            dict.setValue("test1", 1);
 
            expect(dict.totalWeight).toBe(6);
        });
    });
    describe("incrementValue", () => {
        it("element weight increments", function () {
            const dict = new WeightedDictionary<string>();
            dict.setValue("test", 4);
            dict.incrementValue("test", 3);
            expect(dict.getValue("test")).toBe(7);
        });
        it("Total weight increments", function () {
            const dict = new WeightedDictionary<string>();
            dict.setValue("test1", 4);
            dict.setValue("test2", 2);
            dict.setValue("test3", 3);
            dict.incrementValue("test2", 5);
            expect(dict.totalWeight).toBe(14);
        });
    });
    describe("remove", () => {
        it("Removing value decrements weight", function () {
            const dict = new WeightedDictionary<string>();
            dict.setValue("test1", 4);
            dict.setValue("test2", 2);
            dict.setValue("test3", 3);
            dict.remove("test2");
            expect(dict.totalWeight).toBe(7);
        });
    });
    describe("toStrFunction", () => {
        it("setValue respects toStrFunction", function () {
            const dict = new WeightedDictionary<string>(k => k.toLowerCase());
            dict.setValue("TEST", 1);
            dict.setValue("test", 2);
            dict.setValue("teST", 3);
            expect(dict.size()).toBe(1);
        });
        it("getValue respects toStrFunction", function () {
            const dict = new WeightedDictionary<string>(k => k.toLowerCase());
            dict.setValue("TEST", 1);
            dict.setValue("test", 2);
            dict.setValue("teST", 3);
            expect(dict.getValue("TEst")).toBe(3);
        });
        it("remove respects toStrFunction", function () {
            const dict = new WeightedDictionary<string>(k => k.toLowerCase());
            dict.setValue("TEST", 1);
            dict.setValue("test", 2);
            dict.setValue("teST", 3);
            dict.remove("TEst");
            expect(dict.size()).toBe(0);
        });
        it("incrementValue respects toStrFunction", function () {
            const dict = new WeightedDictionary<string>(k => k.toLowerCase());
            dict.setValue("TEST", 1);
            dict.incrementValue("test", 2);
            dict.incrementValue("teST", 3);
            dict.incrementValue("TEst", 7);
            expect(dict.getValue("tESt")).toBe(13);
        });
    });
});