summary refs log tree commit diff
path: root/lib/tests/misc.nix
diff options
context:
space:
mode:
authorSilvan Mosberger <silvan.mosberger@tweag.io>2023-07-14 19:26:08 +0200
committerSilvan Mosberger <silvan.mosberger@tweag.io>2023-07-20 22:42:01 +0200
commit7f61b01600ecbdaa2a57c51097f2ece9e421c4fe (patch)
tree4b18b3b06cb8f0771688757af353b45b56b0dbef /lib/tests/misc.nix
parent53dcfd24ad71bb1a26c9d17cbd2af1f83a2da7a3 (diff)
downloadnixpkgs-7f61b01600ecbdaa2a57c51097f2ece9e421c4fe.tar
nixpkgs-7f61b01600ecbdaa2a57c51097f2ece9e421c4fe.tar.gz
nixpkgs-7f61b01600ecbdaa2a57c51097f2ece9e421c4fe.tar.bz2
nixpkgs-7f61b01600ecbdaa2a57c51097f2ece9e421c4fe.tar.lz
nixpkgs-7f61b01600ecbdaa2a57c51097f2ece9e421c4fe.tar.xz
nixpkgs-7f61b01600ecbdaa2a57c51097f2ece9e421c4fe.tar.zst
nixpkgs-7f61b01600ecbdaa2a57c51097f2ece9e421c4fe.zip
lib.lists.commonPrefix: init
Diffstat (limited to 'lib/tests/misc.nix')
-rw-r--r--lib/tests/misc.nix33
1 files changed, 33 insertions, 0 deletions
diff --git a/lib/tests/misc.nix b/lib/tests/misc.nix
index dfa6a94ee48..ab97409d856 100644
--- a/lib/tests/misc.nix
+++ b/lib/tests/misc.nix
@@ -488,6 +488,39 @@ runTests {
     expected = { a = [ 2 3 ]; b = [7]; c = [8];};
   };
 
+  testListCommonPrefixExample1 = {
+    expr = lists.commonPrefix [ 1 2 3 4 5 6 ] [ 1 2 4 8 ];
+    expected = [ 1 2 ];
+  };
+  testListCommonPrefixExample2 = {
+    expr = lists.commonPrefix [ 1 2 3 ] [ 1 2 3 4 5 ];
+    expected = [ 1 2 3 ];
+  };
+  testListCommonPrefixExample3 = {
+    expr = lists.commonPrefix [ 1 2 3 ] [ 4 5 6 ];
+    expected = [ ];
+  };
+  testListCommonPrefixEmpty = {
+    expr = lists.commonPrefix [ ] [ 1 2 3 ];
+    expected = [ ];
+  };
+  testListCommonPrefixSame = {
+    expr = lists.commonPrefix [ 1 2 3 ] [ 1 2 3 ];
+    expected = [ 1 2 3 ];
+  };
+  testListCommonPrefixLazy = {
+    expr = lists.commonPrefix [ 1 ] [ 1 (abort "lib.lists.commonPrefix shouldn't evaluate this")];
+    expected = [ 1 ];
+  };
+  # This would stack overflow if `commonPrefix` were implemented using recursion
+  testListCommonPrefixLong =
+    let
+      longList = genList (n: n) 100000;
+    in {
+      expr = lists.commonPrefix longList longList;
+      expected = longList;
+    };
+
   testSort = {
     expr = sort builtins.lessThan [ 40 2 30 42 ];
     expected = [2 30 40 42];