1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
|
#include <criterion/criterion.h>
#include <criterion/redirect.h>
#include <criterion/new/assert.h>
#include <iostream>
#include <string>
/* set a timeout for I/O tests */
TestSuite(redirect, .timeout = 1);
#if __GNUC__ >= 5
Test(redirect, mock) {
auto fmock = criterion::mock_file();
fmock << "Hello" << std::flush;
fmock.seekg(0);
std::string contents;
fmock >> contents;
cr_assert(eq(str, contents, "Hello"));
}
#endif
Test(redirect, mock_c) {
std::FILE *fmock = cr_mock_file_size(4096);
std::fprintf(fmock, "Hello");
std::fflush(fmock);
std::rewind(fmock);
char contents[sizeof ("Hello")] = { 0 };
fgets(contents, sizeof (contents), fmock);
cr_assert(eq(str, contents, "Hello"));
}
Test(redirect, assertions) {
std::FILE *f1 = cr_mock_file_size(4096);
std::FILE *f2 = cr_mock_file_size(4096);
std::FILE *f3 = cr_mock_file_size(4096);
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);
}
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_) {
auto &f_cin = criterion::get_redirected_cin();
f_cin << "Foo";
f_cin.close();
std::string read;
std::cin >> read;
cr_expect_eq(read, "Foo");
}
Test(redirect, stdout_empty) {
cr_redirect_stdout();
cr_expect_stdout_eq_str("");
}
|