#include
#include “Int17Matrix3D.h”

Int17Matrix3D::Int17Matrix3D(int x, int y, int z) : width_(x), height_(y), depth_(z), array_(new int32_t[kNumLenght_ * x * y * z]){}

Int17Matrix3D::Int17Matrix3D(const Int17Matrix3D& other) : width_(other.width_), height_(other.height_), depth_(other.depth_), array_(new int32_t[kNumLenght_ * width_ * height_ * depth_]) {
copy(array_, other.array_, kNumLenght_ * width_ * height_ * depth_);
}
Int17Matrix3D::~Int17Matrix3D() { delete[] array_; }

Int17Matrix3D& Int17Matrix3D::operator=(const Int17Matrix3D& other) {
if (this != &other) {
delete[] array_;
width_ = other.width_;
height_ = other.height_;
depth_ = other.depth_;
current_index_ = other.current_index_;
max_index_ = other.max_index_;
current_x_ = other.current_x_;
current_y_ = other.current_y_;
current_z_ = other.current_z_;
array_ = new int32_t[kNumLenght_ * width_ * height_ * depth_];
copy(array_, other.array_, kNumLenght_ * width_ * height_ * depth_);
}
return this;
}

Int17Matrix3D Int17Matrix3D::make_array(int x, int y, int z) {
return Int17Matrix3D(x, y, z);
}

Int17Matrix3D& Int17Matrix3D::operator=(int32_t value) {
int first_bit_index = current_index_ * kNumLenght_;

for (int i = 0; i < kNumLenght_; ++i) {
if (value & (1 << i)) {
SetBit(array_, first_bit_index + i);
} else {
ClearBit(array_, first_bit_index + i);
}
}
return this;
}

void Int17Matrix3D::copy(int32_t array1, int32_t array2, int size) {
for (int i = 0; i < size; ++i) {
array1[i] = array2[i];
}
}

void Int17Matrix3D::SetValueAtCurrentIndex(int32_t value) {
(this)[current_x_][current_y_][current_z_] = value;
}

bool Int17Matrix3D::TakeBit(const int32_t value, uint32_t bit_position) const {
int array_index = bit_position / 32;
int bit_index = bit_position % 32;
return ((value[array_index] >> bit_index) & 1) != 0;
}

Int17Matrix3D& Int17Matrix3D::ClearBit(int32_t* value, int32_t bit_position) {
value[bit_position / 32] &= ~(1 << (bit_position % 32));
return this;
}

Int17Matrix3D& Int17Matrix3D::SetBit(int32_t value, int32_t bit_position) {
value[bit_position / 32] |= (1 << (bit_position % 32));
return this;
}

int Int17Matrix3D::GetIndex(int x, int y, int z) const {
return x + y * (width_ * kNumLenght_) + z * (width_ * height_ * kNumLenght_);
}

int32_t Int17Matrix3D::ToDecimal(const int32_t array_, int32_t current_index_) const {
int first_bit_index = current_index_ * kNumLenght_;
int32_t decimal_value = 0;
int32_t exp = 1;

for (int i = 0; i < kNumLenght_; ++i) {
if (TakeBit(array_, first_bit_index + i)) {
decimal_value += exp;
}
exp <<= 1;
}

return decimal_value;
}

Int17Matrix3D operator+(const Int17Matrix3D& lhs, const Int17Matrix3D& rhs) {
if (lhs.width_ != rhs.width_ || lhs.height_ != rhs.height_ || lhs.depth_ != rhs.depth_) {
throw std::out_of_range(“Arrays must be with the same size”);
}

Int17Matrix3D result(lhs.width_, lhs.height_, lhs.depth_);

for (int z = 0; z < lhs.depth_; ++z) {
for (int y = 0; y < lhs.height_; ++y) {
for (int x = 0; x < lhs.width_; ++x) {
int index = lhs.GetIndex(x, y, z);

result[x][y][z] = lhs.ToDecimal(lhs.array_, index) + rhs.ToDecimal(rhs.array_, index);
}
}
}

return result;
}

