7 ms·
Using Zig to Unit Test a C Application
- raoulj 3y agoI've done this where I use python's ctypes library to write tests for a c codebase. This feels very similar in that it can be tricky to get the type interop correct the first time around. What other strategies/solutions do people like to use for testing their c projects?
- fredyr 3y agoI've done a lot of CFFI with Python for this kind of testing in the past. But nowadays I'm also looking at Zig for this.
- physPop 3y agothe point of zig is that there is no FFI to get right
- pastage 3y agoHas the way Python handles this changed a lot in the last decades? I have never did it because I feared having to chase changes in Python and an ever changing build environment. Mostly a focus thing I knew I alone would not be the one to fix it across all build envs. At the time I was the lone Pythonista now everyone we hire is comfortable with Python so I made the wrong choice.
- ColonelPhantom 3y agoI think (but I'm not sure) that in Zig you can just @cInclude("header.h") or something like that? So your C types are automatically imported/converted to Zig types.
- convivialdingo 3y agoIt would be nice if something in Zig could auto-magically generate the zig to c interop call and type declarations. I see there's "translate-c" to help migrate C to Zig, and maybe that's enough. Generally I fall on the side of if a compiler can toss out a type warning, it could certainly give a reasonable solution also. Best-guess with a "// Warning type was guessed" is better than an opaque "RTFM" in most every case.
- throwawaymaths 3y ago> It would be nice if something in Zig could auto-magically generate the zig to c interop call and type declarations. Do you mean building a .h file from zig code? The other direction is just @cImport.
- wredue 3y agoIf you watch some of the zig streams, there are some C libraries that are quite complex for importing. It’s not always just an @cImport. I wouldn’t so much pin this on zig as much as the complete clusterfuck that is building c libraries.
- throwawaymaths 3y agoOh sure, was just curious what feature gp was looking for. I would love the first one for example
- systems 3y agozig , ocaml, odin many system languages are making the headlines lately its very hard to pick one to learn not sure how to deal with this, learn them all, bet on one, what should we do
- leosanchez 3y agoocaml is a systems language ?
- golergka 3y agoIs ocaml a system language?
- avbanks 3y agoYes believe it or not ocaml is a systems language.
- systems 3y agoyes it can be used to create systems and backend tools - it is used to create an OS https://github.com/mirage - it is used to create a transpiler https://melange.re/v2.2.0/ - it was used to create Rust first compiler ocaml is surely a systems language
- Rendello 3y agoThe predominant OCaml shop is Jane Street, they have a good podcast where they talk to those involved in their infrastructure. A lot of the episodes go into the tradeoffs between the GC'd and functional OCaml vs languages like C++ and Rust: https://signalsandthreads.com/ https://signalsandthreads.com/
- Buttons840 3y agoThere's nothing "system" about a compiler. What language hasn't been used to create a compiler or two? If languages used to create compilers are system languages, then all languages are system languages. Writing a OS is more "system", and certainly those using the Mirage operating system use OCaml as a system language.
- throwawaymaths 3y agoThis is going to get really good when zlibc is done, you'll be able to for example, override the c stdlib "free" function in the c code and add features, for example (runtime) UAF/DF detection with metadata tracking (like stacktraces of where the memory was created and freed)
- duped 3y agoYou can already do this with LD_PRELOAD, no?
- throwawaymaths 3y agoCould be wrong, but only if libc is expected to be dynamically linked (and anyways you'd still need zlibc)
- staunton 3y agoYou can just define your own malloc and free in any C program. Am I missing what you're talking about?
- maxmcd 3y agoWhat is zlibc? A libc implementation in zig? Googling did not turn up much.
- dataangel 3y agoYou can do that in any C program today
- jedisct1 3y agoI also now always use Zig to write tests and benchmarks for C code. This is for example the case in libaegis: https://github.com/jedisct1/libaegis https://github.com/jedisct1/libaegis Calling C functions from Zig is easy (C headers can be imported directly) and doesn't have any overhead. So I can take advantage of the convenience of Zig, even if the tested code only requires a C compiler. I also now always add Zig build files as an alternative to make/libtool/automake/cmake/meson/etc. The main advantage is that cross-compilation to many targets is supported out of the box, including to WebAssembly. So I can quickly test if the C code compiles fine before actually trying to run it on an emulator.
- mtlynch 3y agoUsing it to benchmark C is a cool idea. I hadn't thought of that! Thanks for sharing!
- acqq 3y ago> The main advantage is that cross-compilation to many targets is supported out of the box For the readers who aren't familiar: The code for running test and then cross-compiling (on one machine and OS) for different target platforms is: https://github.com/jedisct1/libaegis/blob/main/.github/workflows/ci.yml https://github.com/jedisct1/libaegis/blob/main/.github/workf... and the only zig file in the repo which drives the build process is: https://github.com/jedisct1/libaegis/blob/main/build.zig https://github.com/jedisct1/libaegis/blob/main/build.zig
- giancarlostoro 3y agoCurious now if you could use D in the same way? Walter Bright built a faster preprocessor for Facebook (kind of a sidenote and not fully relevant), wouldnt surprise me if D is another good candidate especially given its age.
- brabel 3y agoYes: https://dlang.org/spec/importc.html https://dlang.org/spec/importc.html The D compiler(s), as Zig, can be used to compile C code... and D code can import C files as if they were D modules (as in Zig, there are some special types to represent C strings and other types that are not exactly the same).
- giancarlostoro 3y agoThanks for that! I'm a big fan of D, as someone who primarily codes in C# and Python. I really wish a big company would take on D in a serious way that puts it on the map significantly. It is an underrated language in my opinion.
- brabel 3y agoAbsolutely. It was way ahead of its time. And it's improving, albeit slowly. With stuff like @safe, @nogc, pure, betterC[1], and its unmatched metaprogramming, it's a more modern language than most of the newer languages popping up. [1] https://dlang.org/blog/2017/08/23/d-as-a-better-c/ https://dlang.org/blog/2017/08/23/d-as-a-better-c/
- giancarlostoro 3y agoIf someone would hire me to use it, I would take the offer in a heartbeat. Until then I just play with it to test out language features and check it out. I wanted to like Vibe.d but I wanted something a little more batteries included. I might check out the Hunt Framework though.
- roland35 3y agoThis is interesting! I like how unit tests are easier with Zig. The approach I am more familiar with is using Google test and C++ to test C code. It's pretty easy if you already have a cmake project set up, and most C developers can wrap their heads around GTest.
- kazinator 3y agoUnit test it with TXR: $ pwd /home/kaz/ustreamer $ gcc -D_GNU_SOURCE -shared src/libs/base64.c -o base64.o $ valgrind txr --free-all ==8785== Memcheck, a memory error detector ==8785== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al. ==8785== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info ==8785== Command: txr --free-all ==8785== This is the TXR Lisp interactive listener of TXR 292. Quit with :quit or Ctrl-D on an empty line. Ctrl-X ? for cheatsheet. Psst! The complimentary Allen key that comes with TXR is inpired by IKEA. 1> (with-dyn-lib "./base64.o" (deffi us-base64-encode "us_base64_encode" void (buf size-t (ptr (array 1 str-d)) (ptr (array 1 size-t))))) us-base64-encode 2> (let ((out (vec nil)) (sz (vec 0))) (us-base64-encode #b'00112233445566778899AABBCCDDEEFF' 16 out sz) (list out sz)) (#("ABEiM0RVZneImaq7zN3u/w==") #(25)) 3> (let ((out (vec "abcde")) (sz (vec 5))) (us-base64-encode #b'00112233445566778899AABBCCDDEEFF' 16 out sz) (list out sz)) (#("ABEiM0RVZneImaq7zN3u/w==") #(25)) 4> (let ((out (vec "xxxxxxxxxxxxxxxxxxxxxxxxxx")) (sz (vec 25))) (us-base64-encode #b'00112233445566778899AABBCCDDEEFF' 16 out sz) (list out sz)) (#("ABEiM0RVZneImaq7zN3u/w==") #(25)) 5> (let ((out (vec "xxxxxxxxxxxxxxxxxxxxxxxxxxxx")) (sz (vec 27))) (us-base64-encode #b'00112233445566778899AABBCCDDEEFF' 16 out sz) (list out sz)) (#("ABEiM0RVZneImaq7zN3u/w==") #(27)) 6> ==8785== ==8785== HEAP SUMMARY: ==8785== in use at exit: 0 bytes in 0 blocks ==8785== total heap usage: 24,782 allocs, 24,782 frees, 4,806,317 bytes allocated ==8785== ==8785== All heap blocks were freed -- no leaks are possible ==8785== ==8785== For counts of detected and suppressed errors, rerun with: -v ==8785== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0) We covered the cases when the the destination buffer is already allocated, and smaller, equal to or in excess of the space being required. Thus testing the cases when realloc is or isn't necessary. No memory errors or leaks. We can see in the last case that when the buffer is larger, the function leaves the size alone, returning our original 27.
- deleted 3y ago[deleted]
- pjmlp 3y agoC++ doing this for decades.
- DrNosferatu 3y agoGreat use case and a possible killer app for Zig. However, for the complete newcomer to Zig, this post seems a bit convoluted in this presentation. What about the Zig authors do a step by step tutorial on how to leverage Zig to implement tests in a C codebase? Would be a great entry point for the broader community!