From b8d656b3089dfe28fefdf3f319472ce541ce58b5 Mon Sep 17 00:00:00 2001 From: Mihai Galos Date: Wed, 16 Feb 2022 08:12:49 +0100 Subject: [PATCH] examples: enhance the call_v_from_python example, with a fn accepting 2 f64 params (#13480) --- examples/call_v_from_python/test.py | 9 ++++++--- examples/call_v_from_python/test.v | 7 +++++++ 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/examples/call_v_from_python/test.py b/examples/call_v_from_python/test.py index 9cebed3552..d2b6069f9a 100644 --- a/examples/call_v_from_python/test.py +++ b/examples/call_v_from_python/test.py @@ -1,8 +1,11 @@ from ctypes import * -import os +import math, os so_file="./test.so" if os.name=="nt": so_file="./test.dll" -my_functions = CDLL(so_file) -print(my_functions.square(10)) +lib = CDLL(so_file) +assert lib.square(10) == 100, "Cannot validate V square()." + +lib.sqrt_of_sum_of_squares.restype = c_double +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()." diff --git a/examples/call_v_from_python/test.v b/examples/call_v_from_python/test.v index 9d5fbabc5d..87d7e92230 100644 --- a/examples/call_v_from_python/test.v +++ b/examples/call_v_from_python/test.v @@ -1,6 +1,13 @@ module test +import math + [export: 'square'] fn square(i int) int { return i * i } + +[export: 'sqrt_of_sum_of_squares'] +fn sqrt_of_sum_of_squares(x f64, y f64) f64 { + return math.sqrt(x * x + y * y) +}