Skip to content

Commit 3acf9b9

Browse files
committed
[libc++][format] Improves tests.
This is based on the last open review comment in D144331 and is applied to all occurrences. Reviewed By: ldionne, #libc Differential Revision: https://reviews.llvm.org/D147885
1 parent a9ddb77 commit 3acf9b9

File tree

19 files changed

+100
-99
lines changed

19 files changed

+100
-99
lines changed

libcxx/test/std/containers/container.adaptors/container.adaptors.format/parse.pass.cpp

+10-10
Original file line numberDiff line numberDiff line change
@@ -39,30 +39,30 @@
3939
#define SV(S) MAKE_STRING_VIEW(CharT, S)
4040

4141
template <class Arg, class StringViewT>
42-
constexpr void test_parse(StringViewT fmt) {
42+
constexpr void test_parse(StringViewT fmt, std::size_t offset) {
4343
using CharT = typename StringViewT::value_type;
4444
auto parse_ctx = std::basic_format_parse_context<CharT>(fmt);
4545
std::formatter<Arg, CharT> formatter;
4646
static_assert(std::semiregular<decltype(formatter)>);
4747

4848
std::same_as<typename StringViewT::iterator> auto it = formatter.parse(parse_ctx);
49-
assert(it == fmt.end() - (!fmt.empty() && fmt.back() == '}'));
49+
assert(it == fmt.end() - offset);
5050
}
5151

5252
template <class StringViewT>
53-
constexpr void test_parse(StringViewT fmt) {
54-
test_parse<std::queue<int>>(fmt);
55-
test_parse<std::priority_queue<int>>(fmt);
56-
test_parse<std::stack<int>>(fmt);
53+
constexpr void test_parse(StringViewT fmt, std::size_t offset) {
54+
test_parse<std::queue<int>>(fmt, offset);
55+
test_parse<std::priority_queue<int>>(fmt, offset);
56+
test_parse<std::stack<int>>(fmt, offset);
5757
}
5858

5959
template <class CharT>
6060
constexpr void test_fmt() {
61-
test_parse(SV(""));
62-
test_parse(SV(":d"));
61+
test_parse(SV(""), 0);
62+
test_parse(SV(":d"), 0);
6363

64-
test_parse(SV("}"));
65-
test_parse(SV(":d}"));
64+
test_parse(SV("}"), 1);
65+
test_parse(SV(":d}"), 1);
6666
}
6767

6868
constexpr bool test() {

libcxx/test/std/containers/sequences/vector.bool/vector.bool.fmt/parse.pass.cpp

+6-6
Original file line numberDiff line numberDiff line change
@@ -36,23 +36,23 @@
3636
#define SV(S) MAKE_STRING_VIEW(CharT, S)
3737

3838
template <class StringViewT>
39-
constexpr void test_parse(StringViewT fmt) {
39+
constexpr void test_parse(StringViewT fmt, std::size_t offset) {
4040
using CharT = typename StringViewT::value_type;
4141
auto parse_ctx = std::basic_format_parse_context<CharT>(fmt);
4242
std::formatter<std::vector<bool, std::allocator<bool>>::reference, CharT> formatter;
4343
static_assert(std::semiregular<decltype(formatter)>);
4444

4545
std::same_as<typename StringViewT::iterator> auto it = formatter.parse(parse_ctx);
46-
assert(it == fmt.end() - (!fmt.empty() && fmt.back() == '}'));
46+
assert(it == fmt.end() - offset);
4747
}
4848

4949
template <class CharT>
5050
constexpr void test_fmt() {
51-
test_parse(SV(""));
52-
test_parse(SV("b"));
51+
test_parse(SV(""), 0);
52+
test_parse(SV("b"), 0);
5353

54-
test_parse(SV("}"));
55-
test_parse(SV("b}"));
54+
test_parse(SV("}"), 1);
55+
test_parse(SV("b}"), 1);
5656
}
5757

5858
constexpr bool test() {

libcxx/test/std/thread/thread.threads/thread.thread.class/thread.thread.id/parse.pass.cpp

+6-6
Original file line numberDiff line numberDiff line change
@@ -35,23 +35,23 @@
3535
#define SV(S) MAKE_STRING_VIEW(CharT, S)
3636

3737
template <class StringViewT>
38-
constexpr void test_parse(StringViewT fmt) {
38+
constexpr void test_parse(StringViewT fmt, std::size_t offset) {
3939
using CharT = typename StringViewT::value_type;
4040
auto parse_ctx = std::basic_format_parse_context<CharT>(fmt);
4141
std::formatter<std::thread::id, CharT> formatter;
4242
static_assert(std::semiregular<decltype(formatter)>);
4343

4444
std::same_as<typename StringViewT::iterator> auto it = formatter.parse(parse_ctx);
45-
assert(it == fmt.end() - (!fmt.empty() && fmt.back() == '}'));
45+
assert(it == fmt.end() - offset);
4646
}
4747

4848
template <class CharT>
4949
constexpr void test_fmt() {
50-
test_parse(SV(""));
51-
test_parse(SV("1"));
50+
test_parse(SV(""), 0);
51+
test_parse(SV("1"), 0);
5252

53-
test_parse(SV("}"));
54-
test_parse(SV("1}"));
53+
test_parse(SV("}"), 1);
54+
test_parse(SV("1}"), 1);
5555
}
5656

5757
constexpr bool test() {

libcxx/test/std/utilities/format/format.formatter/format.formatter.spec/formatter.bool.pass.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,14 @@
3232
#define STR(S) MAKE_STRING(CharT, S)
3333

3434
template <class StringT, class StringViewT>
35-
void test(StringT expected, StringViewT fmt, bool arg) {
35+
void test(StringT expected, StringViewT fmt, bool arg, std::size_t offset) {
3636
using CharT = typename StringT::value_type;
3737
auto parse_ctx = std::basic_format_parse_context<CharT>(fmt);
3838
std::formatter<bool, CharT> formatter;
3939
static_assert(std::semiregular<decltype(formatter)>);
4040

4141
auto it = formatter.parse(parse_ctx);
42-
assert(it == fmt.end() - (!fmt.empty() && fmt.back() == '}'));
42+
assert(it == fmt.end() - offset);
4343

4444
StringT result;
4545
auto out = std::back_inserter(result);
@@ -61,9 +61,9 @@ void test_termination_condition(StringT expected, StringT f, bool arg) {
6161
std::basic_string_view<CharT> fmt{f};
6262
assert(fmt.back() == CharT('}') && "Pre-condition failure");
6363

64-
test(expected, fmt, arg);
64+
test(expected, fmt, arg, 1);
6565
fmt.remove_suffix(1);
66-
test(expected, fmt, arg);
66+
test(expected, fmt, arg, 0);
6767
}
6868

6969
template <class CharT>

libcxx/test/std/utilities/format/format.formatter/format.formatter.spec/formatter.c_string.pass.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,13 @@
3333
#define CSTR(S) MAKE_CSTRING(CharT, S)
3434

3535
template <class T, class StringT, class StringViewT, class CharT>
36-
void test(StringT expected, StringViewT fmt, const CharT* a) {
36+
void test(StringT expected, StringViewT fmt, const CharT* a, std::size_t offset) {
3737
auto parse_ctx = std::basic_format_parse_context<CharT>(fmt);
3838
std::formatter<T, CharT> formatter;
3939
static_assert(std::semiregular<decltype(formatter)>);
4040

4141
auto it = formatter.parse(parse_ctx);
42-
assert(it == fmt.end() - (!fmt.empty() && fmt.back() == '}'));
42+
assert(it == fmt.end() - offset);
4343

4444
StringT result;
4545
auto out = std::back_inserter(result);
@@ -61,9 +61,9 @@ void test_termination_condition(StringT expected, StringT f, const CharT* arg) {
6161
std::basic_string_view<CharT> fmt{f};
6262
assert(fmt.back() == CharT('}') && "Pre-condition failure");
6363

64-
test<ArgumentT>(expected, fmt, arg);
64+
test<ArgumentT>(expected, fmt, arg, 1);
6565
fmt.remove_suffix(1);
66-
test<ArgumentT>(expected, fmt, arg);
66+
test<ArgumentT>(expected, fmt, arg, 0);
6767
}
6868

6969
#if TEST_STD_VER > 20

libcxx/test/std/utilities/format/format.formatter/format.formatter.spec/formatter.char.pass.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,14 @@
3333
#define SV(S) MAKE_STRING_VIEW(CharT, S)
3434

3535
template <class StringT, class StringViewT, class ArgumentT>
36-
void test(StringT expected, StringViewT fmt, ArgumentT arg) {
36+
void test(StringT expected, StringViewT fmt, ArgumentT arg, std::size_t offset) {
3737
using CharT = typename StringT::value_type;
3838
auto parse_ctx = std::basic_format_parse_context<CharT>(fmt);
3939
std::formatter<ArgumentT, CharT> formatter;
4040
static_assert(std::semiregular<decltype(formatter)>);
4141

4242
auto it = formatter.parse(parse_ctx);
43-
assert(it == fmt.end() - (!fmt.empty() && fmt.back() == '}'));
43+
assert(it == fmt.end() - offset);
4444

4545
StringT result;
4646
auto out = std::back_inserter(result);
@@ -62,9 +62,9 @@ void test_termination_condition(StringT expected, StringT f, ArgumentT arg) {
6262
std::basic_string_view<CharT> fmt{f};
6363
assert(fmt.back() == CharT('}') && "Pre-condition failure");
6464

65-
test(expected, fmt, arg);
65+
test(expected, fmt, arg, 1);
6666
fmt.remove_suffix(1);
67-
test(expected, fmt, arg);
67+
test(expected, fmt, arg, 0);
6868
}
6969

7070
#if TEST_STD_VER > 20

libcxx/test/std/utilities/format/format.formatter/format.formatter.spec/formatter.char_array.pass.cpp

+5-4
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,15 @@ struct Tester {
4444
static const std::size_t size = N - 1;
4545

4646
template <class CharT>
47-
void test(const std::basic_string<CharT>& expected, const std::basic_string_view<CharT>& fmt) const {
47+
void
48+
test(const std::basic_string<CharT>& expected, const std::basic_string_view<CharT>& fmt, std::size_t offset) const {
4849
using Str = CharT[size];
4950
std::basic_format_parse_context<CharT> parse_ctx{fmt};
5051
std::formatter<Str, CharT> formatter;
5152
static_assert(std::semiregular<decltype(formatter)>);
5253

5354
auto it = formatter.parse(parse_ctx);
54-
assert(it == fmt.end() - (!fmt.empty() && fmt.back() == '}'));
55+
assert(it == fmt.end() - offset);
5556

5657
std::basic_string<CharT> result;
5758
auto out = std::back_inserter(result);
@@ -76,9 +77,9 @@ struct Tester {
7677
std::basic_string_view<CharT> fmt{f};
7778
assert(fmt.back() == CharT('}') && "Pre-condition failure");
7879

79-
test(expected, fmt);
80+
test(expected, fmt, 1);
8081
fmt.remove_suffix(1);
81-
test(expected, fmt);
82+
test(expected, fmt, 0);
8283
}
8384
};
8485

libcxx/test/std/utilities/format/format.formatter/format.formatter.spec/formatter.floating_point.pass.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,13 @@
4444
#define STR(S) MAKE_STRING(CharT, S)
4545

4646
template <class CharT, class ArithmeticT>
47-
void test(std::basic_string_view<CharT> fmt, ArithmeticT arg, std::basic_string<CharT> expected) {
47+
void test(std::basic_string_view<CharT> fmt, ArithmeticT arg, std::basic_string<CharT> expected, std::size_t offset) {
4848
auto parse_ctx = std::basic_format_parse_context<CharT>(fmt);
4949
std::formatter<ArithmeticT, CharT> formatter;
5050
static_assert(std::semiregular<decltype(formatter)>);
5151

5252
auto it = formatter.parse(parse_ctx);
53-
assert(it == fmt.end() - (!fmt.empty() && fmt.back() == '}'));
53+
assert(it == fmt.end() - offset);
5454

5555
std::basic_string<CharT> result;
5656
auto out = std::back_inserter(result);
@@ -78,9 +78,9 @@ void test_termination_condition(StringT f, ArithmeticT arg, StringT expected = {
7878
std::basic_string_view<CharT> fmt{f};
7979
assert(fmt.back() == CharT('}') && "Pre-condition failure");
8080

81-
test(fmt, arg, expected);
81+
test(fmt, arg, expected, 1);
8282
fmt.remove_suffix(1);
83-
test(fmt, arg, expected);
83+
test(fmt, arg, expected, 0);
8484
}
8585

8686
template <class CharT, class ArithmeticT>

libcxx/test/std/utilities/format/format.formatter/format.formatter.spec/formatter.handle.pass.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,13 @@ struct std::formatter<color> : std::formatter<const char*> {
3838
}
3939
};
4040

41-
void test(std::string expected, std::string_view fmt, color arg) {
41+
void test(std::string expected, std::string_view fmt, color arg, std::size_t offset) {
4242
auto parse_ctx = std::format_parse_context(fmt);
4343
std::formatter<color, char> formatter;
4444
static_assert(std::semiregular<decltype(formatter)>);
4545

4646
auto it = formatter.parse(parse_ctx);
47-
assert(it == fmt.end() - (!fmt.empty() && fmt.back() == '}'));
47+
assert(it == fmt.end() - offset);
4848

4949
std::string result;
5050
auto out = std::back_inserter(result);
@@ -64,9 +64,9 @@ void test_termination_condition(std::string expected, std::string f, color arg)
6464
std::string_view fmt{f};
6565
assert(fmt.back() == '}' && "Pre-condition failure");
6666

67-
test(expected, fmt, arg);
67+
test(expected, fmt, arg, 1);
6868
fmt.remove_suffix(1);
69-
test(expected, fmt, arg);
69+
test(expected, fmt, arg, 0);
7070
}
7171

7272
int main(int, char**) {

libcxx/test/std/utilities/format/format.formatter/format.formatter.spec/formatter.pointer.pass.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,14 @@
3737
#define STR(S) MAKE_STRING(CharT, S)
3838

3939
template <class StringT, class StringViewT, class PointerT>
40-
void test(StringT expected, StringViewT fmt, PointerT arg) {
40+
void test(StringT expected, StringViewT fmt, PointerT arg, std::size_t offset) {
4141
using CharT = typename StringT::value_type;
4242
auto parse_ctx = std::basic_format_parse_context<CharT>(fmt);
4343
std::formatter<PointerT, CharT> formatter;
4444
static_assert(std::semiregular<decltype(formatter)>);
4545

4646
auto it = formatter.parse(parse_ctx);
47-
assert(it == fmt.end() - (!fmt.empty() && fmt.back() == '}'));
47+
assert(it == fmt.end() - offset);
4848

4949
StringT result;
5050
auto out = std::back_inserter(result);
@@ -74,9 +74,9 @@ void test_termination_condition(StringT expected, StringT f, PointerT arg) {
7474
std::basic_string_view<CharT> fmt{f};
7575
assert(fmt.back() == CharT('}') && "Pre-condition failure");
7676

77-
test(expected, fmt, arg);
77+
test(expected, fmt, arg, 1);
7878
fmt.remove_suffix(1);
79-
test(expected, fmt, arg);
79+
test(expected, fmt, arg, 0);
8080
}
8181

8282
template <class CharT>

libcxx/test/std/utilities/format/format.formatter/format.formatter.spec/formatter.signed_integral.pass.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,14 @@
3939
#define STR(S) MAKE_STRING(CharT, S)
4040

4141
template <class StringT, class StringViewT, class ArithmeticT>
42-
void test(StringT expected, StringViewT fmt, ArithmeticT arg) {
42+
void test(StringT expected, StringViewT fmt, ArithmeticT arg, std::size_t offset) {
4343
using CharT = typename StringT::value_type;
4444
auto parse_ctx = std::basic_format_parse_context<CharT>(fmt);
4545
std::formatter<ArithmeticT, CharT> formatter;
4646
static_assert(std::semiregular<decltype(formatter)>);
4747

4848
auto it = formatter.parse(parse_ctx);
49-
assert(it == fmt.end() - (!fmt.empty() && fmt.back() == '}'));
49+
assert(it == fmt.end() - offset);
5050

5151
StringT result;
5252
auto out = std::back_inserter(result);
@@ -68,9 +68,9 @@ void test_termination_condition(StringT expected, StringT f, ArithmeticT arg) {
6868
std::basic_string_view<CharT> fmt{f};
6969
assert(fmt.back() == CharT('}') && "Pre-condition failure");
7070

71-
test(expected, fmt, arg);
71+
test(expected, fmt, arg, 1);
7272
fmt.remove_suffix(1);
73-
test(expected, fmt, arg);
73+
test(expected, fmt, arg, 0);
7474
}
7575

7676
template <class Arithmetic, class CharT>

libcxx/test/std/utilities/format/format.formatter/format.formatter.spec/formatter.string.pass.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
#define CSTR(S) MAKE_CSTRING(CharT, S)
3636

3737
template <class T, class ArgumentT, class StringT, class StringViewT>
38-
void test(StringT expected, StringViewT fmt, StringT a) {
38+
void test(StringT expected, StringViewT fmt, StringT a, std::size_t offset) {
3939
static_assert(
4040
std::same_as<typename T::value_type,
4141
typename std::decay_t<ArgumentT>::value_type> &&
@@ -47,7 +47,7 @@ void test(StringT expected, StringViewT fmt, StringT a) {
4747
static_assert(std::semiregular<decltype(formatter)>);
4848

4949
auto it = formatter.parse(parse_ctx);
50-
assert(it == fmt.end() - (!fmt.empty() && fmt.back() == '}'));
50+
assert(it == fmt.end() - offset);
5151

5252
StringT result;
5353
auto out = std::back_inserter(result);
@@ -70,9 +70,9 @@ void test_termination_condition(StringT expected, StringT f, StringT arg) {
7070
std::basic_string_view<CharT> fmt{f};
7171
assert(fmt.back() == CharT('}') && "Pre-condition failure");
7272

73-
test<T, ArgumentT>(expected, fmt, arg);
73+
test<T, ArgumentT>(expected, fmt, arg, 1);
7474
fmt.remove_suffix(1);
75-
test<T, ArgumentT>(expected, fmt, arg);
75+
test<T, ArgumentT>(expected, fmt, arg, 0);
7676
}
7777

7878
#if TEST_STD_VER > 20

libcxx/test/std/utilities/format/format.formatter/format.formatter.spec/formatter.unsigned_integral.pass.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,14 @@
3939
#define STR(S) MAKE_STRING(CharT, S)
4040

4141
template <class StringT, class StringViewT, class ArithmeticT>
42-
void test(StringT expected, StringViewT fmt, ArithmeticT arg) {
42+
void test(StringT expected, StringViewT fmt, ArithmeticT arg, std::size_t offset) {
4343
using CharT = typename StringT::value_type;
4444
auto parse_ctx = std::basic_format_parse_context<CharT>(fmt);
4545
std::formatter<ArithmeticT, CharT> formatter;
4646
static_assert(std::semiregular<decltype(formatter)>);
4747

4848
auto it = formatter.parse(parse_ctx);
49-
assert(it == fmt.end() - (!fmt.empty() && fmt.back() == '}'));
49+
assert(it == fmt.end() - offset);
5050

5151
StringT result;
5252
auto out = std::back_inserter(result);
@@ -68,9 +68,9 @@ void test_termination_condition(StringT expected, StringT f, ArithmeticT arg) {
6868
std::basic_string_view<CharT> fmt{f};
6969
assert(fmt.back() == CharT('}') && "Pre-condition failure");
7070

71-
test(expected, fmt, arg);
71+
test(expected, fmt, arg, 1);
7272
fmt.remove_suffix(1);
73-
test(expected, fmt, arg);
73+
test(expected, fmt, arg, 0);
7474
}
7575

7676
template <class ArithmeticT, class CharT>

0 commit comments

Comments
 (0)