Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lab1 + Lab2 + Lab3 + Tests #101

Open
wants to merge 6 commits into
base: Mutovkin_Ilja_Alekseevich
Choose a base branch
from
Open
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
1 change: 0 additions & 1 deletion rpgsaga/saga/src/index.ts

This file was deleted.

23 changes: 23 additions & 0 deletions rpgsaga/saga/src/lab1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
export function formula(x: number, a: number = 1.6): number {
if (Math.abs(x - 1) < 1e-6) {
return NaN;
}
const result = Math.pow(a, Math.pow(x, 2) - 1) - Math.log10(Math.pow(x, 2) - 1) + Math.pow(Math.pow(x, 2) - 1, 1 / 3);
return Math.fround(result * 1e12) / 1e12;
}

export function taskA(xn: number = 1.2, xk: number = 3.7, dx: number = 0.5, a: number = 1.6): number[] {
const y: number[] = [];
for (let x = xn; x <= xk; x += dx) {
y.push(formula(x, a));
}
return y;
}

export function taskB(x: number[] = [1.28, 1.36, 2.47, 3.68, 4.56], a: number = 1.6): number[] {
const y: number[] = [];
for (const xi of x) {
y.push(formula(xi, a));
}
return y;
}
63 changes: 63 additions & 0 deletions rpgsaga/saga/src/lab2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
export class Cat {
private _name: string;
private _age: number;
private _breed: string;

constructor(name: string, age: number, breed: string) {
this._name = name;
this.validateAge(age);
this._age = age;
this._breed = breed;
}

get name(): string {
return this._name;
}

set name(name: string) {
this.validateNonEmpty(name);
this._name = name;
}

get age(): number {
return this._age;
}

set age(age: number) {
this.validateAge(age);
this._age = age;
}

get breed(): string {
return this._breed;
}

set breed(breed: string) {
this.validateNonEmpty(breed);
this._breed = breed;
}

private validateAge(age: number): void {
if (age < 0 || age > 30) {
throw new Error('Возраст должен быть от 0 до 30 лет.');
}
}

private validateNonEmpty(value: string): void {
if (value.length === 0) {
throw new Error('Значение не может быть пустым.');
}
}

public meow(): string {
return `${this._name} говорит: "Мяу!"`;
}

public celebrateBirthday(): void {
this.age += 1;
}

public getInfo(): string {
return `Имя: ${this._name}, Возраст: ${this._age}, Порода: ${this._breed}`;
}
}
71 changes: 71 additions & 0 deletions rpgsaga/saga/src/lab3.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
abstract class Animal {
protected _name: string;
protected _age: number;

constructor(name: string, age: number) {
this.validateAge(age);
this._name = name;
this._age = age;
}

protected validateAge(age: number): void {
if (age < 0 || age > 30) {
throw new Error('Возраст должен быть от 0 до 30 лет.');
}
}

public abstract makeSound(): string;
public abstract getAnimalInfo(): string;

get name(): string {
return this._name;
}

set name(name: string) {
if (!name || name.trim().length === 0) {
throw new Error('Имя не может быть пустым.');
}
this._name = name;
}

get age(): number {
return this._age;
}

set age(age: number) {
this.validateAge(age);
this._age = age;
}
}

export class Cat extends Animal {
private _breed: string;

constructor(name: string, age: number, breed: string) {
super(name, age);
this._breed = breed;
}

get breed(): string {
return this._breed;
}

set breed(breed: string) {
if (!breed || breed.trim().length === 0) {
throw new Error('Порода не может быть пустой.');
}
this._breed = breed;
}

public makeSound(): string {
return `${this._name} говорит: "Мяу!"`;
}

public getAnimalInfo(): string {
return `Кошка "${this._name}", возраст: ${this._age}, порода: ${this._breed}`;
}

public toString(): string {
return `Кошка: ${this._name} (${this._breed}), возраст: ${this._age}`;
}
}
7 changes: 0 additions & 7 deletions rpgsaga/saga/tests/example.spec.ts

This file was deleted.

80 changes: 80 additions & 0 deletions rpgsaga/saga/tests/lab1.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import { formula, taskA, taskB } from '../src/lab1';

describe('Formula', () => {
it('should return NaN when x is close to 1', () => {
const res = formula(1);
expect(res).toBeNaN();
});

it('should return correct value for formula(1.5)', () => {
const res = formula(1.5);
const expected = Math.pow(1.6, Math.pow(1.5, 2) - 1)
- Math.log10(Math.pow(1.5, 2) - 1)
+ Math.pow(Math.pow(1.5, 2) - 1, 1 / 3);
const roundedExpected = Math.fround(expected * 1e12) / 1e12;
expect(res).toBeCloseTo(roundedExpected, 12);
});

it('should return correct value for formula(2)', () => {
const res = formula(2);
const expected = Math.pow(1.6, Math.pow(2, 2) - 1)
- Math.log10(Math.pow(2, 2) - 1)
+ Math.pow(Math.pow(2, 2) - 1, 1 / 3);
const roundedExpected = Math.fround(expected * 1e12) / 1e12;
expect(res).toBeCloseTo(roundedExpected, 12);
});
});

