]> git.proxmox.com Git - ceph.git/blame - ceph/src/seastar/fmt/test/util.h
import ceph 14.2.5
[ceph.git] / ceph / src / seastar / fmt / test / util.h
CommitLineData
11fdf7f2
TL
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 <string>
11
12#include "fmt/posix.h"
13
14enum {BUFFER_SIZE = 256};
15
16#ifdef _MSC_VER
17# define FMT_VSNPRINTF vsprintf_s
18#else
19# define FMT_VSNPRINTF vsnprintf
20#endif
21
22template <std::size_t SIZE>
23void safe_sprintf(char (&buffer)[SIZE], const char *format, ...) {
24 std::va_list args;
25 va_start(args, format);
26 FMT_VSNPRINTF(buffer, SIZE, format, args);
27 va_end(args);
28}
29
30// Increment a number in a string.
31void increment(char *s);
32
33std::string get_system_error(int error_code);
34
35extern const char *const FILE_CONTENT;
36
37// Opens a buffered file for reading.
eafe8130 38fmt::buffered_file open_buffered_file(FILE **fp = FMT_NULL);
11fdf7f2
TL
39
40inline FILE *safe_fopen(const char *filename, const char *mode) {
41#if defined(_WIN32) && !defined(__MINGW32__)
42 // Fix MSVC warning about "unsafe" fopen.
43 FILE *f = 0;
44 errno = fopen_s(&f, filename, mode);
45 return f;
46#else
47 return std::fopen(filename, mode);
48#endif
49}
50
51template <typename Char>
52class BasicTestString {
53 private:
54 std::basic_string<Char> value_;
55
56 static const Char EMPTY[];
57
58 public:
59 explicit BasicTestString(const Char *value = EMPTY) : value_(value) {}
60
61 const std::basic_string<Char> &value() const { return value_; }
62};
63
64template <typename Char>
65const Char BasicTestString<Char>::EMPTY[] = {0};
66
67typedef BasicTestString<char> TestString;
68typedef BasicTestString<wchar_t> TestWString;
69
70template <typename Char>
71std::basic_ostream<Char> &operator<<(
72 std::basic_ostream<Char> &os, const BasicTestString<Char> &s) {
73 os << s.value();
74 return os;
75}
76
77class Date {
78 int year_, month_, day_;
79 public:
80 Date(int year, int month, int day) : year_(year), month_(month), day_(day) {}
81
82 int year() const { return year_; }
83 int month() const { return month_; }
84 int day() const { return day_; }
85};