Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
F
fmt
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Libraries
fmt
Commits
8f8fd769
Commit
8f8fd769
authored
Jul 29, 2014
by
Victor Zverovich
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix Fix
https://github.com/cppformat/cppformat/issues/50
in tests too.
parent
24d6baa6
Changes
9
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
226 additions
and
239 deletions
+226
-239
test/format-test.cc
test/format-test.cc
+68
-75
test/gtest-extra-test.cc
test/gtest-extra-test.cc
+93
-93
test/gtest-extra.cc
test/gtest-extra.cc
+7
-7
test/gtest-extra.h
test/gtest-extra.h
+4
-4
test/macro-test.cc
test/macro-test.cc
+9
-9
test/printf-test.cc
test/printf-test.cc
+9
-17
test/util-test.cc
test/util-test.cc
+18
-18
test/util.cc
test/util.cc
+1
-14
test/util.h
test/util.h
+17
-2
No files found.
test/format-test.cc
View file @
8f8fd769
This diff is collapsed.
Click to expand it.
test/gtest-extra-test.cc
View file @
8f8fd769
This diff is collapsed.
Click to expand it.
test/gtest-extra.cc
View file @
8f8fd769
...
...
@@ -31,7 +31,7 @@
using
fmt
::
File
;
void
OutputRedirect
::
F
lush
()
{
void
OutputRedirect
::
f
lush
()
{
#if EOF != -1
# error "FMT_RETRY assumes return value of -1 indicating failure"
#endif
...
...
@@ -41,17 +41,17 @@ void OutputRedirect::Flush() {
throw
fmt
::
SystemError
(
errno
,
"cannot flush stream"
);
}
void
OutputRedirect
::
R
estore
()
{
void
OutputRedirect
::
r
estore
()
{
if
(
original_
.
descriptor
()
==
-
1
)
return
;
// Already restored.
F
lush
();
f
lush
();
// Restore the original file.
original_
.
dup2
(
FMT_POSIX
(
fileno
(
file_
)));
original_
.
close
();
}
OutputRedirect
::
OutputRedirect
(
FILE
*
file
)
:
file_
(
file
)
{
F
lush
();
f
lush
();
int
fd
=
FMT_POSIX
(
fileno
(
file
));
// Create a File object referring to the original file.
original_
=
File
::
dup
(
fd
);
...
...
@@ -64,15 +64,15 @@ OutputRedirect::OutputRedirect(FILE *file) : file_(file) {
OutputRedirect
::~
OutputRedirect
()
FMT_NOEXCEPT
(
true
)
{
try
{
R
estore
();
r
estore
();
}
catch
(
const
std
::
exception
&
e
)
{
std
::
fputs
(
e
.
what
(),
stderr
);
}
}
std
::
string
OutputRedirect
::
RestoreAndR
ead
()
{
std
::
string
OutputRedirect
::
restore_and_r
ead
()
{
// Restore output.
R
estore
();
r
estore
();
// Read everything from the pipe.
std
::
string
content
;
...
...
test/gtest-extra.h
View file @
8f8fd769
...
...
@@ -99,8 +99,8 @@ class OutputRedirect {
GTEST_DISALLOW_COPY_AND_ASSIGN_
(
OutputRedirect
);
void
F
lush
();
void
R
estore
();
void
f
lush
();
void
r
estore
();
public:
explicit
OutputRedirect
(
FILE
*
file
);
...
...
@@ -108,7 +108,7 @@ class OutputRedirect {
// Restores the original file, reads output from the pipe into a string
// and returns it.
std
::
string
RestoreAndR
ead
();
std
::
string
restore_and_r
ead
();
};
#define FMT_TEST_WRITE_(statement, expected_output, file, fail) \
...
...
@@ -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.
RestoreAndR
ead(); \
std::string gtest_output = gtest_redir.
restore_and_r
ead(); \
if (gtest_output != gtest_expected_output) { \
gtest_ar \
<< #statement " produces different output.\n" \
...
...
test/macro-test.cc
View file @
8f8fd769
...
...
@@ -71,23 +71,23 @@ int result;
result += args[i].int_value; \
}
MAKE_TEST
(
TestF
unc
)
MAKE_TEST
(
test_f
unc
)
typedef
char
Char
;
FMT_WRAP1
(
TestF
unc
,
const
char
*
,
1
)
FMT_WRAP1
(
test_f
unc
,
const
char
*
,
1
)
TEST
(
UtilTest
,
Wrap1
)
{
result
=
0
;
TestF
unc
(
""
,
42
);
test_f
unc
(
""
,
42
);
EXPECT_EQ
(
42
,
result
);
}
MAKE_TEST
(
TestVariadicV
oid
)
FMT_VARIADIC_VOID
(
TestVariadicV
oid
,
const
char
*
)
MAKE_TEST
(
test_variadic_v
oid
)
FMT_VARIADIC_VOID
(
test_variadic_v
oid
,
const
char
*
)
TEST
(
UtilTest
,
VariadicVoid
)
{
result
=
0
;
TestVariadicV
oid
(
""
,
10
,
20
,
30
,
40
,
50
,
60
,
70
,
80
,
90
,
100
);
test_variadic_v
oid
(
""
,
10
,
20
,
30
,
40
,
50
,
60
,
70
,
80
,
90
,
100
);
EXPECT_EQ
(
550
,
result
);
}
...
...
@@ -96,18 +96,18 @@ struct S {};
#define GET_TYPE(n) S<n>
int
TestV
ariadic
(
FMT_GEN
(
10
,
GET_TYPE
),
const
fmt
::
ArgList
&
args
)
{
\
int
test_v
ariadic
(
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
,
TestV
ariadic
,
FMT_VARIADIC
(
int
,
test_v
ariadic
,
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
,
TestV
ariadic
(
FMT_GEN
(
10
,
MAKE_ARG
),
EXPECT_EQ
(
550
,
test_v
ariadic
(
FMT_GEN
(
10
,
MAKE_ARG
),
10
,
20
,
30
,
40
,
50
,
60
,
70
,
80
,
90
,
100
));
}
test/printf-test.cc
View file @
8f8fd769
...
...
@@ -35,18 +35,10 @@
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
MakeP
ositional
(
fmt
::
StringRef
format
)
{
std
::
string
make_p
ositional
(
fmt
::
StringRef
format
)
{
std
::
string
s
(
format
);
s
.
replace
(
s
.
find
(
'%'
),
1
,
"%1$"
);
return
s
;
...
...
@@ -54,7 +46,7 @@ std::string MakePositional(fmt::StringRef format) {
#define EXPECT_PRINTF(expected_output, format, arg) \
EXPECT_EQ(expected_output, fmt::sprintf(format, arg)); \
EXPECT_EQ(expected_output, fmt::sprintf(
MakeP
ositional(format), arg))
EXPECT_EQ(expected_output, fmt::sprintf(
make_p
ositional(format), arg))
TEST
(
PrintfTest
,
NoArgs
)
{
EXPECT_EQ
(
"test"
,
fmt
::
sprintf
(
"test"
));
...
...
@@ -187,17 +179,17 @@ TEST(PrintfTest, HashFlag) {
EXPECT_PRINTF
(
"-42.000000"
,
"%#F"
,
-
42.0
);
char
buffer
[
BUFFER_SIZE
];
SP
rintf
(
buffer
,
"%#e"
,
-
42.0
);
safe_sp
rintf
(
buffer
,
"%#e"
,
-
42.0
);
EXPECT_PRINTF
(
buffer
,
"%#e"
,
-
42.0
);
SP
rintf
(
buffer
,
"%#E"
,
-
42.0
);
safe_sp
rintf
(
buffer
,
"%#E"
,
-
42.0
);
EXPECT_PRINTF
(
buffer
,
"%#E"
,
-
42.0
);
EXPECT_PRINTF
(
"-42.0000"
,
"%#g"
,
-
42.0
);
EXPECT_PRINTF
(
"-42.0000"
,
"%#G"
,
-
42.0
);
SP
rintf
(
buffer
,
"%#a"
,
16.0
);
safe_sp
rintf
(
buffer
,
"%#a"
,
16.0
);
EXPECT_PRINTF
(
buffer
,
"%#a"
,
16.0
);
SP
rintf
(
buffer
,
"%#A"
,
16.0
);
safe_sp
rintf
(
buffer
,
"%#A"
,
16.0
);
EXPECT_PRINTF
(
buffer
,
"%#A"
,
16.0
);
// '#' flag is ignored for non-numeric types.
...
...
@@ -251,12 +243,12 @@ TEST(PrintfTest, IntPrecision) {
TEST
(
PrintfTest
,
FloatPrecision
)
{
char
buffer
[
BUFFER_SIZE
];
SP
rintf
(
buffer
,
"%.3e"
,
1234.5678
);
safe_sp
rintf
(
buffer
,
"%.3e"
,
1234.5678
);
EXPECT_PRINTF
(
buffer
,
"%.3e"
,
1234.5678
);
EXPECT_PRINTF
(
"1234.568"
,
"%.3f"
,
1234.5678
);
SP
rintf
(
buffer
,
"%.3g"
,
1234.5678
);
safe_sp
rintf
(
buffer
,
"%.3g"
,
1234.5678
);
EXPECT_PRINTF
(
buffer
,
"%.3g"
,
1234.5678
);
SP
rintf
(
buffer
,
"%.3a"
,
1234.5678
);
safe_sp
rintf
(
buffer
,
"%.3a"
,
1234.5678
);
EXPECT_PRINTF
(
buffer
,
"%.3a"
,
1234.5678
);
}
...
...
test/util-test.cc
View file @
8f8fd769
...
...
@@ -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
...
...
@@ -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"
;
I
ncrement
(
s
);
i
ncrement
(
s
);
EXPECT_STREQ
(
"124"
,
s
);
s
[
2
]
=
'8'
;
I
ncrement
(
s
);
i
ncrement
(
s
);
EXPECT_STREQ
(
"129"
,
s
);
I
ncrement
(
s
);
i
ncrement
(
s
);
EXPECT_STREQ
(
"130"
,
s
);
s
[
1
]
=
s
[
2
]
=
'9'
;
I
ncrement
(
s
);
i
ncrement
(
s
);
EXPECT_STREQ
(
"200"
,
s
);
}
...
...
@@ -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
,
...
...
@@ -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
...
...
@@ -387,7 +387,7 @@ TEST(UtilTest, UTF8ToUTF16) {
}
template
<
typename
Converter
>
void
CheckUTFConversionE
rror
(
const
char
*
message
)
{
void
check_utf_conversion_e
rror
(
const
char
*
message
)
{
fmt
::
Writer
out
;
fmt
::
internal
::
format_windows_error
(
out
,
ERROR_INVALID_PARAMETER
,
message
);
fmt
::
SystemError
error
(
0
,
""
);
...
...
@@ -401,12 +401,12 @@ void CheckUTFConversionError(const char *message) {
}
TEST
(
UtilTest
,
UTF16ToUTF8Error
)
{
CheckUTFConversionE
rror
<
fmt
::
internal
::
UTF16ToUTF8
>
(
check_utf_conversion_e
rror
<
fmt
::
internal
::
UTF16ToUTF8
>
(
"cannot convert string from UTF-16 to UTF-8"
);
}
TEST
(
UtilTest
,
UTF8ToUTF16Error
)
{
CheckUTFConversionE
rror
<
fmt
::
internal
::
UTF8ToUTF16
>
(
check_utf_conversion_e
rror
<
fmt
::
internal
::
UTF8ToUTF16
>
(
"cannot convert string from UTF-8 to UTF-16"
);
}
...
...
@@ -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__
...
...
@@ -454,7 +454,7 @@ typedef void (*FormatErrorMessage)(
fmt
::
Writer
&
out
,
int
error_code
,
StringRef
message
);
template
<
typename
Error
>
void
CheckThrowE
rror
(
int
error_code
,
FormatErrorMessage
format
)
{
void
check_throw_e
rror
(
int
error_code
,
FormatErrorMessage
format
)
{
fmt
::
SystemError
error
(
0
,
""
);
try
{
throw
Error
(
error_code
,
"test {}"
,
"error"
);
...
...
@@ -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
());
CheckThrowE
rror
<
fmt
::
SystemError
>
(
EDOM
,
fmt
::
internal
::
format_system_error
);
check_throw_e
rror
<
fmt
::
SystemError
>
(
EDOM
,
fmt
::
internal
::
format_system_error
);
}
TEST
(
UtilTest
,
ReportSystemError
)
{
...
...
@@ -506,7 +506,7 @@ TEST(UtilTest, FormatWindowsError) {
}
TEST
(
UtilTest
,
WindowsError
)
{
CheckThrowE
rror
<
fmt
::
WindowsError
>
(
check_throw_e
rror
<
fmt
::
WindowsError
>
(
ERROR_FILE_EXISTS
,
fmt
::
internal
::
format_windows_error
);
}
...
...
test/util.cc
View file @
8f8fd769
...
...
@@ -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
];
...
...
test/util.h
View file @
8f8fd769
...
...
@@ -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
I
ncrement
(
char
*
s
);
void
i
ncrement
(
char
*
s
);
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment