diff options
| author | Eli Barzilay | 2021-04-10 15:10:35 -0400 |
|---|---|---|
| committer | Mattias EngdegÄrd | 2021-04-11 14:00:09 +0200 |
| commit | 686c366f8a63c448d06e5f08d604374fb316bc57 (patch) | |
| tree | 16cfccd8e4c83ce2265a0b769d2e3f1bbc560dd1 | |
| parent | 01a513bf0beb9478e2ef801ca28ebc992455fe3c (diff) | |
| download | emacs-686c366f8a63c448d06e5f08d604374fb316bc57.tar.gz emacs-686c366f8a63c448d06e5f08d604374fb316bc57.zip | |
Fix calculator-string-to-number yet again (bug#47694)
* lisp/calculator.el (calculator-string-to-number):
The last bugfix changed the code to just blindly replace ".e". This
has some minor problems like making "-." parse as 0.0 instead of -0.0,
and ".1.e1" is parsed as 1 instead of 0.1. Instead, replace the first
"." that is followed by a non-digit with ".0". Since this has had
several problems over the years, add some tests too. (Also, restore
the original if-indentation style.)
| -rw-r--r-- | lisp/calculator.el | 9 | ||||
| -rw-r--r-- | test/lisp/calculator-tests.el | 51 |
2 files changed, 56 insertions, 4 deletions
diff --git a/lisp/calculator.el b/lisp/calculator.el index 6dd8d9a7ec1..99c9b6290c4 100644 --- a/lisp/calculator.el +++ b/lisp/calculator.el | |||
| @@ -836,10 +836,11 @@ The result should not exceed the screen width." | |||
| 836 | "Convert the given STR to a number, according to the value of | 836 | "Convert the given STR to a number, according to the value of |
| 837 | `calculator-input-radix'." | 837 | `calculator-input-radix'." |
| 838 | (if calculator-input-radix | 838 | (if calculator-input-radix |
| 839 | (string-to-number str (cadr (assq calculator-input-radix | 839 | (string-to-number str (cadr (assq calculator-input-radix |
| 840 | '((bin 2) (oct 8) (hex 16))))) | 840 | '((bin 2) (oct 8) (hex 16))))) |
| 841 | ;; Allow entry of "1.e3". | 841 | ;; parse numbers similarly to calculators |
| 842 | (let ((str (replace-regexp-in-string (rx "." (any "eE")) "e" str))) | 842 | ;; (see tests in test/lisp/calculator-tests.el) |
| 843 | (let ((str (replace-regexp-in-string "\\.\\([^0-9].*\\)?$" ".0\\1" str))) | ||
| 843 | (float (string-to-number str))))) | 844 | (float (string-to-number str))))) |
| 844 | 845 | ||
| 845 | (defun calculator-push-curnum () | 846 | (defun calculator-push-curnum () |
diff --git a/test/lisp/calculator-tests.el b/test/lisp/calculator-tests.el new file mode 100644 index 00000000000..9551b1a4c61 --- /dev/null +++ b/test/lisp/calculator-tests.el | |||
| @@ -0,0 +1,51 @@ | |||
| 1 | ;;; calculator-tests.el --- Test suite for calculator. -*- lexical-binding: t -*- | ||
| 2 | |||
| 3 | ;; Copyright (C) 2021 Free Software Foundation, Inc. | ||
| 4 | |||
| 5 | ;; This file is part of GNU Emacs. | ||
| 6 | |||
| 7 | ;; GNU Emacs 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 | ;; GNU Emacs 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 GNU Emacs. If not, see <https://www.gnu.org/licenses/>. | ||
| 19 | |||
| 20 | ;;; Code: | ||
| 21 | (require 'ert) | ||
| 22 | (require 'calculator) | ||
| 23 | |||
| 24 | (ert-deftest calculator-test-calculator-string-to-number () | ||
| 25 | (dolist (x '(("" 0.0) | ||
| 26 | ("+" 0.0) | ||
| 27 | ("-" 0.0) | ||
| 28 | ("." 0.0) | ||
| 29 | ("+." 0.0) | ||
| 30 | ("-." -0.0) | ||
| 31 | (".-" 0.0) | ||
| 32 | ("--." 0.0) | ||
| 33 | ("-0.0e" -0.0) | ||
| 34 | ("1e1" 10.0) | ||
| 35 | ("1e+1" 10.0) | ||
| 36 | ("1e-1" 0.1) | ||
| 37 | ("+1e1" 10.0) | ||
| 38 | ("-1e1" -10.0) | ||
| 39 | ("+1e-1" 0.1) | ||
| 40 | ("-1e-1" -0.1) | ||
| 41 | (".1.e1" 0.1) | ||
| 42 | (".1..e1" 0.1) | ||
| 43 | ("1e+1.1" 10.0) | ||
| 44 | ("-2e-1.1" -0.2))) | ||
| 45 | (pcase x | ||
| 46 | (`(,str ,expected) | ||
| 47 | (let ((calculator-input-radix nil)) | ||
| 48 | (should (equal (calculator-string-to-number str) expected))))))) | ||
| 49 | |||
| 50 | (provide 'calculator-tests) | ||
| 51 | ;; calculator-tests.el ends here | ||