-
Notifications
You must be signed in to change notification settings - Fork 0
/
add_example.py
72 lines (48 loc) · 2.66 KB
/
add_example.py
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
71
72
from ctypes import CDLL, c_float, RTLD_GLOBAL
from os.path import join, isfile, abspath
from unittest import TestCase, main, skip
def dir_path(file_path: str) -> str:
if "/" in file_path:
return '/'.join(file_path.split('/')[:-1])
elif "\\" in file_path:
return '\\'.join(file_path.split('\\')[:-1])
class Ctypes_Test(TestCase):
CURRENT_WORKING_DIRECTORY = dir_path(abspath(__file__))
def test_simple_add(self) -> None:
print("Performing Ctypes simple add test.")
ctypes_source_file_location = join(Ctypes_Test.CURRENT_WORKING_DIRECTORY, "c_code", "adder.so")
assert isfile(ctypes_source_file_location), "Unable to locate file " + ctypes_source_file_location
#load the shared object file
adder = CDLL(ctypes_source_file_location)
#Find sum of integers
assert adder.add_int(4,5) == 9
#Find sum of floats
a = c_float(5.5)
b = c_float(4.1)
add_float = adder.add_float
add_float.restype = c_float
c_types_float_result = add_float(a, b)
assert round(c_types_float_result, 2) == round(9.6, 2)
print("Ctypes test was successful.")
def test_rti_dds_connector_load(self) -> None:
print("Performing rti dds connector load test.")
ctypes_source_file_location = join(Ctypes_Test.CURRENT_WORKING_DIRECTORY, "test_connectors", "jetson_nano", 'librtiddsconnector.so')
assert isfile(ctypes_source_file_location), "Unable to locate file " + ctypes_source_file_location
rti = CDLL(ctypes_source_file_location, RTLD_GLOBAL)
print("RTI DDS Connector load test was successful.")
@skip
def test_arm6v_rti_dds_connector_load(self) -> None:
print("Performing rti dds connector load test.")
ctypes_source_file_location = join(Ctypes_Test.CURRENT_WORKING_DIRECTORY, "test_connectors", "arm6vfphLinux3.xgcc4.7.2", 'librtiddsconnector.so')
assert isfile(ctypes_source_file_location), "Unable to locate file " + ctypes_source_file_location
rti = CDLL(ctypes_source_file_location, RTLD_GLOBAL)
print("RTI DDS Connector load test was successful.")
@skip
def test_64Linux_rti_dds_connector_load(self) -> None:
print("Performing rti dds connector load test.")
ctypes_source_file_location = join(Ctypes_Test.CURRENT_WORKING_DIRECTORY, "test_connectors", "x64Linux2.6gcc4.4.5", 'librtiddsconnector.so')
assert isfile(ctypes_source_file_location), "Unable to locate file " + ctypes_source_file_location
rti = CDLL(ctypes_source_file_location, RTLD_GLOBAL)
print("RTI DDS Connector load test was successful.")
if __name__ == "__main__":
main()