CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
sagemathinc

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/util/knapsack.test.ts
Views: 687
1
import knapsack from "@cocalc/util/knapsack";
2
3
describe("knapsack", () => {
4
const input = {
5
banana: { cost: 5, benefit: 33 },
6
apple: { cost: 1, benefit: 12 },
7
kiwi: { cost: 1, benefit: 7 },
8
};
9
10
it("should return the correct result for the first example", () => {
11
const maxCost = 7;
12
const expectedOutput = {
13
items: ["kiwi", "apple", "banana"],
14
cost: 7,
15
benefit: 52,
16
};
17
const output = knapsack(input, maxCost);
18
expect(output).toEqual(expectedOutput);
19
});
20
21
it("should return the correct result for the second example", () => {
22
const maxCost = 2;
23
const expectedOutput = {
24
items: ["kiwi", "apple"],
25
cost: 2,
26
benefit: 19,
27
};
28
const output = knapsack(input, maxCost);
29
expect(output).toEqual(expectedOutput);
30
});
31
32
it("should return the correct result for the third example", () => {
33
const maxCost = 7;
34
const expectedOutput = {
35
items: ["kiwi", "apple", "banana"],
36
cost: 7,
37
benefit: 52,
38
};
39
const output = knapsack(input, maxCost);
40
expect(output).toEqual(expectedOutput);
41
});
42
43
it("should return the correct result for the fourth example", () => {
44
const maxCost = 20;
45
const expectedOutput = {
46
items: ["kiwi", "apple", "banana"],
47
cost: 7,
48
benefit: 52,
49
};
50
const output = knapsack(input, maxCost);
51
expect(output).toEqual(expectedOutput);
52
});
53
54
it("should return the correct result for the fifth example", () => {
55
const maxCost = 6;
56
const expectedOutput = {
57
items: ["apple", "banana"],
58
cost: 6,
59
benefit: 45,
60
};
61
const output = knapsack(input, maxCost);
62
expect(output).toEqual(expectedOutput);
63
});
64
});
65
66