Int17Matrix3D operator-(const Int17Matrix3D& lhs, const Int17Matrix3D& rhs) {
if (lhs.width_ != rhs.width_ || lhs.height_ != rhs.height_ || lhs.depth_ != rhs.depth_) {
throw std::out_of_range(“Arrays must be with the same size”);
}

Int17Matrix3D result(lhs.width_, lhs.height_, lhs.depth_);

for (int z = 0; z < lhs.depth_; ++z) {
for (int y = 0; y < lhs.height_; ++y) {
for (int x = 0; x < lhs.width_; ++x) {
int index = lhs.GetIndex(x, y, z);

result[x][y][z] = lhs.ToDecimal(lhs.array_, index) - rhs.ToDecimal(rhs.array_, index);
}
}
}

return result;
}

Int17Matrix3D operator*(const Int17Matrix3D& lhs, const int32_t& rhs) {
Int17Matrix3D result(lhs.width_, lhs.height_, lhs.depth_);

for (int z = 0; z < lhs.depth_; ++z) {
for (int y = 0; y < lhs.height_; ++y) {
for (int x = 0; x < lhs.width_; ++x) {
int index = lhs.GetIndex(x, y, z);

result[x][y][z] = lhs.ToDecimal(lhs.array_, index) * rhs;
}
}
}

return result;
}

bool operator==(const Int17Matrix3D& lhs, const Int17Matrix3D& rhs) {
return lhs.ToDecimal(lhs.array_, lhs.current_index_) == rhs.ToDecimal(rhs.array_, rhs.current_index_);
}

bool operator!=(const Int17Matrix3D& lhs, const Int17Matrix3D& rhs) {
return !(lhs == rhs);
}

std::ostream& operator<<(std::ostream& stream, const Int17Matrix3D& matrix) {
int32_t value = matrix.ToDecimal(matrix.array_, matrix.current_index_);
stream << value;
return stream;
}

std::istream& operator>>(std::istream& stream, Int17Matrix3D& array) {
int32_t value;
stream >> value;

if (!stream) return stream;

array.SetValueAtCurrentIndex(value);

return stream;
}

Int17Matrix3D::Proxy2D Int17Matrix3D::operator[](int x) {
if (x < 0 || x >= this->width_) {
throw std::out_of_range(“Index out of range in y-dimension”);
}
return Int17Matrix3D::Proxy2D(*this, x);
}

Int17Matrix3D::Proxy1D Int17Matrix3D::Proxy2D::operator[](int y) {
if (y < 0 || y >= parentMatrix.height_) {
throw std::out_of_range(“Index out of range in y-dimension”);
}
return Proxy1D(parentMatrix, x_, y);
}

int& Int17Matrix3D::Proxy1D::operator[](int z) {
if (z < 0 || z >= parentMatrix.depth_) {
throw std::out_of_range(“Index out of range in z-dimension”);
}
int index = parentMatrix.GetIndex(x_, y_, z);
return parentMatrix.array_[index];
}



#include <gtest/gtest.h>
#include <lib/Int17Matrix3D.h>

TEST(Int17Matrix3DTest, Constructor) {
Int17Matrix3D matrix(2, 2, 2);
ASSERT_NO_THROW();
}

TEST(Int17Matrix3DTest, make_array) {
Int17Matrix3D matrix = Int17Matrix3D::make_array(2, 2, 2);
ASSERT_NO_THROW();
}

TEST(Int17Matrix3DTest, CopyConstructor) {
Int17Matrix3D original(1, 2, 3);
ASSERT_NO_THROW(Int17Matrix3D copy = original);
}

TEST(Int17Matrix3DTest, ParenthesesOperator_EqTest) {
Int17Matrix3D matrix(3, 3, 3);
matrix[1][1][1] = 5;
ASSERT_EQ(matrix[1][1][1], 5);
}

TEST(Int17Matrix3DTest, ParenthesesOperator_NonEqTest) {
Int17Matrix3D matrix(3, 3, 3);
matrix[1][1][1] = 5;
ASSERT_NE(matrix[1][1][1], 7);
}

TEST(Int17Matrix3DTest, ParenthesesOperator_BoundsCheck) {
Int17Matrix3D matrix(2, 2, 2);
ASSERT_THROW(matrix[3][1][1] = 5, std::out_of_range);
ASSERT_THROW(matrix[1][3][1] = 5, std::out_of_range);
ASSERT_THROW(matrix[1][1][3] = 5, std::out_of_range);
}