describe('TaskA', () => {
it('should return correct array for taskA(1.2, 3.7, 0.5)', () => {
const expected: number[] = [];
for (let x = 1.2; x <= 3.7; x += 0.5) {
expected.push(formula(x));
}
const res = taskA(1.2, 3.7, 0.5);
expect(res).toEqual(expected);
});

it('should return correct array for taskA(2.0, 3.0, 0.3)', () => {
const expected: number[] = [];
for (let x = 2.0; x <= 3.0; x += 0.3) {
expected.push(formula(x));
}
const res = taskA(2.0, 3.0, 0.3);
expect(res).toEqual(expected);
});

it('should return empty array when start > end', () => {
const res = taskA(3.0, 2.0, 0.5);
expect(res).toEqual([]);
});
});

describe('TaskB', () => {
it('should return correct values for taskB([1.28, 1.36, 2.47, 3.68, 4.56])', () => {
const input = [1.28, 1.36, 2.47, 3.68, 4.56];
const expected = input.map(x => formula(x));
const res = taskB(input);
expect(res).toEqual(expected);
});

it('should return an empty array for taskB([])', () => {
const res = taskB([]);
expect(res).toEqual([]);
});

it('should handle negative values in the input array for taskB([-0.5, -0.2])', () => {
const input = [-0.5, -0.2];
const expected = input.map(x => formula(x));
const res = taskB(input);
expect(res).toEqual(expected);
});

it('should return correct values for taskB([1.5, 2.0, 2.5]) with custom a = 2.0', () => {
const input = [1.5, 2.0, 2.5];
const a = 2.0;
const expected = input.map(x => formula(x, a));
const res = taskB(input, a);
expect(res).toEqual(expected);
});
});
36 changes: 36 additions & 0 deletions rpgsaga/saga/tests/lab2.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { Cat } from '../src/lab2';

describe('Cat Class - Lab 2', () => {
let cat: Cat;

beforeEach(() => {
cat = new Cat('Барсик', 3, 'Шотландская');
});

it('should create a cat with correct properties', () => {
expect(cat.name).toBe('Барсик');
expect(cat.age).toBe(3);
expect(cat.breed).toBe('Шотландская');
});

it('should set and validate name', () => {
cat.name = 'Мурзик';
expect(cat.name).toBe('Мурзик');
expect(() => { cat.name = ''; }).toThrow('Значение не может быть пустым.');
});

it('should set and validate age', () => {
cat.age = 5;
expect(cat.age).toBe(5);
expect(() => { cat.age = -1; }).toThrow('Возраст должен быть от 0 до 30 лет.');
});

it('should celebrate birthday and increment age', () => {
cat.celebrateBirthday();
expect(cat.age).toBe(4);
});

it('should return correct info', () => {
expect(cat.getInfo()).toBe('Имя: Барсик, Возраст: 3, Порода: Шотландская');
});
});
49 changes: 49 additions & 0 deletions rpgsaga/saga/tests/lab3.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { Cat } from '../src/lab3';

describe('Cat Class - Lab 3', () => {
let cat: Cat;

beforeEach(() => {
cat = new Cat('Барсик', 3, 'Шотландская');
});

it('should create a cat with correct properties', () => {
expect(cat.getAnimalInfo()).toBe('Кошка "Барсик", возраст: 3, порода: Шотландская');
});

it('should make a sound', () => {
expect(cat.makeSound()).toBe('Барсик говорит: "Мяу!"');
});

it('should set and get name correctly', () => {
cat.name = 'Мурзик';
expect(cat.name).toBe('Мурзик');
});

it('should throw error for empty name', () => {
expect(() => { cat.name = ''; }).toThrow('Имя не может быть пустым.');
});

it('should set and get breed correctly', () => {
cat.breed = 'Сиамская';
expect(cat.breed).toBe('Сиамская');
});

it('should throw error for empty breed', () => {
expect(() => { cat.breed = ''; }).toThrow('Порода не может быть пустой.');
});

it('should set and validate age', () => {
cat.age = 5;
expect(cat.age).toBe(5);
});

it('should throw error for invalid age', () => {
expect(() => { cat.age = -1; }).toThrow('Возраст должен быть от 0 до 30 лет.');
expect(() => { cat.age = 35; }).toThrow('Возраст должен быть от 0 до 30 лет.');
});

it('should return correct toString representation', () => {
expect(cat.toString()).toBe('Кошка: Барсик (Шотландская), возраст: 3');
});
});