Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Post History

77%
+5 −0
Q&A Testing an opaque type's internals

First: What is an opaque pointer in C? Now when it comes to testing such a type, I know of 3 ways: Include the source file (the one containing the definition of the type and the functions tha...

2 answers  ·  posted 7mo ago by Melkor-1‭  ·  last activity 7mo ago by Alexei‭

#2: Post edited by user avatar Melkor-1‭ · 2024-05-13T15:14:34Z (7 months ago)
  • First: [What is an opaque pointer in C?](https://stackoverflow.com/q/7553750/20017547)
  • Now when it comes to testing such a type, I know of 3 ways:
  • 1. Include the source file (the one containing the definition of the type and the functions that work with it) directory into the test source file. (This is the easiest, but often discouraged without presenting any rationale).
  • 2. Make public accessors that are conditionally available (i.e. only if if `LIB_TEST` is defined before including its header).
  • 3. Make a separate "lib-test.h" header file that contains the public accessors.
  • The last two avoid making the accessors part of the public API (we're speaking of the case where the clients have no business knowing anything about the opaque type's internals, and shouldn't be provided with any accessors).
  • What is the usual approach (or the good approach) in C? Does one way have more upsides/downsides than the other?
  • First: [What is an opaque pointer in C?](https://stackoverflow.com/q/7553750/20017547)
  • Now when it comes to testing such a type, I know of 3 ways:
  • 1. Include the source file (the one containing the definition of the type and the functions that work with it) directory into the test source file. (This is the easiest, but often discouraged without presenting any rationale).
  • 2. Make public accessors that are conditionally available (i.e. only if if `LIB_TEST` is defined before including its header).
  • 3. Make a separate "lib-test.h" header file that contains the public accessors.
  • The last two avoid making the accessors part of the public API (we're speaking of the case where the clients have no business knowing anything about the opaque type's internals, and shouldn't be provided with any accessors).
  • What is the usual approach (or the good approach) in C? Does one way have more upsides/downsides than the other?
  • As an example, say I have this `struct`:
  • ```c
  • typedef struct arena {
  • size_t count;
  • size_t capacity;
  • size_t current;
  • size_t last_alloc_size;
  • M_Pool *pools[];
  • } Arena;
  • ```
  • and a function associated with it:
  • ```c
  • void arena_reset(Arena *arena)
  • {
  • for (size_t i = 0; i < arena->count; ++i) {
  • arena->pools[i]->offset = 0;
  • }
  • arena->current = 1;
  • }
  • ```
  • Now I wish to assert that `arena_reset()` actually has set the `offset` field of all the arena's `pools` to 0, and the arena's `current` field to 1. Including the API (header file) that this library provides is of no good, because only a forward declaration of the struct type has been provided, and its members can not be accessed.
#1: Initial revision by user avatar Melkor-1‭ · 2024-05-13T15:02:35Z (7 months ago)
Testing an opaque type's internals
First: [What is an opaque pointer in C?](https://stackoverflow.com/q/7553750/20017547)

Now when it comes to testing such a type, I know of 3 ways:

1. Include the source file (the one containing the definition of the type and the functions that work with it) directory into the test source file. (This is the easiest, but often discouraged without presenting any rationale).

2. Make public accessors that are conditionally available (i.e. only if if `LIB_TEST` is defined before including its header).

3. Make a separate "lib-test.h" header file that contains the public accessors.

The last two avoid making the accessors part of the public API (we're speaking of the case where the clients have no business knowing anything about the opaque type's internals, and shouldn't be provided with any accessors).

What is the usual approach (or the good approach) in C? Does one way have more upsides/downsides than the other?