| 1 | import { describe, it } from 'node:test'; | |
| 2 | import { square, total } from './main.js'; | |
| 3 | import { strict as assert } from 'node:assert'; | |
| 4 | ||
| 5 | describe('Grains', () => { | |
| 6 | describe('square', () => { | |
| 7 | /** | |
| 8 | * Test case: verify the function correctly calculates grains for square 1 | |
| 9 | * This tests the base case where the first square should have exactly 1 grain | |
| 10 | */ | |
| 11 | it('should calculate the number of grains for square 1 correctly', () => { | |
| 12 | assert.equal(square(1), BigInt(1)); | |
| 13 | }); | |
| 14 | ||
| 15 | /** | |
| 16 | * Test case: verify the function correctly calculates grains for square 2 | |
| 17 | * This tests that the doubling pattern works for the second square | |
| 18 | */ | |
| 19 | it('should calculate the number of grains for square 2 correctly', () => { | |
| 20 | assert.equal(square(2), BigInt(2)); | |
| 21 | }); | |
| 22 | ||
| 23 | /** | |
| 24 | * Test case: verify the function correctly calculates grains for square 3 | |
| 25 | * This tests that the doubling pattern continues correctly | |
| 26 | */ | |
| 27 | it('should calculate the number of grains for square 3 correctly', () => { | |
| 28 | assert.equal(square(3), BigInt(4)); | |
| 29 | }); | |
| 30 | ||
| 31 | /** | |
| 32 | * Test case: verify the function correctly calculates grains for square 4 | |
| 33 | * This tests that the doubling pattern continues correctly | |
| 34 | */ | |
| 35 | it('should calculate the number of grains for square 4 correctly', () => { | |
| 36 | assert.equal(square(4), BigInt(8)); | |
| 37 | }); | |
| 38 | ||
| 39 | /** | |
| 40 | * Test case: verify the function correctly calculates grains for square 16 | |
| 41 | * This tests a mid-range square to ensure the exponential growth is calculated correctly | |
| 42 | */ | |
| 43 | it('should calculate the number of grains for square 16 correctly', () => { | |
| 44 | assert.equal(square(16), BigInt(32768)); | |
| 45 | }); | |
| 46 | ||
| 47 | /** | |
| 48 | * Test case: verify the function correctly calculates grains for square 32 | |
| 49 | * This tests a higher square number to ensure the exponential growth is calculated correctly | |
| 50 | */ | |
| 51 | it('should calculate the number of grains for square 32 correctly', () => { | |
| 52 | assert.equal(square(32), BigInt(2147483648)); | |
| 53 | }); | |
| 54 | ||
| 55 | /** | |
| 56 | * Test case: verify the function correctly calculates grains for square 64 | |
| 57 | * This tests the maximum valid square to ensure the exponential growth is calculated correctly | |
| 58 | */ | |
| 59 | it('should calculate the number of grains for square 64 correctly', () => { | |
| 60 | assert.equal(square(64), BigInt('9223372036854775808')); | |
| 61 | }); | |
| 62 | ||
| 63 | /** | |
| 64 | * Edge case: tests that the function throws an error for square 0 | |
| 65 | * This is important because square numbers start at 1, not 0 | |
| 66 | */ | |
| 67 | it('should throw an error for square 0', () => { | |
| 68 | assert.throws(() => square(0), { message: 'square must be between 1 and 64' }); | |
| 69 | }); | |
| 70 | ||
| 71 | /** | |
| 72 | * Edge case: tests that the function throws an error for negative square numbers | |
| 73 | * This is important to ensure invalid inputs are properly handled | |
| 74 | */ | |
| 75 | it('should throw an error for negative square numbers', () => { | |
| 76 | assert.throws(() => square(-1), { message: 'square must be between 1 and 64' }); | |
| 77 | }); | |
| 78 | ||
| 79 | /** | |
| 80 | * Edge case: tests that the function throws an error for square numbers greater than 64 | |
| 81 | * This is important because a chessboard only has 64 squares | |
| 82 | */ | |
| 83 | it('should throw an error for square numbers greater than 64', () => { | |
| 84 | assert.throws(() => square(65), { message: 'square must be between 1 and 64' }); | |
| 85 | }); | |
| 86 | }); | |
| 87 | ||
| 88 | describe('total', () => { | |
| 89 | /** | |
| 90 | * Test case: verify the function correctly calculates the total grains on the chessboard | |
| 91 | * This tests that the sum of all grains on all 64 squares is calculated correctly | |
| 92 | * The formula is 2^64 - 1 which equals 18446744073709551615 | |
| 93 | */ | |
| 94 | it('should calculate the total number of grains correctly', () => { | |
| 95 | assert.equal(total(), BigInt('18446744073709551615')); | |
| 96 | }); | |
| 97 | }); | |
| 98 | }); |
| 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.