1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
| | ;;; num-base-converters-tests.el --- Test suite for num-base-converters. -*- lexical-binding: t -*-
;; Copyright (C) 2017 Free Software Foundation, Inc.
;; This file is part of GNU Emacs.
;; GNU Emacs 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.
;; GNU Emacs 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 GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
;;; Code:
(require 'ert)
(require 'num-base-converters)
(ert-deftest nbc-test-converters ()
;; Input is case-insensitive.
(dolist (x (list "a" "A"))
(should (equal "10" (nbc-hex2dec x)))
(should (equal "12" (nbc-hex2oct x)))
(should (equal "1010" (nbc-hex2bin x))))
;;
(should (equal "A" (nbc-dec2hex "10")))
(should (equal "12" (nbc-dec2oct "10")))
(should (equal "1010" (nbc-dec2bin "10")))
;;
(should (equal "3F" (nbc-oct2hex "77")))
(should (equal "63" (nbc-oct2dec "77")))
(should (equal "111111" (nbc-oct2bin "77")))
;;
(should (equal "A" (nbc-bin2hex "1010")))
(should (equal "10" (nbc-bin2dec "1010")))
(should (equal "12" (nbc-bin2oct "1010"))))
(ert-deftest nbc-test-nbc-number-base-converter ()
;; Bases `b' must be: 2 <= b <= 36:
(should-error (nbc-number-base-converter "10" 37 10))
(should-error (nbc-number-base-converter "10" 10 37))
(should-error (nbc-number-base-converter "10" 10 1))
(should-error (nbc-number-base-converter "10" 1 10))
;; Invalid input:
(should-error (nbc-number-base-converter "1a" 8 10))
(should-error (nbc-number-base-converter "az" 16 10))
(should-error (nbc-number-base-converter "3" 2 10))
(should-error (nbc-number-base-converter "z" 10 8))
;;
(should (equal "8" (nbc-number-base-converter "10" 8 16)))
(should (equal "10" (nbc-number-base-converter "8" 16 8)))
(should (equal "12" (nbc-number-base-converter "10" 10 8)))
(should (equal "10" (nbc-number-base-converter "12" 8 10)))
(should (equal "1000" (nbc-number-base-converter "10" 8 2)))
(should (equal "10" (nbc-number-base-converter "1000" 2 8)))
;;
(should (equal "10" (nbc-number-base-converter "a" 16 10)))
(should (equal "10" (nbc-number-base-converter "A" 16 10)))
(should (equal "A" (nbc-number-base-converter "10" 10 16)))
(should (equal "1010" (nbc-number-base-converter "a" 16 2)))
(should (equal "1010" (nbc-number-base-converter "A" 16 2)))
(should (equal "A" (nbc-number-base-converter "1010" 2 16)))
;;
(should (equal "1010" (nbc-number-base-converter "10" 10 2)))
(should (equal "10" (nbc-number-base-converter "1010" 2 10))))
(ert-deftest nbc-test-nbc-number-base-converter ()
(let (nbc-define-aliases)
(nbc--create-converters)
(should (fboundp 'nbc-oct2dec))
(should-not (fboundp 'oct2dec))
(setq nbc-define-aliases t)
;; It should create the aliases, i.e. `oct2dec' etc.
(nbc--create-converters)
(should (fboundp 'oct2dec))))
(provide 'num-base-converters-tests)
;; num-base-converters-tests.el ends here
|