| 1 | from ctypes import * |
| 2 | import math, os |
| 3 | |
| 4 | ## Load the V shared library: |
| 5 | so_file="./test.so" |
| 6 | if os.name=="nt": |
| 7 | so_file="./test.dll" |
| 8 | lib = CDLL(so_file) |
| 9 | |
| 10 | ## Pass an integer to a V function, and receiving back an integer: |
| 11 | print("lib.square(10) result is", lib.square(10)) |
| 12 | assert lib.square(10) == 100, "Cannot validate V square()." |
| 13 | |
| 14 | ## Pass a floating point number to a V function: |
| 15 | lib.sqrt_of_sum_of_squares.restype = c_double |
| 16 | assert lib.sqrt_of_sum_of_squares(c_double(1.1), c_double(2.2)) == math.sqrt(1.1*1.1 + 2.2*2.2), "Cannot validate V sqrt_of_sum_of_squares()." |
| 17 | |
| 18 | ## Passing a V string to a V function, and receiving back a V string: |
| 19 | class VString(Structure): |
| 20 | _fields_ = [("str", c_char_p), ("len", c_int)] |
| 21 | |
| 22 | lib.process_v_string.argtypes = [VString] |
| 23 | lib.process_v_string.restype = VString |
| 24 | |
| 25 | assert lib.process_v_string(VString(b'World', 5)).str == b'v World v' |
| 26 | print('Hello', str(lib.process_v_string(VString(b'World', 5)).str, 'utf-8')) |
| 27 | |