Skip to content

Commit

Permalink
Fix Fix #50 in tests too.
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaut committed Jul 29, 2014
1 parent 24d6baa commit 8f8fd76
Show file tree
Hide file tree
Showing 9 changed files with 226 additions and 239 deletions.
143 changes: 68 additions & 75 deletions test/format-test.cc

Large diffs are not rendered by default.

186 changes: 93 additions & 93 deletions test/gtest-extra-test.cc

Large diffs are not rendered by default.

14 changes: 7 additions & 7 deletions test/gtest-extra.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

using fmt::File;

void OutputRedirect::Flush() {
void OutputRedirect::flush() {
#if EOF != -1
# error "FMT_RETRY assumes return value of -1 indicating failure"
#endif
Expand All @@ -41,17 +41,17 @@ void OutputRedirect::Flush() {
throw fmt::SystemError(errno, "cannot flush stream");
}

void OutputRedirect::Restore() {
void OutputRedirect::restore() {
if (original_.descriptor() == -1)
return; // Already restored.
Flush();
flush();
// Restore the original file.
original_.dup2(FMT_POSIX(fileno(file_)));
original_.close();
}

OutputRedirect::OutputRedirect(FILE *file) : file_(file) {
Flush();
flush();
int fd = FMT_POSIX(fileno(file));
// Create a File object referring to the original file.
original_ = File::dup(fd);
Expand All @@ -64,15 +64,15 @@ OutputRedirect::OutputRedirect(FILE *file) : file_(file) {

OutputRedirect::~OutputRedirect() FMT_NOEXCEPT(true) {
try {
Restore();
restore();
} catch (const std::exception &e) {
std::fputs(e.what(), stderr);
}
}

std::string OutputRedirect::RestoreAndRead() {
std::string OutputRedirect::restore_and_read() {
// Restore output.
Restore();
restore();

// Read everything from the pipe.
std::string content;
Expand Down
8 changes: 4 additions & 4 deletions test/gtest-extra.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,16 +99,16 @@ class OutputRedirect {

GTEST_DISALLOW_COPY_AND_ASSIGN_(OutputRedirect);

void Flush();
void Restore();
void flush();
void restore();

public:
explicit OutputRedirect(FILE *file);
~OutputRedirect() FMT_NOEXCEPT(true);

// Restores the original file, reads output from the pipe into a string
// and returns it.
std::string RestoreAndRead();
std::string restore_and_read();
};

#define FMT_TEST_WRITE_(statement, expected_output, file, fail) \
Expand All @@ -117,7 +117,7 @@ class OutputRedirect {
std::string gtest_expected_output = expected_output; \
OutputRedirect gtest_redir(file); \
GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement); \
std::string gtest_output = gtest_redir.RestoreAndRead(); \
std::string gtest_output = gtest_redir.restore_and_read(); \
if (gtest_output != gtest_expected_output) { \
gtest_ar \
<< #statement " produces different output.\n" \
Expand Down
18 changes: 9 additions & 9 deletions test/macro-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -71,23 +71,23 @@ int result;
result += args[i].int_value; \
}

MAKE_TEST(TestFunc)
MAKE_TEST(test_func)

typedef char Char;
FMT_WRAP1(TestFunc, const char *, 1)
FMT_WRAP1(test_func, const char *, 1)

TEST(UtilTest, Wrap1) {
result = 0;
TestFunc("", 42);
test_func("", 42);
EXPECT_EQ(42, result);
}

MAKE_TEST(TestVariadicVoid)
FMT_VARIADIC_VOID(TestVariadicVoid, const char *)
MAKE_TEST(test_variadic_void)
FMT_VARIADIC_VOID(test_variadic_void, const char *)

TEST(UtilTest, VariadicVoid) {
result = 0;
TestVariadicVoid("", 10, 20, 30, 40, 50, 60, 70, 80, 90, 100);
test_variadic_void("", 10, 20, 30, 40, 50, 60, 70, 80, 90, 100);
EXPECT_EQ(550, result);
}

Expand All @@ -96,18 +96,18 @@ struct S {};

#define GET_TYPE(n) S<n>

int TestVariadic(FMT_GEN(10, GET_TYPE), const fmt::ArgList &args) { \
int test_variadic(FMT_GEN(10, GET_TYPE), const fmt::ArgList &args) { \
int result = 0; \
for (std::size_t i = 0, n = args.size(); i < n; ++i) \
result += args[i].int_value; \
return result;
}
FMT_VARIADIC(int, TestVariadic,
FMT_VARIADIC(int, test_variadic,
S<0>, S<1>, S<2>, S<3>, S<4>, S<5>, S<6>, S<7>, S<8>, S<9>)

#define MAKE_ARG(n) S<n>()

TEST(UtilTest, Variadic) {
EXPECT_EQ(550, TestVariadic(FMT_GEN(10, MAKE_ARG),
EXPECT_EQ(550, test_variadic(FMT_GEN(10, MAKE_ARG),
10, 20, 30, 40, 50, 60, 70, 80, 90, 100));
}
26 changes: 9 additions & 17 deletions test/printf-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,26 +35,18 @@
using fmt::format;
using fmt::FormatError;

// Returns a number UINT_MAX + 1 formatted as a string.
std::string GetBigNumber() {
char format[BUFFER_SIZE];
SPrintf(format, "%u", UINT_MAX);
Increment(format + 1);
return format;
}

const unsigned BIG_NUM = INT_MAX + 1u;

// Makes format string argument positional.
std::string MakePositional(fmt::StringRef format) {
std::string make_positional(fmt::StringRef format) {
std::string s(format);
s.replace(s.find('%'), 1, "%1$");
return s;
}

#define EXPECT_PRINTF(expected_output, format, arg) \
EXPECT_EQ(expected_output, fmt::sprintf(format, arg)); \
EXPECT_EQ(expected_output, fmt::sprintf(MakePositional(format), arg))
EXPECT_EQ(expected_output, fmt::sprintf(make_positional(format), arg))

TEST(PrintfTest, NoArgs) {
EXPECT_EQ("test", fmt::sprintf("test"));
Expand Down Expand Up @@ -187,17 +179,17 @@ TEST(PrintfTest, HashFlag) {
EXPECT_PRINTF("-42.000000", "%#F", -42.0);

char buffer[BUFFER_SIZE];
SPrintf(buffer, "%#e", -42.0);
safe_sprintf(buffer, "%#e", -42.0);
EXPECT_PRINTF(buffer, "%#e", -42.0);
SPrintf(buffer, "%#E", -42.0);
safe_sprintf(buffer, "%#E", -42.0);
EXPECT_PRINTF(buffer, "%#E", -42.0);

EXPECT_PRINTF("-42.0000", "%#g", -42.0);
EXPECT_PRINTF("-42.0000", "%#G", -42.0);

SPrintf(buffer, "%#a", 16.0);
safe_sprintf(buffer, "%#a", 16.0);
EXPECT_PRINTF(buffer, "%#a", 16.0);
SPrintf(buffer, "%#A", 16.0);
safe_sprintf(buffer, "%#A", 16.0);
EXPECT_PRINTF(buffer, "%#A", 16.0);

// '#' flag is ignored for non-numeric types.
Expand Down Expand Up @@ -251,12 +243,12 @@ TEST(PrintfTest, IntPrecision) {

TEST(PrintfTest, FloatPrecision) {
char buffer[BUFFER_SIZE];
SPrintf(buffer, "%.3e", 1234.5678);
safe_sprintf(buffer, "%.3e", 1234.5678);
EXPECT_PRINTF(buffer, "%.3e", 1234.5678);
EXPECT_PRINTF("1234.568", "%.3f", 1234.5678);
SPrintf(buffer, "%.3g", 1234.5678);
safe_sprintf(buffer, "%.3g", 1234.5678);
EXPECT_PRINTF(buffer, "%.3g", 1234.5678);
SPrintf(buffer, "%.3a", 1234.5678);
safe_sprintf(buffer, "%.3a", 1234.5678);
EXPECT_PRINTF(buffer, "%.3a", 1234.5678);
}

Expand Down
36 changes: 18 additions & 18 deletions test/util-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ using fmt::internal::Arg;
using fmt::internal::MakeArg;

namespace {
std::string GetSystemErrorMessage(int error_code) {
std::string get_system_error(int error_code) {
#if defined(__MINGW32__) || !defined(_WIN32)
return strerror(error_code);
#else
Expand All @@ -64,19 +64,19 @@ template <typename Char>
std::basic_ostream<Char> &operator<<(std::basic_ostream<Char> &os, Test) {
return os << "test";
}
}
} // namespace

TEST(UtilTest, Increment) {
char s[10] = "123";
Increment(s);
increment(s);
EXPECT_STREQ("124", s);
s[2] = '8';
Increment(s);
increment(s);
EXPECT_STREQ("129", s);
Increment(s);
increment(s);
EXPECT_STREQ("130", s);
s[1] = s[2] = '9';
Increment(s);
increment(s);
EXPECT_STREQ("200", s);
}

Expand Down Expand Up @@ -355,7 +355,7 @@ TEST(ArgVisitorTest, VisitInvalidArg) {

// Tests fmt::internal::count_digits for integer type Int.
template <typename Int>
void TestCountDigits(Int) {
void test_count_digits() {
for (Int i = 0; i < 10; ++i)
EXPECT_EQ(1u, fmt::internal::count_digits(i));
for (Int i = 1, n = 1,
Expand All @@ -367,8 +367,8 @@ void TestCountDigits(Int) {
}

TEST(UtilTest, CountDigits) {
TestCountDigits(uint32_t());
TestCountDigits(uint64_t());
test_count_digits<uint32_t>();
test_count_digits<uint64_t>();
}

#ifdef _WIN32
Expand All @@ -387,7 +387,7 @@ TEST(UtilTest, UTF8ToUTF16) {
}

template <typename Converter>
void CheckUTFConversionError(const char *message) {
void check_utf_conversion_error(const char *message) {
fmt::Writer out;
fmt::internal::format_windows_error(out, ERROR_INVALID_PARAMETER, message);
fmt::SystemError error(0, "");
Expand All @@ -401,12 +401,12 @@ void CheckUTFConversionError(const char *message) {
}

TEST(UtilTest, UTF16ToUTF8Error) {
CheckUTFConversionError<fmt::internal::UTF16ToUTF8>(
check_utf_conversion_error<fmt::internal::UTF16ToUTF8>(
"cannot convert string from UTF-16 to UTF-8");
}

TEST(UtilTest, UTF8ToUTF16Error) {
CheckUTFConversionError<fmt::internal::UTF8ToUTF16>(
check_utf_conversion_error<fmt::internal::UTF8ToUTF16>(
"cannot convert string from UTF-8 to UTF-16");
}

Expand Down Expand Up @@ -437,7 +437,7 @@ TEST(UtilTest, StrError) {
EXPECT_EQ(0, result);
std::size_t message_size = std::strlen(message);
EXPECT_GE(BUFFER_SIZE - 1u, message_size);
EXPECT_EQ(GetSystemErrorMessage(error_code), message);
EXPECT_EQ(get_system_error(error_code), message);

// safe_strerror never uses buffer on MinGW.
#ifndef __MINGW32__
Expand All @@ -454,7 +454,7 @@ typedef void (*FormatErrorMessage)(
fmt::Writer &out, int error_code, StringRef message);

template <typename Error>
void CheckThrowError(int error_code, FormatErrorMessage format) {
void check_throw_error(int error_code, FormatErrorMessage format) {
fmt::SystemError error(0, "");
try {
throw Error(error_code, "test {}", "error");
Expand All @@ -471,14 +471,14 @@ TEST(UtilTest, FormatSystemError) {
fmt::Writer message;
fmt::internal::format_system_error(message, EDOM, "test");
EXPECT_EQ(fmt::format("test: {}",
GetSystemErrorMessage(EDOM)), message.str());
get_system_error(EDOM)), message.str());
}

TEST(UtilTest, SystemError) {
fmt::SystemError e(EDOM, "test");
EXPECT_EQ(fmt::format("test: {}", GetSystemErrorMessage(EDOM)), e.what());
EXPECT_EQ(fmt::format("test: {}", get_system_error(EDOM)), e.what());
EXPECT_EQ(EDOM, e.error_code());
CheckThrowError<fmt::SystemError>(EDOM, fmt::internal::format_system_error);
check_throw_error<fmt::SystemError>(EDOM, fmt::internal::format_system_error);
}

TEST(UtilTest, ReportSystemError) {
Expand Down Expand Up @@ -506,7 +506,7 @@ TEST(UtilTest, FormatWindowsError) {
}

TEST(UtilTest, WindowsError) {
CheckThrowError<fmt::WindowsError>(
check_throw_error<fmt::WindowsError>(
ERROR_FILE_EXISTS, fmt::internal::format_windows_error);
}

Expand Down
15 changes: 1 addition & 14 deletions test/util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,9 @@
*/

#include "util.h"
#include <cstdarg>
#include <cstdio>
#include <cstring>

#ifdef _MSC_VER
# define vsnprintf vsprintf_s
#endif

void SPrintf(char *buffer, const char *format, ...) {
std::va_list args;
va_start(args, format);
vsnprintf(buffer, BUFFER_SIZE, format, args);
va_end(args);
}

void Increment(char *s) {
void increment(char *s) {
for (int i = static_cast<int>(std::strlen(s)) - 1; i >= 0; --i) {
if (s[i] != '9') {
++s[i];
Expand Down
19 changes: 17 additions & 2 deletions test/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,24 @@
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include <cstdarg>
#include <cstdio>

enum {BUFFER_SIZE = 256};

void SPrintf(char *buffer, const char *format, ...);
#ifdef _MSC_VER
# define FMT_VSNPRINTF vsprintf_s
#else
# define FMT_VSNPRINTF vsnprintf
#endif

template <std::size_t SIZE>
void safe_sprintf(char (&buffer)[SIZE], const char *format, ...) {
std::va_list args;
va_start(args, format);
FMT_VSNPRINTF(buffer, SIZE, format, args);
va_end(args);
}

// Increment a number in a string.
void Increment(char *s);
void increment(char *s);

0 comments on commit 8f8fd76

Please sign in to comment.