/jni/boost/libs/function/test/function_n_test.cpp

https://github.com/LeifAndersen/Android-Supertux · C++ · 648 lines · 406 code · 139 blank · 103 comment · 72 complexity · d52f108aab4d1cac7c8bf773f8231cd9 MD5 · raw file

  1. // Boost.Function library
  2. // Copyright Douglas Gregor 2001-2003. Use, modification and
  3. // distribution is subject to the Boost Software License, Version
  4. // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  5. // http://www.boost.org/LICENSE_1_0.txt)
  6. // For more information, see http://www.boost.org
  7. #include <boost/test/minimal.hpp>
  8. #include <boost/function.hpp>
  9. #include <functional>
  10. #include <cassert>
  11. #include <string>
  12. using namespace boost;
  13. using std::string;
  14. using std::negate;
  15. int global_int;
  16. struct write_five_obj { void operator()() const { global_int = 5; } };
  17. struct write_three_obj { int operator()() const { global_int = 3; return 7; }};
  18. static void write_five() { global_int = 5; }
  19. static void write_three() { global_int = 3; }
  20. struct generate_five_obj { int operator()() const { return 5; } };
  21. struct generate_three_obj { int operator()() const { return 3; } };
  22. static int generate_five() { return 5; }
  23. static int generate_three() { return 3; }
  24. static string identity_str(const string& s) { return s; }
  25. static string string_cat(const string& s1, const string& s2) { return s1+s2; }
  26. static int sum_ints(int x, int y) { return x+y; }
  27. struct write_const_1_nonconst_2
  28. {
  29. void operator()() { global_int = 2; }
  30. void operator()() const { global_int = 1; }
  31. };
  32. struct add_to_obj
  33. {
  34. add_to_obj(int v) : value(v) {}
  35. int operator()(int x) const { return value + x; }
  36. int value;
  37. };
  38. static void
  39. test_zero_args()
  40. {
  41. typedef function0<void> func_void_type;
  42. write_five_obj five = write_five_obj(); // Initialization for Borland C++ 5.5
  43. write_three_obj three = write_three_obj(); // Ditto
  44. // Default construction
  45. func_void_type v1;
  46. BOOST_CHECK(v1.empty());
  47. // Assignment to an empty function
  48. v1 = five;
  49. BOOST_CHECK(!v1.empty());
  50. // Invocation of a function
  51. global_int = 0;
  52. v1();
  53. BOOST_CHECK(global_int == 5);
  54. // clear() method
  55. v1.clear();
  56. BOOST_CHECK(!v1);
  57. // Assignment to an empty function
  58. v1 = three;
  59. BOOST_CHECK(!v1.empty());
  60. // Invocation and self-assignment
  61. global_int = 0;
  62. v1 = v1;
  63. v1();
  64. BOOST_CHECK(global_int == 3);
  65. // Assignment to a non-empty function
  66. v1 = five;
  67. // Invocation and self-assignment
  68. global_int = 0;
  69. v1 = (v1);
  70. v1();
  71. BOOST_CHECK(global_int == 5);
  72. // clear
  73. v1 = 0;
  74. BOOST_CHECK(v1.empty());
  75. // Assignment to an empty function from a free function
  76. v1 = &write_five;
  77. BOOST_CHECK(!v1.empty());
  78. // Invocation
  79. global_int = 0;
  80. v1();
  81. BOOST_CHECK(global_int == 5);
  82. // Assignment to a non-empty function from a free function
  83. v1 = &write_three;
  84. BOOST_CHECK(!v1.empty());
  85. // Invocation
  86. global_int = 0;
  87. v1();
  88. BOOST_CHECK(global_int == 3);
  89. // Assignment
  90. v1 = five;
  91. BOOST_CHECK(!v1.empty());
  92. // Invocation
  93. global_int = 0;
  94. v1();
  95. BOOST_CHECK(global_int == 5);
  96. // Assignment to a non-empty function from a free function
  97. v1 = write_three;
  98. BOOST_CHECK(!v1.empty());
  99. // Invocation
  100. global_int = 0;
  101. v1();
  102. BOOST_CHECK(global_int == 3);
  103. // Construction from another function (that is empty)
  104. v1.clear();
  105. func_void_type v2(v1);
  106. BOOST_CHECK(!v2? true : false);
  107. // Assignment to an empty function
  108. v2 = three;
  109. BOOST_CHECK(!v2.empty());
  110. // Invocation
  111. global_int = 0;
  112. v2();
  113. BOOST_CHECK(global_int == 3);
  114. // Assignment to a non-empty function
  115. v2 = (five);
  116. // Invocation
  117. global_int = 0;
  118. v2();
  119. BOOST_CHECK(global_int == 5);
  120. v2.clear();
  121. BOOST_CHECK(v2.empty());
  122. // Assignment to an empty function from a free function
  123. v2 = (&write_five);
  124. BOOST_CHECK(v2? true : false);
  125. // Invocation
  126. global_int = 0;
  127. v2();
  128. BOOST_CHECK(global_int == 5);
  129. // Assignment to a non-empty function from a free function
  130. v2 = &write_three;
  131. BOOST_CHECK(!v2.empty());
  132. // Invocation
  133. global_int = 0;
  134. v2();
  135. BOOST_CHECK(global_int == 3);
  136. // Swapping
  137. v1 = five;
  138. swap(v1, v2);
  139. v2();
  140. BOOST_CHECK(global_int == 5);
  141. v1();
  142. BOOST_CHECK(global_int == 3);
  143. swap(v1, v2);
  144. v1.clear();
  145. // Assignment
  146. v2 = five;
  147. BOOST_CHECK(!v2.empty());
  148. // Invocation
  149. global_int = 0;
  150. v2();
  151. BOOST_CHECK(global_int == 5);
  152. // Assignment to a non-empty function from a free function
  153. v2 = &write_three;
  154. BOOST_CHECK(!v2.empty());
  155. // Invocation
  156. global_int = 0;
  157. v2();
  158. BOOST_CHECK(global_int == 3);
  159. // Assignment to a function from an empty function
  160. v2 = v1;
  161. BOOST_CHECK(v2.empty());
  162. // Assignment to a function from a function with a functor
  163. v1 = three;
  164. v2 = v1;
  165. BOOST_CHECK(!v1.empty());
  166. BOOST_CHECK(!v2.empty());
  167. // Invocation
  168. global_int = 0;
  169. v1();
  170. BOOST_CHECK(global_int == 3);
  171. global_int = 0;
  172. v2();
  173. BOOST_CHECK(global_int == 3);
  174. // Assign to a function from a function with a function
  175. v2 = &write_five;
  176. v1 = v2;
  177. BOOST_CHECK(!v1.empty());
  178. BOOST_CHECK(!v2.empty());
  179. global_int = 0;
  180. v1();
  181. BOOST_CHECK(global_int == 5);
  182. global_int = 0;
  183. v2();
  184. BOOST_CHECK(global_int == 5);
  185. // Construct a function given another function containing a function
  186. func_void_type v3(v1);
  187. // Invocation of a function
  188. global_int = 0;
  189. v3();
  190. BOOST_CHECK(global_int == 5);
  191. // clear() method
  192. v3.clear();
  193. BOOST_CHECK(!v3? true : false);
  194. // Assignment to an empty function
  195. v3 = three;
  196. BOOST_CHECK(!v3.empty());
  197. // Invocation
  198. global_int = 0;
  199. v3();
  200. BOOST_CHECK(global_int == 3);
  201. // Assignment to a non-empty function
  202. v3 = five;
  203. // Invocation
  204. global_int = 0;
  205. v3();
  206. BOOST_CHECK(global_int == 5);
  207. // clear()
  208. v3.clear();
  209. BOOST_CHECK(v3.empty());
  210. // Assignment to an empty function from a free function
  211. v3 = &write_five;
  212. BOOST_CHECK(!v3.empty());
  213. // Invocation
  214. global_int = 0;
  215. v3();
  216. BOOST_CHECK(global_int == 5);
  217. // Assignment to a non-empty function from a free function
  218. v3 = &write_three;
  219. BOOST_CHECK(!v3.empty());
  220. // Invocation
  221. global_int = 0;
  222. v3();
  223. BOOST_CHECK(global_int == 3);
  224. // Assignment
  225. v3 = five;
  226. BOOST_CHECK(!v3.empty());
  227. // Invocation
  228. global_int = 0;
  229. v3();
  230. BOOST_CHECK(global_int == 5);
  231. // Construction of a function from a function containing a functor
  232. func_void_type v4(v3);
  233. // Invocation of a function
  234. global_int = 0;
  235. v4();
  236. BOOST_CHECK(global_int == 5);
  237. // clear() method
  238. v4.clear();
  239. BOOST_CHECK(v4.empty());
  240. // Assignment to an empty function
  241. v4 = three;
  242. BOOST_CHECK(!v4.empty());
  243. // Invocation
  244. global_int = 0;
  245. v4();
  246. BOOST_CHECK(global_int == 3);
  247. // Assignment to a non-empty function
  248. v4 = five;
  249. // Invocation
  250. global_int = 0;
  251. v4();
  252. BOOST_CHECK(global_int == 5);
  253. // clear()
  254. v4.clear();
  255. BOOST_CHECK(v4.empty());
  256. // Assignment to an empty function from a free function
  257. v4 = &write_five;
  258. BOOST_CHECK(!v4.empty());
  259. // Invocation
  260. global_int = 0;
  261. v4();
  262. BOOST_CHECK(global_int == 5);
  263. // Assignment to a non-empty function from a free function
  264. v4 = &write_three;
  265. BOOST_CHECK(!v4.empty());
  266. // Invocation
  267. global_int = 0;
  268. v4();
  269. BOOST_CHECK(global_int == 3);
  270. // Assignment
  271. v4 = five;
  272. BOOST_CHECK(!v4.empty());
  273. // Invocation
  274. global_int = 0;
  275. v4();
  276. BOOST_CHECK(global_int == 5);
  277. // Construction of a function from a functor
  278. func_void_type v5(five);
  279. // Invocation of a function
  280. global_int = 0;
  281. v5();
  282. BOOST_CHECK(global_int == 5);
  283. // clear() method
  284. v5.clear();
  285. BOOST_CHECK(v5.empty());
  286. // Assignment to an empty function
  287. v5 = three;
  288. BOOST_CHECK(!v5.empty());
  289. // Invocation
  290. global_int = 0;
  291. v5();
  292. BOOST_CHECK(global_int == 3);
  293. // Assignment to a non-empty function
  294. v5 = five;
  295. // Invocation
  296. global_int = 0;
  297. v5();
  298. BOOST_CHECK(global_int == 5);
  299. // clear()
  300. v5.clear();
  301. BOOST_CHECK(v5.empty());
  302. // Assignment to an empty function from a free function
  303. v5 = &write_five;
  304. BOOST_CHECK(!v5.empty());
  305. // Invocation
  306. global_int = 0;
  307. v5();
  308. BOOST_CHECK(global_int == 5);
  309. // Assignment to a non-empty function from a free function
  310. v5 = &write_three;
  311. BOOST_CHECK(!v5.empty());
  312. // Invocation
  313. global_int = 0;
  314. v5();
  315. BOOST_CHECK(global_int == 3);
  316. // Assignment
  317. v5 = five;
  318. BOOST_CHECK(!v5.empty());
  319. // Invocation
  320. global_int = 0;
  321. v5();
  322. BOOST_CHECK(global_int == 5);
  323. // Construction of a function from a function
  324. func_void_type v6(&write_five);
  325. // Invocation of a function
  326. global_int = 0;
  327. v6();
  328. BOOST_CHECK(global_int == 5);
  329. // clear() method
  330. v6.clear();
  331. BOOST_CHECK(v6.empty());
  332. // Assignment to an empty function
  333. v6 = three;
  334. BOOST_CHECK(!v6.empty());
  335. // Invocation
  336. global_int = 0;
  337. v6();
  338. BOOST_CHECK(global_int == 3);
  339. // Assignment to a non-empty function
  340. v6 = five;
  341. // Invocation
  342. global_int = 0;
  343. v6();
  344. BOOST_CHECK(global_int == 5);
  345. // clear()
  346. v6.clear();
  347. BOOST_CHECK(v6.empty());
  348. // Assignment to an empty function from a free function
  349. v6 = &write_five;
  350. BOOST_CHECK(!v6.empty());
  351. // Invocation
  352. global_int = 0;
  353. v6();
  354. BOOST_CHECK(global_int == 5);
  355. // Assignment to a non-empty function from a free function
  356. v6 = &write_three;
  357. BOOST_CHECK(!v6.empty());
  358. // Invocation
  359. global_int = 0;
  360. v6();
  361. BOOST_CHECK(global_int == 3);
  362. // Assignment
  363. v6 = five;
  364. BOOST_CHECK(!v6.empty());
  365. // Invocation
  366. global_int = 0;
  367. v6();
  368. BOOST_CHECK(global_int == 5);
  369. // Const vs. non-const
  370. // Initialization for Borland C++ 5.5
  371. write_const_1_nonconst_2 one_or_two = write_const_1_nonconst_2();
  372. const function0<void> v7(one_or_two);
  373. function0<void> v8(one_or_two);
  374. global_int = 0;
  375. v7();
  376. BOOST_CHECK(global_int == 2);
  377. global_int = 0;
  378. v8();
  379. BOOST_CHECK(global_int == 2);
  380. // Test construction from 0 and comparison to 0
  381. func_void_type v9(0);
  382. BOOST_CHECK(v9 == 0);
  383. # if !defined(__SUNPRO_CC) || __SUNPRO_CC > 0x540 || defined(BOOST_STRICT_CONFIG)
  384. BOOST_CHECK(0 == v9);
  385. #else
  386. BOOST_CHECK(v9.empty());
  387. #endif
  388. // Test return values
  389. typedef function0<int> func_int_type;
  390. // Initialization for Borland C++ 5.5
  391. generate_five_obj gen_five = generate_five_obj();
  392. generate_three_obj gen_three = generate_three_obj();
  393. func_int_type i0(gen_five);
  394. BOOST_CHECK(i0() == 5);
  395. i0 = gen_three;
  396. BOOST_CHECK(i0() == 3);
  397. i0 = &generate_five;
  398. BOOST_CHECK(i0() == 5);
  399. i0 = &generate_three;
  400. BOOST_CHECK(i0() == 3);
  401. BOOST_CHECK(i0? true : false);
  402. i0.clear();
  403. BOOST_CHECK(!i0? true : false);
  404. // Test return values with compatible types
  405. typedef function0<long> func_long_type;
  406. func_long_type i1(gen_five);
  407. BOOST_CHECK(i1() == 5);
  408. i1 = gen_three;
  409. BOOST_CHECK(i1() == 3);
  410. i1 = &generate_five;
  411. BOOST_CHECK(i1() == 5);
  412. i1 = &generate_three;
  413. BOOST_CHECK(i1() == 3);
  414. BOOST_CHECK(i1? true : false);
  415. i1.clear();
  416. BOOST_CHECK(!i1? true : false);
  417. }
  418. static void
  419. test_one_arg()
  420. {
  421. negate<int> neg = negate<int>(); // Initialization for Borland C++ 5.5
  422. function1<int, int> f1(neg);
  423. BOOST_CHECK(f1(5) == -5);
  424. function1<string, string> id(&identity_str);
  425. BOOST_CHECK(id("str") == "str");
  426. function1<std::string, const char*> id2(&identity_str);
  427. BOOST_CHECK(id2("foo") == "foo");
  428. add_to_obj add_to(5);
  429. function1<int, int> f2(add_to);
  430. BOOST_CHECK(f2(3) == 8);
  431. const function1<int, int> cf2(add_to);
  432. BOOST_CHECK(cf2(3) == 8);
  433. }
  434. static void
  435. test_two_args()
  436. {
  437. function2<string, const string&, const string&> cat(&string_cat);
  438. BOOST_CHECK(cat("str", "ing") == "string");
  439. function2<int, short, short> sum(&sum_ints);
  440. BOOST_CHECK(sum(2, 3) == 5);
  441. }
  442. static void
  443. test_emptiness()
  444. {
  445. function0<float> f1;
  446. BOOST_CHECK(f1.empty());
  447. function0<float> f2;
  448. f2 = f1;
  449. BOOST_CHECK(f2.empty());
  450. function0<double> f3;
  451. f3 = f2;
  452. BOOST_CHECK(f3.empty());
  453. }
  454. struct X {
  455. X(int v) : value(v) {}
  456. int twice() const { return 2*value; }
  457. int plus(int v) { return value + v; }
  458. int value;
  459. };
  460. static void
  461. test_member_functions()
  462. {
  463. boost::function1<int, X*> f1(&X::twice);
  464. X one(1);
  465. X five(5);
  466. BOOST_CHECK(f1(&one) == 2);
  467. BOOST_CHECK(f1(&five) == 10);
  468. boost::function1<int, X*> f1_2;
  469. f1_2 = &X::twice;
  470. BOOST_CHECK(f1_2(&one) == 2);
  471. BOOST_CHECK(f1_2(&five) == 10);
  472. boost::function2<int, X&, int> f2(&X::plus);
  473. BOOST_CHECK(f2(one, 3) == 4);
  474. BOOST_CHECK(f2(five, 4) == 9);
  475. }
  476. struct add_with_throw_on_copy {
  477. int operator()(int x, int y) const { return x+y; }
  478. add_with_throw_on_copy() {}
  479. add_with_throw_on_copy(const add_with_throw_on_copy&)
  480. {
  481. throw std::runtime_error("But this CAN'T throw");
  482. }
  483. add_with_throw_on_copy& operator=(const add_with_throw_on_copy&)
  484. {
  485. throw std::runtime_error("But this CAN'T throw");
  486. }
  487. };
  488. static void
  489. test_ref()
  490. {
  491. add_with_throw_on_copy atc;
  492. try {
  493. boost::function2<int, int, int> f(ref(atc));
  494. BOOST_CHECK(f(1, 3) == 4);
  495. }
  496. catch(std::runtime_error e) {
  497. BOOST_ERROR("Nonthrowing constructor threw an exception");
  498. }
  499. }
  500. int test_main(int, char* [])
  501. {
  502. test_zero_args();
  503. test_one_arg();
  504. test_two_args();
  505. test_emptiness();
  506. test_member_functions();
  507. test_ref();
  508. return 0;
  509. }