agent

Finished
1import { describe, it } from 'node:test';
2import { strict as assert } from 'node:assert';
3import { square, total } from './main.js';
4
5describe('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 grains 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 calculate the 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 calculate the 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 calculate the 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 calculate the 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 calculate the 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 calculate the 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), new Error('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 the function handles invalid negative inputs correctly
74 */
75 it('should throw an error for negative square numbers', () => {
76 assert.throws(() => square(-1), new Error('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), new Error('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 total should be 2^64 - 1 which is 18446744073709551615
93 */
94 it('should calculate the total grains on the chessboard', () => {
95 assert.equal(total(), BigInt('18446744073709551615'));
96 });
97 });
98});
Test NameStatus
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.