[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Emacs-diffs] scratch/isearch-show-toggles 0ff3145: * test/automated/sor
From: |
Artur Malabarba |
Subject: |
[Emacs-diffs] scratch/isearch-show-toggles 0ff3145: * test/automated/sort-tests.el: New file |
Date: |
Thu, 29 Oct 2015 12:24:09 +0000 |
branch: scratch/isearch-show-toggles
commit 0ff31454a58d5f8f31744f02f5aafdf5fc220a21
Author: Artur Malabarba <address@hidden>
Commit: Artur Malabarba <address@hidden>
* test/automated/sort-tests.el: New file
Tests in this file are randomly generated and then tested with
regular, reverse, and case-fold sorting.
---
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 0000000..22acb83
--- /dev/null
+++ b/test/automated/sort-tests.el
@@ -0,0 +1,106 @@
+;;; sort-tests.el --- Tests for sort.el -*- lexical-binding: t;
-*-
+
+;; Copyright (C) 2015 Free Software Foundation, Inc.
+
+;; Author: Artur Malabarba <address@hidden>
+
+;; This program is free software; you can redistribute it and/or modify
+;; it under the terms of the GNU General Public License as published by
+;; the Free Software Foundation, either version 3 of the License, or
+;; (at your option) any later version.
+
+;; This program is distributed in the hope that it will be useful,
+;; but WITHOUT ANY WARRANTY; without even the implied warranty of
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+;; GNU General Public License for more details.
+
+;; You should have received a copy of the GNU General Public License
+;; along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+;;; Code:
+
+(require 'ert)
+(require 'sort)
+
+(defun sort-tests-random-word (n)
+ (mapconcat (lambda (_) (string (let ((c (random 52)))
+ (+ (if (> c 25) 71 65)
+ c))))
+ (make-list n nil) ""))
+
+(defun sort-tests--insert-words-sort-and-compare (words separator function
reverse less-predicate)
+ (with-temp-buffer
+ (let ((aux words))
+ (while aux
+ (insert (pop aux))
+ (when aux
+ (insert separator))))
+ ;; Final newline.
+ (insert "\n")
+ (funcall function reverse (point-min) (point-max))
+ (let ((sorted-words
+ (mapconcat #'identity
+ (let ((x (sort (copy-sequence words) less-predicate)))
+ (if reverse (reverse x) x))
+ separator)))
+ (should (string= (substring (buffer-string) 0 -1) sorted-words)))))
+
+;;; This function uses randomly generated tests and should satisfy
+;;; most needs for this lib.
+(cl-defun sort-tests-test-sorter-function (separator function &key generator
less-pred noreverse)
+ "Check that FUNCTION correctly sorts words separated by SEPARATOR.
+This checks whether it is equivalent to sorting a list of such
+words via LESS-PREDICATE, and then inserting them separated by
+SEPARATOR.
+LESS-PREDICATE defaults to `string-lessp'.
+GENERATOR is a function called with one argument that returns a
+word, it defaults to `sort-tests-random-word'.
+NOREVERSE means that the first arg of FUNCTION is not used for
+reversing the sort."
+ (dotimes (n 20)
+ ;; Sort n words of length n.
+ (let ((words (mapcar (or generator #'sort-tests-random-word) (make-list n
n)))
+ (sort-fold-case nil)
+ (less-pred (or less-pred #'string<)))
+ (sort-tests--insert-words-sort-and-compare words separator function nil
less-pred)
+ (unless noreverse
+ (sort-tests--insert-words-sort-and-compare
+ words separator function 'reverse less-pred))
+ (let ((less-pred-case (lambda (a b) (funcall less-pred (downcase a)
(downcase b))))
+ (sort-fold-case t))
+ (sort-tests--insert-words-sort-and-compare words separator function
nil less-pred-case)
+ (unless noreverse
+ (sort-tests--insert-words-sort-and-compare
+ words separator function 'reverse less-pred-case))))))
+
+(ert-deftest sort-tests--lines ()
+ (sort-tests-test-sorter-function "\n" #'sort-lines))
+
+(ert-deftest sort-tests--paragraphs ()
+ (let ((paragraph-separate "[\s\t\f]*$"))
+ (sort-tests-test-sorter-function "\n\n" #'sort-paragraphs)))
+
+(ert-deftest sort-tests--numeric-fields ()
+ (cl-labels ((field-to-number (f) (string-to-number (car (split-string f)))))
+ (sort-tests-test-sorter-function "\n" (lambda (_ l r) (sort-numeric-fields
1 l (1- r)))
+ :noreverse t
+ :generator (lambda (_) (format "%s %s" (random)
(sort-tests-random-word 20)))
+ :less-pred (lambda (a b) (< (field-to-number a)
+ (field-to-number b))))))
+
+(ert-deftest sort-tests--fields-1 ()
+ (cl-labels ((field-n (f n) (elt (split-string f) (1- n))))
+ (sort-tests-test-sorter-function "\n" (lambda (_ l r) (sort-fields 1 l (1-
r)))
+ :noreverse t
+ :generator (lambda (n) (concat
(sort-tests-random-word n) " " (sort-tests-random-word n)))
+ :less-pred (lambda (a b) (string< (field-n a 1)
(field-n b 1))))))
+
+(ert-deftest sort-tests--fields-2 ()
+ (cl-labels ((field-n (f n) (elt (split-string f) (1- n))))
+ (sort-tests-test-sorter-function "\n" (lambda (_ l r) (sort-fields 2 l (1-
r)))
+ :noreverse t
+ :generator (lambda (n) (concat
(sort-tests-random-word n) " " (sort-tests-random-word n)))
+ :less-pred (lambda (a b) (string< (field-n a 2)
(field-n b 2))))))
+
+(provide 'sort-tests)
+;;; sort-tests.el ends here
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [Emacs-diffs] scratch/isearch-show-toggles 0ff3145: * test/automated/sort-tests.el: New file,
Artur Malabarba <=