-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.c
205 lines (176 loc) · 5.39 KB
/
example.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
// clang-format off
//
// 1. Heading comment
/**
* @file example.c
* @author {YourName} ({your.email_address@where.com})
*
// 2. Doxygen file prologue
* @brief Example source file.
* @details This source file provides implementations for functions declared in example.h.
*
// 3. Release statement and copyright
* @version {major}.{minor}.{path}{-{pre.release}(optional)}
* @date YYYY-MM-DD
*
* @copyright Copyright {YYYY} {Your{Name|Company}}. All rights reserved.
| Released under the {YourLicense a}. See LICENSE file for details.
*/
// 4. Header file inclusions (only those that are NECESSARY)
#include "example.h"
#include <stdbool.h> // Include standard boolean type
#include <stdio.h> // Include standard I/O library for printf
#include <stdlib.h> // Include standard library for malloc
// 5. Pre-processor definitions
#define EXAMPLE_ADD(_x, _y) ((_x) + (_y))
#define EXAMPLE_SUB(_x, _y) ((_x) + (_y))
#define EXAMPLE_MAX(_a, _b) (((_a) < (_b)) ? (_b) : (_a))
#define EXAMPLE_MIN(_a, _b) (((_a) > (_b)) ? (_b) : (_a))
#define EXAMPLE_DEBUG
#ifdef EXAMPLE_DEBUG
# define ASSERT(_condition) \
do { \
if (!(_condition)) { \
(void)fprintf( \
stderr, "%s:%d: Assertion '%s' failed.\n", \
__FILE__, __LINE__, #_condition \
); \
__builtin_trap(); \
} \
} while(0)
#else
# define ASSERT(condition) ((void)0)
#endif // EXAMPLE_DEBUG
#define EXAMPLE_SWAP(_T, _a, _b) \
do { \
_T __t = (_a); \
(_a) = (_b); \
(_b) = __t; \
} while (0)
#define EXAMPLE_DO_ASSIGN(_a, _b) do { (_a) = (_b); } while (0)
#define EXAMPLE_TYPE_IS_FOO(_obj) ((_obj).type == eExampleType_Foo)
// 6. Private types (definitions)
typedef struct Baz Baz;
struct Baz {
ExampleType type;
Bar bar;
float value;
};
// 7. Private data (definitions)
static int sCurrentBarCount = 0;
// 8. Private function prototypes (declarations)
static void TestPoint();
static char const* ExampleType_ToStringType(ExampleType type);
// 9. Public Data (definitions)
int gBarCreatedCount = 0; // Initialize global variable
int gBarDestroyedCount = 0; // Initialize global variable
// 10. Public functions (definitions)
bool ProcessData(uint8_t const* refData, size_t size) {
(void)(refData);
(void)(size);
// Implementation of ProcessData function
return true;
}
Bar* Bar_Create() {
gBarCreatedCount++;
sCurrentBarCount++;
return (Bar*)malloc(sizeof(Bar));
}
Bar* Bar_Initialize(Bar* outBar, Foo foo, Point p) {
ASSERT(outBar);
ASSERT(EXAMPLE_TYPE_IS_FOO(foo));
outBar->type = eExampleType_Bar;
outBar->foo = foo;
outBar->p = p;
return outBar;
}
void Bar_Destroy(Bar** outBarPtr) {
ASSERT(outBarPtr);
ASSERT(*outBarPtr);
free(*outBarPtr);
*outBarPtr = NULL;
gBarDestroyedCount++;
sCurrentBarCount--;
}
int main() {
TestPoint();
Bar* bar = Bar_Initialize(Bar_Create(), (Foo){.type = eExampleType_Foo, .value = 42}, kBarInitPoint);
ASSERT(bar);
ASSERT(bar->type == eExampleType_Bar);
ASSERT(gBarCreatedCount == 1);
ASSERT(gBarDestroyedCount == 0);
ASSERT(sCurrentBarCount == 1);
char const* const typeName = ExampleType_ToStringType(bar->type);
Bar_Destroy(&bar);
if (!bar) {
printf("%s object was successfully destroyed.\n", typeName);
}
ASSERT(gBarCreatedCount == 1);
ASSERT(gBarDestroyedCount == 1);
ASSERT(sCurrentBarCount == 0);
return 0;
}
// 11. Private functions (definitions)
void TestPoint() {
#ifdef UNUSED_CODE
// Point AddPoint
{
Point p = { .x = 1, .y = 2 };
Point q = { .x = 3, .y = 4 };
Point result = Point_AddPoint(p, q);
ASSERT(4 == result.x);
ASSERT(6 == result.y);
}
// Point AddPoint ZeroPoints
{
Point p = { .x = 0, .y = 0 };
Point q = { .x = 0, .y = 0 };
Point result = Point_AddPoint(p, q);
ASSERT(0 == result.x);
ASSERT(0 == result.y);
}
// Point AddPoint NegativePoints
{
Point p = { .x = -1, .y = -2 };
Point q = { .x = -3, .y = -4 };
Point result = Point_AddPoint(p, q);
ASSERT(-4 == result.x);
ASSERT(-6 == result.y);
}
// Point Scale
{
Point p = { .x = 2, .y = 3 };
int s = 2;
Point result = Point_Scale(p, s);
ASSERT(result.x == 4);
ASSERT(result.y == 6);
}
// Point Scale ZeroScale
{
Point p = { .x = 2, .y = 3 };
int s = 0;
Point result = Point_Scale(p, s);
ASSERT(result.x == 0);
ASSERT(result.y == 0);
}
// Point Scale NegativeScale
{
Point p = { .x = 2, .y = 3 };
int s = -2;
Point result = Point_Scale(p, s);
ASSERT(result.x == -4);
ASSERT(result.y == -6);
}
#endif // UNUSED_CODE
}
char const* ExampleType_ToStringType(ExampleType type) {
switch (type) {
case eExampleType_Foo:
return kFooTypeName;
case eExampleType_Bar:
return kBarTypeName;
default:
return "Unknown";
}
}
// clang-format on