2019-06-08 13:50:03 +00:00
|
|
|
// Formatting library for C++ - scanning API test
|
2019-05-15 17:02:40 +00:00
|
|
|
//
|
2019-06-08 13:50:03 +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"
|
|
|
|
|
2019-06-08 13:50:03 +00:00
|
|
|
#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"
|
2021-04-28 22:59:43 +00:00
|
|
|
#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";
|
2019-05-19 14:02:41 +00:00
|
|
|
auto end = fmt::scan(s, "foo");
|
|
|
|
EXPECT_EQ(end, s.end());
|
|
|
|
EXPECT_THROW_MSG(fmt::scan("fob", "foo"), fmt::format_error, "invalid input");
|
|
|
|
}
|
|
|
|
|
2021-05-06 00:43:06 +00:00
|
|
|
TEST(scan_test, read_int) {
|
2023-12-02 17:34:27 +00:00
|
|
|
int n = 0;
|
2019-05-15 17:02:40 +00:00
|
|
|
fmt::scan("42", "{}", n);
|
|
|
|
EXPECT_EQ(n, 42);
|
2019-05-19 14:02:41 +00:00
|
|
|
fmt::scan("-42", "{}", n);
|
|
|
|
EXPECT_EQ(n, -42);
|
2023-12-22 18:50:01 +00:00
|
|
|
EXPECT_THROW_MSG(fmt::scan(std::to_string(INT_MAX + 1u), "{}", n),
|
|
|
|
fmt::format_error, "number is too big");
|
2019-05-19 14:02:41 +00:00
|
|
|
}
|
|
|
|
|
2021-05-06 00:43:06 +00:00
|
|
|
TEST(scan_test, read_longlong) {
|
2019-05-19 14:02:41 +00:00
|
|
|
long long n = 0;
|
|
|
|
fmt::scan("42", "{}", n);
|
|
|
|
EXPECT_EQ(n, 42);
|
|
|
|
fmt::scan("-42", "{}", n);
|
|
|
|
EXPECT_EQ(n, -42);
|
|
|
|
}
|
|
|
|
|
2021-05-06 00:43:06 +00:00
|
|
|
TEST(scan_test, read_uint) {
|
2023-12-02 17:34:27 +00:00
|
|
|
unsigned n = 0;
|
2019-05-19 14:02:41 +00:00
|
|
|
fmt::scan("42", "{}", n);
|
|
|
|
EXPECT_EQ(n, 42);
|
|
|
|
EXPECT_THROW_MSG(fmt::scan("-42", "{}", n), fmt::format_error,
|
|
|
|
"invalid input");
|
|
|
|
}
|
|
|
|
|
2021-05-06 00:43:06 +00:00
|
|
|
TEST(scan_test, read_ulonglong) {
|
2019-05-19 14:02:41 +00:00
|
|
|
unsigned long long n = 0;
|
|
|
|
fmt::scan("42", "{}", n);
|
|
|
|
EXPECT_EQ(n, 42);
|
|
|
|
EXPECT_THROW_MSG(fmt::scan("-42", "{}", n), fmt::format_error,
|
|
|
|
"invalid input");
|
2019-05-15 17:02:40 +00:00
|
|
|
}
|
2019-06-02 18:11:28 +00:00
|
|
|
|
2021-05-06 00:43:06 +00:00
|
|
|
TEST(scan_test, read_string) {
|
2023-12-02 17:34:27 +00:00
|
|
|
std::string s;
|
2019-06-02 14:30:26 +00:00
|
|
|
fmt::scan("foo", "{}", s);
|
|
|
|
EXPECT_EQ(s, "foo");
|
|
|
|
}
|
2019-05-15 18:05:19 +00:00
|
|
|
|
2021-05-06 00:43:06 +00:00
|
|
|
TEST(scan_test, read_string_view) {
|
2023-12-02 17:34:27 +00:00
|
|
|
fmt::string_view s;
|
2019-06-02 18:11:28 +00:00
|
|
|
fmt::scan("foo", "{}", s);
|
|
|
|
EXPECT_EQ(s, "foo");
|
|
|
|
}
|
|
|
|
|
2023-12-23 14:53:25 +00:00
|
|
|
TEST(scan_test, separator) {
|
|
|
|
int n1 = 0, n2 = 0;
|
|
|
|
fmt::scan("10 20", "{} {}", n1, n2);
|
|
|
|
EXPECT_EQ(n1, 10);
|
|
|
|
EXPECT_EQ(n2, 20);
|
|
|
|
}
|
|
|
|
|
2022-11-26 11:42:02 +00:00
|
|
|
#ifdef FMT_HAVE_STRPTIME
|
2023-12-23 14:53:25 +00:00
|
|
|
struct num {
|
|
|
|
int value;
|
|
|
|
};
|
|
|
|
|
2019-06-08 13:50:03 +00:00
|
|
|
namespace fmt {
|
2023-12-23 14:53:25 +00:00
|
|
|
template <> struct scanner<num> {
|
|
|
|
bool hex = false;
|
2019-06-08 13:50:03 +00:00
|
|
|
|
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();
|
|
|
|
if (it != end && *it == 'x') hex = true;
|
|
|
|
if (it != end && *it != '}') throw_format_error("invalid format");
|
|
|
|
return it;
|
2019-06-08 13:50:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class ScanContext>
|
2023-12-23 14:53:25 +00:00
|
|
|
auto scan(num&, ScanContext& ctx) const -> typename ScanContext::iterator {
|
|
|
|
// TODO
|
|
|
|
//return fmt::scan({ctx.begin(), ctx.end()}, "{}", n.value);
|
2023-12-02 17:34:27 +00:00
|
|
|
return ctx.begin();
|
2019-06-08 13:50:03 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
} // namespace fmt
|
|
|
|
|
2021-05-06 00:43:06 +00:00
|
|
|
TEST(scan_test, read_custom) {
|
2023-12-23 14:53:25 +00:00
|
|
|
auto input = "42";
|
|
|
|
auto n = num();
|
|
|
|
fmt::scan(input, "{:}", n);
|
|
|
|
//EXPECT_EQ(n, 42);
|
2019-06-08 13:50:03 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-05-06 00:43:06 +00:00
|
|
|
TEST(scan_test, invalid_format) {
|
2019-05-15 18:05:19 +00:00
|
|
|
EXPECT_THROW_MSG(fmt::scan("", "{}"), fmt::format_error,
|
|
|
|
"argument index out of range");
|
|
|
|
EXPECT_THROW_MSG(fmt::scan("", "{"), fmt::format_error,
|
|
|
|
"invalid format string");
|
|
|
|
}
|
2019-06-02 14:30:26 +00:00
|
|
|
|
2021-05-06 00:43:06 +00:00
|
|
|
TEST(scan_test, example) {
|
2023-12-23 14:53:25 +00:00
|
|
|
std::string key;
|
|
|
|
int value = 0;
|
2019-06-02 14:30:26 +00:00
|
|
|
fmt::scan("answer = 42", "{} = {}", key, value);
|
|
|
|
EXPECT_EQ(key, "answer");
|
|
|
|
EXPECT_EQ(value, 42);
|
|
|
|
}
|
2023-11-26 17:22:31 +00:00
|
|
|
|
2023-12-25 15:18:23 +00:00
|
|
|
TEST(scan_test, end_of_input) {
|
|
|
|
int value = 0;
|
|
|
|
fmt::scan("", "{}", value);
|
|
|
|
}
|
|
|
|
|
2023-11-26 17:22:31 +00:00
|
|
|
#if FMT_USE_FCNTL
|
|
|
|
TEST(scan_test, file) {
|
|
|
|
fmt::file read_end, write_end;
|
|
|
|
fmt::file::pipe(read_end, write_end);
|
2023-12-24 15:32:27 +00:00
|
|
|
|
|
|
|
fmt::string_view input = "10 20";
|
2023-11-26 17:22:31 +00:00
|
|
|
write_end.write(input.data(), input.size());
|
|
|
|
write_end.close();
|
2023-12-24 15:32:27 +00:00
|
|
|
|
|
|
|
int n1 = 0, n2 = 0;
|
|
|
|
fmt::buffered_file f = read_end.fdopen("r");
|
|
|
|
fmt::scan(f.get(), "{} {}", n1, n2);
|
|
|
|
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) {
|
|
|
|
fmt::file read_end, write_end;
|
|
|
|
fmt::file::pipe(read_end, write_end);
|
|
|
|
|
|
|
|
std::thread producer([&]() {
|
|
|
|
fmt::string_view input = "42 ";
|
|
|
|
for (int i = 0; i < 1000; ++i)
|
|
|
|
write_end.write(input.data(), input.size());
|
|
|
|
write_end.close();
|
|
|
|
});
|
|
|
|
|
|
|
|
fmt::buffered_file f = read_end.fdopen("r");
|
|
|
|
auto fun = [&]() {
|
|
|
|
int value = 0;
|
|
|
|
while (fmt::scan(f.get(), "{}", value)) {
|
|
|
|
if (value != 42) {
|
|
|
|
read_end.close();
|
|
|
|
EXPECT_EQ(value, 42);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
std::thread consumer1(fun);
|
|
|
|
std::thread consumer2(fun);
|
|
|
|
producer.join();
|
|
|
|
consumer1.join();
|
|
|
|
consumer2.join();
|
|
|
|
}
|
2023-11-26 17:22:31 +00:00
|
|
|
#endif // FMT_USE_FCNTL
|