Skip to content

Fix initialization of TStaticArray #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 21 additions & 11 deletions include/cppcore/Container/TStaticArray.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,33 +39,41 @@ template<class T, size_t len>
class TStaticArray {
public:
TStaticArray();
TStaticArray(T initValue);
TStaticArray(const TStaticArray<T, len> &rhs);
~TStaticArray();
void clear();
size_t size() const;
void set(size_t index, T value);
void memset(T value);
T operator[](size_t index) const;
T &operator[](size_t index);
bool operator == (const TStaticArray<T, len> &rhs) const;
TStaticArray<T, len> &operator = (const TStaticArray<T, len> &rhs);

private:
T m_array[len];
size_t m_len;
size_t m_len;
};

template <class T, size_t len>
template<class T, size_t len>
inline
TStaticArray<T,len>::TStaticArray()
: m_len(len) {
clear();
// empty
}

template<class T, size_t len>
inline
TStaticArray<T, len>::TStaticArray( T initValue ) :
m_len(len) {
memset(initValue);
}

template <class T, size_t len>
inline
TStaticArray<T, len>::TStaticArray(const TStaticArray<T, len> &rhs)
: m_len(rhs.m_len) {
for (size_t i = 0; i < m_len; ++i) {
for (size_t i = 0; i < m_len; ++i) {
m_array[i] = rhs.m_array[i];
}
}
Expand All @@ -76,12 +84,6 @@ TStaticArray<T, len>::~TStaticArray() {
// empty
}

template<class T, size_t len>
inline
void TStaticArray<T, len>::clear() {
::memset(m_array, 0, sizeof(T) * m_len);
}

template<class T, size_t len>
inline
void TStaticArray<T, len>::set(size_t index, T value) {
Expand All @@ -90,6 +92,14 @@ void TStaticArray<T, len>::set(size_t index, T value) {
m_array[index] = value;
}

template<class T, size_t len>
inline
void TStaticArray<T, len>::memset( T value ) {
for ( size_t i=0; i<m_len; ++i ) {
m_array[i] = value;
}
}

template<class T, size_t len>
inline
size_t TStaticArray<T, len>::size() const {
Expand Down
5 changes: 5 additions & 0 deletions test/container/TArrayTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,14 @@ TEST_F( TArrayTest, constructTest ) {
// Just checks how to initialize the array
TEST_F( TArrayTest, constructWithSizeTest) {
TArray<float> arrayInstance( 4 );
arrayInstance[0] = 0.0f;
arrayInstance[1] = 1.0f;
arrayInstance[2] = 2.0f;
arrayInstance[3] = 3.0f;
EXPECT_EQ( 4u, arrayInstance.size() );
for ( size_t i=0; i<4; ++i ) {
const float f = arrayInstance[i];
EXPECT_EQ( (float) i, f );
}
}

Expand Down
6 changes: 3 additions & 3 deletions test/container/TStaticArrayTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class TStaticArrayTest : public testing::Test {
};

TEST_F(TStaticArrayTest, constructTest) {
TStaticArray<int, 4> arr;
TStaticArray<int, 4> arr(0);
EXPECT_EQ(4u, arr.size());
EXPECT_EQ(0, arr[0]);
EXPECT_EQ(0, arr[3]);
Expand All @@ -62,14 +62,14 @@ TEST_F(TStaticArrayTest, clear_Test) {
for (size_t i = 0; i < 4; ++i) {
arr[i] = i;
}
arr.clear();
arr.memset( 0 );
for (size_t i = 0; i < 4; ++i) {
EXPECT_EQ(0, arr[i]);
}
}

TEST_F(TStaticArrayTest, access_string_Test) {
TStaticArray < std::string, 4> arr;
TStaticArray <std::string, 4> arr;
arr[0] = std::string("bla");
arr[1] = std::string("bla");
arr[2] = std::string("bla");
Expand Down