Rework quoting in tutorial
[emacs.git] / test / automated / calc-tests.el
blobd5252ea62a919b7a4194d58a9113d8750db246bd
1 ;;; calc-tests.el --- tests for calc -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2014-2015 Free Software Foundation, Inc.
5 ;; Author: Leo Liu <sdl.web@gmail.com>
6 ;; Keywords: maint
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23 ;;; Commentary:
25 ;;; Code:
27 (require 'cl-lib)
28 (require 'ert)
29 (require 'calc)
30 (require 'calc-ext)
31 (require 'calc-units)
33 ;; XXX The order in which calc libraries (in particular calc-units)
34 ;; are loaded influences whether a calc integer in an expression
35 ;; involving units is represented as a lisp integer or a calc float,
36 ;; see bug#19582. Until this will be fixed the following function can
37 ;; be used to compare such calc expressions.
38 (defun calc-tests-equal (a b)
39 "Like `equal' but allow for different representations of numbers.
40 For example: (calc-tests-equal 10 '(float 1 1)) => t.
41 A and B should be calc expressions."
42 (cond ((math-numberp a)
43 (and (math-numberp b)
44 (math-equal a b)))
45 ((atom a)
46 (equal a b))
47 ((consp b)
48 ;; Can't be dotted or circular.
49 (and (= (length a) (length b))
50 (equal (car a) (car b))
51 (cl-every #'calc-tests-equal (cdr a) (cdr b))))))
53 (defun calc-tests-simple (fun string &rest args)
54 "Push STRING on the calc stack, then call FUN and return the new top.
55 The result is a calc (i.e., lisp) expression, not its string representation.
56 Also pop the entire stack afterwards.
57 An existing calc stack is reused, otherwise a new one is created."
58 (calc-eval string 'push)
59 (prog1
60 (ignore-errors
61 (apply fun args)
62 (calc-top-n 1))
63 (calc-pop 0)))
65 (ert-deftest test-math-bignum ()
66 ;; bug#17556
67 (let ((n (math-bignum most-negative-fixnum)))
68 (should (math-negp n))
69 (should (cl-notany #'cl-minusp (cdr n)))))
71 (ert-deftest test-calc-remove-units ()
72 (should (calc-tests-equal (calc-tests-simple #'calc-remove-units "-1 m") -1)))
74 (ert-deftest test-calc-extract-units ()
75 (should (calc-tests-equal (calc-tests-simple #'calc-extract-units "-1 m")
76 '(var m var-m)))
77 (should (calc-tests-equal (calc-tests-simple #'calc-extract-units "-1 m*cm")
78 '(* (float 1 -2) (^ (var m var-m) 2)))))
80 (ert-deftest test-calc-convert-units ()
81 ;; Used to ask for `(The expression is unitless when simplified) Old Units: '.
82 (should (calc-tests-equal (calc-tests-simple #'calc-convert-units "-1 m" nil "cm")
83 '(* -100 (var cm var-cm))))
84 ;; Gave wrong result.
85 (should (calc-tests-equal (calc-tests-simple #'calc-convert-units "-1 m"
86 (math-read-expr "1m") "cm")
87 '(* -100 (var cm var-cm)))))
89 (provide 'calc-tests)
90 ;;; calc-tests.el ends here
92 ;; Local Variables:
93 ;; bug-reference-url-format: "http://debbugs.gnu.org/%s"
94 ;; End: