2015-09-15 19:02:13 +02:00
|
|
|
#include <criterion/criterion.h>
|
|
|
|
#include <criterion/redirect.h>
|
|
|
|
#include <iostream>
|
2015-09-16 00:42:57 +02:00
|
|
|
#include <string>
|
2015-09-15 19:02:13 +02:00
|
|
|
|
2016-09-05 21:32:57 +02:00
|
|
|
/* set a timeout for I/O tests */
|
2015-11-05 10:57:33 +01:00
|
|
|
TestSuite(redirect, .timeout = 1);
|
2015-09-15 19:02:13 +02:00
|
|
|
|
2015-09-15 19:45:57 +02:00
|
|
|
#if __GNUC__ >= 5
|
2015-09-15 19:02:13 +02:00
|
|
|
Test(redirect, mock) {
|
|
|
|
auto fmock = criterion::mock_file();
|
|
|
|
|
|
|
|
fmock << "Hello" << std::flush;
|
|
|
|
fmock.seekg(0);
|
|
|
|
|
|
|
|
std::string contents;
|
|
|
|
fmock >> contents;
|
|
|
|
|
|
|
|
cr_assert_eq(contents, "Hello");
|
|
|
|
}
|
2015-09-15 19:45:57 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
Test(redirect, mock_c) {
|
2016-09-05 21:32:57 +02:00
|
|
|
std::FILE *fmock = cr_mock_file_size(4096);
|
2015-09-15 19:45:57 +02:00
|
|
|
|
|
|
|
std::fprintf(fmock, "Hello");
|
|
|
|
std::fflush(fmock);
|
|
|
|
std::rewind(fmock);
|
|
|
|
|
2016-09-05 21:32:57 +02:00
|
|
|
char contents[sizeof ("Hello")] = { 0 };
|
2015-09-15 19:45:57 +02:00
|
|
|
fgets(contents, sizeof (contents), fmock);
|
|
|
|
|
|
|
|
cr_assert_str_eq(contents, "Hello");
|
|
|
|
}
|
2015-09-15 19:02:13 +02:00
|
|
|
|
2015-09-16 02:08:20 +02:00
|
|
|
Test(redirect, assertions) {
|
2016-09-05 21:32:57 +02:00
|
|
|
std::FILE *f1 = cr_mock_file_size(4096);
|
|
|
|
std::FILE *f2 = cr_mock_file_size(4096);
|
|
|
|
std::FILE *f3 = cr_mock_file_size(4096);
|
2015-09-16 02:08:20 +02:00
|
|
|
|
|
|
|
fprintf(f1, "Foo");
|
|
|
|
fprintf(f2, "Foo");
|
|
|
|
fprintf(f3, "Bar");
|
|
|
|
|
|
|
|
fflush(f1);
|
|
|
|
fflush(f2);
|
|
|
|
fflush(f3);
|
|
|
|
|
|
|
|
cr_expect_file_contents_eq(f1, f1);
|
|
|
|
rewind(f1);
|
|
|
|
|
|
|
|
cr_expect_file_contents_eq(f1, f2);
|
|
|
|
rewind(f1);
|
|
|
|
|
|
|
|
cr_expect_file_contents_neq(f1, f3);
|
|
|
|
|
|
|
|
fclose(f1);
|
|
|
|
fclose(f2);
|
|
|
|
fclose(f3);
|
|
|
|
}
|
|
|
|
|
2015-09-15 19:02:13 +02:00
|
|
|
Test(redirect, stdout_) {
|
|
|
|
cr_redirect_stdout();
|
|
|
|
|
|
|
|
std::cout << "Foo" << std::flush;
|
|
|
|
|
|
|
|
cr_expect_stdout_eq_str("Foo");
|
|
|
|
}
|
|
|
|
|
|
|
|
Test(redirect, stderr_) {
|
|
|
|
cr_redirect_stderr();
|
|
|
|
|
|
|
|
std::cerr << "Foo" << std::flush;
|
|
|
|
|
|
|
|
cr_expect_stderr_eq_str("Foo");
|
|
|
|
}
|
|
|
|
|
|
|
|
Test(redirect, stdin_) {
|
2016-09-05 21:32:57 +02:00
|
|
|
auto &f_cin = criterion::get_redirected_cin();
|
|
|
|
|
2015-09-21 08:35:50 -07:00
|
|
|
f_cin << "Foo";
|
2015-09-15 19:02:13 +02:00
|
|
|
f_cin.close();
|
|
|
|
|
|
|
|
std::string read;
|
|
|
|
std::cin >> read;
|
|
|
|
|
|
|
|
cr_expect_eq(read, "Foo");
|
|
|
|
}
|