From 3ef92fc74df756ac3a6bec727475fee011a99870 Mon Sep 17 00:00:00 2001 From: Kate-Nits Date: Sun, 14 Sep 2025 17:15:30 +0300 Subject: [PATCH 01/94] Added lib_point, lib_point3d, lib_circle, lib_sphere with full implementation .cpp and .h --- CMakeLists.txt | 4 ++++ lib_circle/CMakeLists.txt | 2 ++ lib_circle/circle.cpp | 39 ++++++++++++++++++++++++++++++++++++ lib_circle/circle.h | 25 +++++++++++++++++++++++ lib_point/CMakeLists.txt | 1 + lib_point/point.cpp | 41 ++++++++++++++++++++++++++++++++++++++ lib_point/point.h | 23 +++++++++++++++++++++ lib_point3d/CMakeLists.txt | 2 ++ lib_point3d/point3d.cpp | 34 +++++++++++++++++++++++++++++++ lib_point3d/point3d.h | 22 ++++++++++++++++++++ lib_sphere/CMakeLists.txt | 4 ++++ lib_sphere/sphere.cpp | 22 ++++++++++++++++++++ lib_sphere/sphere.h | 22 ++++++++++++++++++++ tests/test_circle.cpp | 15 ++++++++++++++ tests/test_point.cpp | 15 ++++++++++++++ tests/test_point3d.cpp | 15 ++++++++++++++ tests/test_sphere.cpp | 15 ++++++++++++++ 17 files changed, 301 insertions(+) create mode 100644 lib_circle/CMakeLists.txt create mode 100644 lib_circle/circle.cpp create mode 100644 lib_circle/circle.h create mode 100644 lib_point/CMakeLists.txt create mode 100644 lib_point/point.cpp create mode 100644 lib_point/point.h create mode 100644 lib_point3d/CMakeLists.txt create mode 100644 lib_point3d/point3d.cpp create mode 100644 lib_point3d/point3d.h create mode 100644 lib_sphere/CMakeLists.txt create mode 100644 lib_sphere/sphere.cpp create mode 100644 lib_sphere/sphere.h create mode 100644 tests/test_circle.cpp create mode 100644 tests/test_point.cpp create mode 100644 tests/test_point3d.cpp create mode 100644 tests/test_sphere.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 2ed05fc1..20efda12 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -14,6 +14,10 @@ include(cmake/function.cmake) # подхватываем функции, # и для создания исполняемого проекта в отдельные функции add_subdirectory(lib_easy_example) # подключаем дополнительный CMakeLists.txt из подкаталога с именем lib_easy_example +add_subdirectory(lib_point) +add_subdirectory(lib_circle) +add_subdirectory(lib_point3d) +add_subdirectory(lib_sphere) add_subdirectory(main) # подключаем дополнительный CMakeLists.txt из подкаталога с именем main option(BTEST "build test?" ON) # указываем подключаем ли google-тесты (ON или YES) или нет (OFF или NO) diff --git a/lib_circle/CMakeLists.txt b/lib_circle/CMakeLists.txt new file mode 100644 index 00000000..51eabd81 --- /dev/null +++ b/lib_circle/CMakeLists.txt @@ -0,0 +1,2 @@ +create_project_lib(Circle) +add_depend(Circle Point lib_point) \ No newline at end of file diff --git a/lib_circle/circle.cpp b/lib_circle/circle.cpp new file mode 100644 index 00000000..78d07e8c --- /dev/null +++ b/lib_circle/circle.cpp @@ -0,0 +1,39 @@ +// Copyright 2025 Ekaterina Ushnitskaya + +#include +#include +//#include "../lib_point/point.h" +#include "../lib_circle/circle.h" + +Circle::Circle() { + Point a; + int rad = 0; + _center = a; + _radius = rad; +} +Circle::Circle(Point dot, int rad) { + _center = dot; + _radius = rad; +} + +Circle::Circle(const Circle& other) { + if (&other == NULL) { + throw std::logic_error("Error!!!"); + } + _center = other._center; + _radius = other._radius; +} + +Point Circle::get_center() const { + return _center; +} +int Circle::get_radius() const { + return _radius; +} + +void Circle::set_center(Point dot) { + _center = dot; +} +void Circle::set_radius(int rad) { + _radius = rad; +} \ No newline at end of file diff --git a/lib_circle/circle.h b/lib_circle/circle.h new file mode 100644 index 00000000..3666d25e --- /dev/null +++ b/lib_circle/circle.h @@ -0,0 +1,25 @@ +// Copyright 2025 Ekaterina Ushnitskaya + +#ifndef LIB_CIRCLE_CIRCLE_H +#define LIB_CIRCLE_CIRCLE_H + +#include "../lib_point/point.h" + + +class Circle { +protected: + Point _center; + int _radius; +public: + Circle(); + Circle(Point, int); + Circle(const Circle&); + + Point get_center() const; + int get_radius() const; + + void set_center(Point); + void set_radius(int); +}; + +#endif // LIB_CIRCLE_CIRCLE_H diff --git a/lib_point/CMakeLists.txt b/lib_point/CMakeLists.txt new file mode 100644 index 00000000..baf8c535 --- /dev/null +++ b/lib_point/CMakeLists.txt @@ -0,0 +1 @@ +create_project_lib(Point) \ No newline at end of file diff --git a/lib_point/point.cpp b/lib_point/point.cpp new file mode 100644 index 00000000..79e92958 --- /dev/null +++ b/lib_point/point.cpp @@ -0,0 +1,41 @@ +// Copyright 2025 Ekaterina Ushnitskaya + +#include +#include +#include +#include "../lib_point/point.h" + +Point::Point() { + int ox = 0, oy = 0; + _ox = ox; + _oy = oy; +} +Point::Point(int ox, int oy) { + _ox = ox; + _oy = oy; +} +Point::Point(const Point& other) { + if (&other == NULL) { + throw std::logic_error("Error!!!"); + } + _ox = other._ox; + _oy = other._oy; +} + +int Point::get_ox() const { + return _ox; +} +int Point::get_oy() const { + return _oy; +} +void Point::set_ox(int ox) { + _ox = ox; +} +void Point::set_oy(int oy) { + _oy = oy; +} + +double Point::distance_to(const Point& other) const { + double res = std::sqrt(std::pow(_ox - other._ox, 2) + std::pow(_oy - other._oy, 2)); + return res; +} \ No newline at end of file diff --git a/lib_point/point.h b/lib_point/point.h new file mode 100644 index 00000000..95bfcb4a --- /dev/null +++ b/lib_point/point.h @@ -0,0 +1,23 @@ +// Copyright 2025 Ekaterina Ushnitskaya + +#ifndef LIB_POINT_POINT_H +#define LIB_POINT_POINT_H + +class Point { +protected: + int _ox; + int _oy; +public: + Point(); + Point(int, int); + Point(const Point&); + + int get_ox() const; + int get_oy() const; + void set_ox(int ox); + void set_oy(int oy); + + double distance_to(const Point&) const; // +}; + +#endif // LIB_POINT_POINT_H \ No newline at end of file diff --git a/lib_point3d/CMakeLists.txt b/lib_point3d/CMakeLists.txt new file mode 100644 index 00000000..d87a61b5 --- /dev/null +++ b/lib_point3d/CMakeLists.txt @@ -0,0 +1,2 @@ +create_project_lib(Point3d) +add_depend(Point3d Point lib_point) \ No newline at end of file diff --git a/lib_point3d/point3d.cpp b/lib_point3d/point3d.cpp new file mode 100644 index 00000000..9d943967 --- /dev/null +++ b/lib_point3d/point3d.cpp @@ -0,0 +1,34 @@ +// Copyright 2025 Ekaterina Ushnitskaya + +#include +#include +#include +//#include "../lib_point/point.h" +#include "../lib_point3d/point3d.h" + +Point3D::Point3D() : Point(), _oz(0) {} +Point3D::Point3D(int ox, int oy, int oz) { + _ox = ox; + _oy = oy; + _oz = oz; +} +Point3D::Point3D(const Point3D& other) { + if (&other == NULL) { + throw std::logic_error("Error!!!"); + } + _ox = other._ox; + _oy = other._oy; + _oz = other._oz; +} + +int Point3D::get_oz() const { + return _oz; +} +void Point3D::set_oz(int oz) { + _oz = oz; +} + +double Point3D::distance_to(const Point3D& other) const { + double res = std::sqrt(std::pow(_ox - other._ox, 2) + std::pow(_oy - other._oy, 2) + std::pow(_oz - other._oz, 2)); + return res; +} \ No newline at end of file diff --git a/lib_point3d/point3d.h b/lib_point3d/point3d.h new file mode 100644 index 00000000..2cacd5c5 --- /dev/null +++ b/lib_point3d/point3d.h @@ -0,0 +1,22 @@ +// Copyright 2025 Ekaterina Ushnitskaya + +#ifndef LIB_POINT3D_POINT3D_H +#define LIB_POINT3D_POINT3D_H + +#include "../lib_point/point.h" + +class Point3D : public Point { + int _oz; + +public: + Point3D(); + Point3D(int, int, int); + Point3D(const Point3D&); + + int get_oz() const; + void set_oz(int); + + double distance_to(const Point3D&) const; +}; + +#endif // LIB_POINT3D_POINT3D_H diff --git a/lib_sphere/CMakeLists.txt b/lib_sphere/CMakeLists.txt new file mode 100644 index 00000000..e2b72646 --- /dev/null +++ b/lib_sphere/CMakeLists.txt @@ -0,0 +1,4 @@ +create_project_lib(Sphere) +add_depend(Sphere Point lib_point) +add_depend(Sphere Point3d lib_point3d) +add_depend(Sphere Circle lib_circle) \ No newline at end of file diff --git a/lib_sphere/sphere.cpp b/lib_sphere/sphere.cpp new file mode 100644 index 00000000..f2986382 --- /dev/null +++ b/lib_sphere/sphere.cpp @@ -0,0 +1,22 @@ +// Copyright 2025 Ekaterina Ushnitskaya + +#include +#include "../lib_sphere/sphere.h" +/////////// ??? + +Sphere::Sphere() : Circle(), _center3D(Point3D()) {} +Sphere::Sphere(Point3D center, int radius) { + _center3D = center; + _radius = radius; +} +Sphere::Sphere(const Sphere& other) { + _center3D = other._center3D; + _radius = other._radius; +} + +Point3D Sphere::get_center3D() const { + return _center3D; +} +void Sphere::set_center3D(Point3D center) { + _center3D = center; +} \ No newline at end of file diff --git a/lib_sphere/sphere.h b/lib_sphere/sphere.h new file mode 100644 index 00000000..88704bf1 --- /dev/null +++ b/lib_sphere/sphere.h @@ -0,0 +1,22 @@ +// Copyright 2025 Ekaterina Ushnitskaya + +#ifndef LIB_SPHERE_SPHERE_H +#define LIB_SPHERE_SPHERE_H +#include + +#include "../lib_point/point.h" +#include "../lib_point3d/point3d.h" +#include "../lib_circle/circle.h" + +class Sphere : public Circle { + Point3D _center3D; +public: + Sphere(); + Sphere(Point3D, int); + Sphere(const Sphere&); + + Point3D get_center3D() const; + void set_center3D(Point3D); +}; + +#endif // LIB_SPHERE_SPHERE_H diff --git a/tests/test_circle.cpp b/tests/test_circle.cpp new file mode 100644 index 00000000..b22d51cc --- /dev/null +++ b/tests/test_circle.cpp @@ -0,0 +1,15 @@ +// Copyright 2025 Ekaterina Ushnitskaya + +#include +#include "../lib_circle/circle.h" + +#define EPSILON 0.000001 + +TEST(TestEasyExampleLib, can_div) { + // Arrange + int x = 10; + int y = 2; + + // Act & Assert + ASSERT_NO_THROW(division(x, y)); +} \ No newline at end of file diff --git a/tests/test_point.cpp b/tests/test_point.cpp new file mode 100644 index 00000000..7a28d17f --- /dev/null +++ b/tests/test_point.cpp @@ -0,0 +1,15 @@ +// Copyright 2025 Ekaterina Ushnitskaya + +#include +#include "../lib_point/point.h" + +#define EPSILON 0.000001 + +TEST(TestEasyExampleLib, can_div) { + // Arrange + int x = 10; + int y = 2; + + // Act & Assert + ASSERT_NO_THROW(division(x, y)); +} \ No newline at end of file diff --git a/tests/test_point3d.cpp b/tests/test_point3d.cpp new file mode 100644 index 00000000..a15d5794 --- /dev/null +++ b/tests/test_point3d.cpp @@ -0,0 +1,15 @@ +// Copyright 2025 Ekaterina Ushnitskaya + +#include +#include "../lib_point3d/point3d.h" + +#define EPSILON 0.000001 + +TEST(TestEasyExampleLib, can_div) { + // Arrange + int x = 10; + int y = 2; + + // Act & Assert + ASSERT_NO_THROW(division(x, y)); +} \ No newline at end of file diff --git a/tests/test_sphere.cpp b/tests/test_sphere.cpp new file mode 100644 index 00000000..a2133720 --- /dev/null +++ b/tests/test_sphere.cpp @@ -0,0 +1,15 @@ +// Copyright 2025 Ekaterina Ushnitskaya + +#include +#include "../lib_sphere/sphere.h" + +#define EPSILON 0.000001 + +TEST(TestEasyExampleLib, can_div) { + // Arrange + int x = 10; + int y = 2; + + // Act & Assert + ASSERT_NO_THROW(division(x, y)); +} \ No newline at end of file From a0807ddcb8a0f890ec29992e88aae7b3e041c213 Mon Sep 17 00:00:00 2001 From: Kate-Nits Date: Mon, 15 Sep 2025 01:53:47 +0300 Subject: [PATCH 02/94] Changed double to float, implemented test_point --- lib_circle/circle.cpp | 15 +++------ lib_point/point.cpp | 17 ++++------ lib_point/point.h | 2 +- lib_point3d/point3d.cpp | 13 ++++---- lib_point3d/point3d.h | 2 +- tests/test_circle.cpp | 7 +++- tests/test_easy_example.cpp | 27 +++++++++++++--- tests/test_point.cpp | 64 +++++++++++++++++++++++++++++++++---- tests/test_point3d.cpp | 6 +++- tests/test_sphere.cpp | 6 +++- 10 files changed, 115 insertions(+), 44 deletions(-) diff --git a/lib_circle/circle.cpp b/lib_circle/circle.cpp index 78d07e8c..4a4ede92 100644 --- a/lib_circle/circle.cpp +++ b/lib_circle/circle.cpp @@ -2,19 +2,12 @@ #include #include -//#include "../lib_point/point.h" +////#include "../lib_point/point.h" #include "../lib_circle/circle.h" -Circle::Circle() { - Point a; - int rad = 0; - _center = a; - _radius = rad; -} -Circle::Circle(Point dot, int rad) { - _center = dot; - _radius = rad; -} + +Circle::Circle() : _center(Point()), _radius(1) {}; +Circle::Circle(Point value_dot, int value_rad) : _center(value_dot), _radius(value_rad) {}; Circle::Circle(const Circle& other) { if (&other == NULL) { diff --git a/lib_point/point.cpp b/lib_point/point.cpp index 79e92958..af9f55f0 100644 --- a/lib_point/point.cpp +++ b/lib_point/point.cpp @@ -5,15 +5,10 @@ #include #include "../lib_point/point.h" -Point::Point() { - int ox = 0, oy = 0; - _ox = ox; - _oy = oy; -} -Point::Point(int ox, int oy) { - _ox = ox; - _oy = oy; -} +Point::Point() : _ox(0), _oy(0) {}; + +Point::Point(int value_ox, int value_oy) : _ox(value_ox), _oy(value_oy) {}; + Point::Point(const Point& other) { if (&other == NULL) { throw std::logic_error("Error!!!"); @@ -35,7 +30,7 @@ void Point::set_oy(int oy) { _oy = oy; } -double Point::distance_to(const Point& other) const { - double res = std::sqrt(std::pow(_ox - other._ox, 2) + std::pow(_oy - other._oy, 2)); +float Point::distance_to(const Point& other) const { + float res = std::sqrt(std::pow(_ox - other._ox, 2) + std::pow(_oy - other._oy, 2)); //std::pow - return res; } \ No newline at end of file diff --git a/lib_point/point.h b/lib_point/point.h index 95bfcb4a..c759f572 100644 --- a/lib_point/point.h +++ b/lib_point/point.h @@ -17,7 +17,7 @@ class Point { void set_ox(int ox); void set_oy(int oy); - double distance_to(const Point&) const; // + float distance_to(const Point&) const; // }; #endif // LIB_POINT_POINT_H \ No newline at end of file diff --git a/lib_point3d/point3d.cpp b/lib_point3d/point3d.cpp index 9d943967..368f6a06 100644 --- a/lib_point3d/point3d.cpp +++ b/lib_point3d/point3d.cpp @@ -7,11 +7,12 @@ #include "../lib_point3d/point3d.h" Point3D::Point3D() : Point(), _oz(0) {} -Point3D::Point3D(int ox, int oy, int oz) { - _ox = ox; - _oy = oy; - _oz = oz; +Point3D::Point3D(int value_ox, int value_oy, int value_oz) { + _ox = value_ox; + _oy = value_oy; + _oz = value_oz; } + Point3D::Point3D(const Point3D& other) { if (&other == NULL) { throw std::logic_error("Error!!!"); @@ -28,7 +29,7 @@ void Point3D::set_oz(int oz) { _oz = oz; } -double Point3D::distance_to(const Point3D& other) const { - double res = std::sqrt(std::pow(_ox - other._ox, 2) + std::pow(_oy - other._oy, 2) + std::pow(_oz - other._oz, 2)); +float Point3D::distance_to(const Point3D& other) const { + float res = std::sqrt(std::pow(_ox - other._ox, 2) + std::pow(_oy - other._oy, 2) + std::pow(_oz - other._oz, 2)); return res; } \ No newline at end of file diff --git a/lib_point3d/point3d.h b/lib_point3d/point3d.h index 2cacd5c5..c778bd33 100644 --- a/lib_point3d/point3d.h +++ b/lib_point3d/point3d.h @@ -16,7 +16,7 @@ class Point3D : public Point { int get_oz() const; void set_oz(int); - double distance_to(const Point3D&) const; + float distance_to(const Point3D&) const; }; #endif // LIB_POINT3D_POINT3D_H diff --git a/tests/test_circle.cpp b/tests/test_circle.cpp index b22d51cc..33b0ddcb 100644 --- a/tests/test_circle.cpp +++ b/tests/test_circle.cpp @@ -3,8 +3,11 @@ #include #include "../lib_circle/circle.h" +//#define EXAMPLE + #define EPSILON 0.000001 +#ifdef EXAMPLE TEST(TestEasyExampleLib, can_div) { // Arrange int x = 10; @@ -12,4 +15,6 @@ TEST(TestEasyExampleLib, can_div) { // Act & Assert ASSERT_NO_THROW(division(x, y)); -} \ No newline at end of file +} +#endif // EXAMPLE + diff --git a/tests/test_easy_example.cpp b/tests/test_easy_example.cpp index 3a67b612..7a303d32 100644 --- a/tests/test_easy_example.cpp +++ b/tests/test_easy_example.cpp @@ -1,17 +1,20 @@ -// Copyright 2024 Marina Usova +// Copyright 2025 Ekaterina Ushnitskaya #include #include "../lib_easy_example/easy_example.h" #define EPSILON 0.000001 +//ASSERT_... - , +//EXPECT_... - , + TEST(TestEasyExampleLib, can_div) { // Arrange int x = 10; int y = 2; // Act & Assert - ASSERT_NO_THROW(division(x, y)); + ASSERT_NO_THROW(division(x, y)); // } TEST(TestEasyExampleLib, can_div_correctly) { @@ -24,8 +27,15 @@ TEST(TestEasyExampleLib, can_div_correctly) { // Assert int expected_result = 3; - EXPECT_EQ(expected_result, actual_result); + EXPECT_EQ(expected_result, actual_result); // } +/* : +EXPECT_NE(a, b) - , a != b +EXPECT_LT(a, b) - , a < b(Less Than) +EXPECT_LE(a, b) - , a <= b(Less or Equal) +EXPECT_GT(a, b) - , a > b(Greater Than) +EXPECT_GE(a, b) - , a >= b(Greater or Equal) +*/ TEST(TestEasyExampleLib, can_div_correctly_with_remainder) { // Arrange @@ -37,7 +47,7 @@ TEST(TestEasyExampleLib, can_div_correctly_with_remainder) { // Assert float expected_result = 1.25; - EXPECT_NEAR(expected_result, actual_result, EPSILON); + EXPECT_NEAR(expected_result, actual_result, EPSILON); //, } TEST(TestEasyExampleLib, throw_when_try_div_by_zero) { @@ -46,5 +56,12 @@ TEST(TestEasyExampleLib, throw_when_try_div_by_zero) { int y = 0; // Act & Assert - ASSERT_ANY_THROW(division(x, y)); + ASSERT_ANY_THROW(division(x, y)); //, } +/* +* : +EXPECT_THROW(calc.Divide(10, 0), std::invalid_argument); //, +EXPECT_TRUE(value > 0); //, +EXPECT_FALSE(result.empty()); //, condition + +*/ diff --git a/tests/test_point.cpp b/tests/test_point.cpp index 7a28d17f..befcb58a 100644 --- a/tests/test_point.cpp +++ b/tests/test_point.cpp @@ -4,12 +4,64 @@ #include "../lib_point/point.h" #define EPSILON 0.000001 +#define TRUE 1 +#define FALSE 0 -TEST(TestEasyExampleLib, can_div) { - // Arrange - int x = 10; - int y = 2; +TEST(TestPointLib, default_constructor) { + // Arrange + Point a; - // Act & Assert - ASSERT_NO_THROW(division(x, y)); + // Act + int actual_result = FALSE; + if (a.get_ox() == 0 && a.get_oy() == 0) { + actual_result = TRUE; + } + + // Assert + int expected_result = TRUE; + EXPECT_EQ(expected_result, actual_result); // +} + +TEST(TestPointLib, parameterized_constructor) { + // Arrange + Point a(5, 27); + + // Act + int actual_result = FALSE; + if (a.get_ox() == 5 && a.get_oy() == 27) { + actual_result = TRUE; + } + + // Assert + int expected_result = TRUE; + EXPECT_EQ(expected_result, actual_result); // +} + +TEST(TestPointLib, copy_constructor) { + // Arrange + Point a(-3, 72); + Point b(a); + + // Act + int actual_result = FALSE; + if (a.get_ox() == b.get_ox() && a.get_oy() == b.get_oy()) { + actual_result = TRUE; + } + + // Assert + int expected_result = TRUE; + EXPECT_EQ(expected_result, actual_result); // +} + +TEST(TestPointLib, can_distance_to) { + // Arrange + Point a(1, 2); + Point b(4, 6); + + // Act + float actual_result = a.distance_to(b); + + // Assert + float expected_result = 5.0; + EXPECT_NEAR(expected_result, actual_result, EPSILON); //, } \ No newline at end of file diff --git a/tests/test_point3d.cpp b/tests/test_point3d.cpp index a15d5794..f51df663 100644 --- a/tests/test_point3d.cpp +++ b/tests/test_point3d.cpp @@ -3,8 +3,11 @@ #include #include "../lib_point3d/point3d.h" +//#define EXAMPLE + #define EPSILON 0.000001 +#ifdef EXAMPLE TEST(TestEasyExampleLib, can_div) { // Arrange int x = 10; @@ -12,4 +15,5 @@ TEST(TestEasyExampleLib, can_div) { // Act & Assert ASSERT_NO_THROW(division(x, y)); -} \ No newline at end of file +} +#endif // EXAMPLE \ No newline at end of file diff --git a/tests/test_sphere.cpp b/tests/test_sphere.cpp index a2133720..8679c9e8 100644 --- a/tests/test_sphere.cpp +++ b/tests/test_sphere.cpp @@ -3,8 +3,11 @@ #include #include "../lib_sphere/sphere.h" +//#define EXAMPLE + #define EPSILON 0.000001 +#ifdef EXAMPLE TEST(TestEasyExampleLib, can_div) { // Arrange int x = 10; @@ -12,4 +15,5 @@ TEST(TestEasyExampleLib, can_div) { // Act & Assert ASSERT_NO_THROW(division(x, y)); -} \ No newline at end of file +} +#endif // EXAMPLE \ No newline at end of file From e0d37dad2930b0b1be8ca5aaf0f2f7e260abcb66 Mon Sep 17 00:00:00 2001 From: Kate-Nits Date: Mon, 15 Sep 2025 02:01:12 +0300 Subject: [PATCH 03/94] implemented test_point3d --- tests/test_point3d.cpp | 68 +++++++++++++++++++++++++++++++++++------- 1 file changed, 58 insertions(+), 10 deletions(-) diff --git a/tests/test_point3d.cpp b/tests/test_point3d.cpp index f51df663..d4ad7750 100644 --- a/tests/test_point3d.cpp +++ b/tests/test_point3d.cpp @@ -3,17 +3,65 @@ #include #include "../lib_point3d/point3d.h" -//#define EXAMPLE - #define EPSILON 0.000001 +#define TRUE 1 +#define FALSE 0 + +TEST(TestPoint3dLib, default_constructor) { + // Arrange + Point3D a; -#ifdef EXAMPLE -TEST(TestEasyExampleLib, can_div) { - // Arrange - int x = 10; - int y = 2; + // Act + int actual_result = FALSE; + if (a.get_ox() == 0 && a.get_oy() == 0 && a.get_oz() == 0) { + actual_result = TRUE; + } - // Act & Assert - ASSERT_NO_THROW(division(x, y)); + // Assert + int expected_result = TRUE; + EXPECT_EQ(expected_result, actual_result); // } -#endif // EXAMPLE \ No newline at end of file + +TEST(TestPoint3dLib, parameterized_constructor) { + // Arrange + Point3D a(5, 27, -45); + + // Act + int actual_result = FALSE; + if (a.get_ox() == 5 && a.get_oy() == 27 && a.get_oz() == -45) { + actual_result = TRUE; + } + + // Assert + int expected_result = TRUE; + EXPECT_EQ(expected_result, actual_result); // +} + +TEST(TestPoint3dLib, copy_constructor) { + // Arrange + Point3D a(-3, 72, 15); + Point3D b(a); + + // Act + int actual_result = FALSE; + if (a.get_ox() == b.get_ox() && a.get_oy() == b.get_oy() && a.get_oz() == b.get_oz()) { + actual_result = TRUE; + } + + // Assert + int expected_result = TRUE; + EXPECT_EQ(expected_result, actual_result); // +} + +TEST(TestPoint3dLib, can_distance_to) { + // Arrange + Point3D a(0, -3, 3); + Point3D b(3, 1, 3); + + // Act + float actual_result = a.distance_to(b); + + // Assert + float expected_result = 5.0; + EXPECT_NEAR(expected_result, actual_result, EPSILON); //, +} \ No newline at end of file From 6ef62b6ecd7a3d3c47be73e0e5545728de25549e Mon Sep 17 00:00:00 2001 From: Kate-Nits Date: Mon, 15 Sep 2025 02:18:37 +0300 Subject: [PATCH 04/94] Added operator == for class point and implemented test_circle --- lib_point/point.cpp | 4 +++ lib_point/point.h | 2 ++ tests/test_circle.cpp | 57 +++++++++++++++++++++++++++++++++++-------- 3 files changed, 53 insertions(+), 10 deletions(-) diff --git a/lib_point/point.cpp b/lib_point/point.cpp index af9f55f0..9e86d577 100644 --- a/lib_point/point.cpp +++ b/lib_point/point.cpp @@ -30,6 +30,10 @@ void Point::set_oy(int oy) { _oy = oy; } +bool Point:: operator == (const Point& other) const noexcept { + return this->_ox == other._ox && this->_oy == other._oy; +} + float Point::distance_to(const Point& other) const { float res = std::sqrt(std::pow(_ox - other._ox, 2) + std::pow(_oy - other._oy, 2)); //std::pow - return res; diff --git a/lib_point/point.h b/lib_point/point.h index c759f572..8a59624b 100644 --- a/lib_point/point.h +++ b/lib_point/point.h @@ -17,6 +17,8 @@ class Point { void set_ox(int ox); void set_oy(int oy); + bool operator == (const Point&) const noexcept; + float distance_to(const Point&) const; // }; diff --git a/tests/test_circle.cpp b/tests/test_circle.cpp index 33b0ddcb..bad947e9 100644 --- a/tests/test_circle.cpp +++ b/tests/test_circle.cpp @@ -3,18 +3,55 @@ #include #include "../lib_circle/circle.h" -//#define EXAMPLE - #define EPSILON 0.000001 +#define TRUE 1 +#define FALSE 0 + +TEST(TestCircleLib, default_constructor) { + // Arrange + Circle A; + Point b; + // Act + int actual_result = FALSE; + if (A.get_center() == b && A.get_radius() == 1) { + actual_result = TRUE; + } + + // Assert + int expected_result = TRUE; + EXPECT_EQ(expected_result, actual_result); // +} + +TEST(TestCircleLib, parameterized_constructor) { + // Arrange + Point a(5, 27); + Circle C(a, 6); -#ifdef EXAMPLE -TEST(TestEasyExampleLib, can_div) { - // Arrange - int x = 10; - int y = 2; + // Act + int actual_result = FALSE; + if (C.get_center() == a && C.get_radius() == 6) { + actual_result = TRUE; + } - // Act & Assert - ASSERT_NO_THROW(division(x, y)); + // Assert + int expected_result = TRUE; + EXPECT_EQ(expected_result, actual_result); // } -#endif // EXAMPLE + +TEST(TestCircleLib, copy_constructor) { + // Arrange + Circle A(Point(-4, 10), 7); + Circle B(A); + + // Act + int actual_result = FALSE; + if (B.get_center() == A.get_center() && B.get_radius() == A.get_radius()) { + actual_result = TRUE; + } + + // Assert + int expected_result = TRUE; + EXPECT_EQ(expected_result, actual_result); // +} + From 6b08d0cf7ed55b04b6fe01e5dae5cc9ff1b864e5 Mon Sep 17 00:00:00 2001 From: Kate-Nits Date: Mon, 15 Sep 2025 02:37:29 +0300 Subject: [PATCH 05/94] Added operator == for class Point3D and implemented test_sphere --- lib_circle/circle.h | 2 +- lib_point3d/point3d.cpp | 4 +++ lib_point3d/point3d.h | 2 ++ tests/test_sphere.cpp | 56 +++++++++++++++++++++++++++++++++-------- 4 files changed, 53 insertions(+), 11 deletions(-) diff --git a/lib_circle/circle.h b/lib_circle/circle.h index 3666d25e..fb3d46ca 100644 --- a/lib_circle/circle.h +++ b/lib_circle/circle.h @@ -7,8 +7,8 @@ class Circle { -protected: Point _center; +protected: int _radius; public: Circle(); diff --git a/lib_point3d/point3d.cpp b/lib_point3d/point3d.cpp index 368f6a06..cda174d4 100644 --- a/lib_point3d/point3d.cpp +++ b/lib_point3d/point3d.cpp @@ -29,6 +29,10 @@ void Point3D::set_oz(int oz) { _oz = oz; } +bool Point3D::operator==(const Point3D& other) const noexcept { + return this->_ox == other._ox && this->_oy == other._oy && this->_oy == other._oy; +} + float Point3D::distance_to(const Point3D& other) const { float res = std::sqrt(std::pow(_ox - other._ox, 2) + std::pow(_oy - other._oy, 2) + std::pow(_oz - other._oz, 2)); return res; diff --git a/lib_point3d/point3d.h b/lib_point3d/point3d.h index c778bd33..36759756 100644 --- a/lib_point3d/point3d.h +++ b/lib_point3d/point3d.h @@ -16,6 +16,8 @@ class Point3D : public Point { int get_oz() const; void set_oz(int); + bool operator == (const Point3D&) const noexcept; + float distance_to(const Point3D&) const; }; diff --git a/tests/test_sphere.cpp b/tests/test_sphere.cpp index 8679c9e8..7b43f08e 100644 --- a/tests/test_sphere.cpp +++ b/tests/test_sphere.cpp @@ -3,17 +3,53 @@ #include #include "../lib_sphere/sphere.h" -//#define EXAMPLE - #define EPSILON 0.000001 +#define TRUE 1 +#define FALSE 0 + +TEST(TestSphereLib, default_constructor) { + // Arrange + Sphere A; + Point3D b; + // Act + int actual_result = FALSE; + if (A.get_center3D() == b && A.get_radius() == 1) { + actual_result = TRUE; + } + + // Assert + int expected_result = TRUE; + EXPECT_EQ(expected_result, actual_result); // +} -#ifdef EXAMPLE -TEST(TestEasyExampleLib, can_div) { - // Arrange - int x = 10; - int y = 2; +TEST(TestSphereLib, parameterized_constructor) { + // Arrange + Point3D a(5, 27, 14); + Sphere C(a, 6); - // Act & Assert - ASSERT_NO_THROW(division(x, y)); + // Act + int actual_result = FALSE; + if (C.get_center3D() == a && C.get_radius() == 6) { + actual_result = TRUE; + } + + // Assert + int expected_result = TRUE; + EXPECT_EQ(expected_result, actual_result); // } -#endif // EXAMPLE \ No newline at end of file + +TEST(TestSphereLib, copy_constructor) { + // Arrange + Sphere A(Point3D(-4, 10, 54), 7); + Sphere B(A); + + // Act + int actual_result = FALSE; + if (B.get_center3D() == A.get_center3D() && B.get_radius() == A.get_radius()) { + actual_result = TRUE; + } + + // Assert + int expected_result = TRUE; + EXPECT_EQ(expected_result, actual_result); // +} \ No newline at end of file From 820ae8cf8ac77d3771abfef572381b0005acef45 Mon Sep 17 00:00:00 2001 From: Kate-Nits Date: Mon, 15 Sep 2025 04:19:51 +0300 Subject: [PATCH 06/94] Did application: show position of two circle or two srhere with user input or view all location options --- main/main.cpp | 239 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 237 insertions(+), 2 deletions(-) diff --git a/main/main.cpp b/main/main.cpp index 217f8971..3aa41ed7 100644 --- a/main/main.cpp +++ b/main/main.cpp @@ -1,6 +1,20 @@ -// Copyright 2024 Marina Usova +// Copyright 2025 Ekaterina Ushnitskaya + +#include +#include +#include + +//#define EASY_EXAMPLE +//#define POSITION_OF_TWO_CIRCLE +#define POSITION_OF_TWO_SPHERE + +//#define USER_INPUT + +void set_color(int text_color, int bg_color) { + HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); + SetConsoleTextAttribute(hConsole, (bg_color << 4) | text_color); +} -#define EASY_EXAMPLE #ifdef EASY_EXAMPLE #include @@ -35,3 +49,224 @@ int main() { } #endif // EASY_EXAMPLE + +enum Location { intersecting, do_not_intersecting, tangent, inside, coinside }; +//tangent - , intersecting - , inside - , coinside - +void print_result(const std::string& description, Location result) { + std::cout << description << ": "; + switch (result) { + case coinside: std::cout << "COINCIDE"; break; + case inside: std::cout << "INSIDE"; break; + case tangent: std::cout << "TANGENT"; break; + case intersecting: std::cout << "INTERSECTING"; break; + case do_not_intersecting: std::cout << "DO_NOT_INTERSECT"; break; + } + std::cout << std::endl; +} + +#ifdef POSITION_OF_TWO_CIRCLE +#include "../lib_circle/circle.h" + +Location check_circle_position(const Circle& circle1, const Circle& circle2) { + Point center1 = circle1.get_center(); + Point center2 = circle2.get_center(); + int radius1 = circle1.get_radius(); + int radius2 = circle2.get_radius(); + + double distance = center1.distance_to(center2); + int sum_rad = radius1 + radius2; + int abs_difference = std::abs(radius1 - radius2); // : radius1 - radius2 + + if (distance == 0 && radius1 == radius2) { + return coinside; // + } + else if (distance < abs_difference) { + return inside; // + } + else if (distance == abs_difference) { + return tangent; // + } + else if (distance < sum_rad && distance > abs_difference) { + return intersecting; // + } + else if (distance == sum_rad) { + return tangent; // + } + else { + return do_not_intersecting; // + } +} + +int main() { + std::cout << "POSITION OF TWO CIRCLES" << std::endl; + std::cout << "=======================================" << std::endl; + +#ifdef USER_INPUT + int value_dot1_ox, value_dot1_oy, value_radius1, value_dot2_ox, value_dot2_oy, value_radius2; + std::cout << "Input center of the first circle: " << std::endl << "Input ox "; + std::cin >> value_dot1_ox; + std::cout << "Input oy "; + std::cin >> value_dot1_oy; + std::cout << "Input radius of the first circle: "; + std::cin >> value_radius1; + Circle circle1(Point(value_dot1_ox, value_dot1_oy), value_radius1); + std::cout << "Input center of the second circle: " << std::endl << "Input ox "; + std::cin >> value_dot2_ox; + std::cout << "Input oy "; + std::cin >> value_dot2_oy; + std::cout << "Input radius of the second circle: "; + std::cin >> value_radius2; + Circle circle2(Point(value_dot2_ox, value_dot2_oy), value_radius2); + Location result = check_circle_position(circle1, circle2); + set_color(12, 0); + std::cout << "RESULT:"; + set_color(7, 0); + print_result(" ", result); +#else + // 1 + Circle circle1(Point(0, 0), 5); + Circle circle2(Point(0, 0), 5); + Location result1 = check_circle_position(circle1, circle2); + print_result("1 Coincide circles (same center, same radius)", result1); + + // 2 + Circle circle3(Point(0, 0), 10); + Circle circle4(Point(1, 1), 2); + Location result2 = check_circle_position(circle3, circle4); + print_result("2 One circle inside another", result2); + + // 3 + Circle circle5(Point(0, 0), 5); + Circle circle6(Point(3, 0), 2); + Location result3 = check_circle_position(circle5, circle6); + print_result("3 Internal tangent", result3); + + // 4 + Circle circle7(Point(0, 0), 3); + Circle circle8(Point(4, 0), 3); + Location result4 = check_circle_position(circle7, circle8); + print_result("4 Intersecting circles", result4); + + // 5 + Circle circle9(Point(0, 0), 3); + Circle circle10(Point(8, 0), 5); + Location result5 = check_circle_position(circle9, circle10); + print_result("5 External tangent", result5); + + // 6 + Circle circle11(Point(0, 0), 2); + Circle circle12(Point(10, 0), 3); + Location result6 = check_circle_position(circle11, circle12); + print_result("6 Non-intersecting circles", result6); +#endif // USER_INPUT + std::cout << "=======================================" << std::endl; + return 0; +} +#endif // POSITION_OF_TWO_CIRCLE + +#ifdef POSITION_OF_TWO_SPHERE +#include "../lib_sphere/sphere.h" + +Location check_sphere_position(const Sphere& sphere1, const Sphere& sphere2) { + Point3D center1 = sphere1.get_center3D(); + Point3D center2 = sphere2.get_center3D(); + int radius1 = sphere1.get_radius(); + int radius2 = sphere2.get_radius(); + + double distance = center1.distance_to(center2); + int sum_rad = radius1 + radius2; + int abs_difference = std::abs(radius1 - radius2); + + if (distance == 0 && radius1 == radius2) { + return coinside; // + } + else if (distance < abs_difference) { + return inside; // + } + else if (distance == abs_difference) { + return tangent; // + } + else if (distance < sum_rad && distance > abs_difference) { + return intersecting; // + } + else if (distance == sum_rad) { + return tangent; // + } + else { + return do_not_intersecting; // + } +} +int main() { + std::cout << "POSITION OF TWO SPHERE" << std::endl; + std::cout << "=======================================" << std::endl; + +#ifdef USER_INPUT + int value_dot1_ox, value_dot1_oy, value_dot1_oz, value_radius1, value_dot2_ox, value_dot2_oy, value_dot2_oz, value_radius2; + std::cout << "Input center of the first sphere: " << std::endl << "Input ox "; + std::cin >> value_dot1_ox; + std::cout << "Input oy "; + std::cin >> value_dot1_oy; + std::cout << "Input oz "; + std::cin >> value_dot1_oz; + std::cout << "Input radius of the first sphere: "; + std::cin >> value_radius1; + Sphere sphere1(Point3D(value_dot1_ox, value_dot1_oy, value_dot1_oz), value_radius1); + std::cout << "Input center of the second sphere: " << std::endl << "Input ox "; + std::cin >> value_dot2_ox; + std::cout << "Input oy "; + std::cin >> value_dot2_oy; + std::cout << "Input oz "; + std::cin >> value_dot2_oz; + std::cout << "Input radius of the second sphere: "; + std::cin >> value_radius2; + Sphere sphere2(Point3D(value_dot2_ox, value_dot2_oy, value_dot2_oz), value_radius2); + Location result = check_sphere_position(sphere1, sphere2); + set_color(12, 0); + std::cout << "RESULT:"; + set_color(7, 0); + print_result(" ", result); + std::cout << "=======================================" << std::endl; +#else + // 1 caths + Sphere sphere1(Point3D(0, 0, 0), 5); + Sphere sphere2(Point3D(0, 0, 0), 5); + Location result7 = check_sphere_position(sphere1, sphere2); + print_result("1 Coincide spheres (same center, same radius)", result7); + + // 2 + Sphere sphere3(Point3D(0, 0, 0), 8); + Sphere sphere4(Point3D(1, 1, 1), 2); + Location result8 = check_sphere_position(sphere3, sphere4); + print_result("2 One sphere inside another", result8); + + // 3 + Sphere sphere5(Point3D(0, 0, 0), 5); + Sphere sphere6(Point3D(3, 0, 0), 2); + Location result9 = check_sphere_position(sphere5, sphere6); + print_result("3 Internal tangent spheres", result9); + + // 4 + Sphere sphere7(Point3D(0, 0, 0), 4); + Sphere sphere8(Point3D(3, 0, 0), 3); + Location result10 = check_sphere_position(sphere7, sphere8); + print_result("4 Intersecting spheres", result10); + + // 5 + Sphere sphere9(Point3D(0, 0, 0), 3); + Sphere sphere10(Point3D(0, 8, 0), 5); + Location result11 = check_sphere_position(sphere9, sphere10); + print_result("5 External tangent spheres", result11); + + // 6 + Sphere sphere11(Point3D(0, 0, 0), 2); + Sphere sphere12(Point3D(0, 0, 15), 3); + Location result12 = check_sphere_position(sphere11, sphere12); + print_result("6 Non-intersecting spheres", result12); + + std::cout << "=======================================" << std::endl; +#endif // USER_INPUT + + return 0; +} + +#endif // POSITION_OF_TWO_SPHERE From 09d468097c5a6281e2d87af850a6df3084424980 Mon Sep 17 00:00:00 2001 From: Kate-Nits Date: Tue, 16 Sep 2025 13:22:47 +0300 Subject: [PATCH 07/94] Added lib_algorithms which have check_circle_position and check_sphere_position and now these function are not in main --- CMakeLists.txt | 5 ++ lib_algorithms/CMakeLists.txt | 3 ++ lib_algorithms/algorithms.cpp | 71 +++++++++++++++++++++++++ lib_algorithms/algorithms.h | 17 ++++++ lib_mathvector/CMakeLists.txt | 2 + lib_mathvector/MathVector.cpp | 0 lib_mathvector/MathVector.h | 0 lib_matrix/CMakeLists.txt | 3 ++ lib_matrix/matrix.cpp | 0 lib_matrix/matrix.h | 0 lib_triangle_matrix/CMakeLists.txt | 4 ++ lib_triangle_matrix/triangle_matrix.cpp | 0 lib_triangle_matrix/triangle_matrix.h | 0 lib_tvector/CMakeLists.txt | 1 + lib_tvector/tvector.cpp | 0 lib_tvector/tvector.h | 0 main/main.cpp | 65 ++-------------------- tests/test_mathvector.cpp | 0 tests/test_matrix.cpp | 0 tests/test_triangle_matrix.cpp | 0 tests/test_tvector.cpp | 0 21 files changed, 110 insertions(+), 61 deletions(-) create mode 100644 lib_algorithms/CMakeLists.txt create mode 100644 lib_algorithms/algorithms.cpp create mode 100644 lib_algorithms/algorithms.h create mode 100644 lib_mathvector/CMakeLists.txt create mode 100644 lib_mathvector/MathVector.cpp create mode 100644 lib_mathvector/MathVector.h create mode 100644 lib_matrix/CMakeLists.txt create mode 100644 lib_matrix/matrix.cpp create mode 100644 lib_matrix/matrix.h create mode 100644 lib_triangle_matrix/CMakeLists.txt create mode 100644 lib_triangle_matrix/triangle_matrix.cpp create mode 100644 lib_triangle_matrix/triangle_matrix.h create mode 100644 lib_tvector/CMakeLists.txt create mode 100644 lib_tvector/tvector.cpp create mode 100644 lib_tvector/tvector.h create mode 100644 tests/test_mathvector.cpp create mode 100644 tests/test_matrix.cpp create mode 100644 tests/test_triangle_matrix.cpp create mode 100644 tests/test_tvector.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 20efda12..7dade311 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -18,6 +18,11 @@ add_subdirectory(lib_point) add_subdirectory(lib_circle) add_subdirectory(lib_point3d) add_subdirectory(lib_sphere) +add_subdirectory(lib_tvector) +add_subdirectory(lib_mathvector) +add_subdirectory(lib_matrix) +add_subdirectory(lib_triangle_matrix) +add_subdirectory(lib_algorithms) add_subdirectory(main) # подключаем дополнительный CMakeLists.txt из подкаталога с именем main option(BTEST "build test?" ON) # указываем подключаем ли google-тесты (ON или YES) или нет (OFF или NO) diff --git a/lib_algorithms/CMakeLists.txt b/lib_algorithms/CMakeLists.txt new file mode 100644 index 00000000..f1086c21 --- /dev/null +++ b/lib_algorithms/CMakeLists.txt @@ -0,0 +1,3 @@ +create_project_lib(Algorithms) +add_depend(Algorithms Circle lib_circle) +add_depend(Algorithms Sphere lib_sphere) \ No newline at end of file diff --git a/lib_algorithms/algorithms.cpp b/lib_algorithms/algorithms.cpp new file mode 100644 index 00000000..3be8b297 --- /dev/null +++ b/lib_algorithms/algorithms.cpp @@ -0,0 +1,71 @@ +// Copyright 2025 Ekaterina Ushnitskaya + +#include +#include +#include + +#include "../lib_circle/circle.h" +#include "../lib_sphere/sphere.h" + +enum Location { intersecting, do_not_intersecting, tangent, inside, coinside }; +//tangent - , intersecting - , inside - , coinside - + +Location check_circle_position(const Circle& circle1, const Circle& circle2) { + Point center1 = circle1.get_center(); + Point center2 = circle2.get_center(); + int radius1 = circle1.get_radius(); + int radius2 = circle2.get_radius(); + + double distance = center1.distance_to(center2); + int sum_rad = radius1 + radius2; + int abs_difference = std::abs(radius1 - radius2); + + if (distance == 0 && radius1 == radius2) { + return coinside; // + } + else if (distance < abs_difference) { + return inside; // + } + else if (distance == abs_difference) { + return tangent; // + } + else if (distance < sum_rad && distance > abs_difference) { + return intersecting; // + } + else if (distance == sum_rad) { + return tangent; // + } + else { + return do_not_intersecting; // + } +} + +Location check_sphere_position(const Sphere& sphere1, const Sphere& sphere2) { + Point3D center1 = sphere1.get_center3D(); + Point3D center2 = sphere2.get_center3D(); + int radius1 = sphere1.get_radius(); + int radius2 = sphere2.get_radius(); + + double distance = center1.distance_to(center2); + int sum_rad = radius1 + radius2; + int abs_difference = std::abs(radius1 - radius2); + + if (distance == 0 && radius1 == radius2) { + return coinside; // + } + else if (distance < abs_difference) { + return inside; // + } + else if (distance == abs_difference) { + return tangent; // + } + else if (distance < sum_rad && distance > abs_difference) { + return intersecting; // + } + else if (distance == sum_rad) { + return tangent; // + } + else { + return do_not_intersecting; // + } +} \ No newline at end of file diff --git a/lib_algorithms/algorithms.h b/lib_algorithms/algorithms.h new file mode 100644 index 00000000..dc356088 --- /dev/null +++ b/lib_algorithms/algorithms.h @@ -0,0 +1,17 @@ +// Copyright 2025 Ekaterina Ushnitskaya + +#ifndef LIB_ALGORITHMS_H +#define LIB_ALGORITHMS_H + +#include "../lib_circle/circle.h" +#include "../lib_sphere/sphere.h" + +enum Location { intersecting, do_not_intersecting, tangent, inside, coinside }; +//tangent - , intersecting - , inside - , coinside - + +Location check_circle_position(const Circle& circle1, const Circle& circle2); +Location check_sphere_position(const Sphere& sphere1, const Sphere& sphere2); + + + +#endif // LIB_ALGORITHMS_H \ No newline at end of file diff --git a/lib_mathvector/CMakeLists.txt b/lib_mathvector/CMakeLists.txt new file mode 100644 index 00000000..9970b8ab --- /dev/null +++ b/lib_mathvector/CMakeLists.txt @@ -0,0 +1,2 @@ +create_project_lib(MathVector) +add_depend(MathVector TVector lib_tvector) \ No newline at end of file diff --git a/lib_mathvector/MathVector.cpp b/lib_mathvector/MathVector.cpp new file mode 100644 index 00000000..e69de29b diff --git a/lib_mathvector/MathVector.h b/lib_mathvector/MathVector.h new file mode 100644 index 00000000..e69de29b diff --git a/lib_matrix/CMakeLists.txt b/lib_matrix/CMakeLists.txt new file mode 100644 index 00000000..6adf2649 --- /dev/null +++ b/lib_matrix/CMakeLists.txt @@ -0,0 +1,3 @@ +create_project_lib(Matrix) +add_depend(Matrix TVector lib_tvector) +add_depend(Matrix MathVector lib_mathvector) \ No newline at end of file diff --git a/lib_matrix/matrix.cpp b/lib_matrix/matrix.cpp new file mode 100644 index 00000000..e69de29b diff --git a/lib_matrix/matrix.h b/lib_matrix/matrix.h new file mode 100644 index 00000000..e69de29b diff --git a/lib_triangle_matrix/CMakeLists.txt b/lib_triangle_matrix/CMakeLists.txt new file mode 100644 index 00000000..59f4b257 --- /dev/null +++ b/lib_triangle_matrix/CMakeLists.txt @@ -0,0 +1,4 @@ +create_project_lib(TriangleMatrix) +add_depend(TriangleMatrix TVector lib_tvector) +add_depend(TriangleMatrix MathVector lib_mathvector) +add_depend(TriangleMatrix Matrix lib_matrix) \ No newline at end of file diff --git a/lib_triangle_matrix/triangle_matrix.cpp b/lib_triangle_matrix/triangle_matrix.cpp new file mode 100644 index 00000000..e69de29b diff --git a/lib_triangle_matrix/triangle_matrix.h b/lib_triangle_matrix/triangle_matrix.h new file mode 100644 index 00000000..e69de29b diff --git a/lib_tvector/CMakeLists.txt b/lib_tvector/CMakeLists.txt new file mode 100644 index 00000000..a46980e1 --- /dev/null +++ b/lib_tvector/CMakeLists.txt @@ -0,0 +1 @@ +create_project_lib(TVector) \ No newline at end of file diff --git a/lib_tvector/tvector.cpp b/lib_tvector/tvector.cpp new file mode 100644 index 00000000..e69de29b diff --git a/lib_tvector/tvector.h b/lib_tvector/tvector.h new file mode 100644 index 00000000..e69de29b diff --git a/main/main.cpp b/main/main.cpp index 3aa41ed7..21e5be68 100644 --- a/main/main.cpp +++ b/main/main.cpp @@ -4,11 +4,13 @@ #include #include +#include "../lib_algorithms/algorithms.h" + //#define EASY_EXAMPLE //#define POSITION_OF_TWO_CIRCLE #define POSITION_OF_TWO_SPHERE -//#define USER_INPUT +#define USER_INPUT void set_color(int text_color, int bg_color) { HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); @@ -50,8 +52,7 @@ int main() { #endif // EASY_EXAMPLE -enum Location { intersecting, do_not_intersecting, tangent, inside, coinside }; -//tangent - , intersecting - , inside - , coinside - + void print_result(const std::string& description, Location result) { std::cout << description << ": "; switch (result) { @@ -67,36 +68,6 @@ void print_result(const std::string& description, Location result) { #ifdef POSITION_OF_TWO_CIRCLE #include "../lib_circle/circle.h" -Location check_circle_position(const Circle& circle1, const Circle& circle2) { - Point center1 = circle1.get_center(); - Point center2 = circle2.get_center(); - int radius1 = circle1.get_radius(); - int radius2 = circle2.get_radius(); - - double distance = center1.distance_to(center2); - int sum_rad = radius1 + radius2; - int abs_difference = std::abs(radius1 - radius2); // : radius1 - radius2 - - if (distance == 0 && radius1 == radius2) { - return coinside; // - } - else if (distance < abs_difference) { - return inside; // - } - else if (distance == abs_difference) { - return tangent; // - } - else if (distance < sum_rad && distance > abs_difference) { - return intersecting; // - } - else if (distance == sum_rad) { - return tangent; // - } - else { - return do_not_intersecting; // - } -} - int main() { std::cout << "POSITION OF TWO CIRCLES" << std::endl; std::cout << "=======================================" << std::endl; @@ -167,35 +138,7 @@ int main() { #ifdef POSITION_OF_TWO_SPHERE #include "../lib_sphere/sphere.h" -Location check_sphere_position(const Sphere& sphere1, const Sphere& sphere2) { - Point3D center1 = sphere1.get_center3D(); - Point3D center2 = sphere2.get_center3D(); - int radius1 = sphere1.get_radius(); - int radius2 = sphere2.get_radius(); - - double distance = center1.distance_to(center2); - int sum_rad = radius1 + radius2; - int abs_difference = std::abs(radius1 - radius2); - if (distance == 0 && radius1 == radius2) { - return coinside; // - } - else if (distance < abs_difference) { - return inside; // - } - else if (distance == abs_difference) { - return tangent; // - } - else if (distance < sum_rad && distance > abs_difference) { - return intersecting; // - } - else if (distance == sum_rad) { - return tangent; // - } - else { - return do_not_intersecting; // - } -} int main() { std::cout << "POSITION OF TWO SPHERE" << std::endl; std::cout << "=======================================" << std::endl; diff --git a/tests/test_mathvector.cpp b/tests/test_mathvector.cpp new file mode 100644 index 00000000..e69de29b diff --git a/tests/test_matrix.cpp b/tests/test_matrix.cpp new file mode 100644 index 00000000..e69de29b diff --git a/tests/test_triangle_matrix.cpp b/tests/test_triangle_matrix.cpp new file mode 100644 index 00000000..e69de29b diff --git a/tests/test_tvector.cpp b/tests/test_tvector.cpp new file mode 100644 index 00000000..e69de29b From e5be3a9ef269a7eb1282dd74b62822611f24578d Mon Sep 17 00:00:00 2001 From: Kate-Nits Date: Tue, 16 Sep 2025 13:52:49 +0300 Subject: [PATCH 08/94] Added tests copy_constructor_with_throw --- lib_sphere/sphere.cpp | 4 +++- tests/test_circle.cpp | 9 ++++++++- tests/test_point.cpp | 9 ++++++++- tests/test_point3d.cpp | 9 ++++++++- tests/test_sphere.cpp | 9 ++++++++- 5 files changed, 35 insertions(+), 5 deletions(-) diff --git a/lib_sphere/sphere.cpp b/lib_sphere/sphere.cpp index f2986382..46e6d083 100644 --- a/lib_sphere/sphere.cpp +++ b/lib_sphere/sphere.cpp @@ -2,7 +2,6 @@ #include #include "../lib_sphere/sphere.h" -/////////// ??? Sphere::Sphere() : Circle(), _center3D(Point3D()) {} Sphere::Sphere(Point3D center, int radius) { @@ -10,6 +9,9 @@ Sphere::Sphere(Point3D center, int radius) { _radius = radius; } Sphere::Sphere(const Sphere& other) { + if (&other == NULL) { + throw std::logic_error("Error!!!"); + } _center3D = other._center3D; _radius = other._radius; } diff --git a/tests/test_circle.cpp b/tests/test_circle.cpp index bad947e9..c2bf78fd 100644 --- a/tests/test_circle.cpp +++ b/tests/test_circle.cpp @@ -38,7 +38,7 @@ TEST(TestCircleLib, parameterized_constructor) { EXPECT_EQ(expected_result, actual_result); // } -TEST(TestCircleLib, copy_constructor) { +TEST(TestCircleLib, copy_constructor_without_throw) { // Arrange Circle A(Point(-4, 10), 7); Circle B(A); @@ -54,4 +54,11 @@ TEST(TestCircleLib, copy_constructor) { EXPECT_EQ(expected_result, actual_result); // } +TEST(TestCircleLib, copy_constructor_with_throw) { + // Arrange + Circle* null_pointer = nullptr; + // Act & Assert + EXPECT_THROW({ Circle & bad_reference = *null_pointer; Circle a(bad_reference); }, std::logic_error); +} + diff --git a/tests/test_point.cpp b/tests/test_point.cpp index befcb58a..f0a55ee7 100644 --- a/tests/test_point.cpp +++ b/tests/test_point.cpp @@ -37,7 +37,7 @@ TEST(TestPointLib, parameterized_constructor) { EXPECT_EQ(expected_result, actual_result); // } -TEST(TestPointLib, copy_constructor) { +TEST(TestPointLib, copy_constructor_without_throw) { // Arrange Point a(-3, 72); Point b(a); @@ -53,6 +53,13 @@ TEST(TestPointLib, copy_constructor) { EXPECT_EQ(expected_result, actual_result); // } +TEST(TestPointLib, copy_constructor_with_throw) { + // Arrange + Point* null_pointer = nullptr; + // Act & Assert + EXPECT_THROW({ Point & bad_reference = *null_pointer; Point a(bad_reference); }, std::logic_error); +} + TEST(TestPointLib, can_distance_to) { // Arrange Point a(1, 2); diff --git a/tests/test_point3d.cpp b/tests/test_point3d.cpp index d4ad7750..7a5726c0 100644 --- a/tests/test_point3d.cpp +++ b/tests/test_point3d.cpp @@ -37,7 +37,7 @@ TEST(TestPoint3dLib, parameterized_constructor) { EXPECT_EQ(expected_result, actual_result); // } -TEST(TestPoint3dLib, copy_constructor) { +TEST(TestPoint3dLib, copy_constructor_without_throw) { // Arrange Point3D a(-3, 72, 15); Point3D b(a); @@ -53,6 +53,13 @@ TEST(TestPoint3dLib, copy_constructor) { EXPECT_EQ(expected_result, actual_result); // } +TEST(TestPoint3dLib, copy_constructor_with_throw) { + // Arrange + Point3D* null_pointer = nullptr; + // Act & Assert + EXPECT_THROW({ Point3D & bad_reference = *null_pointer; Point3D a(bad_reference); }, std::logic_error); +} + TEST(TestPoint3dLib, can_distance_to) { // Arrange Point3D a(0, -3, 3); diff --git a/tests/test_sphere.cpp b/tests/test_sphere.cpp index 7b43f08e..cfa25302 100644 --- a/tests/test_sphere.cpp +++ b/tests/test_sphere.cpp @@ -38,7 +38,7 @@ TEST(TestSphereLib, parameterized_constructor) { EXPECT_EQ(expected_result, actual_result); // } -TEST(TestSphereLib, copy_constructor) { +TEST(TestSphereLib, copy_constructor_without_throw) { // Arrange Sphere A(Point3D(-4, 10, 54), 7); Sphere B(A); @@ -52,4 +52,11 @@ TEST(TestSphereLib, copy_constructor) { // Assert int expected_result = TRUE; EXPECT_EQ(expected_result, actual_result); // +} + +TEST(TestSphereLib, copy_constructor_with_throw) { + // Arrange + Sphere* null_pointer = nullptr; + // Act & Assert + EXPECT_THROW({ Sphere & bad_reference = *null_pointer; Sphere a(bad_reference); }, std::logic_error); } \ No newline at end of file From a6365f10b35a19137959282b4ec066d94692646a Mon Sep 17 00:00:00 2001 From: Kate-Nits Date: Tue, 16 Sep 2025 14:04:49 +0300 Subject: [PATCH 09/94] Added tests parameterized_constructor_with_radius_less_than_zero --- lib_circle/circle.cpp | 8 +++++++- lib_sphere/sphere.cpp | 7 +++++-- tests/test_circle.cpp | 8 +++++++- tests/test_sphere.cpp | 8 +++++++- 4 files changed, 26 insertions(+), 5 deletions(-) diff --git a/lib_circle/circle.cpp b/lib_circle/circle.cpp index 4a4ede92..e50cf301 100644 --- a/lib_circle/circle.cpp +++ b/lib_circle/circle.cpp @@ -7,7 +7,13 @@ Circle::Circle() : _center(Point()), _radius(1) {}; -Circle::Circle(Point value_dot, int value_rad) : _center(value_dot), _radius(value_rad) {}; +Circle::Circle(Point value_dot, int value_rad) { + if (value_rad < 0) { + throw std::logic_error("Error!!! The radius can't be less than zero"); + } + _center = value_dot; + _radius = value_rad; +} Circle::Circle(const Circle& other) { if (&other == NULL) { diff --git a/lib_sphere/sphere.cpp b/lib_sphere/sphere.cpp index 46e6d083..129944b9 100644 --- a/lib_sphere/sphere.cpp +++ b/lib_sphere/sphere.cpp @@ -4,9 +4,12 @@ #include "../lib_sphere/sphere.h" Sphere::Sphere() : Circle(), _center3D(Point3D()) {} -Sphere::Sphere(Point3D center, int radius) { +Sphere::Sphere(Point3D center, int value_radius) { + if (value_radius < 0) { + throw std::logic_error("Error!!! The radius can't be less than zero"); + } _center3D = center; - _radius = radius; + _radius = value_radius; } Sphere::Sphere(const Sphere& other) { if (&other == NULL) { diff --git a/tests/test_circle.cpp b/tests/test_circle.cpp index c2bf78fd..a4866041 100644 --- a/tests/test_circle.cpp +++ b/tests/test_circle.cpp @@ -22,7 +22,7 @@ TEST(TestCircleLib, default_constructor) { EXPECT_EQ(expected_result, actual_result); // } -TEST(TestCircleLib, parameterized_constructor) { +TEST(TestCircleLib, parameterized_constructor_with_normal_radius) { // Arrange Point a(5, 27); Circle C(a, 6); @@ -37,6 +37,12 @@ TEST(TestCircleLib, parameterized_constructor) { int expected_result = TRUE; EXPECT_EQ(expected_result, actual_result); // } +TEST(TestCircleLib, parameterized_constructor_with_radius_less_than_zero) { + // Arrange + Point a(-3, 15); + // Act & Assert + EXPECT_THROW({ Circle C(a, -8); }, std::logic_error); +} TEST(TestCircleLib, copy_constructor_without_throw) { // Arrange diff --git a/tests/test_sphere.cpp b/tests/test_sphere.cpp index cfa25302..7f598982 100644 --- a/tests/test_sphere.cpp +++ b/tests/test_sphere.cpp @@ -22,7 +22,7 @@ TEST(TestSphereLib, default_constructor) { EXPECT_EQ(expected_result, actual_result); // } -TEST(TestSphereLib, parameterized_constructor) { +TEST(TestSphereLib, parameterized_constructor_with_normal_radius) { // Arrange Point3D a(5, 27, 14); Sphere C(a, 6); @@ -37,6 +37,12 @@ TEST(TestSphereLib, parameterized_constructor) { int expected_result = TRUE; EXPECT_EQ(expected_result, actual_result); // } +TEST(TestSphereLib, parameterized_constructor_with_radius_less_than_zero) { + // Arrange + Point3D a(7, 6, 14); + // Act & Assert + EXPECT_THROW({ Sphere C(a, -1); }, std::logic_error); +} TEST(TestSphereLib, copy_constructor_without_throw) { // Arrange From 66af1bb462c389977db9d5b3e0186a34f0e024ca Mon Sep 17 00:00:00 2001 From: Kate-Nits Date: Wed, 17 Sep 2025 15:37:59 +0300 Subject: [PATCH 10/94] Edits made by Marina Andreevna: redone the functions check_position and made it a template, rewrote tests, but i should rewrite the rest of the tests --- lib_algorithms/algorithms.cpp | 61 ----------------------------------- lib_algorithms/algorithms.h | 32 ++++++++++++++++-- lib_circle/circle.h | 1 - lib_sphere/sphere.cpp | 14 ++++++-- lib_sphere/sphere.h | 10 ++++-- main/main.cpp | 34 +++++++++---------- tests/test_point.cpp | 59 ++++++++++++++++----------------- tests/test_sphere.cpp | 6 ++-- 8 files changed, 95 insertions(+), 122 deletions(-) diff --git a/lib_algorithms/algorithms.cpp b/lib_algorithms/algorithms.cpp index 3be8b297..a62aff4a 100644 --- a/lib_algorithms/algorithms.cpp +++ b/lib_algorithms/algorithms.cpp @@ -7,65 +7,4 @@ #include "../lib_circle/circle.h" #include "../lib_sphere/sphere.h" -enum Location { intersecting, do_not_intersecting, tangent, inside, coinside }; -//tangent - , intersecting - , inside - , coinside - -Location check_circle_position(const Circle& circle1, const Circle& circle2) { - Point center1 = circle1.get_center(); - Point center2 = circle2.get_center(); - int radius1 = circle1.get_radius(); - int radius2 = circle2.get_radius(); - - double distance = center1.distance_to(center2); - int sum_rad = radius1 + radius2; - int abs_difference = std::abs(radius1 - radius2); - - if (distance == 0 && radius1 == radius2) { - return coinside; // - } - else if (distance < abs_difference) { - return inside; // - } - else if (distance == abs_difference) { - return tangent; // - } - else if (distance < sum_rad && distance > abs_difference) { - return intersecting; // - } - else if (distance == sum_rad) { - return tangent; // - } - else { - return do_not_intersecting; // - } -} - -Location check_sphere_position(const Sphere& sphere1, const Sphere& sphere2) { - Point3D center1 = sphere1.get_center3D(); - Point3D center2 = sphere2.get_center3D(); - int radius1 = sphere1.get_radius(); - int radius2 = sphere2.get_radius(); - - double distance = center1.distance_to(center2); - int sum_rad = radius1 + radius2; - int abs_difference = std::abs(radius1 - radius2); - - if (distance == 0 && radius1 == radius2) { - return coinside; // - } - else if (distance < abs_difference) { - return inside; // - } - else if (distance == abs_difference) { - return tangent; // - } - else if (distance < sum_rad && distance > abs_difference) { - return intersecting; // - } - else if (distance == sum_rad) { - return tangent; // - } - else { - return do_not_intersecting; // - } -} \ No newline at end of file diff --git a/lib_algorithms/algorithms.h b/lib_algorithms/algorithms.h index dc356088..06e0d86f 100644 --- a/lib_algorithms/algorithms.h +++ b/lib_algorithms/algorithms.h @@ -9,8 +9,36 @@ enum Location { intersecting, do_not_intersecting, tangent, inside, coinside }; //tangent - , intersecting - , inside - , coinside - -Location check_circle_position(const Circle& circle1, const Circle& circle2); -Location check_sphere_position(const Sphere& sphere1, const Sphere& sphere2); +template +Location check_position(const T& circle1, const T& circle2) { + Point center1 = circle1.get_center(); + Point center2 = circle2.get_center(); + int radius1 = circle1.get_radius(); + int radius2 = circle2.get_radius(); + + double distance = center1.distance_to(center2); + int sum_rad = radius1 + radius2; + int abs_difference = std::abs(radius1 - radius2); + + if (distance == 0 && radius1 == radius2) { + return coinside; // + } + else if (distance < abs_difference) { + return inside; // + } + else if (distance == abs_difference) { + return tangent; // + } + else if (distance < sum_rad && distance > abs_difference) { + return intersecting; // + } + else if (distance == sum_rad) { + return tangent; // + } + else { + return do_not_intersecting; // + } +} diff --git a/lib_circle/circle.h b/lib_circle/circle.h index fb3d46ca..1199d14e 100644 --- a/lib_circle/circle.h +++ b/lib_circle/circle.h @@ -8,7 +8,6 @@ class Circle { Point _center; -protected: int _radius; public: Circle(); diff --git a/lib_sphere/sphere.cpp b/lib_sphere/sphere.cpp index 129944b9..74d45592 100644 --- a/lib_sphere/sphere.cpp +++ b/lib_sphere/sphere.cpp @@ -3,7 +3,7 @@ #include #include "../lib_sphere/sphere.h" -Sphere::Sphere() : Circle(), _center3D(Point3D()) {} +Sphere::Sphere() : _center3D(Point3D()), _radius(1) {} Sphere::Sphere(Point3D center, int value_radius) { if (value_radius < 0) { throw std::logic_error("Error!!! The radius can't be less than zero"); @@ -19,9 +19,17 @@ Sphere::Sphere(const Sphere& other) { _radius = other._radius; } -Point3D Sphere::get_center3D() const { +Point3D Sphere::get_center() const { return _center3D; } -void Sphere::set_center3D(Point3D center) { +void Sphere::set_center(Point3D center) { _center3D = center; +} + +int Sphere::get_radius() const { + return _radius; +} + +void Sphere::set_radius(int rad) { + _radius = rad; } \ No newline at end of file diff --git a/lib_sphere/sphere.h b/lib_sphere/sphere.h index 88704bf1..43d3e148 100644 --- a/lib_sphere/sphere.h +++ b/lib_sphere/sphere.h @@ -8,15 +8,19 @@ #include "../lib_point3d/point3d.h" #include "../lib_circle/circle.h" -class Sphere : public Circle { +class Sphere { Point3D _center3D; + int _radius; public: Sphere(); Sphere(Point3D, int); Sphere(const Sphere&); - Point3D get_center3D() const; - void set_center3D(Point3D); + Point3D get_center() const; + void set_center(Point3D); + + int get_radius() const; + void set_radius(int); }; #endif // LIB_SPHERE_SPHERE_H diff --git a/main/main.cpp b/main/main.cpp index 21e5be68..b4c5180c 100644 --- a/main/main.cpp +++ b/main/main.cpp @@ -7,10 +7,10 @@ #include "../lib_algorithms/algorithms.h" //#define EASY_EXAMPLE -//#define POSITION_OF_TWO_CIRCLE -#define POSITION_OF_TWO_SPHERE +#define POSITION_OF_TWO_CIRCLE +//#define POSITION_OF_TWO_SPHERE -#define USER_INPUT +//#define USER_INPUT void set_color(int text_color, int bg_color) { HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); @@ -88,7 +88,7 @@ int main() { std::cout << "Input radius of the second circle: "; std::cin >> value_radius2; Circle circle2(Point(value_dot2_ox, value_dot2_oy), value_radius2); - Location result = check_circle_position(circle1, circle2); + Location result = check_position(circle1, circle2); set_color(12, 0); std::cout << "RESULT:"; set_color(7, 0); @@ -97,37 +97,37 @@ int main() { // 1 Circle circle1(Point(0, 0), 5); Circle circle2(Point(0, 0), 5); - Location result1 = check_circle_position(circle1, circle2); + Location result1 = check_position(circle1, circle2); print_result("1 Coincide circles (same center, same radius)", result1); // 2 Circle circle3(Point(0, 0), 10); Circle circle4(Point(1, 1), 2); - Location result2 = check_circle_position(circle3, circle4); + Location result2 = check_position(circle3, circle4); print_result("2 One circle inside another", result2); // 3 Circle circle5(Point(0, 0), 5); Circle circle6(Point(3, 0), 2); - Location result3 = check_circle_position(circle5, circle6); + Location result3 = check_position(circle5, circle6); print_result("3 Internal tangent", result3); // 4 Circle circle7(Point(0, 0), 3); Circle circle8(Point(4, 0), 3); - Location result4 = check_circle_position(circle7, circle8); + Location result4 = check_position(circle7, circle8); print_result("4 Intersecting circles", result4); // 5 Circle circle9(Point(0, 0), 3); Circle circle10(Point(8, 0), 5); - Location result5 = check_circle_position(circle9, circle10); + Location result5 = check_position(circle9, circle10); print_result("5 External tangent", result5); // 6 Circle circle11(Point(0, 0), 2); Circle circle12(Point(10, 0), 3); - Location result6 = check_circle_position(circle11, circle12); + Location result6 = check_position(circle11, circle12); print_result("6 Non-intersecting circles", result6); #endif // USER_INPUT std::cout << "=======================================" << std::endl; @@ -163,7 +163,7 @@ int main() { std::cout << "Input radius of the second sphere: "; std::cin >> value_radius2; Sphere sphere2(Point3D(value_dot2_ox, value_dot2_oy, value_dot2_oz), value_radius2); - Location result = check_sphere_position(sphere1, sphere2); + Location result = check_position(sphere1, sphere2); set_color(12, 0); std::cout << "RESULT:"; set_color(7, 0); @@ -173,37 +173,37 @@ int main() { // 1 caths Sphere sphere1(Point3D(0, 0, 0), 5); Sphere sphere2(Point3D(0, 0, 0), 5); - Location result7 = check_sphere_position(sphere1, sphere2); + Location result7 = check_position(sphere1, sphere2); print_result("1 Coincide spheres (same center, same radius)", result7); // 2 Sphere sphere3(Point3D(0, 0, 0), 8); Sphere sphere4(Point3D(1, 1, 1), 2); - Location result8 = check_sphere_position(sphere3, sphere4); + Location result8 = check_position(sphere3, sphere4); print_result("2 One sphere inside another", result8); // 3 Sphere sphere5(Point3D(0, 0, 0), 5); Sphere sphere6(Point3D(3, 0, 0), 2); - Location result9 = check_sphere_position(sphere5, sphere6); + Location result9 = check_position(sphere5, sphere6); print_result("3 Internal tangent spheres", result9); // 4 Sphere sphere7(Point3D(0, 0, 0), 4); Sphere sphere8(Point3D(3, 0, 0), 3); - Location result10 = check_sphere_position(sphere7, sphere8); + Location result10 = check_position(sphere7, sphere8); print_result("4 Intersecting spheres", result10); // 5 Sphere sphere9(Point3D(0, 0, 0), 3); Sphere sphere10(Point3D(0, 8, 0), 5); - Location result11 = check_sphere_position(sphere9, sphere10); + Location result11 = check_position(sphere9, sphere10); print_result("5 External tangent spheres", result11); // 6 Sphere sphere11(Point3D(0, 0, 0), 2); Sphere sphere12(Point3D(0, 0, 15), 3); - Location result12 = check_sphere_position(sphere11, sphere12); + Location result12 = check_position(sphere11, sphere12); print_result("6 Non-intersecting spheres", result12); std::cout << "=======================================" << std::endl; diff --git a/tests/test_point.cpp b/tests/test_point.cpp index f0a55ee7..9198f3a9 100644 --- a/tests/test_point.cpp +++ b/tests/test_point.cpp @@ -11,53 +11,35 @@ TEST(TestPointLib, default_constructor) { // Arrange Point a; - // Act - int actual_result = FALSE; - if (a.get_ox() == 0 && a.get_oy() == 0) { - actual_result = TRUE; - } - - // Assert - int expected_result = TRUE; - EXPECT_EQ(expected_result, actual_result); // + EXPECT_EQ(0, a.get_oy()); + EXPECT_EQ(0, a.get_ox()); // } TEST(TestPointLib, parameterized_constructor) { - // Arrange + // Arrange & act Point a(5, 27); - // Act - int actual_result = FALSE; - if (a.get_ox() == 5 && a.get_oy() == 27) { - actual_result = TRUE; - } - - // Assert - int expected_result = TRUE; - EXPECT_EQ(expected_result, actual_result); // + EXPECT_EQ(5, a.get_ox()); + EXPECT_EQ(27, a.get_oy()); } TEST(TestPointLib, copy_constructor_without_throw) { // Arrange Point a(-3, 72); - Point b(a); + // Act - int actual_result = FALSE; - if (a.get_ox() == b.get_ox() && a.get_oy() == b.get_oy()) { - actual_result = TRUE; - } + Point b(a); - // Assert - int expected_result = TRUE; - EXPECT_EQ(expected_result, actual_result); // + EXPECT_EQ(-3, b.get_ox()); + EXPECT_EQ(72, b.get_oy()); // } TEST(TestPointLib, copy_constructor_with_throw) { // Arrange Point* null_pointer = nullptr; // Act & Assert - EXPECT_THROW({ Point & bad_reference = *null_pointer; Point a(bad_reference); }, std::logic_error); + ASSERT_ANY_THROW(Point a(*null_pointer)); } TEST(TestPointLib, can_distance_to) { @@ -66,9 +48,22 @@ TEST(TestPointLib, can_distance_to) { Point b(4, 6); // Act - float actual_result = a.distance_to(b); - // Assert - float expected_result = 5.0; - EXPECT_NEAR(expected_result, actual_result, EPSILON); //, + EXPECT_NEAR(5.0, a.distance_to(b), EPSILON); //, +} + +TEST(TestPointLib, can_compare_with_operator_two_equal_object) { + // Arrage + Point a(1, 2); + Point b(1, 2); + + EXPECT_TRUE(a == b); +} + +TEST(TestPointLib, can_compare_with_operator_two_not_equal_object) { + // Arrage + Point a(1, 2); + Point b(4, 2); + + EXPECT_FALSE(a == b); } \ No newline at end of file diff --git a/tests/test_sphere.cpp b/tests/test_sphere.cpp index 7f598982..ba92c253 100644 --- a/tests/test_sphere.cpp +++ b/tests/test_sphere.cpp @@ -13,7 +13,7 @@ TEST(TestSphereLib, default_constructor) { Point3D b; // Act int actual_result = FALSE; - if (A.get_center3D() == b && A.get_radius() == 1) { + if (A.get_center() == b && A.get_radius() == 1) { actual_result = TRUE; } @@ -29,7 +29,7 @@ TEST(TestSphereLib, parameterized_constructor_with_normal_radius) { // Act int actual_result = FALSE; - if (C.get_center3D() == a && C.get_radius() == 6) { + if (C.get_center() == a && C.get_radius() == 6) { actual_result = TRUE; } @@ -51,7 +51,7 @@ TEST(TestSphereLib, copy_constructor_without_throw) { // Act int actual_result = FALSE; - if (B.get_center3D() == A.get_center3D() && B.get_radius() == A.get_radius()) { + if (B.get_center() == A.get_center() && B.get_radius() == A.get_radius()) { actual_result = TRUE; } From 65ba08383f82f043cfad86ebb0f58d080044d6fe Mon Sep 17 00:00:00 2001 From: Kate-Nits Date: Wed, 17 Sep 2025 23:38:52 +0300 Subject: [PATCH 11/94] Started writing the function matrix_application, wrote the methods start_menu_for_matrix, chech_user_input, input_user_choice --- lib_algorithms/algorithms.cpp | 72 +++++++++++++++++++++++++++++++++++ lib_algorithms/algorithms.h | 12 ++++++ main/main.cpp | 62 ++++++++++++++---------------- 3 files changed, 113 insertions(+), 33 deletions(-) diff --git a/lib_algorithms/algorithms.cpp b/lib_algorithms/algorithms.cpp index a62aff4a..5340c3ad 100644 --- a/lib_algorithms/algorithms.cpp +++ b/lib_algorithms/algorithms.cpp @@ -2,9 +2,81 @@ #include #include +#include #include #include "../lib_circle/circle.h" #include "../lib_sphere/sphere.h" +#define START_MENU_MATRIX_SIZE 3 + +void set_color(int text_color, int bg_color) { + HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); + SetConsoleTextAttribute(hConsole, (bg_color << 4) | text_color); +} + +enum Location { intersecting, do_not_intersecting, tangent, inside, coinside }; +//tangent - , intersecting - , inside - , coinside - +void print_result_position(const std::string& description, Location result) { + std::cout << description << ": "; + switch (result) { + case coinside: std::cout << "COINCIDE"; break; + case inside: std::cout << "INSIDE"; break; + case tangent: std::cout << "TANGENT"; break; + case intersecting: std::cout << "INTERSECTING"; break; + case do_not_intersecting: std::cout << "DO_NOT_INTERSECT"; break; + } + std::cout << std::endl; +} + +void start_menu_for_matrix() { + std::cout << "==========================================" << std::endl; + std::cout << " WORKING WITH MATRICES " << std::endl; + std::cout << "==========================================" << std::endl; + std::cout << " 1. Matrix calculator " << std::endl; + std::cout << " 2. Viewing saved matrices " << std::endl; + std::cout << " 3. Exit " << std::endl; + std::cout << "==========================================" << std::endl; + + //std::cout << "==========================================" << std::endl; + //std::cout << "" << std::endl; +} + +void check_user_input(int user_choice, int true_number) { + if (user_choice > true_number) { + set_color(12, 0); + std::cout << "ERROR: "; + set_color(7, 0); + throw std::logic_error("your choice is more than points\n"); + } + else if (user_choice <= 0) { + set_color(12, 0); + std::cout << "ERROR: "; + set_color(7, 0); + throw std::logic_error("your choice is less than points\n"); + } +} +bool input_user_choice(int& user_choice, int true_number) { + std::cout << " Enter the menu you need: " << std::endl; + std::cout << " (if you are sure, press enter)" << std::endl; + std::cout << " "; + std::cin >> user_choice; + try { + check_user_input(user_choice, true_number); + } + catch (const std::exception& ex) { + std::cerr << ex.what(); + std::cout << " Try again:" << std::endl; + return false; + } + return true; +} + +void matrix_application() { + int user_choice; + int& link_user_choice = user_choice; + start_menu_for_matrix(); + while (!input_user_choice(link_user_choice, START_MENU_MATRIX_SIZE)); +} + diff --git a/lib_algorithms/algorithms.h b/lib_algorithms/algorithms.h index 06e0d86f..53234816 100644 --- a/lib_algorithms/algorithms.h +++ b/lib_algorithms/algorithms.h @@ -1,14 +1,23 @@ // Copyright 2025 Ekaterina Ushnitskaya + #ifndef LIB_ALGORITHMS_H #define LIB_ALGORITHMS_H #include "../lib_circle/circle.h" #include "../lib_sphere/sphere.h" +#define START_MENU_MATRIX_SIZE 3 + enum Location { intersecting, do_not_intersecting, tangent, inside, coinside }; //tangent - , intersecting - , inside - , coinside - + +// +void set_color(int text_color, int bg_color); +void print_result_position(const std::string& description, Location result); +void start_menu_for_matrix(); + template Location check_position(const T& circle1, const T& circle2) { Point center1 = circle1.get_center(); @@ -40,6 +49,9 @@ Location check_position(const T& circle1, const T& circle2) { } } +void matrix_application(); +void check_user_input(int user_choice, int true_number); +bool input_user_choice(int& user_choice, int true_number); #endif // LIB_ALGORITHMS_H \ No newline at end of file diff --git a/main/main.cpp b/main/main.cpp index b4c5180c..29551b86 100644 --- a/main/main.cpp +++ b/main/main.cpp @@ -3,19 +3,21 @@ #include #include #include +#include #include "../lib_algorithms/algorithms.h" +// : //#define EASY_EXAMPLE -#define POSITION_OF_TWO_CIRCLE -//#define POSITION_OF_TWO_SPHERE +// /: +//#define POSITION_OF_TWO_CIRCLE +//#define POSITION_OF_TWO_SPHERE +// //#define USER_INPUT -void set_color(int text_color, int bg_color) { - HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); - SetConsoleTextAttribute(hConsole, (bg_color << 4) | text_color); -} +// +#define WORKING_WITH_MATRIX #ifdef EASY_EXAMPLE @@ -52,19 +54,6 @@ int main() { #endif // EASY_EXAMPLE - -void print_result(const std::string& description, Location result) { - std::cout << description << ": "; - switch (result) { - case coinside: std::cout << "COINCIDE"; break; - case inside: std::cout << "INSIDE"; break; - case tangent: std::cout << "TANGENT"; break; - case intersecting: std::cout << "INTERSECTING"; break; - case do_not_intersecting: std::cout << "DO_NOT_INTERSECT"; break; - } - std::cout << std::endl; -} - #ifdef POSITION_OF_TWO_CIRCLE #include "../lib_circle/circle.h" @@ -92,43 +81,43 @@ int main() { set_color(12, 0); std::cout << "RESULT:"; set_color(7, 0); - print_result(" ", result); + print_result_position(" ", result); #else // 1 Circle circle1(Point(0, 0), 5); Circle circle2(Point(0, 0), 5); Location result1 = check_position(circle1, circle2); - print_result("1 Coincide circles (same center, same radius)", result1); + print_result_position("1 Coincide circles (same center, same radius)", result1); // 2 Circle circle3(Point(0, 0), 10); Circle circle4(Point(1, 1), 2); Location result2 = check_position(circle3, circle4); - print_result("2 One circle inside another", result2); + print_result_position("2 One circle inside another", result2); // 3 Circle circle5(Point(0, 0), 5); Circle circle6(Point(3, 0), 2); Location result3 = check_position(circle5, circle6); - print_result("3 Internal tangent", result3); + print_result_position("3 Internal tangent", result3); // 4 Circle circle7(Point(0, 0), 3); Circle circle8(Point(4, 0), 3); Location result4 = check_position(circle7, circle8); - print_result("4 Intersecting circles", result4); + print_result_position("4 Intersecting circles", result4); // 5 Circle circle9(Point(0, 0), 3); Circle circle10(Point(8, 0), 5); Location result5 = check_position(circle9, circle10); - print_result("5 External tangent", result5); + print_result_position("5 External tangent", result5); // 6 Circle circle11(Point(0, 0), 2); Circle circle12(Point(10, 0), 3); Location result6 = check_position(circle11, circle12); - print_result("6 Non-intersecting circles", result6); + print_result_position("6 Non-intersecting circles", result6); #endif // USER_INPUT std::cout << "=======================================" << std::endl; return 0; @@ -167,44 +156,44 @@ int main() { set_color(12, 0); std::cout << "RESULT:"; set_color(7, 0); - print_result(" ", result); + print_result_position(" ", result); std::cout << "=======================================" << std::endl; #else // 1 caths Sphere sphere1(Point3D(0, 0, 0), 5); Sphere sphere2(Point3D(0, 0, 0), 5); Location result7 = check_position(sphere1, sphere2); - print_result("1 Coincide spheres (same center, same radius)", result7); + print_result_position("1 Coincide spheres (same center, same radius)", result7); // 2 Sphere sphere3(Point3D(0, 0, 0), 8); Sphere sphere4(Point3D(1, 1, 1), 2); Location result8 = check_position(sphere3, sphere4); - print_result("2 One sphere inside another", result8); + print_result_position("2 One sphere inside another", result8); // 3 Sphere sphere5(Point3D(0, 0, 0), 5); Sphere sphere6(Point3D(3, 0, 0), 2); Location result9 = check_position(sphere5, sphere6); - print_result("3 Internal tangent spheres", result9); + print_result_position("3 Internal tangent spheres", result9); // 4 Sphere sphere7(Point3D(0, 0, 0), 4); Sphere sphere8(Point3D(3, 0, 0), 3); Location result10 = check_position(sphere7, sphere8); - print_result("4 Intersecting spheres", result10); + print_result_position("4 Intersecting spheres", result10); // 5 Sphere sphere9(Point3D(0, 0, 0), 3); Sphere sphere10(Point3D(0, 8, 0), 5); Location result11 = check_position(sphere9, sphere10); - print_result("5 External tangent spheres", result11); + print_result_position("5 External tangent spheres", result11); // 6 Sphere sphere11(Point3D(0, 0, 0), 2); Sphere sphere12(Point3D(0, 0, 15), 3); Location result12 = check_position(sphere11, sphere12); - print_result("6 Non-intersecting spheres", result12); + print_result_position("6 Non-intersecting spheres", result12); std::cout << "=======================================" << std::endl; #endif // USER_INPUT @@ -213,3 +202,10 @@ int main() { } #endif // POSITION_OF_TWO_SPHERE + +#ifdef WORKING_WITH_MATRIX +int main() { + matrix_application(); + return 0; +} +#endif // WORKING_WITH_MATRIX \ No newline at end of file From 497ceb9b79e5b4fd878999d82bf2dd106804f7e8 Mon Sep 17 00:00:00 2001 From: Kate-Nits Date: Thu, 18 Sep 2025 11:36:28 +0300 Subject: [PATCH 12/94] Started writing the function matrix_application, wrote the methods in_development, start_menu_for_matrix, print_menu_matrix_calculator, check_user_input, input_user_choice, what_matrices, what_matrix_sizes, print_res, do_user_want_to_save, start_matrix_calculator. Started to realised classes TVector, MathVector, TMatrix, TriangleMatrix, but now they are empty --- lib_algorithms/algorithms.cpp | 250 ++++++++++++++++++++++++-- lib_algorithms/algorithms.h | 14 +- lib_mathvector/MathVector.h | 13 ++ lib_matrix/matrix.h | 33 ++++ lib_triangle_matrix/triangle_matrix.h | 15 ++ lib_tvector/tvector.h | 23 +++ main/main.cpp | 3 +- 7 files changed, 338 insertions(+), 13 deletions(-) diff --git a/lib_algorithms/algorithms.cpp b/lib_algorithms/algorithms.cpp index 5340c3ad..87a7a9dd 100644 --- a/lib_algorithms/algorithms.cpp +++ b/lib_algorithms/algorithms.cpp @@ -8,14 +8,38 @@ #include "../lib_circle/circle.h" #include "../lib_sphere/sphere.h" -#define START_MENU_MATRIX_SIZE 3 +#include "../lib_matrix/matrix.h" +#include "../lib_triangle_matrix/triangle_matrix.h" + +#define START_MENU_FOR_MATRIX_SIZE 3 +#define START_MENU_MATRIX_CALCULATE_SIZE 4 +#define WHAT_MATRIX_MENU 3 +#define YES 1 +#define NO 0 + +#define MATRIX_CALCULATOR 1 +#define VIEWING_SAVED_MATRICES 2 +#define EXIT_MAIN_MENU 3 + +#define ADD 1 +#define SUBTRACT 2 +#define MULTIPLY 3 +#define EXIT_CALCULATE_MATRIX_MENU 4 + +//#define REALISED_ADD +//#define REALISED_SUBTRACT +//#define REALISED_MULTIPLY +//#define REALISED_OPERATOR[] + +#define STANDART 1 +#define TRIANGLE 2 void set_color(int text_color, int bg_color) { HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); SetConsoleTextAttribute(hConsole, (bg_color << 4) | text_color); } -enum Location { intersecting, do_not_intersecting, tangent, inside, coinside }; +//enum Location { intersecting, do_not_intersecting, tangent, inside, coinside }; //tangent - , intersecting - , inside - , coinside - void print_result_position(const std::string& description, Location result) { std::cout << description << ": "; @@ -29,18 +53,33 @@ void print_result_position(const std::string& description, Location result) { std::cout << std::endl; } +void in_development() { + std::cout << " Sorry, the function is in development " << std::endl; + std::cout << " Press Enter to exit " << std::endl; + getchar(); + getchar(); + system("cls"); +} void start_menu_for_matrix() { - std::cout << "==========================================" << std::endl; - std::cout << " WORKING WITH MATRICES " << std::endl; - std::cout << "==========================================" << std::endl; + std::cout << "==================================================" << std::endl; + std::cout << " WORKING WITH MATRICES " << std::endl; + std::cout << "==================================================" << std::endl; std::cout << " 1. Matrix calculator " << std::endl; std::cout << " 2. Viewing saved matrices " << std::endl; - std::cout << " 3. Exit " << std::endl; - std::cout << "==========================================" << std::endl; - - //std::cout << "==========================================" << std::endl; + std::cout << " 2. Exit " << std::endl; + std::cout << "==================================================" << std::endl; + //std::cout << "==================================================" << std::endl; //std::cout << "" << std::endl; } +void print_menu_matrix_calculator() { + std::cout << "==================================================" << std::endl; + std::cout << " What would you like to do with matrices:" << std::endl; + std::cout << " 1. Add " << std::endl; + std::cout << " 2. Subtract" << std::endl; + std::cout << " 3. multiply" << std::endl; + std::cout << " 4. Exit" << std::endl; + std::cout << "==================================================" << std::endl; +} void check_user_input(int user_choice, int true_number) { if (user_choice > true_number) { @@ -71,12 +110,201 @@ bool input_user_choice(int& user_choice, int true_number) { } return true; } +void what_matrices(int& what_the_first_matrix, int& what_the_second_matrix, int& isExit) { + int user_choice; + int& link_user_choice = user_choice; + std::cout << "==================================================" << std::endl; + std::cout << " What will the matrices be like?" << std::endl; + std::cout << " 1. Standart" << std::endl; + std::cout << " 2. Triangle" << std::endl; + std::cout << " 3. Exit" << std::endl; + while (!input_user_choice(link_user_choice, WHAT_MATRIX_MENU)); + if (user_choice == 3) { + isExit = YES; + } + else if (user_choice == STANDART) { + what_the_first_matrix = STANDART; + what_the_second_matrix = STANDART; + } + else { + what_the_first_matrix = TRIANGLE; + what_the_second_matrix = TRIANGLE; + } +} +void what_matrix_sizes(size_t& sizeN, size_t& sizeM) { + std::cout << "==================================================" << std::endl; + std::cout << "Enter the number of lines (parametr M): "; + std::cin >> sizeM; + std::cout << "Enter the number of columns (parametr N): "; + std::cin >> sizeN; +} +void print_res(const TMatrix>& matrix, size_t size_M, size_t size_N) { +#ifndef REALISED_OPERATOR[] + in_development(); +#else + for (size_t i = 0; i < size_M; i++) { + for (size_t j = 0; j < size_N; j++) { + std::cout << matrix[i][j] << " "; + } + std::cout << std::endl; + } +#endif // REALISED_OPERATOR[] +} +void print_res(const TMatrix& matrix, size_t size_M, size_t size_N) { +#ifndef REALISED_OPERATOR[] + in_development(); +#else + for (size_t i = 0; i < size_M; i++) { + for (size_t j = 0; j < size_N; j++) { + std::cout << matrix[i][j] << " "; + } + std::cout << std::endl; + } +#endif // REALISED_OPERATOR[] +} +void do_user_want_to_save(int& want_to_save) { + std::cout << "==================================================" << std::endl; + std::cout << "Would you like to save result?" << std::endl; + std::cout << "==================================================" << std::endl; + std::cout << "If answer - yes - input 1. If no, then input 0: "; + std::cin >> want_to_save; + while (want_to_save != YES || want_to_save != NO) { + std::cout << "==================================================" << std::endl; + set_color(12, 0); + std::cout << "ERROR: "; + set_color(7, 0); + std::cout << "your choice is more than points" << std::endl; + std::cout << "==================================================" << std::endl; + std::cout << "Tru again" << std::endl; + std::cout << "If answer - yes - input 1. If no, then input 0: "; + std::cin >> want_to_save; + } +} +void start_matrix_calculator(int& link_user_choice) { + int isExit = NO; + int& link_isExit = isExit; + int want_to_save = NO; + int& linkwant_to_save = want_to_save; + int what_the_first_matrix = 0; + int what_the_second_matrix = 0; + int& link_what_the_first_matrix = what_the_first_matrix; + int& link_what_the_second_matrix = what_the_second_matrix; + print_menu_matrix_calculator(); + while (!input_user_choice(link_user_choice, START_MENU_MATRIX_CALCULATE_SIZE)); + system("cls"); + while (!isExit) { + if (link_user_choice != EXIT_CALCULATE_MATRIX_MENU) { + what_matrices(link_what_the_first_matrix, link_what_the_second_matrix, link_isExit); + system("cls"); + if (isExit == YES) { + break; + } + size_t sizeM1, sizeN1; + size_t& link_sizeM1 = sizeM1; + size_t& link_sizeN1 = sizeN1; + size_t sizeM2, sizeN2; + size_t& link_sizeM2 = sizeM2; + size_t& link_sizeN2 = sizeN2; + size_t size_resM, size_resN; + size_t& link_size_resM = size_resM; + size_t& link_size_resN = size_resN; + if (link_user_choice == ADD || link_user_choice == SUBTRACT) { + what_matrix_sizes(link_sizeN1, link_sizeM1); + sizeM2 = sizeM1; + size_resM = sizeM1; + sizeN2 = sizeN1; + size_resN = sizeN1; + } + else if (link_user_choice == MULTIPLY) { + std::cout << "==================================================" << std::endl; + std::cout << " For the first matrix" << std::endl; + what_matrix_sizes(link_sizeN1, link_sizeM1); + std::cout << "==================================================" << std::endl; + std::cout << " For the second matrix" << std::endl; + what_matrix_sizes(link_sizeN2, link_sizeM2); + while (sizeN1 != sizeM2) { + set_color(12, 0); + std::cout << "ERROR: "; + set_color(7, 0); + std::cout << "The parameter N for 1 of the matrix and the parametr M for 2 must be equal" << std::endl; + std::cout << "==================================================" << std::endl; + std::cout << " For the first matrix" << std::endl; + what_matrix_sizes(link_sizeN1, link_sizeM1); + std::cout << "==================================================" << std::endl; + std::cout << " For the second matrix" << std::endl; + what_matrix_sizes(link_sizeN2, link_sizeM2); + } + size_resM = sizeN1; + size_resM = sizeM2; + } + if (what_the_first_matrix == STANDART && what_the_second_matrix == STANDART) { + TMatrix> matrix1; + TMatrix> matrix2; + TMatrix> res; + + } + else if (what_the_first_matrix == TRIANGLE && what_the_second_matrix == TRIANGLE) { + TriangleMatrix> matrix1; + TriangleMatrix> matrix2; + TriangleMatrix> res; + } + } + system("cls"); + switch (link_user_choice) { + case ADD: +#ifndef REALISED_ADD + in_development(); +#else + res = matrix1.add_matrices(matrix2); + print_res(res, size_resM, size_resN); + void do_user_want_to_save(link_want_to_save); +#endif //REALISED_ADD + break; + + case SUBTRACT: +#ifndef REALISED_SUBTRACT + in_development(); +#else + res = matrix1.subtract_matrices(matrix2); + print_res(res, size_resM, size_resN); +#endif //REALISED_SUBTRACT + break; + case MULTIPLY: +#ifndef REALISED_MULTIPLY + in_development(); +#else + res = matrix1.multiply_matrices(matrix2); + print_res(res, size_resM, size_resN); +#endif //REALISED_MULTIPLY + break; + + case EXIT_CALCULATE_MATRIX_MENU: + isExit = YES; + break; + } + } +} void matrix_application() { int user_choice; int& link_user_choice = user_choice; - start_menu_for_matrix(); - while (!input_user_choice(link_user_choice, START_MENU_MATRIX_SIZE)); + int isExit = NO; + while (!isExit) { + start_menu_for_matrix(); + while (!input_user_choice(link_user_choice, START_MENU_FOR_MATRIX_SIZE)); + system("cls"); + switch (user_choice) { + case MATRIX_CALCULATOR: + start_matrix_calculator(link_user_choice); + break; + case VIEWING_SAVED_MATRICES: + + break; + case EXIT_MAIN_MENU: + isExit = YES; + break; + } + } } diff --git a/lib_algorithms/algorithms.h b/lib_algorithms/algorithms.h index 53234816..d8af2caf 100644 --- a/lib_algorithms/algorithms.h +++ b/lib_algorithms/algorithms.h @@ -6,6 +6,8 @@ #include "../lib_circle/circle.h" #include "../lib_sphere/sphere.h" +#include "../lib_matrix/matrix.h" +#include "../lib_triangle_matrix/triangle_matrix.h" #define START_MENU_MATRIX_SIZE 3 @@ -16,7 +18,8 @@ enum Location { intersecting, do_not_intersecting, tangent, inside, coinside }; // void set_color(int text_color, int bg_color); void print_result_position(const std::string& description, Location result); -void start_menu_for_matrix(); +void in_development(); + template Location check_position(const T& circle1, const T& circle2) { @@ -49,9 +52,18 @@ Location check_position(const T& circle1, const T& circle2) { } } +void start_menu_for_matrix(); void matrix_application(); void check_user_input(int user_choice, int true_number); bool input_user_choice(int& user_choice, int true_number); +void what_matrices(int& what_the_first_matrix, int& what_the_second_matrix, int& isExit); +void print_menu_matrix_calculator(); +void what_matrix_sizes(size_t& link_sizeN, size_t& link_sizeM); +void start_matrix_calculator(int& link_user_choice); +//void print_res(const TMatrix > & matrix, size_t size_M, size_t size_N); +//void print_res(const TriangleMatrix>& matrix, size_t size_M, size_t size_N); +void do_user_want_to_save(int& want_to_save); + #endif // LIB_ALGORITHMS_H \ No newline at end of file diff --git a/lib_mathvector/MathVector.h b/lib_mathvector/MathVector.h index e69de29b..cb6f69bb 100644 --- a/lib_mathvector/MathVector.h +++ b/lib_mathvector/MathVector.h @@ -0,0 +1,13 @@ +// Copyright 2025 Ekaterina Ushnitskaya + +#ifndef LIB_MATHVECTOR_MATHVECTOR_H +#define LIB_MATHVECTOR_MATHVECTOR_H + +#include "../lib_tvector/tvector.h" + +template +class MathVector : private TVector { + +}; + +#endif // LIB_MATHVECTOR_MATHVECTOR_H diff --git a/lib_matrix/matrix.h b/lib_matrix/matrix.h index e69de29b..595b30e9 100644 --- a/lib_matrix/matrix.h +++ b/lib_matrix/matrix.h @@ -0,0 +1,33 @@ +// Copyright 2025 Ekaterina Ushnitskaya + +#ifndef LIB_MATRIX_MATRIX_H +#define LIB_MATRIX_MATRIX_H + +#include "../lib_algorithms/algorithms.h" +#include "../lib_mathvector/MathVector.h" + +template +class TMatrix : public MathVector> { +public: + TMatrix& add_matrices(const TMatrix& other) { + in_development(); + return *this; + } + + TMatrix& operator=(const TMatrix& other) noexcept { + in_development(); + return *this; + } + + TMatrix& subtract_matrices(const TMatrix& other) { + in_development(); + return *this; + } + + TMatrix& multiply_matrices(const TMatrix& other) { + in_development(); + return *this; + } +}; + +#endif // LIB_MATRIX_MATRIX_H diff --git a/lib_triangle_matrix/triangle_matrix.h b/lib_triangle_matrix/triangle_matrix.h index e69de29b..3fef8520 100644 --- a/lib_triangle_matrix/triangle_matrix.h +++ b/lib_triangle_matrix/triangle_matrix.h @@ -0,0 +1,15 @@ +// Copyright 2025 Ekaterina Ushnitskaya + +#ifndef TRIANGLE_MATRIX_TRIANGLE_MATRIX_X +#define TRIANGLE_MATRIX_TRIANGLE_MATRIX_X + +#include "../lib_matrix/matrix.h" + +template +class TriangleMatrix : public TMatrix { +public: + TriangleMatrix() : TMatrix() {} + // +}; + +#endif // TRIANGLE_MATRIX_TRIANGLE_MATRIX_X diff --git a/lib_tvector/tvector.h b/lib_tvector/tvector.h index e69de29b..269c000a 100644 --- a/lib_tvector/tvector.h +++ b/lib_tvector/tvector.h @@ -0,0 +1,23 @@ +// Copyright 2025 Ekaterina Ushnitskaya + +#ifndef LIB_TVECTOR_TVECTOR_H +#define LIB_TVECTOR_TVECTOR_H + +#include +#include // rand() +#include // std::out_of_range, std::invalid_argument + +enum State { empty, busy, deleted }; +template +class TVector { + T* _data; + State* _states; + size_t _size; + size_t _capacity; + size_t _deleted; +public: + static const size_t RESERVE_MEMORY = 15; + static const size_t MAX_PERCENT_DELETED = 15; +}; + +#endif // LIB_TVECTOR diff --git a/main/main.cpp b/main/main.cpp index 29551b86..f9f59130 100644 --- a/main/main.cpp +++ b/main/main.cpp @@ -208,4 +208,5 @@ int main() { matrix_application(); return 0; } -#endif // WORKING_WITH_MATRIX \ No newline at end of file +#endif // WORKING_WITH_MATRIX + From b3fde40eb4e5faa9eb2af5dbcc095111d824e1d9 Mon Sep 17 00:00:00 2001 From: Kate-Nits Date: Thu, 18 Sep 2025 23:18:01 +0300 Subject: [PATCH 13/94] I did the third job of the terminal class: Implemented the interface of the matrix calculator --- lib_algorithms/algorithms.cpp | 180 +++++-- lib_algorithms/algorithms.h | 16 +- lib_mathvector/MathVector.h | 57 ++- lib_matrix/matrix.h | 75 ++- lib_triangle_matrix/triangle_matrix.h | 62 ++- lib_tvector/tvector.h | 654 ++++++++++++++++++++++++++ 6 files changed, 1000 insertions(+), 44 deletions(-) diff --git a/lib_algorithms/algorithms.cpp b/lib_algorithms/algorithms.cpp index 87a7a9dd..18766f70 100644 --- a/lib_algorithms/algorithms.cpp +++ b/lib_algorithms/algorithms.cpp @@ -16,6 +16,11 @@ #define WHAT_MATRIX_MENU 3 #define YES 1 #define NO 0 +#define MAX_COUNT_OF_SAVED_MATRICES 3 +#define EXIT_DELETED_SAVED_MATRICES_MENU 4 +#define MATRIX1 1 +#define MATRIX2 2 +#define MATRIX3 3 #define MATRIX_CALCULATOR 1 #define VIEWING_SAVED_MATRICES 2 @@ -30,6 +35,7 @@ //#define REALISED_SUBTRACT //#define REALISED_MULTIPLY //#define REALISED_OPERATOR[] +//#define REALISED_CLASSES #define STANDART 1 #define TRIANGLE 2 @@ -66,7 +72,7 @@ void start_menu_for_matrix() { std::cout << "==================================================" << std::endl; std::cout << " 1. Matrix calculator " << std::endl; std::cout << " 2. Viewing saved matrices " << std::endl; - std::cout << " 2. Exit " << std::endl; + std::cout << " 3. Exit " << std::endl; std::cout << "==================================================" << std::endl; //std::cout << "==================================================" << std::endl; //std::cout << "" << std::endl; @@ -138,21 +144,12 @@ void what_matrix_sizes(size_t& sizeN, size_t& sizeM) { std::cout << "Enter the number of columns (parametr N): "; std::cin >> sizeN; } -void print_res(const TMatrix>& matrix, size_t size_M, size_t size_N) { -#ifndef REALISED_OPERATOR[] - in_development(); -#else - for (size_t i = 0; i < size_M; i++) { - for (size_t j = 0; j < size_N; j++) { - std::cout << matrix[i][j] << " "; - } - std::cout << std::endl; - } -#endif // REALISED_OPERATOR[] -} void print_res(const TMatrix& matrix, size_t size_M, size_t size_N) { + #ifndef REALISED_OPERATOR[] + in_development(); + #else for (size_t i = 0; i < size_M; i++) { for (size_t j = 0; j < size_N; j++) { @@ -180,11 +177,9 @@ void do_user_want_to_save(int& want_to_save) { std::cin >> want_to_save; } } -void start_matrix_calculator(int& link_user_choice) { +void start_matrix_calculator(int& link_user_choice, int& link_want_to_save, TMatrix res) { int isExit = NO; int& link_isExit = isExit; - int want_to_save = NO; - int& linkwant_to_save = want_to_save; int what_the_first_matrix = 0; int what_the_second_matrix = 0; int& link_what_the_first_matrix = what_the_first_matrix; @@ -238,15 +233,19 @@ void start_matrix_calculator(int& link_user_choice) { size_resM = sizeM2; } if (what_the_first_matrix == STANDART && what_the_second_matrix == STANDART) { - TMatrix> matrix1; - TMatrix> matrix2; - TMatrix> res; + TMatrix matrix1; + TMatrix matrix2; + TMatrix intermediate_res; } else if (what_the_first_matrix == TRIANGLE && what_the_second_matrix == TRIANGLE) { - TriangleMatrix> matrix1; - TriangleMatrix> matrix2; - TriangleMatrix> res; +#ifdef REALISED_CLASSES + TriangleMatrix matrix1; + TriangleMatrix matrix2; + TriangleMatrix intermediate_res; +#else + //in_development(); +#endif // REALISED_CLASSES } } system("cls"); @@ -255,9 +254,10 @@ void start_matrix_calculator(int& link_user_choice) { #ifndef REALISED_ADD in_development(); #else - res = matrix1.add_matrices(matrix2); + intermediate_res = matrix1.add_matrices(matrix2); + res = intermediate_res; print_res(res, size_resM, size_resN); - void do_user_want_to_save(link_want_to_save); + do_user_want_to_save(link_want_to_save); #endif //REALISED_ADD break; @@ -265,8 +265,10 @@ void start_matrix_calculator(int& link_user_choice) { #ifndef REALISED_SUBTRACT in_development(); #else - res = matrix1.subtract_matrices(matrix2); + intermediate_res = matrix1.subtract_matrices(matrix2); + res = intermediate_res; print_res(res, size_resM, size_resN); + do_user_want_to_save(link_want_to_save); #endif //REALISED_SUBTRACT break; @@ -274,8 +276,10 @@ void start_matrix_calculator(int& link_user_choice) { #ifndef REALISED_MULTIPLY in_development(); #else - res = matrix1.multiply_matrices(matrix2); + intermediate_res = matrix1.multiply_matrices(matrix2); + res = intermediate_res; print_res(res, size_resM, size_resN); + do_user_want_to_save(link_want_to_save); #endif //REALISED_MULTIPLY break; @@ -285,20 +289,139 @@ void start_matrix_calculator(int& link_user_choice) { } } } +void deleted_saved_matrix(int& link_user_choice_for_deleted, int& isThereMatrix1, int& isThereMatrix2, int& isThereMatrix3) { + std::cout << "==================================================" << std::endl; + std::cout << " You can store a limited number of matrices" << std::endl; + std::cout << " Which one would you like to delete?" << std::endl; + std::cout << " 1. matrix1" << std::endl; + std::cout << " 2. matrix2" << std::endl; + std::cout << " 3. matrix3" << std::endl;; + std::cout << " 4. exit" << std::endl; + + while (!input_user_choice(link_user_choice_for_deleted, EXIT_DELETED_SAVED_MATRICES_MENU)); + switch (link_user_choice_for_deleted) { + case MATRIX1: + isThereMatrix1 = NO; + break; + + case MATRIX2: + isThereMatrix2 = NO; + break; + + case MATRIX3: + isThereMatrix3 = NO; + break; + case EXIT_DELETED_SAVED_MATRICES_MENU: + break; + } +} +void viewing_saved_matrices(int count_of_saved_matrices, + TMatrix matrix1, TMatrix matrix2, TMatrix matrix3) { + std::cout << "==================================================" << std::endl; + std::cout << " Which matrix would you like to look at?" << std::endl; + if (count_of_saved_matrices > 0) { + if (count_of_saved_matrices == MAX_COUNT_OF_SAVED_MATRICES) { + std::cout << " 1. matrix1" << std::endl; + std::cout << " 2. matrix2" << std::endl; + std::cout << " 3. matrix3" << std::endl; + } + else if (count_of_saved_matrices == MAX_COUNT_OF_SAVED_MATRICES - 1) { + std::cout << " 1. matrix1" << std::endl; + std::cout << " 2. matrix2" << std::endl; + } + else if (count_of_saved_matrices == MAX_COUNT_OF_SAVED_MATRICES - 2) { + std::cout << " 1. matrix1" << std::endl; + } + int user_choice; + int& link_user_choice = user_choice; + while (!input_user_choice(link_user_choice, START_MENU_FOR_MATRIX_SIZE)); + if (user_choice == MATRIX1) { + matrix1.print(); + } + else if (user_choice == MATRIX2) { + matrix2.print(); + } + else if (user_choice == MATRIX3) { + matrix3.print(); + } + } + else if (count_of_saved_matrices == 0) { + std::cout << " You don't have any saved matrices" << std::endl; + } + std::cout << "==================================================" << std::endl; + std::cout << " Press Enter to exit " << std::endl; + getchar(); + getchar(); + system("cls"); +} void matrix_application() { int user_choice; int& link_user_choice = user_choice; int isExit = NO; + + int want_to_save = NO; + int& link_want_to_save = want_to_save; + int count_of_saved_matrices = 0; + + TMatrix res; + TMatrix matrix1; + TMatrix matrix2; + TMatrix matrix3; + int isThereMatrix1 = NO; + int isThereMatrix2 = NO; + int isThereMatrix3 = NO; + int& link_isThereMatrix1 = isThereMatrix1; + int& link_isThereMatrix2 = isThereMatrix2; + int& link_isThereMatrix3 = isThereMatrix3; + while (!isExit) { start_menu_for_matrix(); while (!input_user_choice(link_user_choice, START_MENU_FOR_MATRIX_SIZE)); system("cls"); switch (user_choice) { case MATRIX_CALCULATOR: - start_matrix_calculator(link_user_choice); + start_matrix_calculator(link_user_choice, link_want_to_save, res); + if (want_to_save == YES) { + int user_choice_for_deleted; + int& link_user_choice_for_deleted = user_choice_for_deleted; + while ((count_of_saved_matrices + 1) > MAX_COUNT_OF_SAVED_MATRICES) { + deleted_saved_matrix(link_user_choice_for_deleted, link_isThereMatrix1, link_isThereMatrix2, link_isThereMatrix3); + if (user_choice_for_deleted == EXIT_DELETED_SAVED_MATRICES_MENU) { + break; + } + else { + count_of_saved_matrices--; + } + } + if (isThereMatrix1 == NO) { + std::cout << "==================================================" << std::endl; + std::cout << " The result will be written to the matrix1" << std::endl; + isThereMatrix1 = YES; + count_of_saved_matrices++; + matrix1 = res; + } + else if (isThereMatrix2 == NO) { + std::cout << "==================================================" << std::endl; + std::cout << " The result will be written to the matrix2" << std::endl; + isThereMatrix2 = YES; + count_of_saved_matrices++; + matrix2 = res; + } + else if (isThereMatrix3 == NO) { + std::cout << "==================================================" << std::endl; + std::cout << " The result will be written to the matrix3" << std::endl; + isThereMatrix3 = YES; + count_of_saved_matrices++; + matrix3 = res; + } + std::cout << " Press Enter to exit " << std::endl; + getchar(); + getchar(); + system("cls"); + } break; case VIEWING_SAVED_MATRICES: - + viewing_saved_matrices(count_of_saved_matrices, matrix1, matrix2, matrix3); break; case EXIT_MAIN_MENU: isExit = YES; @@ -308,3 +431,4 @@ void matrix_application() { } + diff --git a/lib_algorithms/algorithms.h b/lib_algorithms/algorithms.h index d8af2caf..2bdd587e 100644 --- a/lib_algorithms/algorithms.h +++ b/lib_algorithms/algorithms.h @@ -10,6 +10,7 @@ #include "../lib_triangle_matrix/triangle_matrix.h" #define START_MENU_MATRIX_SIZE 3 +//#define REALISED_TMATRIX enum Location { intersecting, do_not_intersecting, tangent, inside, coinside }; //tangent - , intersecting - , inside - , coinside - @@ -18,6 +19,7 @@ enum Location { intersecting, do_not_intersecting, tangent, inside, coinside }; // void set_color(int text_color, int bg_color); void print_result_position(const std::string& description, Location result); + void in_development(); @@ -53,17 +55,19 @@ Location check_position(const T& circle1, const T& circle2) { } void start_menu_for_matrix(); -void matrix_application(); +#ifndef REALISED_TMATRIX +#else +void viewing_saved_matrices(int count_of_saved_matrices, TMatrix matrix1, TMatrix matrix2, TMatrix matrix3); +void start_matrix_calculator(int& link_user_choice, int& link_want_to_save, TMatrix res); +void print_res(const TMatrix& matrix, size_t size_M, size_t size_N); +#endif //REALISED_TMATRIX void check_user_input(int user_choice, int true_number); bool input_user_choice(int& user_choice, int true_number); void what_matrices(int& what_the_first_matrix, int& what_the_second_matrix, int& isExit); void print_menu_matrix_calculator(); void what_matrix_sizes(size_t& link_sizeN, size_t& link_sizeM); -void start_matrix_calculator(int& link_user_choice); -//void print_res(const TMatrix > & matrix, size_t size_M, size_t size_N); -//void print_res(const TriangleMatrix>& matrix, size_t size_M, size_t size_N); +void deleted_saved_matrix(int& link_user_choice_for_deleted, int& isThereMatrix1, int& isThereMatrix2, int& isThereMatrix3); void do_user_want_to_save(int& want_to_save); - - +void matrix_application(); #endif // LIB_ALGORITHMS_H \ No newline at end of file diff --git a/lib_mathvector/MathVector.h b/lib_mathvector/MathVector.h index cb6f69bb..8463f4da 100644 --- a/lib_mathvector/MathVector.h +++ b/lib_mathvector/MathVector.h @@ -3,11 +3,66 @@ #ifndef LIB_MATHVECTOR_MATHVECTOR_H #define LIB_MATHVECTOR_MATHVECTOR_H +#include +#include +#include + #include "../lib_tvector/tvector.h" +#include "../lib_algorithms/algorithms.h" template -class MathVector : private TVector { +class MathVector : public TVector { +public: + MathVector() noexcept : TVector() {} + MathVector(size_t size) : TVector(size) {} + MathVector(const MathVector& other) = default; + MathVector(MathVector&& other) noexcept = default; + ~MathVector() = default; + + MathVector& operator=(const MathVector& other) noexcept { + this->assign(other); + return *this; + } + + MathVector& operator=(MathVector&& other) noexcept { + in_development(); + return *this; + } + + T dot(const MathVector& other) const { // + in_development(); + return T(); + } + + + MathVector operator+(const MathVector& other) const { + in_development(); + return MathVector(); + } + MathVector operator-(const MathVector& other) const { + in_development(); + return MathVector(); + } + + MathVector operator*(const T& scalar) const { + in_development(); + return MathVector(); + } + + MathVector& operator+=(const MathVector& other) { + in_development(); + return *this; + } + MathVector& operator-=(const MathVector& other) { + in_development(); + return *this; + } + MathVector& operator*=(const T& scalar) { + in_development(); + return *this; + } }; + #endif // LIB_MATHVECTOR_MATHVECTOR_H diff --git a/lib_matrix/matrix.h b/lib_matrix/matrix.h index 595b30e9..108b7623 100644 --- a/lib_matrix/matrix.h +++ b/lib_matrix/matrix.h @@ -3,31 +3,94 @@ #ifndef LIB_MATRIX_MATRIX_H #define LIB_MATRIX_MATRIX_H +#include +#include + #include "../lib_algorithms/algorithms.h" #include "../lib_mathvector/MathVector.h" template -class TMatrix : public MathVector> { +class TMatrix : public MathVector< MathVector > { + size_t _rows; + size_t _cols; public: - TMatrix& add_matrices(const TMatrix& other) { + TMatrix() noexcept = default; + TMatrix(size_t rows, size_t cols) { in_development(); - return *this; + _rows = rows; + _cols = cols; } + TMatrix(const TMatrix& other) = default; + TMatrix(TMatrix&& other) noexcept = default; + ~TMatrix() = default; + TMatrix& operator=(const TMatrix& other) noexcept { in_development(); return *this; } - TMatrix& subtract_matrices(const TMatrix& other) { + TMatrix& operator=(TMatrix&& other) noexcept { in_development(); return *this; } - TMatrix& multiply_matrices(const TMatrix& other) { + MathVector& operator[](size_t row) { in_development(); - return *this; } + + const MathVector& operator[](size_t row) const { + in_development(); + } + + TMatrix add_matrices(const TMatrix& other) const { + in_development(); + return TMatrix(); + } + + TMatrix subtract_matrices(const TMatrix& other) const { + in_development(); + return TMatrix(); + } + + TMatrix multiply_matrices(const TMatrix& other) const { + in_development(); + return TMatrix(); + } + + size_t rows() const noexcept { + in_development(); + return _rows; + } + + size_t cols() const noexcept { + in_development(); + return _cols; + } + + void resize(size_t newRows, size_t newCols) { + in_development(); + _rows = newRows; + _cols = newCols; + } + + void transpose() { + in_development(); + } + + void print() const { + in_development(); + } + + friend std::ostream& operator<< <>(std::ostream& out, const TMatrix& matrix); }; + +template +std::ostream& operator<<(std::ostream& out, const TMatrix& matrix) { + in_development(); + out << ""; + return out; +} + #endif // LIB_MATRIX_MATRIX_H diff --git a/lib_triangle_matrix/triangle_matrix.h b/lib_triangle_matrix/triangle_matrix.h index 3fef8520..cada60b9 100644 --- a/lib_triangle_matrix/triangle_matrix.h +++ b/lib_triangle_matrix/triangle_matrix.h @@ -3,13 +3,69 @@ #ifndef TRIANGLE_MATRIX_TRIANGLE_MATRIX_X #define TRIANGLE_MATRIX_TRIANGLE_MATRIX_X +#include +#include + #include "../lib_matrix/matrix.h" +/* template class TriangleMatrix : public TMatrix { + size_t _n; public: - TriangleMatrix() : TMatrix() {} - // + TriangleMatrix() noexcept = default; + TriangleMatrix(std::size_t n) { + in_development(); + _n = n; + } + + TriangleMatrix(const TriangleMatrix& other) = default; + TriangleMatrix(TriangleMatrix&& other) noexcept = default; + ~TriangleMatrix() = default; + + TriangleMatrix& operator=(const TriangleMatrix& other) noexcept { + in_development(); + return *this; + } + + TriangleMatrix& operator=(TriangleMatrix&& other) noexcept { + in_development(); + return *this; + } + + TriangleMatrix add_matrices(const TriangleMatrix& other) const { + in_development(); + return TriangleMatrix(); + } + + TriangleMatrix subtract_matrices(const TriangleMatrix& other) const { + in_development(); + return TriangleMatrix(); + } + + TriangleMatrix multiply_matrices(const TriangleMatrix& other) const { + in_development(); + return TriangleMatrix(); + } + + MathVector& operator[](size_t other) { + in_development(); + } + + const MathVector& operator[](size_t row) const { + in_development(); + } + + friend std::ostream& operator<< <>(std::ostream& out, const TriangleMatrix& matrix); }; -#endif // TRIANGLE_MATRIX_TRIANGLE_MATRIX_X +template +std::ostream& operator<<(std::ostream& out, const TriangleMatrix& matrix) { + in_development(); + out << " "; + return out; +} +*/ + + +#endif // TRIANGLE_MATRIX_TRIANGLE_MATRIX_X \ No newline at end of file diff --git a/lib_tvector/tvector.h b/lib_tvector/tvector.h index 269c000a..8d53dc75 100644 --- a/lib_tvector/tvector.h +++ b/lib_tvector/tvector.h @@ -10,14 +10,668 @@ enum State { empty, busy, deleted }; template class TVector { + T* _data; State* _states; size_t _size; size_t _capacity; size_t _deleted; + public: static const size_t RESERVE_MEMORY = 15; static const size_t MAX_PERCENT_DELETED = 15; + + TVector() : _data(nullptr), _states(nullptr), _size(0), _capacity(0), _deleted(0) {} // + TVector(size_t size) { // + _size = size; + _capacity = size + RESERVE_MEMORY; + _deleted = 0; + _data = new T[_capacity]; + _states = new State[_capacity]; + for (size_t i = 0; i < _capacity; ++i) { + _states[i] = i < _size ? State::busy : State::empty; + } + } + TVector(T* data, size_t size) { // + // , ??????????????????? + _size = size; + _capacity = size + RESERVE_MEMORY; + _deleted = 0; + if (size > 0 && data == nullptr) { + throw std::invalid_argument("You sent a nullptr but with a size greater than 0"); + } + _data = new T[_capacity]; + try { + _states = new State[_capacity]; + } + catch (const std::bad_alloc&) { + delete[] _data; // _data + throw; + } + for (size_t i = 0; i < _capacity; ++i) { + if (i < size) { + _data[i] = data[i]; + _states[i] = State::busy; + } + else { + _states[i] = State::empty; + } + } + + } + TVector(const TVector& other) { // + _size = other._size; + _capacity = other._capacity; + _deleted = other._deleted; + _data = nullptr; + _states = nullptr; + try { + _data = new T[_capacity]; + } + catch (const std::bad_alloc&) { // + throw; + std::cout << std::endl; + } + try { + _states = new State[_capacity]; + } + catch (const std::bad_alloc&) { + delete[] _data; + throw; + std::cout << std::endl; + } + for (size_t i = 0; i < other._size; ++i) { + _data[i] = other._data[i]; + _states[i] = other._states[i]; + } + for (size_t i = other._size; i < other._capacity; ++i) { + _states[i] = State::empty; + } + } + ~TVector() noexcept { + delete[] _data; + delete[] _states; + } + + inline T& operator[](size_t index) { + //check_index(index); ////////////////////// + size_t real_index = check_index(index); + return _data[real_index]; + } + inline const T& operator[](size_t index) const { + size_t real_index = check_index(index); + return _data[real_index]; + } + + inline T& at(size_t index) { + size_t real_index = check_index(index); + return _data[real_index]; + } + inline const T& at(size_t index) const { + size_t real_index = check_index(index); + return _data[real_index]; + } + + inline const T* data() const noexcept { + return _data; + } + + inline const T data(size_t i) const { // ??? + size_t real_index = check_index(i); + return _data[real_index]; + } + + inline const State* states() const noexcept { + return _states; + } + inline const State state(size_t i) const {// ? + size_t real_index = check_index(i); // ??????????????? + return _states[real_index]; + } + + + inline const size_t size() const noexcept {//get + return _size; + } + inline void size(size_t new_size) noexcept { //set + resize(new_size); + } + + inline const size_t capacity() const noexcept { //get + return _capacity; + } + inline void capacity(size_t new_capacity) noexcept { //set + reserve(new_capacity); + } + + + + inline T* begin() noexcept { // _data + return _data; + } + inline T* end() noexcept { + return _data + _size; + } + + + inline const size_t deleted() const noexcept { + return _deleted; + } + + inline T& front() { // , deleted empty + for (size_t i = 0; i < _size; ++i) + if (_states[i] == busy) + return _data[i]; + throw std::out_of_range("No busy elements in vector"); + } + + inline T& back() { // , deleted empty + for (size_t i = _size; i-- > 0; ) + if (_states[i] == busy) + return _data[i]; + throw std::out_of_range("No busy elements in vector"); + } + + + inline bool is_empty() const noexcept { return _size == 0 || (_size - _deleted == 0); } // + + // + void push_front(const T& value) noexcept { + if (is_full()) { + reserve(_capacity + RESERVE_MEMORY); + } + + for (size_t i = _size; i > 0; --i) { // + _data[i] = _data[i - 1]; + _states[i] = _states[i - 1]; + } + _data[0] = value; + _states[0] = busy; + ++_size; + } + void push_back(const T& value) noexcept { + if (is_full()) { + reserve(_capacity + RESERVE_MEMORY); + } + _data[_size] = value; + _states[_size] = busy; + ++_size; + } + void insert(size_t index, const T& value) { + if (index > _size) { + throw std::out_of_range("Insert index out of range"); + } + if (is_full()) { + reserve(_capacity + RESERVE_MEMORY); + } + size_t real_index = check_index(index); + for (size_t i = _size; i > real_index; --i) { // + _data[i] = _data[i - 1]; + _states[i] = _states[i - 1]; + } + _data[real_index] = value; + _states[real_index] = busy; + ++_size; + } + + // + void pop_front() { + if (_data != nullptr && _states != nullptr) { + for (size_t i = 0; i < _size; ++i) { + if (_states[i] == State::busy) { + _states[i] = State::deleted; + ++_deleted; + if (_size > 0 && ((_deleted * 100 / _size) > MAX_PERCENT_DELETED)) { + shrink_to_fit(); + } + return; + } + } + } + throw std::underflow_error("Vector is empty"); + } + void pop_back() { + if (_data != nullptr && _states != nullptr) { + for (size_t i = _size - 1; ; --i) { + if (_states[i] == State::deleted) { + _states[i] = State::empty; + --_size; + --_deleted; + } + if (_states[i] == State::busy) { + _states[i] = State::empty; + --_size; + return; + } + if (i <= 0) break; // + } + } + throw std::underflow_error("Vector is empty"); + } + void erase(size_t index) { + size_t real_index = check_index(index); + _states[real_index] = State::deleted; + ++_deleted; + if (_size > 0 && ((_deleted * 100 / _size) > MAX_PERCENT_DELETED)) { + shrink_to_fit(); + } + } + + // + /*void emplace(size_t index, T&& value) { // + check_index(index); + if (is_full()) { + reserve(_capacity + RESERVE_MEMORY); + } + for (size_t i = _size; i > index; --i) { + _data[i] = _data[i - 1]; + _states[i] = _states[i - 1]; + } + _data[index] = value; + _states[index] = State::busy; + ++_size; + }*/ + void emplace(size_t index, T&& value) { + size_t real_index = check_index(index); + _data[real_index] = value; + _states[real_index] = State::busy; + } + + TVector& assign(const TVector& other) { + if (this != &other) { + delete[] _data; + delete[] _states; + + _size = other._size; + _capacity = other._capacity; + _deleted = other._deleted; + + _data = new T[_capacity]; + _states = new State[_capacity]; + for (size_t i = 0; i < _size; ++i) { + _data[i] = other._data[i]; + _states[i] = other._states[i]; + } + for (size_t i = _size; i < _capacity; ++i) + _states[i] = State::empty; + } + return *this; + } + + TVector& operator=(const TVector& other) noexcept { + if (this != &other) { // + delete[] _data; + delete[] _states; + + _size = other._size; + _capacity = other._capacity; + _deleted = other._deleted; + + _data = new T[_capacity]; + _states = new State[_capacity]; + + for (size_t i = 0; i < _size; ++i) { + _data[i] = other._data[i]; + _states[i] = other._states[i]; + } + for (size_t i = _size; i < _capacity; ++i) + _states[i] = State::empty; + } + return *this; + } + + void clear() noexcept { + for (size_t i = 0; i < _size; ++i) { + _states[i] = State::empty; + } + _size = 0; + _deleted = 0; + } + + bool operator == (const TVector& other) const noexcept { + if (_size != other._size) { + return false; + } + for (size_t i = 0; i < _size; ++i) { + if (_states[i] == busy && _data[i] != other._data[i]) { + return false; + } + } + return true; + } + + bool operator != (const TVector& other) const noexcept { + if (*this == other) { + return false; + } + if (_size != other._size) { + return true; + } + for (size_t i = 0; i < _size; ++i) { + if (_states[i] != other._states[i]) { + return true; + } + if (_states[i] == busy && _data[i] != other._data[i]) { + return true; + } + } + return false; + } + + + void reserve(size_t new_capacity) { // _capacity + if (new_capacity <= _capacity) return; + + T* new_data = new T[new_capacity]; + State* new_states = new State[new_capacity]; + + for (size_t i = 0; i < _size; ++i) { // + new_data[i] = _data[i]; + new_states[i] = _states[i]; + } + + for (size_t i = _size; i < new_capacity; ++i) { // + new_states[i] = State::empty; + } + + delete[] _data; + delete[] _states; + _data = new_data; + _states = new_states; + _capacity = new_capacity; + } + + void resize(size_t new_size) { + if (new_size < _size) { + for (size_t i = new_size; i < _size; ++i) { + if (_states[i] == State::deleted) + --_deleted; + _states[i] = State::empty; + _data[i].~T(); + } + _size = new_size; + return; + } + + if (new_size > _capacity) { + reserve(new_size + RESERVE_MEMORY); + } + + for (size_t i = _size; i < new_size; ++i) { + new (_data + i) T(); // _data + i + _states[i] = busy; + } + _size = new_size; + } + + void resize(size_t new_size, const T& value) { + if (new_size == _size) { + return; + } + + if (new_size < _size) { + for (size_t i = new_size; i < _size; ++i) { + if (_states[i] == deleted) + --_deleted; + _states[i] = empty; + _data[i].~T(); + } + _size = new_size; + return; + } + + if (new_size > _capacity) { + reserve(new_size + RESERVE_MEMORY); + } + + for (size_t i = _size; i < new_size; ++i) { + new (_data + i) T(value); // _data + i + _states[i] = busy; + } + _size = new_size; + } + + void shrink_to_fit() { // , + size_t busy_count = 0; + + for (size_t i = 0; i < _size; ++i) { // busy + if (_states[i] == busy) + ++busy_count; + } + + if (busy_count == _size && _capacity == _size) + return; + + size_t new_capacity = busy_count + RESERVE_MEMORY; + T* new_data = new T[new_capacity]; + State* new_states = new State[new_capacity]; + + size_t new_index = 0; + for (size_t i = 0; i < _size; ++i) { + if (_states[i] == busy) { + new_data[new_index] = _data[i]; + new_states[new_index] = busy; + ++new_index; + } + } + + for (size_t i = new_index; i < new_capacity; ++i) { + new_states[i] = empty; + } + + delete[] _data; + delete[] _states; + + _data = new_data; + _states = new_states; + _capacity = new_capacity; + _size = busy_count; + _deleted = 0; + } + + void print() { + std::cout << "[ "; + for (size_t i = 0; i < _size; ++i) { + if (_states[i] == State::busy) { + std::cout << _data[i] << " "; + } + } + std::cout << "]"; + } + template + friend std::ostream& operator<<(std::ostream& out, const TVector& vec); + + + template + friend void swap(U& a, U& b); + template + friend void shuffle(TVector& vec); + template + friend void quick_sort(TVector& vec); + template + friend size_t find_first(const TVector& vec, const U& value); + template + friend size_t find_last(const TVector& vec, const U& value); + template + friend size_t* find_all(const TVector& vec, const U& value); + template + friend void quick_sort_realisation(TVector& vec, int left, int right); + +private: + + /*void check_index(size_t index) const { // 6.09 ( + if (index >= _size) { + throw std::out_of_range("Index out of bounds: index >= size"); + } + if (_states[index] != State::busy) { + throw std::out_of_range("Accessing element that is not active (deleted or empty)"); + } + }*/ + /*void check_index(size_t index) const { + if (index >= _size) { + throw std::out_of_range("Index out of bounds: index >= size"); + } + size_t count = 0; + for (size_t i = 0; i < _size; i++) { + if (_states[i] == State::busy) { + if (count == index) { + return; + } + count++; + } + } + throw std::out_of_range("There no element with this index"); + }*/ + size_t check_index(size_t index) const { + if (index >= _size) { + throw std::out_of_range("Index out of bounds: index >= size"); + } + size_t count = 0; + for (size_t i = 0; i < _size; i++) { + if (_states[i] == State::busy) { + if (count == index) { + return i; + } + count++; + } + } + throw std::out_of_range("There no element with this index"); + } + inline bool is_full() const noexcept { // + return _size >= _capacity; + } + template + friend class TVectorTester; + }; +template +void swap(T& a, T& b) { + T temp = a; + a = b; + b = temp; +} + +template +void shuffle(TVector& vec) { + if (vec._size <= 1) { + return; + } + for (size_t i = vec._size - 1; i > 0; --i) { + if (vec._states[i] != busy) { + continue; + } + size_t j = std::rand() % (i + 1); + while (vec._states[j] != busy) { + j = std::rand() % (i + 1); + } + swap(vec._data[i], vec._data[j]); + } +} + +/* +template +int partition(TVector& vec, int left, int right) { + T p = vec._data[right]; + int i = left - 1; + + for (int j = left; j < right; ++j) { + if (vec._states[j] == busy && vec._data[j] <= p) { + ++i; + swap(vec._data[i], vec._data[j]); + } + } + swap(vec._data[i + 1], vec._data[right]); + return i + 1; +} + +template +void quick_sort_realisation(TVector& vec, int left, int right) { + if (left < right) { + int p = partition(vec, left, right); + quick_sort_realisation(vec, left, p - 1); + quick_sort_realisation(vec, p + 1, right); + } +} +*/ + + +template +void quick_sort_realisation(TVector& vec, int left, int right) { + if (left > right) { + return; + } + T p = vec[(left + right) / 2]; + int i = left; + int j = right; + while (i <= j) { + while (vec[i] < p) i++; + while (vec[j] > p) j--; + if (i <= j) { + swap(vec._data[i], vec._data[j]); + swap(vec._states[i], vec._states[j]); + i++; + j--; + } + } + quick_sort_realisation(vec, left, j); + quick_sort_realisation(vec, i, right); +} +template +void quick_sort(TVector& vec) { + if (vec.size() > 1) + quick_sort_realisation(vec, 0, vec.size() - 1); +} + +template +size_t find_first(const TVector& vec, const T& value) { + for (size_t i = 0; i < vec.size(); ++i) + if (vec.state(i) == State::busy && vec.data(i) == value) { + return i; + } + throw std::logic_error("Element not found"); +} +template +size_t find_last(const TVector& vec, const T& value) { + for (size_t i = vec._size; i-- > 0;) + if (vec.state(i) == State::busy && vec.data(i) == value) { + return i; + } + throw std::logic_error("Element not found"); +} +template +size_t* find_all(const TVector& vec, const T& value) { + size_t result_size = 0; + + for (size_t i = 0; i < vec.size(); ++i) { // + if (vec.state(i) == State::busy && vec.data(i) == value) { + ++result_size; + } + } + + if (result_size == 0) { + return nullptr; + } + size_t* indices = new size_t[result_size]; + size_t j = 0; + for (size_t i = 0; i < vec.size(); ++i) { + if (vec.state(i) == State::busy && vec.data(i) == value) { + indices[j++] = i; + } + } + return indices; +} + +template +std::ostream& operator<<(std::ostream& out, const TVector& vec) { + out << "[ "; + for (size_t i = 0; i < vec._size; ++i) { + if (vec._states[i] == State::busy) { + out << vec._data[i] << " "; + } + } + out << "]"; + return out; +} + #endif // LIB_TVECTOR From 1e97ef8da34a814b5130c700e1b0f9b7561e4073 Mon Sep 17 00:00:00 2001 From: Kate-Nits Date: Wed, 24 Sep 2025 21:46:25 +0300 Subject: [PATCH 14/94] Added a normal implementation of the class TVector without tests --- lib_tvector/tvector.h | 164 +++++++++++------------------------------- 1 file changed, 43 insertions(+), 121 deletions(-) diff --git a/lib_tvector/tvector.h b/lib_tvector/tvector.h index 8d53dc75..184bc6e9 100644 --- a/lib_tvector/tvector.h +++ b/lib_tvector/tvector.h @@ -1,4 +1,4 @@ -// Copyright 2025 Ekaterina Ushnitskaya +// Copyright 2025 Ekaterina Ushnitskaya #ifndef LIB_TVECTOR_TVECTOR_H #define LIB_TVECTOR_TVECTOR_H @@ -21,8 +21,8 @@ class TVector { static const size_t RESERVE_MEMORY = 15; static const size_t MAX_PERCENT_DELETED = 15; - TVector() : _data(nullptr), _states(nullptr), _size(0), _capacity(0), _deleted(0) {} // - TVector(size_t size) { // + TVector() : _data(nullptr), _states(nullptr), _size(0), _capacity(0), _deleted(0) {} //êîíñòðóêòîð ïî óìîë÷àíèþ + TVector(size_t size) { //конструктор вектора заданного размера _size = size; _capacity = size + RESERVE_MEMORY; _deleted = 0; @@ -32,8 +32,7 @@ class TVector { _states[i] = i < _size ? State::busy : State::empty; } } - TVector(T* data, size_t size) { // - // , ??????????????????? + TVector(T* data, size_t size) { //конструктор созданный на основе переаного массива данных _size = size; _capacity = size + RESERVE_MEMORY; _deleted = 0; @@ -45,7 +44,7 @@ class TVector { _states = new State[_capacity]; } catch (const std::bad_alloc&) { - delete[] _data; // _data + delete[] _data; //тк мы же уже под _data выделили throw; } for (size_t i = 0; i < _capacity; ++i) { @@ -57,9 +56,8 @@ class TVector { _states[i] = State::empty; } } - } - TVector(const TVector& other) { // + TVector(const TVector& other) { // конструктор копирования _size = other._size; _capacity = other._capacity; _deleted = other._deleted; @@ -68,7 +66,7 @@ class TVector { try { _data = new T[_capacity]; } - catch (const std::bad_alloc&) { // + catch (const std::bad_alloc&) { //Ловит ошибку нехватки памяти throw; std::cout << std::endl; } @@ -94,13 +92,10 @@ class TVector { } inline T& operator[](size_t index) { - //check_index(index); ////////////////////// - size_t real_index = check_index(index); - return _data[real_index]; + return _data[index]; } inline const T& operator[](size_t index) const { - size_t real_index = check_index(index); - return _data[real_index]; + return _data[index]; } inline T& at(size_t index) { @@ -116,7 +111,7 @@ class TVector { return _data; } - inline const T data(size_t i) const { // ??? + inline const T& data(size_t i) const { size_t real_index = check_index(i); return _data[real_index]; } @@ -124,11 +119,10 @@ class TVector { inline const State* states() const noexcept { return _states; } - inline const State state(size_t i) const {// ? - size_t real_index = check_index(i); // ??????????????? - return _states[real_index]; - } + inline const State state(size_t i) const { + return _states[i]; + } inline const size_t size() const noexcept {//get return _size; @@ -144,44 +138,40 @@ class TVector { reserve(new_capacity); } - - - inline T* begin() noexcept { // _data + inline T* begin() noexcept { // возвращает _data указатель на начало return _data; } inline T* end() noexcept { return _data + _size; } - inline const size_t deleted() const noexcept { return _deleted; } - inline T& front() { // , deleted empty + inline T& front() { //Доступ к первому элементу, который не deleted и не empty for (size_t i = 0; i < _size; ++i) if (_states[i] == busy) return _data[i]; throw std::out_of_range("No busy elements in vector"); } - inline T& back() { // , deleted empty + inline T& back() { //Доступ к последнему элементу, который не deleted и не empty for (size_t i = _size; i-- > 0; ) if (_states[i] == busy) return _data[i]; throw std::out_of_range("No busy elements in vector"); } + inline bool is_empty() const noexcept { return _size == 0 || (_size - _deleted == 0); } //ôóíêöèÿ ïðîâåðêè íà ïóñòîòó - inline bool is_empty() const noexcept { return _size == 0 || (_size - _deleted == 0); } // - - // + //функции вставки void push_front(const T& value) noexcept { if (is_full()) { reserve(_capacity + RESERVE_MEMORY); } - for (size_t i = _size; i > 0; --i) { // + for (size_t i = _size; i > 0; --i) { // Сдвигаем всё вправо _data[i] = _data[i - 1]; _states[i] = _states[i - 1]; } @@ -205,7 +195,7 @@ class TVector { reserve(_capacity + RESERVE_MEMORY); } size_t real_index = check_index(index); - for (size_t i = _size; i > real_index; --i) { // + for (size_t i = _size; i > real_index; --i) { // Сдвигаем вправо _data[i] = _data[i - 1]; _states[i] = _states[i - 1]; } @@ -214,7 +204,7 @@ class TVector { ++_size; } - // + //функции удаления void pop_front() { if (_data != nullptr && _states != nullptr) { for (size_t i = 0; i < _size; ++i) { @@ -243,7 +233,7 @@ class TVector { --_size; return; } - if (i <= 0) break; // + if (i <= 0) break; // иначе уйдём в переполнение } } throw std::underflow_error("Vector is empty"); @@ -257,20 +247,7 @@ class TVector { } } - // - /*void emplace(size_t index, T&& value) { // - check_index(index); - if (is_full()) { - reserve(_capacity + RESERVE_MEMORY); - } - for (size_t i = _size; i > index; --i) { - _data[i] = _data[i - 1]; - _states[i] = _states[i - 1]; - } - _data[index] = value; - _states[index] = State::busy; - ++_size; - }*/ + //замена значения void emplace(size_t index, T&& value) { size_t real_index = check_index(index); _data[real_index] = value; @@ -298,8 +275,8 @@ class TVector { return *this; } - TVector& operator=(const TVector& other) noexcept { - if (this != &other) { // + TVector& operator=(const TVector& other) { + if (this != &other) { //проверка на самоприсваивание delete[] _data; delete[] _states; @@ -358,19 +335,18 @@ class TVector { return false; } - - void reserve(size_t new_capacity) { // _capacity + void reserve(size_t new_capacity) { // увеличивает _capacity if (new_capacity <= _capacity) return; T* new_data = new T[new_capacity]; State* new_states = new State[new_capacity]; - for (size_t i = 0; i < _size; ++i) { // + for (size_t i = 0; i < _size; ++i) { // Копируем существующие элементы new_data[i] = _data[i]; new_states[i] = _states[i]; } - for (size_t i = _size; i < new_capacity; ++i) { // + for (size_t i = _size; i < new_capacity; ++i) { // Помечаем оставшиеся ячейки как пустые new_states[i] = State::empty; } @@ -398,17 +374,14 @@ class TVector { } for (size_t i = _size; i < new_size; ++i) { - new (_data + i) T(); // _data + i + new (_data + i) T(); // размещает результат в уже выделенной памяти по адресу _data + i _states[i] = busy; } _size = new_size; } void resize(size_t new_size, const T& value) { - if (new_size == _size) { - return; - } - + if (new_size == _size) { return; } if (new_size < _size) { for (size_t i = new_size; i < _size; ++i) { if (_states[i] == deleted) @@ -419,28 +392,23 @@ class TVector { _size = new_size; return; } - if (new_size > _capacity) { reserve(new_size + RESERVE_MEMORY); } - for (size_t i = _size; i < new_size; ++i) { - new (_data + i) T(value); // _data + i + new (_data + i) T(value); // размещает результат в уже выделенной памяти по адресу _data + i _states[i] = busy; } _size = new_size; } - void shrink_to_fit() { // , + void shrink_to_fit() { // уменьшение размера, удаляя неиспользуемую память size_t busy_count = 0; - - for (size_t i = 0; i < _size; ++i) { // busy + for (size_t i = 0; i < _size; ++i) { // Считаем busy элементы if (_states[i] == busy) ++busy_count; } - - if (busy_count == _size && _capacity == _size) - return; + if (busy_count == _size && _capacity == _size) { return; } size_t new_capacity = busy_count + RESERVE_MEMORY; T* new_data = new T[new_capacity]; @@ -478,50 +446,33 @@ class TVector { } std::cout << "]"; } + template friend std::ostream& operator<<(std::ostream& out, const TVector& vec); - template friend void swap(U& a, U& b); + template friend void shuffle(TVector& vec); + template friend void quick_sort(TVector& vec); + template friend size_t find_first(const TVector& vec, const U& value); + template friend size_t find_last(const TVector& vec, const U& value); + template friend size_t* find_all(const TVector& vec, const U& value); + template friend void quick_sort_realisation(TVector& vec, int left, int right); private: - /*void check_index(size_t index) const { // 6.09 ( - if (index >= _size) { - throw std::out_of_range("Index out of bounds: index >= size"); - } - if (_states[index] != State::busy) { - throw std::out_of_range("Accessing element that is not active (deleted or empty)"); - } - }*/ - /*void check_index(size_t index) const { - if (index >= _size) { - throw std::out_of_range("Index out of bounds: index >= size"); - } - size_t count = 0; - for (size_t i = 0; i < _size; i++) { - if (_states[i] == State::busy) { - if (count == index) { - return; - } - count++; - } - } - throw std::out_of_range("There no element with this index"); - }*/ size_t check_index(size_t index) const { if (index >= _size) { throw std::out_of_range("Index out of bounds: index >= size"); @@ -537,12 +488,11 @@ class TVector { } throw std::out_of_range("There no element with this index"); } - inline bool is_full() const noexcept { // + inline bool is_full() const noexcept { // функция проверки на заполненость return _size >= _capacity; } template friend class TVectorTester; - }; template @@ -569,33 +519,6 @@ void shuffle(TVector& vec) { } } -/* -template -int partition(TVector& vec, int left, int right) { - T p = vec._data[right]; - int i = left - 1; - - for (int j = left; j < right; ++j) { - if (vec._states[j] == busy && vec._data[j] <= p) { - ++i; - swap(vec._data[i], vec._data[j]); - } - } - swap(vec._data[i + 1], vec._data[right]); - return i + 1; -} - -template -void quick_sort_realisation(TVector& vec, int left, int right) { - if (left < right) { - int p = partition(vec, left, right); - quick_sort_realisation(vec, left, p - 1); - quick_sort_realisation(vec, p + 1, right); - } -} -*/ - - template void quick_sort_realisation(TVector& vec, int left, int right) { if (left > right) { @@ -642,8 +565,7 @@ size_t find_last(const TVector& vec, const T& value) { template size_t* find_all(const TVector& vec, const T& value) { size_t result_size = 0; - - for (size_t i = 0; i < vec.size(); ++i) { // + for (size_t i = 0; i < vec.size(); ++i) { // Считаем количество подходящих элементов if (vec.state(i) == State::busy && vec.data(i) == value) { ++result_size; } From 8b0802895709d44912386ff5206eeacbb163465b Mon Sep 17 00:00:00 2001 From: Kate-Nits Date: Wed, 24 Sep 2025 22:42:33 +0300 Subject: [PATCH 15/94] Rewrote the tests for the classes Point, Point3D, Circle, Sphere --- tests/test_circle.cpp | 42 ++++++++++---------------- tests/test_point.cpp | 38 ++++++++++++----------- tests/test_point3d.cpp | 68 ++++++++++++++++++++++-------------------- tests/test_sphere.cpp | 45 ++++++++++------------------ 4 files changed, 87 insertions(+), 106 deletions(-) diff --git a/tests/test_circle.cpp b/tests/test_circle.cpp index a4866041..9d722fc1 100644 --- a/tests/test_circle.cpp +++ b/tests/test_circle.cpp @@ -11,15 +11,10 @@ TEST(TestCircleLib, default_constructor) { // Arrange Circle A; Point b; - // Act - int actual_result = FALSE; - if (A.get_center() == b && A.get_radius() == 1) { - actual_result = TRUE; - } - // Assert - int expected_result = TRUE; - EXPECT_EQ(expected_result, actual_result); // + // Act & Assert + EXPECT_EQ(1, A.get_radius()); // + EXPECT_EQ(b, A.get_center()); } TEST(TestCircleLib, parameterized_constructor_with_normal_radius) { @@ -27,44 +22,37 @@ TEST(TestCircleLib, parameterized_constructor_with_normal_radius) { Point a(5, 27); Circle C(a, 6); - // Act - int actual_result = FALSE; - if (C.get_center() == a && C.get_radius() == 6) { - actual_result = TRUE; - } - - // Assert - int expected_result = TRUE; - EXPECT_EQ(expected_result, actual_result); // + // Act & Assert + EXPECT_EQ(6, C.get_radius()); // + EXPECT_EQ(a, C.get_center()); } TEST(TestCircleLib, parameterized_constructor_with_radius_less_than_zero) { // Arrange Point a(-3, 15); + // Act & Assert - EXPECT_THROW({ Circle C(a, -8); }, std::logic_error); + ASSERT_ANY_THROW(Circle C(a, -8)); } TEST(TestCircleLib, copy_constructor_without_throw) { // Arrange - Circle A(Point(-4, 10), 7); - Circle B(A); + Point M(-4, 10); + Circle A(M, 7); + // Act - int actual_result = FALSE; - if (B.get_center() == A.get_center() && B.get_radius() == A.get_radius()) { - actual_result = TRUE; - } + Circle B(A); // Assert - int expected_result = TRUE; - EXPECT_EQ(expected_result, actual_result); // + EXPECT_EQ(M, B.get_center()); // + EXPECT_EQ(7, B.get_radius()); } TEST(TestCircleLib, copy_constructor_with_throw) { // Arrange Circle* null_pointer = nullptr; // Act & Assert - EXPECT_THROW({ Circle & bad_reference = *null_pointer; Circle a(bad_reference); }, std::logic_error); + ASSERT_ANY_THROW(Circle C(*null_pointer)); } diff --git a/tests/test_point.cpp b/tests/test_point.cpp index 9198f3a9..a74b0a8f 100644 --- a/tests/test_point.cpp +++ b/tests/test_point.cpp @@ -8,9 +8,10 @@ #define FALSE 0 TEST(TestPointLib, default_constructor) { - // Arrange + // Arrange () Point a; + // Act & Assert EXPECT_EQ(0, a.get_oy()); EXPECT_EQ(0, a.get_ox()); // } @@ -19,18 +20,19 @@ TEST(TestPointLib, parameterized_constructor) { // Arrange & act Point a(5, 27); + // Assert () EXPECT_EQ(5, a.get_ox()); EXPECT_EQ(27, a.get_oy()); } TEST(TestPointLib, copy_constructor_without_throw) { - // Arrange + // Arrange () Point a(-3, 72); - - // Act - Point b(a); + // Act () + Point b(a); + // Assert () EXPECT_EQ(-3, b.get_ox()); EXPECT_EQ(72, b.get_oy()); // } @@ -38,32 +40,34 @@ TEST(TestPointLib, copy_constructor_without_throw) { TEST(TestPointLib, copy_constructor_with_throw) { // Arrange Point* null_pointer = nullptr; + // Act & Assert ASSERT_ANY_THROW(Point a(*null_pointer)); } -TEST(TestPointLib, can_distance_to) { - // Arrange - Point a(1, 2); - Point b(4, 6); - - // Act - // Assert - EXPECT_NEAR(5.0, a.distance_to(b), EPSILON); //, -} - TEST(TestPointLib, can_compare_with_operator_two_equal_object) { - // Arrage + // Arrange Point a(1, 2); Point b(1, 2); + // Act & Assert EXPECT_TRUE(a == b); } TEST(TestPointLib, can_compare_with_operator_two_not_equal_object) { - // Arrage + // Arrange Point a(1, 2); Point b(4, 2); + // Act & Assert EXPECT_FALSE(a == b); +} + +TEST(TestPointLib, can_distance_to) { + // Arrange + Point a(1, 2); + Point b(4, 6); + + // Act & Assert + EXPECT_NEAR(5.0, a.distance_to(b), EPSILON); //, } \ No newline at end of file diff --git a/tests/test_point3d.cpp b/tests/test_point3d.cpp index 7a5726c0..5c95b471 100644 --- a/tests/test_point3d.cpp +++ b/tests/test_point3d.cpp @@ -8,56 +8,62 @@ #define FALSE 0 TEST(TestPoint3dLib, default_constructor) { - // Arrange + // Arrange () Point3D a; - // Act - int actual_result = FALSE; - if (a.get_ox() == 0 && a.get_oy() == 0 && a.get_oz() == 0) { - actual_result = TRUE; - } - - // Assert - int expected_result = TRUE; - EXPECT_EQ(expected_result, actual_result); // + // Assert & Act + EXPECT_EQ(0, a.get_ox()); // + EXPECT_EQ(0, a.get_oy()); + EXPECT_EQ(0, a.get_oz()); } TEST(TestPoint3dLib, parameterized_constructor) { // Arrange Point3D a(5, 27, -45); - // Act - int actual_result = FALSE; - if (a.get_ox() == 5 && a.get_oy() == 27 && a.get_oz() == -45) { - actual_result = TRUE; - } - - // Assert - int expected_result = TRUE; - EXPECT_EQ(expected_result, actual_result); // + // Assert & Act + EXPECT_EQ(5, a.get_ox()); // + EXPECT_EQ(27, a.get_oy()); + EXPECT_EQ(-45, a.get_oz()); } TEST(TestPoint3dLib, copy_constructor_without_throw) { // Arrange Point3D a(-3, 72, 15); - Point3D b(a); // Act - int actual_result = FALSE; - if (a.get_ox() == b.get_ox() && a.get_oy() == b.get_oy() && a.get_oz() == b.get_oz()) { - actual_result = TRUE; - } + Point3D b(a); // Assert - int expected_result = TRUE; - EXPECT_EQ(expected_result, actual_result); // + EXPECT_EQ(-3, b.get_ox()); // + EXPECT_EQ(72, b.get_oy()); + EXPECT_EQ(15, b.get_oz()); } TEST(TestPoint3dLib, copy_constructor_with_throw) { // Arrange Point3D* null_pointer = nullptr; + + // Act & Assert + ASSERT_ANY_THROW(Point3D a(*null_pointer)); +} + +TEST(TestPoint3dLib, can_compare_with_operator_two_equal_object) { + // Arrange + Point3D a(1, 2, 3); + Point3D b(1, 2, 3); + + // Act & Assert + EXPECT_TRUE(a == b); +} + +TEST(TestPoint3dLib, can_compare_with_operator_two_not_equal_object) { + // Arrange + Point3D a(1, 2, 7); + Point3D b(4, 2, 7); + // Act & Assert - EXPECT_THROW({ Point3D & bad_reference = *null_pointer; Point3D a(bad_reference); }, std::logic_error); + EXPECT_FALSE(a == b); } TEST(TestPoint3dLib, can_distance_to) { @@ -65,10 +71,6 @@ TEST(TestPoint3dLib, can_distance_to) { Point3D a(0, -3, 3); Point3D b(3, 1, 3); - // Act - float actual_result = a.distance_to(b); - - // Assert - float expected_result = 5.0; - EXPECT_NEAR(expected_result, actual_result, EPSILON); //, + // Act & Assert + EXPECT_NEAR(5.0, a.distance_to(b), EPSILON); //, } \ No newline at end of file diff --git a/tests/test_sphere.cpp b/tests/test_sphere.cpp index ba92c253..7e3a1e7f 100644 --- a/tests/test_sphere.cpp +++ b/tests/test_sphere.cpp @@ -11,58 +11,45 @@ TEST(TestSphereLib, default_constructor) { // Arrange Sphere A; Point3D b; - // Act - int actual_result = FALSE; - if (A.get_center() == b && A.get_radius() == 1) { - actual_result = TRUE; - } - // Assert - int expected_result = TRUE; - EXPECT_EQ(expected_result, actual_result); // + // Act & Assert + EXPECT_EQ(b, A.get_center()); // + EXPECT_EQ(1, A.get_radius()); } TEST(TestSphereLib, parameterized_constructor_with_normal_radius) { // Arrange - Point3D a(5, 27, 14); - Sphere C(a, 6); - - // Act - int actual_result = FALSE; - if (C.get_center() == a && C.get_radius() == 6) { - actual_result = TRUE; - } + Point3D A(5, 27, 14); + Sphere C(A, 6); - // Assert - int expected_result = TRUE; - EXPECT_EQ(expected_result, actual_result); // + // Act & Assert + EXPECT_EQ(A, C.get_center()); // + EXPECT_EQ(6, C.get_radius()); } TEST(TestSphereLib, parameterized_constructor_with_radius_less_than_zero) { // Arrange Point3D a(7, 6, 14); + // Act & Assert - EXPECT_THROW({ Sphere C(a, -1); }, std::logic_error); + ASSERT_ANY_THROW( Sphere C(a, -1)); } TEST(TestSphereLib, copy_constructor_without_throw) { // Arrange - Sphere A(Point3D(-4, 10, 54), 7); - Sphere B(A); + Point3D C(-4, 10, 54); + Sphere A(C, 7); // Act - int actual_result = FALSE; - if (B.get_center() == A.get_center() && B.get_radius() == A.get_radius()) { - actual_result = TRUE; - } + Sphere B(A); // Assert - int expected_result = TRUE; - EXPECT_EQ(expected_result, actual_result); // + EXPECT_EQ(C, B.get_center()); // + EXPECT_EQ(7, B.get_radius()); } TEST(TestSphereLib, copy_constructor_with_throw) { // Arrange Sphere* null_pointer = nullptr; // Act & Assert - EXPECT_THROW({ Sphere & bad_reference = *null_pointer; Sphere a(bad_reference); }, std::logic_error); + ASSERT_ANY_THROW( Sphere C(*null_pointer)); } \ No newline at end of file From 2d0fa92352701711b05b4c3b9e4d029cd691ca5c Mon Sep 17 00:00:00 2001 From: Kate-Nits Date: Thu, 25 Sep 2025 00:42:35 +0300 Subject: [PATCH 16/94] Added tests to check the constructors of the class TVector --- tests/test_tvector.cpp | 90 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) diff --git a/tests/test_tvector.cpp b/tests/test_tvector.cpp index e69de29b..a0ae7898 100644 --- a/tests/test_tvector.cpp +++ b/tests/test_tvector.cpp @@ -0,0 +1,90 @@ +// Copyright 2025 Ekaterina Ushnitskaya + +#include +#include "../lib_tvector/tvector.h" + +#define EPSILON 0.000001 +#define TRUE 1 +#define FALSE 0 + +TEST(TestTVectorLib, default_constructor) { + // Arrange () + TVector vec; + + // Act & Assert + EXPECT_EQ(nullptr, vec.data()); // + EXPECT_EQ(nullptr, vec.states()); + EXPECT_EQ(0, vec.size()); + EXPECT_EQ(0, vec.capacity()); + EXPECT_EQ(0, vec.deleted()); + EXPECT_TRUE(vec.is_empty()); +} + +TEST(TestTVectorLib, constructor_with_size) { + // Arrange () + size_t size = 10; + TVector vec(size); + + // Act & Assert + EXPECT_EQ(size, vec.size()); + EXPECT_EQ(size + TVector::RESERVE_MEMORY, vec.capacity()); + EXPECT_EQ(0, vec.deleted()); + EXPECT_FALSE(vec.is_empty()); + for (size_t i = 0; i < size; i++) { + EXPECT_EQ(State::busy, vec.state(i)); + } + for (size_t i = size; i < vec.capacity(); i++) { + EXPECT_EQ(State::empty, vec.state(i)); + } +} + +TEST(TestTVectorLib, constructor_with_mass_all_parameters_are_good) { + // Arrange () + size_t size = 3; + int copy_mass[3] = { 1, 2, 3 }; + + // Act + TVector vec(copy_mass, size); + + // Assert + EXPECT_EQ(size, vec.size()); + EXPECT_EQ(size + TVector::RESERVE_MEMORY, vec.capacity()); + EXPECT_EQ(0, vec.deleted()); + EXPECT_FALSE(vec.is_empty()); + EXPECT_EQ(1, vec.data(0)); + EXPECT_EQ(2, vec.data(1)); + EXPECT_EQ(3, vec.data(2)); + for (size_t i = 0; i < size; i++) { + EXPECT_EQ(State::busy, vec.state(i)); + } + for (size_t i = size; i < vec.capacity(); i++) { + EXPECT_EQ(State::empty, vec.state(i)); + } +} + +TEST(TestTVectorLib, constructor_with_mass_with_nullptr_and_size_gt_zero) { + // Arrange + int* null_data = nullptr; + + // Act & Assert + ASSERT_ANY_THROW(TVector vec(null_data, 5)); +} + +TEST(TestTVectorLib, copy_constructor) { + // Arrange + int arr[4] = { 1, 2, 3, 4 }; + TVector vec1(arr, 4); + + // Act + TVector vec2(vec1); + + // Assert + EXPECT_EQ(vec1.size(), vec2.size()); + EXPECT_EQ(vec1.capacity(), vec2.capacity()); + EXPECT_EQ(vec1.deleted(), vec2.deleted()); + + for (size_t i = 0; i < vec1.size(); i++) { + EXPECT_EQ(vec1.data(i), vec2.data(i)); + EXPECT_EQ(vec1.state(i), vec2.state(i)); + } +} From e46803ce6adaf96b53ad3e45c96ff43b6dafaff2 Mon Sep 17 00:00:00 2001 From: Kate-Nits Date: Fri, 26 Sep 2025 00:26:56 +0300 Subject: [PATCH 17/94] Added tests to check at() --- tests/test_tvector.cpp | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/tests/test_tvector.cpp b/tests/test_tvector.cpp index a0ae7898..1555464e 100644 --- a/tests/test_tvector.cpp +++ b/tests/test_tvector.cpp @@ -82,9 +82,31 @@ TEST(TestTVectorLib, copy_constructor) { EXPECT_EQ(vec1.size(), vec2.size()); EXPECT_EQ(vec1.capacity(), vec2.capacity()); EXPECT_EQ(vec1.deleted(), vec2.deleted()); - for (size_t i = 0; i < vec1.size(); i++) { EXPECT_EQ(vec1.data(i), vec2.data(i)); EXPECT_EQ(vec1.state(i), vec2.state(i)); } } + +TEST(TestTVectorLib, test_at_checking_without_difficulties) { + // Arrange + size_t size = 8; + int arr[8] = { 0, 1, 2, 3, 4, 5, 6, 7 }; + TVector vec(arr, size); + + // Act & Assert + EXPECT_EQ(4, vec.at(4)); +} + +TEST(TestTVectorLib, test_at_checking_with_pop_front) { + // Arrange + size_t size = 8; + int arr[8] = { 0, 1, 2, 3, 4, 5, 6, 7 }; + TVector vec(arr, size); + + // Act + vec.pop_front(); + + // Assert + EXPECT_EQ(1, vec.at(0)); +} \ No newline at end of file From 8dc09eb1a003a10818a78006bc8567b585ae5d05 Mon Sep 17 00:00:00 2001 From: Kate-Nits Date: Fri, 26 Sep 2025 00:35:59 +0300 Subject: [PATCH 18/94] Added tests to check state() --- tests/test_tvector.cpp | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/tests/test_tvector.cpp b/tests/test_tvector.cpp index 1555464e..d03b9812 100644 --- a/tests/test_tvector.cpp +++ b/tests/test_tvector.cpp @@ -109,4 +109,37 @@ TEST(TestTVectorLib, test_at_checking_with_pop_front) { // Assert EXPECT_EQ(1, vec.at(0)); +} + +TEST(TestTVectorLib, test_state_state_deleted) { + // Arrange + size_t size = 8; + int arr[8] = { 0, 1, 2, 3, 4, 5, 6, 7 }; + TVector vec(arr, size); + + // Act + vec.pop_front(); + + // Assert + EXPECT_EQ(State::deleted, vec.state(0)); +} + +TEST(TestTVectorLib, test_state_state_busy) { + // Arrange + size_t size = 8; + int arr[8] = { 0, 1, 2, 3, 4, 5, 6, 7 }; + TVector vec(arr, size); + + // Act & Assert + EXPECT_EQ(State::busy, vec.state(4)); +} + +TEST(TestTVectorLib, test_state_state_empty) { + // Arrange + size_t size = 8; + int arr[8] = { 0, 1, 2, 3, 4, 5, 6, 7 }; + TVector vec(arr, size); + + // Act & Assert + EXPECT_EQ(State::empty, vec.state(size+1)); } \ No newline at end of file From b89c71050b61e36df3e16fc022c9192a00de7e42 Mon Sep 17 00:00:00 2001 From: Kate-Nits Date: Fri, 26 Sep 2025 00:46:41 +0300 Subject: [PATCH 19/94] Added tests to check front() --- tests/test_tvector.cpp | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/tests/test_tvector.cpp b/tests/test_tvector.cpp index d03b9812..b19434bf 100644 --- a/tests/test_tvector.cpp +++ b/tests/test_tvector.cpp @@ -142,4 +142,27 @@ TEST(TestTVectorLib, test_state_state_empty) { // Act & Assert EXPECT_EQ(State::empty, vec.state(size+1)); +} + +TEST(TestTVectorLib, test_front_without_deleted_and_empty_elements) { + // Arrange + size_t size = 3; + int arr[8] = { 112, 234, 345 }; + TVector vec(arr, size); + + // Act & Assert + EXPECT_EQ(112, vec.front()); +} + +TEST(TestTVectorLib, test_front_with_deleted_element) { + // Arrange + size_t size = 3; + int arr[8] = { 112, 234, 345 }; + TVector vec(arr, size); + + // Act + vec.pop_front(); + + // Assert + EXPECT_EQ(234, vec.front()); } \ No newline at end of file From ffbf8c7407a7fc70ec620416db20831e495f2798 Mon Sep 17 00:00:00 2001 From: Kate-Nits Date: Fri, 26 Sep 2025 00:53:05 +0300 Subject: [PATCH 20/94] Added tests to check back() --- tests/test_tvector.cpp | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/tests/test_tvector.cpp b/tests/test_tvector.cpp index b19434bf..a990d6b1 100644 --- a/tests/test_tvector.cpp +++ b/tests/test_tvector.cpp @@ -144,7 +144,7 @@ TEST(TestTVectorLib, test_state_state_empty) { EXPECT_EQ(State::empty, vec.state(size+1)); } -TEST(TestTVectorLib, test_front_without_deleted_and_empty_elements) { +TEST(TestTVectorLib, test_front_without_deleted_elements) { // Arrange size_t size = 3; int arr[8] = { 112, 234, 345 }; @@ -165,4 +165,27 @@ TEST(TestTVectorLib, test_front_with_deleted_element) { // Assert EXPECT_EQ(234, vec.front()); +} + +TEST(TestTVectorLib, test_back_without_deleted_elements) { + // Arrange + size_t size = 3; + int arr[8] = { 112, 234, 345 }; + TVector vec(arr, size); + + // Act & Assert + EXPECT_EQ(345, vec.back()); +} + +TEST(TestTVectorLib, test_back_with_deleted_element) { + // Arrange + size_t size = 3; + int arr[8] = { 112, 234, 345 }; + TVector vec(arr, size); + + // Act + vec.pop_back(); + + // Assert + EXPECT_EQ(234, vec.back()); } \ No newline at end of file From 38aec5aace41efabc45002ad604c75c29498f3ec Mon Sep 17 00:00:00 2001 From: Kate-Nits Date: Fri, 26 Sep 2025 01:00:55 +0300 Subject: [PATCH 21/94] Added tests to check begin() and end() --- tests/test_tvector.cpp | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/tests/test_tvector.cpp b/tests/test_tvector.cpp index a990d6b1..c9cf463e 100644 --- a/tests/test_tvector.cpp +++ b/tests/test_tvector.cpp @@ -144,6 +144,24 @@ TEST(TestTVectorLib, test_state_state_empty) { EXPECT_EQ(State::empty, vec.state(size+1)); } +TEST(TestTVectorLib, test_begin_and_end) { + // Arrange + size_t size = 3; + int arr[8] = { 11, 22, 37 }; + TVector vec(arr, size); + int sum = 0; + + // Act + int* begin = vec.begin(); + int* end = vec.end(); + for (int* p = begin; p < end; ++p) { + sum += *p; + } + + // Assert + EXPECT_EQ(11+22+37, sum); +} + TEST(TestTVectorLib, test_front_without_deleted_elements) { // Arrange size_t size = 3; From 1347906cf9622553588b6a4df91b6570165f4e9e Mon Sep 17 00:00:00 2001 From: Kate-Nits Date: Fri, 26 Sep 2025 01:09:48 +0300 Subject: [PATCH 22/94] Added test to check assign --- tests/test_tvector.cpp | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/tests/test_tvector.cpp b/tests/test_tvector.cpp index c9cf463e..df432b5d 100644 --- a/tests/test_tvector.cpp +++ b/tests/test_tvector.cpp @@ -206,4 +206,22 @@ TEST(TestTVectorLib, test_back_with_deleted_element) { // Assert EXPECT_EQ(234, vec.back()); +} + +TEST(TestTVectorLib, test_assign) { + // Arrange + size_t size = 3; + TVector vec1(size); + vec1[0] = 1; + vec1[1] = 2; + vec1[2] = 3; + + // Act + TVector vec2; + vec2.assign(vec1); + + // Assert + EXPECT_EQ(1, vec2[0]); + EXPECT_EQ(2, vec2[1]); + EXPECT_EQ(3, vec2[2]); } \ No newline at end of file From c1cb5edf490b596da880fbfb783d1eed896bf80b Mon Sep 17 00:00:00 2001 From: Kate-Nits Date: Fri, 26 Sep 2025 01:28:56 +0300 Subject: [PATCH 23/94] Added test to check operators equal and not equal --- lib_tvector/tvector.h | 2 +- tests/test_tvector.cpp | 56 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 1 deletion(-) diff --git a/lib_tvector/tvector.h b/lib_tvector/tvector.h index 184bc6e9..80e28b18 100644 --- a/lib_tvector/tvector.h +++ b/lib_tvector/tvector.h @@ -384,7 +384,7 @@ class TVector { if (new_size == _size) { return; } if (new_size < _size) { for (size_t i = new_size; i < _size; ++i) { - if (_states[i] == deleted) + if (_states[i] == State::deleted) --_deleted; _states[i] = empty; _data[i].~T(); diff --git a/tests/test_tvector.cpp b/tests/test_tvector.cpp index df432b5d..630baa4f 100644 --- a/tests/test_tvector.cpp +++ b/tests/test_tvector.cpp @@ -224,4 +224,60 @@ TEST(TestTVectorLib, test_assign) { EXPECT_EQ(1, vec2[0]); EXPECT_EQ(2, vec2[1]); EXPECT_EQ(3, vec2[2]); +} + +TEST(TestTVectorLib, can_compare_with_operator_two_equal_object) { + // Arrange + size_t size = 2; + TVector vec1(size); + vec1[0] = 5; + vec1[1] = 6; + TVector vec2(size); + vec2[0] = 5; + vec2[1] = 6; + + // Act & Assert + EXPECT_TRUE(vec1 == vec2); +} + +TEST(TestTVectorLib, can_compare_with_operator_two_not_equal_object) { + // Arrange + size_t size = 2; + TVector vec1(size); + vec1[0] = 5; + vec1[1] = 6; + TVector vec2(size); + vec2[0] = 5; + vec2[1] = 7; + + // Act & Assert + EXPECT_FALSE(vec1 == vec2); +} + +TEST(TestTVectorLib, can_compare_with_operator_that_says_that_two_objects_are_not_equal_return_true) { + // Arrange + size_t size = 2; + TVector vec1(size); + vec1[0] = 5; + vec1[1] = 6; + TVector vec2(size); + vec2[0] = 3; + vec2[1] = -4; + + // Act & Assert + EXPECT_TRUE(vec1 != vec2); +} + +TEST(TestTVectorLib, can_compare_with_operator_that_says_that_two_objects_are_not_equal_return_fale) { + // Arrange + size_t size = 2; + TVector vec1(size); + vec1[0] = 5; + vec1[1] = 6; + TVector vec2(size); + vec2[0] = 5; + vec2[1] = 6; + + // Act & Assert + EXPECT_FALSE(vec1 != vec2); } \ No newline at end of file From c0d01d67a8b77a33260d26a9845a0224131ddbd8 Mon Sep 17 00:00:00 2001 From: Kate-Nits Date: Fri, 26 Sep 2025 01:39:37 +0300 Subject: [PATCH 24/94] Added test to check operator = --- tests/test_tvector.cpp | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/tests/test_tvector.cpp b/tests/test_tvector.cpp index 630baa4f..c3b6041d 100644 --- a/tests/test_tvector.cpp +++ b/tests/test_tvector.cpp @@ -226,6 +226,24 @@ TEST(TestTVectorLib, test_assign) { EXPECT_EQ(3, vec2[2]); } +TEST(TestTVectorLib, test_operator_equal) { + // Arrange + size_t size = 3; + TVector vec1(size); + vec1[0] = 100; + vec1[1] = 200; + vec1[2] = 369; + + // Act + TVector vec2; + vec2 = vec1; + + // Assert + EXPECT_EQ(100, vec2[0]); + EXPECT_EQ(200, vec2[1]); + EXPECT_EQ(369, vec2[2]); +} + TEST(TestTVectorLib, can_compare_with_operator_two_equal_object) { // Arrange size_t size = 2; From d16d4019725afd017650bdbcaad881988cce7493 Mon Sep 17 00:00:00 2001 From: Kate-Nits Date: Fri, 26 Sep 2025 01:50:11 +0300 Subject: [PATCH 25/94] Added test to check clear --- tests/test_tvector.cpp | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/tests/test_tvector.cpp b/tests/test_tvector.cpp index c3b6041d..04cc296c 100644 --- a/tests/test_tvector.cpp +++ b/tests/test_tvector.cpp @@ -244,6 +244,21 @@ TEST(TestTVectorLib, test_operator_equal) { EXPECT_EQ(369, vec2[2]); } +TEST(TestTVectorLib, test_clear) { + // Arrange + size_t size = 5; + TVector vec(size); + const State* vec_states = vec.states(); + + // Act + vec.clear(); + + // Assert + for (size_t i = 0; i < size; ++i) { EXPECT_EQ(State::empty, vec_states[i]); } + EXPECT_EQ(0, vec.size()); + EXPECT_EQ(0, vec.deleted()); +} + TEST(TestTVectorLib, can_compare_with_operator_two_equal_object) { // Arrange size_t size = 2; From d313ce31aaeec9352517171c9b4747f11a482f7c Mon Sep 17 00:00:00 2001 From: Kate-Nits Date: Fri, 26 Sep 2025 02:03:46 +0300 Subject: [PATCH 26/94] Added test to check resize --- tests/test_tvector.cpp | 45 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/tests/test_tvector.cpp b/tests/test_tvector.cpp index 04cc296c..92b772d9 100644 --- a/tests/test_tvector.cpp +++ b/tests/test_tvector.cpp @@ -313,4 +313,49 @@ TEST(TestTVectorLib, can_compare_with_operator_that_says_that_two_objects_are_no // Act & Assert EXPECT_FALSE(vec1 != vec2); +} + +TEST(TestTVectorLib, test_reserve_new_capacity_more_than_old_capacity) { + // Arrange + TVector vec(3); + size_t old_capacity = vec.capacity(); + size_t new_capacity = old_capacity + 10; + + // Act + vec.reserve(new_capacity); + + // Assert + EXPECT_TRUE(vec.capacity() >= new_capacity); +} + +TEST(TestTVectorLib, test_reserve_no_change_if_new_capacity_less_than_old_capacity) { + // Arrange + TVector vec(4); + size_t old_capacity = vec.capacity(); + + // Act + vec.reserve(old_capacity - 1); + + // Assert + EXPECT_TRUE(vec.capacity() == old_capacity); +} + +TEST(TestTVectorLib, test_resize) { + // Arrange + TVector vec(3); + vec[0] = 10; + vec[1] = 20; + vec[2] = 30; + + // Act + vec.resize(5); + const State* states = vec.states(); + + // Assert + EXPECT_TRUE(5, vec.size()); + EXPECT_TRUE(10, vec[0]); + EXPECT_TRUE(20, vec[1]); + EXPECT_TRUE(30, vec[2]); + EXPECT_TRUE(busy, states[3]); + EXPECT_TRUE(busy, states[4]); } \ No newline at end of file From da11f1ec8257b1458c0b5aa3c585fa3802156a85 Mon Sep 17 00:00:00 2001 From: Kate-Nits Date: Fri, 26 Sep 2025 02:09:05 +0300 Subject: [PATCH 27/94] Added test to check is_empty --- tests/test_tvector.cpp | 31 ++++++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/tests/test_tvector.cpp b/tests/test_tvector.cpp index 92b772d9..60d7e28a 100644 --- a/tests/test_tvector.cpp +++ b/tests/test_tvector.cpp @@ -208,6 +208,22 @@ TEST(TestTVectorLib, test_back_with_deleted_element) { EXPECT_EQ(234, vec.back()); } +TEST(TestTVectorLib, test_is_empty_real_empty_vec) { + // Arrange + TVector vec; + + // Act & Assert + EXPECT_TRUE(vec.is_empty()); +} + +TEST(TestTVectorLib, test_is_empty_not_empty_vec) { + // Arrange + TVector vec(3); + + // Act & Assert + EXPECT_FALSE(vec.is_empty()); +} + TEST(TestTVectorLib, test_assign) { // Arrange size_t size = 3; @@ -352,10 +368,11 @@ TEST(TestTVectorLib, test_resize) { const State* states = vec.states(); // Assert - EXPECT_TRUE(5, vec.size()); - EXPECT_TRUE(10, vec[0]); - EXPECT_TRUE(20, vec[1]); - EXPECT_TRUE(30, vec[2]); - EXPECT_TRUE(busy, states[3]); - EXPECT_TRUE(busy, states[4]); -} \ No newline at end of file + EXPECT_EQ(5, vec.size()); + EXPECT_EQ(10, vec[0]); + EXPECT_EQ(20, vec[1]); + EXPECT_EQ(30, vec[2]); + EXPECT_EQ(busy, states[3]); + EXPECT_EQ(busy, states[4]); +} + From aedd58ace54e10022ea5f0db25cadbd046327fbe Mon Sep 17 00:00:00 2001 From: Kate-Nits Date: Fri, 26 Sep 2025 02:15:27 +0300 Subject: [PATCH 28/94] Added test to check push_front --- tests/test_tvector.cpp | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/tests/test_tvector.cpp b/tests/test_tvector.cpp index 60d7e28a..203f9d45 100644 --- a/tests/test_tvector.cpp +++ b/tests/test_tvector.cpp @@ -224,6 +224,25 @@ TEST(TestTVectorLib, test_is_empty_not_empty_vec) { EXPECT_FALSE(vec.is_empty()); } +TEST(TestTVectorLib, test_push_front) { + // Arrange + TVector vec(3); + vec[0] = 1; + vec[1] = 2; + vec[2] = 3; + const State* states_vec = vec.states(); + + // Act + vec.push_front(777); + + // Assert + EXPECT_EQ(4, vec.size()); + EXPECT_EQ(3 + vec.RESERVE_MEMORY, vec.capacity()); + EXPECT_TRUE(states_vec[3] == State::busy); + EXPECT_EQ(777, vec[0]); + EXPECT_EQ(3, vec[3]); +} + TEST(TestTVectorLib, test_assign) { // Arrange size_t size = 3; @@ -362,10 +381,10 @@ TEST(TestTVectorLib, test_resize) { vec[0] = 10; vec[1] = 20; vec[2] = 30; + const State* states = vec.states(); // Act vec.resize(5); - const State* states = vec.states(); // Assert EXPECT_EQ(5, vec.size()); From 7947bb7863dc7e3c8229270df1bf1b22158ca91d Mon Sep 17 00:00:00 2001 From: Kate-Nits Date: Fri, 26 Sep 2025 02:18:47 +0300 Subject: [PATCH 29/94] Added test to check push_back --- tests/test_tvector.cpp | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/tests/test_tvector.cpp b/tests/test_tvector.cpp index 203f9d45..546f62b5 100644 --- a/tests/test_tvector.cpp +++ b/tests/test_tvector.cpp @@ -243,6 +243,25 @@ TEST(TestTVectorLib, test_push_front) { EXPECT_EQ(3, vec[3]); } +TEST(TestTVectorLib, test_push_back) { + // Arrange + TVector vec(3); + vec[0] = 1; + vec[1] = 2; + vec[2] = 3; + const State* states_vec = vec.states(); + + // Act + vec.push_back(42); + + // Assert + EXPECT_EQ(4, vec.size()); + EXPECT_EQ(3 + vec.RESERVE_MEMORY, vec.capacity()); + EXPECT_TRUE(states_vec[3] == State::busy); + EXPECT_EQ(1, vec[0]); + EXPECT_EQ(42, vec[3]); +} + TEST(TestTVectorLib, test_assign) { // Arrange size_t size = 3; From 06583653da6144365131254314bb82295f2730aa Mon Sep 17 00:00:00 2001 From: Kate-Nits Date: Fri, 26 Sep 2025 02:31:41 +0300 Subject: [PATCH 30/94] Added test to check insert --- tests/test_tvector.cpp | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/tests/test_tvector.cpp b/tests/test_tvector.cpp index 546f62b5..ec3a3000 100644 --- a/tests/test_tvector.cpp +++ b/tests/test_tvector.cpp @@ -262,6 +262,43 @@ TEST(TestTVectorLib, test_push_back) { EXPECT_EQ(42, vec[3]); } +TEST(TestTVectorLib, test_insert_without_deleted_elements) { + // Arrange + TVector vec(3); + vec[0] = 1; + vec[1] = 2; + vec[2] = 3; + + // Act + vec.insert(1, 66); + + // Assert + EXPECT_EQ(4, vec.size()); + EXPECT_EQ(1, vec[0]); + EXPECT_EQ(66, vec[1]); + EXPECT_EQ(2, vec[2]); + EXPECT_EQ(3, vec[3]); +} + +TEST(TestTVectorLib, test_insert_with_deleted_elements) { + // Arrange + size_t size = 5; + int arr[5] = { 1, 6, 4, 5, 7 }; + TVector vec(arr, size); + + // Act + vec.erase(1); + vec.insert(1, 667); + + // Assert + EXPECT_EQ(5, vec.size()); + EXPECT_EQ(1, vec.at(0)); + EXPECT_EQ(667, vec.at(1)); + EXPECT_EQ(4, vec.at(2)); + EXPECT_EQ(5, vec.at(3)); + EXPECT_EQ(7, vec.at(4)); +} + TEST(TestTVectorLib, test_assign) { // Arrange size_t size = 3; From fbc9e0b02696839269066ec443951a4a80e0064a Mon Sep 17 00:00:00 2001 From: Kate-Nits Date: Fri, 26 Sep 2025 19:33:50 +0300 Subject: [PATCH 31/94] Added test to check pop_front --- tests/test_tvector.cpp | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tests/test_tvector.cpp b/tests/test_tvector.cpp index ec3a3000..84da723b 100644 --- a/tests/test_tvector.cpp +++ b/tests/test_tvector.cpp @@ -299,6 +299,20 @@ TEST(TestTVectorLib, test_insert_with_deleted_elements) { EXPECT_EQ(7, vec.at(4)); } +TEST(TestTVectorLib, test_pop_front) { + // Arrange + size_t size = 7; + int arr[7] = { 10, 20, 30, 10, 20, 30, 10 }; + TVector vec(arr, size); + const State* states_vec = vec.states(); + + // Act + vec.pop_front(); + + // Assert + EXPECT_TRUE(states_vec[0] == State::deleted && vec.size() == 7 && vec.deleted() == 1); +} + TEST(TestTVectorLib, test_assign) { // Arrange size_t size = 3; From c2949067aad3791d909ed6bc036ff6cdedfae0e6 Mon Sep 17 00:00:00 2001 From: Kate-Nits Date: Fri, 26 Sep 2025 19:41:33 +0300 Subject: [PATCH 32/94] Added test to check pop_back --- tests/test_tvector.cpp | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/tests/test_tvector.cpp b/tests/test_tvector.cpp index 84da723b..91ee9199 100644 --- a/tests/test_tvector.cpp +++ b/tests/test_tvector.cpp @@ -313,6 +313,19 @@ TEST(TestTVectorLib, test_pop_front) { EXPECT_TRUE(states_vec[0] == State::deleted && vec.size() == 7 && vec.deleted() == 1); } +TEST(TestTVectorLib, test_pop_back) { + // Arrange + TVector vec(3); + vec[2] = 99; + const State* states_vec = vec.states(); + + // Act + vec.pop_back(); + + // Assert + EXPECT_TRUE(states_vec[2] == State::empty && vec.size() == 2); +} + TEST(TestTVectorLib, test_assign) { // Arrange size_t size = 3; From 8a9b3c25930c0b810cdd9e7740a6e1561cb8914c Mon Sep 17 00:00:00 2001 From: Kate-Nits Date: Fri, 26 Sep 2025 19:44:51 +0300 Subject: [PATCH 33/94] Added test to check erase --- tests/test_tvector.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tests/test_tvector.cpp b/tests/test_tvector.cpp index 91ee9199..f5f6968c 100644 --- a/tests/test_tvector.cpp +++ b/tests/test_tvector.cpp @@ -326,6 +326,18 @@ TEST(TestTVectorLib, test_pop_back) { EXPECT_TRUE(states_vec[2] == State::empty && vec.size() == 2); } +TEST(TestTVectorLib, test_erase) { + // Arrange + TVector vec(9); + const State* states_vec = vec.states(); + + // Act + vec.erase(1); + + // Assert + EXPECT_TRUE(states_vec[1] == State::deleted); +} + TEST(TestTVectorLib, test_assign) { // Arrange size_t size = 3; From abc50a80feea34e24671a4a55121b52201ea75b3 Mon Sep 17 00:00:00 2001 From: Kate-Nits Date: Fri, 26 Sep 2025 19:49:38 +0300 Subject: [PATCH 34/94] Added test to check emplace --- tests/test_tvector.cpp | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/tests/test_tvector.cpp b/tests/test_tvector.cpp index f5f6968c..f3c5b618 100644 --- a/tests/test_tvector.cpp +++ b/tests/test_tvector.cpp @@ -338,6 +338,17 @@ TEST(TestTVectorLib, test_erase) { EXPECT_TRUE(states_vec[1] == State::deleted); } +TEST(TestTVectorLib, test_emplace) { + // Arrange + TVector vec(3); + + // Act + vec.emplace(0, std::string("hello")); + + // Assert + EXPECT_EQ("hello", vec[0]); +} + TEST(TestTVectorLib, test_assign) { // Arrange size_t size = 3; From 4b540ab3569b201f8313c86c4b7d45071475d43a Mon Sep 17 00:00:00 2001 From: Kate-Nits Date: Fri, 26 Sep 2025 20:00:59 +0300 Subject: [PATCH 35/94] Added test to check pop_back where vec has size equal 0 --- tests/test_tvector.cpp | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/tests/test_tvector.cpp b/tests/test_tvector.cpp index f3c5b618..b9833149 100644 --- a/tests/test_tvector.cpp +++ b/tests/test_tvector.cpp @@ -313,7 +313,7 @@ TEST(TestTVectorLib, test_pop_front) { EXPECT_TRUE(states_vec[0] == State::deleted && vec.size() == 7 && vec.deleted() == 1); } -TEST(TestTVectorLib, test_pop_back) { +TEST(TestTVectorLib, test_pop_back_checking_without_difficulties) { // Arrange TVector vec(3); vec[2] = 99; @@ -326,6 +326,14 @@ TEST(TestTVectorLib, test_pop_back) { EXPECT_TRUE(states_vec[2] == State::empty && vec.size() == 2); } +TEST(TestTVectorLib, test_pop_back_size_equal_0) { + // Arrange + TVector vec; + + // Act & Assert + ASSERT_ANY_THROW(vec.pop_back()); +} + TEST(TestTVectorLib, test_erase) { // Arrange TVector vec(9); From 21471e2c22874b01544ef2db42d1bcf4879b0f60 Mon Sep 17 00:00:00 2001 From: Kate-Nits Date: Fri, 26 Sep 2025 20:08:05 +0300 Subject: [PATCH 36/94] Added test to check shuffle --- tests/test_tvector.cpp | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/tests/test_tvector.cpp b/tests/test_tvector.cpp index b9833149..134ad239 100644 --- a/tests/test_tvector.cpp +++ b/tests/test_tvector.cpp @@ -509,3 +509,16 @@ TEST(TestTVectorLib, test_resize) { EXPECT_EQ(busy, states[4]); } +TEST(TestTVectorLib, test_shuffle) { + // Arrange + size_t size = 5; + TVector vec(size); + for (int i = 0; i < size; ++i) { vec[i] = i; } + TVector old_vec(vec); + + // Act + shuffle(vec); + + // Assert + EXPECT_TRUE(old_vec != vec); +} \ No newline at end of file From e3a7c92703899fa799e2f4ab02b1c28ae3157390 Mon Sep 17 00:00:00 2001 From: Kate-Nits Date: Fri, 26 Sep 2025 20:13:38 +0300 Subject: [PATCH 37/94] Added test to check quick_sort --- tests/test_tvector.cpp | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/tests/test_tvector.cpp b/tests/test_tvector.cpp index 134ad239..20cc9360 100644 --- a/tests/test_tvector.cpp +++ b/tests/test_tvector.cpp @@ -521,4 +521,19 @@ TEST(TestTVectorLib, test_shuffle) { // Assert EXPECT_TRUE(old_vec != vec); +} + +TEST(TestTVectorLib, test_quick_sort) { + // Arrange + size_t size = 5; + int arr_not_sort[5] = { 5, 3, 4, 1, 2 }; + int arr_sort[5] = { 1, 2, 3, 4, 5 }; + TVector not_sort_vec(arr_not_sort, size); + TVector sort_vec(arr_sort, size); + + // Act + quick_sort(not_sort_vec); + + // Assert + EXPECT_TRUE(sort_vec == not_sort_vec); } \ No newline at end of file From db4ebb0d9fc9a9dd8560aa764d4a1d45f1ba6d11 Mon Sep 17 00:00:00 2001 From: Kate-Nits Date: Fri, 26 Sep 2025 20:20:12 +0300 Subject: [PATCH 38/94] Added test to check find_first and find_last --- tests/test_tvector.cpp | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/tests/test_tvector.cpp b/tests/test_tvector.cpp index 20cc9360..59ed252d 100644 --- a/tests/test_tvector.cpp +++ b/tests/test_tvector.cpp @@ -536,4 +536,24 @@ TEST(TestTVectorLib, test_quick_sort) { // Assert EXPECT_TRUE(sort_vec == not_sort_vec); +} + +TEST(TestTVectorLib, test_find_first) { + // Arrange + size_t size = 5; + int arr[5] = { 7, 3, 5, 3, 9 }; + TVector vec(arr, size); + + // Act & Assert + EXPECT_EQ(1, find_first(vec, 3)); +} + +TEST(TestTVectorLib, test_find_last) { + // Arrange + size_t size = 5; + int arr[5] = { 7, 3, 5, 3, 9 }; + TVector vec(arr, size); + + // Act & Assert + EXPECT_EQ(3, find_last(vec, 3)); } \ No newline at end of file From e901865201967962890a4d2d2988cf400fafe095 Mon Sep 17 00:00:00 2001 From: Kate-Nits Date: Fri, 26 Sep 2025 20:53:46 +0300 Subject: [PATCH 39/94] Added test to check find_all --- tests/test_tvector.cpp | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tests/test_tvector.cpp b/tests/test_tvector.cpp index 59ed252d..90b2f97f 100644 --- a/tests/test_tvector.cpp +++ b/tests/test_tvector.cpp @@ -556,4 +556,18 @@ TEST(TestTVectorLib, test_find_last) { // Act & Assert EXPECT_EQ(3, find_last(vec, 3)); +} + +TEST(TestTVectorLib, test_find_all) { + // Arrange + size_t size = 6; + int arr[6] = { 1, 2, 3, 2, 5, 2 }; + TVector vec(arr, size); + + // Act + size_t* result = find_all(vec, 2); + + // Assert + EXPECT_TRUE(result[0] == 1 && result[1] == 3 && result[2] == 5); + delete[] result; // ???? } \ No newline at end of file From 8fbbd61da3844c3e661e9df799ca03a4e172d15a Mon Sep 17 00:00:00 2001 From: Kate-Nits Date: Fri, 26 Sep 2025 21:02:43 +0300 Subject: [PATCH 40/94] Added test to check push_front with reserve --- tests/test_tvector.cpp | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/tests/test_tvector.cpp b/tests/test_tvector.cpp index 90b2f97f..10590250 100644 --- a/tests/test_tvector.cpp +++ b/tests/test_tvector.cpp @@ -224,7 +224,7 @@ TEST(TestTVectorLib, test_is_empty_not_empty_vec) { EXPECT_FALSE(vec.is_empty()); } -TEST(TestTVectorLib, test_push_front) { +TEST(TestTVectorLib, test_push_front_checking_without_difficulties) { // Arrange TVector vec(3); vec[0] = 1; @@ -243,6 +243,21 @@ TEST(TestTVectorLib, test_push_front) { EXPECT_EQ(3, vec[3]); } +TEST(TestTVectorLib, test_push_front_with_reserve) { + // Arrange + TVector vec(1); + size_t vec_capacity = vec.capacity(); + for (size_t i = 0; i < vec_capacity; ++i) { // capacity + vec.push_back(1); + } + + // Act + vec.push_front(0); // reserve() + + // Assert + EXPECT_TRUE(vec[0] == 0 && vec_capacity <= vec.capacity()); +} + TEST(TestTVectorLib, test_push_back) { // Arrange TVector vec(3); From d78f5f6e2cef207b930e2138c538f0c9a4ff463a Mon Sep 17 00:00:00 2001 From: Kate-Nits Date: Fri, 26 Sep 2025 21:07:03 +0300 Subject: [PATCH 41/94] Added test to check push_back with reserve --- tests/test_tvector.cpp | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/tests/test_tvector.cpp b/tests/test_tvector.cpp index 10590250..ccbcbdbe 100644 --- a/tests/test_tvector.cpp +++ b/tests/test_tvector.cpp @@ -258,7 +258,7 @@ TEST(TestTVectorLib, test_push_front_with_reserve) { EXPECT_TRUE(vec[0] == 0 && vec_capacity <= vec.capacity()); } -TEST(TestTVectorLib, test_push_back) { +TEST(TestTVectorLib, test_push_back_checking_without_difficulties) { // Arrange TVector vec(3); vec[0] = 1; @@ -277,6 +277,21 @@ TEST(TestTVectorLib, test_push_back) { EXPECT_EQ(42, vec[3]); } +TEST(TestTVectorLib, test_push_back_with_reserve) { + // Arrange + TVector vec(1); + size_t vec_capacity1 = vec.capacity(); + for (size_t i = 0; i < vec_capacity1; ++i) { // capacity + vec.push_back(1); + } + + // Act + vec.push_back(0); // reserve() + + // Assert + EXPECT_TRUE(vec[vec.size() - 1] == 0 && vec_capacity1 <= vec.capacity()); +} + TEST(TestTVectorLib, test_insert_without_deleted_elements) { // Arrange TVector vec(3); From 3212f5d71e328c82060c9ae14e11e645da979ffe Mon Sep 17 00:00:00 2001 From: Kate-Nits Date: Fri, 26 Sep 2025 21:29:04 +0300 Subject: [PATCH 42/94] Added test to check insert with reserve and throw out of range --- tests/test_tvector.cpp | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/tests/test_tvector.cpp b/tests/test_tvector.cpp index ccbcbdbe..92e28b22 100644 --- a/tests/test_tvector.cpp +++ b/tests/test_tvector.cpp @@ -329,6 +329,32 @@ TEST(TestTVectorLib, test_insert_with_deleted_elements) { EXPECT_EQ(7, vec.at(4)); } +TEST(TestTVectorLib, test_insert_with_reserve) { + // Arrange + TVector vec(3); + size_t vec_capacity = vec.capacity(); + for (size_t i = 0; i < vec_capacity; ++i) { // a capacity + vec.insert(1, 5); + } + + // Act + vec.insert(1, 5); + + // Assert + EXPECT_TRUE(vec[1] == 5 && vec_capacity <= vec.capacity()); +} + +TEST(TestTVectorLib, test_insert_out_of_range) { + // Arrange + TVector vec(2); + + // Act + vec.insert(1, 66); + + // Assert + ASSERT_ANY_THROW(vec.insert(5, 999)); +} + TEST(TestTVectorLib, test_pop_front) { // Arrange size_t size = 7; From 198eb1c73c8d20f65f9a9d01c2bd09366d8362e1 Mon Sep 17 00:00:00 2001 From: Kate-Nits Date: Fri, 26 Sep 2025 21:49:13 +0300 Subject: [PATCH 43/94] Added test to check pop_front with shrink to fit --- tests/test_tvector.cpp | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/tests/test_tvector.cpp b/tests/test_tvector.cpp index 92e28b22..7b0352ec 100644 --- a/tests/test_tvector.cpp +++ b/tests/test_tvector.cpp @@ -355,7 +355,7 @@ TEST(TestTVectorLib, test_insert_out_of_range) { ASSERT_ANY_THROW(vec.insert(5, 999)); } -TEST(TestTVectorLib, test_pop_front) { +TEST(TestTVectorLib, test_pop_front_checking_without_difficulties) { // Arrange size_t size = 7; int arr[7] = { 10, 20, 30, 10, 20, 30, 10 }; @@ -369,6 +369,19 @@ TEST(TestTVectorLib, test_pop_front) { EXPECT_TRUE(states_vec[0] == State::deleted && vec.size() == 7 && vec.deleted() == 1); } +TEST(TestTVectorLib, test_pop_front_with_shrink_to_fit) { + // Arrange + TVector vec(6); + for (size_t i = 0; i < 1; ++i) { + vec.erase(i); // deleted 1 + } + // Act + vec.pop_front(); + + // Assert + EXPECT_EQ(0, vec.deleted()); +} + TEST(TestTVectorLib, test_pop_back_checking_without_difficulties) { // Arrange TVector vec(3); From ce59712b8b4696ba421ab4705d650ba57310c57e Mon Sep 17 00:00:00 2001 From: Kate-Nits Date: Fri, 26 Sep 2025 22:04:57 +0300 Subject: [PATCH 44/94] Added test to check erase with shrink to fit --- tests/test_tvector.cpp | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/tests/test_tvector.cpp b/tests/test_tvector.cpp index 7b0352ec..9982615b 100644 --- a/tests/test_tvector.cpp +++ b/tests/test_tvector.cpp @@ -403,7 +403,7 @@ TEST(TestTVectorLib, test_pop_back_size_equal_0) { ASSERT_ANY_THROW(vec.pop_back()); } -TEST(TestTVectorLib, test_erase) { +TEST(TestTVectorLib, test_erase_checking_without_difficulties) { // Arrange TVector vec(9); const State* states_vec = vec.states(); @@ -415,6 +415,18 @@ TEST(TestTVectorLib, test_erase) { EXPECT_TRUE(states_vec[1] == State::deleted); } +TEST(TestTVectorLib, test_erase_with_shrink_to_fit) { + // Arrange + TVector vec(20); + + // Act + for (int i = 0; i < 4; ++i) { vec.erase(i); } + + // Assert + EXPECT_EQ(0, vec.deleted()); + EXPECT_EQ(16, vec.size()); +} + TEST(TestTVectorLib, test_emplace) { // Arrange TVector vec(3); From 1ee5f93d8e740ce5a8e8243431ace2f96e43d4be Mon Sep 17 00:00:00 2001 From: Kate-Nits Date: Sat, 27 Sep 2025 12:52:33 +0300 Subject: [PATCH 45/94] I have finished all the tests. Added tests for checking test data, capacity, size, deleted, operator equal for empty vectors, shrink_to_fit --- lib_tvector/tvector.h | 4 +- tests/test_tvector.cpp | 89 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 90 insertions(+), 3 deletions(-) diff --git a/lib_tvector/tvector.h b/lib_tvector/tvector.h index 80e28b18..b8aac37b 100644 --- a/lib_tvector/tvector.h +++ b/lib_tvector/tvector.h @@ -491,8 +491,6 @@ class TVector { inline bool is_full() const noexcept { // функция проверки на заполненость return _size >= _capacity; } - template - friend class TVectorTester; }; template @@ -596,4 +594,4 @@ std::ostream& operator<<(std::ostream& out, const TVector& vec) { return out; } -#endif // LIB_TVECTOR +#endif // LIB_TVECTOR \ No newline at end of file diff --git a/tests/test_tvector.cpp b/tests/test_tvector.cpp index 9982615b..5835229c 100644 --- a/tests/test_tvector.cpp +++ b/tests/test_tvector.cpp @@ -111,6 +111,18 @@ TEST(TestTVectorLib, test_at_checking_with_pop_front) { EXPECT_EQ(1, vec.at(0)); } +TEST(TestTVectorLib, test_data_with_index) { + // Arrange + int arr[3] = { 11, 22, 33 }; + TVector vec(arr, 3); + + // Act & Assert + EXPECT_EQ(11, vec.data(0)); + EXPECT_EQ(22, vec.data(1)); + EXPECT_EQ(33, vec.data(2)); + ASSERT_ANY_THROW(vec.data(10)); // +} + TEST(TestTVectorLib, test_state_state_deleted) { // Arrange size_t size = 8; @@ -144,6 +156,45 @@ TEST(TestTVectorLib, test_state_state_empty) { EXPECT_EQ(State::empty, vec.state(size+1)); } +TEST(TestTVectorLib, test_size_setter_increase) { // + // Arrange + TVector vec(2); + + // Act + vec.size(5); + + // Assert + EXPECT_EQ(5, vec.size()); + for (size_t i = 2; i < 5; i++) { + EXPECT_EQ(State::busy, vec.state(i)); + } +} + +TEST(TestTVectorLib, test_size_setter_decrease) { + // Arrange + TVector vec(5); + vec[0] = 99; + + // Act + vec.size(2); + + // Assert + EXPECT_EQ(2, vec.size()); + EXPECT_EQ(99, vec[0]); // , +} + +TEST(TestTVectorLib, test_capacity_setter) { + // Arrange + TVector vec(2); + size_t old_capacity = vec.capacity(); + + // Act + vec.capacity(old_capacity + 20); + + // Assert + EXPECT_TRUE(vec.capacity() >= old_capacity + 20); +} + TEST(TestTVectorLib, test_begin_and_end) { // Arrange size_t size = 3; @@ -162,6 +213,18 @@ TEST(TestTVectorLib, test_begin_and_end) { EXPECT_EQ(11+22+37, sum); } +TEST(TestTVectorLib, test_deleted_counter) { + // Arrange + TVector vec(35); + + // Act + vec.erase(1); + vec.erase(2); + + // Assert + EXPECT_EQ(2, vec.deleted()); +} + TEST(TestTVectorLib, test_front_without_deleted_elements) { // Arrange size_t size = 3; @@ -517,6 +580,15 @@ TEST(TestTVectorLib, can_compare_with_operator_two_not_equal_object) { EXPECT_FALSE(vec1 == vec2); } +TEST(TestTVectorLib, test_operator_equal_for_empty_vectors) { + // Arrange + TVector vec1; + TVector vec2; + + // Act & Assert + EXPECT_TRUE(vec1 == vec2); +} + TEST(TestTVectorLib, can_compare_with_operator_that_says_that_two_objects_are_not_equal_return_true) { // Arrange size_t size = 2; @@ -590,6 +662,23 @@ TEST(TestTVectorLib, test_resize) { EXPECT_EQ(busy, states[4]); } +TEST(TestTVectorLib, test_shrink_to_fit) { + // Arrange + size_t size = 5; + int arr[5] = { 10, 20, 30, 40, 50 }; + TVector vec(arr, size); + size_t old_capacity = vec.capacity(); + + // Act + vec.erase(1); + vec.erase(3); + vec.shrink_to_fit(); + + // Assert + EXPECT_TRUE(vec.capacity() < old_capacity); + EXPECT_EQ(3, vec.size()); +} + TEST(TestTVectorLib, test_shuffle) { // Arrange size_t size = 5; From a3e288f93b672de1737d7edd85d883b00c0937f2 Mon Sep 17 00:00:00 2001 From: Kate-Nits Date: Sat, 27 Sep 2025 16:10:05 +0300 Subject: [PATCH 46/94] Added constructions for class MathVector --- lib_mathvector/MathVector.h | 26 ++++++++++++++++++-------- lib_tvector/tvector.h | 2 +- tests/test_mathvector.cpp | 8 ++++++++ 3 files changed, 27 insertions(+), 9 deletions(-) diff --git a/lib_mathvector/MathVector.h b/lib_mathvector/MathVector.h index 8463f4da..481bda56 100644 --- a/lib_mathvector/MathVector.h +++ b/lib_mathvector/MathVector.h @@ -3,9 +3,7 @@ #ifndef LIB_MATHVECTOR_MATHVECTOR_H #define LIB_MATHVECTOR_MATHVECTOR_H -#include -#include -#include +#include // {} #include "../lib_tvector/tvector.h" #include "../lib_algorithms/algorithms.h" @@ -13,11 +11,23 @@ template class MathVector : public TVector { public: - MathVector() noexcept : TVector() {} - MathVector(size_t size) : TVector(size) {} - MathVector(const MathVector& other) = default; - MathVector(MathVector&& other) noexcept = default; - ~MathVector() = default; + using TVector::TVector; // TVector + MathVector(const std::initializer_list list) { + this->_size = list.size; + this->_capacity = this->size + TVector::RESERVE_MEMORY; + this->_deleted = 0; + this->_data = new T[this->_capacity]; + + size_t i = 0; + for (const auto& value : list) { //auto list + this->_data[i] = value; + this->_states[i] = State::busy; + ++i; + } + for (; i < this->_capacity; ++i) { + this->_states[i] = State::empty; + } + } MathVector& operator=(const MathVector& other) noexcept { this->assign(other); diff --git a/lib_tvector/tvector.h b/lib_tvector/tvector.h index b8aac37b..72081897 100644 --- a/lib_tvector/tvector.h +++ b/lib_tvector/tvector.h @@ -10,7 +10,7 @@ enum State { empty, busy, deleted }; template class TVector { - +protected: T* _data; State* _states; size_t _size; diff --git a/tests/test_mathvector.cpp b/tests/test_mathvector.cpp index e69de29b..06e389e7 100644 --- a/tests/test_mathvector.cpp +++ b/tests/test_mathvector.cpp @@ -0,0 +1,8 @@ +// Copyright 2025 Ekaterina Ushnitskaya + +#include +#include "../lib_mathvector/MathVector.h" + +#define EPSILON 0.000001 +#define TRUE 1 +#define FALSE 0 From 07359635b1ffac3c39fcb88c25ebc11390cce3ec Mon Sep 17 00:00:00 2001 From: Kate-Nits Date: Sat, 27 Sep 2025 18:03:47 +0300 Subject: [PATCH 47/94] Wrote constructions for class MathVector and added tests for these constructions --- lib_algorithms/algorithms.cpp | 22 ++++--- lib_mathvector/MathVector.h | 11 ++-- lib_matrix/matrix.h | 5 +- tests/test_mathvector.cpp | 115 ++++++++++++++++++++++++++++++++++ 4 files changed, 138 insertions(+), 15 deletions(-) diff --git a/lib_algorithms/algorithms.cpp b/lib_algorithms/algorithms.cpp index 18766f70..ffcecd7f 100644 --- a/lib_algorithms/algorithms.cpp +++ b/lib_algorithms/algorithms.cpp @@ -144,7 +144,7 @@ void what_matrix_sizes(size_t& sizeN, size_t& sizeM) { std::cout << "Enter the number of columns (parametr N): "; std::cin >> sizeN; } -void print_res(const TMatrix& matrix, size_t size_M, size_t size_N) { +/*void print_res(const TMatrix& matrix, size_t size_M, size_t size_N) { #ifndef REALISED_OPERATOR[] @@ -158,7 +158,7 @@ void print_res(const TMatrix& matrix, size_t size_M, size_t size_N) { std::cout << std::endl; } #endif // REALISED_OPERATOR[] -} +}*/ void do_user_want_to_save(int& want_to_save) { std::cout << "==================================================" << std::endl; std::cout << "Would you like to save result?" << std::endl; @@ -177,7 +177,7 @@ void do_user_want_to_save(int& want_to_save) { std::cin >> want_to_save; } } -void start_matrix_calculator(int& link_user_choice, int& link_want_to_save, TMatrix res) { +/*void start_matrix_calculator(int& link_user_choice, int& link_want_to_save, TMatrix res) { int isExit = NO; int& link_isExit = isExit; int what_the_first_matrix = 0; @@ -233,10 +233,14 @@ void start_matrix_calculator(int& link_user_choice, int& link_want_to_save, TMat size_resM = sizeM2; } if (what_the_first_matrix == STANDART && what_the_second_matrix == STANDART) { +#ifdef REALISED_CLASSES TMatrix matrix1; TMatrix matrix2; TMatrix intermediate_res; - +#else + //in_development(); +#endif // REALISED_CLASSES + } else if (what_the_first_matrix == TRIANGLE && what_the_second_matrix == TRIANGLE) { #ifdef REALISED_CLASSES @@ -288,7 +292,7 @@ void start_matrix_calculator(int& link_user_choice, int& link_want_to_save, TMat break; } } -} +}*/ void deleted_saved_matrix(int& link_user_choice_for_deleted, int& isThereMatrix1, int& isThereMatrix2, int& isThereMatrix3) { std::cout << "==================================================" << std::endl; std::cout << " You can store a limited number of matrices" << std::endl; @@ -315,8 +319,7 @@ void deleted_saved_matrix(int& link_user_choice_for_deleted, int& isThereMatrix1 break; } } -void viewing_saved_matrices(int count_of_saved_matrices, - TMatrix matrix1, TMatrix matrix2, TMatrix matrix3) { +/*void viewing_saved_matrices(int count_of_saved_matrices, TMatrix matrix1, TMatrix matrix2, TMatrix matrix3) { std::cout << "==================================================" << std::endl; std::cout << " Which matrix would you like to look at?" << std::endl; if (count_of_saved_matrices > 0) { @@ -353,8 +356,9 @@ void viewing_saved_matrices(int count_of_saved_matrices, getchar(); getchar(); system("cls"); -} +}*/ void matrix_application() { + /* int user_choice; int& link_user_choice = user_choice; int isExit = NO; @@ -362,7 +366,6 @@ void matrix_application() { int want_to_save = NO; int& link_want_to_save = want_to_save; int count_of_saved_matrices = 0; - TMatrix res; TMatrix matrix1; TMatrix matrix2; @@ -428,6 +431,7 @@ void matrix_application() { break; } } + */ } diff --git a/lib_mathvector/MathVector.h b/lib_mathvector/MathVector.h index 481bda56..b2c32c26 100644 --- a/lib_mathvector/MathVector.h +++ b/lib_mathvector/MathVector.h @@ -12,20 +12,21 @@ template class MathVector : public TVector { public: using TVector::TVector; // TVector + MathVector(const MathVector& other) : TVector(other) {} MathVector(const std::initializer_list list) { - this->_size = list.size; - this->_capacity = this->size + TVector::RESERVE_MEMORY; + this->_size = list.size(); + this->_capacity = this->_size + TVector::RESERVE_MEMORY; this->_deleted = 0; this->_data = new T[this->_capacity]; - + this->_states = new State[this->_capacity]; size_t i = 0; for (const auto& value : list) { //auto list this->_data[i] = value; - this->_states[i] = State::busy; + this->_states[i] = busy; ++i; } for (; i < this->_capacity; ++i) { - this->_states[i] = State::empty; + this->_states[i] = empty; } } diff --git a/lib_matrix/matrix.h b/lib_matrix/matrix.h index 108b7623..81a5c001 100644 --- a/lib_matrix/matrix.h +++ b/lib_matrix/matrix.h @@ -9,8 +9,9 @@ #include "../lib_algorithms/algorithms.h" #include "../lib_mathvector/MathVector.h" +/* template -class TMatrix : public MathVector< MathVector > { +class TMatrix : public MathVector> { size_t _rows; size_t _cols; public: @@ -92,5 +93,7 @@ std::ostream& operator<<(std::ostream& out, const TMatrix& matrix) { out << ""; return out; } +*/ + #endif // LIB_MATRIX_MATRIX_H diff --git a/tests/test_mathvector.cpp b/tests/test_mathvector.cpp index 06e389e7..6b1c3e7c 100644 --- a/tests/test_mathvector.cpp +++ b/tests/test_mathvector.cpp @@ -6,3 +6,118 @@ #define EPSILON 0.000001 #define TRUE 1 #define FALSE 0 + +TEST(TestMathVectorLib, default_constructor) { + // Arrange & Act + MathVector vec; + + // Assert + EXPECT_EQ(nullptr, vec.data()); // + EXPECT_EQ(nullptr, vec.states()); + EXPECT_EQ(0, vec.size()); + EXPECT_EQ(0, vec.capacity()); + EXPECT_EQ(0, vec.deleted()); + EXPECT_TRUE(vec.is_empty()); +} + +TEST(TestMathVectorLib, constructor_with_size) { + // Arrange () + size_t size = 10; + + // Act + MathVector vec(size); + + // Assert + EXPECT_EQ(size, vec.size()); + EXPECT_EQ(size + MathVector::RESERVE_MEMORY, vec.capacity()); + EXPECT_EQ(0, vec.deleted()); + EXPECT_FALSE(vec.is_empty()); + for (size_t i = 0; i < size; i++) { + EXPECT_EQ(State::busy, vec.state(i)); + } + for (size_t i = size; i < vec.capacity(); i++) { + EXPECT_EQ(State::empty, vec.state(i)); + } +} + +TEST(TestMathVectorLib, constructor_with_mass_all_parameters_are_good) { + // Arrange () + size_t size = 3; + int copy_mass[3] = { 1, 2, 3 }; + + // Act + MathVector vec(copy_mass, size); + + // Assert + EXPECT_EQ(size, vec.size()); + EXPECT_EQ(size + MathVector::RESERVE_MEMORY, vec.capacity()); + EXPECT_EQ(0, vec.deleted()); + EXPECT_FALSE(vec.is_empty()); + EXPECT_EQ(1, vec.data(0)); + EXPECT_EQ(2, vec.data(1)); + EXPECT_EQ(3, vec.data(2)); + for (size_t i = 0; i < size; i++) { + EXPECT_EQ(State::busy, vec.state(i)); + } + for (size_t i = size; i < vec.capacity(); i++) { + EXPECT_EQ(State::empty, vec.state(i)); + } +} + +TEST(TestMathVectorLib, constructor_with_mass_with_nullptr_and_size_gt_zero) { + // Arrange + int* null_data = nullptr; + + // Act & Assert + ASSERT_ANY_THROW(MathVector vec(null_data, 5)); +} + +TEST(TestMathVectorLib, copy_constructor) { + // Arrange + int arr[4] = { 1, 2, 3, 4 }; + MathVector vec1(arr, 4); + + // Act + MathVector vec2(vec1); + + // Assert + EXPECT_EQ(vec1.size(), vec2.size()); + EXPECT_EQ(vec1.capacity(), vec2.capacity()); + EXPECT_EQ(vec1.deleted(), vec2.deleted()); + for (size_t i = 0; i < vec1.size(); i++) { + EXPECT_EQ(vec1.data(i), vec2.data(i)); + EXPECT_EQ(vec1.state(i), vec2.state(i)); + } +} + +TEST(TestMathVectorLib, test_constructor_list_checking_without_problems) { + // Arrange & Act + MathVector vec{ 1, 2, 3 }; + + // Assert + EXPECT_EQ(vec.size(), 3); + EXPECT_EQ(vec[0], 1); + EXPECT_EQ(vec[1], 2); + EXPECT_EQ(vec[2], 3); +} + +TEST(TestMathVectorLib, test_constructor_list_empty_init) { + // Arrange & Act + MathVector vec{}; + + // Assert + EXPECT_EQ(vec.size(), 0); + EXPECT_TRUE(vec.is_empty()); +} + +TEST(TestMathVectorLib, test_constructor_list_double_init) { + // Arrange & Act + MathVector vec{ 1.1, 2.2, 3.3, 4.4 }; + + // Assert + EXPECT_EQ(vec.size(), 4); + EXPECT_EQ(vec[0], 1.1); + EXPECT_EQ(vec[1], 2.2); + EXPECT_EQ(vec[2], 3.3); + EXPECT_EQ(vec[3], 4.4); +} \ No newline at end of file From 62471242b5dcdf1a96f83b71c8360e60e10166dc Mon Sep 17 00:00:00 2001 From: Kate-Nits Date: Sat, 27 Sep 2025 18:48:32 +0300 Subject: [PATCH 48/94] Added tests on class MathVector checking public function from TVector --- lib_mathvector/MathVector.h | 10 - tests/test_mathvector.cpp | 656 +++++++++++++++++++++++++++++++++++- 2 files changed, 655 insertions(+), 11 deletions(-) diff --git a/lib_mathvector/MathVector.h b/lib_mathvector/MathVector.h index b2c32c26..063e5ca4 100644 --- a/lib_mathvector/MathVector.h +++ b/lib_mathvector/MathVector.h @@ -30,16 +30,6 @@ class MathVector : public TVector { } } - MathVector& operator=(const MathVector& other) noexcept { - this->assign(other); - return *this; - } - - MathVector& operator=(MathVector&& other) noexcept { - in_development(); - return *this; - } - T dot(const MathVector& other) const { // in_development(); return T(); diff --git a/tests/test_mathvector.cpp b/tests/test_mathvector.cpp index 6b1c3e7c..f0c8875b 100644 --- a/tests/test_mathvector.cpp +++ b/tests/test_mathvector.cpp @@ -120,4 +120,658 @@ TEST(TestMathVectorLib, test_constructor_list_double_init) { EXPECT_EQ(vec[1], 2.2); EXPECT_EQ(vec[2], 3.3); EXPECT_EQ(vec[3], 4.4); -} \ No newline at end of file +} + +TEST(TestMathVectorLib, test_at_checking_without_difficulties) { + // Arrange + size_t size = 8; + int arr[8] = { 0, 1, 2, 3, 4, 5, 6, 7 }; + MathVector vec(arr, size); + + // Act & Assert + EXPECT_EQ(4, vec.at(4)); +} + +TEST(TestMathVectorLib, test_at_checking_with_pop_front) { + // Arrange + size_t size = 8; + int arr[8] = { 0, 1, 2, 3, 4, 5, 6, 7 }; + MathVector vec(arr, size); + + // Act + vec.pop_front(); + + // Assert + EXPECT_EQ(1, vec.at(0)); +} + +TEST(TestMathVectorLib, test_data_with_index) { + // Arrange + int arr[3] = { 11, 22, 33 }; + MathVector vec(arr, 3); + + // Act & Assert + EXPECT_EQ(11, vec.data(0)); + EXPECT_EQ(22, vec.data(1)); + EXPECT_EQ(33, vec.data(2)); + ASSERT_ANY_THROW(vec.data(10)); // +} + +TEST(TestMathVectorLib, test_state_state_deleted) { + // Arrange + size_t size = 8; + int arr[8] = { 0, 1, 2, 3, 4, 5, 6, 7 }; + MathVector vec(arr, size); + + // Act + vec.pop_front(); + + // Assert + EXPECT_EQ(State::deleted, vec.state(0)); +} + +TEST(TestMathVectorLib, test_state_state_busy) { + // Arrange + size_t size = 8; + int arr[8] = { 0, 1, 2, 3, 4, 5, 6, 7 }; + MathVector vec(arr, size); + + // Act & Assert + EXPECT_EQ(State::busy, vec.state(4)); +} + +TEST(TestMathVectorLib, test_state_state_empty) { + // Arrange + size_t size = 8; + int arr[8] = { 0, 1, 2, 3, 4, 5, 6, 7 }; + MathVector vec(arr, size); + + // Act & Assert + EXPECT_EQ(State::empty, vec.state(size + 1)); +} + +TEST(TestMathVectorLib, test_size_setter_increase) { // + // Arrange + MathVector vec(2); + + // Act + vec.size(5); + + // Assert + EXPECT_EQ(5, vec.size()); + for (size_t i = 2; i < 5; i++) { + EXPECT_EQ(State::busy, vec.state(i)); + } +} + +TEST(TestMathVectorLib, test_size_setter_decrease) { + // Arrange + MathVector vec(5); + vec[0] = 99; + + // Act + vec.size(2); + + // Assert + EXPECT_EQ(2, vec.size()); + EXPECT_EQ(99, vec[0]); // , +} + +TEST(TestMathVectorLib, test_capacity_setter) { + // Arrange + MathVector vec(2); + size_t old_capacity = vec.capacity(); + + // Act + vec.capacity(old_capacity + 20); + + // Assert + EXPECT_TRUE(vec.capacity() >= old_capacity + 20); +} + +TEST(TestMathVectorLib, test_begin_and_end) { + // Arrange + size_t size = 3; + int arr[8] = { 11, 22, 37 }; + MathVector vec(arr, size); + int sum = 0; + + // Act + int* begin = vec.begin(); + int* end = vec.end(); + for (int* p = begin; p < end; ++p) { + sum += *p; + } + + // Assert + EXPECT_EQ(11 + 22 + 37, sum); +} + +TEST(TestMathVectorLib, test_deleted_counter) { + // Arrange + MathVector vec(35); + + // Act + vec.erase(1); + vec.erase(2); + + // Assert + EXPECT_EQ(2, vec.deleted()); +} + +TEST(TestMathVectorLib, test_front_without_deleted_elements) { + // Arrange + size_t size = 3; + int arr[8] = { 112, 234, 345 }; + MathVector vec(arr, size); + + // Act & Assert + EXPECT_EQ(112, vec.front()); +} + +TEST(TestMathVectorLib, test_front_with_deleted_element) { + // Arrange + size_t size = 3; + int arr[8] = { 112, 234, 345 }; + MathVector vec(arr, size); + + // Act + vec.pop_front(); + + // Assert + EXPECT_EQ(234, vec.front()); +} + +TEST(TestMathVectorLib, test_back_without_deleted_elements) { + // Arrange + size_t size = 3; + int arr[8] = { 112, 234, 345 }; + MathVector vec(arr, size); + + // Act & Assert + EXPECT_EQ(345, vec.back()); +} + +TEST(TestMathVectorLib, test_back_with_deleted_element) { + // Arrange + size_t size = 3; + int arr[8] = { 112, 234, 345 }; + MathVector vec(arr, size); + + // Act + vec.pop_back(); + + // Assert + EXPECT_EQ(234, vec.back()); +} + +TEST(TestMathVectorLib, test_is_empty_real_empty_vec) { + // Arrange + MathVector vec; + + // Act & Assert + EXPECT_TRUE(vec.is_empty()); +} + +TEST(TestMathVectorLib, test_is_empty_not_empty_vec) { + // Arrange + MathVector vec(3); + + // Act & Assert + EXPECT_FALSE(vec.is_empty()); +} + +TEST(TestMathVectorLib, test_push_front_checking_without_difficulties) { + // Arrange + MathVector vec(3); + vec[0] = 1; + vec[1] = 2; + vec[2] = 3; + const State* states_vec = vec.states(); + + // Act + vec.push_front(777); + + // Assert + EXPECT_EQ(4, vec.size()); + EXPECT_EQ(3 + vec.RESERVE_MEMORY, vec.capacity()); + EXPECT_TRUE(states_vec[3] == State::busy); + EXPECT_EQ(777, vec[0]); + EXPECT_EQ(3, vec[3]); +} + +TEST(TestMathVectorLib, test_push_front_with_reserve) { + // Arrange + MathVector vec(1); + size_t vec_capacity = vec.capacity(); + for (size_t i = 0; i < vec_capacity; ++i) { // capacity + vec.push_back(1); + } + + // Act + vec.push_front(0); // reserve() + + // Assert + EXPECT_TRUE(vec[0] == 0 && vec_capacity <= vec.capacity()); +} + +TEST(TestMathVectorLib, test_push_back_checking_without_difficulties) { + // Arrange + MathVector vec(3); + vec[0] = 1; + vec[1] = 2; + vec[2] = 3; + const State* states_vec = vec.states(); + + // Act + vec.push_back(42); + + // Assert + EXPECT_EQ(4, vec.size()); + EXPECT_EQ(3 + vec.RESERVE_MEMORY, vec.capacity()); + EXPECT_TRUE(states_vec[3] == State::busy); + EXPECT_EQ(1, vec[0]); + EXPECT_EQ(42, vec[3]); +} + +TEST(TestMathVectorLib, test_push_back_with_reserve) { + // Arrange + MathVector vec(1); + size_t vec_capacity1 = vec.capacity(); + for (size_t i = 0; i < vec_capacity1; ++i) { // capacity + vec.push_back(1); + } + + // Act + vec.push_back(0); // reserve() + + // Assert + EXPECT_TRUE(vec[vec.size() - 1] == 0 && vec_capacity1 <= vec.capacity()); +} + +TEST(TestMathVectorLib, test_insert_without_deleted_elements) { + // Arrange + MathVector vec(3); + vec[0] = 1; + vec[1] = 2; + vec[2] = 3; + + // Act + vec.insert(1, 66); + + // Assert + EXPECT_EQ(4, vec.size()); + EXPECT_EQ(1, vec[0]); + EXPECT_EQ(66, vec[1]); + EXPECT_EQ(2, vec[2]); + EXPECT_EQ(3, vec[3]); +} + +TEST(TestMathVectorLib, test_insert_with_deleted_elements) { + // Arrange + size_t size = 5; + int arr[5] = { 1, 6, 4, 5, 7 }; + MathVector vec(arr, size); + + // Act + vec.erase(1); + vec.insert(1, 667); + + // Assert + EXPECT_EQ(5, vec.size()); + EXPECT_EQ(1, vec.at(0)); + EXPECT_EQ(667, vec.at(1)); + EXPECT_EQ(4, vec.at(2)); + EXPECT_EQ(5, vec.at(3)); + EXPECT_EQ(7, vec.at(4)); +} + +TEST(TestMathVectorLib, test_insert_with_reserve) { + // Arrange + MathVector vec(3); + size_t vec_capacity = vec.capacity(); + for (size_t i = 0; i < vec_capacity; ++i) { // a capacity + vec.insert(1, 5); + } + + // Act + vec.insert(1, 5); + + // Assert + EXPECT_TRUE(vec[1] == 5 && vec_capacity <= vec.capacity()); +} + +TEST(TestMathVectorLib, test_insert_out_of_range) { + // Arrange + MathVector vec(2); + + // Act + vec.insert(1, 66); + + // Assert + ASSERT_ANY_THROW(vec.insert(5, 999)); +} + +TEST(TestMathVectorLib, test_pop_front_checking_without_difficulties) { + // Arrange + size_t size = 7; + int arr[7] = { 10, 20, 30, 10, 20, 30, 10 }; + MathVector vec(arr, size); + const State* states_vec = vec.states(); + + // Act + vec.pop_front(); + + // Assert + EXPECT_TRUE(states_vec[0] == State::deleted && vec.size() == 7 && vec.deleted() == 1); +} + +TEST(TestMathVectorLib, test_pop_front_with_shrink_to_fit) { + // Arrange + MathVector vec(6); + for (size_t i = 0; i < 1; ++i) { + vec.erase(i); // deleted 1 + } + // Act + vec.pop_front(); + + // Assert + EXPECT_EQ(0, vec.deleted()); +} + +TEST(TestMathVectorLib, test_pop_back_checking_without_difficulties) { + // Arrange + MathVector vec(3); + vec[2] = 99; + const State* states_vec = vec.states(); + + // Act + vec.pop_back(); + + // Assert + EXPECT_TRUE(states_vec[2] == State::empty && vec.size() == 2); +} + +TEST(TestMathVectorLib, test_pop_back_size_equal_0) { + // Arrange + MathVector vec; + + // Act & Assert + ASSERT_ANY_THROW(vec.pop_back()); +} + +TEST(TestMathVectorLib, test_erase_checking_without_difficulties) { + // Arrange + MathVector vec(9); + const State* states_vec = vec.states(); + + // Act + vec.erase(1); + + // Assert + EXPECT_TRUE(states_vec[1] == State::deleted); +} + +TEST(TestMathVectorLib, test_erase_with_shrink_to_fit) { + // Arrange + MathVector vec(20); + + // Act + for (int i = 0; i < 4; ++i) { vec.erase(i); } + + // Assert + EXPECT_EQ(0, vec.deleted()); + EXPECT_EQ(16, vec.size()); +} + +TEST(TestMathVectorLib, test_emplace) { + // Arrange + MathVector vec(3); + + // Act + vec.emplace(0, std::string("hello")); + + // Assert + EXPECT_EQ("hello", vec[0]); +} + +TEST(TestMathVectorLib, test_assign) { + // Arrange + size_t size = 3; + MathVector vec1(size); + vec1[0] = 1; + vec1[1] = 2; + vec1[2] = 3; + + // Act + MathVector vec2; + vec2.assign(vec1); + + // Assert + EXPECT_EQ(1, vec2[0]); + EXPECT_EQ(2, vec2[1]); + EXPECT_EQ(3, vec2[2]); +} + +TEST(TestMathVectorLib, test_operator_equal) { + // Arrange + size_t size = 3; + MathVector vec1(size); + vec1[0] = 100; + vec1[1] = 200; + vec1[2] = 369; + + // Act + MathVector vec2; + vec2 = vec1; + + // Assert + EXPECT_EQ(100, vec2[0]); + EXPECT_EQ(200, vec2[1]); + EXPECT_EQ(369, vec2[2]); +} + +TEST(TestMathVectorLib, test_clear) { + // Arrange + size_t size = 5; + MathVector vec(size); + const State* vec_states = vec.states(); + + // Act + vec.clear(); + + // Assert + for (size_t i = 0; i < size; ++i) { EXPECT_EQ(State::empty, vec_states[i]); } + EXPECT_EQ(0, vec.size()); + EXPECT_EQ(0, vec.deleted()); +} + +TEST(TestMathVectorLib, can_compare_with_operator_two_equal_object) { + // Arrange + size_t size = 2; + MathVector vec1(size); + vec1[0] = 5; + vec1[1] = 6; + MathVector vec2(size); + vec2[0] = 5; + vec2[1] = 6; + + // Act & Assert + EXPECT_TRUE(vec1 == vec2); +} + +TEST(TestMathVectorLib, can_compare_with_operator_two_not_equal_object) { + // Arrange + size_t size = 2; + MathVector vec1(size); + vec1[0] = 5; + vec1[1] = 6; + MathVector vec2(size); + vec2[0] = 5; + vec2[1] = 7; + + // Act & Assert + EXPECT_FALSE(vec1 == vec2); +} + +TEST(TestMathVectorLib, test_operator_equal_for_empty_vectors) { + // Arrange + MathVector vec1; + MathVector vec2; + + // Act & Assert + EXPECT_TRUE(vec1 == vec2); +} + +TEST(TestMathVectorLib, can_compare_with_operator_that_says_that_two_objects_are_not_equal_return_true) { + // Arrange + size_t size = 2; + MathVector vec1(size); + vec1[0] = 5; + vec1[1] = 6; + MathVector vec2(size); + vec2[0] = 3; + vec2[1] = -4; + + // Act & Assert + EXPECT_TRUE(vec1 != vec2); +} + +TEST(TestMathVectorLib, can_compare_with_operator_that_says_that_two_objects_are_not_equal_return_fale) { + // Arrange + size_t size = 2; + MathVector vec1(size); + vec1[0] = 5; + vec1[1] = 6; + MathVector vec2(size); + vec2[0] = 5; + vec2[1] = 6; + + // Act & Assert + EXPECT_FALSE(vec1 != vec2); +} + +TEST(TestMathVectorLib, test_reserve_new_capacity_more_than_old_capacity) { + // Arrange + MathVector vec(3); + size_t old_capacity = vec.capacity(); + size_t new_capacity = old_capacity + 10; + + // Act + vec.reserve(new_capacity); + + // Assert + EXPECT_TRUE(vec.capacity() >= new_capacity); +} + +TEST(TestMathVectorLib, test_reserve_no_change_if_new_capacity_less_than_old_capacity) { + // Arrange + MathVector vec(4); + size_t old_capacity = vec.capacity(); + + // Act + vec.reserve(old_capacity - 1); + + // Assert + EXPECT_TRUE(vec.capacity() == old_capacity); +} + +TEST(TestMathVectorLib, test_resize) { + // Arrange + MathVector vec(3); + vec[0] = 10; + vec[1] = 20; + vec[2] = 30; + const State* states = vec.states(); + + // Act + vec.resize(5); + + // Assert + EXPECT_EQ(5, vec.size()); + EXPECT_EQ(10, vec[0]); + EXPECT_EQ(20, vec[1]); + EXPECT_EQ(30, vec[2]); + EXPECT_EQ(busy, states[3]); + EXPECT_EQ(busy, states[4]); +} + +TEST(TestMathVectorLib, test_shrink_to_fit) { + // Arrange + size_t size = 5; + int arr[5] = { 10, 20, 30, 40, 50 }; + MathVector vec(arr, size); + size_t old_capacity = vec.capacity(); + + // Act + vec.erase(1); + vec.erase(3); + vec.shrink_to_fit(); + + // Assert + EXPECT_TRUE(vec.capacity() < old_capacity); + EXPECT_EQ(3, vec.size()); +} + +TEST(TestMathVectorLib, test_shuffle) { + // Arrange + size_t size = 5; + MathVector vec(size); + for (int i = 0; i < size; ++i) { vec[i] = i; } + MathVector old_vec(vec); + + // Act + shuffle(vec); + + // Assert + EXPECT_TRUE(old_vec != vec); +} + +TEST(TestMathVectorLib, test_quick_sort) { + // Arrange + size_t size = 5; + int arr_not_sort[5] = { 5, 3, 4, 1, 2 }; + int arr_sort[5] = { 1, 2, 3, 4, 5 }; + MathVector not_sort_vec(arr_not_sort, size); + MathVector sort_vec(arr_sort, size); + + // Act + quick_sort(not_sort_vec); + + // Assert + EXPECT_TRUE(sort_vec == not_sort_vec); +} + +TEST(TestMathVectorLib, test_find_first) { + // Arrange + size_t size = 5; + int arr[5] = { 7, 3, 5, 3, 9 }; + MathVector vec(arr, size); + + // Act & Assert + EXPECT_EQ(1, find_first(vec, 3)); +} + +TEST(TestMathVectorLib, test_find_last) { + // Arrange + size_t size = 5; + int arr[5] = { 7, 3, 5, 3, 9 }; + MathVector vec(arr, size); + + // Act & Assert + EXPECT_EQ(3, find_last(vec, 3)); +} + +TEST(TestMathVectorLib, test_find_all) { + // Arrange + size_t size = 6; + int arr[6] = { 1, 2, 3, 2, 5, 2 }; + MathVector vec(arr, size); + + // Act + size_t* result = find_all(vec, 2); + + // Assert + EXPECT_TRUE(result[0] == 1 && result[1] == 3 && result[2] == 5); + delete[] result; // ???? +} From 4706571e40beafccb68cc26cd01044cf316d627a Mon Sep 17 00:00:00 2001 From: Kate-Nits Date: Sat, 27 Sep 2025 19:27:15 +0300 Subject: [PATCH 49/94] Added operator + for MathVector and test on it --- lib_mathvector/MathVector.h | 22 +++++++++++++--------- tests/test_mathvector.cpp | 13 +++++++++++++ 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/lib_mathvector/MathVector.h b/lib_mathvector/MathVector.h index 063e5ca4..909bbcba 100644 --- a/lib_mathvector/MathVector.h +++ b/lib_mathvector/MathVector.h @@ -30,15 +30,16 @@ class MathVector : public TVector { } } - T dot(const MathVector& other) const { // - in_development(); - return T(); - } - - MathVector operator+(const MathVector& other) const { - in_development(); - return MathVector(); + if (this->_size != other._size) { + throw std::invalid_argument("Vectors should have same size for addition"); + } + MathVector result(this->_size); + for (size_t i = 0; i < this->_size; ++i) { + result._data[i] = this->_data[i] + other._data[i]; + result._states[i] = busy; + } + return result; } MathVector operator-(const MathVector& other) const { in_development(); @@ -62,7 +63,10 @@ class MathVector : public TVector { in_development(); return *this; } - + T dot(const MathVector& other) const { // + in_development(); + return T(); + } }; diff --git a/tests/test_mathvector.cpp b/tests/test_mathvector.cpp index f0c8875b..78f37c0e 100644 --- a/tests/test_mathvector.cpp +++ b/tests/test_mathvector.cpp @@ -775,3 +775,16 @@ TEST(TestMathVectorLib, test_find_all) { EXPECT_TRUE(result[0] == 1 && result[1] == 3 && result[2] == 5); delete[] result; // ???? } + +TEST(TestMathVectorLib, test_operator_add) { + // Arrange + MathVector vec1{ 1, 5, 3 }; + MathVector vec2{ 1, 5, 3 }; + //Act + MathVector res = vec1 + vec2; + + // Assert + EXPECT_EQ(2, res[0]); + EXPECT_EQ(10, res[1]); + EXPECT_EQ(6, res[2]); +} \ No newline at end of file From dd4d9973cac5edaf526e4d7fc0ca25df2ed89bd0 Mon Sep 17 00:00:00 2001 From: Kate-Nits Date: Sat, 27 Sep 2025 21:12:22 +0300 Subject: [PATCH 50/94] Added operator -, operator * for MathVector and tests on it --- lib_mathvector/MathVector.h | 35 ++++++++++++--- tests/test_mathvector.cpp | 90 ++++++++++++++++++++++++++++++++++++- 2 files changed, 119 insertions(+), 6 deletions(-) diff --git a/lib_mathvector/MathVector.h b/lib_mathvector/MathVector.h index 909bbcba..3c6c7364 100644 --- a/lib_mathvector/MathVector.h +++ b/lib_mathvector/MathVector.h @@ -29,7 +29,7 @@ class MathVector : public TVector { this->_states[i] = empty; } } - + // int double? MathVector operator+(const MathVector& other) const { if (this->_size != other._size) { throw std::invalid_argument("Vectors should have same size for addition"); @@ -42,13 +42,34 @@ class MathVector : public TVector { return result; } MathVector operator-(const MathVector& other) const { - in_development(); - return MathVector(); + if (this->_size != other._size) { + throw std::invalid_argument("Vectors should have same size for subtraction"); + } + MathVector result(this->_size); + for (size_t i = 0; i < this->_size; ++i) { + result._data[i] = this->_data[i] - other._data[i]; + result._states[i] = State::busy; + } + return result; } MathVector operator*(const T& scalar) const { - in_development(); - return MathVector(); + MathVector result(this->_size); + for (size_t i = 0; i < this->_size; ++i) { + result._data[i] = this->_data[i] * scalar; + } + return result; + } + + T operator*(const MathVector& other_vec) const { + if (this->_size != other_vec._size) { + throw std::invalid_argument("Vectors should have same size for dot product"); + } + T result = 0; + for (size_t i = 0; i < this->_size; ++i) { + result += this->_data[i] * other_vec._data[i]; + } + return result; } MathVector& operator+=(const MathVector& other) { @@ -67,6 +88,10 @@ class MathVector : public TVector { in_development(); return T(); } + double norm() const { // + in_development(); + return double; + } }; diff --git a/tests/test_mathvector.cpp b/tests/test_mathvector.cpp index 78f37c0e..f8b9e8b8 100644 --- a/tests/test_mathvector.cpp +++ b/tests/test_mathvector.cpp @@ -776,7 +776,7 @@ TEST(TestMathVectorLib, test_find_all) { delete[] result; // ???? } -TEST(TestMathVectorLib, test_operator_add) { +TEST(TestMathVectorLib, test_operator_add_for_int) { // Arrange MathVector vec1{ 1, 5, 3 }; MathVector vec2{ 1, 5, 3 }; @@ -787,4 +787,92 @@ TEST(TestMathVectorLib, test_operator_add) { EXPECT_EQ(2, res[0]); EXPECT_EQ(10, res[1]); EXPECT_EQ(6, res[2]); +} + +TEST(TestMathVectorLib, test_operator_add_for_double) { + // Arrange + MathVector vec1{ 1.3, 5.7, 3.1 }; + MathVector vec2{ 2.7, 5.2, 9.5 }; + //Act + MathVector res = vec1 + vec2; + + // Assert + EXPECT_NEAR(4.0, res[0], EPSILON); + EXPECT_NEAR(10.9, res[1], EPSILON); + EXPECT_NEAR(12.6, res[2], EPSILON); +} + +TEST(TestMathVectorLib, test_operator_add_for_different_size_vectors) { + // Arrange + MathVector vec1{ 1.3, 5.7 }; + MathVector vec2{ 2.7, 5.2, 9.5 }; + + // Act & Assert + ASSERT_ANY_THROW(vec1 + vec2); +} + +TEST(TestMathVectorLib, test_operator_sub_for_int) { + // Arrange + MathVector vec1{ 3, 5, -12 }; + MathVector vec2{ 1, 6, 3 }; + //Act + MathVector res = vec1 - vec2; + + // Assert + EXPECT_EQ(2, res[0]); + EXPECT_EQ(-1, res[1]); + EXPECT_EQ(-15, res[2]); +} + +TEST(TestMathVectorLib, test_operator_sub_for_double) { + // Arrange + MathVector vec1{ 2.7, 5.7, -3.1 }; + MathVector vec2{ 1.3, 5.2, 9.5 }; + //Act + MathVector res = vec1 - vec2; + + // Assert + EXPECT_NEAR(1.4, res[0], EPSILON); + EXPECT_NEAR(0.5, res[1], EPSILON); + EXPECT_NEAR(-12.6, res[2], EPSILON); +} + +TEST(TestMathVectorLib, test_operator_sub_for_different_size_vectors) { + // Arrange + MathVector vec1{ 1.3, 5.7 }; + MathVector vec2{ 2.7, 5.2, 9.5 }; + + // Act & Assert + ASSERT_ANY_THROW(vec1 - vec2); +} + +TEST(TestMathVectorLib, test_operator_mult_for_scalar) { + // Arrange + MathVector vec{ 1, 2, 6 }; + + // Act + MathVector result = vec * 2; + + // Assert + EXPECT_EQ(2, result[0]); + EXPECT_EQ(4, result[1]); + EXPECT_EQ(12, result[2]); +} + +TEST(TestMathVectorLib, test_operator_mult_dot_product) { + // Arrange + MathVector vec1{ 1, 2, 6 }; + MathVector vec2{ 3, 1, 5 }; + + // Act & Assert + EXPECT_EQ(35, vec1*vec2); +} + +TEST(TestMathVectorLib, test_operator_mult_which_vectors_have_sizes_not_equal) { + // Arrange + MathVector vec1{ 1, 2, 6 }; + MathVector vec2{ 3, 1, 5, 8 }; + + // Act & Assert + ASSERT_ANY_THROW(vec1 * vec2); } \ No newline at end of file From 4d1668af8dd36ad0d9703c52771c90cb4dd35d2e Mon Sep 17 00:00:00 2001 From: Kate-Nits Date: Sat, 27 Sep 2025 22:13:32 +0300 Subject: [PATCH 51/94] Wrote operators +=, -=, *=, function length of the vector and added tests on they --- lib_mathvector/MathVector.h | 35 +++++++++----- tests/test_mathvector.cpp | 91 +++++++++++++++++++++++++++++++++++++ 2 files changed, 114 insertions(+), 12 deletions(-) diff --git a/lib_mathvector/MathVector.h b/lib_mathvector/MathVector.h index 3c6c7364..2ef673bf 100644 --- a/lib_mathvector/MathVector.h +++ b/lib_mathvector/MathVector.h @@ -61,7 +61,7 @@ class MathVector : public TVector { return result; } - T operator*(const MathVector& other_vec) const { + T operator*(const MathVector& other_vec) const { if (this->_size != other_vec._size) { throw std::invalid_argument("Vectors should have same size for dot product"); } @@ -73,26 +73,37 @@ class MathVector : public TVector { } MathVector& operator+=(const MathVector& other) { - in_development(); + if (this->_size != other._size) { + throw std::invalid_argument("Vectors should have same size for addition"); + } + for (size_t i = 0; i < this->_size; ++i) { + this->_data[i] = this->_data[i] + other._data[i]; + } return *this; } MathVector& operator-=(const MathVector& other) { - in_development(); + if (this->_size != other._size) { + throw std::invalid_argument("Vectors should have same size for subtraction"); + } + for (size_t i = 0; i < this->_size; ++i) { + this->_data[i] = this->_data[i] - other._data[i]; + } return *this; } MathVector& operator*=(const T& scalar) { - in_development(); + for (size_t i = 0; i < this->_size; ++i) { + this->_data[i] = this->_data[i] * scalar; + } return *this; } - T dot(const MathVector& other) const { // - in_development(); - return T(); - } - double norm() const { // - in_development(); - return double; + + double length() const { // + T sum = 0; + for (size_t i = 0; i < this->_size; i++) { + sum += this->_data[i] * this->_data[i]; + } + return std::sqrt((double)sum); } }; - #endif // LIB_MATHVECTOR_MATHVECTOR_H diff --git a/tests/test_mathvector.cpp b/tests/test_mathvector.cpp index f8b9e8b8..2727e298 100644 --- a/tests/test_mathvector.cpp +++ b/tests/test_mathvector.cpp @@ -875,4 +875,95 @@ TEST(TestMathVectorLib, test_operator_mult_which_vectors_have_sizes_not_equal) { // Act & Assert ASSERT_ANY_THROW(vec1 * vec2); +} + +TEST(TestMathVectorLib, test_operator_add_equal_for_int) { + // Arrange + MathVector vec1{ 1, 5, 3 }; + MathVector vec2{ 2, 5, 9 }; + //Act + vec1 += vec2; + + // Assert + EXPECT_EQ(3, vec1[0]); + EXPECT_EQ(10, vec1[1]); + EXPECT_EQ(12, vec1[2]); +} + +TEST(TestMathVectorLib, test_operator_add_equal_for_double) { + // Arrange + MathVector vec1{ 1.3, 5.7, 3.1 }; + MathVector vec2{ 2.7, 5.2, 9.5 }; + //Act + vec1 += vec2; + + // Assert + EXPECT_NEAR(4.0, vec1[0], EPSILON); + EXPECT_NEAR(10.9, vec1[1], EPSILON); + EXPECT_NEAR(12.6, vec1[2], EPSILON); +} + +TEST(TestMathVectorLib, test_operator_add_equal_for_different_size_vectors) { + // Arrange + MathVector vec1{ 1.3, 5.7, 9.5 }; + MathVector vec2{ 2.7, 5.2 }; + + // Act & Assert + ASSERT_ANY_THROW(vec1 += vec2); +} + +TEST(TestMathVectorLib, test_operator_sub_equal_for_int) { + // Arrange + MathVector vec1{ 3, 5, -12 }; + MathVector vec2{ 1, 6, 3 }; + //Act + vec1 -= vec2; + + // Assert + EXPECT_EQ(2, vec1[0]); + EXPECT_EQ(-1, vec1[1]); + EXPECT_EQ(-15, vec1[2]); +} + +TEST(TestMathVectorLib, test_operator_sub_equal_for_double) { + // Arrange + MathVector vec1{ 2.7, 5.7, -3.1 }; + MathVector vec2{ 1.3, 5.2, 9.5 }; + //Act + vec1 -= vec2; + + // Assert + EXPECT_NEAR(1.4, vec1[0], EPSILON); + EXPECT_NEAR(0.5, vec1[1], EPSILON); + EXPECT_NEAR(-12.6, vec1[2], EPSILON); +} + +TEST(TestMathVectorLib, test_operator_sub_equal_for_different_size_vectors) { + // Arrange + MathVector vec1{ 1.3, 5.7 }; + MathVector vec2{ 2.7, 5.2, 9.5 }; + + // Act & Assert + ASSERT_ANY_THROW(vec1 -= vec2); +} + +TEST(TestMathVectorLib, test_operator_mult_equal_for_scalar) { + // Arrange + MathVector vec{ 1, 2, 6 }; + + // Act + vec *= 3; + + // Assert + EXPECT_EQ(3, vec[0]); + EXPECT_EQ(6, vec[1]); + EXPECT_EQ(18, vec[2]); +} + +TEST(TestMathVectorLib, test_length_of_the_vector) { + // Arrange + MathVector vec{ 3, 4 }; + + // Act & Assert + EXPECT_EQ(5.0, vec.length()); } \ No newline at end of file From 2e52da433041b5046d50da5a90c2d801081e7d3b Mon Sep 17 00:00:00 2001 From: Kate-Nits Date: Sat, 27 Sep 2025 23:10:55 +0300 Subject: [PATCH 52/94] Wrote conversion constructor and added test on it --- lib_mathvector/MathVector.h | 17 ++++++++++++++++- tests/test_mathvector.cpp | 14 ++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/lib_mathvector/MathVector.h b/lib_mathvector/MathVector.h index 2ef673bf..8e2a644e 100644 --- a/lib_mathvector/MathVector.h +++ b/lib_mathvector/MathVector.h @@ -29,7 +29,22 @@ class MathVector : public TVector { this->_states[i] = empty; } } - // int double? + template + MathVector(const MathVector& other) { + this->_size = other.size(); + this->_capacity = this->_size + TVector::RESERVE_MEMORY; + this->_deleted = 0; + this->_data = new T[this->_capacity]; + this->_states = new State[this->_capacity]; + + for (size_t i = 0; i < this->_size; ++i) { + this->_data[i] = static_cast(other.data(i)); // U T + this->_states[i] = other.state(i); + } + for (size_t i = this->_size; i < this->_capacity; ++i) { + this->_states[i] = State::empty; + } + } MathVector operator+(const MathVector& other) const { if (this->_size != other._size) { throw std::invalid_argument("Vectors should have same size for addition"); diff --git a/tests/test_mathvector.cpp b/tests/test_mathvector.cpp index 2727e298..45b6c006 100644 --- a/tests/test_mathvector.cpp +++ b/tests/test_mathvector.cpp @@ -122,6 +122,20 @@ TEST(TestMathVectorLib, test_constructor_list_double_init) { EXPECT_EQ(vec[3], 4.4); } +TEST(TestMathVectorLib, test_conversion_constructors) { + // Arrange + MathVector vec_int{ 1, 2, 3 }; + + // Act + MathVector vec_double(vec_int); + + // Assert + EXPECT_EQ(3, vec_double.size()); + EXPECT_NEAR(1.0, vec_double[0], EPSILON); + EXPECT_NEAR(2.0, vec_double[1], EPSILON); + EXPECT_NEAR(3.0, vec_double[2], EPSILON); +} + TEST(TestMathVectorLib, test_at_checking_without_difficulties) { // Arrange size_t size = 8; From cd71dc8e0a4459a38c548cdeed987b37a4a841ab Mon Sep 17 00:00:00 2001 From: Kate-Nits Date: Sat, 27 Sep 2025 23:49:25 +0300 Subject: [PATCH 53/94] Wrote operator + which accepts different types, and wrote tests that check it --- lib_mathvector/MathVector.h | 12 ++++++++++++ tests/test_mathvector.cpp | 16 +++++++++++++++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/lib_mathvector/MathVector.h b/lib_mathvector/MathVector.h index 8e2a644e..a2bc2ebf 100644 --- a/lib_mathvector/MathVector.h +++ b/lib_mathvector/MathVector.h @@ -56,6 +56,18 @@ class MathVector : public TVector { } return result; } + template + MathVector> operator+(const MathVector& other) const { + using R = std::common_type_t; // std::common_type_t + if (this->_size != other.size()) { + throw std::invalid_argument("Vectors should have same size for addition"); + } + MathVector result(this->_size); + for (size_t i = 0; i < this->_size; ++i) { + result[i] = static_cast(this->_data[i]) + static_cast(other[i]); + } + return result; + } MathVector operator-(const MathVector& other) const { if (this->_size != other._size) { throw std::invalid_argument("Vectors should have same size for subtraction"); diff --git a/tests/test_mathvector.cpp b/tests/test_mathvector.cpp index 45b6c006..a4037f73 100644 --- a/tests/test_mathvector.cpp +++ b/tests/test_mathvector.cpp @@ -122,7 +122,7 @@ TEST(TestMathVectorLib, test_constructor_list_double_init) { EXPECT_EQ(vec[3], 4.4); } -TEST(TestMathVectorLib, test_conversion_constructors) { +TEST(TestMathVectorLib, test_conversion_constructor) { // Arrange MathVector vec_int{ 1, 2, 3 }; @@ -825,6 +825,20 @@ TEST(TestMathVectorLib, test_operator_add_for_different_size_vectors) { ASSERT_ANY_THROW(vec1 + vec2); } +TEST(TestMathVectorLib, test_operator_add_for_double_and_int) { + // Arrange + MathVector vec1{ 1.3, 5.7, 3.1 }; + MathVector vec2{ 2, 5, 9 }; + + //Act + auto res = vec1 + vec2; + + // Assert + EXPECT_NEAR(3.3, res[0], EPSILON); + EXPECT_NEAR(10.7, res[1], EPSILON); + EXPECT_NEAR(12.1, res[2], EPSILON); +} + TEST(TestMathVectorLib, test_operator_sub_for_int) { // Arrange MathVector vec1{ 3, 5, -12 }; From 4210377e500b2f6199ff013c15545754dce4a65f Mon Sep 17 00:00:00 2001 From: Kate-Nits Date: Sat, 27 Sep 2025 23:59:50 +0300 Subject: [PATCH 54/94] Wrote operator - which accepts different types, and wrote tests that check it --- lib_mathvector/MathVector.h | 13 ++++++++++++- tests/test_mathvector.cpp | 13 +++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/lib_mathvector/MathVector.h b/lib_mathvector/MathVector.h index a2bc2ebf..19d889ef 100644 --- a/lib_mathvector/MathVector.h +++ b/lib_mathvector/MathVector.h @@ -79,7 +79,18 @@ class MathVector : public TVector { } return result; } - + template + MathVector> operator-(const MathVector& other) const { + using R = std::common_type_t; // std::common_type_t + if (this->_size != other.size()) { + throw std::invalid_argument("Vectors should have same size for subtraction"); + } + MathVector result(this->_size); + for (size_t i = 0; i < this->_size; ++i) { + result[i] = static_cast(this->_data[i]) - static_cast(other[i]); + } + return result; + } MathVector operator*(const T& scalar) const { MathVector result(this->_size); for (size_t i = 0; i < this->_size; ++i) { diff --git a/tests/test_mathvector.cpp b/tests/test_mathvector.cpp index a4037f73..d17613de 100644 --- a/tests/test_mathvector.cpp +++ b/tests/test_mathvector.cpp @@ -874,6 +874,19 @@ TEST(TestMathVectorLib, test_operator_sub_for_different_size_vectors) { ASSERT_ANY_THROW(vec1 - vec2); } +TEST(TestMathVectorLib, test_operator_sub_for_double_and_int) { + // Arrange + MathVector vec1{ 2.7, 5.7, -3.1 }; + MathVector vec2{ 1, 5, 9 }; + //Act + auto res = vec1 - vec2; + + // Assert + EXPECT_NEAR(1.7, res[0], EPSILON); + EXPECT_NEAR(0.7, res[1], EPSILON); + EXPECT_NEAR(-12.1, res[2], EPSILON); +} + TEST(TestMathVectorLib, test_operator_mult_for_scalar) { // Arrange MathVector vec{ 1, 2, 6 }; From 759f39748a9f3166d007f50242f0225dbe9b6a59 Mon Sep 17 00:00:00 2001 From: Kate-Nits Date: Sun, 28 Sep 2025 01:06:29 +0300 Subject: [PATCH 55/94] Wrote operator * which accepts different types, and wrote tests that check it and added exception tests for operators of different types --- lib_mathvector/MathVector.h | 28 +++++++++++++++--- tests/test_mathvector.cpp | 58 +++++++++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+), 4 deletions(-) diff --git a/lib_mathvector/MathVector.h b/lib_mathvector/MathVector.h index 19d889ef..bab05202 100644 --- a/lib_mathvector/MathVector.h +++ b/lib_mathvector/MathVector.h @@ -64,7 +64,7 @@ class MathVector : public TVector { } MathVector result(this->_size); for (size_t i = 0; i < this->_size; ++i) { - result[i] = static_cast(this->_data[i]) + static_cast(other[i]); + result[i] = static_cast((*this)[i]) + static_cast(other[i]); } return result; } @@ -74,7 +74,7 @@ class MathVector : public TVector { } MathVector result(this->_size); for (size_t i = 0; i < this->_size; ++i) { - result._data[i] = this->_data[i] - other._data[i]; + result[i] = this->_data[i] - other._data[i]; result._states[i] = State::busy; } return result; @@ -98,14 +98,34 @@ class MathVector : public TVector { } return result; } - + template + MathVector> operator*(const U& scalar) const { + using R = std::common_type_t; + MathVector result(this->_size); + for (size_t i = 0; i < this->_size; ++i) { + result[i] = static_cast((*this)[i]) * static_cast(scalar); + } + return result; + } T operator*(const MathVector& other_vec) const { if (this->_size != other_vec._size) { throw std::invalid_argument("Vectors should have same size for dot product"); } T result = 0; for (size_t i = 0; i < this->_size; ++i) { - result += this->_data[i] * other_vec._data[i]; + result += this->data(i) * other_vec.data(i); + } + return result; + } + template + std::common_type_t operator*(const MathVector& other) const { + using R = std::common_type_t; + if (this->_size != other.size()) { + throw std::invalid_argument("Vectors should have same size for dot product"); + } + R result = 0; + for (size_t i = 0; i < this->_size; ++i) { + result += static_cast((*this)[i]) * static_cast(other[i]); } return result; } diff --git a/tests/test_mathvector.cpp b/tests/test_mathvector.cpp index d17613de..5a728a6a 100644 --- a/tests/test_mathvector.cpp +++ b/tests/test_mathvector.cpp @@ -839,6 +839,15 @@ TEST(TestMathVectorLib, test_operator_add_for_double_and_int) { EXPECT_NEAR(12.1, res[2], EPSILON); } +TEST(TestMathVectorLib, test_operator_add_for_different_size_vectors_which_have_different_types) { + // Arrange + MathVector vec1{ 1, 5 }; + MathVector vec2{ 2.7, 5.2, 9.5 }; + + // Act & Assert + ASSERT_ANY_THROW(vec1 + vec2); +} + TEST(TestMathVectorLib, test_operator_sub_for_int) { // Arrange MathVector vec1{ 3, 5, -12 }; @@ -887,6 +896,15 @@ TEST(TestMathVectorLib, test_operator_sub_for_double_and_int) { EXPECT_NEAR(-12.1, res[2], EPSILON); } +TEST(TestMathVectorLib, test_operator_sub_for_different_size_vectors_which_have_different_types) { + // Arrange + MathVector vec1{ 1, 5 }; + MathVector vec2{ 2.7, 5.2, 9.5 }; + + // Act & Assert + ASSERT_ANY_THROW(vec1 - vec2); +} + TEST(TestMathVectorLib, test_operator_mult_for_scalar) { // Arrange MathVector vec{ 1, 2, 6 }; @@ -918,6 +936,46 @@ TEST(TestMathVectorLib, test_operator_mult_which_vectors_have_sizes_not_equal) { ASSERT_ANY_THROW(vec1 * vec2); } +TEST(TestMathVectorLib, test_operator_mult_for_scalar_with_different_types) { + // Arrange + MathVector vec{ 1, 2, 6 }; + + // Act + auto result = vec * 2.3; + + // Assert + EXPECT_NEAR(2.3, result[0], EPSILON); + EXPECT_NEAR(4.6, result[1], EPSILON); + EXPECT_NEAR(13.8, result[2], EPSILON); +} + +TEST(TestMathVectorLib, test_operator_mult_dot_product_with_different_types) { + // Arrange + MathVector vec1{ 1, 2, 6 }; + MathVector vec2{ 3.0, 1.1, 5.3 }; + + // Act & Assert + EXPECT_NEAR(37.0, vec1 * vec2, EPSILON); +} + +TEST(TestMathVectorLib, test_operator_mult__with_different_types_which_vectors_have_sizes_not_equal) { + // Arrange + MathVector vec1{ 1, 2, 6 }; + MathVector vec2{ 3, 1, 5, 8 }; + + // Act & Assert + ASSERT_ANY_THROW(vec1 * vec2); +} + +TEST(TestMathVectorLib, test_operator_mult_for_different_size_vectors_which_have_different_types) { + // Arrange + MathVector vec1{ 1, 5 }; + MathVector vec2{ 2.7, 5.2, 9.5 }; + + // Act & Assert + ASSERT_ANY_THROW(vec1 * vec2); +} + TEST(TestMathVectorLib, test_operator_add_equal_for_int) { // Arrange MathVector vec1{ 1, 5, 3 }; From 30a5676495616512adea775be3b10427a90042a3 Mon Sep 17 00:00:00 2001 From: Kate-Nits Date: Sun, 28 Sep 2025 04:17:35 +0300 Subject: [PATCH 56/94] Done wrote class MathVecot and added more tests on this class --- lib_mathvector/MathVector.h | 20 ++++++----- tests/test_mathvector.cpp | 71 ++++++++++++++++++++++++++++++++++++- 2 files changed, 82 insertions(+), 9 deletions(-) diff --git a/lib_mathvector/MathVector.h b/lib_mathvector/MathVector.h index bab05202..0feac3b5 100644 --- a/lib_mathvector/MathVector.h +++ b/lib_mathvector/MathVector.h @@ -45,6 +45,7 @@ class MathVector : public TVector { this->_states[i] = State::empty; } } + MathVector operator+(const MathVector& other) const { if (this->_size != other._size) { throw std::invalid_argument("Vectors should have same size for addition"); @@ -64,10 +65,11 @@ class MathVector : public TVector { } MathVector result(this->_size); for (size_t i = 0; i < this->_size; ++i) { - result[i] = static_cast((*this)[i]) + static_cast(other[i]); + result[i] = static_cast(this->data(i)) + static_cast(other.data(i)); } return result; } + MathVector operator-(const MathVector& other) const { if (this->_size != other._size) { throw std::invalid_argument("Vectors should have same size for subtraction"); @@ -75,7 +77,7 @@ class MathVector : public TVector { MathVector result(this->_size); for (size_t i = 0; i < this->_size; ++i) { result[i] = this->_data[i] - other._data[i]; - result._states[i] = State::busy; + result._states[i] = busy; } return result; } @@ -87,14 +89,16 @@ class MathVector : public TVector { } MathVector result(this->_size); for (size_t i = 0; i < this->_size; ++i) { - result[i] = static_cast(this->_data[i]) - static_cast(other[i]); + result[i] = static_cast(this->data(i)) - static_cast(other.data(i)); } return result; } + MathVector operator*(const T& scalar) const { MathVector result(this->_size); for (size_t i = 0; i < this->_size; ++i) { result._data[i] = this->_data[i] * scalar; + result._states[i] = busy; } return result; } @@ -103,7 +107,7 @@ class MathVector : public TVector { using R = std::common_type_t; MathVector result(this->_size); for (size_t i = 0; i < this->_size; ++i) { - result[i] = static_cast((*this)[i]) * static_cast(scalar); + result[i] = static_cast(this->data(i)) * static_cast(scalar); } return result; } @@ -125,7 +129,7 @@ class MathVector : public TVector { } R result = 0; for (size_t i = 0; i < this->_size; ++i) { - result += static_cast((*this)[i]) * static_cast(other[i]); + result += static_cast(this->data(i)) * static_cast(other.data(i)); } return result; } @@ -155,13 +159,13 @@ class MathVector : public TVector { return *this; } - double length() const { // + auto length() const { // T sum = 0; for (size_t i = 0; i < this->_size; i++) { sum += this->_data[i] * this->_data[i]; } - return std::sqrt((double)sum); + return std::sqrt(sum); } }; -#endif // LIB_MATHVECTOR_MATHVECTOR_H +#endif // LIB_MATHVECTOR_MATHVECTOR_H \ No newline at end of file diff --git a/tests/test_mathvector.cpp b/tests/test_mathvector.cpp index 5a728a6a..ff70eb30 100644 --- a/tests/test_mathvector.cpp +++ b/tests/test_mathvector.cpp @@ -794,6 +794,7 @@ TEST(TestMathVectorLib, test_operator_add_for_int) { // Arrange MathVector vec1{ 1, 5, 3 }; MathVector vec2{ 1, 5, 3 }; + //Act MathVector res = vec1 + vec2; @@ -807,6 +808,7 @@ TEST(TestMathVectorLib, test_operator_add_for_double) { // Arrange MathVector vec1{ 1.3, 5.7, 3.1 }; MathVector vec2{ 2.7, 5.2, 9.5 }; + //Act MathVector res = vec1 + vec2; @@ -848,10 +850,37 @@ TEST(TestMathVectorLib, test_operator_add_for_different_size_vectors_which_have_ ASSERT_ANY_THROW(vec1 + vec2); } +TEST(TestMathVectorLib, states_after_addition) { + // Arrange + MathVector vec1{ 1, 2, 3 }; + MathVector vec2{ 4, 5, 6 }; + + //Act + MathVector res = vec1 + vec2; + + // Assert + for (size_t i = 0; i < res.size(); ++i) { + EXPECT_EQ(State::busy, res.state(i)); + } +} + +TEST(TestMathVectorLib, empty_vectors_addition) { + // Arrange + MathVector vec1; + MathVector vec2; + + //Act + MathVector res = vec1 + vec2; + + // Assert + EXPECT_EQ(0, res.size()); +} + TEST(TestMathVectorLib, test_operator_sub_for_int) { // Arrange MathVector vec1{ 3, 5, -12 }; MathVector vec2{ 1, 6, 3 }; + //Act MathVector res = vec1 - vec2; @@ -865,6 +894,7 @@ TEST(TestMathVectorLib, test_operator_sub_for_double) { // Arrange MathVector vec1{ 2.7, 5.7, -3.1 }; MathVector vec2{ 1.3, 5.2, 9.5 }; + //Act MathVector res = vec1 - vec2; @@ -887,6 +917,7 @@ TEST(TestMathVectorLib, test_operator_sub_for_double_and_int) { // Arrange MathVector vec1{ 2.7, 5.7, -3.1 }; MathVector vec2{ 1, 5, 9 }; + //Act auto res = vec1 - vec2; @@ -905,6 +936,32 @@ TEST(TestMathVectorLib, test_operator_sub_for_different_size_vectors_which_have_ ASSERT_ANY_THROW(vec1 - vec2); } +TEST(TestMathVectorLib, states_after_subtraction) { + // Arrange + MathVector vec1{ 1, 2, 3 }; + MathVector vec2{ 4, 5, 6 }; + + //Act + MathVector res = vec1 - vec2; + + // Assert + for (size_t i = 0; i < res.size(); ++i) { + EXPECT_EQ(State::busy, res.state(i)); + } +} + +TEST(TestMathVectorLib, empty_vectors_subtraction) { + // Arrange + MathVector vec1; + MathVector vec2; + + //Act + MathVector res = vec1 - vec2; + + // Assert + EXPECT_EQ(0, res.size()); +} + TEST(TestMathVectorLib, test_operator_mult_for_scalar) { // Arrange MathVector vec{ 1, 2, 6 }; @@ -980,6 +1037,7 @@ TEST(TestMathVectorLib, test_operator_add_equal_for_int) { // Arrange MathVector vec1{ 1, 5, 3 }; MathVector vec2{ 2, 5, 9 }; + //Act vec1 += vec2; @@ -993,6 +1051,7 @@ TEST(TestMathVectorLib, test_operator_add_equal_for_double) { // Arrange MathVector vec1{ 1.3, 5.7, 3.1 }; MathVector vec2{ 2.7, 5.2, 9.5 }; + //Act vec1 += vec2; @@ -1015,6 +1074,7 @@ TEST(TestMathVectorLib, test_operator_sub_equal_for_int) { // Arrange MathVector vec1{ 3, 5, -12 }; MathVector vec2{ 1, 6, 3 }; + //Act vec1 -= vec2; @@ -1028,6 +1088,7 @@ TEST(TestMathVectorLib, test_operator_sub_equal_for_double) { // Arrange MathVector vec1{ 2.7, 5.7, -3.1 }; MathVector vec2{ 1.3, 5.2, 9.5 }; + //Act vec1 -= vec2; @@ -1059,10 +1120,18 @@ TEST(TestMathVectorLib, test_operator_mult_equal_for_scalar) { EXPECT_EQ(18, vec[2]); } -TEST(TestMathVectorLib, test_length_of_the_vector) { +TEST(TestMathVectorLib, test_length_of_the_double_vector) { // Arrange MathVector vec{ 3, 4 }; // Act & Assert EXPECT_EQ(5.0, vec.length()); +} + +TEST(TestMathVectorLib, test_length_of_the_float_vector) { + // Arrange + MathVector vec{ 3.0f, 4.0f }; + + // Act & Assert + EXPECT_NEAR(5.0, vec.length(), EPSILON); } \ No newline at end of file From 286d2fed6ca54e6c9dc1459b21bdb812edec58be Mon Sep 17 00:00:00 2001 From: Kate-Nits Date: Wed, 1 Oct 2025 03:04:35 +0300 Subject: [PATCH 57/94] Start to write the class Matrix. Fixed an error that Visual Studio wrote in the declaration class Matrix. Wrote all constructions for this class: default constructor, constructor with size, constructor with initializer_list, copy constructor and added test of them. Also wrote at and getters rows(), cols() --- CMakeLists.txt | 2 +- lib_mathvector/MathVector.h | 37 ++++++------ lib_matrix/matrix.h | 113 ++++++++++++++++++------------------ lib_tvector/tvector.h | 2 +- tests/test_matrix.cpp | 112 +++++++++++++++++++++++++++++++++++ 5 files changed, 189 insertions(+), 77 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 7dade311..ecafe97c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -14,6 +14,7 @@ include(cmake/function.cmake) # подхватываем функции, # и для создания исполняемого проекта в отдельные функции add_subdirectory(lib_easy_example) # подключаем дополнительный CMakeLists.txt из подкаталога с именем lib_easy_example +add_subdirectory(lib_algorithms) add_subdirectory(lib_point) add_subdirectory(lib_circle) add_subdirectory(lib_point3d) @@ -22,7 +23,6 @@ add_subdirectory(lib_tvector) add_subdirectory(lib_mathvector) add_subdirectory(lib_matrix) add_subdirectory(lib_triangle_matrix) -add_subdirectory(lib_algorithms) add_subdirectory(main) # подключаем дополнительный CMakeLists.txt из подкаталога с именем main option(BTEST "build test?" ON) # указываем подключаем ли google-тесты (ON или YES) или нет (OFF или NO) diff --git a/lib_mathvector/MathVector.h b/lib_mathvector/MathVector.h index 0feac3b5..6afd0b3c 100644 --- a/lib_mathvector/MathVector.h +++ b/lib_mathvector/MathVector.h @@ -12,10 +12,11 @@ template class MathVector : public TVector { public: using TVector::TVector; // TVector + MathVector() : TVector() {} MathVector(const MathVector& other) : TVector(other) {} MathVector(const std::initializer_list list) { - this->_size = list.size(); - this->_capacity = this->_size + TVector::RESERVE_MEMORY; + this->_size= list.size(); + this->_capacity = this->size()+ TVector::RESERVE_MEMORY; this->_deleted = 0; this->_data = new T[this->_capacity]; this->_states = new State[this->_capacity]; @@ -32,7 +33,7 @@ class MathVector : public TVector { template MathVector(const MathVector& other) { this->_size = other.size(); - this->_capacity = this->_size + TVector::RESERVE_MEMORY; + this->_capacity = this->size()+ TVector::RESERVE_MEMORY; this->_deleted = 0; this->_data = new T[this->_capacity]; this->_states = new State[this->_capacity]; @@ -47,12 +48,12 @@ class MathVector : public TVector { } MathVector operator+(const MathVector& other) const { - if (this->_size != other._size) { + if (this->size()!= other._size) { throw std::invalid_argument("Vectors should have same size for addition"); } MathVector result(this->_size); for (size_t i = 0; i < this->_size; ++i) { - result._data[i] = this->_data[i] + other._data[i]; + result[i] = this->data(i) + other._data[i]; result._states[i] = busy; } return result; @@ -60,7 +61,7 @@ class MathVector : public TVector { template MathVector> operator+(const MathVector& other) const { using R = std::common_type_t; // std::common_type_t - if (this->_size != other.size()) { + if (this->size()!= other.size()) { throw std::invalid_argument("Vectors should have same size for addition"); } MathVector result(this->_size); @@ -71,12 +72,12 @@ class MathVector : public TVector { } MathVector operator-(const MathVector& other) const { - if (this->_size != other._size) { + if (this->size()!= other._size) { throw std::invalid_argument("Vectors should have same size for subtraction"); } MathVector result(this->_size); for (size_t i = 0; i < this->_size; ++i) { - result[i] = this->_data[i] - other._data[i]; + result[i] = this->data(i) - other._data[i]; result._states[i] = busy; } return result; @@ -84,7 +85,7 @@ class MathVector : public TVector { template MathVector> operator-(const MathVector& other) const { using R = std::common_type_t; // std::common_type_t - if (this->_size != other.size()) { + if (this->size()!= other.size()) { throw std::invalid_argument("Vectors should have same size for subtraction"); } MathVector result(this->_size); @@ -97,7 +98,7 @@ class MathVector : public TVector { MathVector operator*(const T& scalar) const { MathVector result(this->_size); for (size_t i = 0; i < this->_size; ++i) { - result._data[i] = this->_data[i] * scalar; + result[i] = this->data(i) * scalar; result._states[i] = busy; } return result; @@ -112,7 +113,7 @@ class MathVector : public TVector { return result; } T operator*(const MathVector& other_vec) const { - if (this->_size != other_vec._size) { + if (this->size()!= other_vec._size) { throw std::invalid_argument("Vectors should have same size for dot product"); } T result = 0; @@ -124,7 +125,7 @@ class MathVector : public TVector { template std::common_type_t operator*(const MathVector& other) const { using R = std::common_type_t; - if (this->_size != other.size()) { + if (this->size()!= other.size()) { throw std::invalid_argument("Vectors should have same size for dot product"); } R result = 0; @@ -135,26 +136,26 @@ class MathVector : public TVector { } MathVector& operator+=(const MathVector& other) { - if (this->_size != other._size) { + if (this->size()!= other._size) { throw std::invalid_argument("Vectors should have same size for addition"); } for (size_t i = 0; i < this->_size; ++i) { - this->_data[i] = this->_data[i] + other._data[i]; + this->_data[i] = this->data(i) + other._data[i]; } return *this; } MathVector& operator-=(const MathVector& other) { - if (this->_size != other._size) { + if (this->size()!= other._size) { throw std::invalid_argument("Vectors should have same size for subtraction"); } for (size_t i = 0; i < this->_size; ++i) { - this->_data[i] = this->_data[i] - other._data[i]; + this->_data[i] = this->data(i) - other._data[i]; } return *this; } MathVector& operator*=(const T& scalar) { for (size_t i = 0; i < this->_size; ++i) { - this->_data[i] = this->_data[i] * scalar; + this->_data[i] = this->data(i) * scalar; } return *this; } @@ -162,7 +163,7 @@ class MathVector : public TVector { auto length() const { // T sum = 0; for (size_t i = 0; i < this->_size; i++) { - sum += this->_data[i] * this->_data[i]; + sum += this->data(i) * this->data(i); } return std::sqrt(sum); } diff --git a/lib_matrix/matrix.h b/lib_matrix/matrix.h index 81a5c001..43dcf4bf 100644 --- a/lib_matrix/matrix.h +++ b/lib_matrix/matrix.h @@ -5,95 +5,94 @@ #include #include - -#include "../lib_algorithms/algorithms.h" +template +class MathVector; #include "../lib_mathvector/MathVector.h" -/* + template -class TMatrix : public MathVector> { - size_t _rows; - size_t _cols; +class Matrix : public MathVector> { + size_t _rows; // + size_t _cols; // public: - TMatrix() noexcept = default; - TMatrix(size_t rows, size_t cols) { - in_development(); - _rows = rows; - _cols = cols; + using MathVector>::MathVector; // + Matrix() : MathVector>(), _rows(0), _cols(0) {} + Matrix(size_t value_rows, size_t value_cols) : MathVector>(value_rows), _rows(value_rows), _cols(value_cols) { + for (size_t i = 0; i < value_rows; ++i) { + this->_data[i] = MathVector(value_cols); + this->_states[i] = busy; + for (size_t j = 0; j < value_cols; ++j) { + this->_data[i][j] = T{}; // {} 0 + } + } + } + Matrix(const std::initializer_list> list) { + _rows = list.size(); + _cols = list.begin()->size(); + + this->_size = _rows; + this->_capacity = _rows + TVector::RESERVE_MEMORY; + this->_deleted = 0; + + this->_data = new MathVector[this->_capacity]; + this->_states = new State[this->_capacity]; + + size_t i = 0; + for (const auto& row : list) { //auto list + if (row.size() != _cols) { + throw std::invalid_argument("All rows must have the same size"); + } + this->_data[i] = row; + this->_states[i] = busy; + ++i; + } + for (; i < this->_capacity; ++i) { + this->_states[i] = empty; + } } + Matrix(const Matrix& other) : MathVector>(other), _rows(other._rows), _cols(other._cols) {} + - TMatrix(const TMatrix& other) = default; - TMatrix(TMatrix&& other) noexcept = default; - ~TMatrix() = default; + size_t rows() const noexcept { return _rows; } + size_t cols() const noexcept { return _cols; } + T& at(size_t row, size_t col) { + return this->_data[row][col]; + } + const T& at(size_t row, size_t col) const{ + return this->_data[row][col]; + } + /* TMatrix& operator=(const TMatrix& other) noexcept { in_development(); return *this; } - TMatrix& operator=(TMatrix&& other) noexcept { in_development(); return *this; } - - MathVector& operator[](size_t row) { - in_development(); - } - - const MathVector& operator[](size_t row) const { - in_development(); - } - + MathVector& operator[](size_t row) { in_development();} + const MathVector& operator[](size_t row) const { in_development(); } TMatrix add_matrices(const TMatrix& other) const { in_development(); return TMatrix(); } - TMatrix subtract_matrices(const TMatrix& other) const { in_development(); return TMatrix(); } - TMatrix multiply_matrices(const TMatrix& other) const { in_development(); return TMatrix(); } - - size_t rows() const noexcept { - in_development(); - return _rows; - } - - size_t cols() const noexcept { - in_development(); - return _cols; - } - void resize(size_t newRows, size_t newCols) { in_development(); _rows = newRows; _cols = newCols; } - - void transpose() { - in_development(); - } - - void print() const { - in_development(); - } - - friend std::ostream& operator<< <>(std::ostream& out, const TMatrix& matrix); + void transpose() { in_development(); } + void print() const { in_development(); } + friend std::ostream& operator<< <>(std::ostream& out, const TMatrix& matrix);*/ }; - -template -std::ostream& operator<<(std::ostream& out, const TMatrix& matrix) { - in_development(); - out << ""; - return out; -} -*/ - - #endif // LIB_MATRIX_MATRIX_H diff --git a/lib_tvector/tvector.h b/lib_tvector/tvector.h index 72081897..28bc4037 100644 --- a/lib_tvector/tvector.h +++ b/lib_tvector/tvector.h @@ -21,7 +21,7 @@ class TVector { static const size_t RESERVE_MEMORY = 15; static const size_t MAX_PERCENT_DELETED = 15; - TVector() : _data(nullptr), _states(nullptr), _size(0), _capacity(0), _deleted(0) {} //êîíñòðóêòîð ïî óìîë÷àíèþ + TVector() : _data(nullptr), _states(nullptr), _size(0), _capacity(0), _deleted(0) {} TVector(size_t size) { //конструктор вектора заданного размера _size = size; _capacity = size + RESERVE_MEMORY; diff --git a/tests/test_matrix.cpp b/tests/test_matrix.cpp index e69de29b..a2d8aa0f 100644 --- a/tests/test_matrix.cpp +++ b/tests/test_matrix.cpp @@ -0,0 +1,112 @@ +// Copyright 2025 Ekaterina Ushnitskaya + +#include +#include "../lib_matrix/matrix.h" + +#define EPSILON 0.000001 +#define TRUE 1 +#define FALSE 0 + +TEST(TestMatrixLib, default_constructor) { + // Arrange & Act + Matrix m; + + // Assert + EXPECT_EQ(0, m.rows()); + EXPECT_EQ(0, m.cols()); +} + +TEST(TestMatrixLib, constructor_with_size) { + // Arrange & Act + Matrix m(3, 4); + + // Assert + EXPECT_EQ(3, m.rows()); + EXPECT_EQ(4, m.cols()); + for (size_t i = 0; i < m.rows(); ++i) { + for (size_t j = 0; j < m.cols(); ++j) { + EXPECT_EQ(0, m.at(i, j)); + } + } +} + +TEST(TestMatrixLib, constructor_with_initializer_list_of_int_vectors_with_equal_size) { + // Arrange + MathVector vec1{ 1, 2, 3 }; + MathVector vec2{ 4, 5, 6 }; + int number = 1; + + // Act + Matrix m{ vec1, vec2 }; + + // Assert + EXPECT_EQ(2, m.rows()); + EXPECT_EQ(3, m.cols()); + for (size_t i = 0; i < m.rows(); ++i) { + for (size_t j = 0; j < m.cols(); ++j) { + EXPECT_EQ(number, m.at(i, j)); + number++; + } + } +} + +TEST(TestMatrixLib, constructor_with_initializer_list_of_double_vectors_with_equal_size) { + // Arrange + MathVector vec1{ 1.1, 2.2 }; + MathVector vec2{ 4.4, 5.5 }; + + // Act + Matrix m{ vec1, vec2 }; + + // Assert + EXPECT_EQ(2, m.rows()); + EXPECT_EQ(2, m.cols()); + EXPECT_NEAR(1.1, m.at(0, 0), EPSILON); + EXPECT_NEAR(2.2, m.at(0, 1), EPSILON); + EXPECT_NEAR(4.4, m.at(1, 0), EPSILON); + EXPECT_NEAR(5.5, m.at(1, 1), EPSILON); +} + +TEST(TestMatrixLib, constructor_with_initializer_list_of_vectors_with_different_size) { + // Arrange + MathVector vec1{ 1, 2 }; + MathVector vec2{ 4, 5, 6 }; + + // Act & Assert + ASSERT_ANY_THROW((Matrix{ vec1, vec2 })); +} + +TEST(TestMatrixLib, constructor_with_initializer_list_without_vectors_with_int) { + // Arrange & Act + int number = 1; + Matrix m{ { 1, 2, 3 }, { 4, 5, 6 } }; + + // Assert + EXPECT_EQ(2, m.rows()); + EXPECT_EQ(3, m.cols()); + for (size_t i = 0; i < m.rows(); ++i) { + for (size_t j = 0; j < m.cols(); ++j) { + EXPECT_EQ(number, m.at(i, j)); + number++; + } + } +} + +TEST(TestMatrixLib, copy_constructor) { + // Arrange + int number = 1; + Matrix m{ { 1, 2, 3 }, { 4, 5, 6 }, {7, 8, 9}, {10, 11, 12} }; + + // Act + Matrix matrix(m); + + // Assert + EXPECT_EQ(4, matrix.rows()); + EXPECT_EQ(3, matrix.cols()); + for (size_t i = 0; i < matrix.rows(); ++i) { + for (size_t j = 0; j < matrix.cols(); ++j) { + EXPECT_EQ(number, matrix.at(i, j)); + number++; + } + } +} \ No newline at end of file From e21d402a8203c7bbe313888ab0e1fa309d77317f Mon Sep 17 00:00:00 2001 From: Kate-Nits Date: Wed, 1 Oct 2025 21:52:49 +0300 Subject: [PATCH 58/94] Wrote operator= for class Matrix and added test on it --- lib_matrix/matrix.h | 27 +++++++++++---------------- tests/test_matrix.cpp | 19 +++++++++++++++++++ 2 files changed, 30 insertions(+), 16 deletions(-) diff --git a/lib_matrix/matrix.h b/lib_matrix/matrix.h index 43dcf4bf..ee4f3256 100644 --- a/lib_matrix/matrix.h +++ b/lib_matrix/matrix.h @@ -51,31 +51,26 @@ class Matrix : public MathVector> { } } Matrix(const Matrix& other) : MathVector>(other), _rows(other._rows), _cols(other._cols) {} - size_t rows() const noexcept { return _rows; } size_t cols() const noexcept { return _cols; } - T& at(size_t row, size_t col) { - return this->_data[row][col]; - } - const T& at(size_t row, size_t col) const{ - return this->_data[row][col]; - } + T& at(size_t row, size_t col) { return this->_data[row][col]; } + const T& at(size_t row, size_t col) const { return this->_data[row][col]; } - /* - TMatrix& operator=(const TMatrix& other) noexcept { - in_development(); - return *this; - } - TMatrix& operator=(TMatrix&& other) noexcept { - in_development(); + Matrix& operator=(const Matrix& other) noexcept { + if (this != &other) { + MathVector>::operator=(other); + _rows = other._rows; + _cols = other._cols; + } return *this; } + /* MathVector& operator[](size_t row) { in_development();} const MathVector& operator[](size_t row) const { in_development(); } - TMatrix add_matrices(const TMatrix& other) const { + Matrix add_matrices(const Matrix& other) const { in_development(); - return TMatrix(); + return Matrix(); } TMatrix subtract_matrices(const TMatrix& other) const { in_development(); diff --git a/tests/test_matrix.cpp b/tests/test_matrix.cpp index a2d8aa0f..eab04bc4 100644 --- a/tests/test_matrix.cpp +++ b/tests/test_matrix.cpp @@ -100,6 +100,25 @@ TEST(TestMatrixLib, copy_constructor) { // Act Matrix matrix(m); + // Assert + EXPECT_EQ(4, matrix.rows()); + EXPECT_EQ(3, matrix.cols()); + for (size_t i = 0; i < matrix.rows(); ++i) { + for (size_t j = 0; j < matrix.cols(); ++j) { + EXPECT_EQ(number, matrix.at(i, j)); + number++; + } + } +} + +TEST(TestMatrixLib, test_operator_equal) { + // Arrange + int number = 1; + Matrix m{ { 1, 2, 3 }, { 4, 5, 6 }, {7, 8, 9}, {10, 11, 12} }; + + // Act + Matrix matrix = m; + // Assert EXPECT_EQ(4, matrix.rows()); EXPECT_EQ(3, matrix.cols()); From ca603227f68876137691059c6b2832b51d5ee3d8 Mon Sep 17 00:00:00 2001 From: Kate-Nits Date: Wed, 1 Oct 2025 23:32:50 +0300 Subject: [PATCH 59/94] Wrote operator[] and rewrote at for class Matrix and added tests on it --- lib_matrix/matrix.h | 25 +++++++++++++++++++++---- tests/test_matrix.cpp | 29 +++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 4 deletions(-) diff --git a/lib_matrix/matrix.h b/lib_matrix/matrix.h index ee4f3256..5db1e904 100644 --- a/lib_matrix/matrix.h +++ b/lib_matrix/matrix.h @@ -54,8 +54,26 @@ class Matrix : public MathVector> { size_t rows() const noexcept { return _rows; } size_t cols() const noexcept { return _cols; } - T& at(size_t row, size_t col) { return this->_data[row][col]; } - const T& at(size_t row, size_t col) const { return this->_data[row][col]; } + T& at(size_t row, size_t col) { + if (row >= _rows) { + throw std::out_of_range("Row index out of range"); + } + if (col >= _cols) { + throw std::out_of_range("Col index out of range"); + } + return this->_data[row][col]; + } + const T& at(size_t row, size_t col) const { + if (row >= _rows) { + throw std::out_of_range("Row index out of range"); + } + if (col >= _cols) { + throw std::out_of_range("Col index out of range"); + } + return this->_data[row][col]; + } + MathVector& operator[](size_t row) { return this->_data[row]; } + const MathVector& operator[](size_t row) const { return this->_data[row]; } // [] TVector Matrix& operator=(const Matrix& other) noexcept { if (this != &other) { @@ -65,9 +83,8 @@ class Matrix : public MathVector> { } return *this; } + /* - MathVector& operator[](size_t row) { in_development();} - const MathVector& operator[](size_t row) const { in_development(); } Matrix add_matrices(const Matrix& other) const { in_development(); return Matrix(); diff --git a/tests/test_matrix.cpp b/tests/test_matrix.cpp index eab04bc4..b0a55be9 100644 --- a/tests/test_matrix.cpp +++ b/tests/test_matrix.cpp @@ -111,6 +111,22 @@ TEST(TestMatrixLib, copy_constructor) { } } +TEST(TestMatrixLib, test_at_row_out_of_range) { + // Arrange & Act + Matrix matrix(2, 2); + + // Assert + ASSERT_ANY_THROW(matrix.at(2, 0)); +} + +TEST(TestMatrixLib, test_at_col_out_of_range) { + // Arrange & Act + Matrix matrix(2, 2); + + // Assert + ASSERT_ANY_THROW(matrix.at(0, 4)); +} + TEST(TestMatrixLib, test_operator_equal) { // Arrange int number = 1; @@ -128,4 +144,17 @@ TEST(TestMatrixLib, test_operator_equal) { number++; } } +} + +TEST(TestMatrixLib, test_element_access) { + // Arrange & Act + Matrix matrix(2, 3); + matrix[0][0] = 10; + matrix[0][1] = 20; + matrix[1][2] = 30; + + // Assert + EXPECT_EQ(10, matrix[0][0]); + EXPECT_EQ(20, matrix[0][1]); + EXPECT_EQ(30, matrix[1][2]); } \ No newline at end of file From 94304660c7908c610a87b9e90470ba2e5f41a870 Mon Sep 17 00:00:00 2001 From: Kate-Nits Date: Thu, 2 Oct 2025 00:02:06 +0300 Subject: [PATCH 60/94] Wrote transpose() for class Matrix and added test on it --- lib_matrix/matrix.h | 11 +++++++++++ tests/test_matrix.cpp | 35 ++++++++++++++++++++++++++--------- 2 files changed, 37 insertions(+), 9 deletions(-) diff --git a/lib_matrix/matrix.h b/lib_matrix/matrix.h index 5db1e904..0e57aad6 100644 --- a/lib_matrix/matrix.h +++ b/lib_matrix/matrix.h @@ -84,6 +84,17 @@ class Matrix : public MathVector> { return *this; } + Matrix& transpose() { + Matrix result(_cols, _rows); + for (size_t i = 0; i < _rows; ++i) { + for (size_t j = 0; j < _cols; ++j) { + result[j][i] = this->at(i, j); + } + } + (*this) = result; + return *this; + } + /* Matrix add_matrices(const Matrix& other) const { in_development(); diff --git a/tests/test_matrix.cpp b/tests/test_matrix.cpp index b0a55be9..ab1a97a4 100644 --- a/tests/test_matrix.cpp +++ b/tests/test_matrix.cpp @@ -127,6 +127,19 @@ TEST(TestMatrixLib, test_at_col_out_of_range) { ASSERT_ANY_THROW(matrix.at(0, 4)); } +TEST(TestMatrixLib, test_element_access) { + // Arrange & Act + Matrix matrix(2, 3); + matrix[0][0] = 10; + matrix[0][1] = 20; + matrix[1][2] = 30; + + // Assert + EXPECT_EQ(10, matrix[0][0]); + EXPECT_EQ(20, matrix[0][1]); + EXPECT_EQ(30, matrix[1][2]); +} + TEST(TestMatrixLib, test_operator_equal) { // Arrange int number = 1; @@ -146,15 +159,19 @@ TEST(TestMatrixLib, test_operator_equal) { } } -TEST(TestMatrixLib, test_element_access) { - // Arrange & Act - Matrix matrix(2, 3); - matrix[0][0] = 10; - matrix[0][1] = 20; - matrix[1][2] = 30; +TEST(TestMatrixLib, test_transpose) { + // Arrange + int number = 1; + Matrix matrix{ { 1, 2, 3 }, { 4, 5, 6 }}; + + // Act + matrix.transpose(); // Assert - EXPECT_EQ(10, matrix[0][0]); - EXPECT_EQ(20, matrix[0][1]); - EXPECT_EQ(30, matrix[1][2]); + EXPECT_EQ(1, matrix[0][0]); + EXPECT_EQ(4, matrix[0][1]); + EXPECT_EQ(2, matrix[1][0]); + EXPECT_EQ(5, matrix[1][1]); + EXPECT_EQ(3, matrix[2][0]); + EXPECT_EQ(6, matrix[2][1]); } \ No newline at end of file From 8ebab1f4f53cca7221baf2a692ad7b3ae817f2bc Mon Sep 17 00:00:00 2001 From: Kate-Nits Date: Thu, 2 Oct 2025 01:02:04 +0300 Subject: [PATCH 61/94] Rewrote constructor with initializer_list for class Matrix. I added check on list.size() == 0 in this constructor. Wrote operator + for class Matrix and added tests on it --- lib_matrix/matrix.h | 53 +++++++++++++++++++++++++++++++++---------- tests/test_matrix.cpp | 38 +++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+), 12 deletions(-) diff --git a/lib_matrix/matrix.h b/lib_matrix/matrix.h index 0e57aad6..f08b6f55 100644 --- a/lib_matrix/matrix.h +++ b/lib_matrix/matrix.h @@ -27,6 +27,20 @@ class Matrix : public MathVector> { } } Matrix(const std::initializer_list> list) { + if (list.size() == 0) { // , list.begin() == list.end() + _rows = 0; + _cols = 0; + this->_size = 0; + this->_capacity = TVector::RESERVE_MEMORY; + this->_deleted = 0; + + this->_data = new MathVector[this->_capacity]; + this->_states = new State[this->_capacity]; + for (size_t i = 0; i < this->_capacity; ++i) { + this->_states[i] = empty; + } + return; + } _rows = list.size(); _cols = list.begin()->size(); @@ -75,7 +89,7 @@ class Matrix : public MathVector> { MathVector& operator[](size_t row) { return this->_data[row]; } const MathVector& operator[](size_t row) const { return this->_data[row]; } // [] TVector - Matrix& operator=(const Matrix& other) noexcept { + Matrix& operator=(const Matrix& other) { if (this != &other) { MathVector>::operator=(other); _rows = other._rows; @@ -95,27 +109,42 @@ class Matrix : public MathVector> { return *this; } + Matrix operator+(const Matrix& other) const { + if (_rows != other._rows) { + throw std::invalid_argument("Rows should have same size for addition"); + } + if (_cols != other._cols) { + throw std::invalid_argument("Cols should have same size for addition"); + } + Matrix result(_rows, _cols); + for (size_t i = 0; i < _rows; ++i) { + for (size_t j = 0; j < _cols; ++j) { + result[i][j] = this->at(i, j) + other.at(i, j); + } + } + return result; + } + /* - Matrix add_matrices(const Matrix& other) const { - in_development(); + Matrix operator-(const Matrix& other) const { return Matrix(); } - TMatrix subtract_matrices(const TMatrix& other) const { - in_development(); - return TMatrix(); + Matrix operator*(const Matrix& other) const { + return Matrix(); } - TMatrix multiply_matrices(const TMatrix& other) const { - in_development(); - return TMatrix(); + Matrix operator*(const T value) const { + return Matrix(); + } + Matrix operator*(const Vector& vec) const { + return Matrix(); } - void resize(size_t newRows, size_t newCols) { + void resize(size_t newRows, size_t newCols) { // ? in_development(); _rows = newRows; _cols = newCols; } - void transpose() { in_development(); } void print() const { in_development(); } friend std::ostream& operator<< <>(std::ostream& out, const TMatrix& matrix);*/ }; -#endif // LIB_MATRIX_MATRIX_H +#endif // LIB_MATRIX_MATRIX_H \ No newline at end of file diff --git a/tests/test_matrix.cpp b/tests/test_matrix.cpp index ab1a97a4..497921b9 100644 --- a/tests/test_matrix.cpp +++ b/tests/test_matrix.cpp @@ -174,4 +174,42 @@ TEST(TestMatrixLib, test_transpose) { EXPECT_EQ(5, matrix[1][1]); EXPECT_EQ(3, matrix[2][0]); EXPECT_EQ(6, matrix[2][1]); +} + +TEST(TestMatrixLib, test_add_to_int_matrixes) { + // Arrange + Matrix matrix1{ { 1, 2, 3 }, { 4, 5, 6 } }; + Matrix matrix2{ { 1, 2, 3 }, { 4, 5, 6 } }; + int number = 1; + + // Act + Matrix result = matrix1 + matrix2; + + // Assert + EXPECT_EQ(2, result.rows()); + EXPECT_EQ(3, result.cols()); + for (size_t i = 0; i < result.rows(); ++i) { + for (size_t j = 0; j < result.cols(); ++j) { + EXPECT_EQ(number * 2, result[i][j]); + number++; + } + } +} + +TEST(TestMatrixLib, test_add_to_int_matrixes_with_different_rows) { + // Arrange + Matrix matrix1{ { 1, 2, 3 }, { 4, 5, 6 } }; + Matrix matrix2{ { 1, 2, 3 }, { 4, 5, 6 }, { 4, 5, 6 } }; + + // Act & Assert + ASSERT_ANY_THROW(matrix1 + matrix2); +} + +TEST(TestMatrixLib, test_add_to_int_matrixes_with_different_cols) { + // Arrange + Matrix matrix1{ { 1, 2, 3 }, { 4, 5, 6 } }; + Matrix matrix2{ { 1, 2, 3, 1 }, { 4, 5, 6, 1 } }; + + // Act & Assert + ASSERT_ANY_THROW(matrix1 + matrix2); } \ No newline at end of file From 23c66e569d71b90c48d22ccbce6c4799b2fb3885 Mon Sep 17 00:00:00 2001 From: Kate-Nits Date: Thu, 2 Oct 2025 01:06:29 +0300 Subject: [PATCH 62/94] Wrote operator - for class Matrix and added tests on it --- lib_matrix/matrix.h | 18 +++++++++++++++--- tests/test_matrix.cpp | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 3 deletions(-) diff --git a/lib_matrix/matrix.h b/lib_matrix/matrix.h index f08b6f55..1806017b 100644 --- a/lib_matrix/matrix.h +++ b/lib_matrix/matrix.h @@ -124,11 +124,23 @@ class Matrix : public MathVector> { } return result; } - - /* Matrix operator-(const Matrix& other) const { - return Matrix(); + if (_rows != other._rows) { + throw std::invalid_argument("Rows should have same size for subtraction"); + } + if (_cols != other._cols) { + throw std::invalid_argument("Cols should have same size for subtraction"); + } + Matrix result(_rows, _cols); + for (size_t i = 0; i < _rows; ++i) { + for (size_t j = 0; j < _cols; ++j) { + result[i][j] = this->at(i, j) - other.at(i, j); + } + } + return result; } + + /* Matrix operator*(const Matrix& other) const { return Matrix(); } diff --git a/tests/test_matrix.cpp b/tests/test_matrix.cpp index 497921b9..41d3891d 100644 --- a/tests/test_matrix.cpp +++ b/tests/test_matrix.cpp @@ -212,4 +212,40 @@ TEST(TestMatrixLib, test_add_to_int_matrixes_with_different_cols) { // Act & Assert ASSERT_ANY_THROW(matrix1 + matrix2); +} + +TEST(TestMatrixLib, test_sub_to_int_matrixes) { + // Arrange + Matrix matrix1{ { 1, 2, 3 }, { 4, 5, 6 } }; + Matrix matrix2{ { 1, 2, 3 }, { 4, 5, 6 } }; + + // Act + Matrix result = matrix1 - matrix2; + + // Assert + EXPECT_EQ(2, result.rows()); + EXPECT_EQ(3, result.cols()); + for (size_t i = 0; i < result.rows(); ++i) { + for (size_t j = 0; j < result.cols(); ++j) { + EXPECT_EQ(0, result[i][j]); + } + } +} + +TEST(TestMatrixLib, test_sub_to_int_matrixes_with_different_rows) { + // Arrange + Matrix matrix1{ { 1, 2, 3 }, { 4, 5, 6 } }; + Matrix matrix2{ { 1, 2, 3 }, { 4, 5, 6 }, { 4, 5, 6 } }; + + // Act & Assert + ASSERT_ANY_THROW(matrix1 - matrix2); +} + +TEST(TestMatrixLib, test_sub_to_int_matrixes_with_different_cols) { + // Arrange + Matrix matrix1{ { 1, 2, 3 }, { 4, 5, 6 } }; + Matrix matrix2{ { 1, 2, 3, 1 }, { 4, 5, 6, 1 } }; + + // Act & Assert + ASSERT_ANY_THROW(matrix1 - matrix2); } \ No newline at end of file From b7da4b8dab8d970a315d386e4728f33f9c8964fe Mon Sep 17 00:00:00 2001 From: Kate-Nits Date: Thu, 2 Oct 2025 02:39:26 +0300 Subject: [PATCH 63/94] Wrote operator * for class Matrix and added tests on it --- lib_matrix/matrix.h | 40 ++++++++++++++++++++++------- tests/test_matrix.cpp | 59 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 90 insertions(+), 9 deletions(-) diff --git a/lib_matrix/matrix.h b/lib_matrix/matrix.h index 1806017b..346b38ce 100644 --- a/lib_matrix/matrix.h +++ b/lib_matrix/matrix.h @@ -139,16 +139,38 @@ class Matrix : public MathVector> { } return result; } - - /* - Matrix operator*(const Matrix& other) const { - return Matrix(); + Matrix operator*(const T scalar) const { + Matrix result(_rows, _cols); + for (size_t i = 0; i < _rows; ++i) { + for (size_t j = 0; j < _cols; ++j) { + result[i][j] = this->at(i, j) * scalar; + } + } + return result; } - Matrix operator*(const T value) const { - return Matrix(); + MathVector operator*(const MathVector& vec) const { + if (_cols != vec.size()) { + throw std::invalid_argument("Size of vector should have same size of cols for mult"); + } + MathVector result(_rows); + for (size_t i = 0; i < _rows; ++i) { + result[i] = this->data(i) * vec; + } + return result; } - Matrix operator*(const Vector& vec) const { - return Matrix(); + Matrix operator*(const Matrix& other) const { + if (_cols != other.rows()) { + throw std::invalid_argument("Cols of the first matrix should be equal rows of the second matrix for mult"); + } + Matrix result(_rows, other.cols()); + Matrix transport_other = other; + transport_other.transpose(); + for (size_t i = 0; i < _rows; ++i) { + for (size_t j = 0; j < transport_other.rows(); ++j) { + result[i][j] = (*this)[i] * transport_other[j]; + } + } + return result; } void resize(size_t newRows, size_t newCols) { // ? in_development(); @@ -156,7 +178,7 @@ class Matrix : public MathVector> { _cols = newCols; } void print() const { in_development(); } - friend std::ostream& operator<< <>(std::ostream& out, const TMatrix& matrix);*/ + friend std::ostream& operator<< <>(std::ostream& out, const TMatrix& matrix); }; #endif // LIB_MATRIX_MATRIX_H \ No newline at end of file diff --git a/tests/test_matrix.cpp b/tests/test_matrix.cpp index 41d3891d..6efbbdf2 100644 --- a/tests/test_matrix.cpp +++ b/tests/test_matrix.cpp @@ -248,4 +248,63 @@ TEST(TestMatrixLib, test_sub_to_int_matrixes_with_different_cols) { // Act & Assert ASSERT_ANY_THROW(matrix1 - matrix2); +} + +TEST(TestMatrixLib, test_mult_a_matrix_by_a_scalar) { + // Arrange + Matrix matrix1{ { 1, 2, 3 }, { 4, 5, 6 } }; + int number = 1; + + // Act + Matrix result = matrix1 * 2; + + // Assert + EXPECT_EQ(2, result.rows()); + EXPECT_EQ(3, result.cols()); + for (size_t i = 0; i < result.rows(); ++i) { + for (size_t j = 0; j < result.cols(); ++j) { + EXPECT_EQ(number*2, result[i][j]); + number++; + } + } +} + +TEST(TestMatrixLib, test_mult_a_matrix_by_a_vector) { + // Arrange + Matrix matrix{ { 1, 2 }, { 3, 4 }, { 5, 6 } }; + MathVector vector{ 10, 20 }; + + // Act + MathVector result = matrix * vector; + + // Assert + EXPECT_EQ(50, result[0]); + EXPECT_EQ(110, result[1]); + EXPECT_EQ(170, result[2]); +} + +TEST(TestMatrixLib, test_mult_a_int_matrix_by_a_int_matrix_with_correct_size) { + // Arrange + Matrix A{ {1, 2}, {3, 4} }; + Matrix B{ {2, 0}, {1, 2} }; + + // Act + Matrix C = A * B; + + // Assert + EXPECT_EQ(C.rows(), 2); + EXPECT_EQ(C.cols(), 2); + EXPECT_EQ(1 * 2 + 2 * 1, C.at(0, 0)); + EXPECT_EQ(1 * 0 + 2 * 2, C.at(0, 1)); + EXPECT_EQ(3 * 2 + 4 * 1, C.at(1, 0)); + EXPECT_EQ(3 * 0 + 4 * 2, C.at(1, 1)); +} + +TEST(TestMatrixLib, test_mult_a_int_matrix_by_a_int_matrix_without_correct_size) { + // Arrange + Matrix A{ {1, 2}, {3, 4} }; + Matrix B{ {2, 0}, {1, 2}, {3, 4} }; + + // Act & Assert + ASSERT_ANY_THROW(A * B); } \ No newline at end of file From 6f71b133523c9b224b06bf064fcd03d28d86e404 Mon Sep 17 00:00:00 2001 From: Kate-Nits Date: Thu, 2 Oct 2025 03:14:44 +0300 Subject: [PATCH 64/94] Wrote print() and << for class Matrix. Done wrote all functions and metods of class Matrix --- lib_matrix/matrix.h | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/lib_matrix/matrix.h b/lib_matrix/matrix.h index 346b38ce..c5a20873 100644 --- a/lib_matrix/matrix.h +++ b/lib_matrix/matrix.h @@ -172,13 +172,26 @@ class Matrix : public MathVector> { } return result; } - void resize(size_t newRows, size_t newCols) { // ? - in_development(); - _rows = newRows; - _cols = newCols; + void print() const { + for (size_t i = 0; i < _rows; ++i) { + std::cout << "[ "; + for (size_t j = 0; j < _cols; ++j) { + std::cout << this->at(i, j) << " "; + } + std::cout << "]" << std::endl;; + } } - void print() const { in_development(); } - friend std::ostream& operator<< <>(std::ostream& out, const TMatrix& matrix); + friend std::ostream& operator<< <>(std::ostream& out, const Matrix& matrix); }; - +template +std::ostream& operator<< <>(std::ostream& out, const Matrix& matrix) { + for (size_t i = 0; i < matrix.rows(); ++i) { + out << "[ "; + for (size_t j = 0; j < matrix.cols(); ++j) { + out << matrix.at(i, j) << " "; + } + out << "]\n"; + } + return out; +} #endif // LIB_MATRIX_MATRIX_H \ No newline at end of file From 63a50c854797d929f038451c175305cf129aeb70 Mon Sep 17 00:00:00 2001 From: Kate-Nits Date: Sat, 4 Oct 2025 00:36:23 +0300 Subject: [PATCH 65/94] Finished writing the class Matrix and wrote all the test for it --- lib_mathvector/MathVector.h | 2 +- lib_matrix/matrix.h | 3 +- tests/test_matrix.cpp | 67 ++++++++++++++++++++++++++++++++++--- 3 files changed, 65 insertions(+), 7 deletions(-) diff --git a/lib_mathvector/MathVector.h b/lib_mathvector/MathVector.h index 6afd0b3c..d20b8d6c 100644 --- a/lib_mathvector/MathVector.h +++ b/lib_mathvector/MathVector.h @@ -33,7 +33,7 @@ class MathVector : public TVector { template MathVector(const MathVector& other) { this->_size = other.size(); - this->_capacity = this->size()+ TVector::RESERVE_MEMORY; + this->_capacity = this->size() + TVector::RESERVE_MEMORY; this->_deleted = 0; this->_data = new T[this->_capacity]; this->_states = new State[this->_capacity]; diff --git a/lib_matrix/matrix.h b/lib_matrix/matrix.h index c5a20873..386701eb 100644 --- a/lib_matrix/matrix.h +++ b/lib_matrix/matrix.h @@ -87,7 +87,8 @@ class Matrix : public MathVector> { return this->_data[row][col]; } MathVector& operator[](size_t row) { return this->_data[row]; } - const MathVector& operator[](size_t row) const { return this->_data[row]; } // [] TVector + const MathVector& operator[](size_t row) const { return this->_data[row]; } + // [] TVector Matrix& operator=(const Matrix& other) { if (this != &other) { diff --git a/tests/test_matrix.cpp b/tests/test_matrix.cpp index 6efbbdf2..5490f318 100644 --- a/tests/test_matrix.cpp +++ b/tests/test_matrix.cpp @@ -92,6 +92,15 @@ TEST(TestMatrixLib, constructor_with_initializer_list_without_vectors_with_int) } } +TEST(TestMatrixLib, constructor_with_empty_initializer_list) { + // Arrange & Act + Matrix matrix{}; + + // Assert + EXPECT_EQ(0, matrix.rows()); + EXPECT_EQ(0, matrix.cols()); +} + TEST(TestMatrixLib, copy_constructor) { // Arrange int number = 1; @@ -162,7 +171,7 @@ TEST(TestMatrixLib, test_operator_equal) { TEST(TestMatrixLib, test_transpose) { // Arrange int number = 1; - Matrix matrix{ { 1, 2, 3 }, { 4, 5, 6 }}; + Matrix matrix{ { 1, 2, 3 }, { 4, 5, 6 } }; // Act matrix.transpose(); @@ -176,6 +185,19 @@ TEST(TestMatrixLib, test_transpose) { EXPECT_EQ(6, matrix[2][1]); } +TEST(TestMatrixLib, test_double_transpose_returns_original) { + // Arrange + Matrix matrix{ { 1, 2, 3 }, { 4, 5, 6 } }; + Matrix original = matrix; + + // Act + matrix.transpose(); + matrix.transpose(); + + // Assert + EXPECT_TRUE(matrix == original); +} + TEST(TestMatrixLib, test_add_to_int_matrixes) { // Arrange Matrix matrix1{ { 1, 2, 3 }, { 4, 5, 6 } }; @@ -263,12 +285,30 @@ TEST(TestMatrixLib, test_mult_a_matrix_by_a_scalar) { EXPECT_EQ(3, result.cols()); for (size_t i = 0; i < result.rows(); ++i) { for (size_t j = 0; j < result.cols(); ++j) { - EXPECT_EQ(number*2, result[i][j]); + EXPECT_EQ(number * 2, result[i][j]); number++; } } } +TEST(TestMatrixLib, test_mult_a_matrix_by_a_zero_scalar) { + // Arrange + Matrix matrix1{ { 1, 2, 3 }, { 4, 5, 6 } }; + int number = 1; + + // Act + Matrix result = matrix1 * 0; + + // Assert + EXPECT_EQ(2, result.rows()); + EXPECT_EQ(3, result.cols()); + for (size_t i = 0; i < result.rows(); ++i) { + for (size_t j = 0; j < result.cols(); ++j) { + EXPECT_EQ(0, result[i][j]); + } + } +} + TEST(TestMatrixLib, test_mult_a_matrix_by_a_vector) { // Arrange Matrix matrix{ { 1, 2 }, { 3, 4 }, { 5, 6 } }; @@ -283,7 +323,7 @@ TEST(TestMatrixLib, test_mult_a_matrix_by_a_vector) { EXPECT_EQ(170, result[2]); } -TEST(TestMatrixLib, test_mult_a_int_matrix_by_a_int_matrix_with_correct_size) { +TEST(TestMatrixLib, test_mult_a_int_matrix_by_a_int_matrix_with_correct_equal_size) { // Arrange Matrix A{ {1, 2}, {3, 4} }; Matrix B{ {2, 0}, {1, 2} }; @@ -292,14 +332,31 @@ TEST(TestMatrixLib, test_mult_a_int_matrix_by_a_int_matrix_with_correct_size) { Matrix C = A * B; // Assert - EXPECT_EQ(C.rows(), 2); - EXPECT_EQ(C.cols(), 2); + EXPECT_EQ(2, C.rows()); + EXPECT_EQ(2, C.cols()); EXPECT_EQ(1 * 2 + 2 * 1, C.at(0, 0)); EXPECT_EQ(1 * 0 + 2 * 2, C.at(0, 1)); EXPECT_EQ(3 * 2 + 4 * 1, C.at(1, 0)); EXPECT_EQ(3 * 0 + 4 * 2, C.at(1, 1)); } +TEST(TestMatrixLib, test_mult_a_int_matrix_by_a_int_matrix_with_correct_not_equal_size) { + // Arrange + Matrix A{ {2, 3, 4}, {1, 2, 0} }; + Matrix B{ {1, 0}, {2, 2}, {1, 4} }; + + // Act + Matrix C = A * B; + + // Assert + EXPECT_EQ(2, C.rows()); + EXPECT_EQ(2, C.cols()); + EXPECT_EQ(12, C.at(0, 0)); + EXPECT_EQ(22, C.at(0, 1)); + EXPECT_EQ(5, C.at(1, 0)); + EXPECT_EQ(4, C.at(1, 1)); +} + TEST(TestMatrixLib, test_mult_a_int_matrix_by_a_int_matrix_without_correct_size) { // Arrange Matrix A{ {1, 2}, {3, 4} }; From 9f2fb661a61ab16846614c17ee6c48382f5903af Mon Sep 17 00:00:00 2001 From: Kate-Nits Date: Sat, 4 Oct 2025 04:12:12 +0300 Subject: [PATCH 66/94] Wrote all constructors for class TriangleMatrix and tests on it. Also wrote at --- lib_matrix/matrix.h | 1 + lib_triangle_matrix/triangle_matrix.h | 63 ++++++++++++++++++++------- tests/test_triangle_matrix.cpp | 61 ++++++++++++++++++++++++++ 3 files changed, 110 insertions(+), 15 deletions(-) diff --git a/lib_matrix/matrix.h b/lib_matrix/matrix.h index 386701eb..baf112bb 100644 --- a/lib_matrix/matrix.h +++ b/lib_matrix/matrix.h @@ -12,6 +12,7 @@ class MathVector; template class Matrix : public MathVector> { +protected: size_t _rows; // size_t _cols; // public: diff --git a/lib_triangle_matrix/triangle_matrix.h b/lib_triangle_matrix/triangle_matrix.h index cada60b9..cb870dd8 100644 --- a/lib_triangle_matrix/triangle_matrix.h +++ b/lib_triangle_matrix/triangle_matrix.h @@ -5,25 +5,59 @@ #include #include - +template +class Matrix; #include "../lib_matrix/matrix.h" -/* template -class TriangleMatrix : public TMatrix { +class TriangleMatrix : public Matrix { size_t _n; public: - TriangleMatrix() noexcept = default; - TriangleMatrix(std::size_t n) { - in_development(); - _n = n; - } + TriangleMatrix() : Matrix(), _n(0) {} + explicit TriangleMatrix(size_t n) : Matrix(n, n), _n(n) { + this->_rows = n; + this->_cols = n; + this->_size = n; + this->_capacity = n + TVector::RESERVE_MEMORY; + this->_deleted = 0; + this->_data = new MathVector[this->_capacity]; + this->_states = new State[this->_capacity]; + + for (size_t i = 0; i < n; i++) { + this->_data[i] = MathVector(n - i); + this->_states[i] = busy; + for (size_t j = 0; j < n - i; ++j) { + this->_data[i][j] = T{}; + } + } + for (size_t i = n; i < this->_capacity; i++) { + this->_states[i] = empty; + } - TriangleMatrix(const TriangleMatrix& other) = default; - TriangleMatrix(TriangleMatrix&& other) noexcept = default; + } + TriangleMatrix(const TriangleMatrix& other) : Matrix(other), _n(other._n) {} ~TriangleMatrix() = default; - - TriangleMatrix& operator=(const TriangleMatrix& other) noexcept { + T& at(size_t i, size_t j) { + if (i >= _n || j >= _n) { + throw std::out_of_range("Index out of range"); + } + if (i > j) { + static T zero{}; + return zero; + } + return this->_data[i][j - i]; + } + const T& at(size_t i, size_t j) const { + if (i >= _n || j >= _n) { + throw std::out_of_range("Index out of range"); + } + if (i > j) { + static const T zero{}; + return zero; + } + return this->_data[i][j - i]; + } + /*TriangleMatrix& operator=(const TriangleMatrix& other) noexcept { in_development(); return *this; } @@ -56,9 +90,9 @@ class TriangleMatrix : public TMatrix { in_development(); } - friend std::ostream& operator<< <>(std::ostream& out, const TriangleMatrix& matrix); + friend std::ostream& operator<< <>(std::ostream& out, const TriangleMatrix& matrix);*/ }; - +/* template std::ostream& operator<<(std::ostream& out, const TriangleMatrix& matrix) { in_development(); @@ -67,5 +101,4 @@ std::ostream& operator<<(std::ostream& out, const TriangleMatrix& matrix) { } */ - #endif // TRIANGLE_MATRIX_TRIANGLE_MATRIX_X \ No newline at end of file diff --git a/tests/test_triangle_matrix.cpp b/tests/test_triangle_matrix.cpp index e69de29b..8e4dca7b 100644 --- a/tests/test_triangle_matrix.cpp +++ b/tests/test_triangle_matrix.cpp @@ -0,0 +1,61 @@ +// Copyright 2025 Ekaterina Ushnitskaya + +#include +#include "../lib_triangle_matrix/triangle_matrix.h" + +#define EPSILON 0.000001 +#define TRUE 1 +#define FALSE 0 + +TEST(TestTriangleMatrixLib, default_constructor) { + // Arrange & Act + TriangleMatrix m; + + // Assert + EXPECT_EQ(0, m.rows()); + EXPECT_EQ(0, m.cols()); +} + +TEST(TestTriangleMatrixLib, constructor_for_int_matrix_with_size) { + // Arrange & Act + TriangleMatrix m(4); + + // Assert + EXPECT_EQ(4, m.rows()); + EXPECT_EQ(4, m.cols()); + for (size_t i = 0; i < m.rows(); ++i) { + for (size_t j = 0; j < m.cols(); ++j) { + EXPECT_EQ(0, m.at(i, j)); + } + } +} + +TEST(TestTriangleMatrixLib, constructor_for_double_matrix_with_size) { + // Arrange & Act + TriangleMatrix m(5); + + // Assert + EXPECT_EQ(5, m.rows()); + EXPECT_EQ(5, m.cols()); + for (size_t i = 0; i < m.rows(); ++i) { + for (size_t j = 0; j < m.cols(); ++j) { + EXPECT_EQ(0.0, m.at(i, j)); + } + } +} + +TEST(TestTriangleMatrixLib, constructor_copy) { + // Arrange + TriangleMatrix matrix1(3); + matrix1.at(0, 0) = 1; + matrix1.at(0, 1) = 2; + + // Act + TriangleMatrix matrix2(matrix1); + + // Assert + EXPECT_EQ(3, matrix2.rows()); + EXPECT_EQ(3, matrix2.cols()); + EXPECT_EQ(1, matrix2.at(0, 0)); + EXPECT_EQ(2, matrix2.at(0, 1)); +} \ No newline at end of file From 4ff9a42d99088c360974fe9e0da846f749a64247 Mon Sep 17 00:00:00 2001 From: Kate-Nits Date: Sun, 5 Oct 2025 16:58:19 +0300 Subject: [PATCH 67/94] Wrote operator + and - for the class TriangleMatrix and added tests on it --- lib_triangle_matrix/triangle_matrix.h | 61 ++++++++++++++++++--------- tests/test_triangle_matrix.cpp | 44 +++++++++++++++++++ 2 files changed, 84 insertions(+), 21 deletions(-) diff --git a/lib_triangle_matrix/triangle_matrix.h b/lib_triangle_matrix/triangle_matrix.h index cb870dd8..517d7468 100644 --- a/lib_triangle_matrix/triangle_matrix.h +++ b/lib_triangle_matrix/triangle_matrix.h @@ -37,6 +37,7 @@ class TriangleMatrix : public Matrix { } TriangleMatrix(const TriangleMatrix& other) : Matrix(other), _n(other._n) {} ~TriangleMatrix() = default; + size_t n() const noexcept { return _n; } T& at(size_t i, size_t j) { if (i >= _n || j >= _n) { throw std::out_of_range("Index out of range"); @@ -57,39 +58,57 @@ class TriangleMatrix : public Matrix { } return this->_data[i][j - i]; } - /*TriangleMatrix& operator=(const TriangleMatrix& other) noexcept { - in_development(); - return *this; + MathVector& operator[](size_t i) { + if (i >= _n) { + throw std::out_of_range("Index out of range"); + } + return this->_data[i]; + } + const MathVector& operator[](size_t row) const { + if (i >= _n) { + throw std::out_of_range("Index out of range"); + } + return this->_data[i]; } - TriangleMatrix& operator=(TriangleMatrix&& other) noexcept { - in_development(); + TriangleMatrix& operator=(const TriangleMatrix& other) noexcept { + if (this != &other) { + Matrix::operator=(other); + _n = other._n; + } return *this; } - TriangleMatrix add_matrices(const TriangleMatrix& other) const { - in_development(); - return TriangleMatrix(); + TriangleMatrix operator+(const TriangleMatrix& other) const { + if (_n != other._n) { + throw std::invalid_argument("Triangle Matrix should have the same size for addition"); + } + TriangleMatrix result(_n); + for (size_t i = 0; i < _n; ++i) { + for (size_t j = 0; j < _n; ++j) { + result.at(i, j) = this->at(i, j) + other.at(i, j); + } + } + return result; } - - TriangleMatrix subtract_matrices(const TriangleMatrix& other) const { - in_development(); - return TriangleMatrix(); + TriangleMatrix operator-(const TriangleMatrix& other) const { + if (_n != other._n) { + throw std::invalid_argument("Triangle Matrix should have the same size for subtraction"); + } + TriangleMatrix result(_n); + for (size_t i = 0; i < _n; ++i) { + for (size_t j = 0; j < _n; ++j) { + result.at(i, j) = this->at(i, j) - other.at(i, j); + } + } + return result; } + /* TriangleMatrix multiply_matrices(const TriangleMatrix& other) const { in_development(); return TriangleMatrix(); } - - MathVector& operator[](size_t other) { - in_development(); - } - - const MathVector& operator[](size_t row) const { - in_development(); - } - friend std::ostream& operator<< <>(std::ostream& out, const TriangleMatrix& matrix);*/ }; /* diff --git a/tests/test_triangle_matrix.cpp b/tests/test_triangle_matrix.cpp index 8e4dca7b..e786114a 100644 --- a/tests/test_triangle_matrix.cpp +++ b/tests/test_triangle_matrix.cpp @@ -58,4 +58,48 @@ TEST(TestTriangleMatrixLib, constructor_copy) { EXPECT_EQ(3, matrix2.cols()); EXPECT_EQ(1, matrix2.at(0, 0)); EXPECT_EQ(2, matrix2.at(0, 1)); +} + +TEST(TestTriangleMatrixLib, test_addition_int_triangle_matrix) { + // Arrange + TriangleMatrix a(3), b(3); + int number = 1; + a.at(0, 0) = 1; a.at(0, 1) = 2; a.at(0, 2) = 3; a.at(1, 1) = 4; a.at(1, 2) = 5; a.at(2, 2) = 6; + b.at(0, 0) = 7; b.at(0, 1) = 8; b.at(0, 2) = 9; b.at(1, 1) = 10; b.at(1, 2) = 11; b.at(2, 2) = 12; + + // Act + TriangleMatrix c = a + b; + + // Assert + EXPECT_EQ(8, c.at(0, 0)); + EXPECT_EQ(10, c.at(0, 1)); + EXPECT_EQ(12, c.at(0, 2)); + EXPECT_EQ(0, c.at(1, 0)); + EXPECT_EQ(14, c.at(1, 1)); + EXPECT_EQ(16, c.at(1, 2)); + EXPECT_EQ(0, c.at(2, 0)); + EXPECT_EQ(0, c.at(2, 1)); + EXPECT_EQ(18, c.at(2, 2)); +} + +TEST(TestTriangleMatrixLib, test_subtraction_int_triangle_matrix) { + // Arrange + TriangleMatrix a(3), b(3); + int number = 1; + a.at(0, 0) = 1; a.at(0, 1) = 2; a.at(0, 2) = 3; a.at(1, 1) = 4; a.at(1, 2) = 5; a.at(2, 2) = 6; + b.at(0, 0) = 7; b.at(0, 1) = 8; b.at(0, 2) = 9; b.at(1, 1) = 10; b.at(1, 2) = 11; b.at(2, 2) = 12; + + // Act + TriangleMatrix c = b - a; + + // Assert + EXPECT_EQ(6, c.at(0, 0)); + EXPECT_EQ(6, c.at(0, 1)); + EXPECT_EQ(6, c.at(0, 2)); + EXPECT_EQ(0, c.at(1, 0)); + EXPECT_EQ(6, c.at(1, 1)); + EXPECT_EQ(6, c.at(1, 2)); + EXPECT_EQ(0, c.at(2, 0)); + EXPECT_EQ(0, c.at(2, 1)); + EXPECT_EQ(6, c.at(2, 2)); } \ No newline at end of file From 811439f5714d8b17f38a766ae1022b6672dcba19 Mon Sep 17 00:00:00 2001 From: Kate-Nits Date: Sun, 5 Oct 2025 17:04:29 +0300 Subject: [PATCH 68/94] Added test on throw for operator + and - for the class TriangleMatrix --- tests/test_triangle_matrix.cpp | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/tests/test_triangle_matrix.cpp b/tests/test_triangle_matrix.cpp index e786114a..b118d5c2 100644 --- a/tests/test_triangle_matrix.cpp +++ b/tests/test_triangle_matrix.cpp @@ -60,10 +60,9 @@ TEST(TestTriangleMatrixLib, constructor_copy) { EXPECT_EQ(2, matrix2.at(0, 1)); } -TEST(TestTriangleMatrixLib, test_addition_int_triangle_matrix) { +TEST(TestTriangleMatrixLib, test_addition_int_triangle_matrix_with_the_same_size) { // Arrange TriangleMatrix a(3), b(3); - int number = 1; a.at(0, 0) = 1; a.at(0, 1) = 2; a.at(0, 2) = 3; a.at(1, 1) = 4; a.at(1, 2) = 5; a.at(2, 2) = 6; b.at(0, 0) = 7; b.at(0, 1) = 8; b.at(0, 2) = 9; b.at(1, 1) = 10; b.at(1, 2) = 11; b.at(2, 2) = 12; @@ -82,7 +81,15 @@ TEST(TestTriangleMatrixLib, test_addition_int_triangle_matrix) { EXPECT_EQ(18, c.at(2, 2)); } -TEST(TestTriangleMatrixLib, test_subtraction_int_triangle_matrix) { +TEST(TestTriangleMatrixLib, test_addition_int_triangle_matrix_without_the_same_size) { + // Arrange + TriangleMatrix a(3), b(4); + + // Act & Assert + EXPECT_ANY_THROW(a + b); +} + +TEST(TestTriangleMatrixLib, test_subtraction_int_triangle_matrix_with_the_same_size) { // Arrange TriangleMatrix a(3), b(3); int number = 1; @@ -102,4 +109,12 @@ TEST(TestTriangleMatrixLib, test_subtraction_int_triangle_matrix) { EXPECT_EQ(0, c.at(2, 0)); EXPECT_EQ(0, c.at(2, 1)); EXPECT_EQ(6, c.at(2, 2)); +} + +TEST(TestTriangleMatrixLib, test_subtraction_int_triangle_matrix_without_the_same_size) { + // Arrange + TriangleMatrix a(4), b(3); + + // Act & Assert + EXPECT_ANY_THROW(a - b); } \ No newline at end of file From 46cc11d3fe5836bd014b96429d1fda98e2b50d52 Mon Sep 17 00:00:00 2001 From: Kate-Nits Date: Sun, 5 Oct 2025 18:42:38 +0300 Subject: [PATCH 69/94] Wrote operator * on scalar, vector and TriangleMatrix for the class TriangleMatrix and added tests on it --- lib_triangle_matrix/triangle_matrix.h | 51 +++++++++++++++--- tests/test_triangle_matrix.cpp | 78 +++++++++++++++++++++++++-- 2 files changed, 119 insertions(+), 10 deletions(-) diff --git a/lib_triangle_matrix/triangle_matrix.h b/lib_triangle_matrix/triangle_matrix.h index 517d7468..e327715c 100644 --- a/lib_triangle_matrix/triangle_matrix.h +++ b/lib_triangle_matrix/triangle_matrix.h @@ -85,7 +85,7 @@ class TriangleMatrix : public Matrix { } TriangleMatrix result(_n); for (size_t i = 0; i < _n; ++i) { - for (size_t j = 0; j < _n; ++j) { + for (size_t j = i; j < _n; ++j) { result.at(i, j) = this->at(i, j) + other.at(i, j); } } @@ -97,18 +97,55 @@ class TriangleMatrix : public Matrix { } TriangleMatrix result(_n); for (size_t i = 0; i < _n; ++i) { - for (size_t j = 0; j < _n; ++j) { + for (size_t j = i; j < _n; ++j) { result.at(i, j) = this->at(i, j) - other.at(i, j); } } return result; } - - /* - TriangleMatrix multiply_matrices(const TriangleMatrix& other) const { - in_development(); - return TriangleMatrix(); + TriangleMatrix operator*(const T scalar) const { + TriangleMatrix result(_n); + for (size_t i = 0; i < _n; ++i) { + for (size_t j = i; j < _n; ++j) { + result.at(i, j) = this->at(i, j) * scalar; + } + } + return result; + } + MathVector operator*(const MathVector& vec) const { + if (_n != vec.size()) { + throw std::invalid_argument("Size of vector should have same size of cols for mult"); + } + MathVector result(_n); + for (size_t i = 0; i < _n; ++i) { + for (size_t j = i; j < _n; ++j) { + T sum{}; + for (size_t k = i; k <= j; ++k) { + sum += this->at(i, k) * vec.at(k); + } + result[i] = sum; + } + } + return result; } + TriangleMatrix operator*(const TriangleMatrix& other) const { + if (_n != other._n) { + throw std::invalid_argument("Triangle Matrix should have the same size for mult"); + } + TriangleMatrix result(_n); + for (size_t i = 0; i < _n; ++i) { + for (size_t j = i; j < _n; ++j) { + T sum{}; + for (size_t k = i; k <= j; ++k) { + sum += this->at(i, k) * other.at(k, j); + } + result.at(i, j) = sum; + } + } + return result; + } + /* + friend std::ostream& operator<< <>(std::ostream& out, const TriangleMatrix& matrix);*/ }; /* diff --git a/tests/test_triangle_matrix.cpp b/tests/test_triangle_matrix.cpp index b118d5c2..a50dd3b2 100644 --- a/tests/test_triangle_matrix.cpp +++ b/tests/test_triangle_matrix.cpp @@ -86,13 +86,12 @@ TEST(TestTriangleMatrixLib, test_addition_int_triangle_matrix_without_the_same_s TriangleMatrix a(3), b(4); // Act & Assert - EXPECT_ANY_THROW(a + b); + ASSERT_ANY_THROW(a + b); } TEST(TestTriangleMatrixLib, test_subtraction_int_triangle_matrix_with_the_same_size) { // Arrange TriangleMatrix a(3), b(3); - int number = 1; a.at(0, 0) = 1; a.at(0, 1) = 2; a.at(0, 2) = 3; a.at(1, 1) = 4; a.at(1, 2) = 5; a.at(2, 2) = 6; b.at(0, 0) = 7; b.at(0, 1) = 8; b.at(0, 2) = 9; b.at(1, 1) = 10; b.at(1, 2) = 11; b.at(2, 2) = 12; @@ -116,5 +115,78 @@ TEST(TestTriangleMatrixLib, test_subtraction_int_triangle_matrix_without_the_sam TriangleMatrix a(4), b(3); // Act & Assert - EXPECT_ANY_THROW(a - b); + ASSERT_ANY_THROW(a - b); +} + +TEST(TestTriangleMatrixLib, test_mult_a_int_TriangleMatrix_by_a_scalar) { + // Arrange + TriangleMatrix A(3); + A.at(0, 0) = 1; A.at(0, 1) = 2; A.at(0, 2) = 3; A.at(1, 1) = 4; A.at(1, 2) = 5; A.at(2, 2) = 6; + + // Act + TriangleMatrix C = A * 2; + + // Assert + EXPECT_EQ(2, C.at(0, 0)); + EXPECT_EQ(4, C.at(0, 1)); + EXPECT_EQ(6, C.at(0, 2)); + EXPECT_EQ(0, C.at(1, 0)); + EXPECT_EQ(8, C.at(1, 1)); + EXPECT_EQ(10, C.at(1, 2)); + EXPECT_EQ(0, C.at(2, 0)); + EXPECT_EQ(0, C.at(2, 1)); + EXPECT_EQ(12, C.at(2, 2)); +} + +TEST(TestTriangleMatrixLib, test_mult_a_int_TriangleMatrix_by_a_vector_with_correct_equal_size) { + // Arrange + TriangleMatrix A(3); + A.at(0, 0) = 1; A.at(0, 1) = 2; A.at(0, 2) = 3; A.at(1, 1) = 4; A.at(1, 2) = 5; A.at(2, 2) = 6; + MathVector vec{ 7, 8, 9 }; + + // Act + MathVector C = A * vec; + + // Assert + EXPECT_EQ(50, C.at(0)); + EXPECT_EQ(77, C.at(1)); + EXPECT_EQ(54, C.at(2)); +} + +TEST(TestTriangleMatrixLib, test_mult_a_int_TriangleMatrix_by_a_vector_without_correct_equal_size) { + // Arrange + TriangleMatrix A(3); + MathVector vec(4); + + // Act & Assert + ASSERT_ANY_THROW(A * vec); +} + +TEST(TestTriangleMatrixLib, test_mult_a_int_TriangleMatrix_by_a_int_TriangleMatrix_with_correct_equal_size) { + // Arrange + TriangleMatrix A(3), B(3); + A.at(0, 0) = 1; A.at(0, 1) = 2; A.at(0, 2) = 3; A.at(1, 1) = 4; A.at(1, 2) = 5; A.at(2, 2) = 6; + B.at(0, 0) = 7; B.at(0, 1) = 8; B.at(0, 2) = 9; B.at(1, 1) = 10; B.at(1, 2) = 11; B.at(2, 2) = 12; + + // Act + TriangleMatrix C = A * B; + + // Assert + EXPECT_EQ(7, C.at(0, 0)); + EXPECT_EQ(28, C.at(0, 1)); + EXPECT_EQ(67, C.at(0, 2)); + EXPECT_EQ(0, C.at(1, 0)); + EXPECT_EQ(40, C.at(1, 1)); + EXPECT_EQ(104, C.at(1, 2)); + EXPECT_EQ(0, C.at(2, 0)); + EXPECT_EQ(0, C.at(2, 1)); + EXPECT_EQ(72, C.at(2, 2)); +} + +TEST(TestTriangleMatrixLib, test_mult_a_int_TriangleMatrix_by_a_int_TriangleMatrix_without_correct_equal_size) { + // Arrange + TriangleMatrix A(3), B(4); + + // Act & Assert + ASSERT_ANY_THROW(A * B); } \ No newline at end of file From ab730f2f8aafbc57e72d402b01976f9c3fcbb3c3 Mon Sep 17 00:00:00 2001 From: Kate-Nits Date: Sun, 5 Oct 2025 23:20:00 +0300 Subject: [PATCH 70/94] Wrote operator << <> for the class TriangleMatrix, also wrote operator + on Matrix and TriangleMatrix and on TriangleMatrix and Matrix and added tests on it --- lib_triangle_matrix/triangle_matrix.h | 42 ++++++++++++++++++++++----- tests/test_triangle_matrix.cpp | 42 +++++++++++++++++++++++++++ 2 files changed, 77 insertions(+), 7 deletions(-) diff --git a/lib_triangle_matrix/triangle_matrix.h b/lib_triangle_matrix/triangle_matrix.h index e327715c..df50e9da 100644 --- a/lib_triangle_matrix/triangle_matrix.h +++ b/lib_triangle_matrix/triangle_matrix.h @@ -144,17 +144,45 @@ class TriangleMatrix : public Matrix { } return result; } - /* - friend std::ostream& operator<< <>(std::ostream& out, const TriangleMatrix& matrix);*/ + friend std::ostream& operator<< <>(std::ostream& out, const TriangleMatrix& matrix); }; -/* template -std::ostream& operator<<(std::ostream& out, const TriangleMatrix& matrix) { - in_development(); - out << " "; +std::ostream& operator<< <>(std::ostream& out, const TriangleMatrix& matrix) { + for (size_t i = 0; i < matrix.n(); ++i) { + out << "[ "; + for (size_t j = 0; i < matrix.n(); ++j) { + out << matrix.at(i, j) << " "; + } + out << "]\n"; + } return out; } -*/ +template +Matrix operator+(const Matrix& matrix, const TriangleMatrix& triangle_matrix) { + if (matrix.rows() != triangle_matrix.n() || matrix.cols() != triangle_matrix.n()) { + throw std::invalid_argument("Matrixes should have the same size for addition"); + } + Matrix result(matrix.rows(), matrix.cols()); + for (size_t i = 0; i < matrix.rows(); ++i) { + for (size_t j = 0; j < matrix.cols(); ++j) { + result[i][j] = matrix.at(i, j) + triangle_matrix.at(i, j); + } + } + return result; +} +template +Matrix operator+(const TriangleMatrix& triangle_matrix, const Matrix& matrix) { + if (matrix.rows() != triangle_matrix.n() || matrix.cols() != triangle_matrix.n()) { + throw std::invalid_argument("Matrixes should have the same size for addition"); + } + Matrix result(matrix.rows(), matrix.cols()); + for (size_t i = 0; i < matrix.rows(); ++i) { + for (size_t j = 0; j < matrix.cols(); ++j) { + result[i][j] = matrix.at(i, j) + triangle_matrix.at(i, j); + } + } + return result; +} #endif // TRIANGLE_MATRIX_TRIANGLE_MATRIX_X \ No newline at end of file diff --git a/tests/test_triangle_matrix.cpp b/tests/test_triangle_matrix.cpp index a50dd3b2..645b79da 100644 --- a/tests/test_triangle_matrix.cpp +++ b/tests/test_triangle_matrix.cpp @@ -189,4 +189,46 @@ TEST(TestTriangleMatrixLib, test_mult_a_int_TriangleMatrix_by_a_int_TriangleMatr // Act & Assert ASSERT_ANY_THROW(A * B); +} + +TEST(TestTriangleMatrixLib, test_addition_int_matrix_and_int_triangle_matrix_with_the_same_size) { + // Arrange + TriangleMatrix a(3); + a.at(0, 0) = 1; a.at(0, 1) = 2; a.at(0, 2) = 3; a.at(1, 1) = 4; a.at(1, 2) = 5; a.at(2, 2) = 6; + Matrix b{ {1, 1, 1}, {1, 1, 1}, {1, 1, 1} }; + + // Act + Matrix c = b + a; + + // Assert + EXPECT_EQ(2, c.at(0, 0)); + EXPECT_EQ(3, c.at(0, 1)); + EXPECT_EQ(4, c.at(0, 2)); + EXPECT_EQ(1, c.at(1, 0)); + EXPECT_EQ(5, c.at(1, 1)); + EXPECT_EQ(6, c.at(1, 2)); + EXPECT_EQ(1, c.at(2, 0)); + EXPECT_EQ(1, c.at(2, 1)); + EXPECT_EQ(7, c.at(2, 2)); +} + +TEST(TestTriangleMatrixLib, test_addition_int_triangle_matrix_and_int_matrix_with_the_same_size) { + // Arrange + TriangleMatrix a(3); + a.at(0, 0) = 1; a.at(0, 1) = 2; a.at(0, 2) = 3; a.at(1, 1) = 4; a.at(1, 2) = 5; a.at(2, 2) = 6; + Matrix b{ {1, 1, 1}, {1, 1, 1}, {1, 1, 1} }; + + // Act + Matrix c = a + b; + + // Assert + EXPECT_EQ(2, c.at(0, 0)); + EXPECT_EQ(3, c.at(0, 1)); + EXPECT_EQ(4, c.at(0, 2)); + EXPECT_EQ(1, c.at(1, 0)); + EXPECT_EQ(5, c.at(1, 1)); + EXPECT_EQ(6, c.at(1, 2)); + EXPECT_EQ(1, c.at(2, 0)); + EXPECT_EQ(1, c.at(2, 1)); + EXPECT_EQ(7, c.at(2, 2)); } \ No newline at end of file From 3b916b2ba88d93ecdf4959ac57bb1108fa9bc6f3 Mon Sep 17 00:00:00 2001 From: Kate-Nits Date: Sun, 5 Oct 2025 23:35:09 +0300 Subject: [PATCH 71/94] Wrote operator - on Matrix and TriangleMatrix and on TriangleMatrix and Matrix and added tests on it --- lib_triangle_matrix/triangle_matrix.h | 26 +++++++++ tests/test_triangle_matrix.cpp | 80 +++++++++++++++++++++++++++ 2 files changed, 106 insertions(+) diff --git a/lib_triangle_matrix/triangle_matrix.h b/lib_triangle_matrix/triangle_matrix.h index df50e9da..d0e5e07e 100644 --- a/lib_triangle_matrix/triangle_matrix.h +++ b/lib_triangle_matrix/triangle_matrix.h @@ -185,4 +185,30 @@ Matrix operator+(const TriangleMatrix& triangle_matrix, const Matrix& m } return result; } +template +Matrix operator-(const Matrix& matrix, const TriangleMatrix& triangle_matrix) { + if (matrix.rows() != triangle_matrix.n() || matrix.cols() != triangle_matrix.n()) { + throw std::invalid_argument("Matrixes should have the same size for addition"); + } + Matrix result(matrix.rows(), matrix.cols()); + for (size_t i = 0; i < matrix.rows(); ++i) { + for (size_t j = 0; j < matrix.cols(); ++j) { + result[i][j] = matrix.at(i, j) - triangle_matrix.at(i, j); + } + } + return result; +} +template +Matrix operator-(const TriangleMatrix& triangle_matrix, const Matrix& matrix) { + if (matrix.rows() != triangle_matrix.n() || matrix.cols() != triangle_matrix.n()) { + throw std::invalid_argument("Matrixes should have the same size for addition"); + } + Matrix result(matrix.rows(), matrix.cols()); + for (size_t i = 0; i < matrix.rows(); ++i) { + for (size_t j = 0; j < matrix.cols(); ++j) { + result[i][j] = triangle_matrix.at(i, j) - matrix.at(i, j); + } + } + return result; +} #endif // TRIANGLE_MATRIX_TRIANGLE_MATRIX_X \ No newline at end of file diff --git a/tests/test_triangle_matrix.cpp b/tests/test_triangle_matrix.cpp index 645b79da..6b26bd5e 100644 --- a/tests/test_triangle_matrix.cpp +++ b/tests/test_triangle_matrix.cpp @@ -212,6 +212,16 @@ TEST(TestTriangleMatrixLib, test_addition_int_matrix_and_int_triangle_matrix_wit EXPECT_EQ(7, c.at(2, 2)); } +TEST(TestTriangleMatrixLib, test_addition_int_matrix_and_int_triangle_matrix_without_the_same_size) { + // Arrange + TriangleMatrix a(3); + a.at(0, 0) = 1; a.at(0, 1) = 2; a.at(0, 2) = 3; a.at(1, 1) = 4; a.at(1, 2) = 5; a.at(2, 2) = 6; + Matrix b{ {1, 1, 1}, {1, 1, 1}, {1, 1, 1}, {1, 1, 1} }; + + // Act & Assert + ASSERT_ANY_THROW(b + a); +} + TEST(TestTriangleMatrixLib, test_addition_int_triangle_matrix_and_int_matrix_with_the_same_size) { // Arrange TriangleMatrix a(3); @@ -231,4 +241,74 @@ TEST(TestTriangleMatrixLib, test_addition_int_triangle_matrix_and_int_matrix_wit EXPECT_EQ(1, c.at(2, 0)); EXPECT_EQ(1, c.at(2, 1)); EXPECT_EQ(7, c.at(2, 2)); +} + +TEST(TestTriangleMatrixLib, test_addition_int_triangle_matrix_and_int_matrix_without_the_same_size) { + // Arrange + TriangleMatrix a(3); + a.at(0, 0) = 1; a.at(0, 1) = 2; a.at(0, 2) = 3; a.at(1, 1) = 4; a.at(1, 2) = 5; a.at(2, 2) = 6; + Matrix b{ {1, 1, 1}, {1, 1, 1}, {1, 1, 1}, {1, 1, 1} }; + + // Act & Assert + ASSERT_ANY_THROW(a + b); +} + +TEST(TestTriangleMatrixLib, test_subtraction_int_matrix_and_int_triangle_matrix_with_the_same_size) { + // Arrange + TriangleMatrix a(3); + a.at(0, 0) = 1; a.at(0, 1) = 2; a.at(0, 2) = 3; a.at(1, 1) = 4; a.at(1, 2) = 5; a.at(2, 2) = 6; + Matrix b{ {20, 20, 20}, {20, 20, 20}, {20, 20, 20} }; + + // Act + Matrix c = b - a; + + // Assert + EXPECT_EQ(19, c.at(0, 0)); + EXPECT_EQ(18, c.at(0, 1)); + EXPECT_EQ(17, c.at(0, 2)); + EXPECT_EQ(20, c.at(1, 0)); + EXPECT_EQ(16, c.at(1, 1)); + EXPECT_EQ(15, c.at(1, 2)); + EXPECT_EQ(20, c.at(2, 0)); + EXPECT_EQ(20, c.at(2, 1)); + EXPECT_EQ(14, c.at(2, 2)); +} + +TEST(TestTriangleMatrixLib, test_subtraction_int_matrix_and_int_triangle_matrix_without_the_same_size) { + // Arrange + TriangleMatrix a(4); + Matrix b{ {20, 20, 20}, {20, 20, 20}, {20, 20, 20} }; + + // Act & Assert + ASSERT_ANY_THROW(b - a); +} + +TEST(TestTriangleMatrixLib, test_subtraction_int_triangle_matrix_and_int_matrix_with_the_same_size) { + // Arrange + TriangleMatrix a(3); + a.at(0, 0) = 1; a.at(0, 1) = 2; a.at(0, 2) = 3; a.at(1, 1) = 4; a.at(1, 2) = 5; a.at(2, 2) = 6; + Matrix b{ {20, 20, 20}, {20, 20, 20}, {20, 20, 20} }; + + // Act + Matrix c = a - b; + + // Assert + EXPECT_EQ(-19, c.at(0, 0)); + EXPECT_EQ(-18, c.at(0, 1)); + EXPECT_EQ(-17, c.at(0, 2)); + EXPECT_EQ(-20, c.at(1, 0)); + EXPECT_EQ(-16, c.at(1, 1)); + EXPECT_EQ(-15, c.at(1, 2)); + EXPECT_EQ(-20, c.at(2, 0)); + EXPECT_EQ(-20, c.at(2, 1)); + EXPECT_EQ(-14, c.at(2, 2)); +} + +TEST(TestTriangleMatrixLib, test_subtraction_int_triangle_matrix_and_int_matrix_without_the_same_size) { + // Arrange + TriangleMatrix a(4); + Matrix b{ {20, 20, 20}, {20, 20, 20}, {20, 20, 20} }; + + // Act & Assert + ASSERT_ANY_THROW(a - b); } \ No newline at end of file From c58b4fa5f5e4fb63cf1abe9630c451e798390deb Mon Sep 17 00:00:00 2001 From: Kate-Nits Date: Mon, 6 Oct 2025 01:53:33 +0300 Subject: [PATCH 72/94] Wrote operator * on Matrix and TriangleMatrix and on TriangleMatrix and Matrix and added tests on it --- lib_triangle_matrix/triangle_matrix.h | 42 +++++++++++++++++- tests/test_triangle_matrix.cpp | 62 +++++++++++++++++++++++++++ 2 files changed, 102 insertions(+), 2 deletions(-) diff --git a/lib_triangle_matrix/triangle_matrix.h b/lib_triangle_matrix/triangle_matrix.h index d0e5e07e..7198b5b4 100644 --- a/lib_triangle_matrix/triangle_matrix.h +++ b/lib_triangle_matrix/triangle_matrix.h @@ -188,7 +188,7 @@ Matrix operator+(const TriangleMatrix& triangle_matrix, const Matrix& m template Matrix operator-(const Matrix& matrix, const TriangleMatrix& triangle_matrix) { if (matrix.rows() != triangle_matrix.n() || matrix.cols() != triangle_matrix.n()) { - throw std::invalid_argument("Matrixes should have the same size for addition"); + throw std::invalid_argument("Matrixes should have the same size for subtraction"); } Matrix result(matrix.rows(), matrix.cols()); for (size_t i = 0; i < matrix.rows(); ++i) { @@ -201,7 +201,7 @@ Matrix operator-(const Matrix& matrix, const TriangleMatrix& triangle_m template Matrix operator-(const TriangleMatrix& triangle_matrix, const Matrix& matrix) { if (matrix.rows() != triangle_matrix.n() || matrix.cols() != triangle_matrix.n()) { - throw std::invalid_argument("Matrixes should have the same size for addition"); + throw std::invalid_argument("Matrixes should have the same size for subtraction"); } Matrix result(matrix.rows(), matrix.cols()); for (size_t i = 0; i < matrix.rows(); ++i) { @@ -211,4 +211,42 @@ Matrix operator-(const TriangleMatrix& triangle_matrix, const Matrix& m } return result; } +template +Matrix operator*(const Matrix& matrix, const TriangleMatrix& triangle_matrix) { + if (matrix.cols() != triangle_matrix.n()) { + throw std::invalid_argument("Matrix should have the same size for mult"); + } + Matrix result(matrix.rows(), matrix.cols()); + TriangleMatrix transpose_triangle_matrix = triangle_matrix; + transpose_triangle_matrix.transpose(); + for (size_t i = 0; i < triangle_matrix.n(); ++i) { + for (size_t j = 0; j < triangle_matrix.n(); ++j) { + T sum{}; + for (size_t k = 0; k < triangle_matrix.n(); ++k) { + sum += matrix.at(i, k) * triangle_matrix.at(k, j); + } + result[i][j] = sum; + } + } + return result; +} +template +Matrix operator*(const TriangleMatrix& triangle_matrix, const Matrix& matrix) { + if (matrix.cols() != triangle_matrix.n()) { + throw std::invalid_argument("Matrix should have the same size for mult"); + } + Matrix result(matrix.rows(), matrix.cols()); + TriangleMatrix transpose_triangle_matrix = triangle_matrix; + transpose_triangle_matrix.transpose(); + for (size_t i = 0; i < triangle_matrix.n(); ++i) { + for (size_t j = 0; j < triangle_matrix.n(); ++j) { + T sum{}; + for (size_t k = 0; k < triangle_matrix.n(); ++k) { + sum += triangle_matrix.at(i, k) * matrix.at(k, j); + } + result[i][j] = sum; + } + } + return result; +} #endif // TRIANGLE_MATRIX_TRIANGLE_MATRIX_X \ No newline at end of file diff --git a/tests/test_triangle_matrix.cpp b/tests/test_triangle_matrix.cpp index 6b26bd5e..ce6f104f 100644 --- a/tests/test_triangle_matrix.cpp +++ b/tests/test_triangle_matrix.cpp @@ -311,4 +311,66 @@ TEST(TestTriangleMatrixLib, test_subtraction_int_triangle_matrix_and_int_matrix_ // Act & Assert ASSERT_ANY_THROW(a - b); +} + +TEST(TestTriangleMatrixLib, test_mult_a_int_Matrix_by_a_int_Triangle_Matrix_with_correct_equal_size) { + // Arrange + TriangleMatrix A(3); + A.at(0, 0) = 1; A.at(0, 1) = 2; A.at(0, 2) = 3; A.at(1, 1) = 4; A.at(1, 2) = 5; A.at(2, 2) = 6; + Matrix B{ {2, 2, 2}, {2, 2, 2}, {2, 2, 2} }; + + // Act + Matrix C = B * A; + + // Assert + EXPECT_EQ(2, C.at(0, 0)); + EXPECT_EQ(12, C.at(0, 1)); + EXPECT_EQ(28, C.at(0, 2)); + EXPECT_EQ(2, C.at(1, 0)); + EXPECT_EQ(12, C.at(1, 1)); + EXPECT_EQ(28, C.at(1, 2)); + EXPECT_EQ(2, C.at(2, 0)); + EXPECT_EQ(12, C.at(2, 1)); + EXPECT_EQ(28, C.at(2, 2)); +} + +TEST(TestTriangleMatrixLib, test_mult_a_int_Matrix_by_a_int_Triangle_Matrix_without_correct_equal_size) { + // Arrange + TriangleMatrix A(4); + A.at(0, 0) = 1; A.at(0, 1) = 2; A.at(0, 2) = 3; A.at(1, 1) = 4; A.at(1, 2) = 5; A.at(2, 2) = 6; + Matrix B{ {2, 2, 2}, {2, 2, 2}, {2, 2, 2} }; + + // Act & Assert + ASSERT_ANY_THROW(B * A); +} + +TEST(TestTriangleMatrixLib, test_mult_a_int_Triangle_Matrix_by_a_int_Matrix_with_correct_equal_size) { + // Arrange + TriangleMatrix A(3); + A.at(0, 0) = 1; A.at(0, 1) = 2; A.at(0, 2) = 3; A.at(1, 1) = 4; A.at(1, 2) = 5; A.at(2, 2) = 6; + Matrix B{ {2, 2, 2}, {2, 2, 2}, {2, 2, 2} }; + + // Act + Matrix C = A * B; + + // Assert + EXPECT_EQ(12, C.at(0, 0)); + EXPECT_EQ(12, C.at(0, 1)); + EXPECT_EQ(12, C.at(0, 2)); + EXPECT_EQ(18, C.at(1, 0)); + EXPECT_EQ(18, C.at(1, 1)); + EXPECT_EQ(18, C.at(1, 2)); + EXPECT_EQ(12, C.at(2, 0)); + EXPECT_EQ(12, C.at(2, 1)); + EXPECT_EQ(12, C.at(2, 2)); +} + +TEST(TestTriangleMatrixLib, test_mult_a_int_Triangle_Matrix_by_a_int_Matrix_without_correct_equal_size) { + // Arrange + TriangleMatrix A(4); + A.at(0, 0) = 1; A.at(0, 1) = 2; A.at(0, 2) = 3; A.at(1, 1) = 4; A.at(1, 2) = 5; A.at(2, 2) = 6; + Matrix B{ {2, 2, 2}, {2, 2, 2}, {2, 2, 2} }; + + // Act & Assert + ASSERT_ANY_THROW(A * B); } \ No newline at end of file From 64c36ede685ecd9143f3c5f42462e2561490a7fd Mon Sep 17 00:00:00 2001 From: Kate-Nits Date: Mon, 6 Oct 2025 03:37:03 +0300 Subject: [PATCH 73/94] Input the classes Matrix and TriangleMatrix in matrix application --- lib_algorithms/algorithms.cpp | 286 +++++++++++++++++----------------- lib_algorithms/algorithms.h | 16 +- 2 files changed, 151 insertions(+), 151 deletions(-) diff --git a/lib_algorithms/algorithms.cpp b/lib_algorithms/algorithms.cpp index ffcecd7f..e1a06c69 100644 --- a/lib_algorithms/algorithms.cpp +++ b/lib_algorithms/algorithms.cpp @@ -31,11 +31,11 @@ #define MULTIPLY 3 #define EXIT_CALCULATE_MATRIX_MENU 4 -//#define REALISED_ADD -//#define REALISED_SUBTRACT -//#define REALISED_MULTIPLY -//#define REALISED_OPERATOR[] -//#define REALISED_CLASSES +#define REALISED_ADD +#define REALISED_SUBTRACT +#define REALISED_MULTIPLY +#define REALISED_OPERATOR +#define REALISED_CLASSES #define STANDART 1 #define TRIANGLE 2 @@ -137,28 +137,21 @@ void what_matrices(int& what_the_first_matrix, int& what_the_second_matrix, int& what_the_second_matrix = TRIANGLE; } } -void what_matrix_sizes(size_t& sizeN, size_t& sizeM) { +void what_matrix_sizes(size_t& size_n, size_t& size_m) { std::cout << "==================================================" << std::endl; std::cout << "Enter the number of lines (parametr M): "; - std::cin >> sizeM; + std::cin >> size_m; std::cout << "Enter the number of columns (parametr N): "; - std::cin >> sizeN; + std::cin >> size_n; } -/*void print_res(const TMatrix& matrix, size_t size_M, size_t size_N) { - -#ifndef REALISED_OPERATOR[] - - in_development(); - -#else - for (size_t i = 0; i < size_M; i++) { +void print_res(const Matrix& matrix, size_t size_m, size_t size_N) { + for (size_t i = 0; i < size_m; i++) { for (size_t j = 0; j < size_N; j++) { - std::cout << matrix[i][j] << " "; + std::cout << matrix.at(i, j) << " "; } std::cout << std::endl; } -#endif // REALISED_OPERATOR[] -}*/ +} void do_user_want_to_save(int& want_to_save) { std::cout << "==================================================" << std::endl; std::cout << "Would you like to save result?" << std::endl; @@ -177,122 +170,145 @@ void do_user_want_to_save(int& want_to_save) { std::cin >> want_to_save; } } -/*void start_matrix_calculator(int& link_user_choice, int& link_want_to_save, TMatrix res) { +void input_standard_matrix(Matrix& matrix, size_t rows, size_t cols) { + std::cout << "Enter matrix elements:" << std::endl; + for (size_t i = 0; i < rows; ++i) { + for (size_t j = 0; j < cols; ++j) { + std::cout << "[" << i << "][" << j << "] = "; + std::cin >> matrix[i][j]; + } + } +} +void input_triangle_matrix(TriangleMatrix& matrix, size_t n) { + std::cout << "Enter upper-triangle elements (including diagonal):" << std::endl; + for (size_t i = 0; i < n; ++i) { + for (size_t j = i; j < n; ++j) { + std::cout << "[" << i << "][" << j << "] = "; + std::cin >> matrix.at(i, j); + } + } +} +Matrix triangle_to_full(const TriangleMatrix& matrix) { + Matrix result(matrix.n(), matrix.n()); + for (size_t i = 0; i < matrix.n(); ++i) + for (size_t j = 0; j < matrix.n(); ++j) + result.at(i, j) = matrix.at(i, j); + return result; +} +void start_matrix_calculator(int& link_user_choice, int& link_want_to_save, Matrix res) { int isExit = NO; - int& link_isExit = isExit; - int what_the_first_matrix = 0; - int what_the_second_matrix = 0; - int& link_what_the_first_matrix = what_the_first_matrix; - int& link_what_the_second_matrix = what_the_second_matrix; - print_menu_matrix_calculator(); - while (!input_user_choice(link_user_choice, START_MENU_MATRIX_CALCULATE_SIZE)); - system("cls"); while (!isExit) { - if (link_user_choice != EXIT_CALCULATE_MATRIX_MENU) { - what_matrices(link_what_the_first_matrix, link_what_the_second_matrix, link_isExit); - system("cls"); - if (isExit == YES) { - break; - } - size_t sizeM1, sizeN1; - size_t& link_sizeM1 = sizeM1; - size_t& link_sizeN1 = sizeN1; - size_t sizeM2, sizeN2; - size_t& link_sizeM2 = sizeM2; - size_t& link_sizeN2 = sizeN2; - size_t size_resM, size_resN; - size_t& link_size_resM = size_resM; - size_t& link_size_resN = size_resN; - if (link_user_choice == ADD || link_user_choice == SUBTRACT) { - what_matrix_sizes(link_sizeN1, link_sizeM1); - sizeM2 = sizeM1; - size_resM = sizeM1; - sizeN2 = sizeN1; - size_resN = sizeN1; - } - else if (link_user_choice == MULTIPLY) { + print_menu_matrix_calculator(); + while (!input_user_choice(link_user_choice, START_MENU_MATRIX_CALCULATE_SIZE)); + system("cls"); + if (link_user_choice == EXIT_CALCULATE_MATRIX_MENU) { break; } + int what_the_first_matrix = 0; + int what_the_second_matrix = 0; + int inner_exit = NO; + what_matrices(what_the_first_matrix, what_the_second_matrix, inner_exit); + system("cls"); + if (inner_exit == YES) { break; } + + size_t size_m1 = 0, size_n1 = 0, size_m2 = 0, size_n2 = 0; + size_t size_res_m = 0, size_res_n = 0; + if (link_user_choice == ADD || link_user_choice == SUBTRACT) { + what_matrix_sizes(size_n1, size_m1); + size_m2 = size_m1; + size_n2 = size_n1; + size_res_m = size_m1; + size_res_n = size_n1; + } + else if (link_user_choice == MULTIPLY) { + std::cout << "==================================================" << std::endl; + std::cout << " For the first matrix" << std::endl; + what_matrix_sizes(size_n1, size_m1); + std::cout << "==================================================" << std::endl; + std::cout << " For the second matrix" << std::endl; + what_matrix_sizes(size_n2, size_m2); + while (size_n1 != size_m2) { + set_color(12, 0); + std::cout << "ERROR: "; + set_color(7, 0); + std::cout << "The parameter N for 1 of the matrix and the parametr M for 2 must be equal" << std::endl; std::cout << "==================================================" << std::endl; std::cout << " For the first matrix" << std::endl; - what_matrix_sizes(link_sizeN1, link_sizeM1); + what_matrix_sizes(size_n1, size_m1); std::cout << "==================================================" << std::endl; std::cout << " For the second matrix" << std::endl; - what_matrix_sizes(link_sizeN2, link_sizeM2); - while (sizeN1 != sizeM2) { - set_color(12, 0); - std::cout << "ERROR: "; - set_color(7, 0); - std::cout << "The parameter N for 1 of the matrix and the parametr M for 2 must be equal" << std::endl; - std::cout << "==================================================" << std::endl; - std::cout << " For the first matrix" << std::endl; - what_matrix_sizes(link_sizeN1, link_sizeM1); - std::cout << "==================================================" << std::endl; - std::cout << " For the second matrix" << std::endl; - what_matrix_sizes(link_sizeN2, link_sizeM2); - } - size_resM = sizeN1; - size_resM = sizeM2; - } - if (what_the_first_matrix == STANDART && what_the_second_matrix == STANDART) { -#ifdef REALISED_CLASSES - TMatrix matrix1; - TMatrix matrix2; - TMatrix intermediate_res; -#else - //in_development(); -#endif // REALISED_CLASSES - - } - else if (what_the_first_matrix == TRIANGLE && what_the_second_matrix == TRIANGLE) { -#ifdef REALISED_CLASSES - TriangleMatrix matrix1; - TriangleMatrix matrix2; - TriangleMatrix intermediate_res; -#else - //in_development(); -#endif // REALISED_CLASSES + what_matrix_sizes(size_n2, size_m2); } + size_res_m = size_n1; + size_res_m = size_m2; } - system("cls"); - switch (link_user_choice) { - case ADD: -#ifndef REALISED_ADD - in_development(); -#else - intermediate_res = matrix1.add_matrices(matrix2); + if (what_the_first_matrix == STANDART && what_the_second_matrix == STANDART) { + Matrix matrix1(size_m1, size_n1); + Matrix matrix2(size_m2, size_n2); + Matrix intermediate_res; + input_standard_matrix(matrix1, size_m1, size_n1); + input_standard_matrix(matrix2, size_m2, size_n2); + switch (link_user_choice) { + case ADD: intermediate_res = matrix1 + matrix2; break; + case SUBTRACT: intermediate_res = matrix1 - matrix2; break; + case MULTIPLY: intermediate_res = matrix1 * matrix2; break; + } res = intermediate_res; - print_res(res, size_resM, size_resN); + print_res(res, size_res_m, size_res_n); do_user_want_to_save(link_want_to_save); -#endif //REALISED_ADD - break; + } + else if (what_the_first_matrix == TRIANGLE && what_the_second_matrix == TRIANGLE) { + TriangleMatrix matrix1(size_m1); + TriangleMatrix matrix2(size_m2); + TriangleMatrix intermediate_res; + input_triangle_matrix(matrix1, size_m1); + input_triangle_matrix(matrix2, size_m2); - case SUBTRACT: -#ifndef REALISED_SUBTRACT - in_development(); -#else - intermediate_res = matrix1.subtract_matrices(matrix2); - res = intermediate_res; - print_res(res, size_resM, size_resN); + switch (link_user_choice) { + case ADD: intermediate_res = matrix1 + matrix2; break; + case SUBTRACT: intermediate_res = matrix1 - matrix2; break; + case MULTIPLY: intermediate_res = matrix1 * matrix2; break; + } + res = triangle_to_full(intermediate_res); + print_res(res, size_res_m, size_res_n); do_user_want_to_save(link_want_to_save); -#endif //REALISED_SUBTRACT - break; + } + else { + if (what_the_first_matrix == STANDART && what_the_second_matrix == TRIANGLE) { + Matrix matrix1(size_m1, size_n1), intermediate_res; + TriangleMatrix matrix2(size_m2); + input_standard_matrix(matrix1, size_m1, size_n1); + input_triangle_matrix(matrix2, size_m2); - case MULTIPLY: -#ifndef REALISED_MULTIPLY - in_development(); -#else - intermediate_res = matrix1.multiply_matrices(matrix2); - res = intermediate_res; - print_res(res, size_resM, size_resN); - do_user_want_to_save(link_want_to_save); -#endif //REALISED_MULTIPLY - break; + switch (link_user_choice) { + case ADD: intermediate_res = matrix1 + matrix2; break; + case SUBTRACT: intermediate_res = matrix1 - matrix2; break; + case MULTIPLY: intermediate_res = matrix1 * matrix2; break; + } + res = intermediate_res; + print_res(res, size_res_m, size_res_n); + do_user_want_to_save(link_want_to_save); + } + else if (what_the_first_matrix == TRIANGLE && what_the_second_matrix == STANDART) { + TriangleMatrix matrix1(size_m1); + Matrix matrix2(size_m2, size_n2), intermediate_res; + input_triangle_matrix(matrix1, size_m1); + input_standard_matrix(matrix2, size_m2, size_n2); - case EXIT_CALCULATE_MATRIX_MENU: - isExit = YES; - break; + switch (link_user_choice) { + case ADD: intermediate_res = matrix1 + matrix2; break; + case SUBTRACT: intermediate_res = matrix1 - matrix2; break; + case MULTIPLY: intermediate_res = matrix1 * matrix2; break; + } + res = intermediate_res; + print_res(res, size_res_m, size_res_n); + do_user_want_to_save(link_want_to_save); + } } + std::cout << "Press Enter to continue..." << std::endl; + getchar(); + getchar(); + system("cls"); } -}*/ +} void deleted_saved_matrix(int& link_user_choice_for_deleted, int& isThereMatrix1, int& isThereMatrix2, int& isThereMatrix3) { std::cout << "==================================================" << std::endl; std::cout << " You can store a limited number of matrices" << std::endl; @@ -319,7 +335,7 @@ void deleted_saved_matrix(int& link_user_choice_for_deleted, int& isThereMatrix1 break; } } -/*void viewing_saved_matrices(int count_of_saved_matrices, TMatrix matrix1, TMatrix matrix2, TMatrix matrix3) { +void viewing_saved_matrices(int count_of_saved_matrices, Matrix matrix1, Matrix matrix2, Matrix matrix3) { std::cout << "==================================================" << std::endl; std::cout << " Which matrix would you like to look at?" << std::endl; if (count_of_saved_matrices > 0) { @@ -356,39 +372,27 @@ void deleted_saved_matrix(int& link_user_choice_for_deleted, int& isThereMatrix1 getchar(); getchar(); system("cls"); -}*/ +} void matrix_application() { - /* - int user_choice; - int& link_user_choice = user_choice; + int user_choice = 0; int isExit = NO; - int want_to_save = NO; - int& link_want_to_save = want_to_save; int count_of_saved_matrices = 0; - TMatrix res; - TMatrix matrix1; - TMatrix matrix2; - TMatrix matrix3; + Matrix res, matrix1, matrix2, matrix3; int isThereMatrix1 = NO; int isThereMatrix2 = NO; int isThereMatrix3 = NO; - int& link_isThereMatrix1 = isThereMatrix1; - int& link_isThereMatrix2 = isThereMatrix2; - int& link_isThereMatrix3 = isThereMatrix3; - while (!isExit) { start_menu_for_matrix(); - while (!input_user_choice(link_user_choice, START_MENU_FOR_MATRIX_SIZE)); + while (!input_user_choice(user_choice, START_MENU_FOR_MATRIX_SIZE)); system("cls"); switch (user_choice) { case MATRIX_CALCULATOR: - start_matrix_calculator(link_user_choice, link_want_to_save, res); + start_matrix_calculator(user_choice, want_to_save, res); if (want_to_save == YES) { int user_choice_for_deleted; - int& link_user_choice_for_deleted = user_choice_for_deleted; while ((count_of_saved_matrices + 1) > MAX_COUNT_OF_SAVED_MATRICES) { - deleted_saved_matrix(link_user_choice_for_deleted, link_isThereMatrix1, link_isThereMatrix2, link_isThereMatrix3); + deleted_saved_matrix(user_choice_for_deleted, isThereMatrix1, isThereMatrix2, isThereMatrix3); if (user_choice_for_deleted == EXIT_DELETED_SAVED_MATRICES_MENU) { break; } @@ -431,8 +435,4 @@ void matrix_application() { break; } } - */ -} - - - +} \ No newline at end of file diff --git a/lib_algorithms/algorithms.h b/lib_algorithms/algorithms.h index 2bdd587e..766252fd 100644 --- a/lib_algorithms/algorithms.h +++ b/lib_algorithms/algorithms.h @@ -10,7 +10,7 @@ #include "../lib_triangle_matrix/triangle_matrix.h" #define START_MENU_MATRIX_SIZE 3 -//#define REALISED_TMATRIX +#define REALISED_Matrix enum Location { intersecting, do_not_intersecting, tangent, inside, coinside }; //tangent - , intersecting - , inside - , coinside - @@ -55,19 +55,19 @@ Location check_position(const T& circle1, const T& circle2) { } void start_menu_for_matrix(); -#ifndef REALISED_TMATRIX -#else -void viewing_saved_matrices(int count_of_saved_matrices, TMatrix matrix1, TMatrix matrix2, TMatrix matrix3); -void start_matrix_calculator(int& link_user_choice, int& link_want_to_save, TMatrix res); -void print_res(const TMatrix& matrix, size_t size_M, size_t size_N); -#endif //REALISED_TMATRIX +void viewing_saved_matrices(int count_of_saved_matrices, Matrix matrix1, Matrix matrix2, Matrix matrix3); +void start_matrix_calculator(int& link_user_choice, int& link_want_to_save, Matrix res); +void print_res(const Matrix& matrix, size_t size_M, size_t size_N); +void do_user_want_to_save(int& want_to_save); +void input_standard_matrix(Matrix& matrix, size_t rows, size_t cols); +void input_triangle_matrix(TriangleMatrix& matrix, size_t n); +Matrix triangle_to_full(const TriangleMatrix& matrix); void check_user_input(int user_choice, int true_number); bool input_user_choice(int& user_choice, int true_number); void what_matrices(int& what_the_first_matrix, int& what_the_second_matrix, int& isExit); void print_menu_matrix_calculator(); void what_matrix_sizes(size_t& link_sizeN, size_t& link_sizeM); void deleted_saved_matrix(int& link_user_choice_for_deleted, int& isThereMatrix1, int& isThereMatrix2, int& isThereMatrix3); -void do_user_want_to_save(int& want_to_save); void matrix_application(); #endif // LIB_ALGORITHMS_H \ No newline at end of file From 09a10f92df8c164b9c8a9b706c36ba07a49e6ff2 Mon Sep 17 00:00:00 2001 From: Kate-Nits Date: Tue, 7 Oct 2025 01:59:52 +0300 Subject: [PATCH 74/94] Finished writing the matrix calculator application, fixed all the logical errors --- lib_algorithms/algorithms.cpp | 348 ++++++++++++++++++---------------- lib_algorithms/algorithms.h | 11 +- lib_mathvector/MathVector.h | 4 +- lib_matrix/matrix.h | 2 +- lib_tvector/tvector.h | 2 +- 5 files changed, 190 insertions(+), 177 deletions(-) diff --git a/lib_algorithms/algorithms.cpp b/lib_algorithms/algorithms.cpp index e1a06c69..27eb0753 100644 --- a/lib_algorithms/algorithms.cpp +++ b/lib_algorithms/algorithms.cpp @@ -31,12 +31,6 @@ #define MULTIPLY 3 #define EXIT_CALCULATE_MATRIX_MENU 4 -#define REALISED_ADD -#define REALISED_SUBTRACT -#define REALISED_MULTIPLY -#define REALISED_OPERATOR -#define REALISED_CLASSES - #define STANDART 1 #define TRIANGLE 2 @@ -45,8 +39,6 @@ void set_color(int text_color, int bg_color) { SetConsoleTextAttribute(hConsole, (bg_color << 4) | text_color); } -//enum Location { intersecting, do_not_intersecting, tangent, inside, coinside }; -//tangent - , intersecting - , inside - , coinside - void print_result_position(const std::string& description, Location result) { std::cout << description << ": "; switch (result) { @@ -67,24 +59,25 @@ void in_development() { system("cls"); } void start_menu_for_matrix() { - std::cout << "==================================================" << std::endl; + std::cout << "======================================================" << std::endl; std::cout << " WORKING WITH MATRICES " << std::endl; - std::cout << "==================================================" << std::endl; + std::cout << "======================================================" << std::endl; std::cout << " 1. Matrix calculator " << std::endl; std::cout << " 2. Viewing saved matrices " << std::endl; std::cout << " 3. Exit " << std::endl; - std::cout << "==================================================" << std::endl; - //std::cout << "==================================================" << std::endl; + std::cout << "======================================================" << std::endl; + //std::cout << "======================================================" << std::endl; //std::cout << "" << std::endl; } void print_menu_matrix_calculator() { - std::cout << "==================================================" << std::endl; + system("cls"); + std::cout << "======================================================" << std::endl; std::cout << " What would you like to do with matrices:" << std::endl; std::cout << " 1. Add " << std::endl; std::cout << " 2. Subtract" << std::endl; std::cout << " 3. multiply" << std::endl; std::cout << " 4. Exit" << std::endl; - std::cout << "==================================================" << std::endl; + std::cout << "======================================================" << std::endl; } void check_user_input(int user_choice, int true_number) { @@ -119,58 +112,65 @@ bool input_user_choice(int& user_choice, int true_number) { void what_matrices(int& what_the_first_matrix, int& what_the_second_matrix, int& isExit) { int user_choice; int& link_user_choice = user_choice; - std::cout << "==================================================" << std::endl; - std::cout << " What will the matrices be like?" << std::endl; + std::cout << "======================================================" << std::endl; + std::cout << " What will the first matrix be like?" << std::endl; std::cout << " 1. Standart" << std::endl; std::cout << " 2. Triangle" << std::endl; - std::cout << " 3. Exit" << std::endl; + std::cout << " 5. Exit" << std::endl; while (!input_user_choice(link_user_choice, WHAT_MATRIX_MENU)); if (user_choice == 3) { isExit = YES; } - else if (user_choice == STANDART) { - what_the_first_matrix = STANDART; - what_the_second_matrix = STANDART; - } else { - what_the_first_matrix = TRIANGLE; - what_the_second_matrix = TRIANGLE; + if (user_choice == STANDART) { what_the_first_matrix = STANDART;} + else { what_the_first_matrix = TRIANGLE; } + std::cout << "======================================================" << std::endl; + std::cout << " What will the second matrix be like?" << std::endl; + std::cout << " 1. Standart" << std::endl; + std::cout << " 2. Triangle" << std::endl; + while (!input_user_choice(link_user_choice, 2)); + if (user_choice == STANDART) { what_the_second_matrix = STANDART; } + else { what_the_second_matrix = TRIANGLE; } } } -void what_matrix_sizes(size_t& size_n, size_t& size_m) { - std::cout << "==================================================" << std::endl; - std::cout << "Enter the number of lines (parametr M): "; - std::cin >> size_m; - std::cout << "Enter the number of columns (parametr N): "; - std::cin >> size_n; -} -void print_res(const Matrix& matrix, size_t size_m, size_t size_N) { - for (size_t i = 0; i < size_m; i++) { - for (size_t j = 0; j < size_N; j++) { - std::cout << matrix.at(i, j) << " "; - } - std::cout << std::endl; +void what_matrix_sizes(const int& type_matrix, size_t& size_rows, size_t& size_cols) { + if (type_matrix == STANDART) { + std::cout << "======================================================" << std::endl; + std::cout << "Enter the number of rows (parametr M): "; + std::cin >> size_rows; + std::cout << "Enter the number of columns (parametr N): "; + std::cin >> size_cols; } + else if (type_matrix == TRIANGLE) { + std::cout << "======================================================" << std::endl; + std::cout << "Enter the size of the triangle matrix (parametr N): "; + std::cin >> size_rows; + size_cols = size_rows; + } + else { + throw std::logic_error("The matrix has an invalid type!"); + } + } void do_user_want_to_save(int& want_to_save) { - std::cout << "==================================================" << std::endl; + std::cout << "======================================================" << std::endl; std::cout << "Would you like to save result?" << std::endl; - std::cout << "==================================================" << std::endl; + std::cout << "======================================================" << std::endl; std::cout << "If answer - yes - input 1. If no, then input 0: "; std::cin >> want_to_save; - while (want_to_save != YES || want_to_save != NO) { - std::cout << "==================================================" << std::endl; + while (want_to_save != YES && want_to_save != NO) { + std::cout << "======================================================" << std::endl; set_color(12, 0); std::cout << "ERROR: "; set_color(7, 0); std::cout << "your choice is more than points" << std::endl; - std::cout << "==================================================" << std::endl; + std::cout << "======================================================" << std::endl; std::cout << "Tru again" << std::endl; std::cout << "If answer - yes - input 1. If no, then input 0: "; std::cin >> want_to_save; } } -void input_standard_matrix(Matrix& matrix, size_t rows, size_t cols) { +void input_standart_matrix(Matrix& matrix, size_t rows, size_t cols) { std::cout << "Enter matrix elements:" << std::endl; for (size_t i = 0; i < rows; ++i) { for (size_t j = 0; j < cols; ++j) { @@ -195,7 +195,10 @@ Matrix triangle_to_full(const TriangleMatrix& matrix) { result.at(i, j) = matrix.at(i, j); return result; } -void start_matrix_calculator(int& link_user_choice, int& link_want_to_save, Matrix res) { +void start_matrix_calculator(int& link_user_choice, int& link_want_to_save, Matrix res, + int& count_of_saved_matrices, Matrix& matrix1, Matrix& matrix2, Matrix& matrix3, + int& isThereMatrix1, int& isThereMatrix2, int& isThereMatrix3) { + int isExit = NO; while (!isExit) { print_menu_matrix_calculator(); @@ -208,109 +211,167 @@ void start_matrix_calculator(int& link_user_choice, int& link_want_to_save, Matr what_matrices(what_the_first_matrix, what_the_second_matrix, inner_exit); system("cls"); if (inner_exit == YES) { break; } - - size_t size_m1 = 0, size_n1 = 0, size_m2 = 0, size_n2 = 0; - size_t size_res_m = 0, size_res_n = 0; - if (link_user_choice == ADD || link_user_choice == SUBTRACT) { - what_matrix_sizes(size_n1, size_m1); - size_m2 = size_m1; - size_n2 = size_n1; - size_res_m = size_m1; - size_res_n = size_n1; + size_t size_rows1 = 0, size_cols1 = 0, size_rows2 = 0, size_cols2 = 0; + size_t size_res_rows = 0, size_res_cols = 0; + if (what_the_first_matrix == TRIANGLE || what_the_second_matrix == TRIANGLE || link_user_choice == ADD || link_user_choice == SUBTRACT) { + if (what_the_first_matrix == TRIANGLE || what_the_second_matrix == TRIANGLE) { + what_matrix_sizes(TRIANGLE, size_rows1, size_cols1); + size_rows2 = size_rows1; + size_cols2 = size_cols1; + size_res_rows = size_rows1; + size_res_cols = size_cols1; + } + else { + what_matrix_sizes(what_the_first_matrix, size_rows1, size_cols1); + size_rows2 = size_rows1; + size_cols2 = size_cols1; + size_res_rows = size_rows1; + size_res_cols = size_cols1; + } } - else if (link_user_choice == MULTIPLY) { - std::cout << "==================================================" << std::endl; + else { + std::cout << "======================================================" << std::endl; std::cout << " For the first matrix" << std::endl; - what_matrix_sizes(size_n1, size_m1); - std::cout << "==================================================" << std::endl; + what_matrix_sizes(what_the_first_matrix, size_cols1, size_rows1); + std::cout << "======================================================" << std::endl; std::cout << " For the second matrix" << std::endl; - what_matrix_sizes(size_n2, size_m2); - while (size_n1 != size_m2) { + what_matrix_sizes(what_the_second_matrix, size_cols2, size_rows2); + while (size_cols1 != size_rows2) { set_color(12, 0); std::cout << "ERROR: "; set_color(7, 0); std::cout << "The parameter N for 1 of the matrix and the parametr M for 2 must be equal" << std::endl; - std::cout << "==================================================" << std::endl; + std::cout << "======================================================" << std::endl; std::cout << " For the first matrix" << std::endl; - what_matrix_sizes(size_n1, size_m1); - std::cout << "==================================================" << std::endl; + what_matrix_sizes(what_the_first_matrix, size_cols1, size_rows1); + std::cout << "======================================================" << std::endl; std::cout << " For the second matrix" << std::endl; - what_matrix_sizes(size_n2, size_m2); + what_matrix_sizes(what_the_second_matrix, size_cols2, size_rows2); } - size_res_m = size_n1; - size_res_m = size_m2; + size_res_rows = size_cols1; + size_res_rows = size_rows2; } + if (what_the_first_matrix == STANDART && what_the_second_matrix == STANDART) { - Matrix matrix1(size_m1, size_n1); - Matrix matrix2(size_m2, size_n2); + Matrix matrix1(size_rows1, size_cols1); + Matrix matrix2(size_rows2, size_cols2); Matrix intermediate_res; - input_standard_matrix(matrix1, size_m1, size_n1); - input_standard_matrix(matrix2, size_m2, size_n2); + std::cout << "======================================================" << std::endl; + std::cout << " For the first matrix:" << std::endl; + input_standart_matrix(matrix1, size_rows1, size_cols1); + std::cout << "======================================================" << std::endl; + std::cout << " For the second matrix:" << std::endl; + input_standart_matrix(matrix2, size_rows2, size_cols2); switch (link_user_choice) { case ADD: intermediate_res = matrix1 + matrix2; break; case SUBTRACT: intermediate_res = matrix1 - matrix2; break; case MULTIPLY: intermediate_res = matrix1 * matrix2; break; } res = intermediate_res; - print_res(res, size_res_m, size_res_n); + std::cout << "======================================================" << std::endl; + std::cout << " RESULT:" << std::endl; + std::cout << res << std::endl; do_user_want_to_save(link_want_to_save); } else if (what_the_first_matrix == TRIANGLE && what_the_second_matrix == TRIANGLE) { - TriangleMatrix matrix1(size_m1); - TriangleMatrix matrix2(size_m2); + TriangleMatrix matrix1(size_rows1); + TriangleMatrix matrix2(size_rows2); TriangleMatrix intermediate_res; - input_triangle_matrix(matrix1, size_m1); - input_triangle_matrix(matrix2, size_m2); - + std::cout << "======================================================" << std::endl; + std::cout << " For the first triangle matrix:" << std::endl; + input_triangle_matrix(matrix1, size_rows1); + std::cout << "======================================================" << std::endl; + std::cout << " For the second triangle matrix:" << std::endl; + input_triangle_matrix(matrix2, size_rows2); switch (link_user_choice) { case ADD: intermediate_res = matrix1 + matrix2; break; case SUBTRACT: intermediate_res = matrix1 - matrix2; break; case MULTIPLY: intermediate_res = matrix1 * matrix2; break; } res = triangle_to_full(intermediate_res); - print_res(res, size_res_m, size_res_n); + std::cout << "======================================================" << std::endl; + std::cout << " RESULT:" << std::endl; + std::cout << res << std::endl; + do_user_want_to_save(link_want_to_save); + } + else if (what_the_first_matrix == STANDART && what_the_second_matrix == TRIANGLE) { + Matrix matrix1(size_rows1, size_cols1), intermediate_res; + TriangleMatrix matrix2(size_rows2); + std::cout << "======================================================" << std::endl; + std::cout << " For the first standart matrix:" << std::endl; + input_standart_matrix(matrix1, size_rows1, size_cols1); + std::cout << "======================================================" << std::endl; + std::cout << " For the second triangle matrix:" << std::endl; + input_triangle_matrix(matrix2, size_rows2); + switch (link_user_choice) { + case ADD: intermediate_res = matrix1 + matrix2; break; + case SUBTRACT: intermediate_res = matrix1 - matrix2; break; + case MULTIPLY: intermediate_res = matrix1 * matrix2; break; + } + res = intermediate_res; + std::cout << "======================================================" << std::endl; + std::cout << " RESULT:" << std::endl; + std::cout << res << std::endl; do_user_want_to_save(link_want_to_save); } - else { - if (what_the_first_matrix == STANDART && what_the_second_matrix == TRIANGLE) { - Matrix matrix1(size_m1, size_n1), intermediate_res; - TriangleMatrix matrix2(size_m2); - input_standard_matrix(matrix1, size_m1, size_n1); - input_triangle_matrix(matrix2, size_m2); - - switch (link_user_choice) { - case ADD: intermediate_res = matrix1 + matrix2; break; - case SUBTRACT: intermediate_res = matrix1 - matrix2; break; - case MULTIPLY: intermediate_res = matrix1 * matrix2; break; - } - res = intermediate_res; - print_res(res, size_res_m, size_res_n); - do_user_want_to_save(link_want_to_save); + else if (what_the_first_matrix == TRIANGLE && what_the_second_matrix == STANDART) { + TriangleMatrix matrix1(size_rows1); + Matrix matrix2(size_rows2, size_cols2), intermediate_res; + std::cout << "======================================================" << std::endl; + std::cout << " For the first triangle matrix:" << std::endl; + input_triangle_matrix(matrix1, size_rows1); + std::cout << "======================================================" << std::endl; + std::cout << " For the second standart matrix:" << std::endl; + input_standart_matrix(matrix2, size_rows2, size_cols2); + switch (link_user_choice) { + case ADD: intermediate_res = matrix1 + matrix2; break; + case SUBTRACT: intermediate_res = matrix1 - matrix2; break; + case MULTIPLY: intermediate_res = matrix1 * matrix2; break; } - else if (what_the_first_matrix == TRIANGLE && what_the_second_matrix == STANDART) { - TriangleMatrix matrix1(size_m1); - Matrix matrix2(size_m2, size_n2), intermediate_res; - input_triangle_matrix(matrix1, size_m1); - input_standard_matrix(matrix2, size_m2, size_n2); + res = intermediate_res; + std::cout << "======================================================" << std::endl; + std::cout << " RESULT:" << std::endl; + std::cout << res << std::endl; + do_user_want_to_save(link_want_to_save); + } - switch (link_user_choice) { - case ADD: intermediate_res = matrix1 + matrix2; break; - case SUBTRACT: intermediate_res = matrix1 - matrix2; break; - case MULTIPLY: intermediate_res = matrix1 * matrix2; break; - } - res = intermediate_res; - print_res(res, size_res_m, size_res_n); - do_user_want_to_save(link_want_to_save); + if (link_want_to_save == YES) { + int user_choice_for_deleted; + while ((count_of_saved_matrices + 1) > MAX_COUNT_OF_SAVED_MATRICES) { + deleted_saved_matrix(user_choice_for_deleted, isThereMatrix1, isThereMatrix2, isThereMatrix3); + if (user_choice_for_deleted == EXIT_DELETED_SAVED_MATRICES_MENU) { break; } + else { count_of_saved_matrices--; } + } + if (isThereMatrix1 == NO) { + std::cout << "======================================================" << std::endl; + std::cout << " The result will be written to the matrix1" << std::endl; + isThereMatrix1 = YES; + count_of_saved_matrices++; + matrix1 = res; + } + else if (isThereMatrix2 == NO) { + std::cout << "======================================================" << std::endl; + std::cout << " The result will be written to the matrix2" << std::endl; + isThereMatrix2 = YES; + count_of_saved_matrices++; + matrix2 = res; + } + else if (isThereMatrix3 == NO) { + std::cout << "======================================================" << std::endl; + std::cout << " The result will be written to the matrix3" << std::endl; + isThereMatrix3 = YES; + count_of_saved_matrices++; + matrix3 = res; } + std::cout << " Press Enter to exit " << std::endl; + getchar(); + getchar(); + system("cls"); } - std::cout << "Press Enter to continue..." << std::endl; - getchar(); - getchar(); - system("cls"); } } void deleted_saved_matrix(int& link_user_choice_for_deleted, int& isThereMatrix1, int& isThereMatrix2, int& isThereMatrix3) { - std::cout << "==================================================" << std::endl; + std::cout << "======================================================" << std::endl; std::cout << " You can store a limited number of matrices" << std::endl; std::cout << " Which one would you like to delete?" << std::endl; std::cout << " 1. matrix1" << std::endl; @@ -320,25 +381,16 @@ void deleted_saved_matrix(int& link_user_choice_for_deleted, int& isThereMatrix1 while (!input_user_choice(link_user_choice_for_deleted, EXIT_DELETED_SAVED_MATRICES_MENU)); switch (link_user_choice_for_deleted) { - case MATRIX1: - isThereMatrix1 = NO; - break; - - case MATRIX2: - isThereMatrix2 = NO; - break; - - case MATRIX3: - isThereMatrix3 = NO; - break; - case EXIT_DELETED_SAVED_MATRICES_MENU: - break; + case MATRIX1: isThereMatrix1 = NO; break; + case MATRIX2: isThereMatrix2 = NO; break; + case MATRIX3: isThereMatrix3 = NO; break; + case EXIT_DELETED_SAVED_MATRICES_MENU: break; } } void viewing_saved_matrices(int count_of_saved_matrices, Matrix matrix1, Matrix matrix2, Matrix matrix3) { - std::cout << "==================================================" << std::endl; - std::cout << " Which matrix would you like to look at?" << std::endl; + std::cout << "======================================================" << std::endl; if (count_of_saved_matrices > 0) { + std::cout << " Which matrix would you like to look at?" << std::endl; if (count_of_saved_matrices == MAX_COUNT_OF_SAVED_MATRICES) { std::cout << " 1. matrix1" << std::endl; std::cout << " 2. matrix2" << std::endl; @@ -355,19 +407,22 @@ void viewing_saved_matrices(int count_of_saved_matrices, Matrix matrix1, Ma int& link_user_choice = user_choice; while (!input_user_choice(link_user_choice, START_MENU_FOR_MATRIX_SIZE)); if (user_choice == MATRIX1) { - matrix1.print(); + std::cout << "======================================================" << std::endl << std::endl; + std::cout << matrix1 << std::endl; } else if (user_choice == MATRIX2) { - matrix2.print(); + std::cout << "======================================================" << std::endl; + std::cout << matrix2 << std::endl; } else if (user_choice == MATRIX3) { - matrix3.print(); + std::cout << "======================================================" << std::endl; + std::cout << matrix3 << std::endl; } } else if (count_of_saved_matrices == 0) { std::cout << " You don't have any saved matrices" << std::endl; } - std::cout << "==================================================" << std::endl; + std::cout << "======================================================" << std::endl; std::cout << " Press Enter to exit " << std::endl; getchar(); getchar(); @@ -388,44 +443,7 @@ void matrix_application() { system("cls"); switch (user_choice) { case MATRIX_CALCULATOR: - start_matrix_calculator(user_choice, want_to_save, res); - if (want_to_save == YES) { - int user_choice_for_deleted; - while ((count_of_saved_matrices + 1) > MAX_COUNT_OF_SAVED_MATRICES) { - deleted_saved_matrix(user_choice_for_deleted, isThereMatrix1, isThereMatrix2, isThereMatrix3); - if (user_choice_for_deleted == EXIT_DELETED_SAVED_MATRICES_MENU) { - break; - } - else { - count_of_saved_matrices--; - } - } - if (isThereMatrix1 == NO) { - std::cout << "==================================================" << std::endl; - std::cout << " The result will be written to the matrix1" << std::endl; - isThereMatrix1 = YES; - count_of_saved_matrices++; - matrix1 = res; - } - else if (isThereMatrix2 == NO) { - std::cout << "==================================================" << std::endl; - std::cout << " The result will be written to the matrix2" << std::endl; - isThereMatrix2 = YES; - count_of_saved_matrices++; - matrix2 = res; - } - else if (isThereMatrix3 == NO) { - std::cout << "==================================================" << std::endl; - std::cout << " The result will be written to the matrix3" << std::endl; - isThereMatrix3 = YES; - count_of_saved_matrices++; - matrix3 = res; - } - std::cout << " Press Enter to exit " << std::endl; - getchar(); - getchar(); - system("cls"); - } + start_matrix_calculator(user_choice, want_to_save, res, count_of_saved_matrices, matrix1, matrix2, matrix3, isThereMatrix1, isThereMatrix2, isThereMatrix3); break; case VIEWING_SAVED_MATRICES: viewing_saved_matrices(count_of_saved_matrices, matrix1, matrix2, matrix3); diff --git a/lib_algorithms/algorithms.h b/lib_algorithms/algorithms.h index 766252fd..c4b6451f 100644 --- a/lib_algorithms/algorithms.h +++ b/lib_algorithms/algorithms.h @@ -15,14 +15,10 @@ enum Location { intersecting, do_not_intersecting, tangent, inside, coinside }; //tangent - , intersecting - , inside - , coinside - - -// void set_color(int text_color, int bg_color); void print_result_position(const std::string& description, Location result); - void in_development(); - template Location check_position(const T& circle1, const T& circle2) { Point center1 = circle1.get_center(); @@ -56,17 +52,16 @@ Location check_position(const T& circle1, const T& circle2) { void start_menu_for_matrix(); void viewing_saved_matrices(int count_of_saved_matrices, Matrix matrix1, Matrix matrix2, Matrix matrix3); -void start_matrix_calculator(int& link_user_choice, int& link_want_to_save, Matrix res); -void print_res(const Matrix& matrix, size_t size_M, size_t size_N); +void start_matrix_calculator(int& link_user_choice, int& link_want_to_save, Matrix res, int& count_of_saved_matrices, Matrix& matrix1, Matrix& matrix2, Matrix& matrix3, int& isThereMatrix1, int& isThereMatrix2, int& isThereMatrix3); void do_user_want_to_save(int& want_to_save); -void input_standard_matrix(Matrix& matrix, size_t rows, size_t cols); +void input_standart_matrix(Matrix& matrix, size_t rows, size_t cols); void input_triangle_matrix(TriangleMatrix& matrix, size_t n); Matrix triangle_to_full(const TriangleMatrix& matrix); void check_user_input(int user_choice, int true_number); bool input_user_choice(int& user_choice, int true_number); void what_matrices(int& what_the_first_matrix, int& what_the_second_matrix, int& isExit); void print_menu_matrix_calculator(); -void what_matrix_sizes(size_t& link_sizeN, size_t& link_sizeM); +void what_matrix_sizes(const int& type_matrix, size_t& size_rows, size_t& size_cols); void deleted_saved_matrix(int& link_user_choice_for_deleted, int& isThereMatrix1, int& isThereMatrix2, int& isThereMatrix3); void matrix_application(); diff --git a/lib_mathvector/MathVector.h b/lib_mathvector/MathVector.h index d20b8d6c..8f2a303b 100644 --- a/lib_mathvector/MathVector.h +++ b/lib_mathvector/MathVector.h @@ -60,7 +60,7 @@ class MathVector : public TVector { } template MathVector> operator+(const MathVector& other) const { - using R = std::common_type_t; // std::common_type_t + using R = std::common_type_t; // std::common_type_t if (this->size()!= other.size()) { throw std::invalid_argument("Vectors should have same size for addition"); } @@ -84,7 +84,7 @@ class MathVector : public TVector { } template MathVector> operator-(const MathVector& other) const { - using R = std::common_type_t; // std::common_type_t + using R = std::common_type_t; // std::common_type_t if (this->size()!= other.size()) { throw std::invalid_argument("Vectors should have same size for subtraction"); } diff --git a/lib_matrix/matrix.h b/lib_matrix/matrix.h index baf112bb..5362855e 100644 --- a/lib_matrix/matrix.h +++ b/lib_matrix/matrix.h @@ -28,7 +28,7 @@ class Matrix : public MathVector> { } } Matrix(const std::initializer_list> list) { - if (list.size() == 0) { // , list.begin() == list.end() + if (list.size() == 0) { // , list.begin() == list.end() _rows = 0; _cols = 0; this->_size = 0; diff --git a/lib_tvector/tvector.h b/lib_tvector/tvector.h index 28bc4037..80820ac2 100644 --- a/lib_tvector/tvector.h +++ b/lib_tvector/tvector.h @@ -163,7 +163,7 @@ class TVector { throw std::out_of_range("No busy elements in vector"); } - inline bool is_empty() const noexcept { return _size == 0 || (_size - _deleted == 0); } //ôóíêöèÿ ïðîâåðêè íà ïóñòîòó + inline bool is_empty() const noexcept { return _size == 0 || (_size - _deleted == 0); } //функции вставки void push_front(const T& value) noexcept { From 7c8d51f0ca1171866c95d4a5efa6de871f9951ae Mon Sep 17 00:00:00 2001 From: Kate-Nits Date: Tue, 7 Oct 2025 22:40:11 +0300 Subject: [PATCH 75/94] Wrote function gradient_descent and added tests on it --- lib_algorithms/algorithms.h | 123 ++++++++++++++++++++++++++ lib_triangle_matrix/triangle_matrix.h | 2 +- tests/test_algorithms.cpp | 47 ++++++++++ 3 files changed, 171 insertions(+), 1 deletion(-) create mode 100644 tests/test_algorithms.cpp diff --git a/lib_algorithms/algorithms.h b/lib_algorithms/algorithms.h index c4b6451f..260d2ee0 100644 --- a/lib_algorithms/algorithms.h +++ b/lib_algorithms/algorithms.h @@ -6,12 +6,22 @@ #include "../lib_circle/circle.h" #include "../lib_sphere/sphere.h" +#include "../lib_tvector/tvector.h" +#include "../lib_mathvector/MathVector.h" #include "../lib_matrix/matrix.h" #include "../lib_triangle_matrix/triangle_matrix.h" #define START_MENU_MATRIX_SIZE 3 #define REALISED_Matrix +#define NUMBER1 1 +#define NUMBER2 2 +#define NUMBER3 3 +#define NUMBER4 4 + +template class Matrix; +template class TriangleMatrix; + enum Location { intersecting, do_not_intersecting, tangent, inside, coinside }; //tangent - , intersecting - , inside - , coinside - @@ -50,6 +60,119 @@ Location check_position(const T& circle1, const T& circle2) { } } +template +bool has_unique_elements(const Matrix& matrix) { + TVector elements; + for (size_t i = 0; i < matrix.rows(); i++) { + for (size_t j = 0; j < matrix.cols(); j++) { + elements.push_back(matrix.at(i, j)); + } + } + quick_sort(elements); + for (size_t i = 1; i < elements.size(); ++i) { + if (elements[i] == elements[i - 1]) { return false; } + } + return true; +} +template +int what_number_less(const T& num1, const T& num2, const T& num3, const T& num4) { + if (num1 < num2 && num1 < num3 && num1 < num4) { + return NUMBER1; + } + else if (num2 < num1 && num2 < num3 && num2 < num4) { + return NUMBER2; + } + else if (num3 < num1 && num3 < num2 && num3 < num4) { + return NUMBER3; + } + else { + return NUMBER4; + } +} +template +T gradient_descent(const Matrix& matrix) { + if (matrix.cols() != matrix.rows()) { + throw std::invalid_argument("The Matrix should be square"); + } + if (!has_unique_elements(matrix)) { + throw std::invalid_argument("The Matrix should has only unique elements"); + } + + size_t start_row_pos = 0, start_col_pos = 0; + std::cout << "Input start position row: "; + std::cin >> start_row_pos; + std::cout << "Input start position column: "; + std::cin >> start_col_pos; + if (start_row_pos >= matrix.rows() || start_col_pos >= matrix.cols()) { + throw std::out_of_range("Start position out of matrix bounds"); + } + + T current_value = matrix.at(start_row_pos, start_col_pos); + size_t current_row = start_row_pos; + size_t current_col = start_col_pos; + + const size_t max_steps = matrix.rows() * matrix.cols(); + size_t steps = 0; + bool improved = true; + + while (improved && steps < max_steps) { + improved = false; + T best_neighbor_value = current_value; + size_t best_neighbor_row = current_row; + size_t best_neighbor_col = current_col; + + if (current_row > 0) { // + T up_value = matrix.at(current_row - 1, current_col); + if (up_value < best_neighbor_value) { + best_neighbor_value = up_value; + best_neighbor_row = current_row - 1; + best_neighbor_col = current_col; + improved = true; + } + } + + if (current_col < matrix.cols() - 1) { // + T right_value = matrix.at(current_row, current_col + 1); + if (right_value < best_neighbor_value) { + best_neighbor_value = right_value; + best_neighbor_row = current_row; + best_neighbor_col = current_col + 1; + improved = true; + } + } + + if (current_row < matrix.rows() - 1) { // + T down_value = matrix.at(current_row + 1, current_col); + if (down_value < best_neighbor_value) { + best_neighbor_value = down_value; + best_neighbor_row = current_row + 1; + best_neighbor_col = current_col; + improved = true; + } + } + + if (current_col > 0) { // + T left_value = matrix.at(current_row, current_col - 1); + if (left_value < best_neighbor_value) { + best_neighbor_value = left_value; + best_neighbor_row = current_row; + best_neighbor_col = current_col - 1; + improved = true; + } + } + + if (improved) { + current_row = best_neighbor_row; + current_col = best_neighbor_col; + current_value = best_neighbor_value; + steps++; + } + } + std::cout << "Final value: " << current_value << std::endl; + + return current_value; +} + void start_menu_for_matrix(); void viewing_saved_matrices(int count_of_saved_matrices, Matrix matrix1, Matrix matrix2, Matrix matrix3); void start_matrix_calculator(int& link_user_choice, int& link_want_to_save, Matrix res, int& count_of_saved_matrices, Matrix& matrix1, Matrix& matrix2, Matrix& matrix3, int& isThereMatrix1, int& isThereMatrix2, int& isThereMatrix3); diff --git a/lib_triangle_matrix/triangle_matrix.h b/lib_triangle_matrix/triangle_matrix.h index 7198b5b4..b6adb102 100644 --- a/lib_triangle_matrix/triangle_matrix.h +++ b/lib_triangle_matrix/triangle_matrix.h @@ -151,7 +151,7 @@ template std::ostream& operator<< <>(std::ostream& out, const TriangleMatrix& matrix) { for (size_t i = 0; i < matrix.n(); ++i) { out << "[ "; - for (size_t j = 0; i < matrix.n(); ++j) { + for (size_t j = 0; j < matrix.n(); ++j) { out << matrix.at(i, j) << " "; } out << "]\n"; diff --git a/tests/test_algorithms.cpp b/tests/test_algorithms.cpp new file mode 100644 index 00000000..4038e7cd --- /dev/null +++ b/tests/test_algorithms.cpp @@ -0,0 +1,47 @@ +// Copyright 2025 Ekaterina Ushnitskaya + +#include +#include "../lib_tvector/tvector.h" +#include "../lib_mathvector/MathVector.h" +#include "../lib_matrix/matrix.h" +#include "../lib_algorithms/algorithms.h" + +#define EPSILON 0.000001 +#define TRUE 1 +#define FALSE 0 + +TEST(TestAlgorithmsLib, test_position00_value11_from_example) { + // Arrange () + Matrix matrix{ {11, 15, 10, 9}, {6, 16, 3, 8}, {7, 4, 2, 13}, {14, 12, 1, 5} }; + + // Act + std::cout << "Testing with start position [0][0] = value 11" << std::endl; + int res = gradient_descent(matrix); + + // Assert + EXPECT_EQ(6, res); +} + +TEST(TestAlgorithmsLib, test_position20_value7_from_example) { + // Arrange () + Matrix matrix{ {11, 15, 10, 9}, {6, 16, 3, 8}, {7, 4, 2, 13}, {14, 12, 1, 5} }; + + // Act + std::cout << "Testing with start position [2][0] = value 7" << std::endl; + int res = gradient_descent(matrix); + + // Assert + EXPECT_EQ(1, res); +} + +TEST(TestAlgorithmsLib, test_position22_value9_from_example) { + // Arrange () + Matrix matrix{ {3, 1, 2}, {5, 8, 4}, {7, 6, 9} }; + + // Act + std::cout << "Testing with start position [2][2] = value 9" << std::endl; + int res = gradient_descent(matrix); + + // Assert + EXPECT_EQ(1, res); +} \ No newline at end of file From d1a709e75d2e841c6a7ff01b123f8dab0980b794 Mon Sep 17 00:00:00 2001 From: Kate-Nits Date: Tue, 14 Oct 2025 03:20:32 +0300 Subject: [PATCH 76/94] Started wrote Stack, added default constructor, constructor with size, copy constructor and tests on it, also wrote size(), is_full(), is_empty(), clear() and a little change is_empty() in class TVector --- CMakeLists.txt | 1 + lib_stack/CMakeLists.txt | 2 ++ lib_stack/stack.cpp | 0 lib_stack/stack.h | 35 ++++++++++++++++++++++++++++++++ lib_tvector/tvector.h | 2 +- tests/test_stack.cpp | 43 ++++++++++++++++++++++++++++++++++++++++ 6 files changed, 82 insertions(+), 1 deletion(-) create mode 100644 lib_stack/CMakeLists.txt create mode 100644 lib_stack/stack.cpp create mode 100644 lib_stack/stack.h create mode 100644 tests/test_stack.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index ecafe97c..5b9f901f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -23,6 +23,7 @@ add_subdirectory(lib_tvector) add_subdirectory(lib_mathvector) add_subdirectory(lib_matrix) add_subdirectory(lib_triangle_matrix) +add_subdirectory(lib_stack) add_subdirectory(main) # подключаем дополнительный CMakeLists.txt из подкаталога с именем main option(BTEST "build test?" ON) # указываем подключаем ли google-тесты (ON или YES) или нет (OFF или NO) diff --git a/lib_stack/CMakeLists.txt b/lib_stack/CMakeLists.txt new file mode 100644 index 00000000..64a9f0cc --- /dev/null +++ b/lib_stack/CMakeLists.txt @@ -0,0 +1,2 @@ +create_project_lib(Stack) +add_depend(Stack TVector lib_tvector) \ No newline at end of file diff --git a/lib_stack/stack.cpp b/lib_stack/stack.cpp new file mode 100644 index 00000000..e69de29b diff --git a/lib_stack/stack.h b/lib_stack/stack.h new file mode 100644 index 00000000..35250dd3 --- /dev/null +++ b/lib_stack/stack.h @@ -0,0 +1,35 @@ +// Copyright 2025 Ekaterina Ushnitskaya + +#ifndef LIB_STACK_STACK_H +#define LIB_STACK_STACK_H + +#include +#include +#include "../lib_tvector/tvector.h" + + +template +class Stack { + TVector _data; + size_t _max_capacity; // +public: + Stack() : _data(), _max_capacity(0) {} + explicit Stack(size_t size) : _data(0), _max_capacity(size) {} + Stack(const Stack& other) : _data(other._data), _max_capacity(other._max_capacity) {} + + size_t size() const noexcept { + return _data.size() - _data.deleted(); + } + bool is_full() const noexcept { + return _max_capacity > 0 && this->size() >= _max_capacity; + } + bool is_empty() const noexcept { + return _data.is_empty(); + } + void clear() noexcept { + _data.clear(); + } +}; + + +#endif // LIB_STACK_STACK_H \ No newline at end of file diff --git a/lib_tvector/tvector.h b/lib_tvector/tvector.h index 80820ac2..964f8ed2 100644 --- a/lib_tvector/tvector.h +++ b/lib_tvector/tvector.h @@ -163,7 +163,7 @@ class TVector { throw std::out_of_range("No busy elements in vector"); } - inline bool is_empty() const noexcept { return _size == 0 || (_size - _deleted == 0); } + inline bool is_empty() const noexcept { return (_size - _deleted) == 0; } //функции вставки void push_front(const T& value) noexcept { diff --git a/tests/test_stack.cpp b/tests/test_stack.cpp new file mode 100644 index 00000000..8f93b722 --- /dev/null +++ b/tests/test_stack.cpp @@ -0,0 +1,43 @@ +// Copyright 2025 Ekaterina Ushnitskaya + +#include +#include "../lib_stack/stack.h" + +#define EPSILON 0.000001 +#define TRUE 1 +#define FALSE 0 + +TEST(TestStackLib, default_constructor_creates_empty_stack) { + // Arrange + Stack stack; + + // Act & Assert + EXPECT_EQ(0, stack.size()); + EXPECT_TRUE(stack.is_empty()); + EXPECT_FALSE(stack.is_full()); +} + +TEST(TestStackLib, constructor_with_capacity) { + // Arrange + size_t capacity = 10; + Stack stack(capacity); + + // Act & Assert + EXPECT_EQ(0, stack.size()); + EXPECT_TRUE(stack.is_empty()); + EXPECT_FALSE(stack.is_full()); +} + +TEST(TestStackLib, copy_constructor) { + // Arrange + size_t capacity = 10; + Stack stack1(capacity); + + // Act + Stack stack2(stack1); + + // Assert + EXPECT_EQ(stack1.size(), stack2.size()); + EXPECT_EQ(stack1.is_empty(), stack2.is_empty()); + EXPECT_EQ(stack1.is_full(), stack2.is_full()); +} \ No newline at end of file From 44001543b10a3b5d7358c6ab574a9ddb8dda1dd9 Mon Sep 17 00:00:00 2001 From: Kate-Nits Date: Tue, 14 Oct 2025 03:37:03 +0300 Subject: [PATCH 77/94] Wrote push and top for Stack and added tests on it --- lib_stack/stack.h | 20 ++++++++++++++++++++ tests/test_stack.cpp | 26 ++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/lib_stack/stack.h b/lib_stack/stack.h index 35250dd3..fad9d3ec 100644 --- a/lib_stack/stack.h +++ b/lib_stack/stack.h @@ -29,6 +29,26 @@ class Stack { void clear() noexcept { _data.clear(); } + + void push(const T& value) { + if (is_full()) { + throw std::overflow_error("Stack overflow: cannot push, because stack is full"); + } + _data.push_back(value); + } + + T& top() { + if (is_empty()) { + throw std::underflow_error("Stack is empty (no top element"); + } + return _data.back(); + } + const T& top() const { + if (is_empty()) { + throw std::underflow_error("Stack is empty (no top element"); + } + return _data.back(); + } }; diff --git a/tests/test_stack.cpp b/tests/test_stack.cpp index 8f93b722..fb810813 100644 --- a/tests/test_stack.cpp +++ b/tests/test_stack.cpp @@ -40,4 +40,30 @@ TEST(TestStackLib, copy_constructor) { EXPECT_EQ(stack1.size(), stack2.size()); EXPECT_EQ(stack1.is_empty(), stack2.is_empty()); EXPECT_EQ(stack1.is_full(), stack2.is_full()); +} + +TEST(TestStackLib, test_push_without_an_overflowing_stack) { + // Arrange + Stack stack; + + // Act + stack.push(10); + stack.push(20); + + // Assert + EXPECT_FALSE(stack.is_empty()); + EXPECT_EQ(2, stack.size()); + EXPECT_EQ(20, stack.top()); +} + +TEST(TestStackLib, test_push_with_an_overflowing_stack) { + // Arrange + Stack stack(2); + + // Act + stack.push(10); + stack.push(20); + + // Assert + EXPECT_ANY_THROW(stack.push(30)); } \ No newline at end of file From 7447ded519f57c674be721fa20baa5e22e6b9fab Mon Sep 17 00:00:00 2001 From: Kate-Nits Date: Tue, 14 Oct 2025 04:01:53 +0300 Subject: [PATCH 78/94] I finished writing Stack, wrote pop, and added all the tests for each methods (pop, is_empty, is_full, clear) --- lib_stack/stack.h | 9 +++- tests/test_stack.cpp | 107 ++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 114 insertions(+), 2 deletions(-) diff --git a/lib_stack/stack.h b/lib_stack/stack.h index fad9d3ec..8b39e514 100644 --- a/lib_stack/stack.h +++ b/lib_stack/stack.h @@ -37,9 +37,16 @@ class Stack { _data.push_back(value); } + void pop() { + if (is_empty()) { + throw std::underflow_error("Stack is empty (can't pop from empty stack)"); + } + _data.pop_back(); + } + T& top() { if (is_empty()) { - throw std::underflow_error("Stack is empty (no top element"); + throw std::underflow_error("Stack is empty (no top element)"); } return _data.back(); } diff --git a/tests/test_stack.cpp b/tests/test_stack.cpp index fb810813..dea2bba9 100644 --- a/tests/test_stack.cpp +++ b/tests/test_stack.cpp @@ -42,7 +42,7 @@ TEST(TestStackLib, copy_constructor) { EXPECT_EQ(stack1.is_full(), stack2.is_full()); } -TEST(TestStackLib, test_push_without_an_overflowing_stack) { +TEST(TestStackLib, test_push_and_top_without_an_overflowing_stack) { // Arrange Stack stack; @@ -66,4 +66,109 @@ TEST(TestStackLib, test_push_with_an_overflowing_stack) { // Assert EXPECT_ANY_THROW(stack.push(30)); +} + +TEST(TestStackLib, test_top_with_empty_stack) { + // Arrange + Stack stack; + + // Act & Assert + EXPECT_TRUE(stack.is_empty()); + EXPECT_ANY_THROW(stack.top()); +} + +TEST(TestStackLib, test_pop_not_all_elements_in_stack_and_top_without_an_underflowing_stack) { + // Arrange + Stack stack; + stack.push(1); + stack.push(2); + stack.push(3); + + // Act + stack.pop(); + + // Assert + EXPECT_FALSE(stack.is_empty()); + EXPECT_EQ(2, stack.size()); + EXPECT_EQ(2, stack.top()); +} + +TEST(TestStackLib, test_pop_all_elements_in_stack_and_top_without_an_underflowing_stack) { + // Arrange + Stack stack; + stack.push(1); + + // Act + stack.pop(); + + // Assert + EXPECT_TRUE(stack.is_empty()); + EXPECT_EQ(0, stack.size()); +} + +TEST(TestStackLib, test_pop_with_an_underflowing_stack) { + // Arrange + Stack stack; + + // Act & Assert + EXPECT_TRUE(stack.is_empty()); + EXPECT_ANY_THROW(stack.pop()); +} + +TEST(TestStackLib, test_clear) { + // Arrange + Stack stack; + stack.push(1); + stack.push(2); + stack.push(3); + + // Act + stack.clear(); + + // Assert + EXPECT_TRUE(stack.is_empty()); + EXPECT_EQ(0, stack.size()); + EXPECT_ANY_THROW(stack.top()); +} + +TEST(TestStackLib, test_is_full_checking_for_a_full_stack) { + // Arrange + Stack stack(3); + stack.push(1); + stack.push(2); + stack.push(3); + + // Act & Assert + EXPECT_TRUE(stack.is_full()); + EXPECT_ANY_THROW(stack.push(4)); +} + +TEST(TestStackLib, test_is_full_checking_for_an_empty_stack) { + // Arrange + Stack stack(3); + + // Act & Assert + EXPECT_FALSE(stack.is_full()); + EXPECT_ANY_THROW(stack.pop()); +} + +TEST(TestStackLib, test_is_empty_checking_for_a_full_stack) { + // Arrange + Stack stack(3); + stack.push(1); + stack.push(2); + stack.push(3); + + // Act & Assert + EXPECT_FALSE(stack.is_empty()); + EXPECT_ANY_THROW(stack.push(4)); +} + +TEST(TestStackLib, test_is_empty_checking_for_an_empty_stack) { + // Arrange + Stack stack(3); + + // Act & Assert + EXPECT_TRUE(stack.is_empty()); + EXPECT_ANY_THROW(stack.pop()); } \ No newline at end of file From 3f337f6c66391b51c9b5d49da694e854f819d2ac Mon Sep 17 00:00:00 2001 From: Kate-Nits Date: Tue, 14 Oct 2025 14:06:04 +0300 Subject: [PATCH 79/94] I finished writing Queue and added all tests on it --- CMakeLists.txt | 1 + lib_queue/CMakeLists.txt | 1 + lib_queue/queue.cpp | 0 lib_queue/queue.h | 101 +++++++++++++++++++++++ tests/test_queue.cpp | 168 +++++++++++++++++++++++++++++++++++++++ 5 files changed, 271 insertions(+) create mode 100644 lib_queue/CMakeLists.txt create mode 100644 lib_queue/queue.cpp create mode 100644 lib_queue/queue.h create mode 100644 tests/test_queue.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 5b9f901f..05b0acc4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -24,6 +24,7 @@ add_subdirectory(lib_mathvector) add_subdirectory(lib_matrix) add_subdirectory(lib_triangle_matrix) add_subdirectory(lib_stack) +add_subdirectory(lib_queue) add_subdirectory(main) # подключаем дополнительный CMakeLists.txt из подкаталога с именем main option(BTEST "build test?" ON) # указываем подключаем ли google-тесты (ON или YES) или нет (OFF или NO) diff --git a/lib_queue/CMakeLists.txt b/lib_queue/CMakeLists.txt new file mode 100644 index 00000000..f0846b24 --- /dev/null +++ b/lib_queue/CMakeLists.txt @@ -0,0 +1 @@ +create_project_lib(Queue) \ No newline at end of file diff --git a/lib_queue/queue.cpp b/lib_queue/queue.cpp new file mode 100644 index 00000000..e69de29b diff --git a/lib_queue/queue.h b/lib_queue/queue.h new file mode 100644 index 00000000..f60ded88 --- /dev/null +++ b/lib_queue/queue.h @@ -0,0 +1,101 @@ +// Copyright 2025 Ekaterina Ushnitskaya + +#ifndef LIB_QUEUE_QUEUE_H +#define LIB_QUEUE_QUEUE_H + +#include +#include + +template +class Queue { + T* _data; + size_t _size; + size_t _head; + size_t _tail; + size_t _count; +public: + static const size_t RESERVE_MEMORY = 15; + Queue() : _data(new T[RESERVE_MEMORY]), _size(RESERVE_MEMORY), _head(0), _tail(0), _count(0) {} + explicit Queue(size_t size) : _data(new T[size]), _size(size), _head(0), _tail(0), _count(0) {} + + Queue(const Queue& other) : _data(new T[other._size]), _size(other._size), _head(other._head), _tail(other._tail), _count(other._count) { + for (size_t i = 0; i < _size; ++i) { + _data[i] = other._data[i]; + } + } + ~Queue() noexcept { delete[] _data; } + + T& tail() { + if (is_empty()) { + throw std::underflow_error("Queue is empty (no tail element)"); + } + size_t last; + if (_tail == 0) { + last = _size - 1; + } + else { + last = _tail - 1; + } + return _data[last]; + } + const T& tail() const { + if (is_empty()) { + throw std::underflow_error("Queue is empty (no tail element)"); + } + size_t last; + if (_tail == 0) { + last = _size - 1; + } + else { + last = _tail - 1; + } + return _data[last]; + } + T& head() { + if (is_empty()) { + throw std::underflow_error("Queue is empty (no head element)"); + } + return _data[_head]; + } + const T& head() const { + if (is_empty()) { + throw std::underflow_error("Queue is empty (no head element)"); + } + return _data[_head]; + } + size_t capacity() const noexcept { + return _size; + } + size_t size() const noexcept { + return _count; + } + bool is_empty() const noexcept { + return _count == 0; + } + bool is_full() const noexcept { + return _count == _size; + } + void clear() noexcept { + _head = 0; + _tail = 0; + _count = 0; + } + + void push(T value) { + if (is_full()) { + throw std::overflow_error("Queue overflow (can't push element)"); + } + _data[_tail] = value; + _tail = (_tail + 1) % _size; + ++_count; + } + void pop() { + if (is_empty()) { + throw std::underflow_error("Queue underflow (can't pop element)"); + } + _head = (_head + 1) % _size; + --_count; + } +}; + +#endif // LIB_QUEUE_QUEUE_H \ No newline at end of file diff --git a/tests/test_queue.cpp b/tests/test_queue.cpp new file mode 100644 index 00000000..9d8f5cfd --- /dev/null +++ b/tests/test_queue.cpp @@ -0,0 +1,168 @@ +// Copyright 2025 Ekaterina Ushnitskaya + +#include +#include "../lib_queue/queue.h" + +#define EPSILON 0.000001 +#define TRUE 1 +#define FALSE 0 + +TEST(TestQueueLib, default_constructor_creates_empty_queue) { + // Arrange + Queue queue; + + // Act & Assert + EXPECT_TRUE(queue.is_empty()); + EXPECT_FALSE(queue.is_full()); + EXPECT_EQ(0, queue.size()); + EXPECT_EQ(Queue::RESERVE_MEMORY, queue.capacity()); +} + +TEST(TestQueueLib, constructor_with_size) { + // Arrange + size_t size = 10; + Queue queue(size); + + // Act & Assert + EXPECT_EQ(size, queue.capacity()); + EXPECT_TRUE(queue.is_empty()); + EXPECT_FALSE(queue.is_full()); +} + +TEST(TestQueueLib, copy_constructor) { + // Arrange + size_t size = 10; + Queue queue1(size); + + // Act + Queue queue2(queue1); + + // Assert + EXPECT_EQ(queue1.size(), queue2.size()); + EXPECT_EQ(queue1.is_empty(), queue2.is_empty()); + EXPECT_EQ(queue1.is_full(), queue2.is_full()); +} + +TEST(TestQueueLib, test_push_and_check_head_and_tail_without_an_overflowing_queue) { + // Arrange + Queue queue; + + // Act + queue.push(10); + queue.push(20); + queue.push(30); + + // Assert + EXPECT_EQ(3, queue.size()); + EXPECT_EQ(10, queue.head()); + EXPECT_EQ(30, queue.tail()); + EXPECT_FALSE(queue.is_empty()); +} + +TEST(TestQueueLib, test_push_and_check_head_and_tail_with_an_overflowing_queue) { + // Arrange + Queue queue(3); + + // Act + queue.push(10); + queue.push(20); + queue.push(30); + + // Assert + EXPECT_ANY_THROW(queue.push(40)); +} + +TEST(TestQueueLib, test_pop_without_an_underflowing_queue) { + // Arrange + Queue queue; + queue.push(1); + queue.push(2); + queue.push(3); + + // Act + queue.pop(); + + // Assert + EXPECT_FALSE(queue.is_empty()); + EXPECT_EQ(2, queue.size()); + EXPECT_EQ(2, queue.head()); + EXPECT_EQ(3, queue.tail()); +} + +TEST(TestQueueLib, test_pop_all_elements_in_queue_without_an_underflowing_stack) { + // Arrange + Queue queue; + queue.push(1); + + // Act + queue.pop(); + + // Assert + EXPECT_TRUE(queue.is_empty()); + EXPECT_EQ(0, queue.size()); +} + +TEST(TestQueueLib, test_pop_with_an_underflowing_queue) { + // Arrange + Queue queue; + + // Act & Assert + EXPECT_TRUE(queue.is_empty()); + EXPECT_ANY_THROW(queue.pop()); +} + +TEST(TestQueueLib, test_clear) { + // Arrange + Queue queue; + queue.push(1); + queue.push(2); + + // Act + queue.clear(); + + // Assert + EXPECT_TRUE(queue.is_empty()); + EXPECT_EQ(0, queue.size()); +} + +TEST(TestQueueLib, test_is_full_checking_for_a_full_queue) { + // Arrange + Queue queue(3); + queue.push(1); + queue.push(2); + queue.push(3); + + // Act & Assert + EXPECT_TRUE(queue.is_full()); + EXPECT_ANY_THROW(queue.push(4)); +} + +TEST(TestQueueLib, test_is_full_checking_for_an_empty_queue) { + // Arrange + Queue queue(3); + + // Act & Assert + EXPECT_FALSE(queue.is_full()); + EXPECT_ANY_THROW(queue.pop()); +} + +TEST(TestQueueLib, test_is_empty_checking_for_a_full_queue) { + // Arrange + Queue queue(3); + queue.push(1); + queue.push(2); + queue.push(3); + + // Act & Assert + EXPECT_FALSE(queue.is_empty()); + EXPECT_ANY_THROW(queue.push(4)); +} + +TEST(TestQueueLib, test_is_empty_checking_for_an_empty_queue) { + // Arrange + Queue queue(3); + + // Act & Assert + EXPECT_TRUE(queue.is_empty()); + EXPECT_ANY_THROW(queue.pop()); +} \ No newline at end of file From 6b04a2c64ca65c83d45c9b3ed97335738ace0bbe Mon Sep 17 00:00:00 2001 From: Kate-Nits Date: Thu, 4 Dec 2025 00:50:48 +0300 Subject: [PATCH 80/94] I fixed all Marina Andreevna's comments about the stack and queue classes: I changed the logic of the variable names, moved the realization beyond the class and wrote added tests --- lib_queue/queue.h | 155 ++++++++++++++++++++++++------------------ lib_stack/stack.h | 108 ++++++++++++++++++----------- lib_tvector/tvector.h | 12 ++++ tests/test_queue.cpp | 55 ++++++++++++++- tests/test_stack.cpp | 22 ++++-- 5 files changed, 240 insertions(+), 112 deletions(-) diff --git a/lib_queue/queue.h b/lib_queue/queue.h index f60ded88..f8f12589 100644 --- a/lib_queue/queue.h +++ b/lib_queue/queue.h @@ -10,11 +10,11 @@ template class Queue { T* _data; size_t _size; - size_t _head; - size_t _tail; + size_t _head; // + size_t _tail; // size_t _count; public: - static const size_t RESERVE_MEMORY = 15; + static const size_t RESERVE_MEMORY = 20; Queue() : _data(new T[RESERVE_MEMORY]), _size(RESERVE_MEMORY), _head(0), _tail(0), _count(0) {} explicit Queue(size_t size) : _data(new T[size]), _size(size), _head(0), _tail(0), _count(0) {} @@ -25,77 +25,100 @@ class Queue { } ~Queue() noexcept { delete[] _data; } - T& tail() { - if (is_empty()) { - throw std::underflow_error("Queue is empty (no tail element)"); - } - size_t last; - if (_tail == 0) { - last = _size - 1; - } - else { - last = _tail - 1; - } - return _data[last]; - } - const T& tail() const { - if (is_empty()) { - throw std::underflow_error("Queue is empty (no tail element)"); - } - size_t last; - if (_tail == 0) { - last = _size - 1; - } - else { - last = _tail - 1; - } - return _data[last]; + T& tail(); + const T& tail() const; + T& head(); + const T& head() const; + inline size_t capacity() const noexcept; + inline size_t size() const noexcept; + bool is_empty() const noexcept; + bool is_full() const noexcept; + void clear() noexcept; + + void push(T value); + void pop(); +}; + + +template +T& Queue::tail() { + if (is_empty()) { + throw std::underflow_error("Queue is empty (no tail element)"); } - T& head() { - if (is_empty()) { - throw std::underflow_error("Queue is empty (no head element)"); - } - return _data[_head]; + size_t last; + if (_tail == 0) { + last = _size - 1; } - const T& head() const { - if (is_empty()) { - throw std::underflow_error("Queue is empty (no head element)"); - } - return _data[_head]; + else { + last = _tail - 1; } - size_t capacity() const noexcept { - return _size; + return _data[last]; +} +template +const T& Queue::tail() const { + if (is_empty()) { + throw std::underflow_error("Queue is empty (no tail element)"); } - size_t size() const noexcept { - return _count; + size_t last; + if (_tail == 0) { + last = _size - 1; } - bool is_empty() const noexcept { - return _count == 0; + else { + last = _tail - 1; } - bool is_full() const noexcept { - return _count == _size; + return _data[last]; +} +template +T& Queue::head() { + if (is_empty()) { + throw std::underflow_error("Queue is empty (no head element)"); } - void clear() noexcept { - _head = 0; - _tail = 0; - _count = 0; + return _data[_head]; +} +template +const T& Queue::head() const { + if (is_empty()) { + throw std::underflow_error("Queue is empty (no head element)"); } - - void push(T value) { - if (is_full()) { - throw std::overflow_error("Queue overflow (can't push element)"); - } - _data[_tail] = value; - _tail = (_tail + 1) % _size; - ++_count; + return _data[_head]; +} +template +inline size_t Queue::capacity() const noexcept { + return _size; +} +template +inline size_t Queue::size() const noexcept { + return _count; +} +template +bool Queue::is_empty() const noexcept { + return _count == 0; +} +template +bool Queue::is_full() const noexcept { + return _count == _size; +} +template +void Queue::clear() noexcept { + _head = 0; + _tail = 0; + _count = 0; +} +template +void Queue::push(T value) { + if (is_full()) { + throw std::overflow_error("Queue overflow (can't push element)"); } - void pop() { - if (is_empty()) { - throw std::underflow_error("Queue underflow (can't pop element)"); - } - _head = (_head + 1) % _size; - --_count; + _data[_tail] = value; + _tail = (_tail + 1) % _size; + ++_count; +} +template +void Queue::pop() { + if (is_empty()) { + throw std::underflow_error("Queue underflow (can't pop element)"); } -}; - + _head = (_head + 1) % _size; + --_count; +} #endif // LIB_QUEUE_QUEUE_H \ No newline at end of file diff --git a/lib_stack/stack.h b/lib_stack/stack.h index 8b39e514..c2023736 100644 --- a/lib_stack/stack.h +++ b/lib_stack/stack.h @@ -11,52 +11,84 @@ template class Stack { TVector _data; + size_t _count; // size_t _max_capacity; // public: - Stack() : _data(), _max_capacity(0) {} - explicit Stack(size_t size) : _data(0), _max_capacity(size) {} - Stack(const Stack& other) : _data(other._data), _max_capacity(other._max_capacity) {} - - size_t size() const noexcept { - return _data.size() - _data.deleted(); - } - bool is_full() const noexcept { - return _max_capacity > 0 && this->size() >= _max_capacity; - } - bool is_empty() const noexcept { - return _data.is_empty(); + static const size_t RESERVE_MEMORY = 20; + Stack() : _data(), _count(0), _max_capacity(RESERVE_MEMORY) { + _data.reserve(_max_capacity); } - void clear() noexcept { - _data.clear(); + explicit Stack(size_t size) : _data(), _count(0), _max_capacity(size) { + _data.reserve(_max_capacity); } + Stack(const Stack& other) : _data(other._data), _count(other._count), _max_capacity(other._max_capacity) {} - void push(const T& value) { - if (is_full()) { - throw std::overflow_error("Stack overflow: cannot push, because stack is full"); - } - _data.push_back(value); - } + inline size_t count() const noexcept; + inline size_t capacity() const noexcept; + bool is_full() const noexcept; + bool is_empty() const noexcept; + void clear() noexcept; - void pop() { - if (is_empty()) { - throw std::underflow_error("Stack is empty (can't pop from empty stack)"); - } - _data.pop_back(); - } + void push(const T& value); + void pop(); + T& top(); + const T& top() const; +}; - T& top() { - if (is_empty()) { - throw std::underflow_error("Stack is empty (no top element)"); - } - return _data.back(); + +template +inline size_t Stack::count() const noexcept { + return _count; +} +template +inline size_t Stack::capacity() const noexcept { + return _max_capacity; +} +template +bool Stack::is_full() const noexcept { + return _count >= _max_capacity; +} +template +bool Stack::is_empty() const noexcept { + return _count == 0; +} +template +void Stack::clear() noexcept { + _count = 0; + _data.clear(); +} +template +void Stack::push(const T& value) { + if (is_full()) { + throw std::overflow_error("Stack overflow: cannot push, because stack is full"); } - const T& top() const { - if (is_empty()) { - throw std::underflow_error("Stack is empty (no top element"); - } - return _data.back(); + if (_data.size() >= _data.capacity()) { + throw std::overflow_error("Stack overflow: stack is full"); } -}; - + _data.push_back(value); + ++_count; +} +template +void Stack::pop() { + if (is_empty()) { + throw std::underflow_error("Stack is empty (can't pop from empty stack)"); + } + _data.pop_back(); + --_count; +} +template +T& Stack::top() { + if (is_empty()) { + throw std::underflow_error("Stack is empty (no top element)"); + } + return _data.back(); +} +template +const T& Stack::top() const { + if (is_empty()) { + throw std::underflow_error("Stack is empty (no top element"); + } + return _data.back(); +} #endif // LIB_STACK_STACK_H \ No newline at end of file diff --git a/lib_tvector/tvector.h b/lib_tvector/tvector.h index 964f8ed2..ff69de27 100644 --- a/lib_tvector/tvector.h +++ b/lib_tvector/tvector.h @@ -162,6 +162,12 @@ class TVector { return _data[i]; throw std::out_of_range("No busy elements in vector"); } + inline const T& back() const { //Доступ к последнему элементу, который не deleted и не empty + for (size_t i = _size; i-- > 0; ) + if (_states[i] == busy) + return _data[i]; + throw std::out_of_range("No busy elements in vector"); + } inline bool is_empty() const noexcept { return (_size - _deleted) == 0; } @@ -254,6 +260,12 @@ class TVector { _states[real_index] = State::busy; } + void emplace(size_t index, const T& value) { + size_t real_index = check_index(index); + _data[real_index] = value; + _states[real_index] = State::busy; + } + TVector& assign(const TVector& other) { if (this != &other) { delete[] _data; diff --git a/tests/test_queue.cpp b/tests/test_queue.cpp index 9d8f5cfd..59c60233 100644 --- a/tests/test_queue.cpp +++ b/tests/test_queue.cpp @@ -29,7 +29,7 @@ TEST(TestQueueLib, constructor_with_size) { EXPECT_FALSE(queue.is_full()); } -TEST(TestQueueLib, copy_constructor) { +TEST(TestQueueLib, copy_constructor_without_difficulties) { // Arrange size_t size = 10; Queue queue1(size); @@ -43,6 +43,24 @@ TEST(TestQueueLib, copy_constructor) { EXPECT_EQ(queue1.is_full(), queue2.is_full()); } +TEST(TestQueueLib, copy_constructor_with_overflow_head_and_tail) { + // Arrange + Queue queue1(3); + + // Act + queue1.push(1); + queue1.push(2); + queue1.push(3); + queue1.pop(); + queue1.push(4); + Queue queue2(queue1); + + // Assert + EXPECT_EQ(queue1.size(), queue2.size()); + EXPECT_EQ(queue1.head(), queue2.head()); + EXPECT_EQ(queue1.tail(), queue2.tail()); +} + TEST(TestQueueLib, test_push_and_check_head_and_tail_without_an_overflowing_queue) { // Arrange Queue queue; @@ -165,4 +183,39 @@ TEST(TestQueueLib, test_is_empty_checking_for_an_empty_queue) { // Act & Assert EXPECT_TRUE(queue.is_empty()); EXPECT_ANY_THROW(queue.pop()); +} + +TEST(TestQueueLib, test_push_after_overflow_tail) { + // Arrange + Queue queue(3); + + // Act + queue.push(1); + queue.push(2); + queue.pop(); + queue.pop(); + queue.push(100); + queue.push(200); + + // Assert + EXPECT_EQ(2, queue.size()); + EXPECT_EQ(100, queue.head()); + EXPECT_EQ(200, queue.tail()); +} + +TEST(TestQueueLib, test_push_after_overflow_head) { + // Arrange + Queue queue(3); + + // Act + queue.push(1); + queue.push(2); + queue.push(3); + queue.pop(); + queue.push(555); + + // Assert + EXPECT_EQ(3, queue.size()); + EXPECT_EQ(2, queue.head()); + EXPECT_EQ(555, queue.tail()); } \ No newline at end of file diff --git a/tests/test_stack.cpp b/tests/test_stack.cpp index dea2bba9..04a4acb7 100644 --- a/tests/test_stack.cpp +++ b/tests/test_stack.cpp @@ -12,7 +12,8 @@ TEST(TestStackLib, default_constructor_creates_empty_stack) { Stack stack; // Act & Assert - EXPECT_EQ(0, stack.size()); + EXPECT_EQ(0, stack.count()); + EXPECT_EQ(20, stack.capacity()); EXPECT_TRUE(stack.is_empty()); EXPECT_FALSE(stack.is_full()); } @@ -23,7 +24,8 @@ TEST(TestStackLib, constructor_with_capacity) { Stack stack(capacity); // Act & Assert - EXPECT_EQ(0, stack.size()); + EXPECT_EQ(0, stack.count()); + EXPECT_EQ(capacity, stack.capacity()); EXPECT_TRUE(stack.is_empty()); EXPECT_FALSE(stack.is_full()); } @@ -32,12 +34,15 @@ TEST(TestStackLib, copy_constructor) { // Arrange size_t capacity = 10; Stack stack1(capacity); + stack1.push(10); + stack1.push(20); // Act Stack stack2(stack1); // Assert - EXPECT_EQ(stack1.size(), stack2.size()); + EXPECT_EQ(stack1.count(), stack2.count()); + EXPECT_EQ(stack1.capacity(), stack2.capacity()); EXPECT_EQ(stack1.is_empty(), stack2.is_empty()); EXPECT_EQ(stack1.is_full(), stack2.is_full()); } @@ -52,7 +57,7 @@ TEST(TestStackLib, test_push_and_top_without_an_overflowing_stack) { // Assert EXPECT_FALSE(stack.is_empty()); - EXPECT_EQ(2, stack.size()); + EXPECT_EQ(2, stack.count()); EXPECT_EQ(20, stack.top()); } @@ -65,6 +70,7 @@ TEST(TestStackLib, test_push_with_an_overflowing_stack) { stack.push(20); // Assert + EXPECT_TRUE(stack.is_full()); EXPECT_ANY_THROW(stack.push(30)); } @@ -89,7 +95,7 @@ TEST(TestStackLib, test_pop_not_all_elements_in_stack_and_top_without_an_underfl // Assert EXPECT_FALSE(stack.is_empty()); - EXPECT_EQ(2, stack.size()); + EXPECT_EQ(2, stack.count()); EXPECT_EQ(2, stack.top()); } @@ -103,7 +109,8 @@ TEST(TestStackLib, test_pop_all_elements_in_stack_and_top_without_an_underflowin // Assert EXPECT_TRUE(stack.is_empty()); - EXPECT_EQ(0, stack.size()); + EXPECT_EQ(0, stack.count()); + EXPECT_ANY_THROW(stack.top()); } TEST(TestStackLib, test_pop_with_an_underflowing_stack) { @@ -127,8 +134,9 @@ TEST(TestStackLib, test_clear) { // Assert EXPECT_TRUE(stack.is_empty()); - EXPECT_EQ(0, stack.size()); + EXPECT_EQ(0, stack.count()); EXPECT_ANY_THROW(stack.top()); + EXPECT_NO_THROW(stack.push(10)); } TEST(TestStackLib, test_is_full_checking_for_a_full_stack) { From f99e4a009cbd56d596cb87e515527fe252c5e67f Mon Sep 17 00:00:00 2001 From: Kate-Nits Date: Thu, 4 Dec 2025 21:00:20 +0300 Subject: [PATCH 81/94] Removed the user input and made a random choice of the position in the gradient_descent --- lib_algorithms/algorithms.h | 9 +++++---- tests/test_algorithms.cpp | 24 +++++------------------- 2 files changed, 10 insertions(+), 23 deletions(-) diff --git a/lib_algorithms/algorithms.h b/lib_algorithms/algorithms.h index 260d2ee0..cda39d04 100644 --- a/lib_algorithms/algorithms.h +++ b/lib_algorithms/algorithms.h @@ -4,6 +4,7 @@ #ifndef LIB_ALGORITHMS_H #define LIB_ALGORITHMS_H +#include #include "../lib_circle/circle.h" #include "../lib_sphere/sphere.h" #include "../lib_tvector/tvector.h" @@ -91,6 +92,7 @@ int what_number_less(const T& num1, const T& num2, const T& num3, const T& num4) } template T gradient_descent(const Matrix& matrix) { + srand(time(0)); if (matrix.cols() != matrix.rows()) { throw std::invalid_argument("The Matrix should be square"); } @@ -99,10 +101,9 @@ T gradient_descent(const Matrix& matrix) { } size_t start_row_pos = 0, start_col_pos = 0; - std::cout << "Input start position row: "; - std::cin >> start_row_pos; - std::cout << "Input start position column: "; - std::cin >> start_col_pos; + start_row_pos = rand() % matrix.rows() + 0; + start_col_pos = rand() % matrix.cols() + 0; + if (start_row_pos >= matrix.rows() || start_col_pos >= matrix.cols()) { throw std::out_of_range("Start position out of matrix bounds"); } diff --git a/tests/test_algorithms.cpp b/tests/test_algorithms.cpp index 4038e7cd..227f03d3 100644 --- a/tests/test_algorithms.cpp +++ b/tests/test_algorithms.cpp @@ -10,38 +10,24 @@ #define TRUE 1 #define FALSE 0 -TEST(TestAlgorithmsLib, test_position00_value11_from_example) { +TEST(TestAlgorithmsLib, test_gradient_descent_from_example1) { // Arrange () - Matrix matrix{ {11, 15, 10, 9}, {6, 16, 3, 8}, {7, 4, 2, 13}, {14, 12, 1, 5} }; + Matrix matrix{ {3, 1, 2}, {5, 8, 4}, {7, 6, 9} }; // Act - std::cout << "Testing with start position [0][0] = value 11" << std::endl; int res = gradient_descent(matrix); // Assert - EXPECT_EQ(6, res); + EXPECT_TRUE(res == 6 || res == 1); } -TEST(TestAlgorithmsLib, test_position20_value7_from_example) { +TEST(TestAlgorithmsLib, test_gradient_descent_from_example2) { // Arrange () Matrix matrix{ {11, 15, 10, 9}, {6, 16, 3, 8}, {7, 4, 2, 13}, {14, 12, 1, 5} }; // Act - std::cout << "Testing with start position [2][0] = value 7" << std::endl; - int res = gradient_descent(matrix); - - // Assert - EXPECT_EQ(1, res); -} - -TEST(TestAlgorithmsLib, test_position22_value9_from_example) { - // Arrange () - Matrix matrix{ {3, 1, 2}, {5, 8, 4}, {7, 6, 9} }; - - // Act - std::cout << "Testing with start position [2][2] = value 9" << std::endl; int res = gradient_descent(matrix); // Assert - EXPECT_EQ(1, res); + EXPECT_TRUE(res == 6 || res == 1); } \ No newline at end of file From 31a50d83b496c7fe6dd4028fd16aa3ebefd97be2 Mon Sep 17 00:00:00 2001 From: Kate-Nits Date: Sat, 6 Dec 2025 16:55:14 +0300 Subject: [PATCH 82/94] I added lib_node and lib_list, declared them and wrote an realization of the struct Node --- CMakeLists.txt | 2 ++ lib_list/CMakeLists.txt | 2 ++ lib_list/list.cpp | 0 lib_list/list.h | 28 ++++++++++++++++++++++++++++ lib_node/CMakeLists.txt | 1 + lib_node/node.cpp | 0 lib_node/node.h | 16 ++++++++++++++++ tests/test_list.cpp | 0 tests/test_node.cpp | 0 9 files changed, 49 insertions(+) create mode 100644 lib_list/CMakeLists.txt create mode 100644 lib_list/list.cpp create mode 100644 lib_list/list.h create mode 100644 lib_node/CMakeLists.txt create mode 100644 lib_node/node.cpp create mode 100644 lib_node/node.h create mode 100644 tests/test_list.cpp create mode 100644 tests/test_node.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 05b0acc4..d16551f4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -25,6 +25,8 @@ add_subdirectory(lib_matrix) add_subdirectory(lib_triangle_matrix) add_subdirectory(lib_stack) add_subdirectory(lib_queue) +add_subdirectory(lib_node) +add_subdirectory(lib_list) add_subdirectory(main) # подключаем дополнительный CMakeLists.txt из подкаталога с именем main option(BTEST "build test?" ON) # указываем подключаем ли google-тесты (ON или YES) или нет (OFF или NO) diff --git a/lib_list/CMakeLists.txt b/lib_list/CMakeLists.txt new file mode 100644 index 00000000..ae414a82 --- /dev/null +++ b/lib_list/CMakeLists.txt @@ -0,0 +1,2 @@ +create_project_lib(List) +add_depend(List Node lib_node) \ No newline at end of file diff --git a/lib_list/list.cpp b/lib_list/list.cpp new file mode 100644 index 00000000..e69de29b diff --git a/lib_list/list.h b/lib_list/list.h new file mode 100644 index 00000000..7bdc3b68 --- /dev/null +++ b/lib_list/list.h @@ -0,0 +1,28 @@ +// Copyright 2025 Ekaterina Ushnitskaya + +#ifndef LIB_LIST_LIST_H +#define LIB_LIST_LIST_H + +#include +#include + +#include "../lib_node/node.h" + +template +class List { + Node* _head; + Node* _tail; +public: + List(); + List(const List&); + ~List(); + Node* head(); + Node* tail(); + bool is_empty(); + void push_front(const T& value) noexcept; + void push_back(const T& value) noexcept; + void insert(size_t pos, const T& value); + void insert(Node* node, const T& value); +}; + +#endif \ No newline at end of file diff --git a/lib_node/CMakeLists.txt b/lib_node/CMakeLists.txt new file mode 100644 index 00000000..33f68928 --- /dev/null +++ b/lib_node/CMakeLists.txt @@ -0,0 +1 @@ +create_project_lib(Node) \ No newline at end of file diff --git a/lib_node/node.cpp b/lib_node/node.cpp new file mode 100644 index 00000000..e69de29b diff --git a/lib_node/node.h b/lib_node/node.h new file mode 100644 index 00000000..19bbaaf8 --- /dev/null +++ b/lib_node/node.h @@ -0,0 +1,16 @@ +// Copyright 2025 Ekaterina Ushnitskaya + +#ifndef LIB_NODE_NODE_H +#define LIB_NODE_NODE_H + +#include + +template +struct Node { + T value; + Node* next; + + explicit Node(const T& value_, Node* next_ = nullptr) : value(value_), next(next_) {} +}; + +#endif \ No newline at end of file diff --git a/tests/test_list.cpp b/tests/test_list.cpp new file mode 100644 index 00000000..e69de29b diff --git a/tests/test_node.cpp b/tests/test_node.cpp new file mode 100644 index 00000000..e69de29b From b0e4a357e4f5ff6685b359b00bf4ee2c34159435 Mon Sep 17 00:00:00 2001 From: Kate-Nits Date: Mon, 8 Dec 2025 02:48:40 +0300 Subject: [PATCH 83/94] Wrote the class List and wrote all tests for it --- lib_list/list.h | 197 +++++++++++- tests/test_algorithms.cpp | 4 +- tests/test_list.cpp | 623 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 812 insertions(+), 12 deletions(-) diff --git a/lib_list/list.h b/lib_list/list.h index 7bdc3b68..48122f36 100644 --- a/lib_list/list.h +++ b/lib_list/list.h @@ -13,16 +13,193 @@ class List { Node* _head; Node* _tail; public: - List(); - List(const List&); - ~List(); - Node* head(); - Node* tail(); - bool is_empty(); - void push_front(const T& value) noexcept; - void push_back(const T& value) noexcept; - void insert(size_t pos, const T& value); - void insert(Node* node, const T& value); + List() : _head(nullptr), _tail(nullptr) {} + List(const List& other) : _head(nullptr), _tail(nullptr) { + Node* cur = other._head; + while (cur != nullptr) { + push_back(cur->value); + cur = cur->next; + } + } + ~List() { + clear(); + } + inline Node* head() { return _head; } + inline const Node* head() const { + return this->_head; + } + inline Node* tail() { return _tail; } + inline const Node* tail() const { + return this->_tail; + } + bool is_empty() const { + return _head == nullptr; + } + void push_front(const T& value) { + Node* node = new Node(value, _head); + if (is_empty()) { + _tail = node; + } + _head = node; + } + void push_back(const T& value) { + Node* node = new Node(value, nullptr); + if (is_empty()) { + _head = node; + _tail = node; + } + else { + _tail->next = node; + _tail = node; + } + } + void insert(size_t pos, const T& value) { + if (pos == 0) { + push_front(value); + return; + } + Node* cur = _head; + for (size_t i = 0; i < pos - 1; ++i) { + if (cur == nullptr) { + throw std::out_of_range("Position out of range"); + } + cur = cur->next; + } + if (cur == nullptr) { + throw std::out_of_range("Position out of range"); + } + if (cur == _tail) { + push_back(value); + } + else { + Node* node = new Node(value, cur->next); + cur->next = node; + } + } + void insert(Node* node, const T& value) { + if (node == nullptr) { + throw std::invalid_argument("Node can't be nullptr"); + } + if (!is_node_in_list(node)) { + throw std::invalid_argument("Node is not in List"); + } + Node* new_node = new Node(value, node->next); + node->next = new_node; + if (node == _tail) { + _tail = new_node; + } + } + void pop_front() { + if (is_empty()) { + throw std::underflow_error("List is empty"); + } + Node* old_head = _head; + _head = _head->next; + if (old_head == _tail) { + _tail = nullptr; + } + delete old_head; + } + void pop_back() { + if (is_empty()) { + throw std::underflow_error("List is empty"); + } + if (_head == _tail) { + Node* node_to_delete = _head; + _head = nullptr; + _tail = nullptr; + delete node_to_delete; + return; + } + Node* cur = _head; + while (cur->next != _tail) { + cur = cur->next; + } + Node* node_to_delete = _tail; + _tail = cur; + cur->next = nullptr; + delete node_to_delete; + } + void erase(size_t pos) { + if (is_empty()) { + throw std::underflow_error("List is empty"); + } + if (pos == 0) { + pop_front(); + return; + } + Node* cur = _head; + for (size_t i = 0; i < pos - 1; ++i) { + if (cur == nullptr) { + throw std::out_of_range("Position out of range"); + } + cur = cur->next; + } + if (cur == nullptr || cur->next == nullptr) { + throw std::out_of_range("Position out of range"); + } + Node* node_to_delete = cur->next; + if (node_to_delete == _tail) { + _tail = cur; + } + cur->next = node_to_delete->next; + delete node_to_delete; + } + void erase(Node* node) { + if (node == nullptr) { + throw std::invalid_argument("Node can't be nullptr"); + } + if (!is_node_in_list(node)) { + throw std::invalid_argument("Node is not in List"); + } + if (node == _head) { + pop_front(); + return; + } + Node* cur = _head; + while (cur->next != node) { + cur = cur->next; + } + Node* node_to_delete = cur->next; + if (node_to_delete == _tail) { + _tail = cur; + } + cur->next = node_to_delete->next; + delete node_to_delete; + } + List& operator=(const List& other) { + if (this == &other) { + return *this; + } + clear(); + Node* other_cur = other._head; + while (other_cur != nullptr) { + push_back(other_cur->value); + other_cur = other_cur->next; + } + return *this; + } +private: + void clear() { + Node* cur = _head; + while (cur != nullptr) { + Node* next_node = cur->next; + delete cur; + cur = next_node; + } + _head = nullptr; + _tail = nullptr; + } + bool is_node_in_list(Node* node) const { + Node* cur = _head; + while (cur != nullptr) { + if (cur == node) { + return true; + } + cur = cur->next; + } + return false; + } }; #endif \ No newline at end of file diff --git a/tests/test_algorithms.cpp b/tests/test_algorithms.cpp index 227f03d3..abc21c95 100644 --- a/tests/test_algorithms.cpp +++ b/tests/test_algorithms.cpp @@ -11,7 +11,7 @@ #define FALSE 0 TEST(TestAlgorithmsLib, test_gradient_descent_from_example1) { - // Arrange () + // Arrange Matrix matrix{ {3, 1, 2}, {5, 8, 4}, {7, 6, 9} }; // Act @@ -22,7 +22,7 @@ TEST(TestAlgorithmsLib, test_gradient_descent_from_example1) { } TEST(TestAlgorithmsLib, test_gradient_descent_from_example2) { - // Arrange () + // Arrange Matrix matrix{ {11, 15, 10, 9}, {6, 16, 3, 8}, {7, 4, 2, 13}, {14, 12, 1, 5} }; // Act diff --git a/tests/test_list.cpp b/tests/test_list.cpp index e69de29b..8162244b 100644 --- a/tests/test_list.cpp +++ b/tests/test_list.cpp @@ -0,0 +1,623 @@ +// Copyright 2025 Ekaterina Ushnitskaya + +#include +#include "../lib_list/list.h" + +#define EPSILON 0.000001 +#define TRUE 1 +#define FALSE 0 + +TEST(TestListLib, default_constructor) { + // Arrange & Act + List list; + + // Assert + EXPECT_EQ(nullptr, list.head()); + EXPECT_EQ(nullptr, list.tail()); + EXPECT_TRUE(list.is_empty()); +} + +TEST(TestListLib, copy_constructor_with_empty_list) { + // Arrange + List list1; + + // Act + List list2(list1); + + // Assert + EXPECT_TRUE(list2.is_empty()); + EXPECT_EQ(list1.head(), list2.head()); + EXPECT_EQ(list1.tail(), list2.tail()); + EXPECT_EQ(nullptr, list2.head()); + EXPECT_EQ(nullptr, list2.tail()); +} + +TEST(TestListLib, copy_constructor_with_not_empty_list) { + // Arrange + List list1; + list1.push_back(1); + list1.push_back(2); + list1.push_back(3); + + // Act + List list2(list1); + + // Assert + EXPECT_FALSE(list2.is_empty()); + EXPECT_EQ(list1.head()->value, list2.head()->value); + EXPECT_EQ(list1.tail()->value, list2.tail()->value); + EXPECT_EQ(1, list2.head()->value); + EXPECT_EQ(3, list2.tail()->value); +} + +TEST(TestListLib, push_front_to_empty_list_one_element) { + // Arrange + List list; + + // Act + list.push_front(678); + + // Assert + EXPECT_FALSE(list.is_empty()); + EXPECT_EQ(678, list.head()->value); + EXPECT_EQ(678, list.tail()->value); + EXPECT_EQ(list.head(), list.tail()); +} + +TEST(TestListLib, push_front_to_empty_list_multiple_elements) { + // Arrange + List list; + + // Act + list.push_front(1); + list.push_front(2); + list.push_front(3); + + // Assert + EXPECT_FALSE(list.is_empty()); + EXPECT_EQ(3, list.head()->value); + EXPECT_EQ(1, list.tail()->value); + Node* cur = list.head(); + EXPECT_EQ(3, cur->value); + cur = cur->next; + EXPECT_EQ(2, cur->value); + cur = cur->next; + EXPECT_EQ(1, cur->value); + EXPECT_EQ(nullptr, cur->next); +} + +TEST(TestListLib, push_back_to_empty_list_one_element) { + // Arrange + List list; + + // Act + list.push_back(678); + + // Assert + EXPECT_FALSE(list.is_empty()); + EXPECT_EQ(678, list.head()->value); + EXPECT_EQ(678, list.tail()->value); + EXPECT_EQ(list.head(), list.tail()); +} + +TEST(TestListLib, push_back_to_empty_list_multiple_elements) { + // Arrange + List list; + + // Act + list.push_back(1); + list.push_back(2); + list.push_back(3); + + // Assert + EXPECT_FALSE(list.is_empty()); + EXPECT_EQ(1, list.head()->value); + EXPECT_EQ(3, list.tail()->value); + Node* cur = list.head(); + EXPECT_EQ(1, cur->value); + cur = cur->next; + EXPECT_EQ(2, cur->value); + cur = cur->next; + EXPECT_EQ(3, cur->value); + EXPECT_EQ(nullptr, cur->next); +} + +TEST(TestListLib, insert_with_position_at_beginning) { + // Arrange + List list; + list.push_back(2); + list.push_back(3); + + // Act + list.insert((size_t)0, 345); + + // Assert + EXPECT_FALSE(list.is_empty()); + EXPECT_EQ(345, list.head()->value); + EXPECT_EQ(2, list.head()->next->value); + EXPECT_EQ(3, list.tail()->value); +} + +TEST(TestListLib, insert_with_position_in_middle) { + // Arrange + List list; + list.push_back(1); + list.push_back(3); + list.push_back(4); + + // Act + list.insert((size_t)1, 2); + + // Assert + EXPECT_FALSE(list.is_empty()); + Node* cur = list.head(); + EXPECT_EQ(1, cur->value); + cur = cur->next; + EXPECT_EQ(2, cur->value); + cur = cur->next; + EXPECT_EQ(3, cur->value); + cur = cur->next; + EXPECT_EQ(4, cur->value); + EXPECT_EQ(nullptr, cur->next); +} + +TEST(TestListLib, insert_with_position_at_end) { + // Arrange + List list; + list.push_back(1); + list.push_back(2); + + // Act + list.insert((size_t)2, 3); + + // Assert + EXPECT_FALSE(list.is_empty()); + EXPECT_EQ(1, list.head()->value); + EXPECT_EQ(2, list.head()->next->value); + EXPECT_EQ(3, list.tail()->value); +} + +TEST(TestListLib, insert_with_throw_out_of_range) { + // Arrange + List list; + list.push_back(1); + list.push_back(2); + + // Act & Assert + EXPECT_ANY_THROW(list.insert((size_t)7, 3)); +} + +TEST(TestListLib, insert_after_node_in_middle) { + // Arrange + List list; + list.push_back(1); + list.push_back(2); + list.push_back(4); + Node* node = list.head()->next; + + // Act + list.insert(node, 3); + + // Assert + EXPECT_FALSE(list.is_empty()); + Node* cur = list.head(); + EXPECT_EQ(1, cur->value); + cur = cur->next; + EXPECT_EQ(2, cur->value); + cur = cur->next; + EXPECT_EQ(3, cur->value); + cur = cur->next; + EXPECT_EQ(4, cur->value); + EXPECT_EQ(nullptr, cur->next); +} + +TEST(TestListLib, insert_after_tail_node) { + // Arrange + List list; + list.push_back(1); + list.push_back(2); + list.push_back(3); + Node* node = list.tail(); + + // Act + list.insert(node, 4); + + // Assert + EXPECT_FALSE(list.is_empty()); + Node* cur = list.head(); + EXPECT_EQ(1, cur->value); + cur = cur->next; + EXPECT_EQ(2, cur->value); + cur = cur->next; + EXPECT_EQ(3, cur->value); + cur = cur->next; + EXPECT_EQ(4, cur->value); + EXPECT_EQ(nullptr, cur->next); +} + +TEST(TestListLib, insert_after_nullptr) { + // Arrange + List list; + list.push_back(1); + list.push_back(2); + + // Act & Assert + EXPECT_ANY_THROW(list.insert(nullptr, 3)); +} + +TEST(TestListLib, insert_after_node_not_in_list) { + // Arrange + List list; + list.push_back(1); + Node* foreign_node = new Node(888); + + // Act & Assert + EXPECT_ANY_THROW(list.insert(foreign_node, 2)); + + // Cleanup + delete foreign_node; +} + +TEST(TestListLib, pop_front_single_element) { + // Arrange + List list; + list.push_back(456); + + // Act + list.pop_front(); + + // Assert + EXPECT_TRUE(list.is_empty()); + EXPECT_EQ(nullptr, list.head()); + EXPECT_EQ(nullptr, list.tail()); +} + +TEST(TestListLib, pop_front_multiple_elements) { + // Arrange + List list; + list.push_back(1); + list.push_back(2); + list.push_back(3); + + // Act + list.pop_front(); + + // Assert + EXPECT_FALSE(list.is_empty()); + EXPECT_EQ(2, list.head()->value); + EXPECT_EQ(3, list.tail()->value); + EXPECT_EQ(nullptr, list.tail()->next); +} + +TEST(TestListLib, pop_front_in_empty_list) { + // Arrange + List list; + + // Act & Assert + EXPECT_ANY_THROW(list.pop_front()); +} + +TEST(TestListLib, pop_back_single_element) { + // Arrange + List list; + list.push_back(456); + + // Act + list.pop_back(); + + // Assert + EXPECT_TRUE(list.is_empty()); + EXPECT_EQ(nullptr, list.head()); + EXPECT_EQ(nullptr, list.tail()); +} + +TEST(TestListLib, pop_back_multiple_elements) { + // Arrange + List list; + list.push_back(1); + list.push_back(2); + list.push_back(3); + + // Act + list.pop_back(); + + // Assert + EXPECT_FALSE(list.is_empty()); + EXPECT_EQ(1, list.head()->value); + EXPECT_EQ(2, list.tail()->value); + EXPECT_EQ(nullptr, list.tail()->next); +} + +TEST(TestListLib, pop_back_in_empty_list) { + // Arrange + List list; + + // Act & Assert + EXPECT_ANY_THROW(list.pop_back()); +} + +TEST(TestListLib, erase_with_position_at_beggining) { + // Arrange + List list; + list.push_back(1); + list.push_back(2); + list.push_back(3); + + // Act + list.erase((size_t)0); + + // Assert + EXPECT_FALSE(list.is_empty()); + EXPECT_EQ(2, list.head()->value); + EXPECT_EQ(3, list.tail()->value); +} + +TEST(TestListLib, erase_with_position_in_middle) { + // Arrange + List list; + list.push_back(1); + list.push_back(2); + list.push_back(3); + + // Act + list.erase((size_t)1); + + // Assert + EXPECT_FALSE(list.is_empty()); + EXPECT_EQ(1, list.head()->value); + EXPECT_EQ(3, list.tail()->value); +} + +TEST(TestListLib, erase_with_position_at_end) { + // Arrange + List list; + list.push_back(1); + list.push_back(2); + list.push_back(3); + + // Act + list.erase((size_t)2); + + // Assert + EXPECT_FALSE(list.is_empty()); + EXPECT_EQ(1, list.head()->value); + EXPECT_EQ(2, list.tail()->value); +} + +TEST(TestListLib, erase_with_position_in_empty_list) { + // Arrange + List list; + + // Act & Assert + EXPECT_ANY_THROW(list.erase((size_t)0)); +} + +TEST(TestListLib, erase_with_position_more_than_list) { + // Arrange + List list; + list.push_back(1); + list.push_back(2); + list.push_back(3); + + // Act & Assert + EXPECT_ANY_THROW(list.erase((size_t)8)); +} + +TEST(TestListLib, erase_first_node) { + // Arrange + List list; + list.push_back(1); + list.push_back(2); + list.push_back(3); + Node* node = list.head(); + + // Act + list.erase(node); + + // Assert + EXPECT_EQ(2, list.head()->value); + EXPECT_EQ(3, list.tail()->value); +} + +TEST(TestListLib, erase_with_node_in_middle) { + // Arrange + List list; + list.push_back(1); + list.push_back(2); + list.push_back(3); + Node* node = list.head(); + + // Act + list.erase(node); + + // Assert + EXPECT_FALSE(list.is_empty()); + EXPECT_EQ(2, list.head()->value); + EXPECT_EQ(3, list.tail()->value); +} + +TEST(TestListLib, erase_with_node_at_end) { + // Arrange + List list; + list.push_back(1); + list.push_back(2); + list.push_back(3); + Node* node = list.tail(); + + // Act + list.erase(node); + + // Assert + EXPECT_FALSE(list.is_empty()); + EXPECT_EQ(1, list.head()->value); + EXPECT_EQ(2, list.tail()->value); +} + +TEST(TestListLib, erase_after_node_not_in_list) { + // Arrange + List list; + list.push_back(1); + Node* foreign_node = new Node(888); + + // Act & Assert + EXPECT_ANY_THROW(list.erase(foreign_node)); + + // Cleanup + delete foreign_node; +} + +TEST(TestListLib, erase_nullptr) { + // Arrange + List list; + list.push_back(1); + + // Act & Assert + ASSERT_ANY_THROW(list.erase(nullptr)); +} + +TEST(TestListLib, operator_assign_empty_to_empty) { + // Arrange + List list1; + List list2; + + // Act + list1 = list2; + + // Assert + EXPECT_TRUE(list1.is_empty()); + EXPECT_TRUE(list2.is_empty()); +} + +TEST(TestListLib, operator_assign_empty_to_not_empty) { + // Arrange + List list1; + list1.push_front(1); + list1.push_front(2); + list1.push_front(3); + List list2; + + // Act + list2 = list1; + + // Assert + EXPECT_FALSE(list2.is_empty()); + EXPECT_EQ(3, list2.head()->value); + EXPECT_EQ(2, list2.head()->next->value); + EXPECT_EQ(1, list2.tail()->value); +} + +TEST(TestListLib, operator_assign_not_empty_to_empty) { + // Arrange + List list1; + list1.push_back(1); + list1.push_back(2); + List list2; + + // Act + list1 = list2; + + // Assert + EXPECT_TRUE(list1.is_empty()); + EXPECT_TRUE(list2.is_empty()); +} + +TEST(TestListLib, operator_assign_self_assignment) { + // Arrange + List list; + list.push_back(1); + list.push_back(2); + list.push_back(3); + + // Act + list = list; + + // Assert + EXPECT_FALSE(list.is_empty()); + EXPECT_EQ(1, list.head()->value); + EXPECT_EQ(3, list.tail()->value); +} + +TEST(TestListLib, operator_assign_chain_of_assignment) { + // Arrange + List list1; + list1.push_back(1); + List list2; + list2.push_back(2); + List list3; + list3.push_back(3); + + // Act + list1 = list2 = list3; + + // Assert + EXPECT_EQ(3, list1.head()->value); + EXPECT_EQ(3, list2.head()->value); + EXPECT_EQ(3, list3.head()->value); +} + +TEST(TestListLib, test_complex_of_operations) { + // Arrange + List list; + + // Act + list.push_back(1); + list.push_front(0); + list.push_back(3); + list.insert(2, 2); + list.push_back(4); + list.pop_front(); + list.pop_back(); + + // Assert + EXPECT_EQ(1, list.head()->value); + EXPECT_EQ(3, list.tail()->value); + const Node* cur = list.head(); + EXPECT_EQ(1, cur->value); + cur = cur->next; + EXPECT_EQ(2, cur->value); + cur = cur->next; + EXPECT_EQ(3, cur->value); + EXPECT_EQ(nullptr, cur->next); +} + +TEST(TestListLib, test_double_list) { + // Arrange + List list; + + // Act + list.push_back(1.1); + list.push_back(2.2); + list.push_back(3.3); + + // Assert + EXPECT_NEAR(1.1, list.head()->value, EPSILON); + EXPECT_NEAR(3.3, list.tail()->value, EPSILON); + + // Act + list.erase((size_t)1); + + // Assert + EXPECT_NEAR(1.1, list.head()->value, EPSILON); + EXPECT_NEAR(3.3, list.tail()->value, EPSILON); + EXPECT_NEAR(3.3, list.head()->next->value, EPSILON); +} + +TEST(TestListLib, test_string_list) { + // Arrange + List list; + + // Act + list.push_back("Hello"); + list.push_back("World"); + list.push_front("Start"); + + // Assert + EXPECT_EQ("Start", list.head()->value); + EXPECT_EQ("World", list.tail()->value); + + // Act + list.pop_front(); + + // Assert + EXPECT_EQ("Hello", list.head()->value); +} \ No newline at end of file From fb00bf923aad03c9308c61395d4997dfd305b898 Mon Sep 17 00:00:00 2001 From: Kate-Nits Date: Mon, 8 Dec 2025 02:58:08 +0300 Subject: [PATCH 84/94] Moved the realization of methods outside the class List --- lib_list/list.h | 345 ++++++++++++++++++++++++++---------------------- 1 file changed, 189 insertions(+), 156 deletions(-) diff --git a/lib_list/list.h b/lib_list/list.h index 48122f36..e993a2c9 100644 --- a/lib_list/list.h +++ b/lib_list/list.h @@ -24,182 +24,215 @@ class List { ~List() { clear(); } - inline Node* head() { return _head; } - inline const Node* head() const { - return this->_head; - } - inline Node* tail() { return _tail; } - inline const Node* tail() const { - return this->_tail; - } - bool is_empty() const { - return _head == nullptr; + inline Node* head(); + inline const Node* head() const; + inline Node* tail(); + inline const Node* tail() const; + bool is_empty() const; + void push_front(const T& value); + void push_back(const T& value); + void insert(size_t pos, const T& value); + void insert(Node* node, const T& value); + void pop_front(); + void pop_back(); + void erase(size_t pos); + void erase(Node* node); + List& operator=(const List& other); +private: + void clear(); + bool is_node_in_list(Node* node) const; +}; + +template +inline Node* List::head() { return _head; } +template +inline const Node* List::head() const { + return this->_head; +} +template +inline Node* List::tail() { return _tail; } +template +inline const Node* List::tail() const { + return this->_tail; +} +template +bool List::is_empty() const { + return _head == nullptr; +} +template +void List::push_front(const T& value) { + Node* node = new Node(value, _head); + if (is_empty()) { + _tail = node; } - void push_front(const T& value) { - Node* node = new Node(value, _head); - if (is_empty()) { - _tail = node; - } + _head = node; +} +template +void List::push_back(const T& value) { + Node* node = new Node(value, nullptr); + if (is_empty()) { _head = node; + _tail = node; } - void push_back(const T& value) { - Node* node = new Node(value, nullptr); - if (is_empty()) { - _head = node; - _tail = node; - } - else { - _tail->next = node; - _tail = node; - } + else { + _tail->next = node; + _tail = node; } - void insert(size_t pos, const T& value) { - if (pos == 0) { - push_front(value); - return; - } - Node* cur = _head; - for (size_t i = 0; i < pos - 1; ++i) { - if (cur == nullptr) { - throw std::out_of_range("Position out of range"); - } - cur = cur->next; - } +} +template +void List::insert(size_t pos, const T& value) { + if (pos == 0) { + push_front(value); + return; + } + Node* cur = _head; + for (size_t i = 0; i < pos - 1; ++i) { if (cur == nullptr) { throw std::out_of_range("Position out of range"); } - if (cur == _tail) { - push_back(value); - } - else { - Node* node = new Node(value, cur->next); - cur->next = node; - } + cur = cur->next; } - void insert(Node* node, const T& value) { - if (node == nullptr) { - throw std::invalid_argument("Node can't be nullptr"); - } - if (!is_node_in_list(node)) { - throw std::invalid_argument("Node is not in List"); - } - Node* new_node = new Node(value, node->next); - node->next = new_node; - if (node == _tail) { - _tail = new_node; - } + if (cur == nullptr) { + throw std::out_of_range("Position out of range"); } - void pop_front() { - if (is_empty()) { - throw std::underflow_error("List is empty"); - } - Node* old_head = _head; - _head = _head->next; - if (old_head == _tail) { - _tail = nullptr; - } - delete old_head; + if (cur == _tail) { + push_back(value); } - void pop_back() { - if (is_empty()) { - throw std::underflow_error("List is empty"); - } - if (_head == _tail) { - Node* node_to_delete = _head; - _head = nullptr; - _tail = nullptr; - delete node_to_delete; - return; - } - Node* cur = _head; - while (cur->next != _tail) { - cur = cur->next; - } - Node* node_to_delete = _tail; - _tail = cur; - cur->next = nullptr; + else { + Node* node = new Node(value, cur->next); + cur->next = node; + } +} +template +void List::insert(Node* node, const T& value) { + if (node == nullptr) { + throw std::invalid_argument("Node can't be nullptr"); + } + if (!is_node_in_list(node)) { + throw std::invalid_argument("Node is not in List"); + } + Node* new_node = new Node(value, node->next); + node->next = new_node; + if (node == _tail) { + _tail = new_node; + } +} +template +void List::pop_front() { + if (is_empty()) { + throw std::underflow_error("List is empty"); + } + Node* old_head = _head; + _head = _head->next; + if (old_head == _tail) { + _tail = nullptr; + } + delete old_head; +} +template +void List::pop_back() { + if (is_empty()) { + throw std::underflow_error("List is empty"); + } + if (_head == _tail) { + Node* node_to_delete = _head; + _head = nullptr; + _tail = nullptr; delete node_to_delete; + return; } - void erase(size_t pos) { - if (is_empty()) { - throw std::underflow_error("List is empty"); - } - if (pos == 0) { - pop_front(); - return; - } - Node* cur = _head; - for (size_t i = 0; i < pos - 1; ++i) { - if (cur == nullptr) { - throw std::out_of_range("Position out of range"); - } - cur = cur->next; - } - if (cur == nullptr || cur->next == nullptr) { + Node* cur = _head; + while (cur->next != _tail) { + cur = cur->next; + } + Node* node_to_delete = _tail; + _tail = cur; + cur->next = nullptr; + delete node_to_delete; +} +template +void List::erase(size_t pos) { + if (is_empty()) { + throw std::underflow_error("List is empty"); + } + if (pos == 0) { + pop_front(); + return; + } + Node* cur = _head; + for (size_t i = 0; i < pos - 1; ++i) { + if (cur == nullptr) { throw std::out_of_range("Position out of range"); } - Node* node_to_delete = cur->next; - if (node_to_delete == _tail) { - _tail = cur; - } - cur->next = node_to_delete->next; - delete node_to_delete; + cur = cur->next; } - void erase(Node* node) { - if (node == nullptr) { - throw std::invalid_argument("Node can't be nullptr"); - } - if (!is_node_in_list(node)) { - throw std::invalid_argument("Node is not in List"); - } - if (node == _head) { - pop_front(); - return; - } - Node* cur = _head; - while (cur->next != node) { - cur = cur->next; - } - Node* node_to_delete = cur->next; - if (node_to_delete == _tail) { - _tail = cur; - } - cur->next = node_to_delete->next; - delete node_to_delete; + if (cur == nullptr || cur->next == nullptr) { + throw std::out_of_range("Position out of range"); } - List& operator=(const List& other) { - if (this == &other) { - return *this; - } - clear(); - Node* other_cur = other._head; - while (other_cur != nullptr) { - push_back(other_cur->value); - other_cur = other_cur->next; - } + Node* node_to_delete = cur->next; + if (node_to_delete == _tail) { + _tail = cur; + } + cur->next = node_to_delete->next; + delete node_to_delete; +} +template +void List::erase(Node* node) { + if (node == nullptr) { + throw std::invalid_argument("Node can't be nullptr"); + } + if (!is_node_in_list(node)) { + throw std::invalid_argument("Node is not in List"); + } + if (node == _head) { + pop_front(); + return; + } + Node* cur = _head; + while (cur->next != node) { + cur = cur->next; + } + Node* node_to_delete = cur->next; + if (node_to_delete == _tail) { + _tail = cur; + } + cur->next = node_to_delete->next; + delete node_to_delete; +} +template +List& List::operator=(const List& other) { + if (this == &other) { return *this; } -private: - void clear() { - Node* cur = _head; - while (cur != nullptr) { - Node* next_node = cur->next; - delete cur; - cur = next_node; - } - _head = nullptr; - _tail = nullptr; + clear(); + Node* other_cur = other._head; + while (other_cur != nullptr) { + push_back(other_cur->value); + other_cur = other_cur->next; } - bool is_node_in_list(Node* node) const { - Node* cur = _head; - while (cur != nullptr) { - if (cur == node) { - return true; - } - cur = cur->next; + return *this; +} +template +void List::clear() { + Node* cur = _head; + while (cur != nullptr) { + Node* next_node = cur->next; + delete cur; + cur = next_node; + } + _head = nullptr; + _tail = nullptr; +} +template +bool List::is_node_in_list(Node* node) const { + Node* cur = _head; + while (cur != nullptr) { + if (cur == node) { + return true; } - return false; + cur = cur->next; } -}; + return false; +} #endif \ No newline at end of file From 06d7da70a2e219f17c139df0e4e3ca0fad5179b0 Mon Sep 17 00:00:00 2001 From: Kate-Nits Date: Mon, 8 Dec 2025 04:36:38 +0300 Subject: [PATCH 85/94] Added functions is_open_bracket, is_close_bracket, matches_pair, check_brackets and wrote all test for the correctness of the brackets --- lib_algorithms/CMakeLists.txt | 5 ++- lib_algorithms/algorithms.cpp | 44 ++++++++++++++++++- lib_algorithms/algorithms.h | 5 +++ tests/test_algorithms.cpp | 80 +++++++++++++++++++++++++++++++++++ 4 files changed, 132 insertions(+), 2 deletions(-) diff --git a/lib_algorithms/CMakeLists.txt b/lib_algorithms/CMakeLists.txt index f1086c21..3a7d8314 100644 --- a/lib_algorithms/CMakeLists.txt +++ b/lib_algorithms/CMakeLists.txt @@ -1,3 +1,6 @@ create_project_lib(Algorithms) add_depend(Algorithms Circle lib_circle) -add_depend(Algorithms Sphere lib_sphere) \ No newline at end of file +add_depend(Algorithms Sphere lib_sphere) +add_depend(Algorithms Stack lib_stack) +add_depend(Algorithms Matrix lib_matrix) +add_depend(Algorithms TriangleMatrix lib_triangle_matrix) \ No newline at end of file diff --git a/lib_algorithms/algorithms.cpp b/lib_algorithms/algorithms.cpp index 27eb0753..69175c30 100644 --- a/lib_algorithms/algorithms.cpp +++ b/lib_algorithms/algorithms.cpp @@ -4,10 +4,11 @@ #include #include #include +#include #include "../lib_circle/circle.h" #include "../lib_sphere/sphere.h" - +#include "../lib_stack/stack.h" #include "../lib_matrix/matrix.h" #include "../lib_triangle_matrix/triangle_matrix.h" @@ -34,6 +35,8 @@ #define STANDART 1 #define TRIANGLE 2 +#define SIZE_OF_STACK_FOR_CHECK_BRACKETS 150 + void set_color(int text_color, int bg_color) { HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); SetConsoleTextAttribute(hConsole, (bg_color << 4) | text_color); @@ -453,4 +456,43 @@ void matrix_application() { break; } } +} + + +inline bool is_open_bracket(char symbol) { + return symbol == '(' || symbol == '[' || symbol == '{'; +} +inline bool is_close_bracket(char symbol) { + return symbol == ')' || symbol == ']' || symbol == '}'; +} +inline bool matches_pair(char open, char close) { + return (open == '(' && close == ')') || + (open == '[' && close == ']') || + (open == '{' && close == '}'); +} +bool check_brackets(const std::string& str) { + Stack stack(SIZE_OF_STACK_FOR_CHECK_BRACKETS); + for (size_t i = 0; i < str.length(); ++i) { + char symbol = str[i]; + if (is_open_bracket(symbol)) { + try { + stack.push(symbol); + } + catch (const std::overflow_error&) { + std::cout << "Stack is full" << std::endl; + return false; + } + } + else if (is_close_bracket(symbol)) { + if (stack.is_empty()) { + return false; + } + char top = stack.top(); + if (!matches_pair(top, symbol)) { + return false; + } + stack.pop(); + } + } + return stack.is_empty(); } \ No newline at end of file diff --git a/lib_algorithms/algorithms.h b/lib_algorithms/algorithms.h index cda39d04..c91afbe8 100644 --- a/lib_algorithms/algorithms.h +++ b/lib_algorithms/algorithms.h @@ -189,4 +189,9 @@ void what_matrix_sizes(const int& type_matrix, size_t& size_rows, size_t& size_c void deleted_saved_matrix(int& link_user_choice_for_deleted, int& isThereMatrix1, int& isThereMatrix2, int& isThereMatrix3); void matrix_application(); +inline bool is_open_bracket(char symbol); +inline bool is_close_bracket(char symbol); +inline bool matches_pair(char open, char close); +bool check_brackets(const std::string& str); + #endif // LIB_ALGORITHMS_H \ No newline at end of file diff --git a/tests/test_algorithms.cpp b/tests/test_algorithms.cpp index abc21c95..598ab0ce 100644 --- a/tests/test_algorithms.cpp +++ b/tests/test_algorithms.cpp @@ -30,4 +30,84 @@ TEST(TestAlgorithmsLib, test_gradient_descent_from_example2) { // Assert EXPECT_TRUE(res == 6 || res == 1); +} + +TEST(TestAlgorithmsLib, test_1_on_check_brackets_empty_list) { + // Arrange + std::string string = ""; + + // Act & Assert + EXPECT_TRUE(check_brackets(string)); +} + +TEST(TestAlgorithmsLib, test_2_on_check_brackets_easy) { + // Arrange + std::string string = "()"; + + // Act & Assert + EXPECT_TRUE(check_brackets(string)); +} + +TEST(TestAlgorithmsLib, test_3_on_check_brackets_easy) { + // Arrange + std::string string = "()()"; + + // Act & Assert + EXPECT_TRUE(check_brackets(string)); +} + +TEST(TestAlgorithmsLib, test_4_on_check_brackets) { + // Arrange + std::string string = "[(()())(())]"; + + // Act & Assert + EXPECT_TRUE(check_brackets(string)); +} + +TEST(TestAlgorithmsLib, test_5_on_check_brackets) { + // Arrange + std::string string = "(()()"; + + // Act & Assert + EXPECT_FALSE(check_brackets(string)); +} + +TEST(TestAlgorithmsLib, test_6_on_check_brackets) { + // Arrange + std::string string = "())(())"; + + // Act & Assert + EXPECT_FALSE(check_brackets(string)); +} + +TEST(TestAlgorithmsLib, test_7_on_check_brackets) { + // Arrange + std::string string = "((()()(()))"; + + // Act & Assert + EXPECT_FALSE(check_brackets(string)); +} + +TEST(TestAlgorithmsLib, different_types_of_brackets_correct) { + // Arrange + std::string str = "({[]})"; + + // Act & Assert + EXPECT_TRUE(check_brackets(str)); +} + +TEST(TestAlgorithmsLib, mixed_brackets_incorrect) { + // Arrange + std::string str = "({[}])"; + + // Act & Assert + EXPECT_FALSE(check_brackets(str)); +} + +TEST(TestAlgorithmsLib, real_example_with_text) { + // Arrange + std::string str = "[5*(x+8)-9]/[(7/(y*1))*(y*1)]"; + + // Act & Assert + EXPECT_TRUE(check_brackets(str)); } \ No newline at end of file From 84d05a023dab7d28607baca10beaab4a9709e4d1 Mon Sep 17 00:00:00 2001 From: Kate-Nits Date: Tue, 9 Dec 2025 01:19:32 +0300 Subject: [PATCH 86/94] Added Iterator for the class List and wrote tests on it --- lib_list/list.h | 45 ++++++++++++++++ tests/test_list.cpp | 121 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 166 insertions(+) diff --git a/lib_list/list.h b/lib_list/list.h index e993a2c9..33af66bd 100644 --- a/lib_list/list.h +++ b/lib_list/list.h @@ -24,6 +24,51 @@ class List { ~List() { clear(); } + class Iterator { + Node* _current; + public: + Iterator() : _current(nullptr) {} + Iterator(Node* ptr) : _current(ptr) {} + + Iterator& operator=(const Iterator& other) { + if (this != &other) { + _current = other._current; + } + return *this; + } + Iterator operator++(int) { //iterator++ + Iterator tmp = *this; + if (_current != nullptr) { + _current = _current->next; + } + return tmp; + } + Iterator& operator++() { // ++iterator + if (_current != nullptr) { + _current = _current->next; + } + return *this; + } + bool operator==(const Iterator& other) const { + return _current == other._current; + } + bool operator!=(const Iterator& other) const { + return _current != other._current; + } + T& operator*() { + return _current->value; + } + Iterator& operator+=(size_t n) { + while (n > 0 && _current != nullptr) { + _current = _current->next; + n--; + } + return *this; + } + }; + Iterator begin() { return Iterator(_head); } + Iterator end() { return Iterator(nullptr); } + inline Node* head(); inline const Node* head() const; inline Node* tail(); diff --git a/tests/test_list.cpp b/tests/test_list.cpp index 8162244b..1e7b2b33 100644 --- a/tests/test_list.cpp +++ b/tests/test_list.cpp @@ -2,6 +2,7 @@ #include #include "../lib_list/list.h" +#include "../lib_tvector/tvector.h" #define EPSILON 0.000001 #define TRUE 1 @@ -620,4 +621,124 @@ TEST(TestListLib, test_string_list) { // Assert EXPECT_EQ("Hello", list.head()->value); +} + +TEST(TestListLib, iterator_read) { + // Arrange + List list; + for (int i = 0; i < 5; i++) { + list.push_back(i); + } + int i = 0; + + // Act & Assert + for (List::Iterator it = list.begin(); it != list.end(); it++) { + EXPECT_EQ(i, *it); + i++; + } +} + +TEST(TestListLib, iterator_write) { + // Arrange + List list; + for (int i = 0; i < 5; i++) { + list.push_back(0); + } + int i = 1; + int j = 10; + + // Act + for (List::Iterator it = list.begin(); it != list.end(); it++) { + *it = i * 10; + i++; + } + + // Assert + for (List::Iterator it = list.begin(); it != list.end(); it++) { + EXPECT_EQ(j, *it); + j += 10; + } +} + +TEST(TestListLib, iterator_in_empty_list) { + // Arrange + List list; + + // Act + int iterator_count = 0; + for (List::Iterator it = list.begin(); it != list.end(); it++) { + iterator_count++; + } + + // Assert + EXPECT_EQ(0, iterator_count); + EXPECT_TRUE(list.begin() == list.end()); + EXPECT_FALSE(list.begin() != list.end()); +} + +TEST(TestListLib, iterator_operator_assigment) { + // Arrange + List list; + list.push_back(76); + list.push_back(32); + + // Act + List::Iterator it1 = list.begin(); + List::Iterator it2 = it1; + + // Assert + EXPECT_EQ(*it1, *it2); + EXPECT_TRUE(it1 == it2); + ++it1; + EXPECT_EQ(32, *it1); + EXPECT_EQ(76, *it2); + EXPECT_FALSE(it1 == it2); +} + +TEST(TestListLib, iterator_operator_prefix_plus_plus) { + // Arrange + List list; + list.push_back(765); + list.push_back(324); + list.push_back(456); + + // Act + List::Iterator it = list.begin(); + + // Assert + EXPECT_EQ(765, *it); + EXPECT_EQ(324, *++it); + EXPECT_EQ(324, * it); + EXPECT_EQ(456, *++it); +} + +TEST(TestListLib, iterator_operator_postfix_plus_plus) { + // Arrange + List list; + list.push_back(765); + list.push_back(324); + list.push_back(456); + + // Act + List::Iterator it = list.begin(); + + // Assert + EXPECT_EQ(765, *it); + EXPECT_EQ(765, *it++); + EXPECT_EQ(324, *it); +} + +TEST(TestListLib, iterator_operator_plus_assigment) { + // Arrange + List list; + for (int i = 1; i < 7; i++) { + list.push_back(i*10); + } + + // Act + List::Iterator it = list.begin(); + it += 3; + + // Assert + EXPECT_EQ(40, *it); } \ No newline at end of file From 71e0ac7c15bdde644d59b5e24ebd3e2a5d3009e5 Mon Sep 17 00:00:00 2001 From: Kate-Nits Date: Tue, 9 Dec 2025 04:32:04 +0300 Subject: [PATCH 87/94] Wrote NodeD, DList, Iterator for DList and wrote tests for it --- CMakeLists.txt | 1 + lib_dlist/CMakeLists.txt | 2 + lib_dlist/dlist.cpp | 0 lib_dlist/dlist.h | 321 +++++++++++++++ lib_list/list.h | 23 +- lib_node/node.h | 12 +- tests/test_dlist.cpp | 848 +++++++++++++++++++++++++++++++++++++++ tests/test_list.cpp | 1 - 8 files changed, 1199 insertions(+), 9 deletions(-) create mode 100644 lib_dlist/CMakeLists.txt create mode 100644 lib_dlist/dlist.cpp create mode 100644 lib_dlist/dlist.h create mode 100644 tests/test_dlist.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index d16551f4..82fe00f3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -27,6 +27,7 @@ add_subdirectory(lib_stack) add_subdirectory(lib_queue) add_subdirectory(lib_node) add_subdirectory(lib_list) +add_subdirectory(lib_dlist) add_subdirectory(main) # подключаем дополнительный CMakeLists.txt из подкаталога с именем main option(BTEST "build test?" ON) # указываем подключаем ли google-тесты (ON или YES) или нет (OFF или NO) diff --git a/lib_dlist/CMakeLists.txt b/lib_dlist/CMakeLists.txt new file mode 100644 index 00000000..22ed0e22 --- /dev/null +++ b/lib_dlist/CMakeLists.txt @@ -0,0 +1,2 @@ +create_project_lib(DList) +add_depend(DList Node lib_node) \ No newline at end of file diff --git a/lib_dlist/dlist.cpp b/lib_dlist/dlist.cpp new file mode 100644 index 00000000..e69de29b diff --git a/lib_dlist/dlist.h b/lib_dlist/dlist.h new file mode 100644 index 00000000..5709dfa7 --- /dev/null +++ b/lib_dlist/dlist.h @@ -0,0 +1,321 @@ +// Copyright 2025 Ekaterina Ushnitskaya + +#ifndef LIB_DLIST_DLIST_H +#define LIB_DLIST_DLIST_H + +#include +#include + +#include "../lib_node/node.h" + +template +class DList { + NodeD* _head; + NodeD* _tail; +public: + DList() : _head(nullptr), _tail(nullptr) {} + DList(const DList& other) : _head(nullptr), _tail(nullptr) { + NodeD* cur = other._head; + while (cur != nullptr) { + push_back(cur->value); + cur = cur->next; + } + } + ~DList() { + clear(); + } + class Iterator { + NodeD* _current; + public: + Iterator() : _current(nullptr) {} + Iterator(NodeD* ptr) : _current(ptr) {} + + Iterator& operator=(const Iterator& other) { + if (this != &other) { + _current = other._current; + } + return *this; + } + Iterator operator++(int) { //iterator++ + Iterator tmp = *this; + if (_current != nullptr) { + _current = _current->next; + } + return tmp; + } + Iterator& operator++() { // ++iterator + if (_current != nullptr) { + _current = _current->next; + } + return *this; + } + bool operator==(const Iterator& other) const { + return _current == other._current; + } + bool operator!=(const Iterator& other) const { + return _current != other._current; + } + T& operator*() { + return _current->value; + } + Iterator& operator+=(size_t n) { + while (n > 0 && _current != nullptr) { + _current = _current->next; + n--; + } + return *this; + } + Iterator operator--(int) { //iterator-- + Iterator tmp = *this; + if (_current != nullptr) { + _current = _current->prev; + } + return tmp; + } + Iterator& operator--() { //--iterator + if (_current != nullptr) { + _current = _current->prev; + } + return *this; + } + Iterator& operator-=(size_t n) { + while (n > 0 && _current != nullptr) { + _current = _current->prev; + n--; + } + return *this; + } + }; + Iterator begin() { return Iterator(_head); } + Iterator end() { return Iterator(nullptr); } + + inline NodeD* head(); + inline const NodeD* head() const; + inline NodeD* tail(); + inline const NodeD* tail() const; + bool is_empty() const; + void push_front(const T& value); + void push_back(const T& value); + void insert(size_t pos, const T& value); + void insert_after(NodeD* node, const T& value); + void insert_before(NodeD* node, const T& value); + void pop_front(); + void pop_back(); + void erase(NodeD* node); + void erase(size_t pos); + DList& operator=(const DList& other); +private: + void clear(); + bool is_node_in_list(NodeD* node) const; +}; + +template +inline NodeD* DList::head() { return _head; } + +template +inline const NodeD* DList::head() const { return _head; } + +template +inline NodeD* DList::tail() { return _tail; } +template +inline const NodeD* DList::tail() const { return _tail; } + +template +bool DList::is_empty() const { return _head == nullptr; } + +template +void DList::push_front(const T& value) { // O(1) + NodeD* node = new NodeD(value, _head, nullptr); + if (is_empty()) { + _tail = node; + } + else { + _head->prev = node; + } + _head = node; +} + +template +void DList::push_back(const T& value) { // O(1) + NodeD* node = new NodeD(value, nullptr, _tail); + if (is_empty()) { + _head = node; + } + else { + _tail->next = node; + } + _tail = node; +} + +template +void DList::insert(size_t pos, const T& value) { // O(n) + if (pos == 0) { + push_front(value); + return; + } + NodeD* cur = _head; + for (size_t i = 0; i < pos; ++i) { + if (cur == nullptr) { + throw std::out_of_range("Position out of range"); + } + cur = cur->next; + } + if (cur == nullptr) { + push_back(value); + return; + } + insert_before(cur, value); +} + +template +void DList::insert_after(NodeD* node, const T& value) { // O(1) + if (node == nullptr) { + throw std::invalid_argument("Node can't be nullptr"); + } + if (!is_node_in_list(node)) { + throw std::invalid_argument("Node is not in List"); + } + + NodeD* new_node = new NodeD(value, node->next, node); + node->next = new_node; + if (new_node->next != nullptr) { + new_node->next->prev = new_node; + } + else { + _tail = new_node; + } +} + +template +void DList::insert_before(NodeD* node, const T& value) { // O(1) + if (node == nullptr) { + throw std::invalid_argument("Node can't be nullptr"); + } + if (!is_node_in_list(node)) { + throw std::invalid_argument("Node is not in List"); + } + + NodeD* new_node = new NodeD(value, node, node->prev); + node->prev = new_node; + if (new_node->prev != nullptr) { + new_node->prev->next = new_node; + } + else { + _head = new_node; + } +} + +template +void DList::pop_front() { // O(1) + if (is_empty()) { + throw std::underflow_error("List is empty"); + } + NodeD* old_head = _head; + _head = _head->next; + if (_head != nullptr) { + _head->prev = nullptr; + } + else { + _tail = nullptr; + } + delete old_head; +} + +template +void DList::pop_back() { // O(1) + if (is_empty()) { + throw std::underflow_error("List is empty"); + } + NodeD* old_tail = _tail; + _tail = _tail->prev; + if (_tail != nullptr) { + _tail->next = nullptr; + } + else { + _head = nullptr; + } + delete old_tail; +} + +template +void DList::erase(NodeD* node) { // O(1) + if (node == nullptr) { + throw std::invalid_argument("Node can't be nullptr"); + } + if (is_empty()) { + throw std::underflow_error("List is empty"); + } + if (!is_node_in_list(node)) { + throw std::invalid_argument("Node is not in List"); + } + if (node == _head) { + pop_front(); + return; + } + if (node == _tail) { + pop_back(); + return; + } + node->prev->next = node->next; + node->next->prev = node->prev; + delete node; +} + + +template +void DList::erase(size_t pos) { // O(n) + if (is_empty()) { + throw std::underflow_error("List is empty"); + } + NodeD* cur = _head; + for (size_t i = 0; i < pos; ++i) { + if (cur == nullptr) { + throw std::out_of_range("Position out of range"); + } + cur = cur->next; + } + if (cur == nullptr) { + throw std::out_of_range("Position out of range"); + } + erase(cur); +} + +template +DList& DList::operator=(const DList& other) { + if (this == &other) { + return *this; + } + clear(); + NodeD* other_cur = other._head; + while (other_cur != nullptr) { + push_back(other_cur->value); + other_cur = other_cur->next; + } + return *this; +} + +template +void DList::clear() { + NodeD* cur = _head; + while (cur != nullptr) { + NodeD* next_node = cur->next; + delete cur; + cur = next_node; + } + _head = nullptr; + _tail = nullptr; +} + +template +bool DList::is_node_in_list(NodeD* node) const { + NodeD* cur = _head; + while (cur != nullptr) { + if (cur == node) { + return true; + } + cur = cur->next; + } + return false; +} + +#endif \ No newline at end of file diff --git a/lib_list/list.h b/lib_list/list.h index 33af66bd..de0ee448 100644 --- a/lib_list/list.h +++ b/lib_list/list.h @@ -90,20 +90,21 @@ class List { template inline Node* List::head() { return _head; } + template -inline const Node* List::head() const { - return this->_head; -} +inline const Node* List::head() const { return this->_head; } + template inline Node* List::tail() { return _tail; } + template -inline const Node* List::tail() const { - return this->_tail; -} +inline const Node* List::tail() const { return this->_tail; } + template bool List::is_empty() const { return _head == nullptr; } + template void List::push_front(const T& value) { Node* node = new Node(value, _head); @@ -112,6 +113,7 @@ void List::push_front(const T& value) { } _head = node; } + template void List::push_back(const T& value) { Node* node = new Node(value, nullptr); @@ -124,6 +126,7 @@ void List::push_back(const T& value) { _tail = node; } } + template void List::insert(size_t pos, const T& value) { if (pos == 0) { @@ -148,6 +151,7 @@ void List::insert(size_t pos, const T& value) { cur->next = node; } } + template void List::insert(Node* node, const T& value) { if (node == nullptr) { @@ -162,6 +166,7 @@ void List::insert(Node* node, const T& value) { _tail = new_node; } } + template void List::pop_front() { if (is_empty()) { @@ -174,6 +179,7 @@ void List::pop_front() { } delete old_head; } + template void List::pop_back() { if (is_empty()) { @@ -195,6 +201,7 @@ void List::pop_back() { cur->next = nullptr; delete node_to_delete; } + template void List::erase(size_t pos) { if (is_empty()) { @@ -221,6 +228,7 @@ void List::erase(size_t pos) { cur->next = node_to_delete->next; delete node_to_delete; } + template void List::erase(Node* node) { if (node == nullptr) { @@ -244,6 +252,7 @@ void List::erase(Node* node) { cur->next = node_to_delete->next; delete node_to_delete; } + template List& List::operator=(const List& other) { if (this == &other) { @@ -257,6 +266,7 @@ List& List::operator=(const List& other) { } return *this; } + template void List::clear() { Node* cur = _head; @@ -268,6 +278,7 @@ void List::clear() { _head = nullptr; _tail = nullptr; } + template bool List::is_node_in_list(Node* node) const { Node* cur = _head; diff --git a/lib_node/node.h b/lib_node/node.h index 19bbaaf8..86b9254b 100644 --- a/lib_node/node.h +++ b/lib_node/node.h @@ -3,8 +3,6 @@ #ifndef LIB_NODE_NODE_H #define LIB_NODE_NODE_H -#include - template struct Node { T value; @@ -13,4 +11,14 @@ struct Node { explicit Node(const T& value_, Node* next_ = nullptr) : value(value_), next(next_) {} }; +template +struct NodeD { + T value; + NodeD* next; + NodeD* prev; + + explicit NodeD(const T& value_, NodeD* next_ = nullptr, NodeD* prev_ = nullptr) + : value(value_), next(next_), prev(prev_) {} +}; + #endif \ No newline at end of file diff --git a/tests/test_dlist.cpp b/tests/test_dlist.cpp new file mode 100644 index 00000000..d3e80fbb --- /dev/null +++ b/tests/test_dlist.cpp @@ -0,0 +1,848 @@ +// Copyright 2025 Ekaterina Ushnitskaya + +#include +#include "../lib_dlist/dlist.h" + +#define EPSILON 0.000001 +#define TRUE 1 +#define FALSE 0 + + +TEST(TestDListLib, default_constructor) { + // Arrange & Act + DList list; + + // Assert + EXPECT_EQ(nullptr, list.head()); + EXPECT_EQ(nullptr, list.tail()); + EXPECT_TRUE(list.is_empty()); +} + +TEST(TestDListLib, copy_constructor_with_empty_list) { + // Arrange + DList list1; + + // Act + DList list2(list1); + + // Assert + EXPECT_TRUE(list2.is_empty()); + EXPECT_EQ(list1.head(), list2.head()); + EXPECT_EQ(list1.tail(), list2.tail()); + EXPECT_EQ(nullptr, list2.head()); + EXPECT_EQ(nullptr, list2.tail()); +} + +TEST(TestDListLib, copy_constructor_with_not_empty_list) { + // Arrange + DList list1; + list1.push_back(1); + list1.push_back(2); + list1.push_back(3); + + // Act + DList list2(list1); + + // Assert + EXPECT_FALSE(list2.is_empty()); + EXPECT_EQ(list1.head()->value, list2.head()->value); + EXPECT_EQ(list1.tail()->value, list2.tail()->value); + EXPECT_EQ(1, list2.head()->value); + EXPECT_EQ(3, list2.tail()->value); +} + +TEST(TestDListLib, push_front_to_empty_list_one_element) { + // Arrange + DList list; + + // Act + list.push_front(678); + + // Assert + EXPECT_FALSE(list.is_empty()); + EXPECT_EQ(678, list.head()->value); + EXPECT_EQ(678, list.tail()->value); + EXPECT_EQ(list.head(), list.tail()); +} + +TEST(TestDListLib, push_front_to_empty_list_multiple_elements) { + // Arrange + DList list; + + // Act + list.push_front(1); + list.push_front(2); + list.push_front(3); + + // Assert + EXPECT_FALSE(list.is_empty()); + EXPECT_EQ(3, list.head()->value); + EXPECT_EQ(1, list.tail()->value); + NodeD* cur = list.head(); + EXPECT_EQ(3, cur->value); + cur = cur->next; + EXPECT_EQ(2, cur->value); + cur = cur->next; + EXPECT_EQ(1, cur->value); + EXPECT_EQ(nullptr, cur->next); +} + +TEST(TestDListLib, push_back_to_empty_list_one_element) { + // Arrange + DList list; + + // Act + list.push_back(678); + + // Assert + EXPECT_FALSE(list.is_empty()); + EXPECT_EQ(678, list.head()->value); + EXPECT_EQ(678, list.tail()->value); + EXPECT_EQ(list.head(), list.tail()); +} + +TEST(TestDListLib, push_back_to_empty_list_multiple_elements) { + // Arrange + DList list; + + // Act + list.push_back(1); + list.push_back(2); + list.push_back(3); + + // Assert + EXPECT_FALSE(list.is_empty()); + EXPECT_EQ(1, list.head()->value); + EXPECT_EQ(3, list.tail()->value); + NodeD* cur = list.head(); + EXPECT_EQ(1, cur->value); + cur = cur->next; + EXPECT_EQ(2, cur->value); + cur = cur->next; + EXPECT_EQ(3, cur->value); + EXPECT_EQ(nullptr, cur->next); +} + +TEST(TestDListLib, insert_with_position_at_beginning) { + // Arrange + DList list; + list.push_back(2); + list.push_back(3); + + // Act + list.insert((size_t)0, 345); + + // Assert + EXPECT_FALSE(list.is_empty()); + EXPECT_EQ(345, list.head()->value); + EXPECT_EQ(2, list.head()->next->value); + EXPECT_EQ(3, list.tail()->value); +} + +TEST(TestDListLib, insert_with_position_in_middle) { + // Arrange + DList list; + list.push_back(1); + list.push_back(3); + list.push_back(4); + + // Act + list.insert((size_t)1, 2); + + // Assert + EXPECT_FALSE(list.is_empty()); + NodeD* cur = list.head(); + EXPECT_EQ(1, cur->value); + cur = cur->next; + EXPECT_EQ(2, cur->value); + cur = cur->next; + EXPECT_EQ(3, cur->value); + cur = cur->next; + EXPECT_EQ(4, cur->value); + EXPECT_EQ(nullptr, cur->next); +} + +TEST(TestDListLib, insert_with_position_at_end) { + // Arrange + DList list; + list.push_back(1); + list.push_back(2); + + // Act + list.insert((size_t)2, 3); + + // Assert + EXPECT_FALSE(list.is_empty()); + EXPECT_EQ(1, list.head()->value); + EXPECT_EQ(2, list.head()->next->value); + EXPECT_EQ(3, list.tail()->value); +} + +TEST(TestDListLib, insert_with_throw_out_of_range) { + // Arrange + DList list; + list.push_back(1); + list.push_back(2); + + // Act & Assert + EXPECT_ANY_THROW(list.insert((size_t)7, 3)); +} + +TEST(TestDListLib, insert_after_node_in_middle) { + // Arrange + DList list; + list.push_back(1); + list.push_back(2); + list.push_back(4); + NodeD* node = list.head()->next; + + // Act + list.insert_after(node, 3); + + // Assert + EXPECT_FALSE(list.is_empty()); + NodeD* cur = list.head(); + EXPECT_EQ(1, cur->value); + cur = cur->next; + EXPECT_EQ(2, cur->value); + cur = cur->next; + EXPECT_EQ(3, cur->value); + cur = cur->next; + EXPECT_EQ(4, cur->value); + EXPECT_EQ(nullptr, cur->next); +} + +TEST(TestDListLib, insert_after_tail_node) { + // Arrange + DList list; + list.push_back(1); + list.push_back(2); + list.push_back(3); + NodeD* node = list.tail(); + + // Act + list.insert_after(node, 4); + + // Assert + EXPECT_FALSE(list.is_empty()); + NodeD* cur = list.head(); + EXPECT_EQ(1, cur->value); + cur = cur->next; + EXPECT_EQ(2, cur->value); + cur = cur->next; + EXPECT_EQ(3, cur->value); + cur = cur->next; + EXPECT_EQ(4, cur->value); + EXPECT_EQ(nullptr, cur->next); +} + +TEST(TestDListLib, insert_after_nullptr) { + // Arrange + DList list; + list.push_back(1); + list.push_back(2); + + // Act & Assert + EXPECT_ANY_THROW(list.insert_after(nullptr, 3)); +} + +TEST(TestDListLib, insert_after_node_not_in_list) { + // Arrange + DList list; + list.push_back(1); + NodeD* foreign_node = new NodeD(888); + + // Act & Assert + EXPECT_ANY_THROW(list.insert_after(foreign_node, 2)); + + // Cleanup + delete foreign_node; +} + +TEST(TestDListLib, insert_before_head) { + // Arrange + DList list; + list.push_back(2); + list.push_back(3); + + // Act + list.insert_before(list.head(), 1); + + // Assert + EXPECT_EQ(1, list.head()->value); + EXPECT_EQ(2, list.head()->next->value); + EXPECT_EQ(3, list.tail()->value); + EXPECT_EQ(nullptr, list.head()->prev); +} + +TEST(TestDListLib, insert_before_middle) { + // Arrange + DList list; + list.push_back(1); + list.push_back(3); + list.push_back(4); + NodeD* middle_node = list.head()->next; + + // Act + list.insert_before(middle_node, 2); + + // Assert + EXPECT_EQ(1, list.head()->value); + EXPECT_EQ(4, list.tail()->value); + NodeD* cur = list.head(); + EXPECT_EQ(1, cur->value); + cur = cur->next; + EXPECT_EQ(2, cur->value); + cur = cur->next; + EXPECT_EQ(3, cur->value); + cur = cur->next; + EXPECT_EQ(4, cur->value); + EXPECT_EQ(nullptr, cur->next); +} + +TEST(TestDListLib, insert_before_tail) { + // Arrange + DList list; + list.push_back(1); + list.push_back(2); + list.push_back(4); + NodeD* tail_node = list.tail(); + + // Act + list.insert_before(tail_node, 3); + + // Assert + EXPECT_EQ(1, list.head()->value); + EXPECT_EQ(4, list.tail()->value); + NodeD* cur = list.head(); + EXPECT_EQ(1, cur->value); + cur = cur->next; + EXPECT_EQ(2, cur->value); + cur = cur->next; + EXPECT_EQ(3, cur->value); + cur = cur->next; + EXPECT_EQ(4, cur->value); + EXPECT_EQ(nullptr, cur->next); + EXPECT_EQ(3, list.tail()->prev->value); + EXPECT_EQ(4, list.tail()->value); +} + +TEST(TestDListLib, insert_before_in_empty_list) { + // Arrange + DList list; + NodeD* foreign_node = new NodeD(999); + + // Act & Assert + EXPECT_ANY_THROW(list.insert_before(foreign_node, 1)); + + // Cleanup + delete foreign_node; +} + +TEST(TestDListLib, insert_before_nullptr) { + // Arrange + DList list; + list.push_back(1); + list.push_back(2); + + // Act & Assert + EXPECT_ANY_THROW(list.insert_before(nullptr, 3)); +} + +TEST(TestDListLib, insert_before_foreign_node) { + // Arrange + DList list1; + list1.push_back(1); + list1.push_back(2); + DList list2; + list2.push_back(10); + list2.push_back(20); + NodeD* foreign_node = list2.head(); + + // Act & Assert + EXPECT_ANY_THROW(list1.insert_before(foreign_node, 3)); +} + +TEST(TestDListLib, pop_front_single_element) { + // Arrange + DList list; + list.push_back(456); + + // Act + list.pop_front(); + + // Assert + EXPECT_TRUE(list.is_empty()); + EXPECT_EQ(nullptr, list.head()); + EXPECT_EQ(nullptr, list.tail()); +} + +TEST(TestDListLib, pop_front_multiple_elements) { + // Arrange + DList list; + list.push_back(1); + list.push_back(2); + list.push_back(3); + + // Act + list.pop_front(); + + // Assert + EXPECT_FALSE(list.is_empty()); + EXPECT_EQ(2, list.head()->value); + EXPECT_EQ(3, list.tail()->value); + EXPECT_EQ(nullptr, list.tail()->next); +} + +TEST(TestDListLib, pop_front_in_empty_list) { + // Arrange + DList list; + + // Act & Assert + EXPECT_ANY_THROW(list.pop_front()); +} + +TEST(TestDListLib, pop_back_single_element) { + // Arrange + DList list; + list.push_back(456); + + // Act + list.pop_back(); + + // Assert + EXPECT_TRUE(list.is_empty()); + EXPECT_EQ(nullptr, list.head()); + EXPECT_EQ(nullptr, list.tail()); +} + +TEST(TestDListLib, pop_back_multiple_elements) { + // Arrange + DList list; + list.push_back(1); + list.push_back(2); + list.push_back(3); + + // Act + list.pop_back(); + + // Assert + EXPECT_FALSE(list.is_empty()); + EXPECT_EQ(1, list.head()->value); + EXPECT_EQ(2, list.tail()->value); + EXPECT_EQ(nullptr, list.tail()->next); +} + +TEST(TestDListLib, pop_back_in_empty_list) { + // Arrange + DList list; + + // Act & Assert + EXPECT_ANY_THROW(list.pop_back()); +} + +TEST(TestDListLib, erase_with_position_at_beggining) { + // Arrange + DList list; + list.push_back(1); + list.push_back(2); + list.push_back(3); + + // Act + list.erase((size_t)0); + + // Assert + EXPECT_FALSE(list.is_empty()); + EXPECT_EQ(2, list.head()->value); + EXPECT_EQ(3, list.tail()->value); +} + +TEST(TestDListLib, erase_with_position_in_middle) { + // Arrange + DList list; + list.push_back(1); + list.push_back(2); + list.push_back(3); + + // Act + list.erase((size_t)1); + + // Assert + EXPECT_FALSE(list.is_empty()); + EXPECT_EQ(1, list.head()->value); + EXPECT_EQ(3, list.tail()->value); +} + +TEST(TestDListLib, erase_with_position_at_end) { + // Arrange + DList list; + list.push_back(1); + list.push_back(2); + list.push_back(3); + + // Act + list.erase((size_t)2); + + // Assert + EXPECT_FALSE(list.is_empty()); + EXPECT_EQ(1, list.head()->value); + EXPECT_EQ(2, list.tail()->value); +} + +TEST(TestDListLib, erase_with_position_in_empty_list) { + // Arrange + DList list; + + // Act & Assert + EXPECT_ANY_THROW(list.erase((size_t)0)); +} + +TEST(TestDListLib, erase_with_position_more_than_list) { + // Arrange + DList list; + list.push_back(1); + list.push_back(2); + list.push_back(3); + + // Act & Assert + EXPECT_ANY_THROW(list.erase((size_t)8)); +} + +TEST(TestDListLib, erase_first_node) { + // Arrange + DList list; + list.push_back(1); + list.push_back(2); + list.push_back(3); + NodeD* node = list.head(); + + // Act + list.erase(node); + + // Assert + EXPECT_EQ(2, list.head()->value); + EXPECT_EQ(3, list.tail()->value); +} + +TEST(TestDListLib, erase_with_node_in_middle) { + // Arrange + DList list; + list.push_back(1); + list.push_back(2); + list.push_back(3); + NodeD* node = list.head(); + + // Act + list.erase(node); + + // Assert + EXPECT_FALSE(list.is_empty()); + EXPECT_EQ(2, list.head()->value); + EXPECT_EQ(3, list.tail()->value); +} + +TEST(TestDListLib, erase_with_node_at_end) { + // Arrange + DList list; + list.push_back(1); + list.push_back(2); + list.push_back(3); + NodeD* node = list.tail(); + + // Act + list.erase(node); + + // Assert + EXPECT_FALSE(list.is_empty()); + EXPECT_EQ(1, list.head()->value); + EXPECT_EQ(2, list.tail()->value); +} + +TEST(TestDListLib, erase_after_node_not_in_list) { + // Arrange + DList list; + list.push_back(1); + NodeD* foreign_node = new NodeD(888); + + // Act & Assert + EXPECT_ANY_THROW(list.erase(foreign_node)); + + // Cleanup + delete foreign_node; +} + +TEST(TestDListLib, erase_nullptr) { + // Arrange + DList list; + list.push_back(1); + + // Act & Assert + ASSERT_ANY_THROW(list.erase(nullptr)); +} + +TEST(TestDListLib, operator_assign_empty_to_empty) { + // Arrange + DList list1; + DList list2; + + // Act + list1 = list2; + + // Assert + EXPECT_TRUE(list1.is_empty()); + EXPECT_TRUE(list2.is_empty()); +} + +TEST(TestDListLib, operator_assign_empty_to_not_empty) { + // Arrange + DList list1; + list1.push_front(1); + list1.push_front(2); + list1.push_front(3); + DList list2; + + // Act + list2 = list1; + + // Assert + EXPECT_FALSE(list2.is_empty()); + EXPECT_EQ(3, list2.head()->value); + EXPECT_EQ(2, list2.head()->next->value); + EXPECT_EQ(1, list2.tail()->value); +} + +TEST(TestDListLib, operator_assign_not_empty_to_empty) { + // Arrange + DList list1; + list1.push_back(1); + list1.push_back(2); + DList list2; + + // Act + list1 = list2; + + // Assert + EXPECT_TRUE(list1.is_empty()); + EXPECT_TRUE(list2.is_empty()); +} + +TEST(TestDListLib, operator_assign_self_assignment) { + // Arrange + DList list; + list.push_back(1); + list.push_back(2); + list.push_back(3); + + // Act + list = list; + + // Assert + EXPECT_FALSE(list.is_empty()); + EXPECT_EQ(1, list.head()->value); + EXPECT_EQ(3, list.tail()->value); +} + +TEST(TestDListLib, operator_assign_chain_of_assignment) { + // Arrange + DList list1; + list1.push_back(1); + DList list2; + list2.push_back(2); + DList list3; + list3.push_back(3); + + // Act + list1 = list2 = list3; + + // Assert + EXPECT_EQ(3, list1.head()->value); + EXPECT_EQ(3, list2.head()->value); + EXPECT_EQ(3, list3.head()->value); +} + +TEST(TestDListLib, test_complex_of_operations) { + // Arrange + DList list; + + // Act + list.push_back(1); + list.push_front(0); + list.push_back(3); + list.insert(2, 2); + list.push_back(4); + list.pop_front(); + list.pop_back(); + + // Assert + EXPECT_EQ(1, list.head()->value); + EXPECT_EQ(3, list.tail()->value); + const NodeD* cur = list.head(); + EXPECT_EQ(1, cur->value); + cur = cur->next; + EXPECT_EQ(2, cur->value); + cur = cur->next; + EXPECT_EQ(3, cur->value); + EXPECT_EQ(nullptr, cur->next); +} + +TEST(TestDListLib, test_double_list) { + // Arrange + DList list; + + // Act + list.push_back(1.1); + list.push_back(2.2); + list.push_back(3.3); + + // Assert + EXPECT_NEAR(1.1, list.head()->value, EPSILON); + EXPECT_NEAR(3.3, list.tail()->value, EPSILON); + + // Act + list.erase((size_t)1); + + // Assert + EXPECT_NEAR(1.1, list.head()->value, EPSILON); + EXPECT_NEAR(3.3, list.tail()->value, EPSILON); + EXPECT_NEAR(3.3, list.head()->next->value, EPSILON); +} + +TEST(TestDListLib, test_string_list) { + // Arrange + DList list; + + // Act + list.push_back("Hello"); + list.push_back("World"); + list.push_front("Start"); + + // Assert + EXPECT_EQ("Start", list.head()->value); + EXPECT_EQ("World", list.tail()->value); + + // Act + list.pop_front(); + + // Assert + EXPECT_EQ("Hello", list.head()->value); +} + +TEST(TestDListLib, iterator_read) { + // Arrange + DList list; + for (int i = 0; i < 5; i++) { + list.push_back(i); + } + int i = 0; + + // Act & Assert + for (DList::Iterator it = list.begin(); it != list.end(); it++) { + EXPECT_EQ(i, *it); + i++; + } +} + +TEST(TestDListLib, iterator_write) { + // Arrange + DList list; + for (int i = 0; i < 5; i++) { + list.push_back(0); + } + int i = 1; + int j = 10; + + // Act + for (DList::Iterator it = list.begin(); it != list.end(); it++) { + *it = i * 10; + i++; + } + + // Assert + for (DList::Iterator it = list.begin(); it != list.end(); it++) { + EXPECT_EQ(j, *it); + j += 10; + } +} + +TEST(TestDListLib, iterator_in_empty_list) { + // Arrange + DList list; + + // Act + int iterator_count = 0; + for (DList::Iterator it = list.begin(); it != list.end(); it++) { + iterator_count++; + } + + // Assert + EXPECT_EQ(0, iterator_count); + EXPECT_TRUE(list.begin() == list.end()); + EXPECT_FALSE(list.begin() != list.end()); +} + +TEST(TestDListLib, iterator_operator_assigment) { + // Arrange + DList list; + list.push_back(76); + list.push_back(32); + + // Act + DList::Iterator it1 = list.begin(); + DList::Iterator it2 = it1; + + // Assert + EXPECT_EQ(*it1, *it2); + EXPECT_TRUE(it1 == it2); + ++it1; + EXPECT_EQ(32, *it1); + EXPECT_EQ(76, *it2); + EXPECT_FALSE(it1 == it2); +} + +TEST(TestDListLib, iterator_operator_prefix_plus_plus) { + // Arrange + DList list; + list.push_back(765); + list.push_back(324); + list.push_back(456); + + // Act + DList::Iterator it = list.begin(); + + // Assert + EXPECT_EQ(765, *it); + EXPECT_EQ(324, *++it); + EXPECT_EQ(324, *it); + EXPECT_EQ(456, *++it); +} + +TEST(TestDListLib, iterator_operator_postfix_plus_plus) { + // Arrange + DList list; + list.push_back(765); + list.push_back(324); + list.push_back(456); + + // Act + DList::Iterator it = list.begin(); + + // Assert + EXPECT_EQ(765, *it); + EXPECT_EQ(765, *it++); + EXPECT_EQ(324, *it); +} + +TEST(TestDListLib, iterator_operator_plus_assigment) { + // Arrange + DList list; + for (int i = 1; i < 7; i++) { + list.push_back(i * 10); + } + + // Act + DList::Iterator it = list.begin(); + it += 3; + + // Assert + EXPECT_EQ(40, *it); +} \ No newline at end of file diff --git a/tests/test_list.cpp b/tests/test_list.cpp index 1e7b2b33..fa4f7ac8 100644 --- a/tests/test_list.cpp +++ b/tests/test_list.cpp @@ -2,7 +2,6 @@ #include #include "../lib_list/list.h" -#include "../lib_tvector/tvector.h" #define EPSILON 0.000001 #define TRUE 1 From d229deb5ee70276b382d9a0dcdd1aefaa6d64529 Mon Sep 17 00:00:00 2001 From: Kate-Nits Date: Tue, 9 Dec 2025 05:09:44 +0300 Subject: [PATCH 88/94] Wrote Iterator for TVector and added tests on it --- lib_tvector/tvector.h | 77 ++++++++++++++++++++++++++++++++++++++- tests/test_mathvector.cpp | 2 + tests/test_tvector.cpp | 70 +++++++++++++++++++++++++++++++++++ 3 files changed, 148 insertions(+), 1 deletion(-) diff --git a/lib_tvector/tvector.h b/lib_tvector/tvector.h index ff69de27..eeb633ab 100644 --- a/lib_tvector/tvector.h +++ b/lib_tvector/tvector.h @@ -22,7 +22,7 @@ class TVector { static const size_t MAX_PERCENT_DELETED = 15; TVector() : _data(nullptr), _states(nullptr), _size(0), _capacity(0), _deleted(0) {} - TVector(size_t size) { //конструктор вектора заданного размера + TVector(size_t size) { _size = size; _capacity = size + RESERVE_MEMORY; _deleted = 0; @@ -90,6 +90,79 @@ class TVector { delete[] _data; delete[] _states; } + class Iterator { + T* _ptr; + State* _state_ptr; + size_t _index; + size_t _size; + public: + Iterator(T* ptr, State* state_ptr, size_t index, size_t size) + : _ptr(ptr), _state_ptr(state_ptr), _index(index), _size(size) { + } + + Iterator& operator=(const Iterator& other) { + if (this != &other) { + _ptr = other._ptr; + _state_ptr = other._state_ptr; + _index = other._index; + _size = other._size; + } + return *this; + } + + Iterator& operator++() { // ++it + do { + ++_index; + } while (_index < _size && _state_ptr[_index] != State::busy); + return *this; + } + + Iterator operator++(int) { // it++ + Iterator temp = *this; + ++(*this); + return temp; + } + + Iterator& operator--() { // --it + if (_index == 0) { + _index = _size; + return *this; + } + do { + --_index; + } while (_index > 0 && _state_ptr[_index] != State::busy); + return *this; + } + + Iterator operator--(int) { // it-- + Iterator temp = *this; + --(*this); + return temp; + } + + bool operator==(const Iterator& other) const { + return _ptr == other._ptr && _index == other._index; + } + + bool operator!=(const Iterator& other) const { + return !(*this == other); + } + + T& operator*() { + return _ptr[_index]; + } + }; + Iterator begin() { + size_t first_busy = 0; + while (first_busy < _size && _states[first_busy] != State::busy) { + ++first_busy; + } + return Iterator(_data, _states, first_busy, _size); + } + + Iterator end() { + return Iterator(_data, _states, _size, _size); + } inline T& operator[](size_t index) { return _data[index]; @@ -138,12 +211,14 @@ class TVector { reserve(new_capacity); } + /* inline T* begin() noexcept { // возвращает _data указатель на начало return _data; } inline T* end() noexcept { return _data + _size; } + */ inline const size_t deleted() const noexcept { return _deleted; diff --git a/tests/test_mathvector.cpp b/tests/test_mathvector.cpp index ff70eb30..68dd6f80 100644 --- a/tests/test_mathvector.cpp +++ b/tests/test_mathvector.cpp @@ -243,6 +243,7 @@ TEST(TestMathVectorLib, test_capacity_setter) { EXPECT_TRUE(vec.capacity() >= old_capacity + 20); } +/* TEST(TestMathVectorLib, test_begin_and_end) { // Arrange size_t size = 3; @@ -260,6 +261,7 @@ TEST(TestMathVectorLib, test_begin_and_end) { // Assert EXPECT_EQ(11 + 22 + 37, sum); } +*/ TEST(TestMathVectorLib, test_deleted_counter) { // Arrange diff --git a/tests/test_tvector.cpp b/tests/test_tvector.cpp index 5835229c..839af71b 100644 --- a/tests/test_tvector.cpp +++ b/tests/test_tvector.cpp @@ -195,6 +195,7 @@ TEST(TestTVectorLib, test_capacity_setter) { EXPECT_TRUE(vec.capacity() >= old_capacity + 20); } +/* TEST(TestTVectorLib, test_begin_and_end) { // Arrange size_t size = 3; @@ -212,6 +213,7 @@ TEST(TestTVectorLib, test_begin_and_end) { // Assert EXPECT_EQ(11+22+37, sum); } +*/ TEST(TestTVectorLib, test_deleted_counter) { // Arrange @@ -740,4 +742,72 @@ TEST(TestTVectorLib, test_find_all) { // Assert EXPECT_TRUE(result[0] == 1 && result[1] == 3 && result[2] == 5); delete[] result; // ???? +} + +TEST(TestTVectorLib, iteration_empty_vector) { + // Arrange + TVector vec; + + // Act + auto it = vec.begin(); + auto end_it = vec.end(); + + // Assert + EXPECT_EQ(it, end_it); + int count = 0; + while (it != end_it) { + ++count; + ++it; + } + EXPECT_EQ(0, count); +} + +TEST(TestTVectorLib, write_iterator) { + // Arrange + TVector vec; + vec.push_back(10); + vec.push_back(20); + vec.push_back(30); + + // Act + int k = 2; + for (auto it = vec.begin(); it != vec.end(); ++it) { + *it = *it * k; + k++; + } + + // Assert + EXPECT_EQ(20, vec.at(0)); + EXPECT_EQ(60, vec.at(1)); + EXPECT_EQ(120, vec.at(2)); + auto it = vec.begin(); + EXPECT_EQ(*it, 20); + ++it; + EXPECT_EQ(*it, 60); + ++it; + EXPECT_EQ(*it, 120); + ++it; + EXPECT_EQ(it, vec.end()); +} + +TEST(TestTVectorLib, read_iterator) { + // Arrange + TVector vec; + vec.push_back(5); + vec.push_back(15); + vec.push_back(25); + vec.push_back(35); + vec.erase(1); + + // Act + int sum = 0; + int count = 0; + for (auto it = vec.begin(); it != vec.end(); ++it) { + sum += *it; + ++count; + } + + // Assert + EXPECT_EQ(3, count); + EXPECT_EQ(65, sum); } \ No newline at end of file From f32e99b5286abd8ff4e97576665051db164e04d6 Mon Sep 17 00:00:00 2001 From: Kate-Nits Date: Tue, 9 Dec 2025 11:27:48 +0300 Subject: [PATCH 89/94] Moved the realization of the methods of the class TVector outside of the class --- lib_tvector/tvector.h | 780 ++++++++++++++++++++++++------------------ 1 file changed, 441 insertions(+), 339 deletions(-) diff --git a/lib_tvector/tvector.h b/lib_tvector/tvector.h index eeb633ab..0a224c15 100644 --- a/lib_tvector/tvector.h +++ b/lib_tvector/tvector.h @@ -32,7 +32,7 @@ class TVector { _states[i] = i < _size ? State::busy : State::empty; } } - TVector(T* data, size_t size) { //конструктор созданный на основе переаного массива данных + TVector(T* data, size_t size) { _size = size; _capacity = size + RESERVE_MEMORY; _deleted = 0; @@ -44,7 +44,7 @@ class TVector { _states = new State[_capacity]; } catch (const std::bad_alloc&) { - delete[] _data; //тк мы же уже под _data выделили + delete[] _data; throw; } for (size_t i = 0; i < _capacity; ++i) { @@ -57,7 +57,7 @@ class TVector { } } } - TVector(const TVector& other) { // конструктор копирования + TVector(const TVector& other) { _size = other._size; _capacity = other._capacity; _deleted = other._deleted; @@ -159,426 +159,528 @@ class TVector { } return Iterator(_data, _states, first_busy, _size); } - Iterator end() { return Iterator(_data, _states, _size, _size); } - inline T& operator[](size_t index) { - return _data[index]; - } - inline const T& operator[](size_t index) const { - return _data[index]; - } + inline T& operator[](size_t index); + inline const T& operator[](size_t index) const; + inline T& at(size_t index); + inline const T& at(size_t index) const; + inline const T* data() const noexcept; + inline const T& data(size_t i) const; + inline const State* states() const noexcept; + inline const State state(size_t i) const; + inline const size_t size() const noexcept; + inline void size(size_t new_size) noexcept; + inline const size_t capacity() const noexcept; + inline void capacity(size_t new_capacity) noexcept; + /* + inline T* begin() noexcept; // возвращала _data указатель на начало + inline T* end() noexceptж + */ + inline const size_t deleted() const noexcept; - inline T& at(size_t index) { - size_t real_index = check_index(index); - return _data[real_index]; - } - inline const T& at(size_t index) const { - size_t real_index = check_index(index); - return _data[real_index]; - } + inline T& front(); //Доступ к первому элементу, который не deleted и не empty + inline T& back(); //Доступ к последнему элементу, который не deleted и не empty + inline const T& back() const; //Доступ к последнему элементу, который не deleted и не empty - inline const T* data() const noexcept { - return _data; - } + inline bool is_empty() const noexcept; - inline const T& data(size_t i) const { - size_t real_index = check_index(i); - return _data[real_index]; - } + void push_front(const T& value) noexcept; + void push_back(const T& value) noexcept; + void insert(size_t index, const T& value); - inline const State* states() const noexcept { - return _states; - } + void pop_front(); + void pop_back(); + void erase(size_t index); - inline const State state(size_t i) const { - return _states[i]; - } + void emplace(size_t index, T&& value); + void emplace(size_t index, const T& value); - inline const size_t size() const noexcept {//get - return _size; - } - inline void size(size_t new_size) noexcept { //set - resize(new_size); - } + TVector& assign(const TVector& other); + TVector& operator=(const TVector& other); - inline const size_t capacity() const noexcept { //get - return _capacity; - } - inline void capacity(size_t new_capacity) noexcept { //set - reserve(new_capacity); - } + void clear() noexcept; - /* - inline T* begin() noexcept { // возвращает _data указатель на начало - return _data; - } - inline T* end() noexcept { - return _data + _size; - } - */ + bool operator == (const TVector& other) const noexcept; + bool operator != (const TVector& other) const noexcept; - inline const size_t deleted() const noexcept { - return _deleted; - } + void reserve(size_t new_capacity); // увеличивает _capacity - inline T& front() { //Доступ к первому элементу, который не deleted и не empty - for (size_t i = 0; i < _size; ++i) - if (_states[i] == busy) - return _data[i]; - throw std::out_of_range("No busy elements in vector"); - } + void resize(size_t new_size); + void resize(size_t new_size, const T& value); - inline T& back() { //Доступ к последнему элементу, который не deleted и не empty - for (size_t i = _size; i-- > 0; ) - if (_states[i] == busy) - return _data[i]; - throw std::out_of_range("No busy elements in vector"); - } - inline const T& back() const { //Доступ к последнему элементу, который не deleted и не empty - for (size_t i = _size; i-- > 0; ) - if (_states[i] == busy) - return _data[i]; - throw std::out_of_range("No busy elements in vector"); + void shrink_to_fit(); // уменьшение размера, удаляя неиспользуемую память + + void print(); + + template + friend std::ostream& operator<<(std::ostream& out, const TVector& vec); + + template + friend void swap(U& a, U& b); + + template + friend void shuffle(TVector& vec); + + template + friend void quick_sort(TVector& vec); + + template + friend size_t find_first(const TVector& vec, const U& value); + + template + friend size_t find_last(const TVector& vec, const U& value); + + template + friend size_t* find_all(const TVector& vec, const U& value); + + template + friend void quick_sort_realisation(TVector& vec, int left, int right); + +private: + size_t check_index(size_t index) const; + inline bool is_full() const noexcept; +}; + +template +inline T& TVector::operator[](size_t index) { + return _data[index]; +} + +template +inline const T& TVector::operator[](size_t index) const { + return _data[index]; +} + +template +inline T& TVector::at(size_t index) { + size_t real_index = check_index(index); + return _data[real_index]; +} + +template +inline const T& TVector::at(size_t index) const { + size_t real_index = check_index(index); + return _data[real_index]; +} + +template +inline const T* TVector::data() const noexcept { + return _data; +} + +template +inline const T& TVector::data(size_t i) const { + size_t real_index = check_index(i); + return _data[real_index]; +} + +template +inline const State* TVector::states() const noexcept { + return _states; +} + +template +inline const State TVector::state(size_t i) const { + return _states[i]; +} + +template +inline const size_t TVector::size() const noexcept { + return _size; +} + +template +inline void TVector::size(size_t new_size) noexcept { + resize(new_size); +} + +template +inline const size_t TVector::capacity() const noexcept { + return _capacity; +} + +template +inline void TVector::capacity(size_t new_capacity) noexcept { + reserve(new_capacity); +} + +/* +template +inline T* TVector::begin() noexcept { // возвращает _data указатель на начало + return _data; +} + +template +inline T* TVector::end() noexcept { + return _data + _size; +} +*/ + +template +inline const size_t TVector::deleted() const noexcept { + return _deleted; +} + +template +inline T& TVector::front() { //Доступ к первому элементу, который не deleted и не empty + for (size_t i = 0; i < _size; ++i) + if (_states[i] == busy) + return _data[i]; + throw std::out_of_range("No busy elements in vector"); +} + +template +inline T& TVector::back() { //Доступ к последнему элементу, который не deleted и не empty + for (size_t i = _size; i-- > 0; ) + if (_states[i] == busy) + return _data[i]; + throw std::out_of_range("No busy elements in vector"); +} + +template +inline const T& TVector::back() const { //Доступ к последнему элементу, который не deleted и не empty + for (size_t i = _size; i-- > 0; ) + if (_states[i] == busy) + return _data[i]; + throw std::out_of_range("No busy elements in vector"); +} + +template +inline bool TVector::is_empty() const noexcept { return (_size - _deleted) == 0; } + +template +void TVector::push_front(const T& value) noexcept { + if (is_full()) { + reserve(_capacity + RESERVE_MEMORY); } - inline bool is_empty() const noexcept { return (_size - _deleted) == 0; } + for (size_t i = _size; i > 0; --i) { // Сдвигаем всё вправо + _data[i] = _data[i - 1]; + _states[i] = _states[i - 1]; + } + _data[0] = value; + _states[0] = busy; + ++_size; +} - //функции вставки - void push_front(const T& value) noexcept { - if (is_full()) { - reserve(_capacity + RESERVE_MEMORY); - } +template +void TVector::push_back(const T& value) noexcept { + if (is_full()) { + reserve(_capacity + RESERVE_MEMORY); + } + _data[_size] = value; + _states[_size] = busy; + ++_size; +} - for (size_t i = _size; i > 0; --i) { // Сдвигаем всё вправо - _data[i] = _data[i - 1]; - _states[i] = _states[i - 1]; - } - _data[0] = value; - _states[0] = busy; - ++_size; +template +void TVector::insert(size_t index, const T& value) { + if (index > _size) { + throw std::out_of_range("Insert index out of range"); } - void push_back(const T& value) noexcept { - if (is_full()) { - reserve(_capacity + RESERVE_MEMORY); - } - _data[_size] = value; - _states[_size] = busy; - ++_size; + if (is_full()) { + reserve(_capacity + RESERVE_MEMORY); } - void insert(size_t index, const T& value) { - if (index > _size) { - throw std::out_of_range("Insert index out of range"); - } - if (is_full()) { - reserve(_capacity + RESERVE_MEMORY); - } - size_t real_index = check_index(index); - for (size_t i = _size; i > real_index; --i) { // Сдвигаем вправо - _data[i] = _data[i - 1]; - _states[i] = _states[i - 1]; - } - _data[real_index] = value; - _states[real_index] = busy; - ++_size; + size_t real_index = check_index(index); + for (size_t i = _size; i > real_index; --i) { // Сдвигаем вправо + _data[i] = _data[i - 1]; + _states[i] = _states[i - 1]; } + _data[real_index] = value; + _states[real_index] = busy; + ++_size; +} - //функции удаления - void pop_front() { - if (_data != nullptr && _states != nullptr) { - for (size_t i = 0; i < _size; ++i) { - if (_states[i] == State::busy) { - _states[i] = State::deleted; - ++_deleted; - if (_size > 0 && ((_deleted * 100 / _size) > MAX_PERCENT_DELETED)) { - shrink_to_fit(); - } - return; +template +void TVector::pop_front() { + if (_data != nullptr && _states != nullptr) { + for (size_t i = 0; i < _size; ++i) { + if (_states[i] == State::busy) { + _states[i] = State::deleted; + ++_deleted; + if (_size > 0 && ((_deleted * 100 / _size) > MAX_PERCENT_DELETED)) { + shrink_to_fit(); } + return; } } - throw std::underflow_error("Vector is empty"); } - void pop_back() { - if (_data != nullptr && _states != nullptr) { - for (size_t i = _size - 1; ; --i) { - if (_states[i] == State::deleted) { - _states[i] = State::empty; - --_size; - --_deleted; - } - if (_states[i] == State::busy) { - _states[i] = State::empty; - --_size; - return; - } - if (i <= 0) break; // иначе уйдём в переполнение + throw std::underflow_error("Vector is empty"); +} + +template +void TVector::pop_back() { + if (_data != nullptr && _states != nullptr) { + for (size_t i = _size - 1; ; --i) { + if (_states[i] == State::deleted) { + _states[i] = State::empty; + --_size; + --_deleted; } - } - throw std::underflow_error("Vector is empty"); - } - void erase(size_t index) { - size_t real_index = check_index(index); - _states[real_index] = State::deleted; - ++_deleted; - if (_size > 0 && ((_deleted * 100 / _size) > MAX_PERCENT_DELETED)) { - shrink_to_fit(); + if (_states[i] == State::busy) { + _states[i] = State::empty; + --_size; + return; + } + if (i <= 0) break; // иначе уйдём в переполнение } } + throw std::underflow_error("Vector is empty"); +} - //замена значения - void emplace(size_t index, T&& value) { - size_t real_index = check_index(index); - _data[real_index] = value; - _states[real_index] = State::busy; +template +void TVector::erase(size_t index) { + size_t real_index = check_index(index); + _states[real_index] = State::deleted; + ++_deleted; + if (_size > 0 && ((_deleted * 100 / _size) > MAX_PERCENT_DELETED)) { + shrink_to_fit(); } +} - void emplace(size_t index, const T& value) { - size_t real_index = check_index(index); - _data[real_index] = value; - _states[real_index] = State::busy; - } +template +void TVector::emplace(size_t index, T&& value) { + size_t real_index = check_index(index); + _data[real_index] = value; + _states[real_index] = State::busy; +} - TVector& assign(const TVector& other) { - if (this != &other) { - delete[] _data; - delete[] _states; +template +void TVector::emplace(size_t index, const T& value) { + size_t real_index = check_index(index); + _data[real_index] = value; + _states[real_index] = State::busy; +} - _size = other._size; - _capacity = other._capacity; - _deleted = other._deleted; +template +TVector& TVector::assign(const TVector& other) { + if (this != &other) { + delete[] _data; + delete[] _states; - _data = new T[_capacity]; - _states = new State[_capacity]; - for (size_t i = 0; i < _size; ++i) { - _data[i] = other._data[i]; - _states[i] = other._states[i]; - } - for (size_t i = _size; i < _capacity; ++i) - _states[i] = State::empty; + _size = other._size; + _capacity = other._capacity; + _deleted = other._deleted; + + _data = new T[_capacity]; + _states = new State[_capacity]; + for (size_t i = 0; i < _size; ++i) { + _data[i] = other._data[i]; + _states[i] = other._states[i]; } - return *this; + for (size_t i = _size; i < _capacity; ++i) + _states[i] = State::empty; } + return *this; +} - TVector& operator=(const TVector& other) { - if (this != &other) { //проверка на самоприсваивание - delete[] _data; - delete[] _states; +template +TVector& TVector::operator=(const TVector& other) { + if (this != &other) { //проверка на самоприсваивание + delete[] _data; + delete[] _states; - _size = other._size; - _capacity = other._capacity; - _deleted = other._deleted; + _size = other._size; + _capacity = other._capacity; + _deleted = other._deleted; - _data = new T[_capacity]; - _states = new State[_capacity]; + _data = new T[_capacity]; + _states = new State[_capacity]; - for (size_t i = 0; i < _size; ++i) { - _data[i] = other._data[i]; - _states[i] = other._states[i]; - } - for (size_t i = _size; i < _capacity; ++i) - _states[i] = State::empty; + for (size_t i = 0; i < _size; ++i) { + _data[i] = other._data[i]; + _states[i] = other._states[i]; } - return *this; + for (size_t i = _size; i < _capacity; ++i) + _states[i] = State::empty; } + return *this; +} - void clear() noexcept { - for (size_t i = 0; i < _size; ++i) { - _states[i] = State::empty; - } - _size = 0; - _deleted = 0; +template +void TVector::clear() noexcept { + for (size_t i = 0; i < _size; ++i) { + _states[i] = State::empty; } + _size = 0; + _deleted = 0; +} - bool operator == (const TVector& other) const noexcept { - if (_size != other._size) { +template +bool TVector::operator == (const TVector& other) const noexcept { + if (_size != other._size) { + return false; + } + for (size_t i = 0; i < _size; ++i) { + if (_states[i] == busy && _data[i] != other._data[i]) { return false; } - for (size_t i = 0; i < _size; ++i) { - if (_states[i] == busy && _data[i] != other._data[i]) { - return false; - } - } - return true; } + return true; +} - bool operator != (const TVector& other) const noexcept { - if (*this == other) { - return false; - } - if (_size != other._size) { +template +bool TVector::operator != (const TVector& other) const noexcept { + if (*this == other) { + return false; + } + if (_size != other._size) { + return true; + } + for (size_t i = 0; i < _size; ++i) { + if (_states[i] != other._states[i]) { return true; } - for (size_t i = 0; i < _size; ++i) { - if (_states[i] != other._states[i]) { - return true; - } - if (_states[i] == busy && _data[i] != other._data[i]) { - return true; - } + if (_states[i] == busy && _data[i] != other._data[i]) { + return true; } - return false; } + return false; +} - void reserve(size_t new_capacity) { // увеличивает _capacity - if (new_capacity <= _capacity) return; - - T* new_data = new T[new_capacity]; - State* new_states = new State[new_capacity]; - - for (size_t i = 0; i < _size; ++i) { // Копируем существующие элементы - new_data[i] = _data[i]; - new_states[i] = _states[i]; - } +template +void TVector::reserve(size_t new_capacity) { // увеличивает _capacity + if (new_capacity <= _capacity) return; - for (size_t i = _size; i < new_capacity; ++i) { // Помечаем оставшиеся ячейки как пустые - new_states[i] = State::empty; - } + T* new_data = new T[new_capacity]; + State* new_states = new State[new_capacity]; - delete[] _data; - delete[] _states; - _data = new_data; - _states = new_states; - _capacity = new_capacity; + for (size_t i = 0; i < _size; ++i) { // Копируем существующие элементы + new_data[i] = _data[i]; + new_states[i] = _states[i]; } - void resize(size_t new_size) { - if (new_size < _size) { - for (size_t i = new_size; i < _size; ++i) { - if (_states[i] == State::deleted) - --_deleted; - _states[i] = State::empty; - _data[i].~T(); - } - _size = new_size; - return; - } + for (size_t i = _size; i < new_capacity; ++i) { // Помечаем оставшиеся ячейки как пустые + new_states[i] = State::empty; + } - if (new_size > _capacity) { - reserve(new_size + RESERVE_MEMORY); - } + delete[] _data; + delete[] _states; + _data = new_data; + _states = new_states; + _capacity = new_capacity; +} - for (size_t i = _size; i < new_size; ++i) { - new (_data + i) T(); // размещает результат в уже выделенной памяти по адресу _data + i - _states[i] = busy; +template +void TVector::resize(size_t new_size) { + if (new_size < _size) { + for (size_t i = new_size; i < _size; ++i) { + if (_states[i] == State::deleted) + --_deleted; + _states[i] = State::empty; + _data[i].~T(); } _size = new_size; + return; } - void resize(size_t new_size, const T& value) { - if (new_size == _size) { return; } - if (new_size < _size) { - for (size_t i = new_size; i < _size; ++i) { - if (_states[i] == State::deleted) - --_deleted; - _states[i] = empty; - _data[i].~T(); - } - _size = new_size; - return; - } - if (new_size > _capacity) { - reserve(new_size + RESERVE_MEMORY); - } - for (size_t i = _size; i < new_size; ++i) { - new (_data + i) T(value); // размещает результат в уже выделенной памяти по адресу _data + i - _states[i] = busy; + if (new_size > _capacity) { + reserve(new_size + RESERVE_MEMORY); + } + + for (size_t i = _size; i < new_size; ++i) { + new (_data + i) T(); // размещает результат в уже выделенной памяти по адресу _data + i + _states[i] = busy; + } + _size = new_size; +} + +template +void TVector::resize(size_t new_size, const T& value) { + if (new_size == _size) { return; } + if (new_size < _size) { + for (size_t i = new_size; i < _size; ++i) { + if (_states[i] == State::deleted) + --_deleted; + _states[i] = empty; + _data[i].~T(); } _size = new_size; + return; + } + if (new_size > _capacity) { + reserve(new_size + RESERVE_MEMORY); } + for (size_t i = _size; i < new_size; ++i) { + new (_data + i) T(value); // размещает результат в уже выделенной памяти по адресу _data + i + _states[i] = busy; + } + _size = new_size; +} - void shrink_to_fit() { // уменьшение размера, удаляя неиспользуемую память - size_t busy_count = 0; - for (size_t i = 0; i < _size; ++i) { // Считаем busy элементы - if (_states[i] == busy) - ++busy_count; - } - if (busy_count == _size && _capacity == _size) { return; } +template +void TVector::shrink_to_fit() { // уменьшение размера, удаляя неиспользуемую память + size_t busy_count = 0; + for (size_t i = 0; i < _size; ++i) { // Считаем busy элементы + if (_states[i] == busy) + ++busy_count; + } + if (busy_count == _size && _capacity == _size) { return; } - size_t new_capacity = busy_count + RESERVE_MEMORY; - T* new_data = new T[new_capacity]; - State* new_states = new State[new_capacity]; + size_t new_capacity = busy_count + RESERVE_MEMORY; + T* new_data = new T[new_capacity]; + State* new_states = new State[new_capacity]; - size_t new_index = 0; - for (size_t i = 0; i < _size; ++i) { - if (_states[i] == busy) { - new_data[new_index] = _data[i]; - new_states[new_index] = busy; - ++new_index; - } + size_t new_index = 0; + for (size_t i = 0; i < _size; ++i) { + if (_states[i] == busy) { + new_data[new_index] = _data[i]; + new_states[new_index] = busy; + ++new_index; } + } - for (size_t i = new_index; i < new_capacity; ++i) { - new_states[i] = empty; - } + for (size_t i = new_index; i < new_capacity; ++i) { + new_states[i] = empty; + } - delete[] _data; - delete[] _states; + delete[] _data; + delete[] _states; - _data = new_data; - _states = new_states; - _capacity = new_capacity; - _size = busy_count; - _deleted = 0; + _data = new_data; + _states = new_states; + _capacity = new_capacity; + _size = busy_count; + _deleted = 0; +} + +template +void TVector::print() { + std::cout << "[ "; + for (size_t i = 0; i < _size; ++i) { + if (_states[i] == State::busy) { + std::cout << _data[i] << " "; + } } + std::cout << "]"; +} - void print() { - std::cout << "[ "; - for (size_t i = 0; i < _size; ++i) { - if (_states[i] == State::busy) { - std::cout << _data[i] << " "; +template +size_t TVector::check_index(size_t index) const { + if (index >= _size) { + throw std::out_of_range("Index out of bounds: index >= size"); + } + size_t count = 0; + for (size_t i = 0; i < _size; i++) { + if (_states[i] == State::busy) { + if (count == index) { + return i; } + count++; } - std::cout << "]"; } + throw std::out_of_range("There no element with this index"); +} - template - friend std::ostream& operator<<(std::ostream& out, const TVector& vec); - - template - friend void swap(U& a, U& b); - - template - friend void shuffle(TVector& vec); - - template - friend void quick_sort(TVector& vec); - - template - friend size_t find_first(const TVector& vec, const U& value); - - template - friend size_t find_last(const TVector& vec, const U& value); - - template - friend size_t* find_all(const TVector& vec, const U& value); - - template - friend void quick_sort_realisation(TVector& vec, int left, int right); +template +inline bool TVector::is_full() const noexcept { // функция проверки на заполненость + return _size >= _capacity; +} -private: - size_t check_index(size_t index) const { - if (index >= _size) { - throw std::out_of_range("Index out of bounds: index >= size"); - } - size_t count = 0; - for (size_t i = 0; i < _size; i++) { - if (_states[i] == State::busy) { - if (count == index) { - return i; - } - count++; - } - } - throw std::out_of_range("There no element with this index"); - } - inline bool is_full() const noexcept { // функция проверки на заполненость - return _size >= _capacity; - } -}; template void swap(T& a, T& b) { From e2e0ea5b50509149e9bdfdcd4367d7d1a9c8aab8 Mon Sep 17 00:00:00 2001 From: Kate-Nits Date: Tue, 9 Dec 2025 13:20:15 +0300 Subject: [PATCH 90/94] Added function is_looped_hare_and_turtle, is_looped_turn_ptr and find_loop_node --- lib_algorithms/CMakeLists.txt | 2 + lib_algorithms/algorithms.cpp | 91 +++++++++++++++++++++++++++++++++++ lib_algorithms/algorithms.h | 10 ++++ lib_dlist/dlist.h | 6 +-- lib_list/list.h | 6 +-- lib_tvector/tvector.h | 16 ++++-- tests/test_algorithms.cpp | 10 +++- 7 files changed, 129 insertions(+), 12 deletions(-) diff --git a/lib_algorithms/CMakeLists.txt b/lib_algorithms/CMakeLists.txt index 3a7d8314..85a989dc 100644 --- a/lib_algorithms/CMakeLists.txt +++ b/lib_algorithms/CMakeLists.txt @@ -2,5 +2,7 @@ create_project_lib(Algorithms) add_depend(Algorithms Circle lib_circle) add_depend(Algorithms Sphere lib_sphere) add_depend(Algorithms Stack lib_stack) +add_depend(Algorithms Node lib_node) +add_depend(Algorithms List lib_list) add_depend(Algorithms Matrix lib_matrix) add_depend(Algorithms TriangleMatrix lib_triangle_matrix) \ No newline at end of file diff --git a/lib_algorithms/algorithms.cpp b/lib_algorithms/algorithms.cpp index 69175c30..568a77b2 100644 --- a/lib_algorithms/algorithms.cpp +++ b/lib_algorithms/algorithms.cpp @@ -11,6 +11,8 @@ #include "../lib_stack/stack.h" #include "../lib_matrix/matrix.h" #include "../lib_triangle_matrix/triangle_matrix.h" +#include "../lib_node/node.h" +#include "../lib_list/list.h" #define START_MENU_FOR_MATRIX_SIZE 3 #define START_MENU_MATRIX_CALCULATE_SIZE 4 @@ -495,4 +497,93 @@ bool check_brackets(const std::string& str) { } } return stack.is_empty(); +} + +template +bool is_looped_hare_and_turtle(const List& list) { + Node* head = list.head(); + if (head == nullptr) { + return false; + } + Node* fast = head; + Node* slow = head; + while (fast != nullptr && fast->next != nullptr) { + slow = slow->next; + fast = fast->next->next; + if (slow == fast) { + return true; + } + } + return false; +} + +template +bool is_looped_turn_ptr(List& list) { + Node* head = list.head(); + if (head == nullptr || head->next == nullptr) { + return false; + } + + Node* cur = head; + Node* prev = nullptr; + Node* next = nullptr; + + while (cur != nullptr) { + next = cur->next; + cur->next = prev; + prev = cur; + cur = next; + + if (cur == head) { + cur = prev; + prev = nullptr; + while (cur != nullptr) { + next = cur->next; + cur->next = prev; + prev = cur; + cur = next; + } + return true; + } + } + cur = prev; + prev = nullptr; + while (current != nullptr) { + next = cur->next; + cur->next = prev; + prev = cur; + cur = next; + } + return false; +} + +template +Node* find_loop_node(List& list) { + Node* head = list.head(); + if (head == nullptr) { + return nullptr; + } + Node* slow = head; + Node* fast = head; + bool has_loop = false; + while (fast != nullptr && fast->next != nullptr) { + slow = slow->next; + fast = fast->next->next; + if (slow == fast) { + has_loop = true; + break; + } + } + if (!has_loop) { + return nullptr; + } + if (slow == fast) { + slow = head; + while (slow != fast) { + slow = slow->next; + fast = fast->next; + } + return slow; + } + return nullptr; } \ No newline at end of file diff --git a/lib_algorithms/algorithms.h b/lib_algorithms/algorithms.h index c91afbe8..04268226 100644 --- a/lib_algorithms/algorithms.h +++ b/lib_algorithms/algorithms.h @@ -11,6 +11,9 @@ #include "../lib_mathvector/MathVector.h" #include "../lib_matrix/matrix.h" #include "../lib_triangle_matrix/triangle_matrix.h" +#include "../lib_stack/stack.h" +#include "../lib_node/node.h" +#include "../lib_list/list.h" #define START_MENU_MATRIX_SIZE 3 #define REALISED_Matrix @@ -194,4 +197,11 @@ inline bool is_close_bracket(char symbol); inline bool matches_pair(char open, char close); bool check_brackets(const std::string& str); +template +bool is_looped_hare_and_turtle(const List& list); +template +bool is_looped_turn_ptr(List& list); +template +Node* find_loop_node(List& list); + #endif // LIB_ALGORITHMS_H \ No newline at end of file diff --git a/lib_dlist/dlist.h b/lib_dlist/dlist.h index 5709dfa7..37007758 100644 --- a/lib_dlist/dlist.h +++ b/lib_dlist/dlist.h @@ -49,12 +49,12 @@ class DList { } return *this; } - bool operator==(const Iterator& other) const { - return _current == other._current; - } bool operator!=(const Iterator& other) const { return _current != other._current; } + bool operator==(const Iterator& other) const { + return !(*this != other); + } T& operator*() { return _current->value; } diff --git a/lib_list/list.h b/lib_list/list.h index de0ee448..b106bd3f 100644 --- a/lib_list/list.h +++ b/lib_list/list.h @@ -49,12 +49,12 @@ class List { } return *this; } - bool operator==(const Iterator& other) const { - return _current == other._current; - } bool operator!=(const Iterator& other) const { return _current != other._current; } + bool operator==(const Iterator& other) const { + return !(*this != other); + } T& operator*() { return _current->value; } diff --git a/lib_tvector/tvector.h b/lib_tvector/tvector.h index 0a224c15..30485f9d 100644 --- a/lib_tvector/tvector.h +++ b/lib_tvector/tvector.h @@ -124,13 +124,19 @@ class TVector { } Iterator& operator--() { // --it - if (_index == 0) { + size_t original_index = _index; + while (_index > 0) { + --_index; + if (_state_ptr[_index] == State::busy) { + return *this; + } + } + if (original_index == _size) { _index = _size; - return *this; } - do { - --_index; - } while (_index > 0 && _state_ptr[_index] != State::busy); + else { + _index = original_index; + } return *this; } diff --git a/tests/test_algorithms.cpp b/tests/test_algorithms.cpp index 598ab0ce..fd89abcd 100644 --- a/tests/test_algorithms.cpp +++ b/tests/test_algorithms.cpp @@ -104,10 +104,18 @@ TEST(TestAlgorithmsLib, mixed_brackets_incorrect) { EXPECT_FALSE(check_brackets(str)); } -TEST(TestAlgorithmsLib, real_example_with_text) { +TEST(TestAlgorithmsLib, real_example1_with_text) { // Arrange std::string str = "[5*(x+8)-9]/[(7/(y*1))*(y*1)]"; // Act & Assert EXPECT_TRUE(check_brackets(str)); +} + +TEST(TestAlgorithmsLib, real_example2_with_text) { + // Arrange + std::string str = "[5*(x+(8)-9]/[(7/(y*1))*(y*1)]"; + + // Act & Assert + EXPECT_FALSE(check_brackets(str)); } \ No newline at end of file From 36a08898bbcebe7a676b1d3160b2f2b5586bbcb9 Mon Sep 17 00:00:00 2001 From: Kate-Nits Date: Wed, 10 Dec 2025 02:34:52 +0300 Subject: [PATCH 91/94] Edits from Marina Andreevna and wrote tests for functions is_looped_hare_and_turtle, is_looped_turn_ptr and find_loop_node --- lib_algorithms/algorithms.cpp | 91 +-------- lib_algorithms/algorithms.h | 89 ++++++++- lib_dlist/dlist.h | 2 + lib_mathvector/MathVector.h | 254 +++++++++++++++----------- lib_matrix/matrix.h | 16 +- lib_triangle_matrix/triangle_matrix.h | 7 +- lib_tvector/tvector.h | 1 + tests/test_algorithms.cpp | 196 ++++++++++++++++++++ tests/test_dlist.cpp | 20 ++ 9 files changed, 464 insertions(+), 212 deletions(-) diff --git a/lib_algorithms/algorithms.cpp b/lib_algorithms/algorithms.cpp index 568a77b2..a28c49d1 100644 --- a/lib_algorithms/algorithms.cpp +++ b/lib_algorithms/algorithms.cpp @@ -121,7 +121,7 @@ void what_matrices(int& what_the_first_matrix, int& what_the_second_matrix, int& std::cout << " What will the first matrix be like?" << std::endl; std::cout << " 1. Standart" << std::endl; std::cout << " 2. Triangle" << std::endl; - std::cout << " 5. Exit" << std::endl; + std::cout << " 3. Exit" << std::endl; while (!input_user_choice(link_user_choice, WHAT_MATRIX_MENU)); if (user_choice == 3) { isExit = YES; @@ -497,93 +497,4 @@ bool check_brackets(const std::string& str) { } } return stack.is_empty(); -} - -template -bool is_looped_hare_and_turtle(const List& list) { - Node* head = list.head(); - if (head == nullptr) { - return false; - } - Node* fast = head; - Node* slow = head; - while (fast != nullptr && fast->next != nullptr) { - slow = slow->next; - fast = fast->next->next; - if (slow == fast) { - return true; - } - } - return false; -} - -template -bool is_looped_turn_ptr(List& list) { - Node* head = list.head(); - if (head == nullptr || head->next == nullptr) { - return false; - } - - Node* cur = head; - Node* prev = nullptr; - Node* next = nullptr; - - while (cur != nullptr) { - next = cur->next; - cur->next = prev; - prev = cur; - cur = next; - - if (cur == head) { - cur = prev; - prev = nullptr; - while (cur != nullptr) { - next = cur->next; - cur->next = prev; - prev = cur; - cur = next; - } - return true; - } - } - cur = prev; - prev = nullptr; - while (current != nullptr) { - next = cur->next; - cur->next = prev; - prev = cur; - cur = next; - } - return false; -} - -template -Node* find_loop_node(List& list) { - Node* head = list.head(); - if (head == nullptr) { - return nullptr; - } - Node* slow = head; - Node* fast = head; - bool has_loop = false; - while (fast != nullptr && fast->next != nullptr) { - slow = slow->next; - fast = fast->next->next; - if (slow == fast) { - has_loop = true; - break; - } - } - if (!has_loop) { - return nullptr; - } - if (slow == fast) { - slow = head; - while (slow != fast) { - slow = slow->next; - fast = fast->next; - } - return slow; - } - return nullptr; } \ No newline at end of file diff --git a/lib_algorithms/algorithms.h b/lib_algorithms/algorithms.h index 04268226..ebd9a309 100644 --- a/lib_algorithms/algorithms.h +++ b/lib_algorithms/algorithms.h @@ -198,10 +198,91 @@ inline bool matches_pair(char open, char close); bool check_brackets(const std::string& str); template -bool is_looped_hare_and_turtle(const List& list); -template -bool is_looped_turn_ptr(List& list); +bool is_looped_hare_and_turtle(List& list) { + Node* head = list.head(); + if (head == nullptr) { + return false; + } + Node* fast = head; + Node* slow = head; + while (fast != nullptr && fast->next != nullptr) { + slow = slow->next; + fast = fast->next->next; + if (slow == fast) { + return true; + } + } + return false; +} + template -Node* find_loop_node(List& list); +bool is_looped_turn_ptr(List& list) { + Node* head = list.head(); + if (head == nullptr || head->next == nullptr) { + return false; + } + + Node* cur = head; + Node* prev = nullptr; + Node* next = nullptr; + + while (cur != nullptr) { + next = cur->next; + cur->next = prev; + prev = cur; + cur = next; + if (cur == head) { + cur = prev; + prev = nullptr; + while (cur != nullptr) { + next = cur->next; + cur->next = prev; + prev = cur; + cur = next; + } + return true; + } + } + cur = prev; + prev = nullptr; + while (cur != nullptr) { + next = cur->next; + cur->next = prev; + prev = cur; + cur = next; + } + return false; +} + +template +Node* find_loop_node(List& list) { + Node* head = list.head(); + if (head == nullptr) { + return nullptr; + } + Node* slow = head; + Node* fast = head; + bool has_loop = false; + while (fast != nullptr && fast->next != nullptr) { + slow = slow->next; + fast = fast->next->next; + if (slow == fast) { + has_loop = true; + break; + } + } + if (!has_loop) { + return nullptr; + } + if (slow == fast) { + slow = head; + while (slow != fast) { + slow = slow->next; + fast = fast->next; + } + return slow; + } + return nullptr; +} #endif // LIB_ALGORITHMS_H \ No newline at end of file diff --git a/lib_dlist/dlist.h b/lib_dlist/dlist.h index 37007758..ef9f27ee 100644 --- a/lib_dlist/dlist.h +++ b/lib_dlist/dlist.h @@ -88,6 +88,8 @@ class DList { }; Iterator begin() { return Iterator(_head); } Iterator end() { return Iterator(nullptr); } + Iterator rbegin() { return Iterator(_tail); } + Iterator rend() { return Iterator(nullptr); } inline NodeD* head(); inline const NodeD* head() const; diff --git a/lib_mathvector/MathVector.h b/lib_mathvector/MathVector.h index 8f2a303b..5948d1a1 100644 --- a/lib_mathvector/MathVector.h +++ b/lib_mathvector/MathVector.h @@ -47,126 +47,166 @@ class MathVector : public TVector { } } - MathVector operator+(const MathVector& other) const { - if (this->size()!= other._size) { - throw std::invalid_argument("Vectors should have same size for addition"); - } - MathVector result(this->_size); - for (size_t i = 0; i < this->_size; ++i) { - result[i] = this->data(i) + other._data[i]; - result._states[i] = busy; - } - return result; - } + MathVector operator+(const MathVector& other) const; template - MathVector> operator+(const MathVector& other) const { - using R = std::common_type_t; // std::common_type_t - if (this->size()!= other.size()) { - throw std::invalid_argument("Vectors should have same size for addition"); - } - MathVector result(this->_size); - for (size_t i = 0; i < this->_size; ++i) { - result[i] = static_cast(this->data(i)) + static_cast(other.data(i)); - } - return result; - } + MathVector> operator+(const MathVector& other) const; - MathVector operator-(const MathVector& other) const { - if (this->size()!= other._size) { - throw std::invalid_argument("Vectors should have same size for subtraction"); - } - MathVector result(this->_size); - for (size_t i = 0; i < this->_size; ++i) { - result[i] = this->data(i) - other._data[i]; - result._states[i] = busy; - } - return result; - } + MathVector operator-(const MathVector& other) const; template - MathVector> operator-(const MathVector& other) const { - using R = std::common_type_t; // std::common_type_t - if (this->size()!= other.size()) { - throw std::invalid_argument("Vectors should have same size for subtraction"); - } - MathVector result(this->_size); - for (size_t i = 0; i < this->_size; ++i) { - result[i] = static_cast(this->data(i)) - static_cast(other.data(i)); - } - return result; - } + MathVector> operator-(const MathVector& other) const; - MathVector operator*(const T& scalar) const { - MathVector result(this->_size); - for (size_t i = 0; i < this->_size; ++i) { - result[i] = this->data(i) * scalar; - result._states[i] = busy; - } - return result; - } + MathVector operator*(const T& scalar) const; template - MathVector> operator*(const U& scalar) const { - using R = std::common_type_t; - MathVector result(this->_size); - for (size_t i = 0; i < this->_size; ++i) { - result[i] = static_cast(this->data(i)) * static_cast(scalar); - } - return result; + MathVector> operator*(const U& scalar) const; + T operator*(const MathVector& other_vec) const; + template + std::common_type_t operator*(const MathVector& other) const; + + MathVector& operator+=(const MathVector& other); + MathVector& operator-=(const MathVector& other); + MathVector& operator*=(const T& scalar); + + auto length() const; +}; + +template +MathVector MathVector::operator+(const MathVector& other) const { + if (this->size() != other._size) { + throw std::invalid_argument("Vectors should have same size for addition"); } - T operator*(const MathVector& other_vec) const { - if (this->size()!= other_vec._size) { - throw std::invalid_argument("Vectors should have same size for dot product"); - } - T result = 0; - for (size_t i = 0; i < this->_size; ++i) { - result += this->data(i) * other_vec.data(i); - } - return result; + MathVector result(this->_size); + for (size_t i = 0; i < this->_size; ++i) { + result[i] = this->_data[i] + other._data[i]; + result._states[i] = busy; } - template - std::common_type_t operator*(const MathVector& other) const { - using R = std::common_type_t; - if (this->size()!= other.size()) { - throw std::invalid_argument("Vectors should have same size for dot product"); - } - R result = 0; - for (size_t i = 0; i < this->_size; ++i) { - result += static_cast(this->data(i)) * static_cast(other.data(i)); - } - return result; + return result; +} + +template +template +MathVector> MathVector::operator+(const MathVector& other) const { + using R = std::common_type_t; // std::common_type_t + if (this->size() != other.size()) { + throw std::invalid_argument("Vectors should have same size for addition"); + } + MathVector result(this->_size); + for (size_t i = 0; i < this->_size; ++i) { + result[i] = static_cast(this->data(i)) + static_cast(other.data(i)); } + return result; +} - MathVector& operator+=(const MathVector& other) { - if (this->size()!= other._size) { - throw std::invalid_argument("Vectors should have same size for addition"); - } - for (size_t i = 0; i < this->_size; ++i) { - this->_data[i] = this->data(i) + other._data[i]; - } - return *this; +template +MathVector MathVector::operator-(const MathVector& other) const { + if (this->size() != other._size) { + throw std::invalid_argument("Vectors should have same size for subtraction"); } - MathVector& operator-=(const MathVector& other) { - if (this->size()!= other._size) { - throw std::invalid_argument("Vectors should have same size for subtraction"); - } - for (size_t i = 0; i < this->_size; ++i) { - this->_data[i] = this->data(i) - other._data[i]; - } - return *this; + MathVector result(this->_size); + for (size_t i = 0; i < this->_size; ++i) { + result[i] = this->data(i) - other._data[i]; + result._states[i] = busy; } - MathVector& operator*=(const T& scalar) { - for (size_t i = 0; i < this->_size; ++i) { - this->_data[i] = this->data(i) * scalar; - } - return *this; + return result; +} + +template +template +MathVector> MathVector::operator-(const MathVector& other) const { + using R = std::common_type_t; // std::common_type_t + if (this->size() != other.size()) { + throw std::invalid_argument("Vectors should have same size for subtraction"); + } + MathVector result(this->_size); + for (size_t i = 0; i < this->_size; ++i) { + result[i] = static_cast(this->data(i)) - static_cast(other.data(i)); } + return result; +} - auto length() const { // - T sum = 0; - for (size_t i = 0; i < this->_size; i++) { - sum += this->data(i) * this->data(i); - } - return std::sqrt(sum); +template +MathVector MathVector::operator*(const T& scalar) const { + MathVector result(this->_size); + for (size_t i = 0; i < this->_size; ++i) { + result[i] = this->data(i) * scalar; + result._states[i] = busy; } -}; + return result; +} + +template +template +MathVector> MathVector::operator*(const U& scalar) const { + using R = std::common_type_t; + MathVector result(this->_size); + for (size_t i = 0; i < this->_size; ++i) { + result[i] = static_cast(this->data(i)) * static_cast(scalar); + } + return result; +} + +template +T MathVector::operator*(const MathVector& other_vec) const { + if (this->size() != other_vec._size) { + throw std::invalid_argument("Vectors should have same size for dot product"); + } + T result = 0; + for (size_t i = 0; i < this->_size; ++i) { + result += this->data(i) * other_vec.data(i); + } + return result; +} + +template +template +std::common_type_t MathVector::operator*(const MathVector& other) const { + using R = std::common_type_t; + if (this->size() != other.size()) { + throw std::invalid_argument("Vectors should have same size for dot product"); + } + R result = 0; + for (size_t i = 0; i < this->_size; ++i) { + result += static_cast(this->data(i)) * static_cast(other.data(i)); + } + return result; +} + +template +MathVector& MathVector::operator+=(const MathVector& other) { + if (this->size() != other._size) { + throw std::invalid_argument("Vectors should have same size for addition"); + } + for (size_t i = 0; i < this->_size; ++i) { + this->_data[i] = this->data(i) + other._data[i]; + } + return *this; +} + +template +MathVector& MathVector::operator-=(const MathVector& other) { + if (this->size() != other._size) { + throw std::invalid_argument("Vectors should have same size for subtraction"); + } + for (size_t i = 0; i < this->_size; ++i) { + this->_data[i] = this->data(i) - other._data[i]; + } + return *this; +} + +template +MathVector& MathVector::operator*=(const T& scalar) { + for (size_t i = 0; i < this->_size; ++i) { + this->_data[i] = this->data(i) * scalar; + } + return *this; +} + +template +auto MathVector::length() const { // + T sum = 0; + for (size_t i = 0; i < this->_size; i++) { + sum += this->data(i) * this->data(i); + } + return std::sqrt(sum); +} #endif // LIB_MATHVECTOR_MATHVECTOR_H \ No newline at end of file diff --git a/lib_matrix/matrix.h b/lib_matrix/matrix.h index 5362855e..48c0149a 100644 --- a/lib_matrix/matrix.h +++ b/lib_matrix/matrix.h @@ -16,19 +16,19 @@ class Matrix : public MathVector> { size_t _rows; // size_t _cols; // public: - using MathVector>::MathVector; // + using MathVector>::MathVector; Matrix() : MathVector>(), _rows(0), _cols(0) {} Matrix(size_t value_rows, size_t value_cols) : MathVector>(value_rows), _rows(value_rows), _cols(value_cols) { for (size_t i = 0; i < value_rows; ++i) { this->_data[i] = MathVector(value_cols); this->_states[i] = busy; for (size_t j = 0; j < value_cols; ++j) { - this->_data[i][j] = T{}; // {} 0 + this->_data[i][j] = T{}; } } } Matrix(const std::initializer_list> list) { - if (list.size() == 0) { // , list.begin() == list.end() + if (list.size() == 0) { _rows = 0; _cols = 0; this->_size = 0; @@ -53,7 +53,7 @@ class Matrix : public MathVector> { this->_states = new State[this->_capacity]; size_t i = 0; - for (const auto& row : list) { //auto list + for (const auto& row : list) { if (row.size() != _cols) { throw std::invalid_argument("All rows must have the same size"); } @@ -89,7 +89,6 @@ class Matrix : public MathVector> { } MathVector& operator[](size_t row) { return this->_data[row]; } const MathVector& operator[](size_t row) const { return this->_data[row]; } - // [] TVector Matrix& operator=(const Matrix& other) { if (this != &other) { @@ -118,12 +117,13 @@ class Matrix : public MathVector> { if (_cols != other._cols) { throw std::invalid_argument("Cols should have same size for addition"); } - Matrix result(_rows, _cols); - for (size_t i = 0; i < _rows; ++i) { + Matrix result(*this); + result.MathVector>::operator+=(other); + /*for (size_t i = 0; i < _rows; ++i) { for (size_t j = 0; j < _cols; ++j) { result[i][j] = this->at(i, j) + other.at(i, j); } - } + }*/ return result; } Matrix operator-(const Matrix& other) const { diff --git a/lib_triangle_matrix/triangle_matrix.h b/lib_triangle_matrix/triangle_matrix.h index b6adb102..1654d55c 100644 --- a/lib_triangle_matrix/triangle_matrix.h +++ b/lib_triangle_matrix/triangle_matrix.h @@ -83,12 +83,13 @@ class TriangleMatrix : public Matrix { if (_n != other._n) { throw std::invalid_argument("Triangle Matrix should have the same size for addition"); } - TriangleMatrix result(_n); - for (size_t i = 0; i < _n; ++i) { + TriangleMatrix result(*this); + result.Matrix::operator+=(other); + /*for (size_t i = 0; i < _n; ++i) { for (size_t j = i; j < _n; ++j) { result.at(i, j) = this->at(i, j) + other.at(i, j); } - } + }*/ return result; } TriangleMatrix operator-(const TriangleMatrix& other) const { diff --git a/lib_tvector/tvector.h b/lib_tvector/tvector.h index 30485f9d..286894f6 100644 --- a/lib_tvector/tvector.h +++ b/lib_tvector/tvector.h @@ -158,6 +158,7 @@ class TVector { return _ptr[_index]; } }; + Iterator begin() { size_t first_busy = 0; while (first_busy < _size && _states[first_busy] != State::busy) { diff --git a/tests/test_algorithms.cpp b/tests/test_algorithms.cpp index fd89abcd..020ed313 100644 --- a/tests/test_algorithms.cpp +++ b/tests/test_algorithms.cpp @@ -4,6 +4,8 @@ #include "../lib_tvector/tvector.h" #include "../lib_mathvector/MathVector.h" #include "../lib_matrix/matrix.h" +#include "../lib_node/node.h" +#include "../lib_list/list.h" #include "../lib_algorithms/algorithms.h" #define EPSILON 0.000001 @@ -118,4 +120,198 @@ TEST(TestAlgorithmsLib, real_example2_with_text) { // Act & Assert EXPECT_FALSE(check_brackets(str)); +} + +TEST(TestAlgorithmsLib, is_looped_hare_and_turtle_on_empty_list) { + // Arrange + List list; + + // Act & Assert + EXPECT_FALSE(is_looped_hare_and_turtle(list)); +} + +TEST(TestAlgorithmsLib, is_looped_hare_and_turtle_on_list_with_one_elem_no_loop) { + // Arrange + List list; + list.push_back(7654); + + // Act & Assert + EXPECT_FALSE(is_looped_hare_and_turtle(list)); +} + +TEST(TestAlgorithmsLib, is_looped_hare_and_turtle_on_list_with_one_elem_with_loop) { + // Arrange + List list; + list.push_back(8765); + list.head()->next = list.head(); + + // Act & Assert + EXPECT_TRUE(is_looped_hare_and_turtle(list)); + list.head()->next = nullptr; +} + +TEST(TestAlgorithmsLib, is_looped_hare_and_turtle_on_large_list_with_no_loop) { + // Arrange + List list; + for (int i = 0; i < 100; ++i) { + list.push_back(i); + } + + // Act & Assert + EXPECT_FALSE(is_looped_hare_and_turtle(list)); +} + +TEST(TestAlgorithmsLib, is_looped_hare_and_turtle_on_large_list_with_loop_in_middle) { + // Arrange + List list; + for (int i = 0; i < 100; ++i) { + list.push_back(i); + } + Node* middle = list.head(); + for (int i = 0; i < 50; ++i) { + middle = middle->next; + } + list.tail()->next = middle; + + // Act & Assert + EXPECT_TRUE(is_looped_hare_and_turtle(list)); + list.tail()->next = nullptr; +} + +TEST(TestAlgorithmsLib, is_looped_turn_ptr_on_empty_list) { + // Arrange + List list; + + // Act & Assert + EXPECT_FALSE(is_looped_turn_ptr(list)); +} + +TEST(TestAlgorithmsLib, is_looped_turn_ptr_on_list_with_one_elem_no_loop) { + // Arrange + List list; + list.push_back(7654); + + // Act & Assert + EXPECT_FALSE(is_looped_turn_ptr(list)); +} + +TEST(TestAlgorithmsLib, is_looped_turn_ptr_on_list_with_one_elem_with_loop) { + // Arrange + List list; + list.push_back(8765); + list.head()->next = list.head(); + + // Act & Assert + EXPECT_TRUE(is_looped_turn_ptr(list)); + list.head()->next = nullptr; +} + +TEST(TestAlgorithmsLib, is_looped_turn_ptr_on_large_list_with_no_loop) { + // Arrange + List list; + for (int i = 0; i < 100; ++i) { + list.push_back(i); + } + + // Act & Assert + EXPECT_FALSE(is_looped_turn_ptr(list)); +} + +TEST(TestAlgorithmsLib, is_looped_turn_ptr_on_large_list_with_loop_in_middle) { + // Arrange + List list; + for (int i = 0; i < 100; ++i) { + list.push_back(i); + } + Node* middle = list.head(); + for (int i = 0; i < 50; ++i) { + middle = middle->next; + } + list.tail()->next = middle; + + // Act & Assert + EXPECT_TRUE(is_looped_turn_ptr(list)); + list.tail()->next = nullptr; +} + +TEST(TestAlgorithmsLib, find_loop_node_in_empty_list) { + // Arrange + List list; + + // Act & Assert + EXPECT_EQ(nullptr, find_loop_node(list)); +} + +TEST(TestAlgorithmsLib, find_loop_node_no_loop) { + // Arrange + List list; + for (int i = 1; i < 5; ++i) { + list.push_back(i); + } + + // Act & Assert + EXPECT_EQ(nullptr, find_loop_node(list)); +} + +TEST(TestAlgorithmsLib, find_loop_node_with_loop_in_list_with_one_elem) { + // Arrange + List list; + list.push_back(1); + list.head()->next = list.head(); + + // Act & Assert + EXPECT_EQ(list.head(), find_loop_node(list)); + list.head()->next = nullptr; +} + +TEST(TestAlgorithmsLib, find_loop_node_with_loop_at_beginning) { + // Arrange + List list; + for (int i = 1; i < 5; ++i) { + list.push_back(i); + } + + // Act + list.tail()->next = list.head(); + + // Act & Assert + EXPECT_EQ(list.head(), find_loop_node(list)); + list.tail()->next = nullptr; +} + +TEST(TestAlgorithmsLib, find_loop_node_with_loop_in_middle) { + // Arrange + List list; + for (int i = 1; i < 6; ++i) { + list.push_back(i); + } + + // Act + Node* third_node = list.head()->next->next; + list.tail()->next = third_node; + + // Act & Assert + EXPECT_EQ(third_node, find_loop_node(list)); + EXPECT_EQ(3, find_loop_node(list)->value); + list.tail()->next = nullptr; +} + +TEST(TestAlgorithmsLib, find_loop_node_with_loop_in_large_list) { + // Arrange + List list; + for (int i = 0; i < 100; ++i) { + list.push_back(i); + } + Node* loop_node = list.head(); + for (int i = 0; i < 75; ++i) { + loop_node = loop_node->next; + } + list.tail()->next = loop_node; + + // Act & Assert + EXPECT_EQ(loop_node, find_loop_node(list)); + EXPECT_EQ(75, find_loop_node(list)->value); + + // + list.tail()->next = nullptr; } \ No newline at end of file diff --git a/tests/test_dlist.cpp b/tests/test_dlist.cpp index d3e80fbb..5fb025b6 100644 --- a/tests/test_dlist.cpp +++ b/tests/test_dlist.cpp @@ -764,6 +764,26 @@ TEST(TestDListLib, iterator_write) { } } +TEST(TestDListLib, back_iterator_write) { + // Arrange + DList list; + for (int i = 0; i < 5; i++) { + list.push_back(0); + } + int i = 1; + + // Act + for (DList::Iterator it = list.rbegin(); it != list.rend(); it--) { + *it = i; + i++; + } + + // Assert + for (DList::Iterator it = list.begin(); it != list.end(); it++) { + EXPECT_EQ(--i, *it); + } +} + TEST(TestDListLib, iterator_in_empty_list) { // Arrange DList list; From 5ae89910871c63bc4b271150bb55808038e9fd9b Mon Sep 17 00:00:00 2001 From: Kate-Nits Date: Thu, 11 Dec 2025 00:04:09 +0300 Subject: [PATCH 92/94] I Fixed everything that Marina Andreevna told me about arithmetic operations in Matix and TriangleMatrix --- lib_mathvector/MathVector.h | 22 +++++++++++----------- lib_matrix/matrix.h | 15 +++------------ lib_triangle_matrix/triangle_matrix.h | 21 +++++++++++++++------ 3 files changed, 29 insertions(+), 29 deletions(-) diff --git a/lib_mathvector/MathVector.h b/lib_mathvector/MathVector.h index 5948d1a1..6ed4ff9a 100644 --- a/lib_mathvector/MathVector.h +++ b/lib_mathvector/MathVector.h @@ -91,7 +91,7 @@ MathVector> MathVector::operator+(const MathVector result(this->_size); for (size_t i = 0; i < this->_size; ++i) { - result[i] = static_cast(this->data(i)) + static_cast(other.data(i)); + result[i] = static_cast(this->_data[i]) + static_cast(other.data(i)); } return result; } @@ -103,7 +103,7 @@ MathVector MathVector::operator-(const MathVector& other) const { } MathVector result(this->_size); for (size_t i = 0; i < this->_size; ++i) { - result[i] = this->data(i) - other._data[i]; + result[i] = this->_data[i] - other._data[i]; result._states[i] = busy; } return result; @@ -118,7 +118,7 @@ MathVector> MathVector::operator-(const MathVector result(this->_size); for (size_t i = 0; i < this->_size; ++i) { - result[i] = static_cast(this->data(i)) - static_cast(other.data(i)); + result[i] = static_cast(this->_data[i]) - static_cast(other.data(i)); } return result; } @@ -127,7 +127,7 @@ template MathVector MathVector::operator*(const T& scalar) const { MathVector result(this->_size); for (size_t i = 0; i < this->_size; ++i) { - result[i] = this->data(i) * scalar; + result._data[i] = this->_data[i] * scalar; result._states[i] = busy; } return result; @@ -139,7 +139,7 @@ MathVector> MathVector::operator*(const U& scalar) c using R = std::common_type_t; MathVector result(this->_size); for (size_t i = 0; i < this->_size; ++i) { - result[i] = static_cast(this->data(i)) * static_cast(scalar); + result[i] = static_cast(this->_data[i]) * static_cast(scalar); } return result; } @@ -151,7 +151,7 @@ T MathVector::operator*(const MathVector& other_vec) const { } T result = 0; for (size_t i = 0; i < this->_size; ++i) { - result += this->data(i) * other_vec.data(i); + result += this->_data[i] * other_vec._data[i]; } return result; } @@ -165,7 +165,7 @@ std::common_type_t MathVector::operator*(const MathVector& other) co } R result = 0; for (size_t i = 0; i < this->_size; ++i) { - result += static_cast(this->data(i)) * static_cast(other.data(i)); + result += static_cast(this->_data[i]) * static_cast(other.data(i)); } return result; } @@ -176,7 +176,7 @@ MathVector& MathVector::operator+=(const MathVector& other) { throw std::invalid_argument("Vectors should have same size for addition"); } for (size_t i = 0; i < this->_size; ++i) { - this->_data[i] = this->data(i) + other._data[i]; + this->_data[i] = this->_data[i] + other._data[i]; } return *this; } @@ -187,7 +187,7 @@ MathVector& MathVector::operator-=(const MathVector& other) { throw std::invalid_argument("Vectors should have same size for subtraction"); } for (size_t i = 0; i < this->_size; ++i) { - this->_data[i] = this->data(i) - other._data[i]; + this->_data[i] = this->_data[i] - other._data[i]; } return *this; } @@ -195,7 +195,7 @@ MathVector& MathVector::operator-=(const MathVector& other) { template MathVector& MathVector::operator*=(const T& scalar) { for (size_t i = 0; i < this->_size; ++i) { - this->_data[i] = this->data(i) * scalar; + this->_data[i] = this->_data[i] * scalar; } return *this; } @@ -204,7 +204,7 @@ template auto MathVector::length() const { // T sum = 0; for (size_t i = 0; i < this->_size; i++) { - sum += this->data(i) * this->data(i); + sum += this->_data[i] * this->_data[i]; } return std::sqrt(sum); } diff --git a/lib_matrix/matrix.h b/lib_matrix/matrix.h index 48c0149a..60ec2b68 100644 --- a/lib_matrix/matrix.h +++ b/lib_matrix/matrix.h @@ -119,11 +119,6 @@ class Matrix : public MathVector> { } Matrix result(*this); result.MathVector>::operator+=(other); - /*for (size_t i = 0; i < _rows; ++i) { - for (size_t j = 0; j < _cols; ++j) { - result[i][j] = this->at(i, j) + other.at(i, j); - } - }*/ return result; } Matrix operator-(const Matrix& other) const { @@ -133,12 +128,8 @@ class Matrix : public MathVector> { if (_cols != other._cols) { throw std::invalid_argument("Cols should have same size for subtraction"); } - Matrix result(_rows, _cols); - for (size_t i = 0; i < _rows; ++i) { - for (size_t j = 0; j < _cols; ++j) { - result[i][j] = this->at(i, j) - other.at(i, j); - } - } + Matrix result(*this); + result.MathVector>::operator-=(other); return result; } Matrix operator*(const T scalar) const { @@ -156,7 +147,7 @@ class Matrix : public MathVector> { } MathVector result(_rows); for (size_t i = 0; i < _rows; ++i) { - result[i] = this->data(i) * vec; + result[i] = this->_data[i] * vec; } return result; } diff --git a/lib_triangle_matrix/triangle_matrix.h b/lib_triangle_matrix/triangle_matrix.h index 1654d55c..27eeefe8 100644 --- a/lib_triangle_matrix/triangle_matrix.h +++ b/lib_triangle_matrix/triangle_matrix.h @@ -96,12 +96,14 @@ class TriangleMatrix : public Matrix { if (_n != other._n) { throw std::invalid_argument("Triangle Matrix should have the same size for subtraction"); } - TriangleMatrix result(_n); + TriangleMatrix result(*this); + result.Matrix::operator-=(other); + /* TriangleMatrix result(_n); for (size_t i = 0; i < _n; ++i) { for (size_t j = i; j < _n; ++j) { result.at(i, j) = this->at(i, j) - other.at(i, j); } - } + }*/ return result; } TriangleMatrix operator*(const T scalar) const { @@ -119,6 +121,13 @@ class TriangleMatrix : public Matrix { } MathVector result(_n); for (size_t i = 0; i < _n; ++i) { + T sum{}; + for (size_t j = i; j < _n; ++j) { + sum += this->at(i, j) * vec[j]; + } + result[i] = sum; + } + /*for (size_t i = 0; i < _n; ++i) { for (size_t j = i; j < _n; ++j) { T sum{}; for (size_t k = i; k <= j; ++k) { @@ -126,7 +135,7 @@ class TriangleMatrix : public Matrix { } result[i] = sum; } - } + }*/ return result; } TriangleMatrix operator*(const TriangleMatrix& other) const { @@ -178,13 +187,13 @@ Matrix operator+(const TriangleMatrix& triangle_matrix, const Matrix& m if (matrix.rows() != triangle_matrix.n() || matrix.cols() != triangle_matrix.n()) { throw std::invalid_argument("Matrixes should have the same size for addition"); } - Matrix result(matrix.rows(), matrix.cols()); + /*Matrix result(matrix.rows(), matrix.cols()); for (size_t i = 0; i < matrix.rows(); ++i) { for (size_t j = 0; j < matrix.cols(); ++j) { result[i][j] = matrix.at(i, j) + triangle_matrix.at(i, j); } - } - return result; + }*/ + return matrix + triangle_matrix; } template Matrix operator-(const Matrix& matrix, const TriangleMatrix& triangle_matrix) { From 3a7057cefbbca0362da0a90a836472585f0557f5 Mon Sep 17 00:00:00 2001 From: Kate-Nits Date: Fri, 12 Dec 2025 01:57:04 +0300 Subject: [PATCH 93/94] Created lib_stack_list and lib_queue_list. Wrote a StackList class and added all the tests for it --- CMakeLists.txt | 2 + lib_list/list.h | 2 +- lib_queue_list/CMakeLists.txt | 3 + lib_queue_list/queue_list.cpp | 0 lib_queue_list/queue_list.h | 0 lib_stack_list/CMakeLists.txt | 3 + lib_stack_list/stack_list.cpp | 0 lib_stack_list/stack_list.h | 65 ++++++++++++ tests/test_queue_list.cpp | 0 tests/test_stack_list.cpp | 182 ++++++++++++++++++++++++++++++++++ 10 files changed, 256 insertions(+), 1 deletion(-) create mode 100644 lib_queue_list/CMakeLists.txt create mode 100644 lib_queue_list/queue_list.cpp create mode 100644 lib_queue_list/queue_list.h create mode 100644 lib_stack_list/CMakeLists.txt create mode 100644 lib_stack_list/stack_list.cpp create mode 100644 lib_stack_list/stack_list.h create mode 100644 tests/test_queue_list.cpp create mode 100644 tests/test_stack_list.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 82fe00f3..d21a4742 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -28,6 +28,8 @@ add_subdirectory(lib_queue) add_subdirectory(lib_node) add_subdirectory(lib_list) add_subdirectory(lib_dlist) +add_subdirectory(lib_stack_list) +add_subdirectory(lib_queue_list) add_subdirectory(main) # подключаем дополнительный CMakeLists.txt из подкаталога с именем main option(BTEST "build test?" ON) # указываем подключаем ли google-тесты (ON или YES) или нет (OFF или NO) diff --git a/lib_list/list.h b/lib_list/list.h index b106bd3f..c4ae8886 100644 --- a/lib_list/list.h +++ b/lib_list/list.h @@ -83,8 +83,8 @@ class List { void erase(size_t pos); void erase(Node* node); List& operator=(const List& other); -private: void clear(); +private: bool is_node_in_list(Node* node) const; }; diff --git a/lib_queue_list/CMakeLists.txt b/lib_queue_list/CMakeLists.txt new file mode 100644 index 00000000..c4511154 --- /dev/null +++ b/lib_queue_list/CMakeLists.txt @@ -0,0 +1,3 @@ +create_project_lib(QueueList) +add_depend(QueueList Node lib_node) +add_depend(QueueList List lib_list) \ No newline at end of file diff --git a/lib_queue_list/queue_list.cpp b/lib_queue_list/queue_list.cpp new file mode 100644 index 00000000..e69de29b diff --git a/lib_queue_list/queue_list.h b/lib_queue_list/queue_list.h new file mode 100644 index 00000000..e69de29b diff --git a/lib_stack_list/CMakeLists.txt b/lib_stack_list/CMakeLists.txt new file mode 100644 index 00000000..2241158b --- /dev/null +++ b/lib_stack_list/CMakeLists.txt @@ -0,0 +1,3 @@ +create_project_lib(StackList) +add_depend(StackList Node lib_node) +add_depend(StackList List lib_list) \ No newline at end of file diff --git a/lib_stack_list/stack_list.cpp b/lib_stack_list/stack_list.cpp new file mode 100644 index 00000000..e69de29b diff --git a/lib_stack_list/stack_list.h b/lib_stack_list/stack_list.h new file mode 100644 index 00000000..7bcbf928 --- /dev/null +++ b/lib_stack_list/stack_list.h @@ -0,0 +1,65 @@ +// Copyright 2025 Ekaterina Ushnitskaya + +#ifndef LIB_STACK_LIST_STACK_LIST_H +#define LIB_STACK_LIST_STACK_LIST_H + +#include +#include +#include "../lib_list/list.h" + +template +class StackList { + List _list; + size_t _count; + size_t _max_capacity; +public: + static const size_t RESERVE_MEMORY = 20; + StackList() : _list(), _count(0), _max_capacity(RESERVE_MEMORY) {} + explicit StackList(size_t size) : _list(), _count(0), _max_capacity(size) {} + StackList(const StackList& other) : _list(other._list), _count(other._count), _max_capacity(other._max_capacity) {} + + inline size_t count() const noexcept { + return _count; + } + inline size_t capacity() const noexcept { + return _max_capacity; + } + bool is_full() const noexcept { + return _count >= _max_capacity; + } + bool is_empty() const noexcept { + return _count == 0; + } + void clear() noexcept { + _list.clear(); + _count = 0; + } + + void push(const T& value) { + if (is_full()) { + throw std::overflow_error("Stack overflow: cannot push, because stack is full"); + } + _list.push_front(value); + _count++; + } + void pop() { + if (is_empty()) { + throw std::underflow_error("Stack is empty"); + } + _list.pop_front(); + _count--; + } + T& top() { + if (is_empty()) { + throw std::underflow_error("Stack is empty"); + } + return _list.head()->value; + } + const T& top() const { + if (is_empty()) { + throw std::underflow_error("Stack is empty"); + } + return _list.head()->value; + } +}; +#endif // LIB_STACK_LIST_STACK_LIST_H \ No newline at end of file diff --git a/tests/test_queue_list.cpp b/tests/test_queue_list.cpp new file mode 100644 index 00000000..e69de29b diff --git a/tests/test_stack_list.cpp b/tests/test_stack_list.cpp new file mode 100644 index 00000000..d3b61511 --- /dev/null +++ b/tests/test_stack_list.cpp @@ -0,0 +1,182 @@ +// Copyright 2025 Ekaterina Ushnitskaya + +#include +#include "../lib_stack_list/stack_list.h" + +#define EPSILON 0.000001 +#define TRUE 1 +#define FALSE 0 + +TEST(TestStackListLib, default_constructor_creates_empty_stack) { + // Arrange + StackList stack; + + // Act & Assert + EXPECT_EQ(0, stack.count()); + EXPECT_EQ(StackList::RESERVE_MEMORY, stack.capacity()); + EXPECT_TRUE(stack.is_empty()); + EXPECT_FALSE(stack.is_full()); +} + +TEST(TestStackListLib, constructor_with_capacity) { + // Arrange + size_t capacity = 10; + StackList stack(capacity); + + // Act & Assert + EXPECT_EQ(0, stack.count()); + EXPECT_EQ(capacity, stack.capacity()); + EXPECT_TRUE(stack.is_empty()); + EXPECT_FALSE(stack.is_full()); +} + +TEST(TestStackListLib, copy_constructor) { + // Arrange + size_t capacity = 10; + StackList stack1(capacity); + stack1.push(10); + stack1.push(20); + + // Act + StackList stack2(stack1); + + // Assert + EXPECT_EQ(stack1.count(), stack2.count()); + EXPECT_EQ(stack1.capacity(), stack2.capacity()); + EXPECT_EQ(stack1.is_empty(), stack2.is_empty()); + EXPECT_EQ(stack1.is_full(), stack2.is_full()); +} + +TEST(TestStackListLib, test_push_and_top_without_an_overflowing_stack) { + // Arrange + StackList stack; + + // Act + stack.push(10); + stack.push(20); + + // Assert + EXPECT_FALSE(stack.is_empty()); + EXPECT_EQ(2, stack.count()); + EXPECT_EQ(20, stack.top()); +} + +TEST(TestStackListLib, test_push_with_an_overflowing_stack) { + // Arrange + StackList stack(2); + + // Act + stack.push(10); + stack.push(20); + + // Assert + EXPECT_TRUE(stack.is_full()); + EXPECT_ANY_THROW(stack.push(30)); +} + +TEST(TestStackListLib, test_top_with_empty_stack) { + // Arrange + StackList stack; + + // Act & Assert + EXPECT_TRUE(stack.is_empty()); + EXPECT_ANY_THROW(stack.top()); +} + +TEST(TestStackListLib, test_pop_not_all_elements_in_stack_and_top_without_an_underflowing_stack) { + // Arrange + StackList stack; + stack.push(1); + stack.push(2); + stack.push(3); + + // Act + stack.pop(); + + // Assert + EXPECT_FALSE(stack.is_empty()); + EXPECT_EQ(2, stack.count()); + EXPECT_EQ(2, stack.top()); +} + +TEST(TestStackListLib, test_pop_all_elements_in_stack_and_top_without_an_underflowing_stack) { + // Arrange + StackList stack; + stack.push(1); + + // Act + stack.pop(); + + // Assert + EXPECT_TRUE(stack.is_empty()); + EXPECT_EQ(0, stack.count()); + EXPECT_ANY_THROW(stack.top()); +} + +TEST(TestStackListLib, test_pop_with_an_underflowing_stack) { + // Arrange + StackList stack; + + // Act & Assert + EXPECT_TRUE(stack.is_empty()); + EXPECT_ANY_THROW(stack.pop()); +} + +TEST(TestStackListLib, test_clear) { + // Arrange + StackList stack; + stack.push(1); + stack.push(2); + stack.push(3); + + // Act + stack.clear(); + + // Assert + EXPECT_TRUE(stack.is_empty()); + EXPECT_EQ(0, stack.count()); + EXPECT_ANY_THROW(stack.top()); + EXPECT_NO_THROW(stack.push(10)); +} + +TEST(TestStackListLib, test_is_full_checking_for_a_full_stack) { + // Arrange + StackList stack(3); + stack.push(1); + stack.push(2); + stack.push(3); + + // Act & Assert + EXPECT_TRUE(stack.is_full()); + EXPECT_ANY_THROW(stack.push(4)); +} + +TEST(TestStackListLib, test_is_full_checking_for_an_empty_stack) { + // Arrange + StackList stack(3); + + // Act & Assert + EXPECT_FALSE(stack.is_full()); + EXPECT_ANY_THROW(stack.pop()); +} + +TEST(TestStackListLib, test_is_empty_checking_for_a_full_stack) { + // Arrange + StackList stack(3); + stack.push(1); + stack.push(2); + stack.push(3); + + // Act & Assert + EXPECT_FALSE(stack.is_empty()); + EXPECT_ANY_THROW(stack.push(4)); +} + +TEST(TestStackListLib, test_is_empty_checking_for_an_empty_stack) { + // Arrange + StackList stack(3); + + // Act & Assert + EXPECT_TRUE(stack.is_empty()); + EXPECT_ANY_THROW(stack.pop()); +} \ No newline at end of file From 691568de1117808a4d223b896f1b3c29b315a384 Mon Sep 17 00:00:00 2001 From: Kate-Nits Date: Fri, 12 Dec 2025 02:59:54 +0300 Subject: [PATCH 94/94] Wrote class QueueList and added all tests for it --- lib_queue/queue.h | 4 +- lib_queue_list/queue_list.h | 78 +++++++++++++ tests/test_queue.cpp | 18 +-- tests/test_queue_list.cpp | 221 ++++++++++++++++++++++++++++++++++++ 4 files changed, 310 insertions(+), 11 deletions(-) diff --git a/lib_queue/queue.h b/lib_queue/queue.h index f8f12589..590a81c8 100644 --- a/lib_queue/queue.h +++ b/lib_queue/queue.h @@ -30,7 +30,7 @@ class Queue { T& head(); const T& head() const; inline size_t capacity() const noexcept; - inline size_t size() const noexcept; + inline size_t count() const noexcept; bool is_empty() const noexcept; bool is_full() const noexcept; void clear() noexcept; @@ -87,7 +87,7 @@ inline size_t Queue::capacity() const noexcept { return _size; } template -inline size_t Queue::size() const noexcept { +inline size_t Queue::count() const noexcept { return _count; } template diff --git a/lib_queue_list/queue_list.h b/lib_queue_list/queue_list.h index e69de29b..1f25dad2 100644 --- a/lib_queue_list/queue_list.h +++ b/lib_queue_list/queue_list.h @@ -0,0 +1,78 @@ +// Copyright 2025 Ekaterina Ushnitskaya + +#ifndef LIB_QUEUE_LIST_QUEUE_LIST_H +#define LIB_QUEUE_LIST_QUEUE_LIST_H + +#include +#include +#include "../lib_list/list.h" + +template +class QueueList { + List _list; + size_t _capacity; + size_t _count; +public: + static const size_t RESERVE_MEMORY = 20; + QueueList() : _list(), _capacity(RESERVE_MEMORY), _count(0) {} + explicit QueueList(size_t size) : _list(), _capacity(size), _count(0) {} + QueueList(const QueueList& other) : _list(other._list), _capacity(other._capacity), _count(other._count) {} + + inline T& tail() { + if (is_empty()) { + throw std::underflow_error("Queue is empty (no tail element)"); + } + return _list.tail()->value; + } + inline const T& tail() const { + if (is_empty()) { + throw std::underflow_error("Queue is empty (no tail element)"); + } + return _list.tail()->value; + } + inline T& head() { + if (is_empty()) { + throw std::underflow_error("Queue is empty (no head element)"); + } + return _list.head()->value; + } + inline const T& head() const { + if (is_empty()) { + throw std::underflow_error("Queue is empty (no head element)"); + } + return _list.head()->value; + } + inline size_t capacity() const noexcept { + return _capacity; + } + inline size_t count() const noexcept { + return _count; + } + bool is_empty() const noexcept { + return _count == 0; + } + bool is_full() const noexcept { + return _count >= _capacity; + } + void clear() { + _list.clear(); + _count = 0; + } + + void push(const T& value) { + if (is_full()) { + throw std::overflow_error("Queue is full (can't push element)"); + } + _list.push_back(value); + _count++; + } + void pop() { + if (is_empty()) { + throw std::underflow_error("Queue is empty (can't pop element)"); + } + _list.pop_front(); + _count--; + } +}; + +#endif // LIB_QUEUE_LIST_QUEUE_LIST_H \ No newline at end of file diff --git a/tests/test_queue.cpp b/tests/test_queue.cpp index 59c60233..e28d2ee7 100644 --- a/tests/test_queue.cpp +++ b/tests/test_queue.cpp @@ -14,7 +14,7 @@ TEST(TestQueueLib, default_constructor_creates_empty_queue) { // Act & Assert EXPECT_TRUE(queue.is_empty()); EXPECT_FALSE(queue.is_full()); - EXPECT_EQ(0, queue.size()); + EXPECT_EQ(0, queue.count()); EXPECT_EQ(Queue::RESERVE_MEMORY, queue.capacity()); } @@ -38,7 +38,7 @@ TEST(TestQueueLib, copy_constructor_without_difficulties) { Queue queue2(queue1); // Assert - EXPECT_EQ(queue1.size(), queue2.size()); + EXPECT_EQ(queue1.count(), queue2.count()); EXPECT_EQ(queue1.is_empty(), queue2.is_empty()); EXPECT_EQ(queue1.is_full(), queue2.is_full()); } @@ -56,7 +56,7 @@ TEST(TestQueueLib, copy_constructor_with_overflow_head_and_tail) { Queue queue2(queue1); // Assert - EXPECT_EQ(queue1.size(), queue2.size()); + EXPECT_EQ(queue1.count(), queue2.count()); EXPECT_EQ(queue1.head(), queue2.head()); EXPECT_EQ(queue1.tail(), queue2.tail()); } @@ -71,7 +71,7 @@ TEST(TestQueueLib, test_push_and_check_head_and_tail_without_an_overflowing_queu queue.push(30); // Assert - EXPECT_EQ(3, queue.size()); + EXPECT_EQ(3, queue.count()); EXPECT_EQ(10, queue.head()); EXPECT_EQ(30, queue.tail()); EXPECT_FALSE(queue.is_empty()); @@ -102,7 +102,7 @@ TEST(TestQueueLib, test_pop_without_an_underflowing_queue) { // Assert EXPECT_FALSE(queue.is_empty()); - EXPECT_EQ(2, queue.size()); + EXPECT_EQ(2, queue.count()); EXPECT_EQ(2, queue.head()); EXPECT_EQ(3, queue.tail()); } @@ -117,7 +117,7 @@ TEST(TestQueueLib, test_pop_all_elements_in_queue_without_an_underflowing_stack) // Assert EXPECT_TRUE(queue.is_empty()); - EXPECT_EQ(0, queue.size()); + EXPECT_EQ(0, queue.count()); } TEST(TestQueueLib, test_pop_with_an_underflowing_queue) { @@ -140,7 +140,7 @@ TEST(TestQueueLib, test_clear) { // Assert EXPECT_TRUE(queue.is_empty()); - EXPECT_EQ(0, queue.size()); + EXPECT_EQ(0, queue.count()); } TEST(TestQueueLib, test_is_full_checking_for_a_full_queue) { @@ -198,7 +198,7 @@ TEST(TestQueueLib, test_push_after_overflow_tail) { queue.push(200); // Assert - EXPECT_EQ(2, queue.size()); + EXPECT_EQ(2, queue.count()); EXPECT_EQ(100, queue.head()); EXPECT_EQ(200, queue.tail()); } @@ -215,7 +215,7 @@ TEST(TestQueueLib, test_push_after_overflow_head) { queue.push(555); // Assert - EXPECT_EQ(3, queue.size()); + EXPECT_EQ(3, queue.count()); EXPECT_EQ(2, queue.head()); EXPECT_EQ(555, queue.tail()); } \ No newline at end of file diff --git a/tests/test_queue_list.cpp b/tests/test_queue_list.cpp index e69de29b..6ef0bc32 100644 --- a/tests/test_queue_list.cpp +++ b/tests/test_queue_list.cpp @@ -0,0 +1,221 @@ +// Copyright 2025 Ekaterina Ushnitskaya + +#include +#include "../lib_queue_list/queue_list.h" + +#define EPSILON 0.000001 +#define TRUE 1 +#define FALSE 0 + +TEST(TestQueueListLib, default_constructor_creates_empty_queue) { + // Arrange + QueueList queue; + + // Act & Assert + EXPECT_TRUE(queue.is_empty()); + EXPECT_FALSE(queue.is_full()); + EXPECT_EQ(0, queue.count()); + EXPECT_EQ(QueueList::RESERVE_MEMORY, queue.capacity()); +} + +TEST(TestQueueListLib, constructor_with_size) { + // Arrange + size_t size = 10; + QueueList queue(size); + + // Act & Assert + EXPECT_EQ(size, queue.capacity()); + EXPECT_TRUE(queue.is_empty()); + EXPECT_FALSE(queue.is_full()); +} + +TEST(TestQueueListLib, copy_constructor_without_difficulties) { + // Arrange + size_t size = 10; + QueueList queue1(size); + + // Act + QueueList queue2(queue1); + + // Assert + EXPECT_EQ(queue1.count(), queue2.count()); + EXPECT_EQ(queue1.is_empty(), queue2.is_empty()); + EXPECT_EQ(queue1.is_full(), queue2.is_full()); +} + +TEST(TestQueueListLib, copy_constructor_with_overflow_head_and_tail) { + // Arrange + QueueList queue1(3); + + // Act + queue1.push(1); + queue1.push(2); + queue1.push(3); + queue1.pop(); + queue1.push(4); + QueueList queue2(queue1); + + // Assert + EXPECT_EQ(queue1.count(), queue2.count()); + EXPECT_EQ(queue1.head(), queue2.head()); + EXPECT_EQ(queue1.tail(), queue2.tail()); +} + +TEST(TestQueueListLib, test_push_and_check_head_and_tail_without_an_overflowing_queue) { + // Arrange + QueueList queue; + + // Act + queue.push(10); + queue.push(20); + queue.push(30); + + // Assert + EXPECT_EQ(3, queue.count()); + EXPECT_EQ(10, queue.head()); + EXPECT_EQ(30, queue.tail()); + EXPECT_FALSE(queue.is_empty()); +} + +TEST(TestQueueListLib, test_push_and_check_head_and_tail_with_an_overflowing_queue) { + // Arrange + QueueList queue(3); + + // Act + queue.push(10); + queue.push(20); + queue.push(30); + + // Assert + EXPECT_ANY_THROW(queue.push(40)); +} + +TEST(TestQueueListLib, test_pop_without_an_underflowing_queue) { + // Arrange + QueueList queue; + queue.push(1); + queue.push(2); + queue.push(3); + + // Act + queue.pop(); + + // Assert + EXPECT_FALSE(queue.is_empty()); + EXPECT_EQ(2, queue.count()); + EXPECT_EQ(2, queue.head()); + EXPECT_EQ(3, queue.tail()); +} + +TEST(TestQueueListLib, test_pop_all_elements_in_queue_without_an_underflowing_stack) { + // Arrange + QueueList queue; + queue.push(1); + + // Act + queue.pop(); + + // Assert + EXPECT_TRUE(queue.is_empty()); + EXPECT_EQ(0, queue.count()); +} + +TEST(TestQueueListLib, test_pop_with_an_underflowing_queue) { + // Arrange + QueueList queue; + + // Act & Assert + EXPECT_TRUE(queue.is_empty()); + EXPECT_ANY_THROW(queue.pop()); +} + +TEST(TestQueueListLib, test_clear) { + // Arrange + QueueList queue; + queue.push(1); + queue.push(2); + + // Act + queue.clear(); + + // Assert + EXPECT_TRUE(queue.is_empty()); + EXPECT_EQ(0, queue.count()); +} + +TEST(TestQueueListLib, test_is_full_checking_for_a_full_queue) { + // Arrange + QueueList queue(3); + queue.push(1); + queue.push(2); + queue.push(3); + + // Act & Assert + EXPECT_TRUE(queue.is_full()); + EXPECT_ANY_THROW(queue.push(4)); +} + +TEST(TestQueueListLib, test_is_full_checking_for_an_empty_queue) { + // Arrange + QueueList queue(3); + + // Act & Assert + EXPECT_FALSE(queue.is_full()); + EXPECT_ANY_THROW(queue.pop()); +} + +TEST(TestQueueListLib, test_is_empty_checking_for_a_full_queue) { + // Arrange + QueueList queue(3); + queue.push(1); + queue.push(2); + queue.push(3); + + // Act & Assert + EXPECT_FALSE(queue.is_empty()); + EXPECT_ANY_THROW(queue.push(4)); +} + +TEST(TestQueueListLib, test_is_empty_checking_for_an_empty_queue) { + // Arrange + QueueList queue(3); + + // Act & Assert + EXPECT_TRUE(queue.is_empty()); + EXPECT_ANY_THROW(queue.pop()); +} + +TEST(TestQueueListLib, test_push_after_overflow_tail) { + // Arrange + QueueList queue(3); + + // Act + queue.push(1); + queue.push(2); + queue.pop(); + queue.pop(); + queue.push(100); + queue.push(200); + + // Assert + EXPECT_EQ(2, queue.count()); + EXPECT_EQ(100, queue.head()); + EXPECT_EQ(200, queue.tail()); +} + +TEST(TestQueueListLib, test_push_after_overflow_head) { + // Arrange + QueueList queue(3); + + // Act + queue.push(1); + queue.push(2); + queue.push(3); + queue.pop(); + queue.push(555); + + // Assert + EXPECT_EQ(3, queue.count()); + EXPECT_EQ(2, queue.head()); + EXPECT_EQ(555, queue.tail()); +} \ No newline at end of file