TEST(Int17Matrix3DTest, EqualOperator_TwoEq) {
Int17Matrix3D arr1(1, 1, 1);
Int17Matrix3D arr2(2, 2, 2);
arr1[0][0][0] = 10;
arr2[0][0][0] = 10;
arr2[1][1][1] = 10;
ASSERT_EQ(arr1[0][0][0], arr2[0][0][0]);
ASSERT_EQ(arr1[0][0][0], arr2[1][1][1]);
}

TEST(Int17Matrix3DTest, EqualOperator_NonEq) {
Int17Matrix3D arr1(1, 1, 1);
Int17Matrix3D arr2(1, 1, 1);
arr1[0][0][0] = 1;
arr2[0][0][0] = 3;
ASSERT_NE(arr1[0][0][0], arr2[0][0][0]);
}

TEST(Int17Matrix3DTest, AssignmentOperator_NoThrow) {
Int17Matrix3D arr1(1, 2, 3);
Int17Matrix3D arr2(2, 2, 2);
ASSERT_NO_THROW(arr2 = arr1);
}

TEST(Int17Matrix3DTest, AssignmentOperator_Trow) {
Int17Matrix3D arr1(1, 2, 3);
Int17Matrix3D arr2(2, 2, 2);
arr2 = arr1;
ASSERT_THROW(arr2[1][1][1], std::out_of_range);
}

TEST(Int17Matrix3DTest, AssignmentOperator) {
Int17Matrix3D arr1(1, 2, 3);
Int17Matrix3D arr2(2, 2, 2);
arr1[0][1][2] = 5;
arr2[0][0][0] = 3;
arr2 = arr1;
ASSERT_EQ(arr2[0][1][2], 5);
ASSERT_NE(arr2[0][0][0], 3);
}

TEST(Int17Matrix3DTest, Addition_EmptyArrays) {
Int17Matrix3D arr1(1, 1, 1);
Int17Matrix3D arr2(1, 1, 1);
Int17Matrix3D arr3 = arr1 + arr2;
ASSERT_EQ(arr3[0][0][0], 0);
}

TEST(Int17Matrix3DTest, Addition_SingleCells) {
Int17Matrix3D arr1(1, 1, 1);
Int17Matrix3D arr2(1, 1, 1);
arr1[0][0][0] = 1;
arr2[0][0][0] = 2;
Int17Matrix3D arr3 = arr1 + arr2;
ASSERT_EQ(arr3[0][0][0], 3);
}

TEST(Int17Matrix3DTest, Addition_FewCells) {
Int17Matrix3D arr1(1, 1, 2);
Int17Matrix3D arr2(1, 1, 2);
arr1[0][0][1] = 1;
arr1[0][0][0] = 5;
arr2[0][0][0] = 2;
arr2[0][0][1] = 0;
Int17Matrix3D arr3 = arr1 + arr2;
ASSERT_EQ(arr3[0][0][0], 7);
ASSERT_EQ(arr3[0][0][1], 1);
}

TEST(Int17Matrix3DTest, Addition_DifferentSize) {
Int17Matrix3D arr1(1, 1, 1);
Int17Matrix3D arr2(1, 1, 2);
arr1[0][0][0] = 1;
arr2[0][0][0] = 2;
ASSERT_THROW(Int17Matrix3D arr3 = arr1 + arr2, std::out_of_range);
}

TEST(Int17Matrix3DTest, Substraction_EmptyArrays) {
Int17Matrix3D arr1(1, 1, 1);
Int17Matrix3D arr2(1, 1, 1);
Int17Matrix3D arr3 = arr1 - arr2;
ASSERT_EQ(arr3[0][0][0], 0);
}

TEST(Int17Matrix3DTest, Substraction_SingleCells) {
Int17Matrix3D arr1(1, 1, 1);
Int17Matrix3D arr2(1, 1, 1);
arr1[0][0][0] = 7;
arr2[0][0][0] = 2;
Int17Matrix3D arr3 = arr1 - arr2;
ASSERT_EQ(arr3[0][0][0], 5);
}

TEST(Int17Matrix3DTest, Substraction_FewCells) {
Int17Matrix3D arr1(1, 1, 2);
Int17Matrix3D arr2(1, 1, 2);
arr1[0][0][1] = 3;
arr1[0][0][0] = 5;
arr2[0][0][0] = 2;
Int17Matrix3D arr3 = arr1 - arr2;
ASSERT_EQ(arr3[0][0][0], 3);
ASSERT_EQ(arr3[0][0][1], 3);
}

