make.cpp 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // make.hpp
  3. //
  4. // Copyright 2008 Eric Niebler. Distributed under the Boost
  5. // Software License, Version 1.0. (See accompanying file
  6. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. #include <boost/proto/core.hpp>
  8. #include <boost/proto/transform/arg.hpp>
  9. #include <boost/proto/transform/make.hpp>
  10. #include <boost/mpl/identity.hpp>
  11. #include <boost/test/unit_test.hpp>
  12. namespace mpl = boost::mpl;
  13. namespace proto = boost::proto;
  14. using proto::_;
  15. template<typename T>
  16. struct type2type {};
  17. template<typename T>
  18. struct wrapper
  19. {
  20. T t_;
  21. explicit wrapper(T const & t = T()) : t_(t) {}
  22. };
  23. template<typename T>
  24. struct careful
  25. {
  26. typedef typename T::not_there not_there;
  27. };
  28. // Test that when no substitution is done, we don't instantiate templates
  29. struct MakeTest1
  30. : proto::make< type2type< careful<int> > >
  31. {};
  32. void make_test1()
  33. {
  34. proto::terminal<int>::type i = {42};
  35. type2type< careful<int> > res = MakeTest1()(i);
  36. }
  37. // Test that when substitution is done, and there is no nested ::type
  38. // typedef, the result is the wrapper
  39. struct MakeTest2
  40. : proto::make< wrapper< proto::_value > >
  41. {};
  42. void make_test2()
  43. {
  44. proto::terminal<int>::type i = {42};
  45. wrapper<int> res = MakeTest2()(i);
  46. BOOST_CHECK_EQUAL(res.t_, 0);
  47. }
  48. // Test that when substitution is done, and there is no nested ::type
  49. // typedef, the result is the wrapper
  50. struct MakeTest3
  51. : proto::make< wrapper< proto::_value >(proto::_value) >
  52. {};
  53. void make_test3()
  54. {
  55. proto::terminal<int>::type i = {42};
  56. wrapper<int> res = MakeTest3()(i);
  57. BOOST_CHECK_EQUAL(res.t_, 42);
  58. }
  59. // Test that when substitution is done, and there is no nested ::type
  60. // typedef, the result is the wrapper
  61. struct MakeTest4
  62. : proto::make< mpl::identity< proto::_value >(proto::_value) >
  63. {};
  64. void make_test4()
  65. {
  66. proto::terminal<int>::type i = {42};
  67. int res = MakeTest4()(i);
  68. BOOST_CHECK_EQUAL(res, 42);
  69. }
  70. using namespace boost::unit_test;
  71. ///////////////////////////////////////////////////////////////////////////////
  72. // init_unit_test_suite
  73. //
  74. test_suite* init_unit_test_suite( int argc, char* argv[] )
  75. {
  76. test_suite *test = BOOST_TEST_SUITE("test the make transform");
  77. test->add(BOOST_TEST_CASE(&make_test1));
  78. test->add(BOOST_TEST_CASE(&make_test2));
  79. test->add(BOOST_TEST_CASE(&make_test3));
  80. test->add(BOOST_TEST_CASE(&make_test4));
  81. return test;
  82. }