adding unit test + make_unique

This commit is contained in:
KjellKod 2013-07-13 17:57:26 -06:00
parent 3fb37b0709
commit 3e2f092924
4 changed files with 150 additions and 0 deletions

View File

@ -0,0 +1,49 @@
/** ==========================================================================
* 2013 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.
*
* make_unique will be in C++14, this implementation is copied as I understood
* Stephan T. Lavavej's description of it.
*
* PUBLIC DOMAIN and NOT under copywrite protection.
*
*
* Example: usage
* auto an_int = make_unique<int>(123);
* auto a_string = make_unique<string>(5, 'x');
* auto an_int_array = make_unique<int[]>(11, 22, 33);
* ********************************************* */
#include <memory>
#include <utility>
#include <type_traits>
namespace std2 {
namespace impl_fut_stl {
template<typename T, typename ... Args>
std::unique_ptr<T> make_unique_helper(std::false_type, Args&&... args) {
return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
}
template<typename T, typename ... Args>
std::unique_ptr<T> make_unique_helper(std::true_type, Args&&... args) {
static_assert(std::extent<T>::value == 0,
"make_unique<T[N]>() is forbidden, please use make_unique<T[]>(),");
typedef typename std::remove_extent<T>::type U;
return std::unique_ptr<T>(new U[sizeof...(Args)] {
std::forward<Args>(args)...});
}
}
template<typename T, typename ... Args>
std::unique_ptr<T> make_unique(Args&&... args) {
return impl_fut_stl::make_unique_helper<T>(
std::is_array<T>(), std::forward<Args>(args)...);
}
}

View File

@ -0,0 +1,17 @@
#include <gtest/gtest.h>
#include "g2log.h"
#include <memory>
#include <string>
#include "testing_helpers.h"
#include "std2_make_unique.hpp"
using namespace std;
using namespace std2;
//class CoutSink{
// stringstream buffer;
// unique_ptr<ScopedCout> scope_ptr;
// CoutSink() : scope_ptr(make_unique<ScopedCout>(&buffer){}
//};
//

View File

@ -0,0 +1,35 @@
#include <gtest/gtest.h>
#include "testing_helpers.h"
#include "g2log.h"
#include "g2logworker.h"
using namespace std;
ScopedCout::ScopedCout(std::stringstream* buffer)
: _old_cout(std::cout.rdbuf()) {
cout.rdbuf(buffer->rdbuf());
}
ScopedCout::~ScopedCout() { cout.rdbuf(_old_cout);}
RestoreLogger::RestoreLogger(std::string directory)
: logger_(new g2LogWorker("UNIT_TEST_LOGGER", directory)) {
g2::initializeLogging(logger_.get());
g2::internal::changeFatalInitHandlerForUnitTesting();
std::future<std::string> filename(logger_->logFileName());
if (!filename.valid()) ADD_FAILURE();
log_file_ = filename.get();
}
RestoreLogger::~RestoreLogger() {
reset();
g2::shutDownLogging();
if (0 != remove(log_file_.c_str()))
ADD_FAILURE();
}
void RestoreLogger::reset() {
logger_.reset();
}

View File

@ -0,0 +1,49 @@
/*
* File: test_helper__restore_logger.h
* Author: kjell
*
* Created on July 13, 2013, 4:46 PM
*/
#ifndef TEST_HELPER__RESTORE_LOGGER_H
#define TEST_HELPER__RESTORE_LOGGER_H
#include <memory>
#include <string>
#include <iostream>
class g2LogWorker;
// After initializing ScopedCout all std::couts is redirected to the buffer
// Example:
// stringstream buffer;
// ScopedCout guard(&buffer);
// cout << "Hello World";
// ASSERT_STREQ(buffer.str().c_str(), "Hello World");
class ScopedCout
{
std::streambuf* _old_cout;
public:
explicit ScopedCout(std::stringstream* buffer);
~ScopedCout();
};
// RAII temporarily replace of logger
// and restoration of original logger at scope end
struct RestoreLogger
{
explicit RestoreLogger(std::string directory);
~RestoreLogger();
void reset();
std::unique_ptr<g2LogWorker> logger_;
std::string logFile(){return log_file_;}
private:
std::string log_file_;
};
#endif /* TEST_HELPER__RESTORE_LOGGER_H */