Skip to content

Commit cad9dd1

Browse files
committed
2-is-proper-fraction.test.js committed
1 parent f0b6924 commit cad9dd1

File tree

1 file changed

+198
-0
lines changed

1 file changed

+198
-0
lines changed

Sprint-3/1-implement-and-rewrite-tests/rewrite-tests-with-jest/2-is-proper-fraction.test.js

Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/**
12
// This statement loads the isProperFraction function you wrote in the implement directory.
23
// We will use the same function, but write tests for it using Jest in this file.
34
const isProperFraction = require("../implement/2-is-proper-fraction");
@@ -8,3 +9,200 @@ const isProperFraction = require("../implement/2-is-proper-fraction");
89
test(`should return false when denominator is zero`, () => {
910
expect(isProperFraction(1, 0)).toEqual(false);
1011
});
12+
13+
// Implement a function isProperFraction,
14+
// when given two numbers, a numerator and a denominator, it should return true if
15+
// the given numbers form a proper fraction, and false otherwise.
16+
17+
// Assumption: The parameters are valid numbers (not NaN or Infinity).
18+
19+
// Note: If you are unfamiliar with proper fractions, please look up its mathematical definition.
20+
21+
// Acceptance criteria:
22+
// After you have implemented the function, write tests to cover all the cases, and
23+
// execute the code to ensure all tests pass.
24+
25+
function isProperFraction(numerator, denominator) {
26+
// A proper fraction is defined as a fraction where the absolute value of the numerator
27+
// is less than the absolute value of the denominator, and the denominator is not zero.
28+
// Additionally, both numerator and denominator must be non-zero (zero numerator is acceptable
29+
// as 0/5 = 0, which is less than 1, but we need to check definition)
30+
31+
// Check for invalid denominator (zero)
32+
if (denominator === 0) {
33+
return false;
34+
}
35+
36+
// Get absolute values for comparison
37+
const absNumerator = Math.abs(numerator);
38+
const absDenominator = Math.abs(denominator);
39+
40+
// A proper fraction has |numerator| < |denominator|
41+
// Note: 0/5 is considered a proper fraction as 0 < 5
42+
// Negative numbers are also considered: -2/3 is proper because |-2| < |3|
43+
return absNumerator < absDenominator;
44+
}
45+
46+
// The line below allows us to load the isProperFraction function into tests in other files.
47+
// This will be useful in the "rewrite tests with jest" step.
48+
module.exports = isProperFraction;
49+
50+
// Here's our helper again
51+
function assertEquals(actualOutput, targetOutput) {
52+
console.assert(
53+
actualOutput === targetOutput,
54+
`Expected ${actualOutput} to equal ${targetOutput}`
55+
);
56+
}
57+
58+
// Write tests to cover all cases.
59+
console.log("Running tests for isProperFraction function...\n");
60+
61+
// Test 1: Basic proper fractions with positive numbers
62+
console.log("Testing proper fractions with positive numbers:");
63+
assertEquals(isProperFraction(1, 2), true); // 1/2 is proper
64+
assertEquals(isProperFraction(3, 4), true); // 3/4 is proper
65+
assertEquals(isProperFraction(5, 8), true); // 5/8 is proper
66+
assertEquals(isProperFraction(1, 100), true); // 1/100 is proper
67+
68+
// Test 2: Fractions that are NOT proper (numerator >= denominator)
69+
console.log("\nTesting improper fractions with positive numbers:");
70+
assertEquals(isProperFraction(2, 2), false); // 2/2 = 1 (equal)
71+
assertEquals(isProperFraction(3, 2), false); // 3/2 > 1
72+
assertEquals(isProperFraction(5, 3), false); // 5/3 > 1
73+
assertEquals(isProperFraction(10, 1), false); // 10/1 > 1
74+
75+
// Test 3: Fractions with zero numerator
76+
console.log("\nTesting fractions with zero numerator:");
77+
assertEquals(isProperFraction(0, 5), true); // 0/5 = 0 (proper)
78+
assertEquals(isProperFraction(0, 1), true); // 0/1 = 0 (proper)
79+
assertEquals(isProperFraction(0, 100), true); // 0/100 = 0 (proper)
80+
81+
// Test 4: Fractions with negative numerator
82+
console.log("\nTesting fractions with negative numerator:");
83+
assertEquals(isProperFraction(-1, 2), true); // |-1| < |2| -> proper
84+
assertEquals(isProperFraction(-3, 4), true); // |-3| < |4| -> proper
85+
assertEquals(isProperFraction(-5, 3), false); // |-5| > |3| -> improper
86+
assertEquals(isProperFraction(-2, 2), false); // |-2| = |2| -> improper
87+
88+
// Test 5: Fractions with negative denominator
89+
console.log("\nTesting fractions with negative denominator:");
90+
assertEquals(isProperFraction(1, -2), true); // |1| < |-2| -> proper
91+
assertEquals(isProperFraction(3, -4), true); // |3| < |-4| -> proper
92+
assertEquals(isProperFraction(5, -3), false); // |5| > |-3| -> improper
93+
assertEquals(isProperFraction(2, -2), false); // |2| = |-2| -> improper
94+
95+
// Test 6: Fractions with both numerator and denominator negative
96+
console.log("\nTesting fractions with both numbers negative:");
97+
assertEquals(isProperFraction(-1, -2), true); // |-1| < |-2| -> proper
98+
assertEquals(isProperFraction(-3, -4), true); // |-3| < |-4| -> proper
99+
assertEquals(isProperFraction(-5, -3), false); // |-5| > |-3| -> improper
100+
assertEquals(isProperFraction(-2, -2), false); // |-2| = |-2| -> improper
101+
102+
// Test 7: Fractions with numerator zero and negative denominator
103+
console.log("\nTesting fractions with zero numerator and negative denominator:");
104+
assertEquals(isProperFraction(0, -5), true); // |0| < |-5| -> proper
105+
assertEquals(isProperFraction(0, -1), true); // |0| < |-1| -> proper
106+
107+
// Test 8: Edge cases with denominator zero
108+
console.log("\nTesting denominator equals zero:");
109+
assertEquals(isProperFraction(1, 0), false); // Denominator zero -> invalid
110+
assertEquals(isProperFraction(0, 0), false); // Both zero -> invalid
111+
assertEquals(isProperFraction(-5, 0), false); // Denominator zero -> invalid
112+
113+
// Test 9: Fractions with large numbers
114+
console.log("\nTesting with large numbers:");
115+
assertEquals(isProperFraction(1000, 1001), true); // 1000/1001 is proper
116+
assertEquals(isProperFraction(1000, 1000), false); // Equal -> improper
117+
assertEquals(isProperFraction(1001, 1000), false); // Numerator larger -> improper
118+
119+
console.log("\nAll tests completed!");
120+
*
121+
*/
122+
123+
// This statement loads the isProperFraction function you wrote in the implement directory.
124+
// We will use the same function, but write tests for it using Jest in this file.
125+
const isProperFraction = require("../implement/2-is-proper-fraction");
126+
127+
// Write tests in Jest syntax to cover all combinations
128+
129+
describe('isProperFraction function', () => {
130+
131+
describe('Proper fractions with positive numbers', () => {
132+
test('should return true when numerator < denominator', () => {
133+
expect(isProperFraction(1, 2)).toBe(true);
134+
expect(isProperFraction(3, 4)).toBe(true);
135+
expect(isProperFraction(5, 8)).toBe(true);
136+
expect(isProperFraction(1, 100)).toBe(true);
137+
});
138+
});
139+
140+
describe('Improper fractions with positive numbers', () => {
141+
test('should return false when numerator >= denominator', () => {
142+
expect(isProperFraction(2, 2)).toBe(false); // Equal
143+
expect(isProperFraction(3, 2)).toBe(false); // Numerator larger
144+
expect(isProperFraction(5, 3)).toBe(false); // Numerator larger
145+
expect(isProperFraction(10, 1)).toBe(false); // Numerator larger
146+
});
147+
});
148+
149+
describe('Fractions with zero numerator', () => {
150+
test('should return true when numerator is zero and denominator non-zero', () => {
151+
expect(isProperFraction(0, 5)).toBe(true);
152+
expect(isProperFraction(0, 1)).toBe(true);
153+
expect(isProperFraction(0, 100)).toBe(true);
154+
});
155+
});
156+
157+
describe('Fractions with negative numerator', () => {
158+
test('should compare absolute values when numerator is negative', () => {
159+
expect(isProperFraction(-1, 2)).toBe(true); // |-1| < |2|
160+
expect(isProperFraction(-3, 4)).toBe(true); // |-3| < |4|
161+
expect(isProperFraction(-5, 3)).toBe(false); // |-5| > |3|
162+
expect(isProperFraction(-2, 2)).toBe(false); // |-2| = |2|
163+
});
164+
});
165+
166+
describe('Fractions with negative denominator', () => {
167+
test('should compare absolute values when denominator is negative', () => {
168+
expect(isProperFraction(1, -2)).toBe(true); // |1| < |-2|
169+
expect(isProperFraction(3, -4)).toBe(true); // |3| < |-4|
170+
expect(isProperFraction(5, -3)).toBe(false); // |5| > |-3|
171+
expect(isProperFraction(2, -2)).toBe(false); // |2| = |-2|
172+
});
173+
});
174+
175+
describe('Fractions with both numbers negative', () => {
176+
test('should compare absolute values when both are negative', () => {
177+
expect(isProperFraction(-1, -2)).toBe(true); // |-1| < |-2|
178+
expect(isProperFraction(-3, -4)).toBe(true); // |-3| < |-4|
179+
expect(isProperFraction(-5, -3)).toBe(false); // |-5| > |-3|
180+
expect(isProperFraction(-2, -2)).toBe(false); // |-2| = |-2|
181+
});
182+
});
183+
184+
describe('Fractions with zero numerator and negative denominator', () => {
185+
test('should return true for 0/negative denominator', () => {
186+
expect(isProperFraction(0, -5)).toBe(true); // |0| < |-5|
187+
expect(isProperFraction(0, -1)).toBe(true); // |0| < |-1|
188+
});
189+
});
190+
191+
describe('Denominator equals zero', () => {
192+
test('should return false when denominator is zero', () => {
193+
expect(isProperFraction(1, 0)).toBe(false); // Division by zero
194+
expect(isProperFraction(0, 0)).toBe(false); // Both zero
195+
expect(isProperFraction(-5, 0)).toBe(false); // Denominator zero
196+
});
197+
});
198+
199+
describe('Large numbers', () => {
200+
test('should handle large numbers correctly', () => {
201+
expect(isProperFraction(1000, 1001)).toBe(true); // Proper
202+
expect(isProperFraction(1000, 1000)).toBe(false); // Equal
203+
expect(isProperFraction(1001, 1000)).toBe(false); // Improper
204+
});
205+
});
206+
207+
});
208+

0 commit comments

Comments
 (0)