|
| 1 | +import * as jest from "@jest/globals"; |
| 2 | +import { convertTemperature, findGCD, generateMultiplicationTable } from "./part_h.js"; |
| 3 | + |
| 4 | +const describe = |
| 5 | + !process.env.HW_VERSION || process.env.HW_VERSION === "H" |
| 6 | + ? jest.describe |
| 7 | + : jest.describe.skip; |
| 8 | + |
| 9 | +describe("generateMultiplicationTable", () => { |
| 10 | + it("should generate multiplication table for a given number", () => { |
| 11 | + const result = generateMultiplicationTable(3); |
| 12 | + expect(result).toEqual([ |
| 13 | + "3 x 1 = 3", |
| 14 | + "3 x 2 = 6", |
| 15 | + "3 x 3 = 9", |
| 16 | + "3 x 4 = 12", |
| 17 | + "3 x 5 = 15", |
| 18 | + "3 x 6 = 18", |
| 19 | + "3 x 7 = 21", |
| 20 | + "3 x 8 = 24", |
| 21 | + "3 x 9 = 27", |
| 22 | + "3 x 10 = 30" |
| 23 | + ]); |
| 24 | + }); |
| 25 | + |
| 26 | + it("should handle multiplication table for 1", () => { |
| 27 | + const result = generateMultiplicationTable(1); |
| 28 | + expect(result[0]).toBe("1 x 1 = 1"); |
| 29 | + expect(result[9]).toBe("1 x 10 = 10"); |
| 30 | + expect(result.length).toBe(10); |
| 31 | + }); |
| 32 | +}); |
| 33 | + |
| 34 | +describe("findGCD", () => { |
| 35 | + it("should find GCD of two positive numbers", () => { |
| 36 | + expect(findGCD(12, 8)).toBe(4); |
| 37 | + expect(findGCD(15, 25)).toBe(5); |
| 38 | + expect(findGCD(17, 13)).toBe(1); |
| 39 | + expect(findGCD(100, 75)).toBe(25); |
| 40 | + }); |
| 41 | + |
| 42 | + it("should handle when one number is zero", () => { |
| 43 | + expect(findGCD(5, 0)).toBe(5); |
| 44 | + expect(findGCD(0, 7)).toBe(7); |
| 45 | + }); |
| 46 | + |
| 47 | + it("should handle identical numbers", () => { |
| 48 | + expect(findGCD(7, 7)).toBe(7); |
| 49 | + expect(findGCD(20, 20)).toBe(20); |
| 50 | + }); |
| 51 | + |
| 52 | + it("should handle when one number divides the other", () => { |
| 53 | + expect(findGCD(20, 5)).toBe(5); |
| 54 | + expect(findGCD(6, 18)).toBe(6); |
| 55 | + }); |
| 56 | + |
| 57 | + it("should handle order independence", () => { |
| 58 | + expect(findGCD(8, 12)).toBe(4); |
| 59 | + expect(findGCD(12, 8)).toBe(4); |
| 60 | + }); |
| 61 | +}); |
| 62 | + |
| 63 | +describe("convertTemperature", () => { |
| 64 | + it("should convert Celsius to Fahrenheit", () => { |
| 65 | + expect(convertTemperature(0, "C", "F")).toBe(32); |
| 66 | + expect(convertTemperature(100, "C", "F")).toBe(212); |
| 67 | + expect(convertTemperature(25, "C", "F")).toBe(77); |
| 68 | + }); |
| 69 | + |
| 70 | + it("should convert Fahrenheit to Celsius", () => { |
| 71 | + expect(convertTemperature(32, "F", "C")).toBe(0); |
| 72 | + expect(convertTemperature(212, "F", "C")).toBe(100); |
| 73 | + }); |
| 74 | + |
| 75 | + it("should convert Celsius to Kelvin", () => { |
| 76 | + expect(convertTemperature(0, "C", "K")).toBe(273.15); |
| 77 | + expect(convertTemperature(25, "C", "K")).toBe(298.15); |
| 78 | + }); |
| 79 | + |
| 80 | + it("should convert Kelvin to Celsius", () => { |
| 81 | + expect(convertTemperature(273.15, "K", "C")).toBe(0); |
| 82 | + expect(convertTemperature(298.15, "K", "C")).toBe(25); |
| 83 | + }); |
| 84 | + |
| 85 | + it("should return same temperature for same scales", () => { |
| 86 | + expect(convertTemperature(25, "C", "C")).toBe(25); |
| 87 | + expect(convertTemperature(77, "F", "F")).toBe(77); |
| 88 | + }); |
| 89 | + |
| 90 | + it("should handle complex conversions", () => { |
| 91 | + // F to K: (77°F - 32) × 5/9 + 273.15 = 298.15K |
| 92 | + expect(convertTemperature(77, "F", "K")).toBeCloseTo(298.15, 2); |
| 93 | + // K to F: (298.15K - 273.15) × 9/5 + 32 = 77°F |
| 94 | + expect(convertTemperature(298.15, "K", "F")).toBeCloseTo(77, 2); |
| 95 | + }); |
| 96 | +}); |
0 commit comments