Skip to content

Commit

Permalink
Added util_test project and util::foreach tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
zhenliang committed May 16, 2011
1 parent edd8ba4 commit 611a87a
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
48 changes: 48 additions & 0 deletions util_test/foreach.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include <numeric>
#include <vector>

#include <util/container.h>

#define TEST_SUITE UtilContainer

TEST(Foreach)
{
// 测试 UTIL_FOREACH 是否能够遍历一个容器

std::vector<int> vec;
vec.resize(99, 1); // 99 个值为 1 的元素

// 统计执行的次数并累加各个元素值

int cnt = 0;
int sum = 0;
UTIL_FOREACH(i, vec, std::vector<int>)
{
sum += *i;
++cnt;
}

CHECK(cnt == vec.size());
CHECK(sum == std::accumulate(vec.begin(), vec.end(), 0));
}

TEST(ConstForeach)
{
// 测试 UTIL_CONST_FOREACH 是否能够遍历一个容器

std::vector<int> vec;
vec.resize(99, 1); // 99 个值为 1 的元素

// 统计执行的次数并累加各个元素值

int cnt = 0;
int sum = 0;
UTIL_CONST_FOREACH(i, vec, std::vector<int>)
{
sum += *i;
++cnt;
}

CHECK(cnt == vec.size());
CHECK(sum == std::accumulate(vec.begin(), vec.end(), 0));
}
7 changes: 7 additions & 0 deletions util_test/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

int main()
{
std::cout << "Testing done." <<std::endl;
std::cin.ignore();
return 0;
}

0 comments on commit 611a87a

Please sign in to comment.