Merge from origin/emacs-25
[emacs.git] / test / lisp / sort-tests.el
blobf3a182cdc14036ddbaadea9987b1fd57e8c289f8
1 ;;; sort-tests.el --- Tests for sort.el -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2015-2016 Free Software Foundation, Inc.
5 ;; Author: Artur Malabarba <bruce.connor.am@gmail.com>
7 ;; This program is free software; you can redistribute it and/or modify
8 ;; it under the terms of the GNU General Public License as published by
9 ;; the Free Software Foundation, either version 3 of the License, or
10 ;; (at your option) any later version.
12 ;; This program is distributed in the hope that it will be useful,
13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;; GNU General Public License for more details.
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
20 ;;; Code:
22 (require 'ert)
23 (require 'sort)
25 (defun sort-tests-random-word (n)
26 (mapconcat (lambda (_) (string (let ((c (random 52)))
27 (+ (if (> c 25) 71 65)
28 c))))
29 (make-list n nil) ""))
31 (defun sort-tests--insert-words-sort-and-compare (words separator function reverse less-predicate)
32 (with-temp-buffer
33 (let ((aux words))
34 (while aux
35 (insert (pop aux))
36 (when aux
37 (insert separator))))
38 ;; Final newline.
39 (insert "\n")
40 (funcall function reverse (point-min) (point-max))
41 (let ((sorted-words
42 (mapconcat #'identity
43 (sort (copy-sequence words)
44 (if reverse
45 (lambda (a b) (funcall less-predicate b a))
46 less-predicate))
47 separator)))
48 (should (string= (substring (buffer-string) 0 -1) sorted-words)))))
50 ;;; This function uses randomly generated tests and should satisfy
51 ;;; most needs for this lib.
52 (cl-defun sort-tests-test-sorter-function (separator function &key generator less-pred noreverse)
53 "Check that FUNCTION correctly sorts words separated by SEPARATOR.
54 This checks whether it is equivalent to sorting a list of such
55 words via LESS-PREDICATE, and then inserting them separated by
56 SEPARATOR.
57 LESS-PREDICATE defaults to `string-lessp'.
58 GENERATOR is a function called with one argument that returns a
59 word, it defaults to `sort-tests-random-word'.
60 NOREVERSE means that the first arg of FUNCTION is not used for
61 reversing the sort."
62 (dotimes (n 20)
63 ;; Sort n words of length n.
64 (let ((words (mapcar (or generator #'sort-tests-random-word) (make-list n n)))
65 (sort-fold-case nil)
66 (less-pred (or less-pred #'string<)))
67 (sort-tests--insert-words-sort-and-compare words separator function nil less-pred)
68 (unless noreverse
69 (sort-tests--insert-words-sort-and-compare
70 words separator function 'reverse less-pred))
71 (let ((less-pred-case (lambda (a b) (funcall less-pred (downcase a) (downcase b))))
72 (sort-fold-case t))
73 (sort-tests--insert-words-sort-and-compare words separator function nil less-pred-case)
74 (unless noreverse
75 (sort-tests--insert-words-sort-and-compare
76 words separator function 'reverse less-pred-case))))))
78 (ert-deftest sort-tests--lines ()
79 (sort-tests-test-sorter-function "\n" #'sort-lines))
81 (ert-deftest sort-tests--paragraphs ()
82 (let ((paragraph-separate "[\s\t\f]*$"))
83 (sort-tests-test-sorter-function "\n\n" #'sort-paragraphs)))
85 (ert-deftest sort-tests--numeric-fields ()
86 (cl-labels ((field-to-number (f) (string-to-number (car (split-string f)))))
87 (sort-tests-test-sorter-function "\n" (lambda (_ l r) (sort-numeric-fields 1 l (1- r)))
88 :noreverse t
89 :generator (lambda (_) (format "%s %s" (random) (sort-tests-random-word 20)))
90 :less-pred (lambda (a b) (< (field-to-number a)
91 (field-to-number b))))))
93 (ert-deftest sort-tests--fields-1 ()
94 (cl-labels ((field-n (f n) (elt (split-string f) (1- n))))
95 (sort-tests-test-sorter-function "\n" (lambda (_ l r) (sort-fields 1 l (1- r)))
96 :noreverse t
97 :generator (lambda (n) (concat (sort-tests-random-word n) " " (sort-tests-random-word n)))
98 :less-pred (lambda (a b) (string< (field-n a 1) (field-n b 1))))))
100 (ert-deftest sort-tests--fields-2 ()
101 (cl-labels ((field-n (f n) (elt (split-string f) (1- n))))
102 (sort-tests-test-sorter-function "\n" (lambda (_ l r) (sort-fields 2 l (1- r)))
103 :noreverse t
104 :generator (lambda (n) (concat (sort-tests-random-word n) " " (sort-tests-random-word n)))
105 :less-pred (lambda (a b) (string< (field-n a 2) (field-n b 2))))))
107 (provide 'sort-tests)
108 ;;; sort-tests.el ends here