TEST(Int17Matrix3DTest, Multiplication_ByZero) {
Int17Matrix3D arr1(2, 2, 2);
arr1[0][0][0] = 1;
arr1[0][0][1] = 2;
arr1[0][1][0] = 3;
arr1[0][1][1] = 4;
arr1[1][0][0] = 5;
arr1[1][0][1] = 6;
arr1[1][1][0] = 7;
arr1[1][1][1] = 8;
int mult = 0;
Int17Matrix3D arr3 = arr1 * mult;
ASSERT_EQ(arr3[0][0][0], 0);
ASSERT_EQ(arr3[0][0][1], 0);
ASSERT_EQ(arr3[0][1][0], 0);
ASSERT_EQ(arr3[0][1][1], 0);
ASSERT_EQ(arr3[1][0][0], 0);
ASSERT_EQ(arr3[1][0][1], 0);
ASSERT_EQ(arr3[1][1][0], 0);
ASSERT_EQ(arr3[1][1][1], 0);
}

TEST(Int17Matrix3DTest, Multiplication) {
Int17Matrix3D arr1(2, 2, 2);
Int17Matrix3D arr2 (2, 2, 2);
arr1[0][0][0] = 1;
arr1[0][0][1] = 2;
arr1[0][1][0] = 3;
arr1[0][1][1] = 4;
arr1[1][0][0] = 5;
arr1[1][0][1] = 6;
arr1[1][1][0] = 7;
arr1[1][1][1] = 8;
int mult = 10;
arr2 = arr1 * mult;
ASSERT_EQ(arr2[0][0][0], 10);
ASSERT_EQ(arr2[0][0][1], 20);
ASSERT_EQ(arr2[0][1][0], 30);
ASSERT_EQ(arr2[0][1][1], 40);
ASSERT_EQ(arr2[1][0][0], 50);
ASSERT_EQ(arr2[1][0][1], 60);
ASSERT_EQ(arr2[1][1][0], 70);
ASSERT_EQ(arr2[1][1][1], 80);
}



14: Test command: /Users/alex/labwork5-SPLOIT47/build/tests/Int17Matrix3D_test “–gtest_filter=Int17Matrix3DTest.Addition_FewCells” “–gtest_also_run_disabled_tests”
14: Working Directory: /Users/alex/labwork5-SPLOIT47/build/tests
14: Test timeout computed to be: 10000000
14: Running main() from /Users/alex/labwork5-SPLOIT47/build/_deps/googletest-src/googletest/src/gtest_main.cc
14: Note: Google Test filter = Int17Matrix3DTest.Addition_FewCells
14: [==========] Running 1 test from 1 test suite.
14: [----------] Global test environment set-up.
14: [----------] 1 test from Int17Matrix3DTest
14: [ RUN ] Int17Matrix3DTest.Addition_FewCells
14: /Users/alex/labwork5-SPLOIT47/tests/Int17Matrix3D_test.cpp:104: Failure
14: Expected equality of these values:
14: arr3[0][0][1]
14: Which is: 0
14: 1
14: [ FAILED ] Int17Matrix3DTest.Addition_FewCells (0 ms)
14: [----------] 1 test from Int17Matrix3DTest (0 ms total)
14:
14: [----------] Global test environment tear-down
14: [==========] 1 test from 1 test suite ran. (0 ms total)
14: [ PASSED ] 0 tests.
14: [ FAILED ] 1 test, listed below:
14: [ FAILED ] Int17Matrix3DTest.Addition_FewCells
14:
14: 1 FAILED TEST
14/20 Test #14: Int17Matrix3DTest.Addition_FewCells …***Failed 0.00 sec
test 15
Start 15: Int17Matrix3DTest.Addition_DifferentSize

