-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstdin.c
77 lines (60 loc) · 1.85 KB
/
stdin.c
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
// NOTE: The location of this include might differ in your code depending on location
// For example, it could be: #include "caught.h"
#include "../src/caught.h"
TEST("stdin - hello world")
{
MOCK_STDIN("Hello, world!\n");
char *line = NULL;
size_t line_cap = 0;
ssize_t len = getline(&line, &line_cap, stdin);
RESTORE_STDIN();
EXPECT_INT(len, !=, -1);
EXPECT_STR(line, ==, "Hello, world!\n");
free(line);
}
TEST("stdin - with expect exit & MOCK_STDOUT")
{
MOCK_STDIN("It's complicated\n");
MOCK_STDOUT();
EXPECT_EXIT(0, {
char *line = NULL;
size_t line_cap = 0;
ssize_t len = getline(&line, &line_cap, stdin);
if (len == -1)
{
exit(1);
}
printf("%s", line);
free(line);
exit(0);
});
RESTORE_STDIN();
char *out = RESTORE_STDOUT();
EXPECT_STR(out, ==, "It's complicated\n");
free(out);
}
TEST("stdin - no newline, only EOF")
{
MOCK_STDIN("Hello, world!"
" This is a really long string that will keep going on and on"
"and on for a while but never ever ever have a newline to be breaking up the flow of life.");
char *line = NULL;
size_t line_cap = 0;
ssize_t len = 0;
char buffer[500];
size_t buffer_size = 0;
while ((len = getline(&line, &line_cap, stdin)) != -1)
{
memcpy(buffer + buffer_size, line, len);
buffer_size += len;
}
buffer[buffer_size] = '\0';
RESTORE_STDIN();
EXPECT_STR(buffer, ==, "Hello, world!"
" This is a really long string "
"that will keep going on and on"
"and on for a while but never ever "
"ever have a newline to be breaking "
"up the flow of life.");
free(line);
}