"Fossies" - the Fresh Open Source Software Archive

Member "libsigc++-2.99.13/tests/test_compose.cc" (4 May 2019, 2234 Bytes) of package /linux/misc/libsigc++-2.99.13.tar.xz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 /* Copyright 2002 - 2016, The libsigc++ Development Team
    2  *  Assigned to public domain.  Use as you wish without restriction.
    3  */
    4 
    5 #include "testutilities.h"
    6 #include <sigc++/adaptors/compose.h>
    7 
    8 namespace
    9 {
   10 std::ostringstream result_stream;
   11 
   12 struct set
   13 {
   14   // choose a type that can hold all return values
   15   using result_type = double;
   16 
   17   double operator()(int i)
   18   {
   19     result_stream << "set(int " << i << ") ";
   20     return i * i;
   21   }
   22 
   23   double operator()(double i)
   24   {
   25     result_stream << "set(double " << i << ") ";
   26     return i * 5;
   27   }
   28 };
   29 
   30 struct set_void
   31 {
   32   using result_type = void;
   33 
   34   void operator()(double i) { result_stream << "set_void(double " << i << ")"; }
   35 };
   36 
   37 struct get
   38 {
   39 #ifdef SIGC_CXX_TYPEOF
   40   bool operator()()
   41   {
   42     result_stream << "get() ";
   43     return true;
   44   }
   45 
   46   int operator()(int i)
   47   {
   48     result_stream << "get(" << i << ") ";
   49     return i * 2;
   50   }
   51 
   52   double operator()(int i, int j)
   53   {
   54     result_stream << "get(" << i << ", " << j << ") ";
   55     return double(i) / double(j);
   56   }
   57 #else
   58   // choose a type that can hold all return values
   59   using result_type = double;
   60 
   61   double operator()()
   62   {
   63     result_stream << "get() ";
   64     return static_cast<double>(true);
   65   }
   66 
   67   double operator()(int i)
   68   {
   69     result_stream << "get(" << i << ") ";
   70     return i * 2;
   71   }
   72 
   73   double operator()(int i, int j)
   74   {
   75     result_stream << "get(" << i << ", " << j << ") ";
   76     return double(i) / double(j);
   77   }
   78 #endif
   79 };
   80 
   81 } // end anonymous namespace
   82 
   83 int
   84 main(int argc, char* argv[])
   85 {
   86   auto util = TestUtilities::get_instance();
   87 
   88   if (!util->check_command_args(argc, argv))
   89     return util->get_result_and_delete_instance() ? EXIT_SUCCESS : EXIT_FAILURE;
   90 
   91   result_stream << sigc::compose(set(), get())();
   92   util->check_result(result_stream, "get() set(double 1) 5");
   93 
   94   result_stream << sigc::compose(set(), get())(1);
   95   util->check_result(result_stream, "get(1) set(double 2) 10");
   96 
   97   result_stream << sigc::compose(set(), get())(1, 2);
   98   util->check_result(result_stream, "get(1, 2) set(double 0.5) 2.5");
   99 
  100   sigc::compose(set_void(), get())(3); // void test
  101   util->check_result(result_stream, "get(3) set_void(double 6)");
  102 
  103   return util->get_result_and_delete_instance() ? EXIT_SUCCESS : EXIT_FAILURE;
  104 }