hardcoded python path in garchive and tragesym
[geda-gaf.git] / xorn / tests / python / fixednum.py
blobddba6d557cd1080a3618bf453d222c047eeefefd
1 # Copyright (C) 2013-2020 Roland Lutz
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version.
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software Foundation,
15 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 import xorn.fixednum
19 def throws(fun, *args):
20 try:
21 fun(*args)
22 except Exception as e:
23 return type(e)
25 assert throws(xorn.fixednum.format, 42., 0) == TypeError
26 assert throws(xorn.fixednum.format, 42, None) == TypeError
27 assert throws(xorn.fixednum.format, 42, -1) == ValueError
29 assert throws(xorn.fixednum.parse, 42, 0) == TypeError
30 assert throws(xorn.fixednum.parse, 42., 0) == TypeError
31 assert throws(xorn.fixednum.parse, '42', None) == TypeError
32 assert throws(xorn.fixednum.parse, '42', -1) == ValueError
34 assert throws(xorn.fixednum.parse, '', 0) == ValueError
35 assert throws(xorn.fixednum.parse, '.42', 0) == ValueError
36 assert throws(xorn.fixednum.parse, '4.2', 0) == ValueError
37 assert throws(xorn.fixednum.parse, ':42', 0) == ValueError
38 assert throws(xorn.fixednum.parse, 'x', 0) == ValueError
40 assert throws(xorn.fixednum.parse, '', 3) == ValueError
41 assert throws(xorn.fixednum.parse, '.4242', 3) == ValueError
42 assert throws(xorn.fixednum.parse, ':42', 3) == ValueError
43 assert throws(xorn.fixednum.parse, 'x', 3) == ValueError
44 assert throws(xorn.fixednum.parse, '.x', 3) == ValueError
46 assert xorn.fixednum.format(0, 0) == '0'
47 assert xorn.fixednum.format(0, 3) == '0'
48 assert str(xorn.fixednum.parse('0', 0)) == '0'
49 assert str(xorn.fixednum.parse('0', 3)) == '0'
50 assert str(xorn.fixednum.parse('-0', 0)) == '0'
51 assert str(xorn.fixednum.parse('-0', 3)) == '0'
53 for x, decimal_digits, s in [
54 (42, 0, '42'),
55 (42, 1, '4.2'),
56 (42, 2, '.42'),
57 (42, 3, '.042'),
58 (42, 4, '.0042'),
59 (42, 5, '.00042'),
61 (-42, 0, '-42'),
62 (-42, 1, '-4.2'),
63 (-42, 2, '-.42'),
65 (12345, 0, '12345'),
66 (12345, 1, '1234.5'),
67 (12345, 2, '123.45'),
68 (12345, 3, '12.345'),
69 (12345, 4, '1.2345'),
70 (12345, 5, '.12345'),
72 (-12345, 0, '-12345'),
73 (-12345, 1, '-1234.5'),
74 (-12345, 2, '-123.45'),
75 (-12345, 3, '-12.345'),
76 (-12345, 4, '-1.2345'),
77 (-12345, 5, '-.12345'),
79 (-42000, 3, '-42'),
80 (-42090, 3, '-42.09'),
81 (-90, 3, '-.09')]:
82 assert xorn.fixednum.format(x, decimal_digits) == s
83 assert xorn.fixednum.parse(s, decimal_digits) == x