about summary refs log tree commit diff
path: root/lib/tests
diff options
context:
space:
mode:
authorRobert Hensing <roberth@users.noreply.github.com>2023-06-06 19:09:56 +0200
committerGitHub <noreply@github.com>2023-06-06 19:09:56 +0200
commit6b078d2f5a2d6d4936fa1fa75786fec0a6137e8e (patch)
tree51b6632fcf62144300e6428fc9b3ad9e51871765 /lib/tests
parentb52f6ac65d27e21516e78bd87b41ea1c81eef8b6 (diff)
parent9790e70150c83693fa2bcdc65814d01536bf4915 (diff)
Merge pull request #235267 from tweag/lazier-findFirst
`lib.findFirst`: Add tests and make lazier
Diffstat (limited to 'lib/tests')
-rw-r--r--lib/tests/misc.nix40
1 files changed, 40 insertions, 0 deletions
diff --git a/lib/tests/misc.nix b/lib/tests/misc.nix
index 231f19c513eb2..ce980436c1bcb 100644
--- a/lib/tests/misc.nix
+++ b/lib/tests/misc.nix
@@ -518,6 +518,46 @@ runTests {
     expected = false;
   };
 
+  testFindFirstExample1 = {
+    expr = findFirst (x: x > 3) 7 [ 1 6 4 ];
+    expected = 6;
+  };
+
+  testFindFirstExample2 = {
+    expr = findFirst (x: x > 9) 7 [ 1 6 4 ];
+    expected = 7;
+  };
+
+  testFindFirstEmpty = {
+    expr = findFirst (abort "when the list is empty, the predicate is not needed") null [];
+    expected = null;
+  };
+
+  testFindFirstSingleMatch = {
+    expr = findFirst (x: x == 5) null [ 5 ];
+    expected = 5;
+  };
+
+  testFindFirstSingleDefault = {
+    expr = findFirst (x: false) null [ (abort "if the predicate doesn't access the value, it must not be evaluated") ];
+    expected = null;
+  };
+
+  testFindFirstNone = {
+    expr = builtins.tryEval (findFirst (x: x == 2) null [ 1 (throw "the last element must be evaluated when there's no match") ]);
+    expected = { success = false; value = false; };
+  };
+
+  # Makes sure that the implementation doesn't cause a stack overflow
+  testFindFirstBig = {
+    expr = findFirst (x: x == 1000000) null (range 0 1000000);
+    expected = 1000000;
+  };
+
+  testFindFirstLazy = {
+    expr = findFirst (x: x == 1) 7 [ 1 (abort "list elements after the match must not be evaluated") ];
+    expected = 1;
+  };
 
 # ATTRSETS