summaryrefslogtreecommitdiff
path: root/numpy/core/tests/test_scalar_ctors.py
blob: e2470779b22d10cae1d0800900993020c4260358 (plain)
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
"""
Test the scalar constructors, which also do type-coercion
"""
from __future__ import division, absolute_import, print_function

import sys
import platform
import numpy as np

from numpy.testing import (
    run_module_suite,
    assert_equal, assert_almost_equal, assert_raises, assert_warns,
    dec
)

class TestFromString(object):
    def test_floating(self):
        # Ticket #640, floats from string
        fsingle = np.single('1.234')
        fdouble = np.double('1.234')
        flongdouble = np.longdouble('1.234')
        assert_almost_equal(fsingle, 1.234)
        assert_almost_equal(fdouble, 1.234)
        assert_almost_equal(flongdouble, 1.234)

    def test_floating_overflow(self):
        """ Strings containing an unrepresentable float overflow """
        fhalf = np.half('1e10000')
        assert_equal(fhalf, np.inf)
        fsingle = np.single('1e10000')
        assert_equal(fsingle, np.inf)
        fdouble = np.double('1e10000')
        assert_equal(fdouble, np.inf)
        flongdouble = assert_warns(RuntimeWarning, np.longdouble, '1e10000')
        assert_equal(flongdouble, np.inf)

        fhalf = np.half('-1e10000')
        assert_equal(fhalf, -np.inf)
        fsingle = np.single('-1e10000')
        assert_equal(fsingle, -np.inf)
        fdouble = np.double('-1e10000')
        assert_equal(fdouble, -np.inf)
        flongdouble = assert_warns(RuntimeWarning, np.longdouble, '-1e10000')
        assert_equal(flongdouble, -np.inf)

    @dec.knownfailureif((sys.version_info[0] >= 3) or
                        (sys.platform == "win32" and
                         platform.architecture()[0] == "64bit"),
                        "numpy.intp('0xff', 16) not supported on Py3, "
                        "as it does not inherit from Python int")
    def test_intp(self):
        # Ticket #99
        i_width = np.int_(0).nbytes*2 - 1
        np.intp('0x' + 'f'*i_width, 16)
        assert_raises(OverflowError, np.intp, '0x' + 'f'*(i_width+1), 16)
        assert_raises(ValueError, np.intp, '0x1', 32)
        assert_equal(255, np.intp('0xFF', 16))


class TestFromInt(object):
    def test_intp(self):
        # Ticket #99
        assert_equal(1024, np.intp(1024))

    def test_uint64_from_negative(self):
        assert_equal(np.uint64(-2), np.uint64(18446744073709551614))


if __name__ == "__main__":
    run_module_suite()