| 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 return 1 grain for square 1', () => { | |
| 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 correctly for the second square | |
| 18 | */ | |
| 19 | it('should return 2 grains for square 2', () => { | |
| 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 works correctly for the third square | |
| 26 | */ | |
| 27 | it('should return 4 grains for square 3', () => { | |
| 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 works correctly for the fourth square | |
| 34 | */ | |
| 35 | it('should return 8 grains for square 4', () => { | |
| 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 return 32768 grains for square 16', () => { | |
| 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 return 2147483648 grains for square 32', () => { | |
| 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 return 9223372036854775808 grains for square 64', () => { | |
| 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), { | |
| 69 | name: 'Error', | |
| 70 | message: 'square must be between 1 and 64' | |
| 71 | }); | |
| 72 | }); | |
| 73 | ||
| 74 | /** | |
| 75 | * Edge case: tests that the function throws an error for negative square numbers | |
| 76 | * This is important to ensure invalid negative inputs are properly handled | |
| 77 | */ | |
| 78 | it('should throw an error for negative square numbers', () => { | |
| 79 | assert.throws(() => square(-1), { | |
| 80 | name: 'Error', | |
| 81 | message: 'square must be between 1 and 64' | |
| 82 | }); | |
| 83 | }); | |
| 84 | ||
| 85 | /** | |
| 86 | * Edge case: tests that the function throws an error for square numbers greater than 64 | |
| 87 | * This is important because a chessboard only has 64 squares | |
| 88 | */ | |
| 89 | it('should throw an error for square numbers greater than 64', () => { | |
| 90 | assert.throws(() => square(65), { | |
| 91 | name: 'Error', | |
| 92 | message: 'square must be between 1 and 64' | |
| 93 | }); | |
| 94 | }); | |
| 95 | }); | |
| 96 | ||
| 97 | describe('total', () => { | |
| 98 | /** | |
| 99 | * Test case: verify the function correctly calculates the total grains on the chessboard | |
| 100 | * This tests that the sum of all grains on all 64 squares is calculated correctly | |
| 101 | * The total should be 2^64 - 1 which is 18446744073709551615 | |
| 102 | */ | |
| 103 | it('should return the total number of grains on the chessboard', () => { | |
| 104 | assert.equal(total(), BigInt('18446744073709551615')); | |
| 105 | }); | |
| 106 | }); | |
| 107 | }); |
| 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.