1 /*
2 Formatting library implementation tests.
3
4 Copyright (c) 2012-2014, Victor Zverovich
5 All rights reserved.
6
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9
10 1. Redistributions of source code must retain the above copyright notice, this
11 list of conditions and the following disclaimer.
12 2. Redistributions in binary form must reproduce the above copyright notice,
13 this list of conditions and the following disclaimer in the documentation
14 and/or other materials provided with the distribution.
15
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
20 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #define FMT_NOEXCEPT
29 #include "test-assert.h"
30
31 // Include *.cc instead of *.h to test implementation-specific stuff.
32 #include "fmt/format.cc"
33 #include "fmt/printf.cc"
34
35 #include <algorithm>
36 #include <cstring>
37
38 #include "gmock/gmock.h"
39 #include "gtest-extra.h"
40 #include "util.h"
41
42 #undef min
43 #undef max
44
TEST(FormatTest,ArgConverter)45 TEST(FormatTest, ArgConverter) {
46 using fmt::internal::Arg;
47 Arg arg = Arg();
48 arg.type = Arg::LONG_LONG;
49 arg.long_long_value = std::numeric_limits<fmt::LongLong>::max();
50 fmt::internal::ArgConverter<fmt::LongLong>(arg, 'd').visit(arg);
51 EXPECT_EQ(Arg::LONG_LONG, arg.type);
52 }
53
TEST(FormatTest,FormatNegativeNaN)54 TEST(FormatTest, FormatNegativeNaN) {
55 double nan = std::numeric_limits<double>::quiet_NaN();
56 if (fmt::internal::FPUtil::isnegative(-nan))
57 EXPECT_EQ("-nan", fmt::format("{}", -nan));
58 else
59 fmt::print("Warning: compiler doesn't handle negative NaN correctly");
60 }
61
TEST(FormatTest,StrError)62 TEST(FormatTest, StrError) {
63 char *message = 0;
64 char buffer[BUFFER_SIZE];
65 EXPECT_ASSERT(fmt::safe_strerror(EDOM, message = 0, 0), "invalid buffer");
66 EXPECT_ASSERT(fmt::safe_strerror(EDOM, message = buffer, 0),
67 "invalid buffer");
68 buffer[0] = 'x';
69 #if defined(_GNU_SOURCE) && !defined(__COVERITY__)
70 // Use invalid error code to make sure that safe_strerror returns an error
71 // message in the buffer rather than a pointer to a static string.
72 int error_code = -1;
73 #else
74 int error_code = EDOM;
75 #endif
76
77 int result = fmt::safe_strerror(error_code, message = buffer, BUFFER_SIZE);
78 EXPECT_EQ(0, result);
79 std::size_t message_size = std::strlen(message);
80 EXPECT_GE(BUFFER_SIZE - 1u, message_size);
81 EXPECT_EQ(get_system_error(error_code), message);
82
83 // safe_strerror never uses buffer on MinGW.
84 #ifndef __MINGW32__
85 result = fmt::safe_strerror(error_code, message = buffer, message_size);
86 EXPECT_EQ(ERANGE, result);
87 result = fmt::safe_strerror(error_code, message = buffer, 1);
88 EXPECT_EQ(buffer, message); // Message should point to buffer.
89 EXPECT_EQ(ERANGE, result);
90 EXPECT_STREQ("", message);
91 #endif
92 }
93
TEST(FormatTest,FormatErrorCode)94 TEST(FormatTest, FormatErrorCode) {
95 std::string msg = "error 42", sep = ": ";
96 {
97 fmt::MemoryWriter w;
98 w << "garbage";
99 fmt::format_error_code(w, 42, "test");
100 EXPECT_EQ("test: " + msg, w.str());
101 }
102 {
103 fmt::MemoryWriter w;
104 std::string prefix(
105 fmt::internal::INLINE_BUFFER_SIZE - msg.size() - sep.size() + 1, 'x');
106 fmt::format_error_code(w, 42, prefix);
107 EXPECT_EQ(msg, w.str());
108 }
109 int codes[] = {42, -1};
110 for (std::size_t i = 0, n = sizeof(codes) / sizeof(*codes); i < n; ++i) {
111 // Test maximum buffer size.
112 msg = fmt::format("error {}", codes[i]);
113 fmt::MemoryWriter w;
114 std::string prefix(
115 fmt::internal::INLINE_BUFFER_SIZE - msg.size() - sep.size(), 'x');
116 fmt::format_error_code(w, codes[i], prefix);
117 EXPECT_EQ(prefix + sep + msg, w.str());
118 std::size_t size = fmt::internal::INLINE_BUFFER_SIZE;
119 EXPECT_EQ(size, w.size());
120 w.clear();
121 // Test with a message that doesn't fit into the buffer.
122 prefix += 'x';
123 fmt::format_error_code(w, codes[i], prefix);
124 EXPECT_EQ(msg, w.str());
125 }
126 }
127