From 8c59577e4b182f33d360b3a47446f4958b91c756 Mon Sep 17 00:00:00 2001 From: unknown Date: Mon, 25 Nov 2024 08:27:29 +0300 Subject: [PATCH] Labs --- rpgsaga/car/src/car.ts | 68 ++++++++++++++++++++++++ rpgsaga/car/tests/car.test.ts | 80 +++++++++++++++++++++++++++++ rpgsaga/function/src/func.ts | 34 ++++++++++++ rpgsaga/function/src/index.ts | 3 ++ rpgsaga/function/tests/func.test.ts | 62 ++++++++++++++++++++++ 5 files changed, 247 insertions(+) create mode 100644 rpgsaga/car/src/car.ts create mode 100644 rpgsaga/car/tests/car.test.ts create mode 100644 rpgsaga/function/src/func.ts create mode 100644 rpgsaga/function/src/index.ts create mode 100644 rpgsaga/function/tests/func.test.ts diff --git a/rpgsaga/car/src/car.ts b/rpgsaga/car/src/car.ts new file mode 100644 index 000000000..bff616db5 --- /dev/null +++ b/rpgsaga/car/src/car.ts @@ -0,0 +1,68 @@ +export class Auto { + private speed: number; + private color: string; + private size: string; + private distance: number; + + constructor() { + this.speed = 0; + this.color = ""; + this.size = ""; + this.distance = 0; + } + + getSpeed(): number { + if (this.speed <= 0) { + console.log("Ты физику уважаешь? Отрицательная скорость? Цвет и размер уже не важны."); + this.speed = 0; + } else if (this.speed > 300) { + console.log("Это птица? Это самолёт? Нет, это машину в воздух хотят запустить. Цвет и размер уже не уважны."); + this.speed = 0; + } + return this.speed; + } + + setSpeed(speed: number): void { + this.speed = speed; + } + + getColor(): string { + return this.color; + } + + setColor(color: string): void { + this.color = color; + } + + getDistance(): number { + if (this.distance < 0) { + console.log("Ну я так не играю, давай нормальное расстояние"); + this.distance = 0; + } + return this.distance; + } + + setDistance(distance: number): void { + this.distance = distance; + } + + getSize(): string { + return this.size; + } + + setSize(size: string): void { + this.size = size; + } +} + +export function calculateTime(auto: Auto): number { + const distance = auto.getDistance(); + const speed = auto.getSpeed(); + + if (speed === 0) { + return 0; + } + + const time = distance / speed; + return time === 1 ? 0 : time; +} diff --git a/rpgsaga/car/tests/car.test.ts b/rpgsaga/car/tests/car.test.ts new file mode 100644 index 000000000..8e641b9a0 --- /dev/null +++ b/rpgsaga/car/tests/car.test.ts @@ -0,0 +1,80 @@ +import { Auto, calculateTime } from "../src/car"; + +describe('Auto class', () => { + let auto: Auto; + + beforeEach(() => { + auto = new Auto(); + }); + + test('should set and get speed correctly', () => { + auto.setSpeed(100); + expect(auto.getSpeed()).toBe(100); + }); + + test('should reset speed to 0 for invalid values (<=0 or >300)', () => { + auto.setSpeed(-10); + expect(auto.getSpeed()).toBe(0); + + auto.setSpeed(350); + expect(auto.getSpeed()).toBe(0); + }); + + test('should set and get color correctly', () => { + auto.setColor("Красный"); + expect(auto.getColor()).toBe("Красный"); + }); + + test('should set and get size correctly', () => { + auto.setSize("Четырехместная"); + expect(auto.getSize()).toBe("Четырехместная"); + }); + + test('should set and get distance correctly', () => { + auto.setDistance(200); + expect(auto.getDistance()).toBe(200); + }); + + test('should reset distance to 0 for negative values', () => { + auto.setDistance(-50); + expect(auto.getDistance()).toBe(0); + }); +}); + +describe('calculateTime function', () => { + let auto: Auto; + + beforeEach(() => { + auto = new Auto(); + }); + + test('should calculate time correctly for valid speed and distance', () => { + auto.setSpeed(100); + auto.setDistance(200); + expect(calculateTime(auto)).toBe(2); + }); + + test('should return 0 time when speed is 0', () => { + auto.setSpeed(0); + auto.setDistance(200); + expect(calculateTime(auto)).toBe(0); + }); + + test('should return 0 time when time equals 1', () => { + auto.setSpeed(100); + auto.setDistance(100); + expect(calculateTime(auto)).toBe(0); + }); + + test('should handle distance of 0 correctly', () => { + auto.setSpeed(100); + auto.setDistance(0); + expect(calculateTime(auto)).toBe(0); + }); + + test('should handle both speed and distance of 0 correctly', () => { + auto.setSpeed(0); + auto.setDistance(0); + expect(calculateTime(auto)).toBe(0); + }); +}); diff --git a/rpgsaga/function/src/func.ts b/rpgsaga/function/src/func.ts new file mode 100644 index 000000000..1cdf1b83d --- /dev/null +++ b/rpgsaga/function/src/func.ts @@ -0,0 +1,34 @@ +function calc(a: number, b: number, x: number): number { + if (x <= 0) { + throw new Error("x must be greater than 0"); + } + return Math.pow((a + b * x) / Math.pow(Math.log10(x), 3), 1 / 5); +} + +function task1(a: number, b: number, xn: number, xk: number, deltax: number): number[] { + if (deltax <= 0) { + throw new Error("deltax must be greater than 0"); + } + const numElements = Math.floor((xk - xn) / deltax + 1); + const answersA: number[] = []; + for (let x = xn; x <= xk; x += deltax) { + const y = calc(a, b, x); + answersA.push(y); + } + return answersA; +} + +function task2(a: number, b: number, znX: number[]): number[] { + const answersB: number[] = []; + for (const x of znX) { + if (x <= 0) { + throw new Error("All elements in znX must be greater than 0"); + } + const y = calc(a, b, x); + answersB.push(y); + } + return answersB; +} + +// Exporting functions to use in other modules or for testing +export { calc, task1, task2 }; \ No newline at end of file diff --git a/rpgsaga/function/src/index.ts b/rpgsaga/function/src/index.ts new file mode 100644 index 000000000..97a90db01 --- /dev/null +++ b/rpgsaga/function/src/index.ts @@ -0,0 +1,3 @@ +import { calc } from "./func"; + +console.log(calc(1, 2, 10)); \ No newline at end of file diff --git a/rpgsaga/function/tests/func.test.ts b/rpgsaga/function/tests/func.test.ts new file mode 100644 index 000000000..43f7981f5 --- /dev/null +++ b/rpgsaga/function/tests/func.test.ts @@ -0,0 +1,62 @@ +import { calc, task1, task2 } from '../src/func'; // Adjust the import according to your file structure + +describe('calc function', () => { + test('should calculate correctly for positive numbers', () => { + const result = calc(2, 3, 10); + expect(result).toBeCloseTo(2, 3); // Example expected value + }); + + test('should handle edge case where log10(x) is undefined (x <= 0)', () => { + expect(() => calc(2, 3, 0)).toThrow(); + expect(() => calc(2, 3, -10)).toThrow(); + }); + + test('should handle very large x values', () => { + const result = calc(1, 2, 1e6); + expect(result).toBeGreaterThan(0); + }); +}); + +describe('task1 function', () => { + test('should calculate array correctly with uniform step size', () => { + const result = task1(2, 3, 1, 5, 1); + expect(result).toHaveLength(5); + expect(result[0]).toBeCloseTo(calc(2, 3, 1), 3); + expect(result[4]).toBeCloseTo(calc(2, 3, 5), 3); + }); + + test('should return single element when xn equals xk', () => { + const result = task1(2, 3, 4, 4, 1); + expect(result).toHaveLength(1); + expect(result[0]).toBeCloseTo(calc(2, 3, 4), 3); + }); + + test('should handle negative deltax by throwing an exception', () => { + expect(() => {task1(2, 3, 1, 5, -1);}).toThrow(); + }); + + test('should throw error if deltax is zero (division by zero)', () => { + expect(() => task1(2, 3, 1, 5, 0)).toThrow(); + }); +}); + +describe('task2 function', () => { + test('should calculate values correctly for an array of inputs', () => { + const znX = [1, 2, 3]; + const result = task2(2, 3, znX); + expect(result).toHaveLength(3); + expect(result[0]).toBeCloseTo(calc(2, 3, 1), 3); + expect(result[1]).toBeCloseTo(calc(2, 3, 2), 3); + expect(result[2]).toBeCloseTo(calc(2, 3, 3), 3); + }); + + test('should handle empty input array', () => { + const result = task2(2, 3, []); + expect(result).toEqual([]); + }); + + test('should throw error for invalid x values in znX', () => { + const znX = [1, -2, 3]; + expect(() => task2(2, 3, znX)).toThrow(); + }); +}); \ No newline at end of file