fmt/test/scan-test.cc

185 lines
4.7 KiB
C++
Raw Normal View History

// Formatting library for C++ - scanning API test
2019-05-15 17:02:40 +00:00
//
// Copyright (c) 2019 - present, Victor Zverovich
2019-05-15 17:02:40 +00:00
// All rights reserved.
//
// For the license information refer to format.h.
2020-05-07 22:59:46 +00:00
#include "scan.h"
#include <time.h>
2020-05-07 22:59:46 +00:00
2019-05-15 17:20:51 +00:00
#include <climits>
2023-12-25 15:18:23 +00:00
#include <thread>
2019-05-15 17:02:40 +00:00
2023-12-02 17:34:27 +00:00
#include "fmt/os.h"
#include "gmock/gmock.h"
2019-05-15 18:05:19 +00:00
#include "gtest-extra.h"
2019-05-15 17:02:40 +00:00
2021-05-06 00:43:06 +00:00
TEST(scan_test, read_text) {
2023-12-02 17:34:27 +00:00
fmt::string_view s = "foo";
2024-01-14 16:51:33 +00:00
auto end = fmt::scan_to(s, "foo");
EXPECT_EQ(end, s.end());
2024-01-14 17:16:18 +00:00
EXPECT_THROW_MSG(fmt::scan<int>("fob", "foo"), fmt::format_error,
2024-01-14 16:51:33 +00:00
"invalid input");
}
2021-05-06 00:43:06 +00:00
TEST(scan_test, read_int) {
2024-01-14 17:16:18 +00:00
EXPECT_EQ(fmt::scan<int>("42", "{}")->value(), 42);
EXPECT_EQ(fmt::scan<int>("-42", "{}")->value(), -42);
EXPECT_EQ(fmt::scan<int>("42", "{:}")->value(), 42);
EXPECT_THROW_MSG(fmt::scan<int>(std::to_string(INT_MAX + 1u), "{}"),
2023-12-22 18:50:01 +00:00
fmt::format_error, "number is too big");
}
2024-01-14 17:16:18 +00:00
TEST(scan_test, read_long_long) {
EXPECT_EQ(fmt::scan<long long>("42", "{}")->value(), 42);
EXPECT_EQ(fmt::scan<long long>("-42", "{}")->value(), -42);
}
2021-05-06 00:43:06 +00:00
TEST(scan_test, read_uint) {
2024-01-14 17:16:18 +00:00
EXPECT_EQ(fmt::scan<unsigned>("42", "{}")->value(), 42);
EXPECT_THROW_MSG(fmt::scan<unsigned>("-42", "{}"), fmt::format_error,
"invalid input");
}
2024-01-14 17:16:18 +00:00
TEST(scan_test, read_ulong_long) {
EXPECT_EQ(fmt::scan<unsigned long long>("42", "{}")->value(), 42);
EXPECT_THROW_MSG(fmt::scan<unsigned long long>("-42", "{}")->value(),
fmt::format_error, "invalid input");
2019-05-15 17:02:40 +00:00
}
2019-06-02 18:11:28 +00:00
2024-01-01 16:39:12 +00:00
TEST(scan_test, read_hex) {
2024-01-14 17:16:18 +00:00
EXPECT_EQ(fmt::scan<unsigned>("2a", "{:x}")->value(), 42);
2024-01-01 17:17:10 +00:00
auto num_digits = std::numeric_limits<unsigned>::digits / 4;
2024-01-14 16:51:33 +00:00
EXPECT_THROW_MSG(
2024-01-14 17:16:18 +00:00
fmt::scan<unsigned>(fmt::format("1{:0{}}", 0, num_digits), "{:x}")
->value(),
2024-01-14 16:51:33 +00:00
fmt::format_error, "number is too big");
2024-01-01 16:39:12 +00:00
}
2021-05-06 00:43:06 +00:00
TEST(scan_test, read_string) {
2024-01-14 17:16:18 +00:00
EXPECT_EQ(fmt::scan<std::string>("foo", "{}")->value(), "foo");
2019-06-02 14:30:26 +00:00
}
2019-05-15 18:05:19 +00:00
2021-05-06 00:43:06 +00:00
TEST(scan_test, read_string_view) {
2024-01-14 17:16:18 +00:00
EXPECT_EQ(fmt::scan<fmt::string_view>("foo", "{}")->value(), "foo");
2019-06-02 18:11:28 +00:00
}
2023-12-23 14:53:25 +00:00
TEST(scan_test, separator) {
int n1 = 0, n2 = 0;
2024-01-14 16:51:33 +00:00
fmt::scan_to("10 20", "{} {}", n1, n2);
2023-12-23 14:53:25 +00:00
EXPECT_EQ(n1, 10);
EXPECT_EQ(n2, 20);
}
struct num {
int value;
};
namespace fmt {
2023-12-23 14:53:25 +00:00
template <> struct scanner<num> {
bool hex = false;
2023-11-25 15:41:04 +00:00
auto parse(scan_parse_context& ctx) -> scan_parse_context::iterator {
2023-12-23 14:53:25 +00:00
auto it = ctx.begin(), end = ctx.end();
2024-01-14 16:11:13 +00:00
if (it != end && *it == 'x') {
hex = true;
++it;
}
if (it != end && *it != '}') report_error("invalid format");
2023-12-23 14:53:25 +00:00
return it;
}
template <class ScanContext>
2023-12-29 14:25:57 +00:00
auto scan(num& n, ScanContext& ctx) const -> typename ScanContext::iterator {
2024-01-14 17:16:18 +00:00
return hex ? scan_to(ctx, "{:x}", n.value) : scan_to(ctx, "{}", n.value);
}
};
} // namespace fmt
2021-05-06 00:43:06 +00:00
TEST(scan_test, read_custom) {
2024-01-14 17:16:18 +00:00
EXPECT_EQ(fmt::scan<num>("42", "{}")->value().value, 42);
EXPECT_EQ(fmt::scan<num>("2a", "{:x}")->value().value, 42);
}
2021-05-06 00:43:06 +00:00
TEST(scan_test, invalid_format) {
2024-01-14 16:51:33 +00:00
EXPECT_THROW_MSG(fmt::scan_to("", "{}"), fmt::format_error,
2019-05-15 18:05:19 +00:00
"argument index out of range");
2024-01-14 16:51:33 +00:00
EXPECT_THROW_MSG(fmt::scan_to("", "{"), fmt::format_error,
2019-05-15 18:05:19 +00:00
"invalid format string");
}
2019-06-02 14:30:26 +00:00
2024-02-10 15:38:29 +00:00
namespace std {
using fmt::scan;
using fmt::scan_error;
}
2021-05-06 00:43:06 +00:00
TEST(scan_test, example) {
2024-02-04 15:44:11 +00:00
// Example from https://wg21.link/p1729r3.
2024-02-10 15:38:29 +00:00
if (auto result = std::scan<std::string, int>("answer = 42", "{} = {}")) {
auto range = result->range();
EXPECT_EQ(range.begin(), range.end());
EXPECT_EQ(result->begin(), result->end());
2024-02-04 15:44:11 +00:00
#ifdef __cpp_structured_bindings
const auto& [key, value] = result->values();
EXPECT_EQ(key, "answer");
EXPECT_EQ(value, 42);
#endif
} else {
2024-02-10 15:38:29 +00:00
std::scan_error error = result.error();
(void)error;
2024-02-04 15:44:11 +00:00
FAIL();
}
2019-06-02 14:30:26 +00:00
}
2023-11-26 17:22:31 +00:00
2024-01-14 17:16:18 +00:00
TEST(scan_test, end_of_input) { fmt::scan<int>("", "{}"); }
2023-12-25 15:18:23 +00:00
2023-11-26 17:22:31 +00:00
#if FMT_USE_FCNTL
TEST(scan_test, file) {
2024-01-01 21:25:07 +00:00
auto pipe = fmt::pipe();
2023-12-24 15:32:27 +00:00
fmt::string_view input = "10 20";
2024-01-01 21:25:07 +00:00
pipe.write_end.write(input.data(), input.size());
pipe.write_end.close();
2023-12-24 15:32:27 +00:00
int n1 = 0, n2 = 0;
2024-01-01 21:25:07 +00:00
fmt::buffered_file f = pipe.read_end.fdopen("r");
2024-01-14 17:16:18 +00:00
fmt::scan_to(f.get(), "{} {}", n1, n2);
2023-12-24 15:32:27 +00:00
EXPECT_EQ(n1, 10);
EXPECT_EQ(n2, 20);
2023-11-26 17:22:31 +00:00
}
2023-12-25 16:39:14 +00:00
TEST(scan_test, lock) {
2024-01-01 21:25:07 +00:00
auto pipe = fmt::pipe();
2023-12-25 16:39:14 +00:00
std::thread producer([&]() {
fmt::string_view input = "42 ";
2024-01-01 21:25:07 +00:00
for (int i = 0; i < 1000; ++i)
pipe.write_end.write(input.data(), input.size());
pipe.write_end.close();
2023-12-25 16:39:14 +00:00
});
2023-12-29 16:22:55 +00:00
std::atomic<int> count(0);
2024-01-01 21:25:07 +00:00
fmt::buffered_file f = pipe.read_end.fdopen("r");
2023-12-25 16:39:14 +00:00
auto fun = [&]() {
int value = 0;
2024-01-14 17:16:18 +00:00
while (fmt::scan_to(f.get(), "{}", value)) {
2023-12-25 16:39:14 +00:00
if (value != 42) {
2024-01-01 21:25:07 +00:00
pipe.read_end.close();
2023-12-25 16:39:14 +00:00
EXPECT_EQ(value, 42);
break;
}
2023-12-29 16:22:55 +00:00
++count;
2023-12-25 16:39:14 +00:00
}
};
std::thread consumer1(fun);
std::thread consumer2(fun);
2024-01-14 14:34:25 +00:00
2023-12-25 16:39:14 +00:00
producer.join();
consumer1.join();
consumer2.join();
2023-12-29 16:22:55 +00:00
EXPECT_EQ(count, 1000);
2023-12-25 16:39:14 +00:00
}
2023-11-26 17:22:31 +00:00
#endif // FMT_USE_FCNTL