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
9f70fc3e
Commit
9f70fc3e
authored
Mar 16, 2020
by
Victor Zverovich
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Minor tweaks for dynamic_format_arg_store
parent
6012dc9a
Changes
4
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
236 additions
and
259 deletions
+236
-259
include/fmt/core.h
include/fmt/core.h
+158
-175
test/CMakeLists.txt
test/CMakeLists.txt
+0
-1
test/core-test.cc
test/core-test.cc
+78
-2
test/format-dyn-args-test.cc
test/format-dyn-args-test.cc
+0
-81
No files found.
include/fmt/core.h
View file @
9f70fc3e
This diff is collapsed.
Click to expand it.
test/CMakeLists.txt
View file @
9f70fc3e
...
@@ -95,7 +95,6 @@ add_fmt_test(grisu-test)
...
@@ -95,7 +95,6 @@ add_fmt_test(grisu-test)
target_compile_definitions
(
grisu-test PRIVATE FMT_USE_GRISU=1
)
target_compile_definitions
(
grisu-test PRIVATE FMT_USE_GRISU=1
)
add_fmt_test
(
gtest-extra-test
)
add_fmt_test
(
gtest-extra-test
)
add_fmt_test
(
format-test mock-allocator.h
)
add_fmt_test
(
format-test mock-allocator.h
)
add_fmt_test
(
format-dyn-args-test
)
if
(
MSVC
)
if
(
MSVC
)
target_compile_options
(
format-test PRIVATE /bigobj
)
target_compile_options
(
format-test PRIVATE /bigobj
)
endif
()
endif
()
...
...
test/core-test.cc
View file @
9f70fc3e
...
@@ -15,9 +15,8 @@
...
@@ -15,9 +15,8 @@
#include <string>
#include <string>
#include <type_traits>
#include <type_traits>
#include "test-assert.h"
#include "gmock.h"
#include "gmock.h"
#include "test-assert.h"
// Check if fmt/core.h compiles with windows.h included before it.
// Check if fmt/core.h compiles with windows.h included before it.
#ifdef _WIN32
#ifdef _WIN32
...
@@ -402,6 +401,83 @@ TEST(ArgTest, VisitInvalidArg) {
...
@@ -402,6 +401,83 @@ TEST(ArgTest, VisitInvalidArg) {
fmt
::
visit_format_arg
(
visitor
,
arg
);
fmt
::
visit_format_arg
(
visitor
,
arg
);
}
}
TEST
(
FormatDynArgsTest
,
Basic
)
{
fmt
::
dynamic_format_arg_store
<
fmt
::
format_context
>
store
;
store
.
push_back
(
42
);
store
.
push_back
(
"abc1"
);
store
.
push_back
(
1.5
f
);
std
::
string
result
=
fmt
::
vformat
(
"{} and {} and {}"
,
store
);
EXPECT_EQ
(
"42 and abc1 and 1.5"
,
result
);
}
TEST
(
FormatDynArgsTest
,
StringsAndRefs
)
{
// Unfortunately the tests are compiled with old ABI so strings use COW.
fmt
::
dynamic_format_arg_store
<
fmt
::
format_context
>
store
;
char
str
[]
=
"1234567890"
;
store
.
push_back
(
str
);
store
.
push_back
(
std
::
cref
(
str
));
store
.
push_back
(
fmt
::
string_view
{
str
});
str
[
0
]
=
'X'
;
std
::
string
result
=
fmt
::
vformat
(
"{} and {} and {}"
,
store
);
EXPECT_EQ
(
"1234567890 and X234567890 and X234567890"
,
result
);
}
struct
custom_type
{
int
i
=
0
;
};
FMT_BEGIN_NAMESPACE
template
<
>
struct
formatter
<
custom_type
>
{
auto
parse
(
format_parse_context
&
ctx
)
const
->
decltype
(
ctx
.
begin
())
{
return
ctx
.
begin
();
}
template
<
typename
FormatContext
>
auto
format
(
const
custom_type
&
p
,
FormatContext
&
ctx
)
->
decltype
(
format_to
(
ctx
.
out
(),
std
::
declval
<
typename
FormatContext
::
char_type
const
*>
()))
{
return
format_to
(
ctx
.
out
(),
"cust={}"
,
p
.
i
);
}
};
FMT_END_NAMESPACE
TEST
(
FormatDynArgsTest
,
CustomFormat
)
{
fmt
::
dynamic_format_arg_store
<
fmt
::
format_context
>
store
;
custom_type
c
{};
store
.
push_back
(
c
);
++
c
.
i
;
store
.
push_back
(
c
);
++
c
.
i
;
store
.
push_back
(
std
::
cref
(
c
));
++
c
.
i
;
std
::
string
result
=
fmt
::
vformat
(
"{} and {} and {}"
,
store
);
EXPECT_EQ
(
"cust=0 and cust=1 and cust=3"
,
result
);
}
TEST
(
FormatDynArgsTest
,
NamedArgByRef
)
{
fmt
::
dynamic_format_arg_store
<
fmt
::
format_context
>
store
;
// Note: fmt::arg() constructs an object which holds a reference
// to its value. It's not an aggregate, so it doesn't extend the
// reference lifetime. As a result, it's a very bad idea passing temporary
// as a named argument value. Only GCC with optimization level >0
// complains about this.
//
// A real life usecase is when you have both name and value alive
// guarantee their lifetime and thus don't want them to be copied into
// storages.
int
a1_val
{
42
};
auto
a1
=
fmt
::
arg
(
"a1_"
,
a1_val
);
store
.
push_back
(
std
::
cref
(
a1
));
std
::
string
result
=
fmt
::
vformat
(
"{a1_}"
,
// and {} and {}",
store
);
EXPECT_EQ
(
"42"
,
result
);
}
TEST
(
StringViewTest
,
ValueType
)
{
TEST
(
StringViewTest
,
ValueType
)
{
static_assert
(
std
::
is_same
<
string_view
::
value_type
,
char
>::
value
,
""
);
static_assert
(
std
::
is_same
<
string_view
::
value_type
,
char
>::
value
,
""
);
}
}
...
...
test/format-dyn-args-test.cc
View file @
9f70fc3e
...
@@ -4,84 +4,3 @@
...
@@ -4,84 +4,3 @@
#include <fmt/core.h>
#include <fmt/core.h>
#include "gtest-extra.h"
#include "gtest-extra.h"
TEST
(
FormatDynArgsTest
,
Basic
)
{
fmt
::
dynamic_format_arg_store
<
fmt
::
format_context
>
store
;
store
.
push_back
(
42
);
store
.
push_back
(
"abc1"
);
store
.
push_back
(
1.5
f
);
std
::
string
result
=
fmt
::
vformat
(
"{} and {} and {}"
,
store
);
EXPECT_EQ
(
"42 and abc1 and 1.5"
,
result
);
}
TEST
(
FormatDynArgsTest
,
StringsAndRefs
)
{
// Unfortunately the tests are compiled with old ABI
// So strings use COW.
fmt
::
dynamic_format_arg_store
<
fmt
::
format_context
>
store
;
char
str
[]{
"1234567890"
};
store
.
push_back
(
str
);
store
.
push_back
(
std
::
cref
(
str
));
store
.
push_back
(
fmt
::
string_view
{
str
});
str
[
0
]
=
'X'
;
std
::
string
result
=
fmt
::
vformat
(
"{} and {} and {}"
,
store
);
EXPECT_EQ
(
"1234567890 and X234567890 and X234567890"
,
result
);
}
struct
custom_type
{
int
i
{
0
};
};
FMT_BEGIN_NAMESPACE
template
<
>
struct
formatter
<
custom_type
>
{
auto
parse
(
format_parse_context
&
ctx
)
const
->
decltype
(
ctx
.
begin
())
{
return
ctx
.
begin
();
}
template
<
typename
FormatContext
>
auto
format
(
const
custom_type
&
p
,
FormatContext
&
ctx
)
->
decltype
(
format_to
(
ctx
.
out
(),
std
::
declval
<
typename
FormatContext
::
char_type
const
*>
()))
{
return
format_to
(
ctx
.
out
(),
"cust={}"
,
p
.
i
);
}
};
FMT_END_NAMESPACE
TEST
(
FormatDynArgsTest
,
CustomFormat
)
{
fmt
::
dynamic_format_arg_store
<
fmt
::
format_context
>
store
;
custom_type
c
{};
store
.
push_back
(
c
);
++
c
.
i
;
store
.
push_back
(
c
);
++
c
.
i
;
store
.
push_back
(
std
::
cref
(
c
));
++
c
.
i
;
std
::
string
result
=
fmt
::
vformat
(
"{} and {} and {}"
,
store
);
EXPECT_EQ
(
"cust=0 and cust=1 and cust=3"
,
result
);
}
TEST
(
FormatDynArgsTest
,
NamedArgByRef
)
{
fmt
::
dynamic_format_arg_store
<
fmt
::
format_context
>
store
;
// Note: fmt::arg() constructs an object which holds a reference
// to its value. It's not an aggregate, so it doesn't extend the
// reference lifetime. As a result, it's a very bad idea passing temporary
// as a named argument value. Only GCC with optimization level >0
// complains about this.
//
// A real life usecase is when you have both name and value alive
// guarantee their lifetime and thus don't want them to be copied into
// storages.
int
a1_val
{
42
};
auto
a1
=
fmt
::
arg
(
"a1_"
,
a1_val
);
store
.
push_back
(
std
::
cref
(
a1
));
std
::
string
result
=
fmt
::
vformat
(
"{a1_}"
,
// and {} and {}",
store
);
EXPECT_EQ
(
"42"
,
result
);
}
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