Skip to content

Commit 801188b

Browse files
authored
Update WaterJugProblem.test.js
1 parent a7ed8dc commit 801188b

File tree

1 file changed

+39
-20
lines changed

1 file changed

+39
-20
lines changed

Diff for: Recursive/test/WaterJugProblem.test.js

+39-20
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,46 @@
1-
const canMeasureWater = require('./Recursive/Water_jug_Problem');
1+
import { canMeasureWater } from '../WaterJugProblem'
22

33
describe('Water Jug Problem', () => {
4-
test('should return true when target amount is achievable', () => {
5-
expect(canMeasureWater(3, 5, 4)).toBe(true); // Known solution exists
6-
});
4+
5+
// Test case 1: Valid input where water can be measured
6+
it('should return true for values x=3, y=5, z=4', () => {
7+
expect(canMeasureWater(3, 5, 4)).toBe(true)
8+
})
79

8-
test('should return false when target amount is not achievable', () => {
9-
expect(canMeasureWater(2, 6, 5)).toBe(false); // Impossible to measure 5 using 2 and 6
10-
});
10+
// Test case 2: Valid input where water cannot be measured
11+
it('should return false for values x=2, y=6, z=5', () => {
12+
expect(canMeasureWater(2, 6, 5)).toBe(false)
13+
})
1114

12-
test('should return true when one of the jugs exactly matches the target', () => {
13-
expect(canMeasureWater(3, 5, 5)).toBe(true); // Exact match with jug 2
14-
});
15+
// Test case 3: Measure exact amount of water in one jug
16+
it('should return true for values x=5, y=3, z=5', () => {
17+
expect(canMeasureWater(5, 3, 5)).toBe(true)
18+
})
1519

16-
test('should return true when both jugs are enough to make the target', () => {
17-
expect(canMeasureWater(4, 3, 7)).toBe(true); // Combined total equals target
18-
});
20+
// Test case 4: Invalid inputs (negative or non-integer)
21+
it('Throw Error for Invalid Input', () => {
22+
expect(() => canMeasureWater(-3, 5, 4)).toThrow(
23+
'Input should be non-negative whole numbers'
24+
)
25+
expect(() => canMeasureWater(3, null, 4)).toThrow(
26+
'Input should be non-negative whole numbers'
27+
)
28+
expect(() => canMeasureWater(3, 5, 2.5)).toThrow(
29+
'Input should be non-negative whole numbers'
30+
)
31+
expect(() => canMeasureWater('a', 5, 4)).toThrow(
32+
'Input should be non-negative whole numbers'
33+
)
34+
})
1935

20-
test('should return false when target amount exceeds both jug capacities combined', () => {
21-
expect(canMeasureWater(3, 5, 9)).toBe(false); // 9 exceeds the total capacity (3 + 5)
22-
});
36+
// Test case 5: Edge case where z is 0
37+
it('should return true for values x=3, y=5, z=0', () => {
38+
expect(canMeasureWater(3, 5, 0)).toBe(true)
39+
})
2340

24-
test('should return true for zero target', () => {
25-
expect(canMeasureWater(3, 5, 0)).toBe(true); // It's always possible to measure 0
26-
});
27-
});
41+
// Test case 6: Edge case where z equals the sum of both jugs
42+
it('should return true for values x=3, y=5, z=8', () => {
43+
expect(canMeasureWater(3, 5, 8)).toBe(true)
44+
})
45+
46+
})

0 commit comments

Comments
 (0)