diff --git a/example 1.png b/example 1.png new file mode 100644 index 000000000..dda85b14f Binary files /dev/null and b/example 1.png differ diff --git a/include/tmatrix.h b/include/tmatrix.h index b77f08845..10d505657 100644 --- a/include/tmatrix.h +++ b/include/tmatrix.h @@ -1,174 +1,279 @@ -// ННГУ, ИИТММ, Курс "Алгоритмы и структуры данных" -// -// Copyright (c) Сысоев А.В. -// -// - -#ifndef __TDynamicMatrix_H__ -#define __TDynamicMatrix_H__ +#ifndef __TMATRIX_H__ +#define __TMATRIX_H__ #include +#include +#include using namespace std; + const int MAX_VECTOR_SIZE = 100000000; const int MAX_MATRIX_SIZE = 10000; -// Динамический вектор - -// шаблонный вектор на динамической памяти -template -class TDynamicVector + +//* ---класс Вектор--- +template +class TVector { -protected: - size_t sz; - T* pMem; -public: - TDynamicVector(size_t size = 1) : sz(size) - { - if (sz == 0) - throw out_of_range("Vector size should be greater than zero"); - pMem = new T[sz]();// {}; // У типа T д.б. констуктор по умолчанию - } - TDynamicVector(T* arr, size_t s) : sz(s) - { - assert(arr != nullptr && "TDynamicVector ctor requires non-nullptr arg"); - pMem = new T[sz]; - std::copy(arr, arr + sz, pMem); - } - TDynamicVector(const TDynamicVector& v) - { - } - TDynamicVector(TDynamicVector&& v) noexcept - { - } - ~TDynamicVector() - { - } - TDynamicVector& operator=(const TDynamicVector& v) - { - } - TDynamicVector& operator=(TDynamicVector&& v) noexcept - { - } + protected: + ValType* pVector; + int Size; // размер + int StartIndex; // индекс + public: + TVector(int s = 10, int si = 0); + TVector(const TVector& v); // копирования + ~TVector(); // деструктор + int GetSize() { return Size; } // получить размер + int GetStartIndex() { return StartIndex; } // получить индекс первого элемента + ValType& operator[](int pos); // индекс + bool operator==(const TVector& v) const; // сравнение + bool operator!=(const TVector& v) const; // сравнение + TVector& operator=(const TVector& v); // присваивание - size_t size() const noexcept { return sz; } + TVector operator+(const ValType& val); // прибавить скаляр + TVector operator-(const ValType& val); // вычесть скаляр + TVector operator*(const ValType& val); // умножить на скаляр - // индексация - T& operator[](size_t ind) - { - } - const T& operator[](size_t ind) const - { - } - // индексация с контролем - T& at(size_t ind) - { - } - const T& at(size_t ind) const - { - } + TVector operator+(const TVector& v); // сложение + TVector operator-(const TVector& v); // вычитание + ValType operator*(const TVector& v); // скалярное произведение - // сравнение - bool operator==(const TDynamicVector& v) const noexcept - { - } - bool operator!=(const TDynamicVector& v) const noexcept + friend istream& operator>>(istream& in, TVector& v) //ввод { + for (int i = 0; i < v.Size; i++) + in >> v.pVector[i]; + return in; } - // скалярные операции - TDynamicVector operator+(T val) - { - } - TDynamicVector operator-(double val) - { - } - TDynamicVector operator*(double val) + friend ostream& operator<<(ostream& out, const TVector& v) //вывод { + for (int i = 0; i < v.Size; i++) + out << v.pVector[i] << ' '; + return out; } +}; - // векторные операции - TDynamicVector operator+(const TDynamicVector& v) - { - } - TDynamicVector operator-(const TDynamicVector& v) - { - } - T operator*(const TDynamicVector& v) noexcept(noexcept(T())) - { - } +template +TVector::TVector(int s, int si) : Size(s), StartIndex(si) +{ + if ((s < 1) || (s > MAX_VECTOR_SIZE) || (si < 0) || (si > MAX_MATRIX_SIZE - 1)) + throw string("Wrong arguments"); + pVector = new ValType[s]{}; +} - friend void swap(TDynamicVector& lhs, TDynamicVector& rhs) noexcept - { - std::swap(lhs.sz, rhs.sz); - std::swap(lhs.pMem, rhs.pMem); - } +template +TVector::TVector(const TVector& v) +{ + Size = v.Size; + StartIndex = v.StartIndex; + pVector = new ValType[Size]; + for (int i = 0; i < Size; i++) + pVector[i] = v.pVector[i]; +} - // ввод/вывод - friend istream& operator>>(istream& istr, TDynamicVector& v) - { - for (size_t i = 0; i < v.sz; i++) - istr >> v.pMem[i]; // требуется оператор>> для типа T - return istr; - } - friend ostream& operator<<(ostream& ostr, const TDynamicVector& v) - { - for (size_t i = 0; i < v.sz; i++) - ostr << v.pMem[i] << ' '; // требуется оператор<< для типа T - return ostr; - } -}; +template +TVector::~TVector() +{ + delete[] pVector; +} +template +ValType& TVector::operator[](int pos) +{ + pos -= StartIndex; + if ((pos < 0) || (pos >= Size)) + throw string("Wrong argument"); + return pVector[pos]; +} -// Динамическая матрица - -// шаблонная матрица на динамической памяти -template -class TDynamicMatrix : private TDynamicVector> +template +bool TVector::operator==(const TVector& v) const { - using TDynamicVector>::pMem; - using TDynamicVector>::sz; -public: - TDynamicMatrix(size_t s = 1) : TDynamicVector>(s) - { - for (size_t i = 0; i < sz; i++) - pMem[i] = TDynamicVector(sz); + if (this == &v) + return true; + if (Size != v.Size) + return false; + if (StartIndex != v.StartIndex) + return false; + for (int i = 0; i < Size; i++) { + if (pVector[i] != v.pVector[i]) + return false; } + return true; +} - using TDynamicVector>::operator[]; +template +bool TVector::operator!=(const TVector& v) const +{ + return !(*this == v); +} - // сравнение - bool operator==(const TDynamicMatrix& m) const noexcept - { +template +TVector& TVector::operator=(const TVector& v) +{ + if (this == &v) + return *this; + if (Size != v.Size) { + ValType* tmp = new ValType[v.Size]; + delete[] pVector; + pVector = tmp; + Size = v.Size; } + StartIndex = v.StartIndex; + for (int i = 0; i < Size; i++) + pVector[i] = v.pVector[i]; + return *this; +} - // матрично-скалярные операции - TDynamicVector operator*(const T& val) - { - } +template +TVector TVector::operator+(const ValType& val) +{ + TVector out(Size, StartIndex); + for (int i = 0; i < Size; i++) + out.pVector[i] = pVector[i] + val; + return out; +} - // матрично-векторные операции - TDynamicVector operator*(const TDynamicVector& v) - { - } +template +TVector TVector::operator-(const ValType& val) +{ + TVector out(Size, StartIndex); + for (int i = 0; i < Size; i++) + out.pVector[i] = pVector[i] - val; + return out; +} - // матрично-матричные операции - TDynamicMatrix operator+(const TDynamicMatrix& m) - { - } - TDynamicMatrix operator-(const TDynamicMatrix& m) - { - } - TDynamicMatrix operator*(const TDynamicMatrix& m) - { - } +template +TVector TVector::operator*(const ValType& val) +{ + TVector out(Size, StartIndex); + for (int i = 0; i < Size; i++) + out.pVector[i] = pVector[i] * val; + return out; - // ввод/вывод - friend istream& operator>>(istream& istr, TDynamicMatrix& v) - { +} + +template +TVector TVector::operator+(const TVector& v) +{ + if ((Size != v.Size) || (StartIndex != v.StartIndex)) + throw string("Different size of TVector's"); + TVector out(Size, StartIndex); + for (int i = 0; i < Size; i++) + out.pVector[i] = pVector[i] + v.pVector[i]; + return out; +} + +template +TVector TVector::operator-(const TVector& v) +{ + if ((Size != v.Size) || (StartIndex != v.StartIndex)) + throw string("Different size of TVector's"); + TVector out(Size, StartIndex); + for (int i = 0; i < Size; i++) + out.pVector[i] = pVector[i] - v.pVector[i]; + return out; + +} + +template +ValType TVector::operator*(const TVector& v) +{ + if ((Size != v.Size) || (StartIndex != v.StartIndex)) + throw string("Different size of TVector's"); + ValType sum = 0; + for (int i = 0; i < Size; i++) + sum += pVector[i] * v.pVector[i]; + return sum; +} + + +//* ---Верхнетреугольная матрица--- +template +class TMatrix : public TVector > +{ + public: + TMatrix(int s = 10); // констуктор + TMatrix(const TMatrix& mt); // копирование + TMatrix(const TVector >& mt); // преобразование типа + bool operator==(const TMatrix& mt) const; // сравнение + bool operator!=(const TMatrix& mt) const; // сравнение + TMatrix& operator= (const TMatrix& mt); // присваивание + TMatrix operator+ (const TMatrix& mt); // сложение + TMatrix operator- (const TMatrix& mt); // вычитание + + friend istream& operator>>(istream& in, TMatrix& mt) //ввод + { + for (int i = 0; i < mt.Size; i++) + in >> mt.pVector[i]; + return in; + } + + friend ostream& operator<<(ostream& out, const TMatrix& mt) //вывод + { + for (int i = 0; i < mt.Size; i++) + out << mt.pVector[i] << endl; + return out; + } +}; + +template +TMatrix::TMatrix(int s) : TVector>(s) +{ + if ((s < 1) || (s > MAX_MATRIX_SIZE)) + throw string("Wrong argument"); + for (int i = 0; i < s; i++) { + TVector tmp(s - i, i); + this->pVector[i] = tmp; } - friend ostream& operator<<(ostream& ostr, const TDynamicMatrix& v) - { +} + +template +TMatrix::TMatrix(const TMatrix& mt) : + TVector >(mt) {} + +template +TMatrix::TMatrix(const TVector >& mt) : + TVector >(mt) {} + +template +bool TMatrix::operator==(const TMatrix& mt) const +{ + return TVector>:: operator==(mt); +} + +template +bool TMatrix::operator!=(const TMatrix& mt) const +{ + return !(*this == mt); +} + +template +TMatrix& TMatrix::operator=(const TMatrix& mt) +{ + if (this == &mt) + return *this; + TVector* tmp = new TVector[mt.Size]; + delete[] this->pVector; + this->pVector = tmp; + this->Size = mt.Size; + for (int i = 0; i < this->Size; i++) { + this->pVector[i] = mt.pVector[i]; } -}; + return *this; +} + +template +TMatrix TMatrix::operator+(const TMatrix& mt) +{ + return TVector>:: operator+(mt); +} + +template +TMatrix TMatrix::operator-(const TMatrix& mt) +{ + return TVector>:: operator-(mt); +} -#endif +#endif \ No newline at end of file diff --git a/new test 1.png b/new test 1.png new file mode 100644 index 000000000..c2fc5e41d Binary files /dev/null and b/new test 1.png differ diff --git a/new test 2.png b/new test 2.png new file mode 100644 index 000000000..147cadb30 Binary files /dev/null and b/new test 2.png differ diff --git a/samples/sample_matrix.cpp b/samples/sample_matrix.cpp index d6bc73330..663407c77 100644 --- a/samples/sample_matrix.cpp +++ b/samples/sample_matrix.cpp @@ -10,21 +10,29 @@ void main() { - TDynamicMatrix a(5), b(5), c(5); - int i, j; - setlocale(LC_ALL, "Russian"); - cout << "Тестирование класс работы с матрицами" - << endl; - for (i = 0; i < 5; i++) - for (j = i; j < 5; j++ ) - { - a[i][j] = i * 10 + j; - b[i][j] = (i * 10 + j) * 100; - } - c = a + b; - cout << "Matrix a = " << endl << a << endl; - cout << "Matrix b = " << endl << b << endl; - cout << "Matrix c = a + b" << endl << c << endl; + cout << "Тестирование программ поддержки представления треугольных матриц" << endl; + + int size; + cout << "Enter size of matrices: "; + cin >> size; + + TMatrix A(size), B(size); + cout << "Enter elements of matrix A (row by row):" << endl; + for (int i = 0; i < size; i++) + for (int j = i; j < size; ++j) + { + cin >> A[i][j]; + } + + cout << endl << "Enter elements of matrix B (row by row):" << endl; + for (int i = 0; i < size; i++) + for (int j = i; j < size; ++j) + { + cin >> B[i][j]; + } + + cout << "A+B:" << endl << A+B << endl; + cout << "A-B:" << endl << A-B << endl; } //--------------------------------------------------------------------------- diff --git a/test/test_tmatrix.cpp b/test/test_tmatrix.cpp index edaf75ef6..af0867177 100644 --- a/test/test_tmatrix.cpp +++ b/test/test_tmatrix.cpp @@ -1,111 +1,169 @@ -#include "tmatrix.h" - -#include - -TEST(TDynamicMatrix, can_create_matrix_with_positive_length) -{ - ASSERT_NO_THROW(TDynamicMatrix m(5)); -} - -TEST(TDynamicMatrix, cant_create_too_large_matrix) -{ - ASSERT_ANY_THROW(TDynamicMatrix m(MAX_MATRIX_SIZE + 1)); -} - -TEST(TDynamicMatrix, throws_when_create_matrix_with_negative_length) -{ - ASSERT_ANY_THROW(TDynamicMatrix m(-5)); -} - -TEST(TDynamicMatrix, can_create_copied_matrix) -{ - TDynamicMatrix m(5); - - ASSERT_NO_THROW(TDynamicMatrix m1(m)); -} - -TEST(TDynamicMatrix, copied_matrix_is_equal_to_source_one) -{ - ADD_FAILURE(); -} - -TEST(TDynamicMatrix, copied_matrix_has_its_own_memory) -{ - ADD_FAILURE(); -} - -TEST(TDynamicMatrix, can_get_size) -{ - ADD_FAILURE(); -} - -TEST(TDynamicMatrix, can_set_and_get_element) -{ - ADD_FAILURE(); -} - -TEST(TDynamicMatrix, throws_when_set_element_with_negative_index) -{ - ADD_FAILURE(); -} - -TEST(TDynamicMatrix, throws_when_set_element_with_too_large_index) -{ - ADD_FAILURE(); -} - -TEST(TDynamicMatrix, can_assign_matrix_to_itself) -{ - ADD_FAILURE(); -} - -TEST(TDynamicMatrix, can_assign_matrices_of_equal_size) -{ - ADD_FAILURE(); -} - -TEST(TDynamicMatrix, assign_operator_change_matrix_size) -{ - ADD_FAILURE(); -} - -TEST(TDynamicMatrix, can_assign_matrices_of_different_size) -{ - ADD_FAILURE(); -} - -TEST(TDynamicMatrix, compare_equal_matrices_return_true) -{ - ADD_FAILURE(); -} - -TEST(TDynamicMatrix, compare_matrix_with_itself_return_true) -{ - ADD_FAILURE(); -} - -TEST(TDynamicMatrix, matrices_with_different_size_are_not_equal) -{ - ADD_FAILURE(); -} - -TEST(TDynamicMatrix, can_add_matrices_with_equal_size) -{ - ADD_FAILURE(); -} - -TEST(TDynamicMatrix, cant_add_matrices_with_not_equal_size) -{ - ADD_FAILURE(); -} - -TEST(TDynamicMatrix, can_subtract_matrices_with_equal_size) -{ - ADD_FAILURE(); -} - -TEST(TDynamicMatrix, cant_subtract_matrixes_with_not_equal_size) -{ - ADD_FAILURE(); -} - +#include "tmatrix.h" +#include + +TEST(TMatrix, can_create_matrix_with_positive_length) +{ + ASSERT_NO_THROW(TMatrix m(5)); +} + +TEST(TMatrix, cant_create_too_large_matrix) +{ + ASSERT_ANY_THROW(TMatrix m(MAX_MATRIX_SIZE + 1)); +} + +TEST(TMatrix, throws_when_create_matrix_with_negative_length) +{ + ASSERT_ANY_THROW(TMatrix m(-5)); +} + +TEST(TMatrix, can_create_copied_matrix) +{ + TMatrix m(5); + + ASSERT_NO_THROW(TMatrix m1(m)); +} + +TEST(TMatrix, copied_matrix_is_equal_to_source_one) +{ + TMatrix m1(5); + m1[0][1] = 4; + TMatrix m2(m1); + + EXPECT_EQ(m1, m2); +} + +TEST(TMatrix, copied_matrix_has_its_own_memory) +{ + TMatrix m1(5); + TMatrix m2(5); + + EXPECT_NE(&m1[0][0], &m2[0][0]); +} +TEST(TMatrix, can_get_size) +{ + const int size = 5; + TMatrix m(size); + + EXPECT_EQ(size, m.GetSize()); +} + +TEST(TMatrix, can_set_and_get_element) +{ + TMatrix m(5); + int b = 10; + m[2][3] = b; + + EXPECT_EQ(b, m[2][3]); +} + +TEST(TMatrix, throws_when_set_element_with_negative_index) +{ + TMatrix m(5); + + ASSERT_ANY_THROW(m[-1][-1] = 1); +} + +TEST(TMatrix, throws_when_set_element_with_too_large_index) +{ + TMatrix m(5); + + ASSERT_ANY_THROW(m[5][6] = 1); +} + +TEST(TMatrix, can_assign_matrix_to_itself) +{ + TMatrix m1(5); + m1[0][0] = 5; + TMatrix m2(m1); + m1 = m1; + + EXPECT_EQ(m1, m2); +} + +TEST(TMatrix, can_assign_matrices_of_equal_size) +{ + TMatrix m1(5); + m1[0][0] = 5; + TMatrix m2(5); + m2 = m1; + + EXPECT_EQ(m1, m2); +} + +TEST(TMatrix, assign_operator_change_matrix_size) +{ + TMatrix m1(5); + m1[0][0] = 5; + TMatrix m2(4); + m2 = m1; + + EXPECT_EQ(5, m2.GetSize()); +} + +TEST(TMatrix, can_assign_matrices_of_different_size) +{ + TMatrix m1(5); + m1[0][0] = 5; + TMatrix m2(4); + m2 = m1; + + EXPECT_EQ(m1, m2); +} + +TEST(TMatrix, compare_equal_matrices_return_true) +{ + TMatrix m1(5), m2(5); + + EXPECT_EQ(true, m1 == m2); +} + +TEST(TMatrix, compare_matrix_with_itself_return_true) +{ + TMatrix m1(5); + + EXPECT_EQ(true, m1 == m1); +} + +TEST(TMatrix, matrices_with_different_size_are_not_equal) +{ + TMatrix m1(5), m2(4); + + EXPECT_EQ(false, m1 == m2); +} + +TEST(TMatrix, can_add_matrices_with_equal_size) +{ + TMatrix m1(5), m2(5), m3(5); + m1[0][0] = 5; + m2[0][0] = 5; + m3[0][0] = 10; + + EXPECT_EQ(m3, m1 + m2); +} + +TEST(TMatrix, cant_add_matrices_with_not_equal_size) +{ + TMatrix m1(5), m2(4); + m1[0][0] = 5; + m2[0][0] = 5; + + ASSERT_ANY_THROW(m1 + m2); +} + +TEST(TMatrix, can_subtract_matrices_with_equal_size) +{ + TMatrix m1(5), m2(5); + m1[0][0] = 10; + m2[0][0] = 5; + + EXPECT_EQ(m2, m1 - m2); +} + +TEST(TMatrix, cant_subtract_matrixes_with_not_equal_size) +{ + TMatrix m1(5), m2(4); + m1[0][0] = 5; + m2[0][0] = 5; + + ASSERT_ANY_THROW(m1 - m2); +} \ No newline at end of file diff --git a/test/test_tvector.cpp b/test/test_tvector.cpp index 1f7737755..a48fc9079 100644 --- a/test/test_tvector.cpp +++ b/test/test_tvector.cpp @@ -1,141 +1,227 @@ -#include "tmatrix.h" - -#include - -TEST(TDynamicVector, can_create_vector_with_positive_length) -{ - ASSERT_NO_THROW(TDynamicVector v(5)); -} - -TEST(TDynamicVector, cant_create_too_large_vector) -{ - ASSERT_ANY_THROW(TDynamicVector v(MAX_VECTOR_SIZE + 1)); -} - -TEST(TDynamicVector, throws_when_create_vector_with_negative_length) -{ - ASSERT_ANY_THROW(TDynamicVector v(-5)); -} - -TEST(TDynamicVector, can_create_copied_vector) -{ - TDynamicVector v(10); - - ASSERT_NO_THROW(TDynamicVector v1(v)); -} - -TEST(TDynamicVector, copied_vector_is_equal_to_source_one) -{ - ADD_FAILURE(); -} - -TEST(TDynamicVector, copied_vector_has_its_own_memory) -{ - ADD_FAILURE(); -} - -TEST(TDynamicVector, can_get_size) -{ - TDynamicVector v(4); - - EXPECT_EQ(4, v.size()); -} - -//TEST(TDynamicVector, can_set_and_get_element) -//{ -// TDynamicVector v(4); -// v[0] = 4; -// -// EXPECT_EQ(4, v[0]); -//} - -TEST(TDynamicVector, throws_when_set_element_with_negative_index) -{ - ADD_FAILURE(); -} - -TEST(TDynamicVector, throws_when_set_element_with_too_large_index) -{ - ADD_FAILURE(); -} - -TEST(TDynamicVector, can_assign_vector_to_itself) -{ - ADD_FAILURE(); -} - -TEST(TDynamicVector, can_assign_vectors_of_equal_size) -{ - ADD_FAILURE(); -} - -TEST(TDynamicVector, assign_operator_change_vector_size) -{ - ADD_FAILURE(); -} - -TEST(TDynamicVector, can_assign_vectors_of_different_size) -{ - ADD_FAILURE(); -} - -TEST(TDynamicVector, compare_equal_vectors_return_true) -{ - ADD_FAILURE(); -} - -TEST(TDynamicVector, compare_vector_with_itself_return_true) -{ - ADD_FAILURE(); -} - -TEST(TDynamicVector, vectors_with_different_size_are_not_equal) -{ - ADD_FAILURE(); -} - -TEST(TDynamicVector, can_add_scalar_to_vector) -{ - ADD_FAILURE(); -} - -TEST(TDynamicVector, can_subtract_scalar_from_vector) -{ - ADD_FAILURE(); -} - -TEST(TDynamicVector, can_multiply_scalar_by_vector) -{ - ADD_FAILURE(); -} - -TEST(TDynamicVector, can_add_vectors_with_equal_size) -{ - ADD_FAILURE(); -} - -TEST(TDynamicVector, cant_add_vectors_with_not_equal_size) -{ - ADD_FAILURE(); -} - -TEST(TDynamicVector, can_subtract_vectors_with_equal_size) -{ - ADD_FAILURE(); -} - -TEST(TDynamicVector, cant_subtract_vectors_with_not_equal_size) -{ - ADD_FAILURE(); -} - -TEST(TDynamicVector, can_multiply_vectors_with_equal_size) -{ - ADD_FAILURE(); -} - -TEST(TDynamicVector, cant_multiply_vectors_with_not_equal_size) -{ - ADD_FAILURE(); -} - +#include "tmatrix.h" +#include + +TEST(TVector, can_create_vector_with_positive_length) +{ + ASSERT_NO_THROW(TVector v(5)); +} + +TEST(TVector, cant_create_too_large_vector) +{ + ASSERT_ANY_THROW(TVector v(MAX_VECTOR_SIZE + 1)); +} + +TEST(TVector, throws_when_create_vector_with_negative_length) +{ + ASSERT_ANY_THROW(TVector v(-5)); +} + +TEST(TVector, throws_when_create_vector_with_negative_startindex) +{ + ASSERT_ANY_THROW(TVector v(5, -2)); +} + +TEST(TVector, can_create_copied_vector) +{ + TVector v(10); + + ASSERT_NO_THROW(TVector v1(v)); +} + +TEST(TVector, copied_vector_is_equal_to_source_one) +{ + TVector v1(4); + v1[0] = 2; + TVector v2(v1); + + EXPECT_EQ(v1, v2); +} + +TEST(TVector, copied_vector_has_its_own_memory) +{ + TVector v1(4); + TVector v2(v1); + + EXPECT_NE(&v1[0], &v2[0]); +} + +TEST(TVector, can_get_size) +{ + TVector v(4); + + EXPECT_EQ(4, v.GetSize()); +} + +TEST(TVector, can_get_start_index) +{ + TVector v(4, 2); + + EXPECT_EQ(2, v.GetStartIndex()); +} + +TEST(TVector, can_set_and_get_element) +{ + TVector v(4); + v[0] = 4; + + EXPECT_EQ(4, v[0]); +} + +TEST(TVector, throws_when_set_element_with_negative_index) +{ + TVector v(4); + + ASSERT_ANY_THROW(v[-1] = 1); +} + +TEST(TVector, throws_when_set_element_with_too_large_index) +{ + TVector v(4); + + ASSERT_ANY_THROW(v[4] = 1); +} + +TEST(TVector, can_assign_vector_to_itself) +{ + TVector v1(4); + TVector v2(v1); + v1 = v1; + + EXPECT_EQ(v2, v1); +} + +TEST(TVector, can_assign_vectors_of_equal_size) +{ + TVector v1(4), v2(4); + v1[0] = 4; + v2 = v1; + + EXPECT_EQ(v2, v1); +} + +TEST(TVector, assign_operator_change_vector_size) +{ + TVector v1(4), v2(3); + v1[0] = 4; + v2 = v1; + + EXPECT_EQ(4, v2.GetSize()); +} + +TEST(TVector, can_assign_vectors_of_different_size) +{ + TVector v1(4), v2(3); + v1[0] = 4; + v2 = v1; + + EXPECT_EQ(v2, v1); +} + +TEST(TVector, compare_equal_vectors_return_true) +{ + TVector v1(4); + v1[0] = 4; + TVector v2(v1); + + EXPECT_EQ(true, v1 == v2); +} + +TEST(TVector, compare_vector_with_itself_return_true) +{ + TVector v1(4); + v1[0] = 4; + + EXPECT_EQ(true, v1 == v1); +} + +TEST(TVector, vectors_with_different_size_are_not_equal) +{ + TVector v1(4), v2(3); + v1[0] = 4; + v2[0] = 4; + + EXPECT_NE(v2, v1); +} + +TEST(TVector, can_add_scalar_to_vector) +{ + TVector v1(4), v2(4); + v2 = v1 + 4; + for (int i = v1.GetStartIndex(); i < (v1.GetSize()) + v1.GetStartIndex(); i++) + v1[i] += 4; + + EXPECT_EQ(v2, v1); +} + +TEST(TVector, can_subtract_scalar_from_vector) +{ + TVector v1(4), v2(4); + v2 = v1 - 4; + for (int i = v1.GetStartIndex(); i < (v1.GetSize()) + v1.GetStartIndex(); i++) + v1[i] -= 4; + + EXPECT_EQ(v2, v1); +} + +TEST(TVector, can_multiply_scalar_by_vector) +{ + TVector v1(4), v2(4); + v1 = v1 + 4; + v2 = v1 * 4; + for (int i = v1.GetStartIndex(); i < (v1.GetSize()) + v1.GetStartIndex(); i++) + v1[i] *= 4; + + EXPECT_EQ(v2, v1); +} + +TEST(TVector, can_add_vectors_with_equal_size) +{ + TVector v1(4), v2(4), v3(4); + v1 = v1 + 1; + v2 = v2 + 2; + v3 = v3 + 3; + + EXPECT_EQ(v3, v1 + v2); +} + +TEST(TVector, cant_add_vectors_with_not_equal_size) +{ + TVector v1(4), v2(3); + + ASSERT_ANY_THROW(v1 + v2); +} + +TEST(TVector, can_subtract_vectors_with_equal_size) +{ + TVector v1(4), v2(4), v3(4); + v1 = v1 + 1; + v2 = v2 + 2; + v3 = v3 + 3; + + EXPECT_EQ(v2, v3 - v1); +} + +TEST(TVector, cant_subtract_vectors_with_not_equal_size) +{ + TVector v1(4), v2(3); + + ASSERT_ANY_THROW(v1 - v2); +} + +TEST(TVector, can_multiply_vectors_with_equal_size) +{ + TVector v1(4); + v1 = v1 + 1; + TVector v2(v1); + + EXPECT_EQ(4, v1 * v2); + +} + +TEST(TVector, cant_multiply_vectors_with_not_equal_size) +{ + TVector v1(4), v2(3); + + ASSERT_ANY_THROW(v1 * v2); +} +