GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: test/zserio/CppRuntimeExceptionTest.cpp Lines: 63 63 100.0 %
Date: 2023-12-13 14:51:09 Branches: 151 396 38.1 %

Line Branch Exec Source
1
#include <algorithm>
2
#include <array>
3
#include <limits>
4
5
#include "gtest/gtest.h"
6
7
#include "zserio/CppRuntimeException.h"
8
9
namespace zserio
10
{
11
12


802
TEST(CppRuntimeExceptionTest, emptyConstructor)
13
{
14
2
    CppRuntimeException exception;
15



1
    ASSERT_EQ(std::string(), exception.what());
16
}
17
18


802
TEST(CppRuntimeExceptionTest, cStringConstructor)
19
{
20
2
    CppRuntimeException noDescriptionException;
21



1
    ASSERT_EQ(std::string(), noDescriptionException.what());
22
23

2
    CppRuntimeException emptyDescriptionException("");
24



1
    ASSERT_EQ(std::string(), emptyDescriptionException.what());
25
26

2
    const std::string testMessage = "this is the test message";
27

2
    CppRuntimeException exception(testMessage.c_str());
28



1
    ASSERT_EQ(testMessage, exception.what());
29
}
30
31


802
TEST(CppRuntimeExceptionTest, appendCString)
32
{
33
2
    std::string testMessage = "1234567890123456";
34

2
    const std::string appendix = "1234567890123456";
35

2
    CppRuntimeException exception = CppRuntimeException(testMessage.c_str()) << appendix.c_str();
36
1
    testMessage += appendix;
37



1
    ASSERT_EQ(testMessage, exception.what());
38
39
1
    const size_t exceptionBufferSize = 512;
40
1
    const size_t maxLen = exceptionBufferSize - 1;
41

101
    for (int i = 0; i < 100; ++i)
42
    {
43
100
        exception = exception << appendix.c_str();
44
100
        testMessage += appendix;
45
100
        const size_t len = std::min(testMessage.size(), maxLen);
46



100
        ASSERT_EQ(testMessage.substr(0, len), exception.what());
47
    }
48
}
49
50


802
TEST(CppRuntimeExceptionTest, appendBool)
51
{
52
2
    std::string testMessage = "test true: ";
53

2
    CppRuntimeException exception = CppRuntimeException(testMessage.c_str()) << true;
54
1
    testMessage += "true";
55



1
    ASSERT_EQ(testMessage, exception.what());
56
57

1
    exception << ", and false: " << false;
58
1
    testMessage += ", and false: false";
59



1
    ASSERT_EQ(testMessage, exception.what());
60
}
61
62


802
TEST(CppRuntimeExceptionTest, appendFloat)
63
{
64
1
    const float value = 123.456F;
65

2
    CppRuntimeException exception = CppRuntimeException() << value;
66



1
    ASSERT_EQ(std::string("123.456"), exception.what());
67
68
1
    const float valueWithoutFloatingPart = 123.0F;
69

1
    exception = CppRuntimeException() << valueWithoutFloatingPart;
70



1
    ASSERT_EQ(std::string("123.0"), exception.what());
71
72
1
    const float valueInfinity = std::numeric_limits<float>::infinity();
73

1
    exception = CppRuntimeException() << valueInfinity;
74




1
    ASSERT_EQ(std::string("+Inf"), exception.what());
75
}
76
77


802
TEST(CppRuntimeExceptionTest, appendDouble)
78
{
79
1
    const double value = 123.456;
80

2
    CppRuntimeException exception = CppRuntimeException() << value;
81




1
    ASSERT_EQ(std::string("123.456"), exception.what());
82
}
83
84


802
TEST(CppRuntimeExceptionTest, appendInt)
85
{
86
1
    const int value = 42;
87

2
    CppRuntimeException exception = CppRuntimeException() << value;
88




1
    ASSERT_EQ(std::to_string(value), exception.what());
89
}
90
91
class Bitmask
92
{
93
public:
94
    using underlying_type = uint8_t;
95
96
    enum class Values : underlying_type
97
    {
98
        READ = UINT8_C(1),
99
        WRITE = UINT8_C(2)
100
    };
101
102
    constexpr explicit Bitmask(Values value) noexcept :
103
        m_value(static_cast<underlying_type>(value))
104
    {}
105
106
1
    constexpr underlying_type getValue() const
107
    {
108
1
        return m_value;
109
    }
110
111
private:
112
    underlying_type m_value;
113
};
114
115


802
TEST(CppRuntimeExceptionTest, appendBitmask)
116
{
117
1
    const Bitmask value(Bitmask::Values::WRITE);
118

2
    CppRuntimeException exception = CppRuntimeException() << value;
119



1
    ASSERT_STREQ("2", exception.what());
120
}
121
122


802
TEST(CppRuntimeExceptionTest, appendString)
123
{
124

2
    CppRuntimeException exception = CppRuntimeException() << std::string("test");
125



1
    ASSERT_STREQ("test", exception.what());
126
}
127
128


802
TEST(CppRuntimeExceptionTest, appendVector)
129
{
130

2
    CppRuntimeException exception = CppRuntimeException() << std::vector<int>{{1, 2, 3}};
131



1
    ASSERT_STREQ("vector([...], 3)", exception.what());
132
}
133
134

2394
} // namespace zserio