Gustavo Picon is sharing code with you

Bitbucket is a code hosting site. Unlimited public and private repositories. Free for small teams.

Don't show this again

tabo / numconv

Python library to convert strings to numbers and numbers to strings. Docs: http://docs.tabo.pe/numconv/tip/

Clone this repository (size: 168.1 KB): HTTPS / SSH
hg clone https://bitbucket.org/tabo/numconv
hg clone ssh://hg@bitbucket.org/tabo/numconv

numconv / tests.py

commit
ad57c29904b5
parent
df9072dc040d
branch
default

tagging 2.1.1

1
a035ba7fb21b
# -*- coding: utf-8 -*-
2
a035ba7fb21b
3
a035ba7fb21b
"""Test numconv.py"""
4
a035ba7fb21b
5
6ae83e41ff11
from numconv import NumConv, int2str, str2int, BASE85
6
a035ba7fb21b
import unittest
7
a035ba7fb21b
8
a035ba7fb21b
9
a035ba7fb21b
class BaseconvI2s(unittest.TestCase):
10
a035ba7fb21b
    """tests for int2str()"""
11
a035ba7fb21b
12
a035ba7fb21b
    def test_i2s(self):
13
a035ba7fb21b
        """testing int2str: expected values"""
14
6ae83e41ff11
        self.assertEqual(int2str(3735928559, 16), 'DEADBEEF')
15
6ae83e41ff11
        self.assertEqual(int2str(238327, 62), 'zzz')
16
6ae83e41ff11
        self.assertEqual(int2str(14776335, 62), 'zzzz')
17
6ae83e41ff11
        self.assertEqual(int2str(466, 7), '1234')
18
6ae83e41ff11
        self.assertEqual(int2str(151880, 2), '100101000101001000')
19
434a77c07a95
        self.assertEqual(int2str(2693233728041137, 85), '~123AFz@')
20
6ae83e41ff11
        self.assertEqual(int2str(543543, 40), '8JSN')
21
6ae83e41ff11
        self.assertEqual(int2str(1949459, 61), '8ZtL')
22
6ae83e41ff11
        self.assertEqual(int2str(19284, 2), '100101101010100')
23
6ae83e41ff11
        self.assertEqual(int2str(100, 10, 'abcdefghijklm'), 'baa')
24
6ae83e41ff11
        self.assertEqual(int2str(37, 4, 'rofl'), 'foo')
25
a035ba7fb21b
26
a035ba7fb21b
    def test_i2s_nonint_number(self):
27
a035ba7fb21b
        """testing int2str: error on non-integer number"""
28
6ae83e41ff11
        self.assertRaises(TypeError, int2str, 0.1, 8)
29
a035ba7fb21b
30
a035ba7fb21b
    def test_i2s_nonint_radix(self):
31
a035ba7fb21b
        """testing int2str: error on non-integer radix"""
32
6ae83e41ff11
        self.assertRaises(TypeError, int2str, 100, 0.1)
33
a035ba7fb21b
34
a035ba7fb21b
    def test_i2s_invalid_radix(self):
35
a035ba7fb21b
        """testing int2str: error on invalid radix"""
36
6ae83e41ff11
        self.assertRaises(ValueError, int2str, 100, -10)
37
6ae83e41ff11
        self.assertRaises(ValueError, int2str, 100, 10, 'abcde')
38
a035ba7fb21b
39
a035ba7fb21b
    def test_i2s_negative_number(self):
40
a035ba7fb21b
        """testing int2str: error on negative number"""
41
6ae83e41ff11
        self.assertRaises(ValueError, int2str, -100, 10)
42
a035ba7fb21b
43
a035ba7fb21b
44
a035ba7fb21b
class BaseconvS2i(unittest.TestCase):
45
a035ba7fb21b
    """tests for str2int()"""
46
a035ba7fb21b
47
a035ba7fb21b
    def test_s2i(self):
48
a035ba7fb21b
        """testing str2int: expected values"""
49
434a77c07a95
        self.assertEqual(str2int('DEADBEEF', 16), 3735928559)
