test_void_ptr_cast.cpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // Copyright 2017 Antony Polukhin.
  2. //
  3. // Distributed under the Boost Software License, Version 1.0.
  4. // (See accompanying file LICENSE_1_0.txt
  5. // or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. #include <boost/stacktrace/detail/void_ptr_cast.hpp>
  7. #include <boost/core/lightweight_test.hpp>
  8. int foo1_func(int) { return 0; }
  9. void foo2_func(int, int, ...) {}
  10. struct test_struct {
  11. int foo1_memb(int) const { return 0; }
  12. void foo2_memb(int, int, ...) {}
  13. };
  14. template <class F1, class F2>
  15. void test(F1 foo1, F2 foo2) {
  16. using boost::stacktrace::detail::void_ptr_cast;
  17. typedef void(*void_f_ptr)();
  18. // Function/variable to void(*)()
  19. void_f_ptr fp1 = void_ptr_cast<void_f_ptr>(foo1);
  20. void_f_ptr fp2 = void_ptr_cast<void_f_ptr>(foo2);
  21. BOOST_TEST(fp1);
  22. BOOST_TEST(fp2);
  23. BOOST_TEST(fp1 != fp2);
  24. // Function/variable to void*
  25. void* vp1 = void_ptr_cast<void*>(foo1);
  26. void* vp2 = void_ptr_cast<void*>(foo2);
  27. BOOST_TEST(vp1);
  28. BOOST_TEST(vp2);
  29. BOOST_TEST(vp1 != vp2);
  30. // void* to void(*)()
  31. void_f_ptr fp1_2 = void_ptr_cast<void_f_ptr>(vp1);
  32. void_f_ptr fp2_2 = void_ptr_cast<void_f_ptr>(vp2);
  33. BOOST_TEST(fp1_2);
  34. BOOST_TEST(fp2_2);
  35. BOOST_TEST(fp1_2 != fp2_2);
  36. BOOST_TEST(fp1 == fp1_2);
  37. BOOST_TEST(fp2 == fp2_2);
  38. // void(*)() to void*
  39. BOOST_TEST(void_ptr_cast<void*>(fp1) == vp1);
  40. BOOST_TEST(void_ptr_cast<void*>(fp2) == vp2);
  41. // void(*)() to function/variable
  42. BOOST_TEST(void_ptr_cast<F1>(fp1) == foo1);
  43. BOOST_TEST(void_ptr_cast<F2>(fp2) == foo2);
  44. // void* to function/variable
  45. BOOST_TEST(void_ptr_cast<F1>(vp1) == foo1);
  46. BOOST_TEST(void_ptr_cast<F2>(vp2) == foo2);
  47. }
  48. int main() {
  49. // Testing for functions
  50. test(foo1_func, foo2_func);
  51. typedef void(func_t)();
  52. test(
  53. boost::stacktrace::detail::void_ptr_cast<func_t* const>(foo1_func),
  54. boost::stacktrace::detail::void_ptr_cast<func_t* const>(foo2_func)
  55. );
  56. // Testing for variables (just in case...)
  57. int i = 0;
  58. double j= 1;
  59. test(&i, &j);
  60. return boost::report_errors();
  61. }