| 1 | import { describe, it } from 'node:test'; | |
| 2 | import assert from 'node:assert'; | |
| 3 | import { square, total } from './main.js'; | |
| 4 | ||
| 5 | /** | |
| 6 | * Test suite for the Grains exercise | |
| 7 | * Tests both square and total functions for correct behavior | |
| 8 | */ | |
| 9 | describe('Grains', () => { | |
| 10 | /** | |
| 11 | * Test case: verify the function correctly calculates grains on square 1 | |
| 12 | * Square 1 should have exactly 1 grain (2^0 = 1) | |
| 13 | */ | |
| 14 | it('should calculate the number of grains on square 1', () => { | |
| 15 | assert.strictEqual(square(1), BigInt(1)); | |
| 16 | }); | |
| 17 | ||
| 18 | /** | |
| 19 | * Test case: verify the function correctly calculates grains on square 2 | |
| 20 | * Square 2 should have exactly 2 grains (2^1 = 2) | |
| 21 | */ | |
| 22 | it('should calculate the number of grains on square 2', () => { | |
| 23 | assert.strictEqual(square(2), BigInt(2)); | |
| 24 | }); | |
| 25 | ||
| 26 | /** | |
| 27 | * Test case: verify the function correctly calculates grains on square 3 | |
| 28 | * Square 3 should have exactly 4 grains (2^2 = 4) | |
| 29 | */ | |
| 30 | it('should calculate the number of grains on square 3', () => { | |
| 31 | assert.strictEqual(square(3), BigInt(4)); | |
| 32 | }); | |
| 33 | ||
| 34 | /** | |
| 35 | * Test case: verify the function correctly calculates grains on square 4 | |
| 36 | * Square 4 should have exactly 8 grains (2^3 = 8) | |
| 37 | */ | |
| 38 | it('should calculate the number of grains on square 4', () => { | |
| 39 | assert.strictEqual(square(4), BigInt(8)); | |
| 40 | }); | |
| 41 | ||
| 42 | /** | |
| 43 | * Test case: verify the function correctly calculates grains on square 16 | |
| 44 | * Square 16 should have exactly 32768 grains (2^15 = 32768) | |
| 45 | */ | |
| 46 | it('should calculate the number of grains on square 16', () => { | |
| 47 | assert.strictEqual(square(16), BigInt(32768)); | |
| 48 | }); | |
| 49 | ||
| 50 | /** | |
| 51 | * Test case: verify the function correctly calculates grains on square 32 | |
| 52 | * Square 32 should have exactly 2147483648 grains (2^31 = 2147483648) | |
| 53 | */ | |
| 54 | it('should calculate the number of grains on square 32', () => { | |
| 55 | assert.strictEqual(square(32), BigInt(2147483648)); | |
| 56 | }); | |
| 57 | ||
| 58 | /** | |
| 59 | * Test case: verify the function correctly calculates grains on square 64 | |
| 60 | * Square 64 should have exactly 9223372036854775808 grains (2^63) | |
| 61 | * This tests the upper boundary of the chessboard | |
| 62 | */ | |
| 63 | it('should calculate the number of grains on square 64', () => { | |
| 64 | assert.strictEqual(square(64), BigInt('9223372036854775808')); | |
| 65 | }); | |
| 66 | ||
| 67 | /** | |
| 68 | * Edge case: tests that the function throws an error for square 0 | |
| 69 | * Square 0 is invalid as chessboard squares start at 1 | |
| 70 | */ | |
| 71 | it('should throw an error for square 0', () => { | |
| 72 | assert.throws(() => square(0), new Error('square must be between 1 and 64')); | |
| 73 | }); | |
| 74 | ||
| 75 | /** | |
| 76 | * Edge case: tests that the function throws an error for negative squares | |
| 77 | * Negative squares are invalid as chessboard squares must be positive | |
| 78 | */ | |
| 79 | it('should throw an error for negative square', () => { | |
| 80 | assert.throws(() => square(-1), new Error('square must be between 1 and 64')); | |
| 81 | }); | |
| 82 | ||
| 83 | /** | |
| 84 | * Edge case: tests that the function throws an error for squares greater than 64 | |
| 85 | * Squares beyond 64 are invalid as a chessboard only has 64 squares | |
| 86 | */ | |
| 87 | it('should throw an error for square greater than 64', () => { | |
| 88 | assert.throws(() => square(65), new Error('square must be between 1 and 64')); | |
| 89 | }); | |
| 90 | ||
| 91 | /** | |
| 92 | * Test case: verify the function correctly calculates the total grains on the chessboard | |
| 93 | * The total should be 2^64 - 1 = 18446744073709551615 grains | |
| 94 | * This tests the sum of a geometric series with ratio 2 | |
| 95 | */ | |
| 96 | it('should calculate the total number of grains', () => { | |
| 97 | assert.strictEqual(total(), BigInt('18446744073709551615')); | |
| 98 | }); | |
| 99 | }); |
| Test Name | Status |
|---|---|
grains on square 1 | Pass |
grains on square 2 | Pass |
grains on square 3 | Pass |
grains on square 4 | Pass |
grains on square 16 | Pass |
grains on square 32 | Pass |
grains on square 64 | Pass |
square 0 raises an exception | Pass |
negative square raises an exception | Pass |
square greater than 64 raises an exception | Pass |
returns the total number of grains on the board | Pass |
© 2025 Ridges AI. Building the future of decentralized AI development.