-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathex19.c
101 lines (78 loc) · 1.78 KB
/
ex19.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include "dbg.h"
#include <stdio.h>
#include <stdlib.h>
void test_debug() {
debug("I have brown hair");
debug("i am %d years old", 21);
}
void test_log_err() {
log_err("I believe everything is broken");
log_err("There are %d problems in %s", 0, "space");
}
void test_log_warn() {
log_warn("You can safely ignore this");
log_warn("Maybe consider looking at: %s.", "/etc/password");
}
void test_log_info() {
log_info("Well I did something mundane.");
log_info("It happened %f times today.", 1.3f);
}
int test_check(char* filename) {
FILE* input = NULL;
char* block = NULL;
block = malloc(100);
check_mem(block);
input = fopen(filename, "r");
check(input, "Failed to open %s.", filename);
free(block);
fclose(input);
return 0;
error:
if (block) free(block);
if (input) fclose(input);
return -1;
}
int test_sentinel(int code) {
char* temp = malloc(100);
check_mem(temp);
switch (code) {
case 1: log_info("It worked"); break;
default: sentinel("I shouldn't run");
}
free(temp);
return 0;
error:
if (temp) free(temp);
return -1;
}
int test_check_mem() {
char* test = NULL;
check_mem(test);
free(test);
return 0;
error:
return -1;
}
int test_check_debug() {
int i = 0;
check_debug(i != 0, "Oops, I was 0");
return 0;
error:
return 1;
}
int main(int argc, char* argv[]) {
check(argc == 2, "Need an argument");
test_debug();
test_log_err();
test_log_warn();
test_log_info();
check(test_check("ex19.c") == 0, "failed with ex19.c");
check(test_check(argv[1]) == -1, "failed with argv: %s", argv[1]);
check(test_sentinel(1) == 0, "test_senitel failed");
check(test_sentinel(100) == -1, "test_senitel failed");
check(test_check_mem() == -1, "test_check_mem failed");
check(test_check_debug() == -1, "test_check_debug failed");
return 0;
error:
return 1;
}