Skip to content

Commit

Permalink
Add unit tests for random_queue.
Browse files Browse the repository at this point in the history
  • Loading branch information
tatsy committed Feb 8, 2015
1 parent bc8df29 commit 1882def
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/core/random_queue_detail.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ Ty random_queue<Ty>::pop() {
Random rand = Random::getRNG();
int i = rand.randInt(static_cast<int>(ptr));
ptr--;
swap(data[i], data[ptr]);
std::swap(data[i], data[ptr]);
return data[ptr];
}

Expand Down
14 changes: 13 additions & 1 deletion tests/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,19 @@ target_link_libraries(test_random ${GTEST_MAIN_LIBRARY})
target_link_libraries(test_random ${OpenCV_LIBS})

add_test(NAME test_random COMMAND test_random)
add_custom_target(check COMMAND ${CMAKE_CTEST_COMMAND} DEPENDS test_point test_random)

# Test for random_queue
set(RANDOM_QUEUE_SOURCE_FILES test_random_queue.cpp)

add_executable(test_random_queue ${RANDOM_QUEUE_SOURCE_FILES})
target_link_libraries(test_random_queue ${GTEST_LIBRARY})
target_link_libraries(test_random_queue ${GTEST_MAIN_LIBRARY})
target_link_libraries(test_random_queue ${OpenCV_LIBS})

add_test(NAME test_random_queue COMMAND test_random_queue)

# Add tests to "make check"
add_custom_target(check COMMAND ${CMAKE_CTEST_COMMAND} DEPENDS test_point test_random test_random_queue)

# Include directories
include_directories(${CMAKE_CURRENT_LIST_DIR})
Expand Down
57 changes: 57 additions & 0 deletions tests/core/test_random_queue.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/******************************************************************************
Copyright 2015 Tatsuya Yatagawa (tatsy)
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
******************************************************************************/

#include "gtest/gtest.h"

#include "../../include/lime.hpp"
using lime::random_queue;

static const int nTest = 1 << 20;

class RandomQueueTest : public ::testing::Test {
protected:
virtual void setUp() {
}

random_queue<int> que;
};

TEST_F(RandomQueueTest, InEmpty) {
EXPECT_TRUE(que.empty());
}

TEST_F(RandomQueueTest, EnqueueDequeueWorks) {
for (int i = 0; i < nTest; i++) {
EXPECT_EQ(que.size(), i);
que.push(i);
}
EXPECT_FALSE(que.empty());
for (int i = 0; i < nTest; i++) {
EXPECT_EQ(que.size(), nTest - i);
que.pop();
}
EXPECT_TRUE(que.empty());
}

int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

0 comments on commit 1882def

Please sign in to comment.