Add ASSERT_ERRNO and EXPECT_ERRNO (and use them).
We've talked about this many times in the past, but partners struggle to
understand "expected 38, got 22" in these contexts, and I always have to
go and check the header files just to be sure I'm sure.
I actually think the glibc geterrorname_np() function (which would
return "ENOSYS" rather than "Function not implemented") would be more
helpful, but I'll have to go and implement that first, and then come
back.
Being forced to go through all our errno assertions did also make me
want to use a more consistent style for our ENOSYS assertions in
particular --- there's a particularly readable idiom, and I'll also come
back and move more of those checks to the most readable idiom.
I've added a few missing `errno = 0`s before tests, and removed a few
stray `errno = 0`s from tests that don't actually make assertions about
errno, since I had to look at every single reference to errno anyway.
Test: treehugger
Change-Id: Iba7c56f2adc30288c3e00ade106635e515e88179
diff --git a/tests/stdlib_test.cpp b/tests/stdlib_test.cpp
index 45169e3..21c79c8 100644
--- a/tests/stdlib_test.cpp
+++ b/tests/stdlib_test.cpp
@@ -279,7 +279,7 @@
for (size_t fail_align = last_align + 1; fail_align < align; fail_align++) {
ASSERT_TRUE(aligned_alloc(fail_align, fail_align) == nullptr)
<< "Unexpected success at align " << fail_align;
- ASSERT_EQ(EINVAL, errno) << "Unexpected errno at align " << fail_align;
+ ASSERT_ERRNO(EINVAL) << "Unexpected errno at align " << fail_align;
}
void* ptr = aligned_alloc(align, 2 * align);
ASSERT_TRUE(ptr != nullptr) << "Unexpected failure at align " << align;
@@ -310,21 +310,21 @@
const char* path = nullptr;
char* p = realpath(path, nullptr);
ASSERT_TRUE(p == nullptr);
- ASSERT_EQ(EINVAL, errno);
+ ASSERT_ERRNO(EINVAL);
}
TEST(stdlib, realpath__empty_filename) {
errno = 0;
char* p = realpath("", nullptr);
ASSERT_TRUE(p == nullptr);
- ASSERT_EQ(ENOENT, errno);
+ ASSERT_ERRNO(ENOENT);
}
TEST(stdlib, realpath__ENOENT) {
errno = 0;
char* p = realpath("/this/directory/path/almost/certainly/does/not/exist", nullptr);
ASSERT_TRUE(p == nullptr);
- ASSERT_EQ(ENOENT, errno);
+ ASSERT_ERRNO(ENOENT);
}
TEST(stdlib, realpath__ELOOP) {
@@ -335,19 +335,19 @@
errno = 0;
char* p = realpath(link.c_str(), nullptr);
ASSERT_TRUE(p == nullptr);
- ASSERT_EQ(ELOOP, errno);
+ ASSERT_ERRNO(ELOOP);
}
TEST(stdlib, realpath__component_after_non_directory) {
errno = 0;
char* p = realpath("/dev/null/.", nullptr);
ASSERT_TRUE(p == nullptr);
- ASSERT_EQ(ENOTDIR, errno);
+ ASSERT_ERRNO(ENOTDIR);
errno = 0;
p = realpath("/dev/null/..", nullptr);
ASSERT_TRUE(p == nullptr);
- ASSERT_EQ(ENOTDIR, errno);
+ ASSERT_ERRNO(ENOTDIR);
}
TEST(stdlib, realpath) {
@@ -403,7 +403,7 @@
errno = 0;
char* result = realpath(path.c_str(), nullptr);
ASSERT_EQ(nullptr, result) << result;
- ASSERT_EQ(ENOENT, errno);
+ ASSERT_ERRNO(ENOENT);
free(result);
}
@@ -695,7 +695,7 @@
errno = 0;
char buf[128];
ASSERT_EQ(ENOTTY, ptsname_r(STDOUT_FILENO, buf, sizeof(buf)));
- ASSERT_EQ(ENOTTY, errno);
+ ASSERT_ERRNO(ENOTTY);
}
TEST(stdlib, ptsname_r_EINVAL) {
@@ -704,7 +704,7 @@
errno = 0;
char* buf = nullptr;
ASSERT_EQ(EINVAL, ptsname_r(fd, buf, 128));
- ASSERT_EQ(EINVAL, errno);
+ ASSERT_ERRNO(EINVAL);
close(fd);
}
@@ -714,7 +714,7 @@
errno = 0;
char buf[1];
ASSERT_EQ(ERANGE, ptsname_r(fd, buf, sizeof(buf)));
- ASSERT_EQ(ERANGE, errno);
+ ASSERT_ERRNO(ERANGE);
close(fd);
}
@@ -745,7 +745,7 @@
errno = 0;
char buf[128];
ASSERT_EQ(ENOTTY, ttyname_r(fd, buf, sizeof(buf)));
- ASSERT_EQ(ENOTTY, errno);
+ ASSERT_ERRNO(ENOTTY);
close(fd);
}
@@ -755,7 +755,7 @@
errno = 0;
char* buf = nullptr;
ASSERT_EQ(EINVAL, ttyname_r(fd, buf, 128));
- ASSERT_EQ(EINVAL, errno);
+ ASSERT_ERRNO(EINVAL);
close(fd);
}
@@ -765,7 +765,7 @@
errno = 0;
char buf[1];
ASSERT_EQ(ERANGE, ttyname_r(fd, buf, sizeof(buf)));
- ASSERT_EQ(ERANGE, errno);
+ ASSERT_ERRNO(ERANGE);
close(fd);
}
@@ -773,7 +773,7 @@
int fd = open("/dev/null", O_WRONLY);
errno = 0;
ASSERT_EQ(-1, unlockpt(fd));
- ASSERT_EQ(ENOTTY, errno);
+ ASSERT_ERRNO(ENOTTY);
close(fd);
}
@@ -830,17 +830,17 @@
// Negative base => invalid.
errno = 0;
ASSERT_EQ(T(0), fn("123", &end_p, -1));
- ASSERT_EQ(EINVAL, errno);
+ ASSERT_ERRNO(EINVAL);
// Base 1 => invalid (base 0 means "please guess").
errno = 0;
ASSERT_EQ(T(0), fn("123", &end_p, 1));
- ASSERT_EQ(EINVAL, errno);
+ ASSERT_ERRNO(EINVAL);
// Base > 36 => invalid.
errno = 0;
ASSERT_EQ(T(0), fn("123", &end_p, 37));
- ASSERT_EQ(EINVAL, errno);
+ ASSERT_ERRNO(EINVAL);
// Both leading + or - are always allowed (even for the strtou* family).
ASSERT_EQ(T(-123), fn("-123", &end_p, 10));
@@ -875,14 +875,14 @@
end_p = nullptr;
errno = 0;
ASSERT_EQ(std::numeric_limits<T>::min(), fn(min.c_str(), &end_p, 0));
- ASSERT_EQ(0, errno);
+ ASSERT_ERRNO(0);
ASSERT_EQ('\0', *end_p);
// Too negative (such as -129).
min.back() = (min.back() + 1);
end_p = nullptr;
errno = 0;
ASSERT_EQ(std::numeric_limits<T>::min(), fn(min.c_str(), &end_p, 0));
- ASSERT_EQ(ERANGE, errno);
+ ASSERT_ERRNO(ERANGE);
ASSERT_EQ('\0', *end_p);
}
@@ -891,14 +891,14 @@
end_p = nullptr;
errno = 0;
ASSERT_EQ(std::numeric_limits<T>::max(), fn(max.c_str(), &end_p, 0));
- ASSERT_EQ(0, errno);
+ ASSERT_ERRNO(0);
ASSERT_EQ('\0', *end_p);
// Too positive (such as 128).
max.back() = (max.back() + 1);
end_p = nullptr;
errno = 0;
ASSERT_EQ(std::numeric_limits<T>::max(), fn(max.c_str(), &end_p, 0));
- ASSERT_EQ(ERANGE, errno);
+ ASSERT_ERRNO(ERANGE);
ASSERT_EQ('\0', *end_p);
// In case of overflow, strto* leaves us pointing past the end of the number,
@@ -907,14 +907,14 @@
errno = 0;
ASSERT_EQ(std::numeric_limits<T>::max(),
fn("99999999999999999999999999999999999999999999999999999abc", &end_p, 0));
- ASSERT_EQ(ERANGE, errno);
+ ASSERT_ERRNO(ERANGE);
ASSERT_STREQ("abc", end_p);
if (std::numeric_limits<T>::is_signed) {
end_p = nullptr;
errno = 0;
ASSERT_EQ(std::numeric_limits<T>::min(),
fn("-99999999999999999999999999999999999999999999999999999abc", &end_p, 0));
- ASSERT_EQ(ERANGE, errno);
+ ASSERT_ERRNO(ERANGE);
ASSERT_STREQ("abc", end_p);
}
}