mirror of
https://github.com/KjellKod/g3log.git
synced 2024-12-13 18:45:12 +01:00
87a095e384
--HG-- rename : g2log/Build.cmake => Build.cmake rename : g2log/CMakeLists.txt => CMakeLists.txt rename : g2log/CPackLists.txt => CPackLists.txt rename : g2log/Dynamic.cmake => Dynamic.cmake rename : g2log/example/Example.cmake => example/Example.cmake rename : g2log/example/main_contract.cpp => example/main_contract.cpp rename : g2log/example/main_sigsegv.cpp => example/main_sigsegv.cpp rename : g2log/src/active.hpp => src/active.hpp rename : g2log/src/crashhandler.hpp => src/crashhandler.hpp rename : g2log/src/crashhandler_unix.cpp => src/crashhandler_unix.cpp rename : g2log/src/crashhandler_win.cpp => src/crashhandler_win.cpp rename : g2log/src/g2filesink.cpp => src/g2filesink.cpp rename : g2log/src/g2filesink.hpp => src/g2filesink.hpp rename : g2log/src/g2filesinkhelper.ipp => src/g2filesinkhelper.ipp rename : g2log/src/g2future.hpp => src/g2future.hpp rename : g2log/src/g2log.cpp => src/g2log.cpp rename : g2log/src/g2log.hpp => src/g2log.hpp rename : g2log/src/g2loglevels.cpp => src/g2loglevels.cpp rename : g2log/src/g2loglevels.hpp => src/g2loglevels.hpp rename : g2log/src/g2logmessage.cpp => src/g2logmessage.cpp rename : g2log/src/g2logmessage.hpp => src/g2logmessage.hpp rename : g2log/src/g2logmessagecapture.cpp => src/g2logmessagecapture.cpp rename : g2log/src/g2logmessagecapture.hpp => src/g2logmessagecapture.hpp rename : g2log/src/g2logworker.cpp => src/g2logworker.cpp rename : g2log/src/g2logworker.hpp => src/g2logworker.hpp rename : g2log/src/g2moveoncopy.hpp => src/g2moveoncopy.hpp rename : g2log/src/g2sink.hpp => src/g2sink.hpp rename : g2log/src/g2sinkhandle.hpp => src/g2sinkhandle.hpp rename : g2log/src/g2sinkwrapper.hpp => src/g2sinkwrapper.hpp rename : g2log/src/g2time.cpp => src/g2time.cpp rename : g2log/src/g2time.hpp => src/g2time.hpp rename : g2log/src/shared_queue.hpp => src/shared_queue.hpp rename : g2log/src/std2_make_unique.hpp => src/std2_make_unique.hpp rename : g2log/src/stlpatch_future.hpp => src/stlpatch_future.hpp rename : g2log/test_performance/Performance.cmake => test_performance/Performance.cmake rename : g2log/test_performance/main_threaded_mean.cpp => test_performance/main_threaded_mean.cpp rename : g2log/test_performance/main_threaded_worst.cpp => test_performance/main_threaded_worst.cpp rename : g2log/test_performance/performance.h => test_performance/performance.h rename : g2log/test_unit/Test.cmake => test_unit/Test.cmake rename : g2log/test_unit/test_concept_sink.cpp => test_unit/test_concept_sink.cpp rename : g2log/test_unit/test_configuration.cpp => test_unit/test_configuration.cpp rename : g2log/test_unit/test_filechange.cpp => test_unit/test_filechange.cpp rename : g2log/test_unit/test_io.cpp => test_unit/test_io.cpp rename : g2log/test_unit/test_linux_dynamic_loaded_sharedlib.cpp => test_unit/test_linux_dynamic_loaded_sharedlib.cpp rename : g2log/test_unit/test_sink.cpp => test_unit/test_sink.cpp rename : g2log/test_unit/tester_sharedlib.cpp => test_unit/tester_sharedlib.cpp rename : g2log/test_unit/tester_sharedlib.h => test_unit/tester_sharedlib.h rename : g2log/test_unit/testing_helpers.cpp => test_unit/testing_helpers.cpp rename : g2log/test_unit/testing_helpers.h => test_unit/testing_helpers.h
198 lines
5.8 KiB
C++
198 lines
5.8 KiB
C++
/** ==========================================================================
|
|
* 2012 by KjellKod.cc. This is PUBLIC DOMAIN to use at your own risk and comes
|
|
* with no warranties. This code is yours to share, use and modify with no
|
|
* strings attached and no restrictions or obligations.
|
|
*
|
|
* For more information see g3log/LICENSE or refer refer to http://unlicense.org
|
|
* ============================================================================*/
|
|
|
|
#include <gtest/gtest.h>
|
|
|
|
#include <chrono>
|
|
#include <thread>
|
|
#include <future>
|
|
#include <string>
|
|
#include <exception>
|
|
#include <functional>
|
|
#include <memory>
|
|
#include "g2time.hpp"
|
|
#include "g2future.hpp"
|
|
|
|
TEST(Configuration, LOG)
|
|
{ // ref: http://www.cplusplus.com/reference/clibrary/ctime/strftime/
|
|
// ref: http://en.cppreference.com/w/cpp/io/manip/put_time
|
|
// Day Month Date Time Year: is written as "%a %b %d %H:%M:%S %Y" and formatted output as : Wed Sep 19 08:28:16 2012
|
|
// --- WARNING: The try/catch setup does NOT work,. but for fun and for fake-clarity I leave it
|
|
// --- For formatting options to std::put_time that are NOT YET implemented on Windows fatal errors/assert will occurr
|
|
// --- the last example is such an example.
|
|
try
|
|
{
|
|
std::cout << g2::localtime_formatted(g2::systemtime_now(), "%a %b %d %H:%M:%S %Y") << std::endl;
|
|
std::this_thread::sleep_for(std::chrono::seconds(1));
|
|
std::cout << g2::localtime_formatted(g2::systemtime_now(), "%%Y/%%m/%%d %%H:%%M:%%S = %Y/%m/%d %H:%M:%S") << std::endl;
|
|
#if (defined(WIN32) || defined(_WIN32) || defined(__WIN32__))
|
|
std::cerr << "Formatting options skipped due to VS2012, C++11 non-conformance for" << std::endl;
|
|
std::cerr << " some formatting options. The skipped code was:\n\t\t %EX %Ec, \n(see http://en.cppreference.com/w/cpp/io/manip/put_time for details)" << std::endl;
|
|
#else
|
|
std::cout << "C++11 new formatting options:\n" << g2::localtime_formatted(g2::systemtime_now(), "%%EX: %EX\n%%z: %z\n%%Ec: %Ec") << std::endl;
|
|
#endif
|
|
}
|
|
// This does not work. Other kinds of fatal exits (on Windows) seems to be used instead of exceptions
|
|
// Maybe a signal handler catch would be better? --- TODO: Make it better, both failing and correct
|
|
catch(...)
|
|
{
|
|
ADD_FAILURE() << "On this platform the library does not support given (C++11?) specifiers";
|
|
return;
|
|
}
|
|
ASSERT_TRUE(true); // no exception. all good
|
|
}
|
|
|
|
std::future<std::string> sillyFutureReturn()
|
|
{
|
|
std::packaged_task<std::string()> task([](){return std::string("Hello Future");}); // wrap the function
|
|
std::future<std::string> result = task.get_future(); // get a future
|
|
std::thread(std::move(task)).detach(); // launch on a thread
|
|
std::cout << "Waiting...";
|
|
result.wait();
|
|
return result; // already wasted
|
|
}
|
|
TEST(Configuration, FutureSilly)
|
|
{
|
|
std::string hello = sillyFutureReturn().get();
|
|
ASSERT_STREQ(hello.c_str(), "Hello Future");
|
|
}
|
|
|
|
struct MsgType
|
|
{
|
|
std::string msg_;
|
|
MsgType(std::string m): msg_(m){};
|
|
std::string msg(){return msg_;}
|
|
};
|
|
|
|
|
|
TEST(TestOf_CopyableCall, Expecting_SmoothSailing)
|
|
{
|
|
using namespace kjellkod;
|
|
const std::string str("Hello from struct");
|
|
MsgType type(str);
|
|
std::unique_ptr<Active> bgWorker(Active::createActive());
|
|
std::future<std::string> fstring =
|
|
g2::spawn_task(std::bind(&MsgType::msg, type), bgWorker.get());
|
|
ASSERT_STREQ(str.c_str(), fstring.get().c_str());
|
|
}
|
|
|
|
|
|
|
|
TEST(TestOf_CopyableLambdaCall, Expecting_AllFine)
|
|
{
|
|
using namespace kjellkod;
|
|
std::unique_ptr<Active> bgWorker(Active::createActive());
|
|
|
|
// lambda task
|
|
const std::string str_standalone("Hello from standalone");
|
|
auto msg_lambda=[=](){return (str_standalone+str_standalone);};
|
|
std::string expected(str_standalone+str_standalone);
|
|
|
|
auto fstring_standalone = g2::spawn_task(msg_lambda, bgWorker.get());
|
|
ASSERT_STREQ(expected.c_str(), fstring_standalone.get().c_str());
|
|
}
|
|
|
|
|
|
|
|
|
|
template<typename F>
|
|
std::future<typename std::result_of<F()>::type> ObsoleteSpawnTask(F f)
|
|
{
|
|
typedef typename std::result_of<F()>::type result_type;
|
|
typedef std::packaged_task<result_type()> task_type;
|
|
|
|
task_type task(std::move(f));
|
|
std::future<result_type> result = task.get_future();
|
|
|
|
std::vector<std::function<void()>> vec;
|
|
vec.push_back(g2::MoveOnCopy<task_type>(std::move(task)));
|
|
std::thread(std::move(vec.back())).detach();
|
|
result.wait();
|
|
return std::move(result);
|
|
}
|
|
|
|
TEST(TestOf_ObsoleteSpawnTaskWithStringReturn, Expecting_FutureString)
|
|
{
|
|
std::string str("Hello");
|
|
std::string expected(str+str);
|
|
auto msg_lambda=[=](){return (str+str);};
|
|
auto future_string = ObsoleteSpawnTask(msg_lambda);
|
|
|
|
ASSERT_STREQ(expected.c_str(), future_string.get().c_str());
|
|
}
|
|
// gcc thread example below
|
|
// tests code below copied from mail-list conversion between
|
|
// Lars Gullik Bjønnes and Jonathan Wakely
|
|
// http://gcc.gnu.org/ml/gcc-help/2011-11/msg00052.html
|
|
|
|
// --------------------------------------------------------------
|
|
namespace WORKING
|
|
{
|
|
using namespace g2;
|
|
|
|
#include <gtest/gtest.h>
|
|
|
|
#include <iostream>
|
|
#include <future>
|
|
#include <thread>
|
|
#include <vector>
|
|
|
|
std::vector<std::function<void()>> vec;
|
|
|
|
template<typename F>
|
|
std::future<typename std::result_of<F()>::type> spawn_task(F f)
|
|
{
|
|
typedef typename std::result_of<F()>::type result_type;
|
|
typedef std::packaged_task<result_type()> task_type;
|
|
|
|
task_type task(std::move(f));
|
|
std::future<result_type> res = task.get_future();
|
|
|
|
vec.push_back(
|
|
MoveOnCopy<task_type>(
|
|
std::move(task)));
|
|
|
|
std::thread([]()
|
|
{
|
|
auto task = std::move(vec.back());
|
|
vec.pop_back();
|
|
task();
|
|
}
|
|
).detach();
|
|
|
|
return std::move(res);
|
|
}
|
|
|
|
|
|
|
|
double get_res()
|
|
{
|
|
return 42.2;
|
|
}
|
|
|
|
std::string msg2(){return "msg2";}
|
|
} // WORKING
|
|
|
|
TEST(Yalla, Testar)
|
|
{
|
|
using namespace WORKING;
|
|
auto f = spawn_task(get_res);
|
|
std::cout << "Res = " << f.get() << std::endl;
|
|
|
|
auto f2 = spawn_task(msg2);
|
|
std::cout << "Res2 = " << f2.get() << std::endl;
|
|
|
|
|
|
ASSERT_TRUE(true);
|
|
}
|
|
|
|
|
|
|
|
|
|
|