aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/blog/2023-11-14-test-visibility-c-macro/foo4.c6
-rw-r--r--src/blog/2023-11-14-test-visibility-c-macro/test-visibility-c-macro.md21
2 files changed, 17 insertions, 10 deletions
diff --git a/src/blog/2023-11-14-test-visibility-c-macro/foo4.c b/src/blog/2023-11-14-test-visibility-c-macro/foo4.c
index 85dea34..66a0c82 100644
--- a/src/blog/2023-11-14-test-visibility-c-macro/foo4.c
+++ b/src/blog/2023-11-14-test-visibility-c-macro/foo4.c
@@ -4,12 +4,12 @@
4/* cc main4.c # foo is static */ 4/* cc main4.c # foo is static */
5 5
6#ifdef TEST 6#ifdef TEST
7#define _static 7#define STATIC
8#else 8#else
9#define _static static 9#define STATIC static
10#endif 10#endif
11 11
12_static int foo(int x, int y) 12STATIC int foo(int x, int y)
13{ 13{
14 return 42*x - 69*y; 14 return 42*x - 69*y;
15} 15}
diff --git a/src/blog/2023-11-14-test-visibility-c-macro/test-visibility-c-macro.md b/src/blog/2023-11-14-test-visibility-c-macro/test-visibility-c-macro.md
index ffc9956..0716aad 100644
--- a/src/blog/2023-11-14-test-visibility-c-macro/test-visibility-c-macro.md
+++ b/src/blog/2023-11-14-test-visibility-c-macro/test-visibility-c-macro.md
@@ -1,5 +1,12 @@
1# Making functions public for tests only... with C macros! 1# Making functions public for tests only... with C macros!
2 2
3*Note from the future (2024-09-05): in the first version of this post,
4I used the identifier `_static` instead of `STATIC` for a macro.
5I have recently found out that one should almost never use identifiers
6starting with underscore, so I have edited this post accordingly. See
7[this StackOverflow question](https://stackoverflow.com/questions/69084726/what-are-the-rules-about-using-an-underscore-in-a-c-identifier)
8for an explanation.*
9
3As a programmer, I often face this dilemma: should I make this 10As a programmer, I often face this dilemma: should I make this
4function private to improve encapsulation, or should I make it 11function private to improve encapsulation, or should I make it
5public so that I can write tests for it? I believe this problem is 12public so that I can write tests for it? I believe this problem is
@@ -108,17 +115,17 @@ The trick I came up with is the following:
108 115
109``` 116```
110#ifdef TEST 117#ifdef TEST
111#define _static 118#define STATIC
112#else 119#else
113#define _static static 120#define STATIC static
114#endif 121#endif
115``` 122```
116 123
117Now put the snippet above at the top of the C file where the functions 124Now put the snippet above at the top of the C file where the functions
118you want to test are implemented and declare your functions as 125you want to test are implemented and declare your functions as
119`_static` with an underscore. When you compile your code normally, 126`STATIC`. When you compile your code normally,
120these functions will be compiled as `static`, but if you use the 127these functions will be compiled as `static`, but if you use the
121`-DTEST` option, `_static` will expand to nothing and the functions 128`-DTEST` option, `STATIC` will expand to nothing and the functions
122will be visible outside the file. 129will be visible outside the file.
123 130
124Here is a complete example. 131Here is a complete example.
@@ -129,12 +136,12 @@ Here is a complete example.
129#include <stdio.h> 136#include <stdio.h>
130 137
131#ifdef TEST 138#ifdef TEST
132#define _static 139#define STATIC
133#else 140#else
134#define _static static 141#define STATIC static
135#endif 142#endif
136 143
137_static int foo(int x, int y) 144STATIC int foo(int x, int y)
138{ 145{
139 return 42*x - 69*y; 146 return 42*x - 69*y;
140} 147}

Generated with cgit - Back to sebastiano.tronto.net