Belle II Software  release-06-00-14
ScopeGuards.cc
1 /**************************************************************************
2  * basf2 (Belle II Analysis Software Framework) *
3  * Author: The Belle II Collaboration *
4  * *
5  * See git log for contributors and copyright holders. *
6  * This file is licensed under LGPL-3.0, see LICENSE.md. *
7  **************************************************************************/
8 
9 #include <framework/utilities/ScopeGuard.h>
10 #include <framework/utilities/Utils.h>
11 #include <gtest/gtest.h>
12 #include <boost/filesystem.hpp>
13 
14 namespace {
16  struct IntSetterGetterFunctor {
18  explicit IntSetterGetterFunctor(int& reference): ref(reference) {}
20  void operator()(const int& v) { ref = v; }
22  int operator()() const { return ref; }
24  int& ref;
25  };
26 
28  TEST(ScopeGuards, IntRef)
29  {
30  int old{5};
31  {
32  auto guard = Belle2::ScopeGuard::guardValue(old);
33  ASSERT_EQ(old, 5);
34  old = 17;
35  }
36  ASSERT_EQ(old, 5);
37  {
38  auto guard = Belle2::ScopeGuard::guardValue(old, 17);
39  ASSERT_EQ(old, 17);
40  }
41  ASSERT_EQ(old, 5);
42  {
43  auto guard = Belle2::ScopeGuard::guardValue(old, 17);
44  ASSERT_EQ(old, 17);
45  guard.release();
46  }
47  ASSERT_EQ(old, 17);
48  }
49 
51  TEST(ScopeGuards, IntSetterGetter)
52  {
53  int old{5};
54  auto setter = [&old](int v) {old = v;};
55  auto getter = [&old]() {return old;};
56  {
57  auto guard = Belle2::ScopeGuard::guardGetterSetter(getter, setter);
58  ASSERT_EQ(old, 5);
59  old = 17;
60  }
61  ASSERT_EQ(old, 5);
62  {
63  auto guard = Belle2::ScopeGuard::guardGetterSetter(getter, setter, 17);
64  ASSERT_EQ(old, 17);
65  }
66  ASSERT_EQ(old, 5);
67  {
68  auto guard = Belle2::ScopeGuard::guardGetterSetter(getter, setter, 17);
69  ASSERT_EQ(old, 17);
70  guard.release();
71  }
72  ASSERT_EQ(old, 17);
73  }
74 
76  TEST(ScopeGuards, IntSetterGetterFunctor)
77  {
78  int old{5};
79  IntSetterGetterFunctor functor(old);
80  {
81  auto guard = Belle2::ScopeGuard::guardFunctor(functor);
82  ASSERT_EQ(old, 5);
83  old = 17;
84  }
85  ASSERT_EQ(old, 5);
86  {
87  auto guard = Belle2::ScopeGuard::guardFunctor(functor, 17);
88  ASSERT_EQ(old, 17);
89  }
90  ASSERT_EQ(old, 5);
91  {
92  auto guard = Belle2::ScopeGuard::guardFunctor(functor, 17);
93  ASSERT_EQ(old, 17);
94  guard.release();
95  }
96  ASSERT_EQ(old, 17);
97  {
99  [&old](int v) { old = v; },
100  [&old]() { return old; }
101  }, 21);
102  ASSERT_EQ(old, 21);
103  }
104  ASSERT_EQ(old, 17);
105  }
106 
110  TEST(ScopeGuards, StringReference)
111  {
112  std::string value{"before"};
113  {
114  auto guard = Belle2::ScopeGuard::guardValue(value, "after");
115  ASSERT_EQ(value, "after");
116  }
117  ASSERT_EQ(value, "before");
118  {
120  [&value](const std::string & v) { value = v; },
121  [&value]() { return value; }
122  }, "after");
123  ASSERT_EQ(value, "after");
124  };
125  ASSERT_EQ(value, "before");
126  }
127 
129  TEST(ScopeGuards, StreamState)
130  {
131  std::stringstream buf;
132  buf << "a:" << std::setprecision(4) << 1.2;
133  {
134  auto guard1 = Belle2::ScopeGuard::guardStreamState(buf);
135  buf << ":b:" << std::fixed << std::setprecision(4) << 2.3;
136  {
137 
138  auto guard3 = Belle2::ScopeGuard::guardStreamState(buf);
139  buf << ":c:" << std::setprecision(5) << std::setw(10) << std::setfill('-') << 3.4;
140  }
141 
142  buf << ":d:" << std::scientific << 4.5;
143  }
144  buf << ":e:" << 5.4;
145  ASSERT_EQ(buf.str(), "a:1.2:b:2.3000:c:---3.40000:d:4.5000e+00:e:5.4");
146  }
147 
149  TEST(ScopeGuards, WorkingDirectory)
150  {
151  std::string start{boost::filesystem::current_path().c_str()};
152  std::string tmpdir("/tmp");
153  std::string root("/");
154  {
155  auto guard1 = Belle2::ScopeGuard::guardWorkingDirectory("/tmp");
156  ASSERT_EQ(tmpdir, boost::filesystem::current_path().c_str());
157  {
159  ASSERT_EQ(root, boost::filesystem::current_path().c_str());
160  guard2.release();
161  }
162  ASSERT_EQ(root, boost::filesystem::current_path().c_str());
163  }
164  ASSERT_EQ(start, boost::filesystem::current_path().c_str());
165  }
166 
167 
169  TEST(ScopeGuards, Batch)
170  {
171  const bool start = gROOT->IsBatch();
172  {
173  auto guard1 = Belle2::ScopeGuard::guardBatchMode();
174  ASSERT_EQ(true, gROOT->IsBatch());
175  {
176  auto guard2 = Belle2::ScopeGuard::guardBatchMode(false);
177  ASSERT_EQ(false, gROOT->IsBatch());
178  }
179  ASSERT_EQ(true, gROOT->IsBatch());
180  }
181  ASSERT_EQ(start, gROOT->IsBatch());
182  }
183 }
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:262
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:76
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:137
static ScopeGuard guardFunctor(Functor functor)
Create a ScopeGuard from a functor object with appropriately overloaded operator() calls to get and s...
Definition: ScopeGuard.h:188
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:352
static ScopeGuard guardWorkingDirectory()
Create a ScopeGuard of the current working directory.
Definition: ScopeGuard.h:296
TEST(TestgetDetectorRegion, TestgetDetectorRegion)
Test Constructors.
Helper struct for the C++17 std::visit overload pattern to allow simple use of variants.
Definition: Utils.h:26