Belle II Software development
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 <map>
19#include <vector>
20
21TEST(TrackFindingCDCTest, cpp_float)
22{
23 EXPECT_TRUE(std::signbit(-0.0));
24 EXPECT_FALSE(std::signbit(0.0));
25 EXPECT_FALSE(std::signbit(NAN));
26}
27
28TEST(TrackFindingCDCTest, cpp_max)
29{
30 float value = 1.0;
31
32 // If the values are incomparable the first one is always returned.
33 double maximum = std::max(NAN, value);
34 EXPECT_TRUE(std::isnan(maximum));
35
36 double maximum2 = std::max(value, NAN);
37 EXPECT_EQ(value, maximum2);
38}
39
40TEST(TrackFindingCDCTest, cpp_array_init)
41{
42 float values[10] = {};
43 for (int i = 0; i < 10; ++i) {
44 EXPECT_EQ(0, values[i]);
45 }
46}
47
48TEST(TrackFindingCDCTest, cpp_char_is_signed)
49{
50 char isSigned = -1;
51 EXPECT_GT(0, isSigned);
52}
53
54TEST(TrackFindingCDCTest, cpp_stringstream_copy)
55{
56 // Howto copy a string stream even if its constant.
57 std::stringstream filled_non_const;
58 filled_non_const << "filled "
59 << "with "
60 << "stuff.";
61
62 const std::stringstream& filled = filled_non_const;
63
64 std::stringstream copy1;
65 copy1 << filled.rdbuf();
66 filled.rdbuf()->pubseekpos(0, std::ios_base::in);
67
68 std::stringstream copy2;
69 copy2 << filled.rdbuf();
70 filled.rdbuf()->pubseekpos(0, std::ios_base::in);
71
72 EXPECT_EQ(filled.str(), copy1.str());
73 EXPECT_EQ(filled.str(), copy2.str());
74}
75
76TEST(TrackFindingCDCTest, cpp_map_insert)
77{
78 std::map<int, int> defaults{{1, 1}, {2, 2}};
79
80 std::map<int, int> concret{{1, 10}};
81
82 concret.insert(defaults.begin(), defaults.end());
83
84 // Does not overwrite a value that was present before.
85 EXPECT_EQ(10, concret[1]);
86
87 // Inserts new value.
88 EXPECT_EQ(2, concret[2]);
89}
90
91TEST(TrackFindingCDCTest, cpp_remainder)
92{
93 // Test if remainder brings a value to the range [-1, 1]
94 // proving that remainder is the best function to transform an angle to range [-M_PI, M_PI]
95 {
96 double value = 3.0 / 2.0;
97 double reduced_value = std::remainder(value, 2.0);
98
99 EXPECT_FLOAT_EQ(-1.0 / 2.0, reduced_value);
100 }
101
102 {
103 double value = -3.0 / 2.0;
104 double reduced_value = std::remainder(value, 2.0);
105
106 EXPECT_FLOAT_EQ(1.0 / 2.0, reduced_value);
107 }
108}