Skip to content

Commit 0272419

Browse files
committed
Rewrite tests
1 parent ad50489 commit 0272419

File tree

1 file changed

+39
-38
lines changed

1 file changed

+39
-38
lines changed

tests/file.cpp

Lines changed: 39 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
namespace tmp {
2626
namespace {
2727

28+
using namespace testing;
29+
2830
/// Returns whether the underlying raw file device object is open
2931
template<class charT, class traits>
3032
bool is_open(const basic_file<charT, traits>& file) {
@@ -45,47 +47,50 @@ bool is_open(file::native_handle_type handle) {
4547
}
4648

4749
template<typename charT>
48-
constexpr std::basic_string<charT> convert_string(const char* string) {
49-
if constexpr (std::is_same_v<charT, char>) {
50-
return std::basic_string<charT>(string);
50+
constexpr std::basic_string<charT> convert_string(std::string_view string) {
51+
std::basic_string<charT> result;
52+
53+
for (char character : string) {
54+
result += static_cast<charT>(character);
5155
}
5256

53-
if constexpr (std::is_same_v<charT, wchar_t>) {
54-
std::mbstate_t state = std::mbstate_t();
57+
return result;
58+
}
5559

56-
std::basic_string<charT> result;
57-
result.resize(std::mbsrtowcs(nullptr, &string, 0, &state));
60+
template<typename charT> class file : public Test {
61+
public:
62+
using file_t = basic_file<charT>;
63+
};
5864

59-
std::size_t ret =
60-
std::mbsrtowcs(result.data(), &string, result.size(), &state);
61-
assert(ret == result.size());
62-
return result;
63-
}
65+
class name_generator {
66+
public:
67+
template<typename T> static std::string GetName(int) {
68+
if constexpr (std::is_same_v<T, char>) {
69+
return "char";
70+
}
6471

65-
throw std::invalid_argument("Unknown character type");
66-
}
72+
if constexpr (std::is_same_v<T, wchar_t>) {
73+
return "wchar_t";
74+
}
6775

68-
template<typename charT> class file : public testing::Test {};
76+
throw std::invalid_argument("Unknown character type");
77+
}
78+
};
6979

70-
using char_types = testing::Types<char, wchar_t>;
71-
TYPED_TEST_SUITE(file, char_types, testing::internal::DefaultNameGenerator);
80+
using char_types = Types<char, wchar_t>;
81+
TYPED_TEST_SUITE(file, char_types, name_generator);
7282

7383
/// Tests file type traits and member types
7484
TYPED_TEST(file, type_traits) {
7585
using traits = std::char_traits<TypeParam>;
76-
77-
static_assert(
78-
std::is_base_of_v<std::basic_iostream<TypeParam>, basic_file<TypeParam>>);
79-
static_assert(
80-
std::is_same_v<typename basic_file<TypeParam>::char_type, TypeParam>);
81-
static_assert(
82-
std::is_same_v<typename basic_file<TypeParam>::traits_type, traits>);
83-
static_assert(std::is_same_v<typename basic_file<TypeParam>::int_type,
84-
typename traits::int_type>);
85-
static_assert(std::is_same_v<typename basic_file<TypeParam>::pos_type,
86-
typename traits::pos_type>);
87-
static_assert(std::is_same_v<typename basic_file<TypeParam>::off_type,
88-
typename traits::off_type>);
86+
using file_t = basic_file<TypeParam>;
87+
88+
static_assert(std::is_base_of_v<std::basic_iostream<TypeParam>, file_t>);
89+
StaticAssertTypeEq<typename file_t::char_type, TypeParam>();
90+
StaticAssertTypeEq<typename file_t::traits_type, traits>();
91+
StaticAssertTypeEq<typename file_t::int_type, typename traits::int_type>();
92+
StaticAssertTypeEq<typename file_t::pos_type, typename traits::pos_type>();
93+
StaticAssertTypeEq<typename file_t::off_type, typename traits::off_type>();
8994
}
9095

9196
/// Tests file creation
@@ -150,10 +155,8 @@ TYPED_TEST(file, move_assignment) {
150155
basic_file<TypeParam> snd = basic_file<TypeParam>();
151156
snd << "Hello!";
152157

153-
typename basic_file<TypeParam>::native_handle_type fst_handle =
154-
fst.native_handle();
155-
typename basic_file<TypeParam>::native_handle_type snd_handle =
156-
snd.native_handle();
158+
typename decltype(fst)::native_handle_type fst_handle = fst.native_handle();
159+
typename decltype(snd)::native_handle_type snd_handle = snd.native_handle();
157160

158161
fst = std::move(snd);
159162

@@ -175,10 +178,8 @@ TYPED_TEST(file, swap) {
175178
basic_file<TypeParam> fst = basic_file<TypeParam>();
176179
basic_file<TypeParam> snd = basic_file<TypeParam>();
177180

178-
typename basic_file<TypeParam>::native_handle_type fst_handle =
179-
fst.native_handle();
180-
typename basic_file<TypeParam>::native_handle_type snd_handle =
181-
snd.native_handle();
181+
typename decltype(fst)::native_handle_type fst_handle = fst.native_handle();
182+
typename decltype(snd)::native_handle_type snd_handle = snd.native_handle();
182183

183184
std::swap(fst, snd);
184185

0 commit comments

Comments
 (0)