lib/tests: add more tests for fold functions

Also the invocation of tests is documented.
This commit is contained in:
Profpatsch 2017-03-18 20:41:02 +01:00
parent cb9ff8bfa7
commit 4f1d977877

View File

@ -1,3 +1,6 @@
# to run these tests:
# nix-instantiate --eval --strict nixpkgs/lib/tests.nix
# if the resulting list is empty, all tests passed
let inherit (builtins) add; in
with import ./default.nix;
@ -45,10 +48,34 @@ runTests {
expected = ["b" "c"];
};
testFold = {
expr = fold (builtins.add) 0 (range 0 100);
expected = 5050;
};
testFold =
let
f = op: fold: fold op 0 (range 0 100);
# fold with associative operator
assoc = f builtins.add;
# fold with non-associative operator
nonAssoc = f builtins.sub;
in {
expr = {
assocRight = assoc foldr;
# right fold with assoc operator is same as left fold
assocRightIsLeft = assoc foldr == assoc foldl;
nonAssocRight = nonAssoc foldr;
nonAssocLeft = nonAssoc foldl;
# with non-assoc operator the fold results are not the same
nonAssocRightIsNotLeft = nonAssoc foldl != nonAssoc foldr;
# fold is an alias for foldr
foldIsRight = nonAssoc fold == nonAssoc foldr;
};
expected = {
assocRight = 5050;
assocRightIsLeft = true;
nonAssocRight = 50;
nonAssocLeft = (-5050);
nonAssocRightIsNotLeft = true;
foldIsRight = true;
};
};
testTake = testAllTrue [
([] == (take 0 [ 1 2 3 ]))