lib/tests: add more tests for fold functions
Also the invocation of tests is documented.
This commit is contained in:
parent
cb9ff8bfa7
commit
4f1d977877
|
@ -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
|
let inherit (builtins) add; in
|
||||||
with import ./default.nix;
|
with import ./default.nix;
|
||||||
|
|
||||||
|
@ -45,10 +48,34 @@ runTests {
|
||||||
expected = ["b" "c"];
|
expected = ["b" "c"];
|
||||||
};
|
};
|
||||||
|
|
||||||
testFold = {
|
testFold =
|
||||||
expr = fold (builtins.add) 0 (range 0 100);
|
let
|
||||||
expected = 5050;
|
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 [
|
testTake = testAllTrue [
|
||||||
([] == (take 0 [ 1 2 3 ]))
|
([] == (take 0 [ 1 2 3 ]))
|
||||||
|
|
Loading…
Reference in a new issue