-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplate-program.c
126 lines (109 loc) · 3.41 KB
/
template-program.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
* -----------------------------------------------------------------------------
* Implement <describe problem being implemented>
*
* Ref:
*
* Usage: gcc -o template-program-c template-program.c
* ./template-program-c [--help | test_<something> | test_<prefix> ]
*
* History:
* -----------------------------------------------------------------------------
*/
#include <stdio.h>
#include <string.h>
#include <assert.h>
const char *Usage = "%s [ --help | test_<fn-name> ]\n";
#define ARRAYSIZE(arr) ((int) (sizeof(arr) / sizeof(*arr)))
// Test Function Prototypes
void test_this(void);
void test_that(void);
void test_msg(const char *msg);
// -----------------------------------------------------------------------------
// List of test functions one can invoke from the command-line
typedef struct test_fns
{
const char * tfn_name;
void (*tfn)(void);
} TEST_FNS;
TEST_FNS Test_fns[] = {
{ "test_this" , test_this }
, { "test_that" , test_that }
};
// Test start / end info-msg macros
#define TEST_START() printf("%s ", __func__)
#define TEST_END() printf(" ...OK\n")
#define STRINGIFY(x) #x
#define STRINGIFY_VALUE(s) STRINGIFY(s)
// Fabricate a string to track code-location of call-site.
// RESOLVE: This isn't quite compiling ...
#define __LOC__ \
"[" __func__ "():" STRINGIFY_VALUE(__LINE__) "] "
// "[" STRINGIFY_VALUE(__func__) "():" STRINGIFY_VALUE(__LINE__) "] "
// "[" __FUNC__ "():" STRINGIFY_VALUE(__LINE__) "] "
/*
* *****************************************************************************
* main()
* *****************************************************************************
*/
int
main(int argc, char *argv[])
{
const char *hello_msg = "Hello World";
printf("%s: %s. (argc=%d)\n", argv[0], hello_msg, argc);
int rv = 0;
// Run all test cases if no args are provided.
if (argc == 1) {
test_this();
test_msg(hello_msg);
for (int tctr = 0; tctr < ARRAYSIZE(Test_fns); tctr++) {
Test_fns[tctr].tfn();
}
} else if (strncmp("--help", argv[1], strlen("--help")) == 0) {
printf(Usage, argv[0]);
return rv;
} else if (strncmp("test_", argv[1], strlen("test_")) == 0) {
// Execute the named test-function, if it's a supported test-function
int tctr = 0;
int ntests = 0;
for (; tctr < ARRAYSIZE(Test_fns); tctr++) {
if (!strncmp(Test_fns[tctr].tfn_name, argv[1], strlen(argv[1]))) {
Test_fns[tctr].tfn();
ntests++;
}
}
if (tctr == ARRAYSIZE(Test_fns) && !ntests) {
printf("Warning: Named test-function '%s' not found.\n", argv[1]);
rv = 1;
}
} else {
printf("Unknown argument: '%s'\n", argv[1]);
rv = 1;
}
return rv;
}
// **** Test cases ****
void
test_this(void)
{
TEST_START();
assert(1 == 1);
TEST_END();
}
void
test_that(void)
{
TEST_START();
test_msg("Hello World");
TEST_END();
}
void
test_msg(const char *msg)
{
TEST_START();
const char *expmsg = "Hello World";
printf("%s: msg='%s', expmsg='%s', strlen()=%lu, sizeof()=%lu\n",
__FUNCTION__, msg, expmsg, strlen(expmsg), sizeof(expmsg));
assert(strncmp(expmsg, msg, strlen(expmsg)) == 0);
TEST_END();
}