Belle II Software  release-06-00-14
cpp.test.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 /*
10  * This file contains test to check the behaviour of the c++ programming language.
11  * Its purpose is mainly to asure the programmer that his assumptions about run time behaviour are
12  * correct.
13  */
14 
15 #include <cmath>
16 #include <gtest/gtest.h>
17 
18 #include <boost/optional.hpp>
19 
20 #include <map>
21 #include <vector>
22 
23 TEST(TrackFindingCDCTest, cpp_float)
24 {
25  EXPECT_TRUE(std::signbit(-0.0));
26  EXPECT_FALSE(std::signbit(0.0));
27  EXPECT_FALSE(std::signbit(NAN));
28 }
29 
30 TEST(TrackFindingCDCTest, cpp_max)
31 {
32  float value = 1.0;
33 
34  // If the values are incomparable the first one is always returned.
35  double maximum = std::max(NAN, value);
36  EXPECT_TRUE(std::isnan(maximum));
37 
38  double maximum2 = std::max(value, NAN);
39  EXPECT_EQ(value, maximum2);
40 }
41 
42 TEST(TrackFindingCDCTest, cpp_array_init)
43 {
44  float values[10] = {};
45  for (int i = 0; i < 10; ++i) {
46  EXPECT_EQ(0, values[i]);
47  }
48 }
49 
50 TEST(TrackFindingCDCTest, cpp_char_is_signed)
51 {
52  char isSigned = -1;
53  EXPECT_GT(0, isSigned);
54 }
55 
56 TEST(TrackFindingCDCTest, cpp_stringstream_copy)
57 {
58  // Howto copy a string stream even if its constant.
59  std::stringstream filled_non_const;
60  filled_non_const << "filled "
61  << "with "
62  << "stuff.";
63 
64  const std::stringstream& filled = filled_non_const;
65 
66  std::stringstream copy1;
67  copy1 << filled.rdbuf();
68  filled.rdbuf()->pubseekpos(0, std::ios_base::in);
69 
70  std::stringstream copy2;
71  copy2 << filled.rdbuf();
72  filled.rdbuf()->pubseekpos(0, std::ios_base::in);
73 
74  EXPECT_EQ(filled.str(), copy1.str());
75  EXPECT_EQ(filled.str(), copy2.str());
76 }
77 
78 TEST(TrackFindingCDCTest, cpp_map_insert)
79 {
80  std::map<int, int> defaults{{1, 1}, {2, 2}};
81 
82  std::map<int, int> concret{{1, 10}};
83 
84  concret.insert(defaults.begin(), defaults.end());
85 
86  // Does not overwrite a value that was present before.
87  EXPECT_EQ(10, concret[1]);
88 
89  // Inserts new value.
90  EXPECT_EQ(2, concret[2]);
91 }
92 
93 TEST(TrackFindingCDCTest, cpp_remainder)
94 {
95  // Test if remainder brings a value to the range [-1, 1]
96  // proving that remainder is the best function to transform an angle to range [-M_PI, M_PI]
97  {
98  double value = 3.0 / 2.0;
99  double reduced_value = std::remainder(value, 2.0);
100 
101  EXPECT_FLOAT_EQ(-1.0 / 2.0, reduced_value);
102  }
103 
104  {
105  double value = -3.0 / 2.0;
106  double reduced_value = std::remainder(value, 2.0);
107 
108  EXPECT_FLOAT_EQ(1.0 / 2.0, reduced_value);
109  }
110 }
111 
112 TEST(TrackFindingCDCTest, boost_optional_reference)
113 {
114  std::vector<int> integers({1});
115  boost::optional<std::vector<int>&> ref_integers(integers);
116  int i = ref_integers->at(0);
117  EXPECT_EQ(1, i);
118 }
TEST(TestgetDetectorRegion, TestgetDetectorRegion)
Test Constructors.