enum_name
Loading...
Searching...
No Matches
util.h
1// Formatting library for C++ - test utilities
2//
3// Copyright (c) 2012 - present, Victor Zverovich
4// All rights reserved.
5//
6// For the license information refer to format.h.
7
8#include <cstdarg>
9#include <cstdio>
10#include <locale>
11#include <string>
12
13#include "fmt/os.h"
14
15#ifdef _MSC_VER
16# define FMT_VSNPRINTF vsprintf_s
17#else
18# define FMT_VSNPRINTF vsnprintf
19#endif
20
21template <size_t SIZE>
22void safe_sprintf(char (&buffer)[SIZE], const char* format, ...) {
23 std::va_list args;
24 va_start(args, format);
25 FMT_VSNPRINTF(buffer, SIZE, format, args);
26 va_end(args);
27}
28
29extern const char* const file_content;
30
31// Opens a buffered file for reading.
32auto open_buffered_file(FILE** fp = nullptr) -> fmt::buffered_file;
33
34inline auto safe_fopen(const char* filename, const char* mode) -> FILE* {
35#if defined(_WIN32) && !defined(__MINGW32__)
36 // Fix MSVC warning about "unsafe" fopen.
37 FILE* f = nullptr;
38 errno = fopen_s(&f, filename, mode);
39 return f;
40#else
41 return std::fopen(filename, mode);
42#endif
43}
44
45template <typename Char> class basic_test_string {
46 private:
47 std::basic_string<Char> value_;
48
49 static const Char empty[];
50
51 public:
52 explicit basic_test_string(const Char* value = empty) : value_(value) {}
53
54 auto value() const -> const std::basic_string<Char>& { return value_; }
55};
56
57template <typename Char> const Char basic_test_string<Char>::empty[] = {0};
58
61
62template <typename Char>
63auto operator<<(std::basic_ostream<Char>& os, const basic_test_string<Char>& s)
65 os << s.value();
66 return os;
67}
68
69class date {
70 int year_, month_, day_;
71
72 public:
73 date(int year, int month, int day) : year_(year), month_(month), day_(day) {}
74
75 auto year() const -> int { return year_; }
76 auto month() const -> int { return month_; }
77 auto day() const -> int { return day_; }
78};
79
80// Returns a locale with the given name if available or classic locale
81// otherwise.
82auto get_locale(const char* name, const char* alt_name = nullptr)
83 -> std::locale;
Definition util.h:45
Definition util.h:69
Definition doctest.h:530