• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
7 #include <boost/stacktrace/detail/void_ptr_cast.hpp>
8 
9 #include <boost/core/lightweight_test.hpp>
10 
foo1_func(int)11 int foo1_func(int) { return 0; }
foo2_func(int,int,...)12 void foo2_func(int, int, ...) {}
13 
14 struct test_struct {
foo1_membtest_struct15     int foo1_memb(int) const { return 0; }
foo2_membtest_struct16     void foo2_memb(int, int, ...) {}
17 };
18 
19 template <class F1, class F2>
test(F1 foo1,F2 foo2)20 void test(F1 foo1, F2 foo2) {
21     using boost::stacktrace::detail::void_ptr_cast;
22 
23     typedef void(*void_f_ptr)();
24 
25     // Function/variable to void(*)()
26     void_f_ptr fp1 = void_ptr_cast<void_f_ptr>(foo1);
27     void_f_ptr fp2 = void_ptr_cast<void_f_ptr>(foo2);
28     BOOST_TEST(fp1);
29     BOOST_TEST(fp2);
30     BOOST_TEST(fp1 != fp2);
31 
32     // Function/variable to void*
33     void* vp1 = void_ptr_cast<void*>(foo1);
34     void* vp2 = void_ptr_cast<void*>(foo2);
35     BOOST_TEST(vp1);
36     BOOST_TEST(vp2);
37     BOOST_TEST(vp1 != vp2);
38 
39     // void* to void(*)()
40     void_f_ptr fp1_2 = void_ptr_cast<void_f_ptr>(vp1);
41     void_f_ptr fp2_2 = void_ptr_cast<void_f_ptr>(vp2);
42     BOOST_TEST(fp1_2);
43     BOOST_TEST(fp2_2);
44     BOOST_TEST(fp1_2 != fp2_2);
45     BOOST_TEST(fp1 == fp1_2);
46     BOOST_TEST(fp2 == fp2_2);
47 
48     // void(*)() to void*
49     BOOST_TEST(void_ptr_cast<void*>(fp1) == vp1);
50     BOOST_TEST(void_ptr_cast<void*>(fp2) == vp2);
51 
52     // void(*)() to function/variable
53     BOOST_TEST(void_ptr_cast<F1>(fp1) == foo1);
54     BOOST_TEST(void_ptr_cast<F2>(fp2) == foo2);
55 
56     // void* to function/variable
57     BOOST_TEST(void_ptr_cast<F1>(vp1) == foo1);
58     BOOST_TEST(void_ptr_cast<F2>(vp2) == foo2);
59 }
60 
main()61 int main() {
62     // Testing for functions
63     test(foo1_func, foo2_func);
64 
65     typedef void(func_t)();
66     test(
67         boost::stacktrace::detail::void_ptr_cast<func_t* const>(foo1_func),
68         boost::stacktrace::detail::void_ptr_cast<func_t* const>(foo2_func)
69     );
70 
71     // Testing for variables (just in case...)
72     int i = 0;
73     double j= 1;
74     test(&i, &j);
75 
76 
77 
78     return boost::report_errors();
79 }
80