Repository navigation
Expand file tree
/
Copy pathclass-test.cpp
More file actions
121 lines (106 loc) · 3.21 KB
/
Copy pathclass-test.cpp
File metadata and controls
121 lines (106 loc) · 3.21 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include "test_common.h"
TEST_START
#if !PIKA_NANO_ENABLE
TEST(class, class_par) {
/* init */
g_PikaMemInfo.heapUsedMax = 0;
/* run */
PikaObj* self = newRootObj("pikaMain", New_PikaMain);
__platform_printf("BEGIN\r\n");
obj_run(self, "pi = PikaMath.Math.pi\n");
/* assert */
pika_float pi = obj_getFloat(self, "pi");
#if PIKA_FLOAT_TYPE_DOUBLE
EXPECT_DOUBLE_EQ(pi, 3.141592653589793115997963468544185161590576171875l);
#else
EXPECT_FLOAT_EQ(pi, 3.141592653589793115997963468544185161590576171875l);
#endif
/* deinit */
obj_deinit(self);
EXPECT_EQ(pikaMemNow(), 0);
}
TEST(class, classpar1) {
/* init */
g_PikaMemInfo.heapUsedMax = 0;
/* run */
PikaObj* self = newRootObj("pikaMain", New_PikaMain);
__platform_printf("BEGIN\r\n");
pikaVM_runSingleFile(self, "../../examples/Class/classpar1.py");
/* assert */
EXPECT_STREQ(log_buff[4], "BEGIN\r\n");
EXPECT_STREQ(log_buff[3], "1\r\n");
EXPECT_STREQ(log_buff[2], "test\r\n");
EXPECT_STREQ(log_buff[1], "2\r\n");
EXPECT_STREQ(log_buff[0], "pewq\r\n");
/* deinit */
obj_deinit(self);
EXPECT_EQ(pikaMemNow(), 0);
}
TEST(class, static_method) {
/* init */
g_PikaMemInfo.heapUsedMax = 0;
/* run */
PikaObj* self = newRootObj("pikaMain", New_PikaMain);
__platform_printf("BEGIN\r\n");
obj_run(self,
"import PikaMath\n"
"a = PikaMath.Operator.plusInt(10, 20)\n");
/* collect */
int a = obj_getInt(self, "a");
/* assert */
EXPECT_EQ(a, 30);
/* deinit */
obj_deinit(self);
EXPECT_EQ(pikaMemNow(), 0);
}
#endif
TEST(class, dir_) {
/* init */
g_PikaMemInfo.heapUsedMax = 0;
/* run */
PikaObj* self = newRootObj("pikaMain", New_PikaMain);
__platform_printf("BEGIN\r\n");
obj_run(self,
"class T:\n"
" def a(self):\n"
" return 1\n"
"t = T()\n"
"l = dir(t)\n"
"print(l)\n");
/* collect */
/* assert */
/* deinit */
obj_deinit(self);
EXPECT_EQ(pikaMemNow(), 0);
}
extern "C" {
void PikaStdLib_SysObj_intMethod(PikaObj* self, Args* args);
method_typedef(PikaStdLib_SysObj_int, "int", "arg");
void PikaStdLib_SysObj_floatMethod(PikaObj* self, Args* args);
method_typedef(PikaStdLib_SysObj_float, "float", "arg");
void PikaStdLib_SysObj_printMethod(PikaObj* self, Args* args);
method_typedef(PikaStdLib_SysObj_print, "print", "*val,**ops");
class_def(PikaStdLib_SysObj){
method_def(PikaStdLib_SysObj_int, hash_time33("int")),
method_def(PikaStdLib_SysObj_float, hash_time33("float")),
method_def(PikaStdLib_SysObj_print, hash_time33("print")),
};
class_inhert(PikaStdLib_SysObj, TinyObj);
PikaObj* New_NativeMethodBase(Args* args) {
PikaObj* self = New_TinyObj(NULL);
obj_setClass(self, PikaStdLib_SysObj);
return self;
}
}
TEST(class, native_class1) {
PikaObj* native_obj = newRootObj("root", New_NativeMethodBase);
obj_run(native_obj,
"print('hello', 123)\n"
"int(123.0000)\n"
"float(123)\n");
obj_deinit(native_obj);
EXPECT_STREQ(log_buff[1], "123\r\n");
EXPECT_STREQ(log_buff[0], "123.000000\r\n");
EXPECT_EQ(pikaMemNow(), 0);
}
TEST_END