test_double.py 2.62 KB
Newer Older
John Zhang's avatar
John Zhang committed
1
from util import fncptr_from_c_script, may_spawn_proc
2
import ctypes
3
import pytest
4
5
6
7
8

def within_err(res, exp, err=1e15):
    return abs(res - exp) < err


John Zhang's avatar
John Zhang committed
9
@may_spawn_proc
10
def test_double_add():
John Zhang's avatar
John Zhang committed
11
    fnp, _ = fncptr_from_c_script("test_double_add.c", "test_fnc", restype=ctypes.c_double)
12
13
    assert fnp() == 5.859873

John Zhang's avatar
John Zhang committed
14
@may_spawn_proc
15
def test_double_sub():
John Zhang's avatar
John Zhang committed
16
    fnp, _ = fncptr_from_c_script("test_double_sub.c", "test_fnc", restype=ctypes.c_double)
17
18
    assert within_err(fnp(), 0.423313)

John Zhang's avatar
John Zhang committed
19
@may_spawn_proc
20
def test_double_mul():
John Zhang's avatar
John Zhang committed
21
    fnp, _ = fncptr_from_c_script("test_double_mul.c", "test_fnc", restype=ctypes.c_double)
22
23
    assert fnp() == 8.53972942004

John Zhang's avatar
John Zhang committed
24
@may_spawn_proc
25
def test_double_div():
John Zhang's avatar
John Zhang committed
26
    fnp, _ = fncptr_from_c_script("test_double_div.c", "test_fnc", restype=ctypes.c_double)
John Zhang's avatar
John Zhang committed
27
    assert within_err(fnp(), 1.1557282546316052)
28

John Zhang's avatar
John Zhang committed
29
@may_spawn_proc
30
def test_double_ordered_eq():
John Zhang's avatar
John Zhang committed
31
    fnp, _ = fncptr_from_c_script("test_double_ordered_eq.c", "test_fnc")
32
33
    assert fnp() == 0

John Zhang's avatar
John Zhang committed
34
@may_spawn_proc
35
def test_double_ordered_ne():
John Zhang's avatar
John Zhang committed
36
    fnp, _ = fncptr_from_c_script("test_double_ordered_ne.c", "test_fnc")
37
38
    assert fnp() == 1

John Zhang's avatar
John Zhang committed
39
@may_spawn_proc
40
def test_double_ordered_lt():
John Zhang's avatar
John Zhang committed
41
    fnp, _ = fncptr_from_c_script("test_double_ordered_lt.c", "test_fnc")
42
43
    assert fnp() == 1

John Zhang's avatar
John Zhang committed
44
@may_spawn_proc
45
def test_double_ordered_le():
John Zhang's avatar
John Zhang committed
46
    fnp, _ = fncptr_from_c_script("test_double_ordered_le.c", "test_fnc")
47
48
    assert fnp() == 1

John Zhang's avatar
John Zhang committed
49
@may_spawn_proc
50
def test_double_ordered_ge():
John Zhang's avatar
John Zhang committed
51
    fnp, _ = fncptr_from_c_script("test_double_ordered_ge.c", "test_fnc")
52
53
    assert fnp() == 1

John Zhang's avatar
John Zhang committed
54
@may_spawn_proc
55
def test_double_ordered_gt():
John Zhang's avatar
John Zhang committed
56
    fnp, _ = fncptr_from_c_script("test_double_ordered_gt.c", "test_fnc")
57
    assert fnp() == 1
58

John Zhang's avatar
John Zhang committed
59
@may_spawn_proc
60
def test_double_arg_pass():
John Zhang's avatar
John Zhang committed
61
    fnp, _ = fncptr_from_c_script("test_double_arg_pass.c", "test_fnc",
62
63
                               [ctypes.c_double, ctypes.c_double], ctypes.c_double)
    assert fnp(3.141593, 2.71828) == 5.859873
64

John Zhang's avatar
John Zhang committed
65
@may_spawn_proc
66
67
68
69
70
def test_double_sitofp():
    fnp, _ = fncptr_from_c_script("test_double_sitofp.c", "test_fnc", restype=ctypes.c_double)
    assert fnp() == -42.0


71
@pytest.mark.xfail(reason='not implemented yet')
John Zhang's avatar
John Zhang committed
72
@may_spawn_proc
73
74
75
76
def test_double_uitofp():
    fnp, _ = fncptr_from_c_script("test_double_uitofp.c", "test_fnc", restype=ctypes.c_double)
    assert fnp() == 42.0

John Zhang's avatar
John Zhang committed
77
@may_spawn_proc
78
79
80
81
82
def test_double_fptosi():
    fnp, _ = fncptr_from_c_script("test_double_fptosi.c", "test_fnc", restype=ctypes.c_int64)
    assert fnp() == -3


83
@pytest.mark.xfail(reason='not implemented yet')
John Zhang's avatar
John Zhang committed
84
@may_spawn_proc
85
86
87
88
def test_double_fptoui():
    fnp, _ = fncptr_from_c_script("test_double_fptoui.c", "test_fnc", restype=ctypes.c_uint64)
    assert fnp() == 3