diff options
| author | Sebastiano Tronto <sebastiano@tronto.net> | 2024-09-05 08:29:31 +0200 |
|---|---|---|
| committer | Sebastiano Tronto <sebastiano@tronto.net> | 2024-09-05 08:29:31 +0200 |
| commit | 5a5502e5275f7a3f1eaa95bde26257e732cacaf9 (patch) | |
| tree | 760eecb3bd54c4aee0ae71514b229fa9febcaa26 /src/blog | |
| parent | 255738c334b055209b710b948368ba66cc1dcb52 (diff) | |
| download | sebastiano.tronto.net-5a5502e5275f7a3f1eaa95bde26257e732cacaf9.tar.gz sebastiano.tronto.net-5a5502e5275f7a3f1eaa95bde26257e732cacaf9.zip | |
Fixed blog post
Diffstat (limited to 'src/blog')
| -rw-r--r-- | src/blog/2023-11-14-test-visibility-c-macro/foo4.c | 6 | ||||
| -rw-r--r-- | src/blog/2023-11-14-test-visibility-c-macro/test-visibility-c-macro.md | 21 |
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) | 12 | STATIC 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, | ||
| 4 | I used the identifier `_static` instead of `STATIC` for a macro. | ||
| 5 | I have recently found out that one should almost never use identifiers | ||
| 6 | starting 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) | ||
| 8 | for an explanation.* | ||
| 9 | |||
| 3 | As a programmer, I often face this dilemma: should I make this | 10 | As a programmer, I often face this dilemma: should I make this |
| 4 | function private to improve encapsulation, or should I make it | 11 | function private to improve encapsulation, or should I make it |
| 5 | public so that I can write tests for it? I believe this problem is | 12 | public 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 | ||
| 117 | Now put the snippet above at the top of the C file where the functions | 124 | Now put the snippet above at the top of the C file where the functions |
| 118 | you want to test are implemented and declare your functions as | 125 | you 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, |
| 120 | these functions will be compiled as `static`, but if you use the | 127 | these 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 |
| 122 | will be visible outside the file. | 129 | will be visible outside the file. |
| 123 | 130 | ||
| 124 | Here is a complete example. | 131 | Here 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) | 144 | STATIC int foo(int x, int y) |
| 138 | { | 145 | { |
| 139 | return 42*x - 69*y; | 146 | return 42*x - 69*y; |
| 140 | } | 147 | } |
