-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added util_test project and util::foreach tests.
- Loading branch information
Showing
2 changed files
with
55 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |