#pragma once #include <iostream> class Array{ public: Array(sizet x, sizet y, size_t z);…
- User
#pragma once #include
class Array{
public:
Array(size_t x, size_t y, size_t z); ~Array(); Array& operator=(const Array& other); static Array MakeArray(size_t x, size_t y, size_t z); int GetIndex(size_t x, size_t y, size_t z) const; int GetCurX() const; int GetCurY() const; int GetCurZ() const; int GetX() const; int GetY() const; int GetZ() const; void StoreElement(size_t x, size_t y, size_t z, uint32_t number); uint32_t GetElement(size_t x, size_t y, size_t z) const; Array& operator[](size_t index); Array& operator=(uint32_t x); friend std::ostream& operator<<(std::ostream& stream, const Array& val); friend std::istream& operator>>(std::istream& stream, Array& val); Array operator*(uint32_t num); friend Array operator-(const Array& lhs, const Array& rhs); friend Array operator+(const Array& lhs, const Array& rhs);private:
uint32_t* arr_; size_t x_; size_t y_; size_t z_; size_t current_x_; size_t current_y_; size_t current_z_;}; #include "/Users/alishashelby/Desktop/labwork5-alishashelby/lib/3D17BitArray.h"
Array::Array(size_t x, size_t y, size_t z) { x_ = x; y_ = y; z_ = z; arr_ = new uint32_t[x * y * z]; for (size_t i = 0; i < x * y * z; ++i) { arr_[i] = 0; } }
Array::~Array() { delete[] arr_; }
Array& Array::operator=(const Array& other) { delete[] arr_; x_ = other.x_; y_ = other.y_; z_ = other.z_; arr_ = new uint32_t[x_ * y_ * z_]; for (size_t i = 0; i < x_ * y_ * z_; ++i) { arr_[i] = other.arr_[i]; }
return *this;}
Array Array::MakeArray(size_t x, size_t y, size_t z) { return Array(x, y, z); }
int Array::GetIndex(size_t x, size_t y, size_t z) const { return ((x * y_ + y) * z_ + z) * 17; }
int Array::GetCurX() const { return current_x_; }
int Array::GetCurY() const { return current_y_; }
int Array::GetCurZ() const { return current_z_; }
int Array::GetX() const { return x_; }
int Array::GetY() const { return y_; }
int Array::GetZ() const { return z_; }
void Array::StoreElement(size_t x, size_t y, size_t z, uint32_t number) { size_t bit_start = GetIndex(x, y, z); //std::cout << bit_start<< '\n'; for (int i = 0; i < 17; ++i) { if ((number >> i) & 1) { arr_[bit_start / 32] |= (1 << bit_start % 32); } else { arr_[bit_start / 32] &= ~(1 << bit_start % 32); } ++bit_start; } }
uint32_t Array::GetElement(size_t x, size_t y, size_t z) const { size_t bit_start = GetIndex(x, y, z); uint32_t value = 0; for (int i = 0; i < 17; ++i) { if (arr_[bit_start / 32] & (1 << (bit_start % 32))) { value |= (1 << i); } ++bit_start; }
return value;}
Array& Array::operator[](size_t index) { static int operator_count = 0; if (operator_count == 0) { current_x_ = index; } if (operator_count == 1) { current_y_ = index; } if (operator_count == 2) { current_z_ = index; operator_count = -1; } ++operator_count;
- Assistant
- User
#include "/Users/alishashelby/Desktop/labwork5-alishashelby/lib/3D17BitArray.h" #include <gtest/gtest.h> #include
class ArrayTest : public testing::Test {
protected: Array* arr; ArrayTest() {} ~ArrayTest() override {} void SetUp() override { arr = new Array::MakeArray(10, 2, 30); } void TearDown() override { //delete arr; }};
TEST_F(ArrayTest, SetAndGetElement) { arr[4][1][2] = 100500; uint32_t element = arr->GetElement(4, 1, 2); EXPECT_EQ(element, 100500); }
int main(int argc, char **argv) { testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); } why it doesn't work
- Assistant
The issue lies in how you're trying to instantiate the
Arrayobject and access it in your test. The way you are using bracket operators onarrwill not work as you intend.Here's why it doesn't work and how you can fix it:
-
In
SetUp(), you're trying to assignnew Array::MakeArray(10, 2, 30)toarr. However,MakeArrayis a static method that returns anArrayobject by value. You shouldn't use here. Instead, you can directly instantiate your without using , like this:
-
- User
Implement other tests
- Assistant
- User
I have void Array::StoreElement(size_t x, size_t y, size_t z, uint32_t number) { if (x >= x_ || y >= y_ || z >= z_) { std::cerr << "--array size out of range"; std::exit(EXIT_FAILURE); } size_t bit_start = GetIndex(x, y, z); //std::cout << bit_start<< '\n'; for (int i = 0; i < 17; ++i) { if ((number >> i) & 1) { arr_[bit_start / 32] |= (1 << bit_start % 32); } else { arr_[bit_start / 32] &= ~(1 << bit_start % 32); } ++bit_start; } }, fix TEST_F(ArrayTest, StoreAndGetElementIllegalIndices) { // Testing outside the bounds should throw an exception or otherwise be handled. // This will depend on how you manage out-of-bounds in your Array implementation. // For example, you might expect a std::out_of_range exception or an assert failure. // Here we assume it throws: EXPECT_THROW(arr->StoreElement(10, 2, 30, 123), std::out_of_range); EXPECT_THROW(arr->GetElement(10, 2, 30), std::out_of_range); }
- Assistant
Your
Array::StoreElementmethod usesstd::exit(EXIT_FAILURE)upon encountering an out-of-bounds index, which exits the entire program. GoogleTest provides a mechanism to test these sorts of program exits using death tests.To test that your method causes the program to exit with an error due to out-of-bounds access, you would use the
EXPECT_EXITGoogleTest macro as follows:TEST_F(ArrayTest, StoreElementOutOfRangeExitsProgram) { // EXPECT_EXIT(statement, exit_predicate, expected_message); EXPECT_EXIT( arr->StoreElement(10, 2, 30, 123), ::testing::ExitedWithCode(EXIT_FAILURE), "--array size out of range" ); } // If GetElement has a similar out-of-range check and causes program exit, you can test it too TEST_F(ArrayTest, GetElementOutOfRangeExitsProgram) { EXPECT_EXIT( arr->GetElement(10, 2, 30), ::testing::ExitedWithCode(EXIT_FAILURE), "--array size out of range" ); } - User
- Assistant
Based on the code you provided, there is a small syntax error in the expected messages for the out-of-range tests. The error is caused by the usage of fancy quotes “ and ” instead of the regular straight double quotes ". This can happen due to text formatting in certain editors or word processors that automatically convert straight quotes into typographic ones.
Here's how you should correct the
StoreElementOutOfRangeExitsProgramandGetElementOutOfRangeExitsProgramtests:TEST_F(ArrayTest, StoreElementOutOfRangeExitsProgram) { EXPECT_EXIT( arr->StoreElement(10, 2, 30, 123), ::testing::ExitedWithCode(EXIT_FAILURE), "--array size out of range" // Use straight quotes ); } TEST_F(ArrayTest, GetElementOutOfRangeExitsProgram) { EXPECT_EXIT( arr->GetElement(10, 2, 30), ::testing::ExitedWithCode(EXIT_FAILURE), "--array size out of range" // Use straight quotes ); } - User
- Assistant
The error message you are encountering, which says
EXPECT_EXITis undefined, suggests there might be an issue with the GoogleTest framework not being correctly included in your project, or the version of GoogleTest you're using may not defineEXPECT_EXIT.However,
EXPECT_EXITis part of the standard set of macros provided by GoogleTest for writing death tests. Ensure you have the following: - User
include(FetchContent)
FetchContent_Declare( googletest GIT_REPOSITORY https://github.com/google/googletest.git GIT_TAG release-1.12.1 )
For Windows: Prevent overriding the parent project's compiler/linker settings
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE) FetchContent_MakeAvailable(googletest)
enable_testing()
add_executable( 3D17BitArray_tests 3D17BitArray_test.cpp )
target_link_libraries( 3D17BitArray_tests 3D17BitArray GTest::gtest_main )
target_include_directories(3D17BitArray_tests PUBLIC ${PROJECT_SOURCE_DIR})
include(GoogleTest)
gtest_discover_tests(3D17BitArray_tests) what should I add to my make in order to my death tests will work
- Assistant
- User
- Assistant
The failures you're seeing in the death tests are related to the actual error messages not matching the expected ones.
- User
EXPECT_EXIT(arr->StoreElement(10, 2, 30, 123), testing::ExitedWithCode(EXIT_FAILURE), "--array size out of range"); count symbols
- Assistant
The macro uses a string to match against the standard error output of the test statement that causes the program to exit. With the expectation string "--array size out of range", the macro will look for a standard error output that contains exactly this string.
- User
- Assistant
- User
TEST_F(ArrayDeathTest, SubtractionOperator) { Array other(10, 2, 30); // To cause the death test to trigger, you must call a function that exits. // For example, if subtracting two arrays of different sizes would cause an exit, // you’ll do something like this: EXPECT_EXIT({ Array faultyArray(5, 5, 5); // Incorrect size faultyArray - *arr; // This operation should exit }, testing::ExitedWithCode(EXIT_FAILURE), "–-array sizes do not match"); } it returns Death test: { Array faultyArray(5, 5, 5); faultyArray - *arr; } 3: Result: died but not with expected error. 3: Expected: contains regular expression "\xE2\x80\x93-array sizes do not match" 3: As Text: "–-array sizes do not match" 3: Actual msg: 3: [ DEATH ] --array sizes do not match 3: [ FAILED ] ArrayDeathTest.SubtractionOperator (3 ms) 3: [----------] 1 test from ArrayDeathTest (3 ms total) 3: 3: [----------] Global test environment tear-down 3: [==========] 1 test from 1 test suite ran. (3 ms total) 3: [ PASSED ] 0 tests. 3: [ FAILED ] 1 test, listed below: 3: [ FAILED ] ArrayDeathTest.SubtractionOperator 3: 3: 1 FAILED TEST
- Assistant
The issue with your test is again related to the expected error message string. The error message shows a mismatch due to the inclusion of the character sequence
\xE2\x80\x93, which represents an en dash character (–) in UTF-8 encoding. However, the actual message from your program likely contains two hyphen-minus characters (--) instead of an en dash.Here's the proper way to write the expected message string:
TEST_F(ArrayDeathTest, SubtractionOperator) { Array other(10, 2, 30); EXPECT_EXIT({ Array faultyArray(5, 5, 5); // Incorrect size faultyArray - *arr; // This operation should exit }, testing::ExitedWithCode(EXIT_FAILURE), "--array sizes do not match"); // Use regular hyphens (--). }Please notice the correction to the expected message, where we use two regular hyphen-minus characters (--) instead of an en dash.
The expected regular expression string should match exactly what's being printed to the standard error by your class methods when an error occurs. Be sure to use regular ASCII characters in your test expectations, and ensure they align exactly with what your class outputs.
- User
Array Array::operator*(uint32_t num) { Array new_arr(x_, y_, z_); for (size_t i = 0; i < x_; ++i) { for (size_t j = 0; j < y_; ++j) { for (size_t k = 0; k < z_; ++k) { uint32_t temp = (*this).GetElement(i, j, k); new_arr.StoreElement(i, j, k, temp * num); } } } return new_arr; } Array result = 15 * (*arr); doesn't work
- Assistant
In C++, operator overloading works based on the left-hand side of the operation. The syntax
Array result = 15 * (*arr);looks for anoperator*that is a member function of the type of the left-hand side operand—in this case, the integer literal15, which obviously cannot have member functions.The
Array::operator*you defined is a non-static member function that expects theArrayobject it is called on to be the left-hand operand and auint32_tvalue as the right-hand operand. However, there's no suchoperator*that would be called when the integer is on the left and theArrayis on the right.To allow multiplication of an by an integer with the integer on the left-hand side, you would define a non-member operator function:
- User
implement universal operator*
- Assistant
To implement a universal
operator*that allows multiplication with an object on either the left-hand or the right-hand side of the multiplication, you would define two overloads: one as a member function for when the is on the left-hand side, and one as a non-member function for when the integer is on the left-hand side. - User
TEST_F(ArrayTest, OstreamOperator) { std::stringstream stream; stream << "0 0 0 45"; stream >> *arr; stream << "9 1 29 1005"; stream >> *arr; stream << "5 0 5 99"; stream >> *arr; uint32_t value1 = arr->GetElement(0, 0, 0); uint32_t value2 = arr->GetElement(9, 1, 29); uint32_t value3 = arr->GetElement(5, 0, 5);
EXPECT_EQ(value1, 45); EXPECT_EQ(value2, 1005); EXPECT_EQ(value3, 99);} it returns Start 14: ArrayTest.OstreamOperator
14: Test command: /Users/alishashelby/Desktop/labwork5-alishashelby/build/tests/3D17BitArray_tests "--gtest_filter=ArrayTest.OstreamOperator" "--gtest_also_run_disabled_tests" 14: Working Directory: /Users/alishashelby/Desktop/labwork5-alishashelby/build/tests 14: Test timeout computed to be: 10000000 14: Note: Google Test filter = ArrayTest.OstreamOperator 14: [==========] Running 1 test from 1 test suite. 14: [----------] Global test environment set-up. 14: [----------] 1 test from ArrayTest 14: [ RUN ] ArrayTest.OstreamOperator 14: --array size out of range 14/15 Test #14: ArrayTest.OstreamOperator ..........................***Failed 0.00 sec test 15
- Assistant
The failure you're encountering in the
ArrayTest.OstreamOperatortest is indicative of a problem where the setup of yourArrayor the handling withinstd::istream& operator>>(std::istream& stream, Array& val)results in a "--array size out of range" message.The test is named , but it actually tests the input stream . Based on the operator implementation in your original code example, the operator reads the indices and the value from the stream and then tries to store the element using .
- User
TEST_F(ArrayTest, OstreamOperator) { std::stringstream stream; char* output; (*arr)[1][1][16] = 34; stream << *arr; output = stream.get(); (*arr)[2][0][7] = 11; (arr)[9][1][29] = 400; std::getline(stream, output); EXPECT_EQ(output, "12345"); } use char
- Assistant
The code you've provided has several issues that need to be addressed to make it work and to test the expected output correctly:
-
std::stringstream::get()doesn't return achar*, but a singlechar. If the intention is to read into a , you would need to use method, but it's not a typical use case for streams in C++ and you would need to manage the memory allocation for yourself, which is not ideal.
-
- User
std::istream& operator>>(std::istream& stream, Array& val) { uint32_t value; stream >> value; val.StoreElement(, y, z, value);
return stream;} implement
- Assistant
The
operator>>forstd::istreamneeds to extract and store all necessary values to determine the correct location in theArrayand theuint32_tvalue to be stored. To fully implement this operator, you need to read from the stream all indices (, , ) and the corresponding . - User
TEST_F(ArrayTest, OstreamOperator) { std::stringstream stream; std::string output; (*arr)[1][1][16] = 34; stream << *arr << " "; (*arr)[2][0][7] = 11; stream << *arr << " "; (*arr)[9][1][29] = 400; stream << *arr; std::getline(stream, output);
EXPECT_EQ(output, "34 11 400");} do not use std::string
- Assistant
If you do not wish to use
std::stringto store the output from the stream, another way to extract text from astringstreamis to use the stream'sstrmethod, which returns the underlyingstd::stringrepresentation of the stream contents. Subsequently, you can usec_str()to obtain aconst char*pointer to the C-string equivalent.Here's how you could modify your test:
TEST_F(ArrayTest, OstreamOperator) { std::stringstream stream; (*arr)[1][1][16] = 34; // Make sure these indices and the ones below are within the bounds of your array stream << (*arr).GetElement(1, 1, 16) << " "; (*arr)[2][0][7] = 11; stream << (*arr).GetElement(2, 0, 7) << " "; (*arr)[9][1][29] = 400; stream << (*arr).GetElement(9, 1, 29); const std::string temp = stream.str(); const char* output = temp.c_str(); // You can now compare output to the expected literal string EXPECT_STREQ(output, "34 11 400"); }