Skip to content

Commit 1fed884

Browse files
Add unit test for the lazyt class
This tests that the force function returns the expected result and the evalution function is not called more than necessary.
1 parent 087946b commit 1fed884

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

unit/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ SRC += analyses/ai/ai.cpp \
7474
util/irep_sharing.cpp \
7575
util/json_array.cpp \
7676
util/json_object.cpp \
77+
util/lazy.cpp \
7778
util/memory_info.cpp \
7879
util/message.cpp \
7980
util/optional.cpp \

unit/util/lazy.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*******************************************************************\
2+
3+
Module: Unit tests for lazy
4+
5+
Author: Romain Brenguier, [email protected]
6+
7+
\*******************************************************************/
8+
9+
#include <testing-utils/use_catch.h>
10+
#include <util/lazy.h>
11+
12+
SCENARIO("lazy test", "[core][util][lazy]")
13+
{
14+
std::size_t call_counter = 0;
15+
auto lazy_length = lazyt<int, const std::string &>::from_fun(
16+
[&call_counter](const std::string &s) {
17+
++call_counter;
18+
return s.length();
19+
});
20+
21+
REQUIRE(call_counter == 0);
22+
auto result = lazy_length.force("foo");
23+
REQUIRE(call_counter == 1);
24+
REQUIRE(result == 3);
25+
result = lazy_length.force("foo");
26+
REQUIRE(call_counter == 1);
27+
REQUIRE(result == 3);
28+
}

0 commit comments

Comments
 (0)