Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .geometry_test.ts.swp
Binary file not shown.
15 changes: 13 additions & 2 deletions fraction.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
import { roundTo } from "./utils.ts";
import { gcdBruteForce, roundTo } from "./utils.ts";

export class Fraction {
constructor(
private numerator: number,
private denominator: number,
) {}
) {
if (denominator === 0) {
throw new Error("denominator cannot be zero");
}
this.cancel();
}

public cancel(): void {
const gcd = gcdBruteForce(Math.abs(this.numerator), Math.abs(this.denominator));
this.numerator = this.numerator / gcd;
this.denominator = this.denominator / gcd;
}

public add(other: Fraction) {
const newNumerator =
Expand Down
79 changes: 56 additions & 23 deletions fraction_test.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,69 @@
import { assertAlmostEquals, assertEquals } from "@std/assert";
import { assertAlmostEquals, assertEquals, assertThrows } from "@std/assert";
import { Fraction } from "./fraction.ts";

Deno.test("fraction of 1/1 is 1.0", () => {
// Arrange
const fraction = new Fraction(1, 1);
Deno.test("fraction toFloat and toString", () => {
const fraction = new Fraction(1, 2);
assertEquals(fraction.toFloat(0.01), 0.5);
assertEquals(fraction.toString(), "1/2");
});

// Act
const float = fraction.toFloat(0.1);
Deno.test("add fractions", () => {
const fraction = new Fraction(1, 3);
fraction.add(new Fraction(2, 6));
assertAlmostEquals(fraction.toFloat(0.01), 0.67);
});

// Assert
assertEquals(float, 1.0);
Deno.test("subtract fractions", () => {
const fraction = new Fraction(5, 6);
fraction.subtract(new Fraction(1, 6));
assertAlmostEquals(fraction.toFloat(0.01), 0.67);
});

Deno.test("fraction of 2/3 is roughly 0.67", () => {
// Arrange
const fraction = new Fraction(2, 3);
Deno.test("multiply and divide fractions", () => {
const f1 = new Fraction(1, 2);
f1.multiply(new Fraction(2, 3));
assertEquals(f1.toFloat(0.01), 0.33);

const f2 = new Fraction(1, 2);
f2.divide(new Fraction(2, 3));
assertEquals(f2.toFloat(0.01), 0.75);
});

// Act
const float = fraction.toFloat(0.01);
Deno.test("parse valid fraction with decimal denominator", () => {
const fraction = Fraction.parse("1/0.5");
assertEquals(fraction.toFloat(0.01), 2.0);
});

// Assert
assertAlmostEquals(float, 0.67);
Deno.test("parse invalid format throws error", () => {
assertThrows(() => Fraction.parse("invalid"));
assertThrows(() => Fraction.parse("1"));
assertThrows(() => Fraction.parse("abc/2"));
assertThrows(() => Fraction.parse("1/abc"));
});

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);
Deno.test("constructor and parse with zero denominator throw error", () => {
assertThrows(() => new Fraction(3, 0));
assertThrows(() => Fraction.parse("3/0"));
});

// Act
left.add(right);
Deno.test("cancel trivial fraction stays unchanged", () => {
const f = new Fraction(1, 3);
f.cancel();
assertEquals(f.toString(), "1/3");
});

Deno.test("cancel reduces fraction", () => {
const f = new Fraction(2, 3); // already reduced by constructor
f.cancel();
assertEquals(f.toString(), "2/3");
});

// Assert
assertAlmostEquals(left.toFloat(0.01), 0.67);
Deno.test("fraction auto-cancels on construction", () => {
assertEquals(new Fraction(6, 9).toString(), "2/3");
assertEquals(new Fraction(4, 8).toString(), "1/2");
assertEquals(new Fraction(10, 5).toString(), "2/1");
});

Deno.test("parse auto-cancels fraction", () => {
assertEquals(Fraction.parse("6/9").toString(), "2/3");
});
37 changes: 29 additions & 8 deletions geometry_test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,34 @@
import { assertAlmostEquals } from "@std/assert";
import { Circle, Point2D } from "./geometry.ts";
import { assertAlmostEquals, assertEquals } from "@std/assert";
import { Circle, Point2D, Rectangle } from "./geometry.ts";

Deno.test("circumference of a circle with radius 5 is roughtly 31.416", () => {
// Given
Deno.test("circle circumference area diameter", () => {
const circle = new Circle(new Point2D(3, 4), 5);
assertAlmostEquals(circle.circumference(), 31.416, 0.01);
assertAlmostEquals(circle.area(), 78.54, 0.01);
assertEquals(circle.diameter(), 10);
});

// When
const actual = circle.circumference();
Deno.test("point distance with positive coordinates", () => {
const p1 = new Point2D(0, 0);
const p2 = new Point2D(3, 4);
assertEquals(p1.distanceTo(p2), 5);
});

// Then
assertAlmostEquals(actual, 31.416, 0.01);
Deno.test("point distance with negative coordinates and same point", () => {
const p1 = new Point2D(-1, -1);
const p2 = new Point2D(2, 3);
assertEquals(p1.distanceTo(p2), 5);
assertEquals(p1.distanceTo(p1), 0);
});

Deno.test("rectangle area circumference diagonal", () => {
const rect = new Rectangle(new Point2D(0, 0), new Point2D(4, 3));
assertEquals(rect.area(), 12);
assertEquals(rect.circumference(), 20);
assertEquals(rect.diagonal(), 5);
});

Deno.test("rectangle with negative coordinates", () => {
const rect = new Rectangle(new Point2D(-2, -3), new Point2D(2, 3));
assertEquals(rect.area(), 24);
});
15 changes: 15 additions & 0 deletions utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,18 @@ export function roundTo(value: number, precision: number): number {
const scaledDown = rounded / factor;
return scaledDown;
}

export function gcdBruteForce(a: number, b: number): number {
for (let i = Math.min(a, b); i >= 1; i--) {
if (a % i === 0 && b % i === 0) {
return i;
}
}
return 1;
}

export function gcdEuclid(a: number, b: number): number {
if (a === b) return a;
const c = Math.max(a, b) - Math.min(a, b);
return gcdEuclid(Math.min(a, b), c);
}
40 changes: 40 additions & 0 deletions utils_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { assertEquals } from "@std/assert";
import { gcdBruteForce, gcdEuclid, roundTo } from "./utils.ts";

Deno.test("round to different precisions", () => {
assertEquals(roundTo(1.234, 0.1), 1.2);
assertEquals(roundTo(1.234, 0.01), 1.23);
assertEquals(roundTo(5.7, 1), 6);
});

Deno.test("round negative and zero", () => {
assertEquals(roundTo(-1.234, 0.1), -1.2);
assertEquals(roundTo(0, 0.1), 0);
});

Deno.test("round at halfway and small numbers", () => {
assertEquals(roundTo(1.25, 0.1), 1.3);
assertEquals(roundTo(0.01234, 0.001), 0.012);
});

Deno.test("gcdBruteForce trivial case", () => {
assertEquals(gcdBruteForce(1, 1), 1);
});

Deno.test("gcdBruteForce basic cases", () => {
assertEquals(gcdBruteForce(6, 9), 3);
assertEquals(gcdBruteForce(12, 8), 4);
assertEquals(gcdBruteForce(7, 3), 1);
assertEquals(gcdBruteForce(100, 75), 25);
});

Deno.test("gcdEuclid trivial case", () => {
assertEquals(gcdEuclid(1, 1), 1);
});

Deno.test("gcdEuclid matches gcdBruteForce", () => {
assertEquals(gcdEuclid(6, 9), 3);
assertEquals(gcdEuclid(12, 8), 4);
assertEquals(gcdEuclid(7, 3), 1);
assertEquals(gcdEuclid(100, 75), 25);
});