示例#1
0
def test_cpp():
    assert 3 == c.add(1, 2)
    assert -1002 == c.add(-1000, -2)
    assert abs(10.0 - c.subtract(20.0, 10.0)) < eps
    assert abs(-10.0 - c.subtract(90.0, 100.0)) < eps
    assert abs(0.0 - c.add_or_subtract(0.1234, -0.1234, True)) < eps
    assert abs(100.0 - c.add_or_subtract(123.456, 23.456, False)) < eps

    a = 3.1415926
    values = [float(x) for x in range(100)]
    c_values = c.scalar_multiply(a, values)
    for i in range(len(values)):
        assert abs(a * values[i] - c_values[i]) < eps

    assert '0987654321' == c.reverse_string_v1('1234567890')
    assert '[abc x|z j 1 *&' == c.reverse_string_v2('&* 1 j z|x cba[')

    if loadlib.IS_PYTHON3:
        # can't pickle.dump a ctypes.Structure in Python 2 and then
        # pickle.load it in Python 3 (the interpreter that Server32 is running on)
        fp = FourPoints((0, 0), (0, 1), (1, 1), (1, 0))
        assert c.distance_4_points(fp) == 4.0

    assert c.circumference(0.5, 0) == 0.0
    assert c.circumference(0.5, 2) == 2.0
    assert c.circumference(0.5, 2**16) == pytest.approx(math.pi)
    assert c.circumference(1.0, 2**16) == pytest.approx(2.0 * math.pi)
示例#2
0
def test_cpp():
    bitness = '64' if loadlib.IS_PYTHON_64BIT else '32'
    path = os.path.join(EXAMPLES_DIR, 'cpp_lib' + bitness)
    cpp = loadlib.LoadLibrary(path)

    lib = cpp.lib
    lib.add.argtypes = [c_int, c_int]
    lib.add.restype = c_int
    lib.subtract.argtypes = [c_float, c_float]
    lib.subtract.restype = c_float
    lib.add_or_subtract.argtypes = [c_double, c_double, c_bool]
    lib.add_or_subtract.restype = c_double
    lib.scalar_multiply.argtypes = [
        c_double, POINTER(c_double), c_int,
        POINTER(c_double)
    ]
    lib.scalar_multiply.restype = None
    lib.reverse_string_v1.argtypes = [c_char_p, c_int, c_char_p]
    lib.reverse_string_v1.restype = None
    lib.reverse_string_v2.argtypes = [c_char_p, c_int]
    lib.reverse_string_v2.restype = c_char_p
    lib.distance_4_points.argtypes = [FourPoints]
    lib.distance_4_points.restype = c_double
    lib.distance_n_points.argtypes = [NPoints]
    lib.distance_n_points.restype = c_double

    assert 3 == lib.add(1, 2)
    assert -1002 == lib.add(-1000, -2)
    assert 10.0 == pytest.approx(lib.subtract(20.0, 10.0))
    assert -10.0 == pytest.approx(lib.subtract(90.0, 100.0))
    assert 0.0 == pytest.approx(lib.add_or_subtract(0.1234, -0.1234, True))
    assert 100.0 == pytest.approx(lib.add_or_subtract(123.456, 23.456, False))

    n = 100
    a = 3.1415926
    values = (c_double * n)(*tuple(x for x in range(n)))
    out = (c_double * n)()
    lib.scalar_multiply(a, values, n, out)
    for i in range(n):
        assert a * values[i] == pytest.approx(out[i])

    str_in = '1234567890'
    str_out = create_string_buffer(len(str_in))
    lib.reverse_string_v1(create_string_buffer(str_in.encode()), len(str_in),
                          str_out)
    assert '0987654321' == str_out.raw.decode()

    str_in = '&* 1 j z|x cba['
    str_out = lib.reverse_string_v2(create_string_buffer(str_in.encode()),
                                    len(str_in))
    # ignore testing for null termination on different platforms
    assert '[abc x|z j 1 *&' == str_out[:len(str_in)].decode()

    fp = FourPoints((0, 0), (0, 1), (1, 1), (1, 0))
    assert lib.distance_4_points(fp) == pytest.approx(4.0)

    n = 2**16
    theta = 0.0
    delta = (2.0 * math.pi) / float(n)
    pts = NPoints()
    pts.n = n
    pts.points = (Point * n)()
    for i in range(n):
        pts.points[i] = Point(math.cos(theta), math.sin(theta))
        theta += delta
    assert lib.distance_n_points(pts) == pytest.approx(2.0 * math.pi)