Skip to content

Commit b73ef69

Browse files
committed
more ut for knapsack
1 parent 47836c2 commit b73ef69

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

src/algorithms/knapsack-fractional.spec.js

+22
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,26 @@ describe('solveFractionalKnapsack', () => {
4242
expect(knapsack.value).toBeCloseTo(6 + 7 + 4);
4343
expect(knapsack.items.length).toEqual(3);
4444
});
45+
46+
it('should take none if max is 0', () => {
47+
const maxWeight = 0;
48+
const items = [
49+
{ value: 1, weight: 1 }, // 1/1 = 1
50+
];
51+
const knapsack = solveFractionalKnapsack(items, maxWeight);
52+
expect(knapsack.items.length).toEqual(0);
53+
expect(knapsack.weight).toBeCloseTo(0);
54+
expect(knapsack.value).toBeCloseTo(0);
55+
});
56+
57+
it('should take all if capacity allows it', () => {
58+
const maxWeight = 10;
59+
const items = [
60+
{ value: 1, weight: 1 }, // 1/1 = 1
61+
];
62+
const knapsack = solveFractionalKnapsack(items, maxWeight);
63+
expect(knapsack.items.length).toEqual(1);
64+
expect(knapsack.weight).toBeCloseTo(1);
65+
expect(knapsack.value).toBeCloseTo(1);
66+
});
4567
});

0 commit comments

Comments
 (0)