15: Test command: /Users/alex/labwork5-SPLOIT47/build/tests/Int17Matrix3D_test “–gtest_filter=Int17Matrix3DTest.Addition_DifferentSize” “–gtest_also_run_disabled_tests”
15: Working Directory: /Users/alex/labwork5-SPLOIT47/build/tests
15: Test timeout computed to be: 10000000
15: Running main() from /Users/alex/labwork5-SPLOIT47/build/_deps/googletest-src/googletest/src/gtest_main.cc
15: Note: Google Test filter = Int17Matrix3DTest.Addition_DifferentSize
15: [==========] Running 1 test from 1 test suite.
15: [----------] Global test environment set-up.
15: [----------] 1 test from Int17Matrix3DTest
15: [ RUN ] Int17Matrix3DTest.Addition_DifferentSize
15: [ OK ] Int17Matrix3DTest.Addition_DifferentSize (0 ms)
15: [----------] 1 test from Int17Matrix3DTest (0 ms total)
15:
15: [----------] Global test environment tear-down
15: [==========] 1 test from 1 test suite ran. (0 ms total)
15: [ PASSED ] 1 test.
15/20 Test #15: Int17Matrix3DTest.Addition_DifferentSize … Passed 0.00 sec
test 16
Start 16: Int17Matrix3DTest.Substraction_EmptyArrays

16: Test command: /Users/alex/labwork5-SPLOIT47/build/tests/Int17Matrix3D_test “–gtest_filter=Int17Matrix3DTest.Substraction_EmptyArrays” “–gtest_also_run_disabled_tests”
16: Working Directory: /Users/alex/labwork5-SPLOIT47/build/tests
16: Test timeout computed to be: 10000000
16: Running main() from /Users/alex/labwork5-SPLOIT47/build/_deps/googletest-src/googletest/src/gtest_main.cc
16: Note: Google Test filter = Int17Matrix3DTest.Substraction_EmptyArrays
16: [==========] Running 1 test from 1 test suite.
16: [----------] Global test environment set-up.
16: [----------] 1 test from Int17Matrix3DTest
16: [ RUN ] Int17Matrix3DTest.Substraction_EmptyArrays
16: [ OK ] Int17Matrix3DTest.Substraction_EmptyArrays (0 ms)
16: [----------] 1 test from Int17Matrix3DTest (0 ms total)
16:
16: [----------] Global test environment tear-down
16: [==========] 1 test from 1 test suite ran. (0 ms total)
16: [ PASSED ] 1 test.
16/20 Test #16: Int17Matrix3DTest.Substraction_EmptyArrays … Passed 0.00 sec
test 17
Start 17: Int17Matrix3DTest.Substraction_SingleCells

17: Test command: /Users/alex/labwork5-SPLOIT47/build/tests/Int17Matrix3D_test “–gtest_filter=Int17Matrix3DTest.Substraction_SingleCells” “–gtest_also_run_disabled_tests”
17: Working Directory: /Users/alex/labwork5-SPLOIT47/build/tests
17: Test timeout computed to be: 10000000
17: Running main() from /Users/alex/labwork5-SPLOIT47/build/_deps/googletest-src/googletest/src/gtest_main.cc
17: Note: Google Test filter = Int17Matrix3DTest.Substraction_SingleCells
17: [==========] Running 1 test from 1 test suite.
17: [----------] Global test environment set-up.
17: [----------] 1 test from Int17Matrix3DTest
17: [ RUN ] Int17Matrix3DTest.Substraction_SingleCells
17: [ OK ] Int17Matrix3DTest.Substraction_SingleCells (0 ms)
17: [----------] 1 test from Int17Matrix3DTest (0 ms total)
17:
17: [----------] Global test environment tear-down
17: [==========] 1 test from 1 test suite ran. (0 ms total)
17: [ PASSED ] 1 test.
17/20 Test #17: Int17Matrix3DTest.Substraction_SingleCells … Passed 0.00 sec
test 18
Start 18: Int17Matrix3DTest.Substraction_FewCells

