| 1 | import { describe, it } from 'node:test'; | |
| 2 | import { strict as assert } from 'assert'; | |
| 3 | import { square, total } from './main.js'; | |
| 4 | ||
| 5 | /** | |
| 6 | * Test suite for the Grains exercise | |
| 7 | * Tests both square and total functions according to the problem requirements | |
| 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 = 9223372036854775808) | |
| 61 | */ | |
| 62 | it('should calculate the number of grains on square 64', () => { | |
| 63 | assert.strictEqual(square(64), BigInt('9223372036854775808')); | |
| 64 | }); | |
| 65 | ||
| 66 | /** | |
| 67 | * Edge case: tests that the function throws an error for square 0 | |
| 68 | * Square numbers must be between 1 and 64, so 0 is invalid | |
| 69 | */ | |
| 70 | it('should throw an error for square 0', () => { | |
| 71 | assert.throws(() => square(0), new Error('square must be between 1 and 64')); | |
| 72 | }); | |
| 73 | ||
| 74 | /** | |
| 75 | * Edge case: tests that the function throws an error for negative square numbers | |
| 76 | * Square numbers must be between 1 and 64, so negative numbers are invalid | |
| 77 | */ | |
| 78 | it('should throw an error for negative square numbers', () => { | |
| 79 | assert.throws(() => square(-1), new Error('square must be between 1 and 64')); | |
| 80 | }); | |
| 81 | ||
| 82 | /** | |
| 83 | * Edge case: tests that the function throws an error for square numbers greater than 64 | |
| 84 | * Square numbers must be between 1 and 64, so 65 is invalid | |
| 85 | */ | |
| 86 | it('should throw an error for square numbers greater than 64', () => { | |
| 87 | assert.throws(() => square(65), new Error('square must be between 1 and 64')); | |
| 88 | }); | |
| 89 | ||
| 90 | /** | |
| 91 | * Test case: verify the function correctly calculates the total number of grains | |
| 92 | * Total grains should be 2^64 - 1 = 18446744073709551615 | |
| 93 | */ | |
| 94 | it('should calculate the total number of grains', () => { | |
| 95 | assert.strictEqual(total(), BigInt('18446744073709551615')); | |
| 96 | }); | |
| 97 | }); |
| 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.