]> code.delx.au - gnu-emacs/blob - test/automated/sort-tests.el
Update copyright year to 2016
[gnu-emacs] / test / automated / sort-tests.el
1 ;;; sort-tests.el --- Tests for sort.el -*- lexical-binding: t; -*-
2
3 ;; Copyright (C) 2015-2016 Free Software Foundation, Inc.
4
5 ;; Author: Artur Malabarba <bruce.connor.am@gmail.com>
6
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.
11
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.
16
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/>.
19
20 ;;; Code:
21
22 (require 'ert)
23 (require 'sort)
24
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) ""))
30
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 (let ((x (sort (copy-sequence words) less-predicate)))
44 (if reverse (reverse x) x))
45 separator)))
46 (should (string= (substring (buffer-string) 0 -1) sorted-words)))))
47
48 ;;; This function uses randomly generated tests and should satisfy
49 ;;; most needs for this lib.
50 (cl-defun sort-tests-test-sorter-function (separator function &key generator less-pred noreverse)
51 "Check that FUNCTION correctly sorts words separated by SEPARATOR.
52 This checks whether it is equivalent to sorting a list of such
53 words via LESS-PREDICATE, and then inserting them separated by
54 SEPARATOR.
55 LESS-PREDICATE defaults to `string-lessp'.
56 GENERATOR is a function called with one argument that returns a
57 word, it defaults to `sort-tests-random-word'.
58 NOREVERSE means that the first arg of FUNCTION is not used for
59 reversing the sort."
60 (dotimes (n 20)
61 ;; Sort n words of length n.
62 (let ((words (mapcar (or generator #'sort-tests-random-word) (make-list n n)))
63 (sort-fold-case nil)
64 (less-pred (or less-pred #'string<)))
65 (sort-tests--insert-words-sort-and-compare words separator function nil less-pred)
66 (unless noreverse
67 (sort-tests--insert-words-sort-and-compare
68 words separator function 'reverse less-pred))
69 (let ((less-pred-case (lambda (a b) (funcall less-pred (downcase a) (downcase b))))
70 (sort-fold-case t))
71 (sort-tests--insert-words-sort-and-compare words separator function nil less-pred-case)
72 (unless noreverse
73 (sort-tests--insert-words-sort-and-compare
74 words separator function 'reverse less-pred-case))))))
75
76 (ert-deftest sort-tests--lines ()
77 (sort-tests-test-sorter-function "\n" #'sort-lines))
78
79 (ert-deftest sort-tests--paragraphs ()
80 (let ((paragraph-separate "[\s\t\f]*$"))
81 (sort-tests-test-sorter-function "\n\n" #'sort-paragraphs)))
82
83 (ert-deftest sort-tests--numeric-fields ()
84 (cl-labels ((field-to-number (f) (string-to-number (car (split-string f)))))
85 (sort-tests-test-sorter-function "\n" (lambda (_ l r) (sort-numeric-fields 1 l (1- r)))
86 :noreverse t
87 :generator (lambda (_) (format "%s %s" (random) (sort-tests-random-word 20)))
88 :less-pred (lambda (a b) (< (field-to-number a)
89 (field-to-number b))))))
90
91 (ert-deftest sort-tests--fields-1 ()
92 (cl-labels ((field-n (f n) (elt (split-string f) (1- n))))
93 (sort-tests-test-sorter-function "\n" (lambda (_ l r) (sort-fields 1 l (1- r)))
94 :noreverse t
95 :generator (lambda (n) (concat (sort-tests-random-word n) " " (sort-tests-random-word n)))
96 :less-pred (lambda (a b) (string< (field-n a 1) (field-n b 1))))))
97
98 (ert-deftest sort-tests--fields-2 ()
99 (cl-labels ((field-n (f n) (elt (split-string f) (1- n))))
100 (sort-tests-test-sorter-function "\n" (lambda (_ l r) (sort-fields 2 l (1- r)))
101 :noreverse t
102 :generator (lambda (n) (concat (sort-tests-random-word n) " " (sort-tests-random-word n)))
103 :less-pred (lambda (a b) (string< (field-n a 2) (field-n b 2))))))
104
105 (provide 'sort-tests)
106 ;;; sort-tests.el ends here