🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c
Article #1501

Re: Inversion-of-control and dependency-injection in C

#1501
From: Ross Finlayson
Date: Fri, 13 Nov 2026 01:22
196 lines
11681 bytes
On 11/12/2026 03:26 PM, Janis Papanagnou wrote:
> On 2026-11-12 22:15, Ross Finlayson wrote:
>> On 11/12/2026 12:11 PM, Lew Pitcher wrote:
>>> On Thu, 12 Nov 2026 09:41:04 -0800, Ross Finlayson wrote:
>>>
>>>> Hello, dear C experts, I'm hoping you can tell me a story about
>>>> modularity and composability, about the concerns of
>>>> inversion-of-control
>>>> and dependency-injection in C, basically about wiring
>>>> test-time and run-time targets, for example about conditional-includes
>>>> and various build-targets, probably mostly about function pointers.
>>>
>>> Pardon my impertinence, but this sounds like a homework problem to me.
>>>
>>> Could you rephrase your question without the academic wording?
>>
>> Not at all, meaning, no offense taken.
>
> Your original post and its intention had been also to me quite
> unclear. Here, now, I seem to understand at what you are aiming.
>
> (Disclaimer: my comments are just based on personal experience
> in our software developing practice from the 1990's and not some
> general accepted opinion or habits.)
>
>> How about "writing testable code", what are common frameworks
>> or best practices for "writing testable code" in C.
>
> Our approach was not to "write testable code" by _adapting_ to
> some existing framework. While we had standards and practices how
> to design software the _tests followed our software_ (not v.v.).
> One exception here was that including dedicated trace records in
> our code (that could be activated specifically WRT a module and
> in granularity) helped for the test evaluation and regressions.
>
> Then we have to differentiate what tests you have in mind here;
> there's various types of tests which require their own principles.
> For example; Unit Tests (in our [C++] context mainly classes, in
> "C" probably functions or a set of related functions) required
> functions that own little test programs would call to verify all
> the relevant conditions. We haven't used frameworks that created
> the tests but wrote our own dedicated test-functions along with the
> application functions. For our Component Tests (where components
> were in our case software pieces that constitute runtime processes
> with a well-defined interface) we wrote stubs that could challenge
> the components on the interface levels. Such stubs could operate
> some predefined procedures with well-defined input, or arbitrary
> data, or be an operator-triggered manual interaction, depending on
> the type of the component. Storing all the test input and output
> results made regression tests possible. For the Integration Tests I
> don't recall the procedures (it was done in a dedicated integration
> test group); but they defined and checked the system configuration,
> the interoperability (I think!) mostly manually. Malfunctions were
> reported back to development accompanied by protocols, traces, and
> actual data. The System Tests were checked against the specification
> documents, and the Acceptance Tests against the requirements specs,
> sometimes accompanied by the customers. One aspect yet not listed
> was the test of the GUIs; beside manual tests we used tools to do
> all that "mouse shoving and clicking". But given that the functional
> tests already happened on the automatable interface level in former
> stages that was effectively just to check the "UI ergonomy" ("UX")
> factors. And, finally, we had our own hierarchical regression test
> framework to run and document the tests on each abstraction stage.
> (Eventually there were also external third-party tools existing but
> back then we wrote (had to write) our own regression test framework
> that fulfilled our demands.)
>
>>
>> I'd suggest abstraction and layers and modularity and composability,
>
> Yes, those are principles that support the mentioned QA processes.
>
>> and having an inversion-of-control/dependency-injection approach,
>
> (See my comment about "tests follow code (not v.v.)". - Opinions may
> differ.)
>
>> a convention moreso than a framework, which pretty much means wrapping
>> all the standard functions, then about for example having an include
>> where those are wrapper functions to stub and mock, and another include
>> where those are macros, or, perhaps, using includes, to point the
>> include directory for the standard library, into instead what makes
>> for stubbable/mockable calls to standard library and system functions,
>> or just leaving the headers alone and linking to modules that aren't
>> the standard libraries, this being an example of something like
>> "can't mock strftime", or, for an example.
>
> Just a comment on one aspect here; yes, you have to organize your
> software and tests. Key principles are the modularity, hierarchical
> organization, being able to automate processes, and organize all in
> some consistent way. In case your company is certified (or wants to
> get certified) make sure your processes are documented and that you
> can reproduce your quality processes and the results.
>
>>
>> Then, about the modularity, and pointers (to pointers to pointers
>> to pointers, ...) to functions, then seems about conventions of
>> types for typeof and the like about that like anybody else I rely
>> on the compiler to fail with -Wall and failing on warnings to
>> catch type errors, ..., has that I want to program to interfaces
>> and then have objects composable, ..., about using conditions of
>> the build, to, basically make for test-time targets and run-time
>> targets, with actually testing the code that goes into the build.
>
> It's not clear to me what you want to say here but it sounds that
> you want very specific ("micro-management") advice on a low level?
> (If so, I can't and don't want to comment here.)
>
> (All above from memories and off the top of my head. Hope there's
> something useful for your purpose.)
>
> Janis
>

Thanks for writing.

Usually enough these days with alike "CI/CD", ..., or the latest
buzzwords of 2018, there's "build & test & scan & deploy", usually
enough that "build & test & scan" is usually enough for "the artifacts",
the build artifacts and the test artifacts and so on.
When "automating the pipeline" and these kinds of things, it's not
un-usual that the test-suite whatever it may be, has that there are
some XML formats that are readily ingested and fungible to the
framework, vis-a-vis, usually enough it's that "./test returns
either EXIT_SUCCESS or EXIT_FAILURE and that's our predicate
and build-step and action and work-flow too in our Git Hub/Lab".

