Testing KnapsackItem Class Implementation in javascript-algorithms
This test suite validates the KnapsackItem class implementation, focusing on item creation, value/weight calculations, and quantity management. The tests ensure proper functionality of a core component used in solving the classic knapsack optimization problem.
Test Coverage Overview
Implementation Analysis
Technical Details
Best Practices Demonstrated
trekhleb/javascript-algorithms
src/algorithms/sets/knapsack-problem/__test__/KnapsackItem.test.js
import KnapsackItem from '../KnapsackItem';
describe('KnapsackItem', () => {
it('should create knapsack item and count its total weight and value', () => {
const knapsackItem = new KnapsackItem({ value: 3, weight: 2 });
expect(knapsackItem.value).toBe(3);
expect(knapsackItem.weight).toBe(2);
expect(knapsackItem.quantity).toBe(1);
expect(knapsackItem.valuePerWeightRatio).toBe(1.5);
expect(knapsackItem.toString()).toBe('v3 w2 x 1');
expect(knapsackItem.totalValue).toBe(3);
expect(knapsackItem.totalWeight).toBe(2);
knapsackItem.quantity = 0;
expect(knapsackItem.value).toBe(3);
expect(knapsackItem.weight).toBe(2);
expect(knapsackItem.quantity).toBe(0);
expect(knapsackItem.valuePerWeightRatio).toBe(1.5);
expect(knapsackItem.toString()).toBe('v3 w2 x 0');
expect(knapsackItem.totalValue).toBe(0);
expect(knapsackItem.totalWeight).toBe(0);
knapsackItem.quantity = 2;
expect(knapsackItem.value).toBe(3);
expect(knapsackItem.weight).toBe(2);
expect(knapsackItem.quantity).toBe(2);
expect(knapsackItem.valuePerWeightRatio).toBe(1.5);
expect(knapsackItem.toString()).toBe('v3 w2 x 2');
expect(knapsackItem.totalValue).toBe(6);
expect(knapsackItem.totalWeight).toBe(4);
});
});