From 4f1d977877b5477d84ca1204ec7a3f87d6bbf871 Mon Sep 17 00:00:00 2001 From: Profpatsch Date: Sat, 18 Mar 2017 20:41:02 +0100 Subject: [PATCH] lib/tests: add more tests for fold functions Also the invocation of tests is documented. --- lib/tests.nix | 35 +++++++++++++++++++++++++++++++---- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/lib/tests.nix b/lib/tests.nix index d33e3a824e3..f7119cf18ab 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 ]))