So, for whatever results "test: pass/fail", may make for that
there are fungible report formats that might be useful downstream,
for example "automated code-reviews that show what broke where, when".


So, agreeably, basically it's figured a build step.

Then, it sounds like we're both familiar with settings where
the notions of "unit tests, module tests, integration tests,
end-to-end tests, smoke-tests, sanity-tests, activation-tests",
representing ye olde "acceptance criteria" usually though often
practically "broken" or "not broken" the build, then about the
mania for "branch and line coverage, with whatever possible stubs
and mocks make the instrumented code covered", that's agreeable
and usual.


Then, about testing the black-box or white-box, say, here it's
considered that both are apropos, like for example "health checks
and internal and external health checks". Then, here's a sort of
example, if you're familiar with something like "React and Angular
and these sorts of front-ends". So, often there are things like
"jest tests then Web Driver and somebody walked through the work-flows
and it re-plays them and it only takes 8 GiB and forty-five minutes".
Well, that's with the expectation that "it has to run in the browser",
yet, at one point it was reasonably simple, for example for "React",
about 16 or 17 I suppose, to just implement "the algorithm" and just
include a file that basically modeled the runtime, then it can test
all the workflows programmatically, as a sort of example. That
got less possible when "effects" were added, yet otherwise it
was possible to have it "test" a lot faster.

It's agreeable that it may be impossible to model the system in
software when it's involved a system, and, black-box and integration
tests ("don't forget regression!"), then it may also be so that
a sort of mini-model of the surface of the library and framework
that's used, can be much, much reduced, from all the library. For
example, say "C" has 100's standard functions and "POSIX" hundreds
more. Well, often I have here programs that effectively use dozens, or
less. Then, the model of the machine is plenty involved, like "the
operating system" and "the process model" and "handles" and "sockets"
and "signals" all that, it may make sense though to make "mini-models",
basically emulating "the surface", i.e., "the test-able surface".


Then, about C, which may be less structured for abstraction as
modeling the surface of the run-time, has that many use-cases
only use a tiny, tiny fraction of the run-time, about figuring
that as modular and composable, and with basically zero available
tooling, this kind of thing.


Abstraction introduces complexity, and indirection, with the
usual idea that it compiles to what's the "zero-cost abstraction",
i.e. the abstraction gets compiled away, here basically about
replacing the implementations of the standard and library functions
with a, "mini-model", of operation, then to sort of build that
and make it re-usable, then to be able to build unit-tests off
that (and, you know, unit tests of those, ..., peeling-the-onion),
that "verified" and "certified" are like "validated" and only
"not falsified" and "not in-verified" and "not in-certified"
and "not in-validated", ..., then for "module tests" after those
unit test, then after the real build artifacts built against the
real dependencies, integration and so on.




Technical Details
Message-ID: <k9-cnQp2h5HY-2v3nZ2dnZfqn_ednZ2d@giganews.com>
Path: mbox-mo1.pugleaf.net!news-lax.pugleaf.net!srv.newsdeef.eu!weretis.net!feeder9.news.weretis.net!border-4.nntp.ord.giganews.com!nntp.giganews.com!local-1.nntp.ord.giganews.com!Xl.tags.giganews.com!local-4.nntp.ord.giganews.com!news.giganews.com.POSTED!not-for-mail
References: <yQOdnQ-qnKiiZ2j3nZ2dnZfqn_udnZ2d@giganews.com> <11d56gl$3ud5q$1@dont-email.me> <16ScnZKb_MQfsWv3nZ2dnZfqn_idnZ2d@giganews.com> <11d5huo$3flo0$1@dont-email.me>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-lax.pugleaf.net!srv.newsdeef.eu!weretis.net!feeder9.news.weretis.net!border-4.nntp.ord.giganews.com!nntp.giganews.com!local-1.nntp.ord.giganews.com!Xl.tags.giganews.com!local-4.nntp.ord.giganews.com!news.giganews.com.POSTED!not-for-mail
NNTP-Posting-Date: Fri, 13 Nov 2026 01:22:13 +0000
Subject: Re: Inversion-of-control and dependency-injection in C
Newsgroups: comp.lang.c
References: <yQOdnQ-qnKiiZ2j3nZ2dnZfqn_udnZ2d@giganews.com>
 <11d56gl$3ud5q$1@dont-email.me>
 <16ScnZKb_MQfsWv3nZ2dnZfqn_idnZ2d@giganews.com>
 <11d5huo$3flo0$1@dont-email.me>
From: Ross Finlayson <ross.a.finlayson@gmail.com>
Date: Thu, 12 Nov 2026 17:22:00 -0800
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101
 Thunderbird/38.6.0
MIME-Version: 1.0
In-Reply-To: <11d5huo$3flo0$1@dont-email.me>
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: 7bit
Message-ID: <k9-cnQp2h5HY-2v3nZ2dnZfqn_ednZ2d@giganews.com>
Lines: 196
X-Usenet-Provider: http://www.giganews.com
X-Trace: sv3-slCLTLuRgFytMEzwQc25NjVxF+7wRa4jBZIcWhMizbQNC3qBmdP5XsGrZDtmOCKwNcQ8EA9OQlT8Ybq!VbaAXXgo4C6NWEwX0ZdfV87vhEsKs41fTQq8Mvl6sNX2uRoi+XMpIfMT29YOHojqaeTZifAlDvUJ
X-Complaints-To: abuse@giganews.com
X-DMCA-Notifications: http://www.giganews.com/info/dmca.html
X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers
X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly
X-Postfilter: 1.3.40