sebastiano.tronto.net

Source files and build scripts for my personal website
git clone https://git.tronto.net/sebastiano.tronto.net
Download | Log | Files | Refs | README

commit 5a5502e5275f7a3f1eaa95bde26257e732cacaf9
parent 255738c334b055209b710b948368ba66cc1dcb52
Author: Sebastiano Tronto <sebastiano@tronto.net>
Date:   Thu,  5 Sep 2024 08:29:31 +0200

Fixed blog post

Diffstat:
Msrc/blog/2023-11-14-test-visibility-c-macro/foo4.c | 6+++---
Msrc/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 @@ -4,12 +4,12 @@ /* cc main4.c # foo is static */ #ifdef TEST -#define _static +#define STATIC #else -#define _static static +#define STATIC static #endif -_static int foo(int x, int y) +STATIC int foo(int x, int y) { return 42*x - 69*y; } 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 @@ -1,5 +1,12 @@ # Making functions public for tests only... with C macros! +*Note from the future (2024-09-05): in the first version of this post, +I used the identifier `_static` instead of `STATIC` for a macro. +I have recently found out that one should almost never use identifiers +starting with underscore, so I have edited this post accordingly. See +[this StackOverflow question](https://stackoverflow.com/questions/69084726/what-are-the-rules-about-using-an-underscore-in-a-c-identifier) +for an explanation.* + As a programmer, I often face this dilemma: should I make this function private to improve encapsulation, or should I make it 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: ``` #ifdef TEST -#define _static +#define STATIC #else -#define _static static +#define STATIC static #endif ``` Now put the snippet above at the top of the C file where the functions you want to test are implemented and declare your functions as -`_static` with an underscore. When you compile your code normally, +`STATIC`. When you compile your code normally, these functions will be compiled as `static`, but if you use the -`-DTEST` option, `_static` will expand to nothing and the functions +`-DTEST` option, `STATIC` will expand to nothing and the functions will be visible outside the file. Here is a complete example. @@ -129,12 +136,12 @@ Here is a complete example. #include <stdio.h> #ifdef TEST -#define _static +#define STATIC #else -#define _static static +#define STATIC static #endif -_static int foo(int x, int y) +STATIC int foo(int x, int y) { return 42*x - 69*y; }