From 89246c8aeef025ddbe39b32a8b7d879ada042081 Mon Sep 17 00:00:00 2001 From: Matvej Kurakin Date: Fri, 13 Oct 2023 14:57:36 +0300 Subject: [PATCH 1/9] first commit --- include/utmatrix.h | 124 +++++++++++++++++++++++++++----- samples/sample_matrix.cpp | 12 +++- test/test_tmatrix.cpp | 116 +++++++++++++++++++++++++----- test/test_tvector.cpp | 147 ++++++++++++++++++++++++++++++++------ 4 files changed, 340 insertions(+), 59 deletions(-) diff --git a/include/utmatrix.h b/include/utmatrix.h index ddf23c6..a907dda 100644 --- a/include/utmatrix.h +++ b/include/utmatrix.h @@ -20,7 +20,7 @@ class TVector public: static const size_t max_size = std::numeric_limits::max(); - TVector() {} + TVector():size(0), startIndex(0), pVector(nullptr) {} TVector(int size, int startIndex = 0); //конструктор инициализации TVector(size_t size, size_t startIndex = 0); //конструктор инициализации @@ -29,10 +29,10 @@ class TVector TVector(const TVector &v); // конструктор копирования ~TVector(); - size_t getSize() { return size; } // размер вектора - size_t getStartIndex(){ return startIndex; } // индекс первого элемента - T& getElement(int i); - T& getElement(size_t i); + size_t getSize() const { return this->size; } // размер вектора + size_t getStartIndex() const{ return this->startIndex; } // индекс первого элемента + T& getElement(int i) const; + T& getElement(size_t i) const; void setElement(int index, T element); void setElement(size_t index, T element); @@ -56,104 +56,164 @@ class TVector // ввод-вывод friend std::istream& operator>>(std::istream &in, TVector &v) { + return in; } friend std::ostream& operator<<(std::ostream &out, const TVector &v) { + for (int i = 0; i < v.getSize()-v.getStartIndex() ; i++) + out << v.getElement(i)<<" "; return out; } }; template //конструктор инициализации -TVector::TVector(int _size, int startIndex) +TVector::TVector(int _size, int startIndex): size(_size), startIndex(startIndex) { - pVector = new T[_size]; + if (size > max_size || startIndex < 0 || startIndex >= size) + throw 1; + pVector = new T[size-startIndex]; + for (int i = 0;i//конструктор инициализации -TVector::TVector(size_t _size, size_t startIndex) +TVector::TVector(size_t _size, size_t startIndex) : size(_size), startIndex(startIndex) { + if (size > max_size || startIndex < 0 || startIndex >= size) + throw 1; + pVector = new T[size-startIndex]; + for (int i = 0; i < size - startIndex; i++) + pVector[i] = (T)0; } /*-------------------------------------------------------------------------*/ template //конструктор копирования -TVector::TVector(const TVector &v) +TVector::TVector(const TVector &v) : size(0), startIndex(0), pVector(nullptr) { + *this = v; } /*-------------------------------------------------------------------------*/ template //деструктор TVector::~TVector() { + delete[] pVector; } /*-------------------------------------------------------------------------*/ template // доступ T& TVector::operator[](int pos) { - return *pVector; + if (pos-startIndex < 0 || pos >= size) + throw 1; + return pVector[pos-startIndex]; } /*-------------------------------------------------------------------------*/ template // доступ T& TVector::operator[](size_t pos) { - return *pVector; + if (pos - startIndex < 0 || pos >= size) + throw 1; + return *pVector[pos - startIndex]; } /*-------------------------------------------------------------------------*/ template // сравнение -bool TVector::operator==(const TVector &v) const +bool TVector::operator==(const TVector& v) const { - return false; + if (size != v.size || startIndex != v.startIndex) return false; + for (int i = 0; i < size-startIndex; i++) + if (pVector[i] != v.pVector[i]) return false; + return true; } /*-------------------------------------------------------------------------*/ template // сравнение bool TVector::operator!=(const TVector &v) const { + return !(*this==v); } /*-------------------------------------------------------------------------*/ template // присваивание TVector& TVector::operator=(const TVector &v) { - return TVector(); + if (this == &v) return *this; + size = v.size; + startIndex = v.startIndex; + delete[] pVector; + pVector = new T[size-startIndex]; + for (int i = 0; i < size-startIndex; i++) + pVector[i] = v.pVector[i]; + return *this; } /*-------------------------------------------------------------------------*/ template // прибавить скаляр TVector TVector::operator+(const T &val) { + TVector a(size, startIndex); + for (int i = 0; i < size-startIndex; i++) + a.pVector[i] = pVector[i] + val; + return a; } /*-------------------------------------------------------------------------*/ template // вычесть скаляр TVector TVector::operator-(const T &val) { + TVector a(size, startIndex); + for (int i = 0; i < size - startIndex; i++) + a.pVector[i] = pVector[i] - val; + return a; } /*-------------------------------------------------------------------------*/ template // умножить на скаляр TVector TVector::operator*(const T &val) { + TVector a(size, startIndex); + for (int i = 0; i < size-startIndex; i++) + a.pVector[i] = pVector[i] * val; + return a; } /*-------------------------------------------------------------------------*/ template // сложение TVector TVector::operator+(const TVector &v) { + if (size != v.size || startIndex != v.startIndex) throw 1; + TVector a(size, startIndex); + for (int i = 0; i < size-startIndex; i++) + a.pVector[i] = pVector[i] + v.pVector[i]; + return a; + } /*-------------------------------------------------------------------------*/ template // вычитание TVector TVector::operator-(const TVector &v) { + if (size != v.size || startIndex != v.startIndex) throw 1; + TVector a(size, startIndex); + for (int i = 0; i < size-startIndex; i++) + a.pVector[i] = pVector[i] - v.pVector[i]; + return a; } /*-------------------------------------------------------------------------*/ template // скалярное произведение -T TVector::operator*(const TVector &v) +T TVector::operator*(const TVector& v) { - return T(); + if (size != v.size || startIndex != v.startIndex) throw 1; + T summ = 0; + for (int i = 0; i < size - startIndex; i++) + summ += pVector[i] * v.pVector[i]; + return summ; } /*-------------------------------------------------------------------------*/ template -T& TVector::getElement(int index) +T& TVector::getElement(int index) const { - return *pVector; + if (index < 0 || index + startIndex >= size) throw 1; + return pVector[index]; } template void TVector::setElement(int index, T element) { + if(index < 0 || index + startIndex >= size) + throw 1; + pVector[index] = element; } // Верхнетреугольная матрица @@ -196,8 +256,13 @@ class TMatrix : public TVector > }; template -TMatrix::TMatrix(int s): TVector >(s) +TMatrix::TMatrix(int s): TVector>(s) { + if ((s*s) > max_size) + throw 1; + for (int i = 0; i < s; i++) { + TVector>::pVector[i] = TVector(s, i); + } } /*-------------------------------------------------------------------------*/ template // конструктор копирования @@ -211,27 +276,48 @@ TMatrix::TMatrix(const TVector > &mt): template // сравнение bool TMatrix::operator==(const TMatrix &mt) const { + if (TVector>::size != mt.size || TVector>::startIndex != mt.startIndex) + return false; + for (int i = 0; i < TVector>::size; i++) { + if (TVector>::pVector[i] != mt.pVector[i]) + return false; + } + return true; } /*-------------------------------------------------------------------------*/ template // сравнение bool TMatrix::operator!=(const TMatrix &mt) const { + return !(*this == mt); } /*-------------------------------------------------------------------------*/ template // присваивание TMatrix& TMatrix::operator=(const TMatrix &mt) { + if (this == &mt) return *this; + size = mt.size; + startIndex = mt.startIndex; + delete[] pVector; + pVector = new TVector[size]; + for (int i = 0; i < size; i++) + this->setElement(i, mt.getElement(i)); return *this; } /*-------------------------------------------------------------------------*/ template // сложение TMatrix TMatrix::operator+(const TMatrix &mt) { + for (int i = 0; i < size; i++) + this->setElement(i, this->getElement(i) + mt.getElement(i)); return *this; } /*-------------------------------------------------------------------------*/ template // вычитание TMatrix TMatrix::operator-(const TMatrix &mt) { + //for (int i = 0; i < size - startIndex; i++) + // this->setElement(i, this->getElement(i) - mt.getElement(i)); + for (int i = 0; i < size; i++) + this->setElement(i, this->getElement(i) - mt.getElement(i)); return *this; } /*-------------------------------------------------------------------------*/ diff --git a/samples/sample_matrix.cpp b/samples/sample_matrix.cpp index 4c2b826..a7f1312 100644 --- a/samples/sample_matrix.cpp +++ b/samples/sample_matrix.cpp @@ -10,7 +10,13 @@ int main() { - TMatrix a(5), b(5), c(5); + const int size = 2; + TMatrix m1(size); + m1 = m1; + // << "\n" << m2; + //std::cout << a; + //std::cout << a; + /*TMatrix a(5), b(5), c(5); int i, j; setlocale(LC_ALL, "Russian"); std::cout << " " @@ -25,6 +31,8 @@ int main() std::cout << "Matrix a = " << std::endl << a << std::endl; std::cout << "Matrix b = " << std::endl << b << std::endl; std::cout << "Matrix c = a + b" << std::endl << c << std::endl; - return 0; + TMatrix d = a; + std::cout << d; + return 0;*/ } //--------------------------------------------------------------------------- diff --git a/test/test_tmatrix.cpp b/test/test_tmatrix.cpp index dc26a84..6f806e3 100644 --- a/test/test_tmatrix.cpp +++ b/test/test_tmatrix.cpp @@ -9,7 +9,8 @@ TEST(TMatrix, can_create_matrix_with_positive_length) TEST(TMatrix, cant_create_too_large_matrix) { - + ASSERT_ANY_THROW(TMatrix(static_cast(sqrt(TVector::max_size)))); + //ADD_FAILURE(); } TEST(TMatrix, throws_when_create_matrix_with_negative_length) @@ -26,85 +27,164 @@ TEST(TMatrix, can_create_copied_matrix) TEST(TMatrix, copied_matrix_is_equal_to_source_one) { - ADD_FAILURE(); + TMatrix m1(2); + m1[0][0] = 1; + m1[0][1] = 2; + m1[1][1] = 3; + TMatrix m2 = m1; + EXPECT_EQ(m1, m2); } TEST(TMatrix, copied_matrix_has_its_own_memory) { - ADD_FAILURE(); + TMatrix m1(2); + m1[0][0] = 1; + m1[0][1] = 2; + m1[1][1] = 3; + TMatrix m2 = m1; + m1[0][0] = 10; + EXPECT_NE(m1, m2); } TEST(TMatrix, can_get_size) { - ADD_FAILURE(); + TMatrix m(2); + EXPECT_EQ(m.getSize(), 2); } TEST(TMatrix, can_set_and_get_element) { - ADD_FAILURE(); + TMatrix m(4); + m[0][0] = 4; + + EXPECT_EQ(4, m[0][0]); } TEST(TMatrix, throws_when_set_element_with_negative_index) { - ADD_FAILURE(); + TMatrix m(4); + ASSERT_ANY_THROW(m[-1]); } TEST(TMatrix, throws_when_set_element_with_too_large_index) { - ADD_FAILURE(); + TMatrix m(4); + ASSERT_ANY_THROW(m[5]); } TEST(TMatrix, can_assign_matrix_to_itself) { - ADD_FAILURE(); + TMatrix m(4); + ASSERT_NO_THROW(m = m); } TEST(TMatrix, can_assign_matrices_of_equal_size) { - ADD_FAILURE(); + const int size = 2; + TMatrix m1(size), m2(size); + m1[0][0] = 1; + m1[1][1] = 2; + m2 = m1; + EXPECT_EQ(m1[0][0], m2[0][0]); + EXPECT_EQ(m1[1][1], m2[1][1]); } TEST(TMatrix, assign_operator_change_matrix_size) { - ADD_FAILURE(); + const int size1 = 2, size2 = 1; + TMatrix m1(size1), m2(size2); + m1[0][0] = 1; + m1[1][1] = 2; + m2[0][0] = 1; + m1 = m2; + EXPECT_EQ(size2, m1.getSize()); } TEST(TMatrix, can_assign_matrices_of_different_size) { - ADD_FAILURE(); + const int size1 = 2, size2 = 1; + TMatrix m1(size1), m2(size2); + m1[0][0] = 1; + m1[1][1] = 2; + m2 = m1; + EXPECT_EQ(m1[0][0], m2[0][0]); + EXPECT_EQ(m1[1][1], m2[1][1]); } TEST(TMatrix, compare_equal_matrices_return_true) { - ADD_FAILURE(); + const int size = 2; + TMatrix m1(size), m2(size); + m1[0][0] = 1; + m1[1][1] = 2; + m2[0][0] = 1; + m2[1][1] = 2; + EXPECT_EQ(m1, m2); } TEST(TMatrix, compare_matrix_with_itself_return_true) { - ADD_FAILURE(); + const int size = 2; + TMatrix m1(size); + m1[0] = 1; + m1[1] = 2; + EXPECT_EQ(m1, m1); } TEST(TMatrix, matrices_with_different_size_are_not_equal) { - ADD_FAILURE(); + const int size1 = 2, size2 = 1; + TMatrix m1(size1), m2(size2); + m1[0][0] = 1; + m1[1][1] = 2; + m2[0][0] = 1; + EXPECT_NE(m1, m2); } TEST(TMatrix, can_add_matrices_with_equal_size) { - ADD_FAILURE(); + const int size = 2; + TMatrix m1(size), m2(size), m3(size); + m1[0][0] = 1; + m1[1][1] = 2; + m2[0][0] = 2; + m2[1][1] = 4; + + m3[0][0] = 3; + m3[1][1] = 6; + EXPECT_EQ(m3, m1 + m2); } TEST(TMatrix, cant_add_matrices_with_not_equal_size) { - ADD_FAILURE(); + const int size1 = 2, size2 = 1; + TMatrix m1(size1), m2(size2); + m1[0][0] = 1; + m1[1][1] = 2; + m2[0][0] = 2; + ASSERT_ANY_THROW(m1 + m2); } TEST(TMatrix, can_subtract_matrices_with_equal_size) { - ADD_FAILURE(); + const int size = 2; + TMatrix m1(size), m2(size), m3(size); + m1[0][0] = 1; + m1[1][1] = 2; + m2[0][0] = 2; + m2[1][1] = 4; + + m3[0][0] = 1; + m3[1][1] = 2; + EXPECT_EQ(m3, m2 - m1); } TEST(TMatrix, cant_subtract_matrixes_with_not_equal_size) { - ADD_FAILURE(); + const int size1 = 2, size2 = 1; + TMatrix m1(size1), m2(size2); + m1[0][0] = 1; + m1[1][1] = 2; + m2[0][0] = 2; + ASSERT_ANY_THROW(m2 - m1); } diff --git a/test/test_tvector.cpp b/test/test_tvector.cpp index 804be2c..bfbf1f4 100644 --- a/test/test_tvector.cpp +++ b/test/test_tvector.cpp @@ -31,12 +31,22 @@ TEST(TVector, can_create_copied_vector) TEST(TVector, copied_vector_is_equal_to_source_one) { - ADD_FAILURE(); + TVector v1(2); + v1[0] = 1; + v1[1] = 2; + TVector v2 = v1; + EXPECT_EQ(v1, v2); } TEST(TVector, copied_vector_has_its_own_memory) { - ADD_FAILURE(); + TVector v1(2); + v1[0] = 1; + v1[1] = 2; + TVector v2(1); + v2[0] = 3; + v2 = v1; + EXPECT_EQ(v1, v2); } TEST(TVector, can_get_size) @@ -63,90 +73,187 @@ TEST(TVector, can_set_and_get_element) TEST(TVector, throws_when_set_element_with_negative_index) { - ADD_FAILURE(); + TVector v(4); + ASSERT_ANY_THROW(v[-1] = 1); } TEST(TVector, throws_when_set_element_with_too_large_index) { - ADD_FAILURE(); + TVector v(4); + ASSERT_ANY_THROW(v[5] = 1); } TEST(TVector, can_assign_vector_to_itself) { - ADD_FAILURE(); + TVector v1(2); + ASSERT_NO_THROW(v1 = v1); } TEST(TVector, can_assign_vectors_of_equal_size) { - ADD_FAILURE(); + const int size = 2; + TVector v1(size), v2(size); + v1[0] = 1; + v1[1] = 2; + v2 = v1; + EXPECT_EQ(v1[0], v2[0]); + EXPECT_EQ(v1[1], v2[1]); } TEST(TVector, assign_operator_change_vector_size) { - ADD_FAILURE(); + const int size1 = 2, size2 = 1; + TVector v1(size1), v2(size2); + v1[0] = 1; + v1[1] = 2; + v2[0] = 1; + v1 = v2; + EXPECT_EQ(size2, v1.getSize()); } TEST(TVector, can_assign_vectors_of_different_size) { - ADD_FAILURE(); + const int size1 = 2, size2 = 1; + TVector v1(size1), v2(size2); + v1[0] = 1; + v1[1] = 2; + v2 = v1; + EXPECT_EQ(v1[0], v2[0]); + EXPECT_EQ(v1[1], v2[1]); } TEST(TVector, compare_equal_vectors_return_true) { - ADD_FAILURE(); + const int size = 2; + TVector v1(size), v2(size); + v1[0] = 1; + v1[1] = 2; + v2[0] = 1; + v2[1] = 2; + EXPECT_EQ(v1, v2); } TEST(TVector, compare_vector_with_itself_return_true) { - ADD_FAILURE(); + const int size = 2; + TVector v1(size); + v1[0] = 1; + v1[1] = 2; + EXPECT_EQ(v1, v1); } TEST(TVector, vectors_with_different_size_are_not_equal) { - ADD_FAILURE(); + const int size1 = 2, size2 = 1; + TVector v1(size1), v2(size2); + v1[0] = 1; + v1[1] = 2; + v2[0] = 1; + EXPECT_NE(v1, v2); } TEST(TVector, can_add_scalar_to_vector) { - ADD_FAILURE(); + const int size = 2; + TVector v1(size),v2(size); + v1[0] = 1; + v1[1] = 2; + v2[0] = 2; + v2[1] = 3; + v1 = v1 + 1; + EXPECT_EQ(v1, v2); } TEST(TVector, can_subtract_scalar_from_vector) { - ADD_FAILURE(); + const int size = 2; + TVector v1(size), v2(size); + v1[0] = 1; + v1[1] = 2; + v2[0] = 2; + v2[1] = 3; + v2 = v2 - 1; + EXPECT_EQ(v1, v2); } TEST(TVector, can_multiply_scalar_by_vector) { - ADD_FAILURE(); + const int size = 2; + TVector v1(size), v2(size); + v1[0] = 1; + v1[1] = 2; + v2[0] = 2; + v2[1] = 4; + v1 = v1 * 2; + EXPECT_EQ(v1, v2); } TEST(TVector, can_add_vectors_with_equal_size) { - ADD_FAILURE(); + const int size = 2; + TVector v1(size), v2(size), v3(size); + v1[0] = 1; + v1[1] = 2; + v2[0] = 2; + v2[1] = 4; + + v3[0] = 3; + v3[1] = 6; + EXPECT_EQ(v3, v1 + v2); } TEST(TVector, cant_add_vectors_with_not_equal_size) { - ADD_FAILURE(); + const int size1 = 2, size2 =1; + TVector v1(size1), v2(size2); + v1[0] = 1; + v1[1] = 2; + v2[0] = 2; + ASSERT_ANY_THROW(v1 + v2); } TEST(TVector, can_subtract_vectors_with_equal_size) { - ADD_FAILURE(); + const int size = 2; + TVector v1(size), v2(size), v3(size); + v1[0] = 1; + v1[1] = 2; + v2[0] = 2; + v2[1] = 4; + + v3[0] = 1; + v3[1] = 2; + EXPECT_EQ(v3, v2 - v1); } TEST(TVector, cant_subtract_vectors_with_not_equal_size) { - ADD_FAILURE(); + const int size1 = 2, size2 = 1; + TVector v1(size1), v2(size2); + v1[0] = 1; + v1[1] = 2; + v2[0] = 2; + ASSERT_ANY_THROW(v2 - v1); } TEST(TVector, can_multiply_vectors_with_equal_size) { - ADD_FAILURE(); + const int size = 2; + TVector v1(size), v2(size); + int m = 10; + v1[0] = 1; + v1[1] = 2; + v2[0] = 2; + v2[1] = 4; + EXPECT_EQ(m, v1 * v2); } TEST(TVector, cant_multiply_vectors_with_not_equal_size) { - ADD_FAILURE(); + const int size1 = 2, size2 = 1; + TVector v1(size1), v2(size2); + v1[0] = 1; + v1[1] = 2; + v2[0] = 2; + ASSERT_ANY_THROW(v1 * v2); } From d2216a25c54e3442a254fd29e7f0e3f06b6f6841 Mon Sep 17 00:00:00 2001 From: Matvej Kurakin Date: Fri, 13 Oct 2023 15:16:02 +0300 Subject: [PATCH 2/9] first commit --- include/utmatrix.h | 18 ++++++++---------- test/test_tmatrix.cpp | 2 +- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/include/utmatrix.h b/include/utmatrix.h index a907dda..5c43923 100644 --- a/include/utmatrix.h +++ b/include/utmatrix.h @@ -258,7 +258,7 @@ class TMatrix : public TVector > template TMatrix::TMatrix(int s): TVector>(s) { - if ((s*s) > max_size) + if ((s*s) > TVector::max_size) throw 1; for (int i = 0; i < s; i++) { TVector>::pVector[i] = TVector(s, i); @@ -295,11 +295,11 @@ template // присваивание TMatrix& TMatrix::operator=(const TMatrix &mt) { if (this == &mt) return *this; - size = mt.size; - startIndex = mt.startIndex; - delete[] pVector; - pVector = new TVector[size]; - for (int i = 0; i < size; i++) + this->size = mt.size; + this->startIndex = mt.startIndex; + delete[] this->pVector; + this->pVector = new TVector[size]; + for (int i = 0; i < this->size; i++) this->setElement(i, mt.getElement(i)); return *this; } /*-------------------------------------------------------------------------*/ @@ -307,7 +307,7 @@ TMatrix& TMatrix::operator=(const TMatrix &mt) template // сложение TMatrix TMatrix::operator+(const TMatrix &mt) { - for (int i = 0; i < size; i++) + for (int i = 0; i < this->size; i++) this->setElement(i, this->getElement(i) + mt.getElement(i)); return *this; } /*-------------------------------------------------------------------------*/ @@ -315,9 +315,7 @@ TMatrix TMatrix::operator+(const TMatrix &mt) template // вычитание TMatrix TMatrix::operator-(const TMatrix &mt) { - //for (int i = 0; i < size - startIndex; i++) - // this->setElement(i, this->getElement(i) - mt.getElement(i)); - for (int i = 0; i < size; i++) + for (int i = 0; i < this->size; i++) this->setElement(i, this->getElement(i) - mt.getElement(i)); return *this; } /*-------------------------------------------------------------------------*/ diff --git a/test/test_tmatrix.cpp b/test/test_tmatrix.cpp index 6f806e3..0b60de6 100644 --- a/test/test_tmatrix.cpp +++ b/test/test_tmatrix.cpp @@ -9,7 +9,7 @@ TEST(TMatrix, can_create_matrix_with_positive_length) TEST(TMatrix, cant_create_too_large_matrix) { - ASSERT_ANY_THROW(TMatrix(static_cast(sqrt(TVector::max_size)))); + ASSERT_ANY_THROW(TMatrix(static_cast(sqrt(TVector::max_size)+1))); //ADD_FAILURE(); } From c039b8e48e0283d34848f022fa2f7eed0a9560a8 Mon Sep 17 00:00:00 2001 From: Matvej Kurakin Date: Fri, 13 Oct 2023 15:17:53 +0300 Subject: [PATCH 3/9] second commit --- include/utmatrix.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/utmatrix.h b/include/utmatrix.h index 5c43923..5c222a7 100644 --- a/include/utmatrix.h +++ b/include/utmatrix.h @@ -298,7 +298,7 @@ TMatrix& TMatrix::operator=(const TMatrix &mt) this->size = mt.size; this->startIndex = mt.startIndex; delete[] this->pVector; - this->pVector = new TVector[size]; + this->pVector = new TVector[this->size]; for (int i = 0; i < this->size; i++) this->setElement(i, mt.getElement(i)); return *this; From 3892a73d147a4349614be3765cc19062274f06ca Mon Sep 17 00:00:00 2001 From: Matvej Kurakin Date: Fri, 13 Oct 2023 15:18:54 +0300 Subject: [PATCH 4/9] second commit --- include/utmatrix.h | 1 + 1 file changed, 1 insertion(+) diff --git a/include/utmatrix.h b/include/utmatrix.h index 5c222a7..b59caab 100644 --- a/include/utmatrix.h +++ b/include/utmatrix.h @@ -299,6 +299,7 @@ TMatrix& TMatrix::operator=(const TMatrix &mt) this->startIndex = mt.startIndex; delete[] this->pVector; this->pVector = new TVector[this->size]; + for (int i = 0; i < this->size; i++) this->setElement(i, mt.getElement(i)); return *this; From d34627654a46aa3355d5b3fe00489faa47405f97 Mon Sep 17 00:00:00 2001 From: Matvej Kurakin Date: Wed, 18 Oct 2023 13:14:32 +0300 Subject: [PATCH 5/9] third commit --- include/utmatrix.h | 3 ++- samples/sample_matrix.cpp | 5 ++--- test/test_tmatrix.cpp | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/include/utmatrix.h b/include/utmatrix.h index b59caab..1ff27fb 100644 --- a/include/utmatrix.h +++ b/include/utmatrix.h @@ -258,7 +258,8 @@ class TMatrix : public TVector > template TMatrix::TMatrix(int s): TVector>(s) { - if ((s*s) > TVector::max_size) + //unsigned long long x = (s * s); + if (static_cast(static_cast(s)* static_cast(s)) > static_cast(TVector::max_size)) throw 1; for (int i = 0; i < s; i++) { TVector>::pVector[i] = TVector(s, i); diff --git a/samples/sample_matrix.cpp b/samples/sample_matrix.cpp index a7f1312..bc120a0 100644 --- a/samples/sample_matrix.cpp +++ b/samples/sample_matrix.cpp @@ -10,9 +10,8 @@ int main() { - const int size = 2; - TMatrix m1(size); - m1 = m1; + TMatrix m(65536); + std::cout << m; // << "\n" << m2; //std::cout << a; //std::cout << a; diff --git a/test/test_tmatrix.cpp b/test/test_tmatrix.cpp index 0b60de6..9ba2f7d 100644 --- a/test/test_tmatrix.cpp +++ b/test/test_tmatrix.cpp @@ -9,7 +9,7 @@ TEST(TMatrix, can_create_matrix_with_positive_length) TEST(TMatrix, cant_create_too_large_matrix) { - ASSERT_ANY_THROW(TMatrix(static_cast(sqrt(TVector::max_size)+1))); + ASSERT_ANY_THROW(TMatrix(static_cast(sqrt(TVector::max_size))+1)); //ADD_FAILURE(); } From 0515b6a2ca5b4728d2365e8d5c98eb39d3af3703 Mon Sep 17 00:00:00 2001 From: Matvej Kurakin Date: Wed, 18 Oct 2023 13:17:54 +0300 Subject: [PATCH 6/9] fourth commit --- test/test_tmatrix.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test_tmatrix.cpp b/test/test_tmatrix.cpp index 9ba2f7d..c983f27 100644 --- a/test/test_tmatrix.cpp +++ b/test/test_tmatrix.cpp @@ -9,7 +9,7 @@ TEST(TMatrix, can_create_matrix_with_positive_length) TEST(TMatrix, cant_create_too_large_matrix) { - ASSERT_ANY_THROW(TMatrix(static_cast(sqrt(TVector::max_size))+1)); + ASSERT_ANY_THROW(TMatrix(static_cast(65537))); //ADD_FAILURE(); } From 4bc30e4e84e018e20651e91006efda14bd5b8f7e Mon Sep 17 00:00:00 2001 From: Matvej Kurakin Date: Wed, 20 Dec 2023 09:08:21 +0300 Subject: [PATCH 7/9] first commit --- include/utmatrix.h | 34 ++++++++++++--- samples/sample_matrix.cpp | 6 ++- test/test_tvector.cpp | 91 +++++++++++++++++++++++++++++++++++---- 3 files changed, 116 insertions(+), 15 deletions(-) diff --git a/include/utmatrix.h b/include/utmatrix.h index 1ff27fb..f6e8c86 100644 --- a/include/utmatrix.h +++ b/include/utmatrix.h @@ -134,9 +134,10 @@ template // присваивание TVector& TVector::operator=(const TVector &v) { if (this == &v) return *this; + if(size!=0) + delete[] pVector; size = v.size; startIndex = v.startIndex; - delete[] pVector; pVector = new T[size-startIndex]; for (int i = 0; i < size-startIndex; i++) pVector[i] = v.pVector[i]; @@ -204,16 +205,29 @@ T TVector::operator*(const TVector& v) template T& TVector::getElement(int index) const { - if (index < 0 || index + startIndex >= size) throw 1; + if (index < startIndex || index>= size) throw 1; return pVector[index]; } +template +T& TVector::getElement(size_t index) const +{ + if (index < startIndex || index >= size) throw 1; + return pVector[index-startIndex]; +} + template void TVector::setElement(int index, T element) { - if(index < 0 || index + startIndex >= size) - throw 1; - pVector[index] = element; + if(index < startIndex || index>= size) throw 1; + pVector[index-startIndex] = element; +} + +template +void TVector::setElement(size_t index, T element) +{ + if (index < startIndex || index >= size) throw 1; + pVector[index - startIndex] = element; } // Верхнетреугольная матрица @@ -265,6 +279,16 @@ TMatrix::TMatrix(int s): TVector>(s) TVector>::pVector[i] = TVector(s, i); } } /*-------------------------------------------------------------------------*/ +template +TMatrix::TMatrix(size_t s): TVector>(s) +{ + //unsigned long long x = (s * s); + if (static_cast(static_cast(s)* static_cast(s)) > static_cast(TVector::max_size)) + throw 1; + for (int i = 0; i < s; i++) { + TVector>::pVector[i] = TVector(s, i); + } +} /*-------------------------------------------------------------------------*/ template // конструктор копирования TMatrix::TMatrix(const TMatrix &mt): diff --git a/samples/sample_matrix.cpp b/samples/sample_matrix.cpp index bc120a0..019299e 100644 --- a/samples/sample_matrix.cpp +++ b/samples/sample_matrix.cpp @@ -10,8 +10,10 @@ int main() { - TMatrix m(65536); - std::cout << m; + TMatrix m(4); + m[0].setElement(0,4); + + std::cout << m[0].getElement(0); // << "\n" << m2; //std::cout << a; //std::cout << a; diff --git a/test/test_tvector.cpp b/test/test_tvector.cpp index bfbf1f4..d4b8e8a 100644 --- a/test/test_tvector.cpp +++ b/test/test_tvector.cpp @@ -1,25 +1,81 @@ #include #include "utmatrix.h" -TEST(TVector, can_create_vector_with_positive_length) +TEST(TVector, can_create_vector_with_positive_length_int) { - ASSERT_NO_THROW(TVector v(5)); + const int size = 5; + ASSERT_NO_THROW(TVector v(size)); } -TEST(TVector, cant_create_too_large_vector) +TEST(TVector, can_create_vector_with_positive_length_size_t) +{ + const size_t size = 5; + ASSERT_NO_THROW(TVector v(size)); +} + +TEST(Tvector, can_create_huge_vector) +{ + const size_t size = 200ull * 100ull * 100ull * 100ull; + const size_t startIndex = size - 10ull; + TVector v1(size, startIndex); + ASSERT_NO_THROW(v1.setElement(size - 1ull, 1.0)); + + EXPECT_EQ(size, v1.getSize()); + EXPECT_EQ(1.0, v1.getElement(size - 1ull)); + +} + +TEST(TVector, cant_create_too_large_vector_int) { // = TVector::max_size ASSERT_ANY_THROW(TVector v(TVector::max_size + static_cast(1))); } -TEST(TVector, throws_when_create_vector_with_negative_length) +TEST(TVector, cant_create_too_large_vector_size_t) +{ + // = TVector::max_size + ASSERT_ANY_THROW(TVector v(TVector::max_size + static_cast(1))); +} + +TEST(TVector, throws_when_create_vector_with_negative_length_int) +{ + const int size = -5; + ASSERT_ANY_THROW(TVector v(size)); +} + +TEST(TVector, throws_when_create_vector_with_negative_length_size_t) { - ASSERT_ANY_THROW(TVector v(-5)); + const size_t size = -5; + ASSERT_ANY_THROW(TVector v(size)); } -TEST(TVector, throws_when_create_vector_with_negative_startindex) +TEST(TVector, create_vector_with_startindex_int) { - ASSERT_ANY_THROW(TVector v(5, -2)); + const int size = 5; + const int startIndex = 2; + ASSERT_NO_THROW(TVector v(size, startIndex)); +} + +TEST(TVector, create_vector_with_startindex_size_t) +{ + const size_t size = 5; + const size_t startIndex = 2; + ASSERT_NO_THROW(TVector v(size, startIndex)); +} + + +TEST(TVector, throws_when_create_vector_with_negative_startindex_int) +{ + const int size = 5; + const int startIndex = -2; + ASSERT_ANY_THROW(TVector v(size, startIndex)); +} + +TEST(TVector, throws_when_create_vector_with_negative_startindex_size_t) +{ + const size_t size = 5; + const size_t startIndex = -2; + ASSERT_ANY_THROW(TVector v(size, startIndex)); } TEST(TVector, can_create_copied_vector) @@ -63,7 +119,7 @@ TEST(TVector, can_get_start_index) EXPECT_EQ(2, v.getStartIndex()); } -TEST(TVector, can_set_and_get_element) +TEST(TVector, can_set_and_get_element_with) { TVector v(4); v[0] = 4; @@ -71,6 +127,14 @@ TEST(TVector, can_set_and_get_element) EXPECT_EQ(4, v[0]); } +TEST(TVector, can_set_and_get_element) +{ + TVector v(4); + v.getElement(0) = 4; + + EXPECT_EQ(4, v.getElement(0)); +} + TEST(TVector, throws_when_set_element_with_negative_index) { TVector v(4); @@ -257,3 +321,14 @@ TEST(TVector, cant_multiply_vectors_with_not_equal_size) v2[0] = 2; ASSERT_ANY_THROW(v1 * v2); } + +TEST(TVector, can_1) +{ + const int size1 = 2, size2 = 0; + TVector v1(size1), v2; + v1[0] = 1; + v1[1] = 2; + v2 = v1 + 1; + v1 = v1 + 1; + EXPECT_EQ(v1, v2); +} From 0a0914bc66d3e8a466a55570c9c9b248334a36b5 Mon Sep 17 00:00:00 2001 From: Matvej Kurakin Date: Wed, 20 Dec 2023 13:23:21 +0300 Subject: [PATCH 8/9] second commit --- include/utmatrix.h | 12 ++++++------ test/test_tvector.cpp | 4 ++-- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/include/utmatrix.h b/include/utmatrix.h index f6e8c86..363b014 100644 --- a/include/utmatrix.h +++ b/include/utmatrix.h @@ -31,9 +31,9 @@ class TVector ~TVector(); size_t getSize() const { return this->size; } // размер вектора size_t getStartIndex() const{ return this->startIndex; } // индекс первого элемента - T& getElement(int i) const; + //T& getElement(int i) const; T& getElement(size_t i) const; - void setElement(int index, T element); + //void setElement(int index, T element); void setElement(size_t index, T element); T& operator[](int pos); // доступ @@ -202,12 +202,12 @@ T TVector::operator*(const TVector& v) return summ; } /*-------------------------------------------------------------------------*/ -template +/*template T& TVector::getElement(int index) const { if (index < startIndex || index>= size) throw 1; return pVector[index]; -} +}*/ template T& TVector::getElement(size_t index) const @@ -216,12 +216,12 @@ T& TVector::getElement(size_t index) const return pVector[index-startIndex]; } -template +/*template void TVector::setElement(int index, T element) { if(index < startIndex || index>= size) throw 1; pVector[index-startIndex] = element; -} +}*/ template void TVector::setElement(size_t index, T element) diff --git a/test/test_tvector.cpp b/test/test_tvector.cpp index d4b8e8a..05b4c8c 100644 --- a/test/test_tvector.cpp +++ b/test/test_tvector.cpp @@ -15,8 +15,8 @@ TEST(TVector, can_create_vector_with_positive_length_size_t) TEST(Tvector, can_create_huge_vector) { - const size_t size = 200ull * 100ull * 100ull * 100ull; - const size_t startIndex = size - 10ull; + size_t size = 200ull * 100ull * 100ull * 100ull; + size_t startIndex = size - 10ull; TVector v1(size, startIndex); ASSERT_NO_THROW(v1.setElement(size - 1ull, 1.0)); From de78b060e9c273182011e9d131241029b04f73d1 Mon Sep 17 00:00:00 2001 From: Matvej Kurakin Date: Wed, 20 Dec 2023 13:25:29 +0300 Subject: [PATCH 9/9] second commit --- test/test_tvector.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/test_tvector.cpp b/test/test_tvector.cpp index 05b4c8c..d4b8e8a 100644 --- a/test/test_tvector.cpp +++ b/test/test_tvector.cpp @@ -15,8 +15,8 @@ TEST(TVector, can_create_vector_with_positive_length_size_t) TEST(Tvector, can_create_huge_vector) { - size_t size = 200ull * 100ull * 100ull * 100ull; - size_t startIndex = size - 10ull; + const size_t size = 200ull * 100ull * 100ull * 100ull; + const size_t startIndex = size - 10ull; TVector v1(size, startIndex); ASSERT_NO_THROW(v1.setElement(size - 1ull, 1.0));