Posts Tagged ‘Ocaml’
CamlP4 for unit testing
We’ve been mulling over how to add new unit tests to our OCaml codebase. Our first thought was to write a test suite apart from the code itself; we’ve already some end-to-end tests written in that way. The problem with that approach is that the tests are separated from the code they’re testing, which discourages writing tests.
A few weeks ago, we wrote some time- and heap-profiling CamlP4 macros, which add entries to a centralized table as our code runs. When the program finishes, it spits out statistics that we can examine for time and space sinks. The profiling hints — macros, that is — wrap the code to be profiled. So why not use the same approach for our tests?
In Praise of s-Expressions
OCaml is a wonderful language. But as with any language, there are certain features you would like to see.
The obvious one in OCaml is the lack of type-classes, specifically the lack of Haskell’s Show. There is no more useful debugging tool than printf, and it is a pain trying to debug complex types when you can’t see them.
This is where s-expressions come in. Specifically, Jane Street’s s-expression library.
Strict Beyond Reproach
Pascal Cuoq made an interesting comment on my last post about C developers accidentally writing “==” in OCaml when they meant to use “=”. It reminds me of a similar issue I run into, when I am writing in OCaml but thinking in Haskell, and I am confronted with a value of type:
'a option list
Glossing over Bugs
We have a set of end-to-end tests that run on Goanna every night. This ensures that our commits during the day don’t break our development tree too badly.
Here is one of those tests. It was producing a strange result.
void example(void) {
int *x;
x = malloc(sizeof(int));
free(x);
if (rand()) {
x = malloc(sizeof(int));
}
*x++;
}
