unit-threaded v0.6.19 - preliminary support for property-based testing

Atila Neves via Digitalmars-d-announce digitalmars-d-announce at puremagic.com
Sat Jul 2 11:21:10 PDT 2016


http://code.dlang.org/packages/unit-threaded

After merging code from Robert's fork I added some support for 
property-based testing. There's no shrinking yet and user-defined 
types aren't supported. Right now most if not all primitive types 
are, as well as string, wstring, dstring and arrays of any of 
them. Here's a simple example that is hopefully self-explanatory:


@("sorting int[] twice yields the same result")
unittest {
     import std.algorithm: sort;
     check!((int[] a) {
         sort(a);
         auto b = a.dup;
         sort(b);
         return a == b;
     });
}

Essentially, call `check` with a property function and it will 
get run (by default) 100 times with random inputs. Multiple 
parameters are supported, but the function must return bool.

I wrote this today to test cerealed, my serialization library:

@Types!(bool, byte, ubyte, short, ushort, int, uint, long, ulong,
         float, double,
         char, wchar, dchar,
         ubyte[], ushort[], int[])
void testEncodeDecodeProperty(T)() {
     check!((T val) {
         auto enc = Cerealiser();
         enc ~= val;
         auto dec = Decerealiser(enc.bytes);
         return dec.value!T == val;
     });
}

That's running 100 auto-generated tests for each of those 17 
types, checking that if you serialize and then deserialize you 
should get the same value back. Not bad for 12 lines of code, huh?

Destroy!

Atila




More information about the Digitalmars-d-announce mailing list