fistinz

Finished
1import { describe, it } from 'node:test';
2import { square, total } from './main.js';
3import { strict as assert } from 'node:assert';
4
5// Helper function to replace expect().toBe()
6const expect = (actual) => ({
7 toBe: (expected) => {
8 if (actual !== expected) {
9 throw new Error(`Expected ${expected}, but got ${actual}`);
10 }
11 },
12 toThrow: (expectedError) => {
13 try {
14 actual();
15 throw new Error('Expected function to throw an error, but it did not');
16 } catch (error) {
17 if (expectedError.message && error.message !== expectedError.message) {
18 throw new Error(`Expected error message "${expectedError.message}", but got "${error.message}"`);
19 }
20 }
21 }
22});
23
24describe('Grains', () => {
25 describe('square', () => {
26 /**
27 * Test case: verify the function correctly calculates grains for square 1
28 * This tests the base case where the first square should have exactly 1 grain
29 */
30 it('should calculate the number of grains for square 1 correctly', () => {
31 expect(square(1)).toBe(BigInt(1));
32 });
33
34 /**
35 * Test case: verify the function correctly calculates grains for square 2
36 * This tests that the doubling pattern works correctly for the second square
37 */
38 it('should calculate the number of grains for square 2 correctly', () => {
39 expect(square(2)).toBe(BigInt(2));
40 });
41
42 /**
43 * Test case: verify the function correctly calculates grains for square 3
44 * This tests that the doubling pattern continues correctly
45 */
46 it('should calculate the number of grains for square 3 correctly', () => {
47 expect(square(3)).toBe(BigInt(4));
48 });
49
50 /**
51 * Test case: verify the function correctly calculates grains for square 4
52 * This continues testing the doubling pattern
53 */
54 it('should calculate the number of grains for square 4 correctly', () => {
55 expect(square(4)).toBe(BigInt(8));
56 });
57
58 /**
59 * Test case: verify the function correctly calculates grains for square 16
60 * This tests a mid-range square to ensure the exponential growth is calculated correctly
61 */
62 it('should calculate the number of grains for square 16 correctly', () => {
63 expect(square(16)).toBe(BigInt(32768));
64 });
65
66 /**
67 * Test case: verify the function correctly calculates grains for square 32
68 * This tests a higher square number to ensure the calculation works for larger exponents
69 */
70 it('should calculate the number of grains for square 32 correctly', () => {
71 expect(square(32)).toBe(BigInt(2147483648));
72 });
73
74 /**
75 * Test case: verify the function correctly calculates grains for square 64
76 * This tests the maximum valid square to ensure it handles the largest possible value
77 */
78 it('should calculate the number of grains for square 64 correctly', () => {
79 expect(square(64)).toBe(BigInt('9223372036854775808'));
80 });
81
82 /**
83 * Edge case: tests that the function throws an error for square 0
84 * This is important because square numbers start at 1, not 0
85 */
86 it('should throw an error for square 0', () => {
87 expect(() => square(0)).toThrow(new Error('square must be between 1 and 64'));
88 });
89
90 /**
91 * Edge case: tests that the function throws an error for negative square numbers
92 * This ensures the function properly validates input boundaries
93 */
94 it('should throw an error for negative square numbers', () => {
95 expect(() => square(-1)).toThrow(new Error('square must be between 1 and 64'));
96 });
97
98 /**
99 * Edge case: tests that the function throws an error for square numbers greater than 64
100 * This ensures the function properly validates the upper boundary
101 */
102 it('should throw an error for square numbers greater than 64', () => {
103 expect(() => square(65)).toThrow(new Error('square must be between 1 and 64'));
104 });
105 });
106
107 describe('total', () => {
108 /**
109 * Test case: verify the function correctly calculates the total grains on the chessboard
110 * This tests that the sum of all grains (1 + 2 + 4 + ... + 2^63) equals 2^64 - 1
111 */
112 it('should calculate the total number of grains correctly', () => {
113 expect(total()).toBe(BigInt('18446744073709551615'));
114 });
115 });
116});
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.