-
-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tests: Some tests around the dinit-iostream
Signed-off-by: Mobin "Hojjat" Aydinfar <mobin@mobintestserver.ir>
- Loading branch information
1 parent
2e516fe
commit 68f3311
Showing
4 changed files
with
219 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,197 @@ | ||
#include <array> | ||
#include <iostream> | ||
#include <limits> | ||
#include <cassert> | ||
|
||
#include "baseproc-sys.h" | ||
#include "dinit-iostream.h" | ||
|
||
#ifdef NDEBUG | ||
#error "This file must be built with assertions ENABLED!" | ||
#endif | ||
|
||
void ostream_basic_test() | ||
{ | ||
int fd = bp_sys::allocfd(); | ||
dio::ostream stream(fd); | ||
dio::streambuf* buf = stream.get_buf(); | ||
assert(buf != nullptr); | ||
assert(stream.good()); | ||
|
||
const char msg[] = "This is a test message!\n"; | ||
|
||
assert(stream.write(msg)); | ||
assert(buf->get_length() == sizeof(msg) - 1); | ||
|
||
char* ptr = buf->get_ptr(0); | ||
unsigned len = buf->get_contiguous_length(ptr); | ||
assert(strncmp(ptr, msg, len) == 0); | ||
|
||
assert(stream.flush_nx() == sizeof(msg) - 1); | ||
assert(buf->get_length() == 0); | ||
|
||
std::vector<char> wdata; | ||
bp_sys::extract_written_data(fd, wdata); | ||
|
||
assert(wdata.size() == sizeof(msg) - 1); | ||
assert(strncmp(wdata.data(), msg, wdata.size()) == 0); | ||
} | ||
|
||
void ostream_write_buf_test() | ||
{ | ||
int fd = bp_sys::allocfd(); | ||
dio::ostream stream(fd); | ||
dio::streambuf* buf = stream.get_buf(); | ||
assert(buf != nullptr); | ||
assert(stream.good()); | ||
|
||
const char msg[] = "This is a test message!\n"; | ||
|
||
assert(stream.write_buf(msg) == sizeof(msg) - 1); | ||
assert(buf->get_length() == sizeof(msg) - 1); | ||
|
||
char* ptr = buf->get_ptr(0); | ||
unsigned len = buf->get_contiguous_length(ptr); | ||
assert(strncmp(ptr, msg, len) == 0); | ||
|
||
assert(stream.flush_nx() == sizeof(msg) - 1); | ||
assert(buf->get_length() == 0); | ||
|
||
std::vector<char> wdata; | ||
bp_sys::extract_written_data(fd, wdata); | ||
|
||
assert(wdata.size() == sizeof(msg) - 1); | ||
assert(strncmp(wdata.data(), msg, wdata.size()) == 0); | ||
} | ||
|
||
void ostream_int_conversion_test() | ||
{ | ||
int fd = bp_sys::allocfd(); | ||
dio::ostream stream(fd); | ||
dio::streambuf *buf = stream.get_buf(); | ||
assert(buf != nullptr); | ||
assert(stream.good()); | ||
|
||
const char msg1[] = "The 2 + 2 equals to: "; | ||
const int msg2 = 4; | ||
const char msg_full[] = "The 2 + 2 equals to: 4"; | ||
|
||
assert(stream.write(msg1, msg2)); | ||
assert(buf->get_length() == sizeof(msg_full) - 1); | ||
|
||
char* ptr = buf->get_ptr(0); | ||
unsigned len = buf->get_contiguous_length(ptr); | ||
assert(strncmp(ptr, msg_full, len) == 0); | ||
|
||
assert(stream.flush_nx() == sizeof(msg_full) - 1); | ||
assert(buf->get_length() == 0); | ||
|
||
std::vector<char> wdata; | ||
bp_sys::extract_written_data(fd, wdata); | ||
|
||
assert(wdata.size() == sizeof(msg_full) - 1); | ||
assert(strncmp(wdata.data(), msg_full, wdata.size()) == 0); | ||
} | ||
|
||
void ostream_large_msg_test() | ||
{ | ||
int fd = bp_sys::allocfd(); | ||
dio::ostream stream(fd); | ||
dio::streambuf *buf = stream.get_buf(); | ||
assert(buf != nullptr); | ||
assert(stream.good()); | ||
|
||
char msg[IOSTREAM_BUFSIZE + 2]; | ||
std::fill_n(msg, IOSTREAM_BUFSIZE + 1, 'a'); | ||
msg[IOSTREAM_BUFSIZE + 1] = '\0'; | ||
|
||
assert(stream.write(msg)); | ||
assert(buf->get_length() == 0); | ||
|
||
std::vector<char> wdata; | ||
bp_sys::extract_written_data(fd, wdata); | ||
|
||
assert(wdata.size() == sizeof(msg) - 1); | ||
assert(strncmp(wdata.data(), msg, wdata.size()) == 0); | ||
} | ||
|
||
void istream_basic_test() | ||
{ | ||
std::vector<char> wdata = { 'L', '1', '\n', 'L','2', '\n', '\n', 'L', '3' }; | ||
bp_sys::supply_file_content("file", std::move(wdata)); | ||
|
||
dio::istream stream; | ||
dio::streambuf *buf = stream.get_buf(); | ||
assert(buf != nullptr); | ||
assert(stream.good()); | ||
|
||
assert(stream.open_nx("file")); | ||
assert(stream.is_open()); | ||
assert(stream.get_fd() >= 0); | ||
|
||
std::string line; | ||
|
||
assert(dio::get_line(stream, line) == 2); | ||
assert(line.compare("L1") == 0); | ||
|
||
assert(dio::get_line(stream, line) == 2); | ||
assert(line.compare("L2") == 0); | ||
|
||
assert(dio::get_line(stream, line) == 1); | ||
assert(line.size() == 0); | ||
|
||
assert(dio::get_line(stream, line) == 2); | ||
assert(line.compare("L3") == 0); | ||
|
||
assert(dio::get_line_until_eof(stream, line) == 0); | ||
assert(stream.eof()); | ||
assert(line.compare("L3") == 0); | ||
|
||
assert(stream.close()); | ||
} | ||
|
||
void istream_buffer_boundary_test() | ||
{ | ||
int fd = bp_sys::allocfd(); | ||
dio::istream stream(fd); | ||
dio::streambuf *buf = stream.get_buf(); | ||
assert(buf != nullptr); | ||
assert(stream.good()); | ||
|
||
std::array<char, IOSTREAM_BUFSIZE> msg; | ||
msg.fill('a'); | ||
buf->append(msg.begin(), msg.size()); | ||
|
||
std::fill_n(msg.begin(), 100, 'b'); | ||
buf->consume(100); | ||
buf->append(msg.begin(), 100); | ||
|
||
std::string line; | ||
assert(dio::get_line(stream, line) == IOSTREAM_BUFSIZE); | ||
assert(strncmp(line.c_str(), msg.begin(), IOSTREAM_BUFSIZE)); | ||
} | ||
|
||
#define RUN_TEST(name, spacing) \ | ||
std::cout << #name "..." spacing << std::flush; \ | ||
name(); \ | ||
std::cout << "PASSED" << std::endl; | ||
|
||
int main(int argc, char **argv) | ||
{ | ||
RUN_TEST(ostream_basic_test, " "); | ||
RUN_TEST(ostream_write_buf_test, " "); | ||
RUN_TEST(ostream_int_conversion_test, " "); | ||
RUN_TEST(ostream_large_msg_test, " "); | ||
RUN_TEST(istream_basic_test, " "); | ||
RUN_TEST(istream_buffer_boundary_test, " "); | ||
|
||
// The destructors of the cout, cerr, and cin global objects will attempt to close the associated | ||
// file descriptors, if they are still open. The bp_sys test infrastructure may have been torn | ||
// down by that time, however, leading to errors. So, close the (bp_sys-based) descriptors now | ||
// to avoid that. | ||
dio::cout.close_nx(); | ||
dio::cerr.close_nx(); | ||
dio::cin.close_nx(); | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters