diff options
| author | Mattias EngdegÄrd | 2024-03-19 13:03:47 +0100 |
|---|---|---|
| committer | Mattias EngdegÄrd | 2024-03-29 11:39:38 +0100 |
| commit | ae5f2c02bd2fc269e2cc32c8039d95fbf4225e69 (patch) | |
| tree | a4c4b2d9cb7288524b7946e0f3263dca4357fd9c /test/src | |
| parent | a52f1121a3589af8f89828e04d66f1215c361bcf (diff) | |
| download | emacs-ae5f2c02bd2fc269e2cc32c8039d95fbf4225e69.tar.gz emacs-ae5f2c02bd2fc269e2cc32c8039d95fbf4225e69.zip | |
New `sort` keyword arguments (bug#69709)
Add the :key, :lessp, :reverse and :in-place keyword arguments.
The old calling style remains available and is unchanged.
* src/fns.c (sort_list, sort_vector, Fsort):
* src/sort.c (tim_sort):
Add keyword arguments with associated new features.
All callers of Fsort adapted.
* test/src/fns-tests.el (fns-tests--shuffle-vector, fns-tests-sort-kw):
New test.
* doc/lispref/sequences.texi (Sequence Functions): Update manual.
* etc/NEWS: Announce.
Diffstat (limited to 'test/src')
| -rw-r--r-- | test/src/fns-tests.el | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/test/src/fns-tests.el b/test/src/fns-tests.el index 844000cdc76..1b13785a9fc 100644 --- a/test/src/fns-tests.el +++ b/test/src/fns-tests.el | |||
| @@ -375,6 +375,49 @@ | |||
| 375 | (should (equal (should-error (sort "cba" #'<) :type 'wrong-type-argument) | 375 | (should (equal (should-error (sort "cba" #'<) :type 'wrong-type-argument) |
| 376 | '(wrong-type-argument list-or-vector-p "cba")))) | 376 | '(wrong-type-argument list-or-vector-p "cba")))) |
| 377 | 377 | ||
| 378 | (defun fns-tests--shuffle-vector (vect) | ||
| 379 | "Shuffle VECT in place." | ||
| 380 | (let ((n (length vect))) | ||
| 381 | (dotimes (i (1- n)) | ||
| 382 | (let* ((j (+ i (random (- n i)))) | ||
| 383 | (vi (aref vect i))) | ||
| 384 | (aset vect i (aref vect j)) | ||
| 385 | (aset vect j vi))))) | ||
| 386 | |||
| 387 | (ert-deftest fns-tests-sort-kw () | ||
| 388 | ;; Test the `sort' keyword calling convention by comparing with | ||
| 389 | ;; the results from using the old (positional) style tested above. | ||
| 390 | (random "my seed") | ||
| 391 | (dolist (size '(0 1 2 3 10 100 1000)) | ||
| 392 | ;; Use a vector with both positive and negative numbers (asymmetric). | ||
| 393 | (let ((numbers (vconcat | ||
| 394 | (number-sequence (- (/ size 3)) (- size 1 (/ size 3)))))) | ||
| 395 | (fns-tests--shuffle-vector numbers) | ||
| 396 | ;; Test both list and vector input. | ||
| 397 | (dolist (input (list (append numbers nil) numbers)) | ||
| 398 | (dolist (in-place '(nil t)) | ||
| 399 | (dolist (reverse '(nil t)) | ||
| 400 | (dolist (key '(nil abs)) | ||
| 401 | (dolist (lessp '(nil >)) | ||
| 402 | (let* ((seq (copy-sequence input)) | ||
| 403 | (res (sort seq :key key :lessp lessp | ||
| 404 | :in-place in-place :reverse reverse)) | ||
| 405 | (pred (or lessp #'value<)) | ||
| 406 | (exp-in (copy-sequence input)) | ||
| 407 | (exp-out | ||
| 408 | (sort (if reverse (reverse exp-in) exp-in) | ||
| 409 | (if key | ||
| 410 | (lambda (a b) | ||
| 411 | (funcall pred | ||
| 412 | (funcall key a) (funcall key b))) | ||
| 413 | pred))) | ||
| 414 | (expected (if reverse (reverse exp-out) exp-out))) | ||
| 415 | (should (equal res expected)) | ||
| 416 | (if in-place | ||
| 417 | (should (eq res seq)) | ||
| 418 | (should-not (and (> size 0) (eq res seq))) | ||
| 419 | (should (equal seq input)))))))))))) | ||
| 420 | |||
| 378 | (defvar w32-collate-ignore-punctuation) | 421 | (defvar w32-collate-ignore-punctuation) |
| 379 | 422 | ||
| 380 | (ert-deftest fns-tests-collate-sort () | 423 | (ert-deftest fns-tests-collate-sort () |