Belle II Software  release-05-01-25
ScopeGuards.cc
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2013 - Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Christian Pulvermacher
7  * *
8  * This software is provided "as is" without any warranty. *
9  **************************************************************************/
10 
11 #include <framework/utilities/ScopeGuard.h>
12 #include <framework/utilities/Utils.h>
13 #include <gtest/gtest.h>
14 #include <boost/filesystem.hpp>
15 
16 namespace {
18  struct IntSetterGetterFunctor {
20  explicit IntSetterGetterFunctor(int& reference): ref(reference) {}
22  void operator()(const int& v) { ref = v; }
24  int operator()() const { return ref; }
26  int& ref;
27  };
28 
30  TEST(ScopeGuards, IntRef)
31  {
32  int old{5};
33  {
34  auto guard = Belle2::ScopeGuard::guardValue(old);
35  ASSERT_EQ(old, 5);
36  old = 17;
37  }
38  ASSERT_EQ(old, 5);
39  {
40  auto guard = Belle2::ScopeGuard::guardValue(old, 17);
41  ASSERT_EQ(old, 17);
42  }
43  ASSERT_EQ(old, 5);
44  {
45  auto guard = Belle2::ScopeGuard::guardValue(old, 17);
46  ASSERT_EQ(old, 17);
47  guard.release();
48  }
49  ASSERT_EQ(old, 17);
50  }
51 
53  TEST(ScopeGuards, IntSetterGetter)
54  {
55  int old{5};
56  auto setter = [&old](int v) {old = v;};
57  auto getter = [&old]() {return old;};
58  {
59  auto guard = Belle2::ScopeGuard::guardGetterSetter(getter, setter);
60  ASSERT_EQ(old, 5);
61  old = 17;
62  }
63  ASSERT_EQ(old, 5);
64  {
65  auto guard = Belle2::ScopeGuard::guardGetterSetter(getter, setter, 17);
66  ASSERT_EQ(old, 17);
67  }
68  ASSERT_EQ(old, 5);
69  {
70  auto guard = Belle2::ScopeGuard::guardGetterSetter(getter, setter, 17);
71  ASSERT_EQ(old, 17);
72  guard.release();
73  }
74  ASSERT_EQ(old, 17);
75  }
76 
78  TEST(ScopeGuards, IntSetterGetterFunctor)
79  {
80  int old{5};
81  IntSetterGetterFunctor functor(old);
82  {
83  auto guard = Belle2::ScopeGuard::guardFunctor(functor);
84  ASSERT_EQ(old, 5);
85  old = 17;
86  }
87  ASSERT_EQ(old, 5);
88  {
89  auto guard = Belle2::ScopeGuard::guardFunctor(functor, 17);
90  ASSERT_EQ(old, 17);
91  }
92  ASSERT_EQ(old, 5);
93  {
94  auto guard = Belle2::ScopeGuard::guardFunctor(functor, 17);
95  ASSERT_EQ(old, 17);
96  guard.release();
97  }
98  ASSERT_EQ(old, 17);
99  {
101  [&old](int v) { old = v; },
102  [&old]() { return old; }
103  }, 21);
104  ASSERT_EQ(old, 21);
105  }
106  ASSERT_EQ(old, 17);
107  }
108 
112  TEST(ScopeGuards, StringReference)
113  {
114  std::string value{"before"};
115  {
116  auto guard = Belle2::ScopeGuard::guardValue(value, "after");
117  ASSERT_EQ(value, "after");
118  }
119  ASSERT_EQ(value, "before");
120  {
122  [&value](const std::string & v) { value = v; },
123  [&value]() { return value; }
124  }, "after");
125  ASSERT_EQ(value, "after");
126  };
127  ASSERT_EQ(value, "before");
128  }
129 
131  TEST(ScopeGuards, StreamState)
132  {
133  std::stringstream buf;
134  buf << "a:" << std::setprecision(4) << 1.2;
135  {
136  auto guard1 = Belle2::ScopeGuard::guardStreamState(buf);
137  buf << ":b:" << std::fixed << std::setprecision(4) << 2.3;
138  {
139 
140  auto guard3 = Belle2::ScopeGuard::guardStreamState(buf);
141  buf << ":c:" << std::setprecision(5) << std::setw(10) << std::setfill('-') << 3.4;
142  }
143 
144  buf << ":d:" << std::scientific << 4.5;
145  }
146  buf << ":e:" << 5.4;
147  ASSERT_EQ(buf.str(), "a:1.2:b:2.3000:c:---3.40000:d:4.5000e+00:e:5.4");
148  }
149 
151  TEST(ScopeGuards, WorkingDirectory)
152  {
153  std::string start{boost::filesystem::current_path().c_str()};
154  std::string tmpdir("/tmp");
155  std::string root("/");
156  {
157  auto guard1 = Belle2::ScopeGuard::guardWorkingDirectory("/tmp");
158  ASSERT_EQ(tmpdir, boost::filesystem::current_path().c_str());
159  {
161  ASSERT_EQ(root, boost::filesystem::current_path().c_str());
162  guard2.release();
163  }
164  ASSERT_EQ(root, boost::filesystem::current_path().c_str());
165  }
166  ASSERT_EQ(start, boost::filesystem::current_path().c_str());
167  }
168 
169 
171  TEST(ScopeGuards, Batch)
172  {
173  const bool start = gROOT->IsBatch();
174  {
175  auto guard1 = Belle2::ScopeGuard::guardBatchMode();
176  ASSERT_EQ(true, gROOT->IsBatch());
177  {
178  auto guard2 = Belle2::ScopeGuard::guardBatchMode(false);
179  ASSERT_EQ(false, gROOT->IsBatch());
180  }
181  ASSERT_EQ(true, gROOT->IsBatch());
182  }
183  ASSERT_EQ(start, gROOT->IsBatch());
184  }
185 }
Belle2::Utils::VisitOverload
Helper struct for the C++17 std::visit overload pattern to allow simple use of variants.
Definition: Utils.h:19
Belle2::ScopeGuard::guardWorkingDirectory
static ScopeGuard guardWorkingDirectory()
Create a ScopeGuard of the current working directory.
Definition: ScopeGuard.h:306
Belle2::ScopeGuard::guardBatchMode
static ScopeGuard guardBatchMode(bool batchMode=true)
Create a ScopeGuard to turn ROOT into batch mode and restore the initial batch mode status after the ...
Definition: ScopeGuard.h:362
Belle2::ScopeGuard::guardFunctor
static ScopeGuard guardFunctor(Functor functor)
Create a ScopeGuard from a functor object with appropriately overloaded operator() calls to get and s...
Definition: ScopeGuard.h:198
Belle2::TEST
TEST(TestgetDetectorRegion, TestgetDetectorRegion)
Test Constructors.
Definition: utilityFunctions.cc:18
Belle2::ScopeGuard::guardGetterSetter
static ScopeGuard guardGetterSetter(Getter getter, Setter setter)
Create a ScopeGuard from a getter and setter: On construction the getter object is called to obtain a...
Definition: ScopeGuard.h:147
Belle2::ScopeGuard::guardValue
static ScopeGuard guardValue(T &reference)
Create a ScopeGuard for a value: The content of reference will be copied and reset when the returned ...
Definition: ScopeGuard.h:86
Belle2::ScopeGuard::guardStreamState
static ScopeGuard guardStreamState(std::basic_ios< CharT, Traits > &stream)
Create a ScopeGuard for the state of a stream to reset all the formatting at the end of the object li...
Definition: ScopeGuard.h:272