18: Test command: /Users/alex/labwork5-SPLOIT47/build/tests/Int17Matrix3D_test “–gtest_filter=Int17Matrix3DTest.Substraction_FewCells” “–gtest_also_run_disabled_tests”
18: Working Directory: /Users/alex/labwork5-SPLOIT47/build/tests
18: Test timeout computed to be: 10000000
18: Running main() from /Users/alex/labwork5-SPLOIT47/build/_deps/googletest-src/googletest/src/gtest_main.cc
18: Note: Google Test filter = Int17Matrix3DTest.Substraction_FewCells
18: [==========] Running 1 test from 1 test suite.
18: [----------] Global test environment set-up.
18: [----------] 1 test from Int17Matrix3DTest
18: [ RUN ] Int17Matrix3DTest.Substraction_FewCells
18: /Users/alex/labwork5-SPLOIT47/tests/Int17Matrix3D_test.cpp:139: Failure
18: Expected equality of these values:
18: arr3[0][0][1]
18: Which is: 0
18: 3
18: [ FAILED ] Int17Matrix3DTest.Substraction_FewCells (0 ms)
18: [----------] 1 test from Int17Matrix3DTest (0 ms total)
18:
18: [----------] Global test environment tear-down
18: [==========] 1 test from 1 test suite ran. (0 ms total)
18: [ PASSED ] 0 tests.
18: [ FAILED ] 1 test, listed below:
18: [ FAILED ] Int17Matrix3DTest.Substraction_FewCells
18:
18: 1 FAILED TEST
18/20 Test #18: Int17Matrix3DTest.Substraction_FewCells …***Failed 0.00 sec
test 19
Start 19: Int17Matrix3DTest.Multiplication_ByZero

19: Test command: /Users/alex/labwork5-SPLOIT47/build/tests/Int17Matrix3D_test “–gtest_filter=Int17Matrix3DTest.Multiplication_ByZero” “–gtest_also_run_disabled_tests”
19: Working Directory: /Users/alex/labwork5-SPLOIT47/build/tests
19: Test timeout computed to be: 10000000
19: Running main() from /Users/alex/labwork5-SPLOIT47/build/_deps/googletest-src/googletest/src/gtest_main.cc
19: Note: Google Test filter = Int17Matrix3DTest.Multiplication_ByZero
19: [==========] Running 1 test from 1 test suite.
19: [----------] Global test environment set-up.
19: [----------] 1 test from Int17Matrix3DTest
19: [ RUN ] Int17Matrix3DTest.Multiplication_ByZero
19: [ OK ] Int17Matrix3DTest.Multiplication_ByZero (0 ms)
19: [----------] 1 test from Int17Matrix3DTest (0 ms total)
19:
19: [----------] Global test environment tear-down
19: [==========] 1 test from 1 test suite ran. (0 ms total)
19: [ PASSED ] 1 test.
19/20 Test #19: Int17Matrix3DTest.Multiplication_ByZero … Passed 0.00 sec
test 20
Start 20: Int17Matrix3DTest.Multiplication

20: Test command: /Users/alex/labwork5-SPLOIT47/build/tests/Int17Matrix3D_test “–gtest_filter=Int17Matrix3DTest.Multiplication” “–gtest_also_run_disabled_tests”
20: Working Directory: /Users/alex/labwork5-SPLOIT47/build/tests
20: Test timeout computed to be: 10000000
20: Running main() from /Users/alex/labwork5-SPLOIT47/build/_deps/googletest-src/googletest/src/gtest_main.cc
20: Note: Google Test filter = Int17Matrix3DTest.Multiplication
20: [==========] Running 1 test from 1 test suite.
20: [----------] Global test environment set-up.
20: [----------] 1 test from Int17Matrix3DTest
20: [ RUN ] Int17Matrix3DTest.Multiplication
20: /Users/alex/labwork5-SPLOIT47/tests/Int17Matrix3D_test.cpp:178: Failure
20: Expected equality of these values:
20: arr2[0][0][1]
20: Which is: 0
20: 20
20: [ FAILED ] Int17Matrix3DTest.Multiplication (0 ms)
20: [----------] 1 test from Int17Matrix3DTest (0 ms total)
20:
20: [----------] Global test environment tear-down
20: [==========] 1 test from 1 test suite ran. (0 ms total)
20: [ PASSED ] 0 tests.
20: [ FAILED ] 1 test, listed below:
20: [ FAILED ] Int17Matrix3DTest.Multiplication
20:
20: 1 FAILED TEST
20/20 Test #20: Int17Matrix3DTest.Multiplication …***Failed 0.00 sec

85% tests passed, 3 tests failed out of 20

Total Test time (real) = 0.06 sec

The following tests FAILED:
14 - Int17Matrix3DTest.Addition_FewCells (Failed)
18 - Int17Matrix3DTest.Substraction_FewCells (Failed)
20 - Int17Matrix3DTest.Multiplication (Failed)


can u fix it