50
6ae83e41ff11
        self.assertEqual(str2int('zzz', 62), 238327)
51
6ae83e41ff11
        self.assertEqual(str2int('zzzz', 62), 14776335)
52
6ae83e41ff11
        self.assertEqual(str2int('1234', 7), 466)
53
6ae83e41ff11
        self.assertEqual(str2int('100101000101001000', 2), 151880)
54
434a77c07a95
        self.assertEqual(str2int('~123AFz@', 85), 2693233728041137)
55
6ae83e41ff11
        self.assertEqual(str2int('8JSN', 40), 543543)
56
6ae83e41ff11
        self.assertEqual(str2int('8ZtL', 61), 1949459)
57
6ae83e41ff11
        self.assertEqual(str2int('100101101010100', 2), 19284)
58
6ae83e41ff11
        self.assertEqual(str2int('baa', 10, 'abcdefghijklm'), 100)
59
6ae83e41ff11
        self.assertEqual(str2int('foo', 4, 'rofl'), 37)
60
a035ba7fb21b
61
a035ba7fb21b
    def test_s2i_nonint_base(self):
62
a035ba7fb21b
        """testing str2int: error on non-integer base"""
63
6ae83e41ff11
        self.assertRaises(TypeError, str2int, '100', 0.1)
64
a035ba7fb21b
65
a035ba7fb21b
    def test_s2i_bad_number(self):
66
a035ba7fb21b
        """testing str2int: error on invalid number"""
67
a035ba7fb21b
        # raised by python
68
6ae83e41ff11
        self.assertRaises(ValueError, str2int, '1234z', 8)
69
a035ba7fb21b
        # raised by numconv.py
70
6ae83e41ff11
        self.assertRaises(ValueError, str2int, '1234z', 37)
71
a035ba7fb21b
72
a035ba7fb21b
    def test_s2i_invalid_radix(self):
73
a035ba7fb21b
        """testing str2int: error on invalid radix"""
74
6ae83e41ff11
        self.assertRaises(ValueError, str2int, 'abcd', -10)
75
a035ba7fb21b
76
a035ba7fb21b
77
bbef845a6732
class BaseconvCmap(unittest.TestCase):
78
a035ba7fb21b
    """tests for getcmap()"""
79
a035ba7fb21b
80
6ae83e41ff11
    def test_get_cmap(self):
81
6ae83e41ff11
        """testing get_cmap: expected values"""
82
6ae83e41ff11
        self.assertEqual(NumConv(alphabet='0123456789').cached_map,
83
a035ba7fb21b
            {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4,
84
a035ba7fb21b
             '5': 5, '6': 6, '7': 7, '8': 8, '9': 9})
85
6ae83e41ff11
        self.assertEqual(NumConv(radix=4, alphabet='abcd').cached_map,
86
a035ba7fb21b
            {'a': 0, 'b': 1, 'c': 2, 'd': 3})
87
a035ba7fb21b
88
a035ba7fb21b
    def test_getcmap_dupechars(self):
89
a035ba7fb21b
        """testing getcmap: error on alphabet with duplicate chars"""
90
6ae83e41ff11
        self.assertRaises(ValueError, NumConv, 6, 'abcdaf')
91
bbef845a6732
92
a035ba7fb21b
93
a035ba7fb21b
class BaseconvSanity(unittest.TestCase):
94
a035ba7fb21b
    """sanity checks"""
95
a035ba7fb21b
96
a035ba7fb21b
    def test_sanity(self):
97
a035ba7fb21b
        """sanity check: testing a large interval and lots of radixes"""
98
6ae83e41ff11
        for radix in range(2, len(BASE85)):
99
6ae83e41ff11
            ncobj = NumConv(radix)
100
434a77c07a95
            for num in list(range(1000)) + [10 ** x for x in range(5, 15)]:
101
6ae83e41ff11
                self.assertEqual(num, ncobj.str2int(ncobj.int2str(num)))
102
a035ba7fb21b
103
a035ba7fb21b
if __name__ == "__main__":
104
a035ba7fb21b
    unittest.main()