summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorProfpatsch <mail@profpatsch.de>2017-03-18 20:41:02 +0100
committerProfpatsch <mail@profpatsch.de>2017-03-19 22:06:53 +0100
commit4f1d977877b5477d84ca1204ec7a3f87d6bbf871 (patch)
tree3384b87c34eb8cae56d497f8e0bbedcc39fc8c7d
parentcb9ff8bfa73fdfa1a38b609e878a52650dfdb682 (diff)
lib/tests: add more tests for fold functions
Also the invocation of tests is documented.
-rw-r--r--lib/tests.nix35
1 files changed, 31 insertions, 4 deletions
diff --git a/lib/tests.nix b/lib/tests.nix
index d33e3a824e34..f7119cf18ab6 100644
--- a/lib/tests.nix
+++ b/lib/tests.nix
@@ -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 ]))