Live data from Hacker News

Using Zig to Unit Test a C Application

mtlynch.io

11–20 of 54 posts

Re: Using Zig to Unit Test a C Application

#11

It 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…

> 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.

If 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.

Re: Using Zig to Unit Test a C Application

#12
I 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

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.

Re: Using Zig to Unit Test a C Application

#13
post #8
post #4

zig , 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

Is ocaml a system language?

It depends on what you mean by "system language." For me, this category mainly includes languages that provide fine-grained control over memory management (C, C++, Zig, Rust, ...), so I personally wouldn't include OCaml.

Re: Using Zig to Unit Test a C Application

#14
post #4

zig , 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

I think the two main ones are Rust and Zig. This may be controversial (but it seems very obvious to me) that:

Rust is created in the same spirit that created and evolved C++: create a complex and featureful language that enables compiling your solution from a high level representation in an expressive/safe/performant way.

Zig is created in the same spirit that created and evolved C: create a simple language that allows you to directly and transparently represent and reason about what you want to have happen.

You'll probably think that one of these statements is more biased than the other, and that probably reflects your own preferences :)

Re: Using Zig to Unit Test a C Application

#15

I 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 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/l…

Using it to benchmark C is a cool idea. I hadn't thought of that!

Thanks for sharing!

Re: Using Zig to Unit Test a C Application

#16
post #11

Earlier quoted context omitted.

> 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.

If 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.

Oh sure, was just curious what feature gp was looking for. I would love the first one for example

Re: Using Zig to Unit Test a C Application

#17
post #4

zig , 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

I think the two main ones are Rust and Zig. This may be controversial (but it seems very obvious to me) that: Rust is created in the same spirit that created and evolved C++: create a complex and featureful language that enables compiling your solution from a high level representation in an expressive/safe/performant way. Zig is created in the same spirit that created and evolved C: create a simple language that allo…

I think a stronger statement is that zig is explicitly created to out-C c. Even the high level comptime stuff evolved out of simplification and explicit-ification of some opaque things that c (and especially c++) compilers might do.

Re: Using Zig to Unit Test a C Application

#18
post #2

I'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?

I'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.

Re: Using Zig to Unit Test a C Application

#19
post #10
post #8

Earlier quoted context omitted.

Is ocaml a system language?

yes 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

The 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/

Re: Using Zig to Unit Test a C Application

#20

This 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)

You can already do this with LD_PRELOAD, no?
Post reply on HN