File: redirect.c

package info (click to toggle)
criterion 2.3.3git1-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 3,832 kB
  • sloc: ansic: 17,852; cpp: 795; python: 72; sh: 27; makefile: 23
file content (57 lines) | stat: -rw-r--r-- 1,150 bytes parent folder | download | duplicates (2)
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
#include <criterion/criterion.h>
#include <criterion/redirect.h>

#include <stdio.h>
#include <ctype.h>

/* Testing stdout/stderr */

void redirect_all_std(void)
{
    cr_redirect_stdout();
    cr_redirect_stderr();
}

Test(redirect, test_outputs, .init = redirect_all_std) {
    fprintf(stdout, "foo");
    fflush(stdout);

    cr_assert_stdout_eq_str("foo");

    fprintf(stderr, "bar");
    fflush(stderr);

    cr_assert_stderr_eq_str("bar");
}

/* Testing general I/O with sample command-line rot13 */

char rot13_char(char c)
{
    return isalpha(c) ? (c - 'a' + 13) % 26 + 'a' : c;
}

void rot13_io(void)
{
    char buf[512];

    size_t read;

    while ((read = fread(buf, 1, sizeof (buf), stdin)) > 0) {
        for (size_t i = 0; i < read; ++i)
            buf[i] = rot13_char(buf[i]);
        fwrite(buf, 1, read, stdout);
    }
    fflush(stdout);
}

Test(redirect, rot13, .init = cr_redirect_stdout) {
    FILE *f_stdin = cr_get_redirected_stdin();

    fprintf(f_stdin, "the quick brown fox jumps over the lazy dog");
    fclose(f_stdin);

    rot13_io();

    cr_assert_stdout_eq_str("gur dhvpx oebja sbk whzcf bire gur ynml qbt");
}