diff options
| author | Artur Malabarba | 2015-10-29 13:21:48 +0000 |
|---|---|---|
| committer | Artur Malabarba | 2015-10-29 13:23:28 +0000 |
| commit | 0ff31454a58d5f8f31744f02f5aafdf5fc220a21 (patch) | |
| tree | 1b1852a686c11d4ddca201139cf937ddf1a0fe81 | |
| parent | 37d169df4433be1869eac9c916e548b9bd66ff04 (diff) | |
| download | emacs-scratch/isearch-show-toggles.tar.gz emacs-scratch/isearch-show-toggles.zip | |
* test/automated/sort-tests.el: New filescratch/isearch-show-toggles
Tests in this file are randomly generated and then tested with
regular, reverse, and case-fold sorting.
| -rw-r--r-- | test/automated/sort-tests.el | 106 |
1 files changed, 106 insertions, 0 deletions
diff --git a/test/automated/sort-tests.el b/test/automated/sort-tests.el new file mode 100644 index 00000000000..22acb83e26a --- /dev/null +++ b/test/automated/sort-tests.el | |||
| @@ -0,0 +1,106 @@ | |||
| 1 | ;;; sort-tests.el --- Tests for sort.el -*- lexical-binding: t; -*- | ||
| 2 | |||
| 3 | ;; Copyright (C) 2015 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 | ||