test/zserio/CppRuntimeExceptionTest.cpp
Line | Count | 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 | | TEST(CppRuntimeExceptionTest, emptyConstructor) |
13 | 1 | { |
14 | 1 | CppRuntimeException exception; |
15 | 1 | ASSERT_EQ(std::string(), exception.what()); |
16 | 1 | } |
17 | | |
18 | | TEST(CppRuntimeExceptionTest, cStringConstructor) |
19 | 1 | { |
20 | 1 | CppRuntimeException noDescriptionException; |
21 | 1 | ASSERT_EQ(std::string(), noDescriptionException.what()); |
22 | | |
23 | 1 | CppRuntimeException emptyDescriptionException(""); |
24 | 1 | ASSERT_EQ(std::string(), emptyDescriptionException.what()); |
25 | | |
26 | 1 | const std::string testMessage = "this is the test message"; |
27 | 1 | CppRuntimeException exception(testMessage.c_str()); |
28 | 1 | ASSERT_EQ(testMessage, exception.what()); |
29 | 1 | } |
30 | | |
31 | | TEST(CppRuntimeExceptionTest, appendCString) |
32 | 1 | { |
33 | 1 | std::string testMessage = "1234567890123456"; |
34 | 1 | const std::string appendix = "1234567890123456"; |
35 | 1 | 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; ++i100 ) |
42 | 100 | { |
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 | 100 | } |
48 | 1 | } |
49 | | |
50 | | TEST(CppRuntimeExceptionTest, appendBool) |
51 | 1 | { |
52 | 1 | std::string testMessage = "test true: "; |
53 | 1 | 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 | 1 | } |
61 | | |
62 | | TEST(CppRuntimeExceptionTest, appendFloat) |
63 | 1 | { |
64 | 1 | const float value = 123.456F; |
65 | 1 | 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 | 1 | } |
76 | | |
77 | | TEST(CppRuntimeExceptionTest, appendDouble) |
78 | 1 | { |
79 | 1 | const double value = 123.456; |
80 | 1 | CppRuntimeException exception = CppRuntimeException() << value; |
81 | 1 | ASSERT_EQ(std::string("123.456"), exception.what()); |
82 | 1 | } |
83 | | |
84 | | TEST(CppRuntimeExceptionTest, appendInt) |
85 | 1 | { |
86 | 1 | const int value = 42; |
87 | 1 | CppRuntimeException exception = CppRuntimeException() << value; |
88 | 1 | ASSERT_EQ(std::to_string(value), exception.what()); |
89 | 1 | } |
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 | 1 | {} |
105 | | |
106 | | constexpr underlying_type getValue() const |
107 | 1 | { |
108 | 1 | return m_value; |
109 | 1 | } |
110 | | |
111 | | private: |
112 | | underlying_type m_value; |
113 | | }; |
114 | | |
115 | | TEST(CppRuntimeExceptionTest, appendBitmask) |
116 | 1 | { |
117 | 1 | const Bitmask value(Bitmask::Values::WRITE); |
118 | 1 | CppRuntimeException exception = CppRuntimeException() << value; |
119 | 1 | ASSERT_STREQ("2", exception.what()); |
120 | 1 | } |
121 | | |
122 | | TEST(CppRuntimeExceptionTest, appendString) |
123 | 1 | { |
124 | 1 | CppRuntimeException exception = CppRuntimeException() << std::string("test"); |
125 | 1 | ASSERT_STREQ("test", exception.what()); |
126 | 1 | } |
127 | | |
128 | | TEST(CppRuntimeExceptionTest, appendVector) |
129 | 1 | { |
130 | 1 | CppRuntimeException exception = CppRuntimeException() << std::vector<int>{{1, 2, 3}}; |
131 | 1 | ASSERT_STREQ("vector([...], 3)", exception.what()); |
132 | 1 | } |
133 | | |
134 | | } // namespace zserio |