-
Notifications
You must be signed in to change notification settings - Fork 183
/
redirect.cc
50 lines (37 loc) · 1.03 KB
/
redirect.cc
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
#include <criterion/criterion.h>
#include <criterion/redirect.h>
#include <string>
#include <iostream>
#include <cctype>
/* Testing stdout/stderr */
void redirect_all_std(void)
{
cr_redirect_stdout();
cr_redirect_stderr();
}
Test(redirect, test_outputs, .init = redirect_all_std) {
std::cout << "foo" << std::flush;
std::cerr << "bar" << std::flush;
cr_assert_stdout_eq_str("foo");
cr_assert_stderr_eq_str("bar");
}
/* Testing general I/O with sample command-line rot13 */
char rot13_char(char c)
{
return std::isalpha(c) ? (c - 'a' + 13) % 26 + 'a' : c;
}
void rot13_io(void)
{
std::string s;
std::getline(std::cin, s);
for (size_t i = 0; i < s.length(); ++i)
s[i] = rot13_char(s[i]);
std::cout << s << std::flush;
}
Test(redirect, rot13, .init = cr_redirect_stdout) {
auto &f_cin = criterion::get_redirected_cin();
f_cin << "the quick brown fox jumps over the lazy dog";
f_cin.close();
rot13_io();
cr_assert_stdout_eq_str("gur dhvpx oebja sbk whzcf bire gur ynml qbt");
}