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