1+ #include < gtest/gtest.h>
2+
13#include < thread_pool/fixed_function.hpp>
2- #include < test.hpp>
34
45#include < string>
5- #include < cassert>
66#include < type_traits>
77#include < functional>
88
9- using namespace tp ;
10-
119int test_free_func (int i)
1210{
1311 return i;
@@ -25,10 +23,12 @@ void test_void(int &p, int v)
2523}
2624
2725struct A {
28- int b (const int &p) {
26+ int b (const int &p)
27+ {
2928 return p;
3029 }
31- void c (int &i) {
30+ void c (int &i)
31+ {
3232 i = 43 ;
3333 }
3434};
@@ -37,140 +37,153 @@ template <typename T>
3737struct Foo
3838{
3939 template <typename U>
40- U bar (U p) {
40+ U bar (U p)
41+ {
4142 return p + payload;
4243 }
4344
4445 T payload;
4546};
4647
4748template <typename T>
48- void print_overhead () {
49- using func_type = FixedFunction<void (), sizeof (T)>;
49+ void print_overhead ()
50+ {
51+ using func_type = tp::FixedFunction<void (), sizeof (T)>;
5052 int t_s = sizeof (T);
5153 int f_s = sizeof (func_type);
5254 std::cout << " - for type size " << t_s << " \n "
5355 << " function size is " << f_s << " \n "
5456 << " overhead is " << float (f_s - t_s)/t_s * 100 << " %\n " ;
5557}
5658
57- static std::string str_fun () {
59+ static std::string str_fun ()
60+ {
5861 return " 123" ;
5962}
6063
61- int main ( )
64+ TEST (FixedFunction, Overhead )
6265{
63- std::cout << " *** Testing FixedFunction ***" << std::endl;
64-
6566 print_overhead<char [8 ]>();
6667 print_overhead<char [16 ]>();
6768 print_overhead<char [32 ]>();
6869 print_overhead<char [64 ]>();
6970 print_overhead<char [128 ]>();
71+ }
7072
71- doTest (" alloc/dealloc" , []() {
72- static size_t def = 0 ;
73- static size_t cop = 0 ;
74- static size_t mov = 0 ;
75- static size_t cop_ass = 0 ;
76- static size_t mov_ass = 0 ;
77- static size_t destroyed = 0 ;
78- struct cnt {
79- std::string payload;
80- cnt () { def++; }
81- cnt (const cnt &o) { payload = o.payload ; cop++;}
82- cnt (cnt &&o) { payload = std::move (o.payload ); mov++;}
83- cnt & operator =(const cnt &o) { payload = o.payload ; cop_ass++; return *this ; }
84- cnt & operator =(cnt &&o) { payload = std::move (o.payload ); mov_ass++; return *this ; }
85- ~cnt () { destroyed++; }
86- std::string operator ()() { return payload; }
87- };
88-
89- {
90- cnt c1;
91- c1.payload = " xyz" ;
92- FixedFunction<std::string ()> f1 (c1);
93- ASSERT (std::string (" xyz" ) == f1 ());
94-
95- FixedFunction<std::string ()> f2;
96- f2 = std::move (f1);
97- ASSERT (std::string (" xyz" ) == f2 ());
98-
99- FixedFunction<std::string ()> f3 (std::move (f2));
100- ASSERT (std::string (" xyz" ) == f3 ());
101-
102- FixedFunction<std::string ()> f4 (str_fun);
103- ASSERT (std::string (" 123" ) == f4 ());
104-
105- f4 = std::move (f3);
106- ASSERT (std::string (" xyz" ) == f4 ());
107-
108- cnt c2;
109- c2.payload = " qwe" ;
110- f4 = std::move (FixedFunction<std::string ()>(c2));
111- ASSERT (std::string (" qwe" ) == f4 ());
112- }
113-
114- ASSERT (def + cop + mov == destroyed);
115- ASSERT (2 == def);
116- ASSERT (0 == cop);
117- ASSERT (6 == mov);
118- ASSERT (0 == cop_ass);
119- ASSERT (0 == mov_ass);
120- });
73+ TEST (FixedFunction, allocDealloc)
74+ {
75+ static size_t def = 0 ;
76+ static size_t cop = 0 ;
77+ static size_t mov = 0 ;
78+ static size_t cop_ass = 0 ;
79+ static size_t mov_ass = 0 ;
80+ static size_t destroyed = 0 ;
81+ struct cnt {
82+ std::string payload;
83+ cnt () { def++; }
84+ cnt (const cnt &o) { payload = o.payload ; cop++;}
85+ cnt (cnt &&o) { payload = std::move (o.payload ); mov++;}
86+ cnt & operator =(const cnt &o) { payload = o.payload ; cop_ass++; return *this ; }
87+ cnt & operator =(cnt &&o) { payload = std::move (o.payload ); mov_ass++; return *this ; }
88+ ~cnt () { destroyed++; }
89+ std::string operator ()() { return payload; }
90+ };
91+
92+ {
93+ cnt c1;
94+ c1.payload = " xyz" ;
95+ tp::FixedFunction<std::string ()> f1 (c1);
96+ ASSERT_EQ (std::string (" xyz" ), f1 ());
97+
98+ tp::FixedFunction<std::string ()> f2;
99+ f2 = std::move (f1);
100+ ASSERT_EQ (std::string (" xyz" ), f2 ());
101+
102+ tp::FixedFunction<std::string ()> f3 (std::move (f2));
103+ ASSERT_EQ (std::string (" xyz" ), f3 ());
104+
105+ tp::FixedFunction<std::string ()> f4 (str_fun);
106+ ASSERT_EQ (std::string (" 123" ), f4 ());
107+
108+ f4 = std::move (f3);
109+ ASSERT_EQ (std::string (" xyz" ), f4 ());
110+
111+ cnt c2;
112+ c2.payload = " qwe" ;
113+ f4 = std::move (tp::FixedFunction<std::string ()>(c2));
114+ ASSERT_EQ (std::string (" qwe" ), f4 ());
115+ }
121116
122- doTest (" free func" , []() {
123- FixedFunction<int (int )> f (test_free_func);
124- ASSERT (3 == f (3 ));
125- });
117+ ASSERT_EQ (def + cop + mov, destroyed);
118+ ASSERT_EQ (2 , def);
119+ ASSERT_EQ (0 , cop);
120+ ASSERT_EQ (6 , mov);
121+ ASSERT_EQ (0 , cop_ass);
122+ ASSERT_EQ (0 , mov_ass);
123+ }
126124
127- doTest (" free func template" , []() {
128- FixedFunction<std::string (std::string)> f (test_free_func_template<std::string>);
129- ASSERT (std::string (" abc" ) == f (" abc" ));
130- });
125+ TEST (FixedFunction, freeFunc)
126+ {
127+ tp::FixedFunction<int (int )> f (test_free_func);
128+ ASSERT_EQ (3 , f (3 ));
129+ };
131130
131+ TEST (FixedFunction, freeFuncTemplate)
132+ {
133+ tp::FixedFunction<std::string (std::string)> f (test_free_func_template<std::string>);
134+ ASSERT_EQ (std::string (" abc" ), f (" abc" ));
135+ }
132136
133- doTest (" void func" , []() {
134- FixedFunction<void (int &, int )> f (test_void);
135- int p = 0 ;
136- f (p, 42 );
137- ASSERT (42 == p);
138- });
139137
140- doTest (" class method void" , []() {
141- using namespace std ::placeholders;
142- A a;
143- int i = 0 ;
144- FixedFunction<void (int &)> f (std::bind (&A::c, &a, _1));
145- f (i);
146- ASSERT (43 == i);
147- });
138+ TEST (FixedFunction, voidFunc)
139+ {
140+ tp::FixedFunction<void (int &, int )> f (test_void);
141+ int p = 0 ;
142+ f (p, 42 );
143+ ASSERT_EQ (42 , p);
144+ }
148145
149- doTest (" class method 1" , []() {
150- using namespace std ::placeholders;
151- A a;
152- FixedFunction<int (const int &)> f (std::bind (&A::b, &a, _1));
153- ASSERT (4 == f (4 ));
154- });
146+ TEST (FixedFunction, classMethodVoid)
147+ {
148+ using namespace std ::placeholders;
149+ A a;
150+ int i = 0 ;
151+ tp::FixedFunction<void (int &)> f (std::bind (&A::c, &a, _1));
152+ f (i);
153+ ASSERT_EQ (43 , i);
154+ }
155155
156- doTest ( " class method 2 " , []() {
157- using namespace std ::placeholders ;
158- Foo< float > foo ;
159- foo. payload = 1 . f ;
160- FixedFunction<int (int )> f (std::bind (&Foo< float >::bar< int > , &foo , _1));
161- ASSERT ( 2 == f ( 1 ));
162- });
156+ TEST (FixedFunction, classMethod1)
157+ {
158+ using namespace std ::placeholders ;
159+ A a ;
160+ tp:: FixedFunction<int (const int & )> f (std::bind (&A::b , &a , _1));
161+ ASSERT_EQ ( 4 , f ( 4 ));
162+ }
163163
164- doTest (" lambda" , []() {
165- const std::string s1 = " s1" ;
166- FixedFunction<std::string ()> f ([&s1]() {
167- return s1;
168- });
164+ TEST (FixedFunction, classMethod2)
165+ {
166+ using namespace std ::placeholders;
167+ Foo<float > foo;
168+ foo.payload = 1 .f ;
169+ tp::FixedFunction<int (int )> f (std::bind (&Foo<float >::bar<int >, &foo, _1));
170+ ASSERT_EQ (2 , f (1 ));
171+ }
169172
170- ASSERT (s1 == f ());
173+ TEST (FixedFunction, lambda)
174+ {
175+ const std::string s1 = " s1" ;
176+ tp::FixedFunction<std::string ()> f ([&s1]()
177+ {
178+ return s1;
171179 });
172180
181+ ASSERT_EQ (s1, f ());
173182}
174183
184+ int main (int argc, char **argv) {
185+ ::testing::InitGoogleTest (&argc, argv);
186+ return RUN_ALL_TESTS ();
187+ }
175188
176189
0 commit comments