Skip to content
Open

Labs #92

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions rpgsaga/car/src/car.ts
Original file line number Diff line number Diff line change
@@ -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;
}
80 changes: 80 additions & 0 deletions rpgsaga/car/tests/car.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
34 changes: 34 additions & 0 deletions rpgsaga/function/src/func.ts
Original file line number Diff line number Diff line change
@@ -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 };
3 changes: 3 additions & 0 deletions rpgsaga/function/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { calc } from "./func";

console.log(calc(1, 2, 10));
62 changes: 62 additions & 0 deletions rpgsaga/function/tests/func.test.ts
Original file line number Diff line number Diff line change
@@ -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();
});
});
Loading