Skip to content

Commit

Permalink
TArray: add const back method + unittests.
Browse files Browse the repository at this point in the history
  • Loading branch information
kimkulling committed Dec 23, 2017
1 parent 7dee571 commit 6753b5f
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 4 deletions.
18 changes: 15 additions & 3 deletions include/cppcore/Container/TArray.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ class TArray {
/// @return The last item.
T &back();

/// @brief Returns the last item as a const reference.
/// @return The last item.
const T &back() const;

/// @brief Moves the items from the start- to the end-index.
/// @param startIdx [in] The start index.
/// @param endIdx [in] The last index.
Expand Down Expand Up @@ -308,6 +312,14 @@ T &TArray<T>::back() {
return ( m_pData[ m_Size-1 ] );
}

template<class T>
inline
const T &TArray<T>::back() const {
assert( m_Size > 0 );

return ( m_pData[ m_Size - 1 ] );
}

template<class T>
inline
void TArray<T>::move( size_t fromIdx, size_t toIdx ) {
Expand Down Expand Up @@ -511,13 +523,13 @@ TArray<T> &TArray<T>::operator = ( const TArray<T> &rOther ) {

template<class T>
inline
bool TArray<T>::operator == ( const TArray<T> &rOther ) const {
if ( rOther.m_Size != m_Size ) {
bool TArray<T>::operator == ( const TArray<T> &rhs ) const {
if ( rhs.m_Size != m_Size ) {
return false;
}

for( size_t i = 0; i<m_Size; ++i ) {
if ( m_pData[ i ] != rOther.m_pData[ i ] ) {
if ( m_pData[ i ] != rhs.m_pData[ i ] ) {
return false;
}
}
Expand Down
34 changes: 33 additions & 1 deletion test/container/TArrayTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ class TArrayTest : public testing::Test {
arrayInstance.add( pOrig[ i ] );
}
}

};

TEST_F( TArrayTest, constructTest ) {
Expand Down Expand Up @@ -103,6 +102,39 @@ TEST_F( TArrayTest, accessTest) {
EXPECT_EQ( 1.0f, arrayInstance[ 1 ] );
}

TEST_F( TArrayTest, backTest ) {
float item( 0.0f );
TArray<float> arrayInstance;
arrayInstance.add( 1.0f );
item = arrayInstance.back();
EXPECT_FLOAT_EQ( 1.0f, item );

arrayInstance.add( 2.0f );
item = arrayInstance.back();
EXPECT_FLOAT_EQ( 2.0f, item );
}

struct foo {
float m_item;
foo() : m_item( 1.0f ) {}
};

TEST_F( TArrayTest, constBackTest ) {
TArray<foo> fooArrayInstance;
foo foo1;
foo1.m_item = 1.0f;
fooArrayInstance.add( foo1 );
const foo &res1 = fooArrayInstance.back();
EXPECT_FLOAT_EQ( 1.0f, res1.m_item );

foo foo2;
foo2.m_item = 2.0f;
fooArrayInstance.add( foo2 );
const foo &res2 = fooArrayInstance.back();
EXPECT_FLOAT_EQ( 2.0f, res2.m_item );

}

TEST_F( TArrayTest, removeTest) {
TArray<float> arrayInstance;
createArray( ArrayData, ArraySize, arrayInstance );
Expand Down

0 comments on commit 6753b5f

Please sign in to comment.