forked from m426-2026/math
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfraction_test.ts
More file actions
72 lines (54 loc) · 1.41 KB
/
fraction_test.ts
File metadata and controls
72 lines (54 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import { assertAlmostEquals, assertEquals } from "@std/assert";
import { Fraction } from "./fraction.ts";
Deno.test("fraction of 1/1 is 1.0", () => {
// Arrange
const fraction = new Fraction(1, 1);
// Act
const float = fraction.toFloat(0.1);
// Assert
assertEquals(float, 1.0);
});
Deno.test("fraction of 2/3 is roughly 0.67", () => {
// Arrange
const fraction = new Fraction(2, 3);
// Act
const float = fraction.toFloat(0.01);
// Assert
assertAlmostEquals(float, 0.67);
});
Deno.test("1/3 + 2/6 = 2/3 is roughly 0.67", () => {
// Arrange
const left = new Fraction(1, 3);
const right = new Fraction(2, 6);
// Act
left.add(right);
// Assert
assertAlmostEquals(left.toFloat(0.01), 0.67);
});
Deno.test("3/3 - 1/3 = 2/3 is roughly 0.67", () => {
// Arrange
const left = new Fraction(3, 3);
const right = new Fraction(1, 3);
// Act
left.subtract(right);
// Assert
assertAlmostEquals(left.toFloat(0.01), 0.67);
});
Deno.test("2/3 * 2/3 = 4/9 is roughly 0.444", () => {
// Arrange
const left = new Fraction(2, 3);
const right = new Fraction(2, 3);
// Act
left.multiply(right);
// Assert
assertAlmostEquals(left.toFloat(0.001), 0.444);
});
Deno.test("10/3 / 2/3 = 5/3 is roughly 2.0", () => {
// Arrange
const left = new Fraction(10, 3);
const right = new Fraction(5, 3);
// Act
left.divide(right);
// Assert
assertAlmostEquals(left.toFloat(0.1), 2.0);
});