2022-06-21 09:16:38 +00:00
|
|
|
#include "test_common.h"
|
|
|
|
|
2022-09-10 23:16:59 +08:00
|
|
|
#if !PIKA_NANO_ENABLE
|
2022-06-21 09:16:38 +00:00
|
|
|
TEST(class, class_par) {
|
|
|
|
/* init */
|
|
|
|
pikaMemInfo.heapUsedMax = 0;
|
|
|
|
/* run */
|
|
|
|
PikaObj* self = newRootObj("pikaMain", New_PikaMain);
|
|
|
|
__platform_printf("BEGIN\r\n");
|
|
|
|
obj_run(self, "pi = PikaMath.Math.pi\n");
|
|
|
|
/* assert */
|
2022-09-28 11:23:39 +08:00
|
|
|
pika_float pi = obj_getFloat(self, "pi");
|
2022-09-28 12:31:05 +08:00
|
|
|
#if PIKA_FLOAT_TYPE_DOUBLE
|
2022-06-21 09:16:38 +00:00
|
|
|
EXPECT_DOUBLE_EQ(pi, 3.141592653589793115997963468544185161590576171875l);
|
2022-09-28 11:23:39 +08:00
|
|
|
#else
|
|
|
|
EXPECT_FLOAT_EQ(pi, 3.141592653589793115997963468544185161590576171875l);
|
|
|
|
#endif
|
2022-06-21 09:16:38 +00:00
|
|
|
/* deinit */
|
|
|
|
obj_deinit(self);
|
|
|
|
EXPECT_EQ(pikaMemNow(), 0);
|
|
|
|
}
|
2022-07-01 09:12:47 +08:00
|
|
|
|
|
|
|
TEST(class, classpar1) {
|
|
|
|
/* init */
|
|
|
|
pikaMemInfo.heapUsedMax = 0;
|
|
|
|
/* run */
|
|
|
|
PikaObj* self = newRootObj("pikaMain", New_PikaMain);
|
|
|
|
__platform_printf("BEGIN\r\n");
|
|
|
|
pikaVM_runSingleFile(self, "../../examples/Class/classpar1.py");
|
|
|
|
/* assert */
|
2022-07-01 09:37:09 +08:00
|
|
|
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");
|
2022-07-01 09:12:47 +08:00
|
|
|
/* deinit */
|
|
|
|
obj_deinit(self);
|
|
|
|
EXPECT_EQ(pikaMemNow(), 0);
|
|
|
|
}
|
2022-07-08 18:00:46 +08:00
|
|
|
|
|
|
|
TEST(class, static_method) {
|
|
|
|
/* init */
|
|
|
|
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);
|
|
|
|
}
|
2022-09-10 23:16:59 +08:00
|
|
|
#endif
|
2022-08-10 11:49:54 +08:00
|
|
|
|
|
|
|
TEST(class, dir_) {
|
|
|
|
/* init */
|
|
|
|
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);
|
2022-10-14 23:58:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
|
2022-10-15 00:22:01 +08:00
|
|
|
void PikaStdLib_SysObj_intMethod(PikaObj* self, Args* args);
|
|
|
|
void PikaStdLib_SysObj_floatMethod(PikaObj* self, Args* args);
|
2022-10-14 23:58:29 +08:00
|
|
|
PikaObj* New_NativeMethodBase(Args* args) {
|
|
|
|
PikaObj* self = New_TinyObj(NULL);
|
2022-10-15 00:22:01 +08:00
|
|
|
static const MethodInfoStore method = {
|
|
|
|
.ptr = (void*)PikaStdLib_SysObj_floatMethod,
|
|
|
|
.bytecode_frame = NULL,
|
|
|
|
.def_context = NULL,
|
|
|
|
.declareation = "float(arg)",
|
|
|
|
.type_list = "arg",
|
2022-10-15 09:54:07 +08:00
|
|
|
.name = "float",
|
2022-10-15 00:22:01 +08:00
|
|
|
};
|
|
|
|
static const Arg methods[] = {
|
|
|
|
{._ = {.buffer = (uint8_t*)&method},
|
|
|
|
.size = sizeof(method),
|
|
|
|
#if PIKA_ARG_CACHE_ENABLE
|
|
|
|
.heap_size = 0,
|
|
|
|
#endif
|
|
|
|
.type = ARG_TYPE_METHOD_NATIVE,
|
|
|
|
.flag = 0,
|
|
|
|
.name_hash = 259121563},
|
|
|
|
};
|
|
|
|
static const NativeProperty prop = {
|
2022-10-14 23:58:29 +08:00
|
|
|
.super = NULL,
|
2022-10-15 00:22:01 +08:00
|
|
|
.methodGroup = methods,
|
|
|
|
.methodGroupCount = sizeof(methods) / sizeof(methods[0]),
|
2022-10-14 23:58:29 +08:00
|
|
|
};
|
2022-10-15 00:22:01 +08:00
|
|
|
obj_setPtr(self, "@n", (void*)&prop);
|
2022-10-14 23:58:29 +08:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-15 00:22:01 +08:00
|
|
|
TEST(class, native_class1) {
|
|
|
|
PikaObj* native_obj = newNormalObj(New_NativeMethodBase);
|
|
|
|
obj_run(native_obj, "float(123)\n");
|
|
|
|
obj_deinit(native_obj);
|
|
|
|
EXPECT_STREQ(log_buff[0], "123.000000\r\n");
|
|
|
|
EXPECT_EQ(pikaMemNow(), 0);
|
|
|
|
}
|