Skip to content

Commit ec31b21

Browse files
committed
Rewrite tests
1 parent ad50489 commit ec31b21

File tree

1 file changed

+51
-63
lines changed

1 file changed

+51
-63
lines changed

tests/file.cpp

Lines changed: 51 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -25,74 +25,66 @@
2525
namespace tmp {
2626
namespace {
2727

28-
/// Returns whether the underlying raw file device object is open
29-
template<class charT, class traits>
30-
bool is_open(const basic_file<charT, traits>& file) {
31-
auto filebuf = dynamic_cast<std::basic_filebuf<charT, traits>*>(file.rdbuf());
32-
return filebuf != nullptr && filebuf->is_open();
33-
}
28+
/// Test fixture for `basic_file` tests
29+
template<class charT> class file : public testing::Test {
30+
public:
31+
/// Returns whether the underlying raw file device object is open
32+
bool is_open(const basic_file<charT>& file) {
33+
auto filebuf = dynamic_cast<std::basic_filebuf<charT>*>(file.rdbuf());
34+
return filebuf != nullptr && filebuf->is_open();
35+
}
3436

35-
/// Checks if the given file handle is valid
36-
/// @param handle handle to check
37-
/// @returns whether the handle is valid
38-
bool is_open(file::native_handle_type handle) {
37+
/// Checks if the given file handle is valid
38+
bool is_open(typename basic_file<charT>::native_handle_type handle) {
3939
#ifdef _WIN32
40-
BY_HANDLE_FILE_INFORMATION info;
41-
return GetFileInformationByHandle(handle, &info);
40+
BY_HANDLE_FILE_INFORMATION info;
41+
return GetFileInformationByHandle(handle, &info);
4242
#else
43-
return fcntl(handle, F_GETFD) != -1;
43+
return fcntl(handle, F_GETFD) != -1;
4444
#endif
45-
}
46-
47-
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);
5145
}
5246

53-
if constexpr (std::is_same_v<charT, wchar_t>) {
54-
std::mbstate_t state = std::mbstate_t();
55-
47+
/// Converts the string to the `charT` string type
48+
constexpr std::basic_string<charT> convert_string(std::string_view string) {
5649
std::basic_string<charT> result;
57-
result.resize(std::mbsrtowcs(nullptr, &string, 0, &state));
5850

59-
std::size_t ret =
60-
std::mbsrtowcs(result.data(), &string, result.size(), &state);
61-
assert(ret == result.size());
51+
for (char character : string) {
52+
result += static_cast<charT>(character);
53+
}
54+
6255
return result;
6356
}
57+
};
6458

65-
throw std::invalid_argument("Unknown character type");
66-
}
67-
68-
template<typename charT> class file : public testing::Test {};
59+
/// Name generator for typed test suite
60+
struct name_generator {
61+
template<typename T> static std::string GetName(int) {
62+
return typeid(T).name();
63+
}
64+
};
6965

7066
using char_types = testing::Types<char, wchar_t>;
71-
TYPED_TEST_SUITE(file, char_types, testing::internal::DefaultNameGenerator);
67+
TYPED_TEST_SUITE(file, char_types, name_generator);
7268

7369
/// Tests file type traits and member types
7470
TYPED_TEST(file, type_traits) {
7571
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>);
72+
using file_t = basic_file<TypeParam>;
73+
using testing::StaticAssertTypeEq;
74+
75+
static_assert(std::is_base_of_v<std::basic_iostream<TypeParam>, file_t>);
76+
StaticAssertTypeEq<typename file_t::char_type, TypeParam>();
77+
StaticAssertTypeEq<typename file_t::traits_type, traits>();
78+
StaticAssertTypeEq<typename file_t::int_type, typename traits::int_type>();
79+
StaticAssertTypeEq<typename file_t::pos_type, typename traits::pos_type>();
80+
StaticAssertTypeEq<typename file_t::off_type, typename traits::off_type>();
8981
}
9082

9183
/// Tests file creation
9284
TYPED_TEST(file, create) {
9385
basic_file<TypeParam> tmpfile = basic_file<TypeParam>();
94-
EXPECT_TRUE(is_open(tmpfile));
95-
EXPECT_TRUE(is_open(tmpfile.native_handle()));
86+
EXPECT_TRUE(TestFixture::is_open(tmpfile));
87+
EXPECT_TRUE(TestFixture::is_open(tmpfile.native_handle()));
9688
}
9789

9890
/// Tests multiple file creation
@@ -124,7 +116,7 @@ TYPED_TEST(file, destructor) {
124116
handle = tmpfile.native_handle();
125117
}
126118

127-
EXPECT_FALSE(is_open(handle));
119+
EXPECT_FALSE(TestFixture::is_open(handle));
128120
}
129121

130122
/// Tests file move constructor
@@ -134,12 +126,12 @@ TYPED_TEST(file, move_constructor) {
134126

135127
basic_file<TypeParam> snd = basic_file<TypeParam>(std::move(fst));
136128

137-
EXPECT_TRUE(is_open(snd));
129+
EXPECT_TRUE(TestFixture::is_open(snd));
138130

139131
snd.seekg(0);
140132
std::basic_string<TypeParam> content;
141133
snd >> content;
142-
EXPECT_EQ(content, convert_string<TypeParam>("Hello!"));
134+
EXPECT_EQ(content, TestFixture::convert_string("Hello!"));
143135
}
144136

145137
/// Tests file move assignment operator
@@ -150,42 +142,38 @@ TYPED_TEST(file, move_assignment) {
150142
basic_file<TypeParam> snd = basic_file<TypeParam>();
151143
snd << "Hello!";
152144

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();
145+
typename decltype(fst)::native_handle_type fst_handle = fst.native_handle();
146+
typename decltype(snd)::native_handle_type snd_handle = snd.native_handle();
157147

158148
fst = std::move(snd);
159149

160-
EXPECT_FALSE(is_open(fst_handle));
161-
EXPECT_TRUE(is_open(snd_handle));
150+
EXPECT_FALSE(TestFixture::is_open(fst_handle));
151+
EXPECT_TRUE(TestFixture::is_open(snd_handle));
162152
EXPECT_EQ(fst.native_handle(), snd_handle);
163153
}
164154

165-
EXPECT_TRUE(is_open(fst));
155+
EXPECT_TRUE(TestFixture::is_open(fst));
166156

167157
fst.seekg(0);
168158
std::basic_string<TypeParam> content;
169159
fst >> content;
170-
EXPECT_EQ(content, convert_string<TypeParam>("Hello!"));
160+
EXPECT_EQ(content, TestFixture::convert_string("Hello!"));
171161
}
172162

173163
/// Tests file swapping
174164
TYPED_TEST(file, swap) {
175165
basic_file<TypeParam> fst = basic_file<TypeParam>();
176166
basic_file<TypeParam> snd = basic_file<TypeParam>();
177167

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();
168+
typename decltype(fst)::native_handle_type fst_handle = fst.native_handle();
169+
typename decltype(snd)::native_handle_type snd_handle = snd.native_handle();
182170

183171
std::swap(fst, snd);
184172

185173
EXPECT_EQ(fst.native_handle(), snd_handle);
186174
EXPECT_EQ(snd.native_handle(), fst_handle);
187-
EXPECT_TRUE(is_open(fst));
188-
EXPECT_TRUE(is_open(snd));
175+
EXPECT_TRUE(TestFixture::is_open(fst));
176+
EXPECT_TRUE(TestFixture::is_open(snd));
189177
}
190178
} // namespace
191179
} // namespace tmp

0 commit comments

Comments
 (0)