/*!
 * \file Tinvoke.cpp
 *
 * Copyright (C) 2018-2019 Christos Choutouridis
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation, either version 3
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
#include <utl/utility/invoke.h>
#include <gtest/gtest.h>
#include <type_traits>

namespace test_meta {
   using namespace utl;

   /*
    * Types to behave like Fixtures
    */
   int Ifun(int i) { return i; }
   struct Ifoo {
      int operator() (int i) {
         return i;
      }
   };

   struct Ibar {
      Ibar(int num) : num_(num) {}
      int echo(int i) const { return i; }
      int get() const { return num_; }
      int num_;
   };

   /*
    * Test  invoke()
    */
   TEST(Tinvoke, Invoke) {
      Ifoo ifoo{};
      const Ibar ibar{42};

      EXPECT_EQ (42, invoke(Ifun, 42));
      EXPECT_EQ (42, invoke([] () { return Ifun(42); }));
      EXPECT_EQ (42, invoke(Ifoo{}, 42));
      EXPECT_EQ (42, invoke(ifoo, 42));
      EXPECT_EQ (42, invoke(&Ibar::echo, ibar, 42));
      EXPECT_EQ (42, invoke(&Ibar::get, ibar));

      EXPECT_EQ (true,  (is_invocable<Ifoo, int>::value));
      EXPECT_EQ (false, (is_invocable<Ifoo>::value));
      EXPECT_EQ (false, (is_invocable<Ifoo, int, double>::value));
      EXPECT_EQ (false, (is_invocable<Ibar, int>::value));

      EXPECT_EQ (true,  (is_invocable_r<int, Ifoo, int>::value));
      EXPECT_EQ (false, (is_invocable_r<int*, Ifoo, int>::value));
      EXPECT_EQ (false, (is_invocable_r<int, Ifoo>::value));

      EXPECT_EQ (true, (std::is_same<int, invoke_result_t<Ifoo, int>>()));
      EXPECT_EQ (true, (std::is_same<meta::nil_, invoke_result_t<Ifoo>>()));
      EXPECT_EQ (true, (std::is_same<meta::nil_, invoke_result_t<Ifoo, int, double>>()));

   }
}
