forked from przeprogramowani/advent-of-frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Aktualizacja test pattern (przeprogramowani#34)
- Loading branch information
1 parent
fbc43f4
commit 8b2a163
Showing
13 changed files
with
232 additions
and
196 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
const { exec } = require('child_process'); | ||
const { existsSync } = require('fs'); | ||
const { join } = require('path'); | ||
|
||
const date = new Date(); | ||
const year = date.getFullYear(); | ||
const month = String(date.getMonth() + 1).padStart(2, '0'); | ||
const day = String(date.getDate()).padStart(2, '0'); | ||
const testPath = `./tasks/${year}-${month}-${day}/index.test.ts`; | ||
|
||
const fullTestPath = join(process.cwd(), testPath); | ||
|
||
if (existsSync(fullTestPath)) { | ||
exec(`jest --colors ${testPath}`, (error, stdout, stderr) => { | ||
if (error) { | ||
console.error(`exec error: ${error}`); | ||
return; | ||
} | ||
console.log(`stdout: ${stdout}`); | ||
console.error(`stderr: ${stderr}`); | ||
}); | ||
} else { | ||
console.log(`Nie znaleziono folderu z dzisiejszą datą: ${testPath}. Uruchamiam wszystkie testy.`); | ||
exec('jest --colors', (error, stdout, stderr) => { | ||
if (error) { | ||
console.error(`exec error: ${error}`); | ||
return; | ||
} | ||
console.log(`stdout: ${stdout}`); | ||
console.error(`stderr: ${stderr}`); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,26 @@ | ||
import { GiftRegistry } from './index'; | ||
|
||
describe('GiftRegistry', () => { | ||
it('should retrieve all gifts for a given child ID', () => { | ||
const registry = new GiftRegistry(); | ||
registry.addGift(1, 'teddy bear'); | ||
registry.addGift(1, 'bicycle'); | ||
registry.addGift(2, 'car model'); | ||
expect(registry.getGiftsForChild(1)).toEqual(['teddy bear', 'bicycle']); | ||
}); | ||
it('should retrieve all gifts for a given child ID', () => { | ||
const registry = new GiftRegistry(); | ||
registry.addGift(1, 'teddy bear'); | ||
registry.addGift(1, 'bicycle'); | ||
registry.addGift(2, 'car model'); | ||
expect(registry.getGiftsForChild(1)).toEqual(['teddy bear', 'bicycle']); | ||
}); | ||
|
||
it('should handle removal of gifts correctly', () => { | ||
const registry = new GiftRegistry(); | ||
registry.addGift(1, 'teddy bear'); | ||
registry.addGift(1, 'bicycle'); | ||
registry.removeGift(1, 'teddy bear'); | ||
expect(registry.getGiftsForChild(1)).toEqual(['bicycle']); | ||
expect(registry.getGiftsForChild(1)).not.toContain('teddy bear'); | ||
}); | ||
it('should handle removal of gifts correctly', () => { | ||
const registry = new GiftRegistry(); | ||
registry.addGift(1, 'teddy bear'); | ||
registry.addGift(1, 'bicycle'); | ||
registry.removeGift(1, 'teddy bear'); | ||
expect(registry.getGiftsForChild(1)).toEqual(['bicycle']); | ||
expect(registry.getGiftsForChild(1)).not.toContain('teddy bear'); | ||
}); | ||
|
||
it('should throw an error if trying to remove a gift that does not exist', () => { | ||
const registry = new GiftRegistry(); | ||
registry.addGift(1, 'teddy bear'); | ||
expect(() => registry.removeGift(1, 'puzzle')).toThrow('Gift not found'); | ||
}); | ||
it('should throw an error if trying to remove a gift that does not exist', () => { | ||
const registry = new GiftRegistry(); | ||
registry.addGift(1, 'teddy bear'); | ||
expect(() => registry.removeGift(1, 'puzzle')).toThrow('Gift not found'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,22 @@ | ||
export class GiftRegistry { | ||
private _registry = new Map<number, Set<string>>([]); | ||
|
||
public addGift(childId: number, gift: string) { | ||
const childGiftList = this._registry.get(childId) ?? new Set<string>([]); | ||
childGiftList.add(gift); | ||
|
||
this._registry.set(childId, childGiftList); | ||
private registry: Record<number, string[]> = {}; | ||
|
||
addGift(childId: number, gift: string) { | ||
if (!this.registry[childId]) { | ||
this.registry[childId] = []; | ||
} | ||
|
||
public getGiftsForChild(childId: number) { | ||
return [...(this._registry.get(childId) ?? [])]; | ||
} | ||
|
||
public removeGift(childId: number, gift: string) { | ||
const childGiftList = this._registry.get(childId); | ||
if (!childGiftList || !childGiftList.has(gift)) { | ||
throw new Error('Gift not found'); | ||
} | ||
|
||
childGiftList.delete(gift); | ||
this.registry[childId].push(gift); | ||
} | ||
|
||
getGiftsForChild(childId: number): string[] { | ||
return this.registry[childId] || []; | ||
} | ||
|
||
removeGift(childId: number, gift: string) { | ||
const gifts = this.registry[childId]; | ||
if (!gifts || !gifts.includes(gift)) { | ||
throw new Error('Gift not found!'); | ||
} | ||
this.registry[childId] = gifts.filter(item => item !== gift); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,28 @@ | ||
type ChristmasQueueItem<DataType> = { priority: number, items: DataType[] }; | ||
|
||
export class ChristmasQueue<DataType> { | ||
private _queue: ChristmasQueueItem<DataType>[] = []; | ||
export class ChristmasQueue<T> { | ||
private queue: { item: T; priority: number }[] = []; | ||
|
||
public enqueue(item: DataType, priority: number) { | ||
const queueItem = this._queue.find(record => record.priority === priority); | ||
|
||
if (queueItem) { | ||
queueItem.items.push(item); | ||
} else { | ||
this._queue.push({ | ||
priority, | ||
items: [item], | ||
}); | ||
this._queue.sort((a, b) => b.priority - a.priority); | ||
enqueue(item: T, priority: number): void { | ||
let added = false; | ||
for (let i = 0; i < this.queue.length; i++) { | ||
if (priority > this.queue[i].priority) { | ||
this.queue.splice(i, 0, { item, priority }); | ||
added = true; | ||
break; | ||
} | ||
} | ||
if (!added) { | ||
this.queue.push({ item, priority }); | ||
} | ||
} | ||
} | ||
|
||
public dequeue() { | ||
if (this.isEmpty()) { | ||
throw new Error('There are no letters in the queue!'); | ||
} | ||
|
||
const highestPriority = this._queue[0]; | ||
const item = highestPriority.items.shift(); | ||
|
||
if (highestPriority.items.length === 0) { | ||
this._queue.shift(); | ||
dequeue(): T { | ||
if (this.isEmpty()) { | ||
throw new Error('There are no letters in the queue!'); | ||
} | ||
return this.queue.shift()!.item; | ||
} | ||
|
||
return item; | ||
} | ||
|
||
public isEmpty() { | ||
return this._queue.length === 0; | ||
isEmpty(): boolean { | ||
return this.queue.length === 0; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,31 @@ | ||
import { Lokalizacja, MapaCzasoprzestrzenna, znajdzWorek } from './index'; | ||
|
||
describe('znajdzWorek', () => { | ||
test('powinien zwrócić lokalizację z najwyższą wartością mapy', () => { | ||
const lokalizacje: Lokalizacja[] = [ | ||
{ x: 1, y: 2, z: 3, czas: 4 }, | ||
{ x: 5, y: 6, z: 7, czas: 8 }, | ||
{ x: 9, y: 10, z: 11, czas: 12 }, | ||
]; | ||
const mapa: MapaCzasoprzestrzenna = (x, y, z, czas) => x + y + z + czas; | ||
const wynik = znajdzWorek(lokalizacje, mapa); | ||
expect(wynik).toEqual({ x: 9, y: 10, z: 11, czas: 12 }); | ||
}); | ||
test('powinien zwrócić lokalizację z najwyższą wartością mapy', () => { | ||
const lokalizacje: Lokalizacja[] = [ | ||
{ x: 1, y: 2, z: 3, czas: 4 }, | ||
{ x: 5, y: 6, z: 7, czas: 8 }, | ||
{ x: 9, y: 10, z: 11, czas: 12 }, | ||
]; | ||
const mapa: MapaCzasoprzestrzenna = (x, y, z, czas) => x + y + z + czas; | ||
const wynik = znajdzWorek(lokalizacje, mapa); | ||
expect(wynik).toEqual({ x: 9, y: 10, z: 11, czas: 12 }); | ||
}); | ||
|
||
test('powinien zwrócić null, gdy lista lokalizacji jest pusta', () => { | ||
const lokalizacje: Lokalizacja[] = []; | ||
const mapa: MapaCzasoprzestrzenna = jest.fn(); | ||
const wynik = znajdzWorek(lokalizacje, mapa); | ||
expect(wynik).toBeNull(); | ||
}); | ||
test('powinien zwrócić null, gdy lista lokalizacji jest pusta', () => { | ||
const lokalizacje: Lokalizacja[] = []; | ||
const mapa: MapaCzasoprzestrzenna = jest.fn(); | ||
const wynik = znajdzWorek(lokalizacje, mapa); | ||
expect(wynik).toBeNull(); | ||
}); | ||
|
||
test('powinien obsłużyć wartości niepoprawne matematycznie', () => { | ||
const lokalizacje: Lokalizacja[] = [ | ||
{ x: -1, y: -2, z: -3, czas: -4 }, | ||
{ x: 0, y: 0, z: 0, czas: 0 }, | ||
]; | ||
const mapa: MapaCzasoprzestrzenna = () => NaN; | ||
const wynik = znajdzWorek(lokalizacje, mapa); | ||
expect(wynik).toBeNull(); | ||
}); | ||
test('powinien obsłużyć wartości niepoprawne matematycznie', () => { | ||
const lokalizacje: Lokalizacja[] = [ | ||
{ x: -1, y: -2, z: -3, czas: -4 }, | ||
{ x: 0, y: 0, z: 0, czas: 0 }, | ||
]; | ||
const mapa: MapaCzasoprzestrzenna = () => NaN; | ||
const wynik = znajdzWorek(lokalizacje, mapa); | ||
expect(wynik).toBeNull(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,32 @@ | ||
export type MapaCzasoprzestrzenna = (x: number, y: number, z: number, czas: number) => number; | ||
|
||
export interface Lokalizacja { | ||
x: number; | ||
y: number; | ||
z: number; | ||
czas: number; | ||
} | ||
|
||
export type MapaCzasoprzestrzenna = (x: number, y: number, z: number, czas: number) => number; | ||
|
||
|
||
|
||
export function znajdzWorek(lokalizacje: Lokalizacja[], mapa: MapaCzasoprzestrzenna): Lokalizacja | null { | ||
return lokalizacje.reduce<Lokalizacja | null>((obecnaLokalizacja, lokalizacja) => { | ||
if (obecnaLokalizacja === null) { | ||
return lokalizacja; | ||
} | ||
|
||
const mapaCzasoprzestrzennaLokalizacja = mapa(lokalizacja.x, lokalizacja.y, lokalizacja.z, lokalizacja.czas); | ||
const mapaCzasoprzestrzennaWynik = | ||
mapa(obecnaLokalizacja.x, obecnaLokalizacja.y, obecnaLokalizacja.z, obecnaLokalizacja.czas); | ||
if (lokalizacje.length === 0) { | ||
return null; | ||
} | ||
|
||
let maxWartosc = -Infinity; | ||
let najlepszaLokalizacja: Lokalizacja | null = null; | ||
|
||
for (const lokalizacja of lokalizacje) { | ||
const { x, y, z, czas } = lokalizacja; | ||
const wartoscMapy = mapa(x, y, z, czas); | ||
|
||
if (isNaN(mapaCzasoprzestrzennaLokalizacja) || isNaN(mapaCzasoprzestrzennaWynik)) { | ||
return null; | ||
if (!isNaN(wartoscMapy) && isFinite(wartoscMapy) && wartoscMapy > maxWartosc) { | ||
maxWartosc = wartoscMapy; | ||
najlepszaLokalizacja = lokalizacja | ||
} | ||
|
||
return mapaCzasoprzestrzennaLokalizacja > mapaCzasoprzestrzennaWynik | ||
? lokalizacja | ||
: obecnaLokalizacja; | ||
}, null); | ||
} | ||
|
||
return najlepszaLokalizacja; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,26 @@ | ||
import { memoize } from './index'; | ||
|
||
describe('memoize', () => { | ||
it('should memoize function results', () => { | ||
const complexCalculation = jest.fn().mockImplementation((x: number) => x * x); | ||
const memoizedCalculation = memoize(complexCalculation); | ||
it('should memoize function results', () => { | ||
const complexCalculation = jest.fn().mockImplementation((x: number) => x * x); | ||
const memoizedCalculation = memoize(complexCalculation); | ||
|
||
expect(memoizedCalculation(2)).toBe(4); | ||
expect(memoizedCalculation(2)).toBe(4); | ||
expect(memoizedCalculation(3)).toBe(9); | ||
expect(complexCalculation).toHaveBeenCalledTimes(2); | ||
}); | ||
expect(memoizedCalculation(2)).toBe(4); | ||
expect(memoizedCalculation(2)).toBe(4); | ||
expect(memoizedCalculation(3)).toBe(9); | ||
expect(complexCalculation).toHaveBeenCalledTimes(2); | ||
}); | ||
|
||
it('should handle different arguments correctly', () => { | ||
const greeting = jest.fn().mockImplementation((p: string) => `${p}!`); | ||
const memoizedGreeting = memoize(greeting); | ||
it('should handle different arguments correctly', () => { | ||
const greeting = jest.fn().mockImplementation((p: string) => `${p}!`); | ||
const memoizedGreeting = memoize(greeting); | ||
|
||
expect(memoizedGreeting('John')).toBe('John!'); | ||
expect(memoizedGreeting('Paul')).toBe('Paul!'); | ||
expect(greeting).toHaveBeenCalledTimes(2); | ||
}); | ||
expect(memoizedGreeting('John')).toBe('John!'); | ||
expect(memoizedGreeting('Paul')).toBe('Paul!'); | ||
expect(greeting).toHaveBeenCalledTimes(2); | ||
}); | ||
|
||
it('should throw an error when non-function is memoized', () => { | ||
expect(() => memoize(42 as never)).toThrow('Function to be memoized must be a function.'); | ||
}); | ||
it('should throw an error when non-function is memoized', () => { | ||
expect(() => memoize(42 as never)).toThrow('Function to be memoized must be a function.'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,22 @@ | ||
export function memoize<DataType>(fn: (arg: DataType) => DataType) { | ||
if (typeof fn !== 'function') { | ||
throw new Error('Function to be memoized must be a function.'); | ||
} | ||
|
||
const cache = new Map<DataType, DataType>([]); | ||
type MemoizedFunction<T> = (...args: any[]) => T; | ||
|
||
export function memoize<T>(func: (...args: any[]) => T): MemoizedFunction<T> { | ||
if (typeof func !== 'function') { | ||
throw new Error('Function to be memoized must be a function.'); | ||
} | ||
|
||
const cache = new Map<string, T>(); | ||
|
||
return (arg: DataType) => { | ||
const cachedResult = cache.get(arg); | ||
if (cachedResult !== undefined) { | ||
return cachedResult; | ||
} | ||
return (...args: any[]): T => { | ||
const key = JSON.stringify(args); | ||
|
||
const result = fn(arg); | ||
cache.set(arg, result); | ||
return result; | ||
if (cache.has(key)) { | ||
return cache.get(key)!; | ||
} | ||
|
||
const result = func(...args); | ||
cache.set(key, result); | ||
|
||
return result; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.