test/zserio/CppRuntimeExceptionTest.cpp
Line | Count | Source |
1 | | #include <algorithm> |
2 | | #include <array> |
3 | | #include <limits> |
4 | | |
5 | | #include "gtest/gtest.h" |
6 | | #include "zserio/CppRuntimeException.h" |
7 | | |
8 | | namespace zserio |
9 | | { |
10 | | |
11 | | TEST(CppRuntimeExceptionTest, emptyConstructor) |
12 | 1 | { |
13 | 1 | CppRuntimeException exception; |
14 | 1 | ASSERT_EQ(std::string(), exception.what()); |
15 | 1 | } |
16 | | |
17 | | TEST(CppRuntimeExceptionTest, cStringConstructor) |
18 | 1 | { |
19 | 1 | CppRuntimeException noDescriptionException; |
20 | 1 | ASSERT_EQ(std::string(), noDescriptionException.what()); |
21 | | |
22 | 1 | CppRuntimeException emptyDescriptionException(""); |
23 | 1 | ASSERT_EQ(std::string(), emptyDescriptionException.what()); |
24 | | |
25 | 1 | const std::string testMessage = "this is the test message"; |
26 | 1 | CppRuntimeException exception(testMessage.c_str()); |
27 | 1 | ASSERT_EQ(testMessage, exception.what()); |
28 | 1 | } |
29 | | |
30 | | TEST(CppRuntimeExceptionTest, appendCString) |
31 | 1 | { |
32 | 1 | std::string testMessage = "1234567890123456"; |
33 | 1 | const std::string appendix = "1234567890123456"; |
34 | 1 | CppRuntimeException exception = CppRuntimeException(testMessage.c_str()) << appendix.c_str(); |
35 | 1 | testMessage += appendix; |
36 | 1 | ASSERT_EQ(testMessage, exception.what()); |
37 | | |
38 | 1 | const size_t exceptionBufferSize = 512; |
39 | 1 | const size_t maxLen = exceptionBufferSize - 1; |
40 | 101 | for (int i = 0; i < 100; ++i100 ) |
41 | 100 | { |
42 | 100 | exception = exception << appendix.c_str(); |
43 | 100 | testMessage += appendix; |
44 | 100 | const size_t len = std::min(testMessage.size(), maxLen); |
45 | 100 | ASSERT_EQ(testMessage.substr(0, len), exception.what()); |
46 | 100 | } |
47 | 1 | } |
48 | | |
49 | | TEST(CppRuntimeExceptionTest, appendBool) |
50 | 1 | { |
51 | 1 | std::string testMessage = "test true: "; |
52 | 1 | CppRuntimeException exception = CppRuntimeException(testMessage.c_str()) << true; |
53 | 1 | testMessage += "true"; |
54 | 1 | ASSERT_EQ(testMessage, exception.what()); |
55 | | |
56 | 1 | exception << ", and false: " << false; |
57 | 1 | testMessage += ", and false: false"; |
58 | 1 | ASSERT_EQ(testMessage, exception.what()); |
59 | 1 | } |
60 | | |
61 | | TEST(CppRuntimeExceptionTest, appendFloat) |
62 | 1 | { |
63 | 1 | const float value = 123.456F; |
64 | 1 | CppRuntimeException exception = CppRuntimeException() << value; |
65 | 1 | ASSERT_EQ(std::string("123.456"), exception.what()); |
66 | | |
67 | 1 | const float valueWithoutFloatingPart = 123.0F; |
68 | 1 | exception = CppRuntimeException() << valueWithoutFloatingPart; |
69 | 1 | ASSERT_EQ(std::string("123.0"), exception.what()); |
70 | | |
71 | 1 | const float valueInfinity = std::numeric_limits<float>::infinity(); |
72 | 1 | exception = CppRuntimeException() << valueInfinity; |
73 | 1 | ASSERT_EQ(std::string("+Inf"), exception.what()); |
74 | 1 | } |
75 | | |
76 | | TEST(CppRuntimeExceptionTest, appendDouble) |
77 | 1 | { |
78 | 1 | const double value = 123.456; |
79 | 1 | CppRuntimeException exception = CppRuntimeException() << value; |
80 | 1 | ASSERT_EQ(std::string("123.456"), exception.what()); |
81 | 1 | } |
82 | | |
83 | | TEST(CppRuntimeExceptionTest, appendInt) |
84 | 1 | { |
85 | 1 | const int value = 42; |
86 | 1 | CppRuntimeException exception = CppRuntimeException() << value; |
87 | 1 | ASSERT_EQ(std::to_string(value), exception.what()); |
88 | 1 | } |
89 | | |
90 | | class Bitmask |
91 | | { |
92 | | public: |
93 | | using underlying_type = uint8_t; |
94 | | |
95 | | enum class Values : underlying_type |
96 | | { |
97 | | READ = UINT8_C(1), |
98 | | WRITE = UINT8_C(2) |
99 | | }; |
100 | | |
101 | | constexpr explicit Bitmask(Values value) noexcept : |
102 | | m_value(static_cast<underlying_type>(value)) |
103 | 1 | {} |
104 | | |
105 | | constexpr underlying_type getValue() const |
106 | 1 | { |
107 | 1 | return m_value; |
108 | 1 | } |
109 | | |
110 | | private: |
111 | | underlying_type m_value; |
112 | | }; |
113 | | |
114 | | TEST(CppRuntimeExceptionTest, appendBitmask) |
115 | 1 | { |
116 | 1 | const Bitmask value(Bitmask::Values::WRITE); |
117 | 1 | CppRuntimeException exception = CppRuntimeException() << value; |
118 | 1 | ASSERT_STREQ("2", exception.what()); |
119 | 1 | } |
120 | | |
121 | | TEST(CppRuntimeExceptionTest, appendString) |
122 | 1 | { |
123 | 1 | CppRuntimeException exception = CppRuntimeException() << std::string("test"); |
124 | 1 | ASSERT_STREQ("test", exception.what()); |
125 | 1 | } |
126 | | |
127 | | TEST(CppRuntimeExceptionTest, appendVector) |
128 | 1 | { |
129 | 1 | CppRuntimeException exception = CppRuntimeException() << std::vector<int>{{1, 2, 3}}; |
130 | 1 | ASSERT_STREQ("vector([...], 3)", exception.what()); |
131 | 1 | } |
132 | | |
133 | | } // namespace zserio |