File: func.h

package info (click to toggle)
residualvm 0.2.1%2Bdfsg-3
  • links: PTS, VCS
  • area: contrib
  • in suites: stretch
  • size: 23,344 kB
  • ctags: 27,386
  • sloc: cpp: 175,667; sh: 6,640; xml: 1,731; perl: 1,024; java: 839; asm: 738; python: 564; makefile: 145; sed: 11; php: 1
file content (60 lines) | stat: -rw-r--r-- 1,352 bytes parent folder | download | duplicates (13)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <cxxtest/TestSuite.h>

#include "common/func.h"

void myFunction1(int &dst, const int src) { dst = src; }
void myFunction2(const int src, int &dst) { dst = src; }

class FuncTestSuite : public CxxTest::TestSuite
{
	public:
	void test_bind1st() {
		int dst = 0;
		Common::bind1st(Common::ptr_fun(myFunction1), dst)(1);
		TS_ASSERT_EQUALS(dst, 1);
	}

	void test_bind2nd() {
		int dst = 0;
		Common::bind2nd(Common::ptr_fun(myFunction2), dst)(1);
		TS_ASSERT_EQUALS(dst, 1);
	}

	struct Foo {
		void fooAdd(int &foo) {
			++foo;
		}

		void fooSub(int &foo) const {
			--foo;
		}
	};

	void test_mem_fun_ref() {
		Foo myFoos[4];
		int counter = 0;

		Common::for_each(myFoos, myFoos+4, Common::bind2nd(Common::mem_fun_ref(&Foo::fooAdd), counter));
		TS_ASSERT_EQUALS(counter, 4);

		Common::for_each(myFoos, myFoos+4, Common::bind2nd(Common::mem_fun_ref(&Foo::fooSub), counter));
		TS_ASSERT_EQUALS(counter, 0);
	}

	void test_mem_fun() {
		Foo *myFoos[4];
		for (int i = 0; i < 4; ++i)
			myFoos[i] = new Foo;

		int counter = 0;

		Common::for_each(myFoos, myFoos+4, Common::bind2nd(Common::mem_fun(&Foo::fooAdd), counter));
		TS_ASSERT_EQUALS(counter, 4);

		Common::for_each(myFoos, myFoos+4, Common::bind2nd(Common::mem_fun(&Foo::fooSub), counter));
		TS_ASSERT_EQUALS(counter, 0);

		for (int i = 0; i < 4; ++i)
			delete myFoos[i];
	}
};