11 #include <framework/utilities/ScopeGuard.h>
12 #include <framework/utilities/Utils.h>
13 #include <gtest/gtest.h>
14 #include <boost/filesystem.hpp>
18 struct IntSetterGetterFunctor {
20 explicit IntSetterGetterFunctor(
int& reference): ref(reference) {}
22 void operator()(
const int& v) { ref = v; }
24 int operator()()
const {
return ref; }
30 TEST(ScopeGuards, IntRef)
53 TEST(ScopeGuards, IntSetterGetter)
56 auto setter = [&old](
int v) {old = v;};
57 auto getter = [&old]() {
return old;};
78 TEST(ScopeGuards, IntSetterGetterFunctor)
81 IntSetterGetterFunctor functor(old);
101 [&old](
int v) { old = v; },
102 [&old]() {
return old; }
112 TEST(ScopeGuards, StringReference)
114 std::string value{
"before"};
117 ASSERT_EQ(value,
"after");
119 ASSERT_EQ(value,
"before");
122 [&value](
const std::string & v) { value = v; },
123 [&value]() {
return value; }
125 ASSERT_EQ(value,
"after");
127 ASSERT_EQ(value,
"before");
131 TEST(ScopeGuards, StreamState)
133 std::stringstream buf;
134 buf <<
"a:" << std::setprecision(4) << 1.2;
137 buf <<
":b:" << std::fixed << std::setprecision(4) << 2.3;
141 buf <<
":c:" << std::setprecision(5) << std::setw(10) << std::setfill(
'-') << 3.4;
144 buf <<
":d:" << std::scientific << 4.5;
147 ASSERT_EQ(buf.str(),
"a:1.2:b:2.3000:c:---3.40000:d:4.5000e+00:e:5.4");
151 TEST(ScopeGuards, WorkingDirectory)
153 std::string start{boost::filesystem::current_path().c_str()};
154 std::string tmpdir(
"/tmp");
155 std::string root(
"/");
158 ASSERT_EQ(tmpdir, boost::filesystem::current_path().c_str());
161 ASSERT_EQ(root, boost::filesystem::current_path().c_str());
164 ASSERT_EQ(root, boost::filesystem::current_path().c_str());
166 ASSERT_EQ(start, boost::filesystem::current_path().c_str());
171 TEST(ScopeGuards, Batch)
173 const bool start = gROOT->IsBatch();
176 ASSERT_EQ(
true, gROOT->IsBatch());
179 ASSERT_EQ(
false, gROOT->IsBatch());
181 ASSERT_EQ(
true, gROOT->IsBatch());
183 ASSERT_EQ(start, gROOT->IsBatch());