How to write unit tests in plain C? -


i've started dig glib documentation , discovered offers unit testing framework.

but how unit tests in procedural language? or require program oo in c?

unit testing requires "cut-planes" or boundaries @ testing can done. quite straightforward test c functions not call other functions, or call other functions tested. examples of functions perform calculations or logic operations, , functional in nature. functional in sense same input results in same output. testing these functions can have huge benefit, though small part of thought of unit testing.

more sophisticated testing, such use of mocks or stubs possible, not easy in more dynamic languages, or object oriented languages such c++. 1 way approach use #defines. 1 example of this article, unit testing opengl applications, shows how mock out opengl calls. allows test valid sequences of opengl calls made.

another option take advantage of weak symbols. example, mpi api functions weak symbols, if define same symbol in own application, implementation overrides weak implementation in library. if symbols in library weren't weak, duplicate symbol errors @ link time. can implement mock of entire mpi c api, allows ensure calls matched , there aren't calls cause deadlocks. possible load library's weak symbols using dlopen() , dlsym(), , pass call on if necessary. mpi provides pmpi symbols, strong, not necessary use dlopen() , friends.

you can realize many of benefits of unit testing c. harder, , may not possible same level of coverage might expect written in ruby or java, it's worth doing.


Comments

Popular posts from this blog

Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12:test (default-test) on project.Error occurred in starting fork -

windows - Debug iNetMgr.exe unhandle exception System.Management.Automation.CmdletInvocationException -

configurationsection - activeMq-5.13.3 setup configurations for wildfly 10.0.0 -