|
| 1 | +import { createCounter, CounterFunction } from '@/app/2620-counter'; |
| 2 | + |
| 3 | +describe('Problem 2620: Counter', () => { |
| 4 | + describe('Correctness Tests', () => { |
| 5 | + test.each([ |
| 6 | + [0, [0, 1, 2, 3, 4]], |
| 7 | + [5, [5, 6, 7, 8, 9]], |
| 8 | + [-3, [-3, -2, -1, 0, 1]], |
| 9 | + [10, [10, 11, 12]], // Example 1: n=10, three calls |
| 10 | + [-2, [-2, -1, 0, 1, 2]], // Example 2: n=-2, five calls |
| 11 | + ])('counter starting at %i produces sequence %j', (start, expectedSequence) => { |
| 12 | + const counter = createCounter(start); |
| 13 | + const results = Array.from({ length: expectedSequence.length }, () => counter()); |
| 14 | + expect(results).toEqual(expectedSequence); |
| 15 | + }); |
| 16 | + }); |
| 17 | + |
| 18 | + describe('Multiple counters & Type Tests', () => { |
| 19 | + it('multiple counters operate independently', () => { |
| 20 | + const c1 = createCounter(10); |
| 21 | + const c2 = createCounter(20); |
| 22 | + expect(c1()).toBe(10); |
| 23 | + expect(c1()).toBe(11); |
| 24 | + expect(c2()).toBe(20); |
| 25 | + expect(c1()).toBe(12); |
| 26 | + expect(c2()).toBe(21); |
| 27 | + }); |
| 28 | + |
| 29 | + test('createCounter returns a function', () => { |
| 30 | + const counter = createCounter(0); |
| 31 | + expect(typeof counter).toBe('function'); |
| 32 | + }); |
| 33 | + |
| 34 | + test('type signature matches CounterFunction', () => { |
| 35 | + const fn: CounterFunction = createCounter(1); |
| 36 | + expect(typeof fn).toBe('function'); |
| 37 | + expect(fn()).toBe(1); |
| 38 | + }); |
| 39 | + }); |
| 40 | +}); |
0 commit comments