-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest.py.cpp
More file actions
82 lines (70 loc) · 3.04 KB
/
test.py.cpp
File metadata and controls
82 lines (70 loc) · 3.04 KB
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
73
74
75
76
77
78
79
80
81
82
#include "runtime/runtime.h"
#include "runtime/builtin_functions.h"
#include "runtime/python_list.h"
#include "runtime/python_dict.h"
#include <iostream>
#include <any>
using namespace python_runtime;
// Function definition from Python source
std::any my_function(const std::any& a, const std::any& b) {
// return a + b
return add(a, b);
}
int main() {
initialize_runtime();
// Register function
register_function("my_function", [](const std::vector<std::any>& args) -> std::any {
if (args.size() != 2) {
throw std::runtime_error("my_function() takes exactly 2 arguments");
}
return my_function(args[0], args[1]);
});
// Test case: my_function(10, 20)
try {
std::any result = get_function("my_function")({std::make_any<int>(10), std::make_any<int>(20)});
std::cout << "Result of my_function(10, 20): " << std::any_cast<int>(result) << std::endl;
} catch (const std::exception& e) {
std::cerr << "Error: " << e.what() << std::endl;
}
// Test case: my_function("hello", "world")
try {
std::any result = get_function("my_function")({std::make_any<std::string>("hello"), std::make_any<std::string>("world")});
std::cout << "Result of my_function(\"hello\", \"world\"): " << std::any_cast<std::string>(result) << std::endl;
} catch (const std::exception& e) {
std::cerr << "Error: " << e.what() << std::endl;
}
// Test case: my_function(10, "world") - Should fail
try {
std::any result = get_function("my_function")({std::make_any<int>(10), std::make_any<std::string>("world")});
std::cout << "Result of my_function(10, \"world\"): " << std::any_cast<std::string>(result) << std::endl;
} catch (const std::exception& e) {
std::cerr << "Error: " << e.what() << std::endl;
}
// --- Len tests ---
std::cout << "\n--- Len tests ---" << std::endl;
// Test case: len("test")
try {
std::any result = len(std::make_any<std::string>("test"));
std::cout << "Result of len(\"test\"): " << std::any_cast<int>(result) << std::endl;
} catch (const std::exception& e) {
std::cerr << "Error: " << e.what() << std::endl;
}
// Test case: len(PythonList)
try {
PythonList list({std::make_any<int>(1), std::make_any<int>(2), std::make_any<int>(3)});
std::any result = len(std::make_any<PythonList>(list));
std::cout << "Result of len(list): " << std::any_cast<int>(result) << std::endl;
} catch (const std::exception& e) {
std::cerr << "Error: " << e.what() << std::endl;
}
// Test case: len(PythonDict)
try {
PythonDict dict;
dict.set(std::make_any<std::string>("a"), std::make_any<int>(1));
dict.set(std::make_any<std::string>("b"), std::make_any<int>(2));
std::any result = len(std::make_any<PythonDict>(dict));
std::cout << "Result of len(dict): " << std::any_cast<int>(result) << std::endl;
} catch (const std::exception& e) {
std::cerr << "Error: " << e.what() << std::endl;
}
}