mirror of
https://gitee.com/Lyon1998/pikapython.git
synced 2025-01-15 17:02:53 +08:00
int_float_convert is tested ok
This commit is contained in:
parent
3b4fa967e3
commit
b56c2bcc68
@ -19,6 +19,12 @@ class SysObj(BaseObj):
|
||||
def remove(argPath: str):
|
||||
pass
|
||||
|
||||
def int(arg: any) -> int:
|
||||
pass
|
||||
|
||||
def float(arg: any) -> float:
|
||||
pass
|
||||
|
||||
|
||||
class List(TinyObj):
|
||||
def init():
|
||||
|
@ -49,3 +49,29 @@ void PikaStdLib_SysObj_type(PikaObj* self, char* argPath) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
float PikaStdLib_SysObj_float(PikaObj* self, Arg* arg) {
|
||||
ArgType type = arg_getType(arg);
|
||||
if (TYPE_INT == type) {
|
||||
return (float)arg_getInt(arg);
|
||||
}
|
||||
if (TYPE_FLOAT == type) {
|
||||
return (float)arg_getFloat(arg);
|
||||
}
|
||||
obj_setSysOut(self, "[error] convert to float type faild.");
|
||||
obj_setErrorCode(self, 1);
|
||||
return -99999.99999;
|
||||
}
|
||||
|
||||
int PikaStdLib_SysObj_int(PikaObj* self, Arg* arg) {
|
||||
ArgType type = arg_getType(arg);
|
||||
if (TYPE_INT == type) {
|
||||
return (int)arg_getInt(arg);
|
||||
}
|
||||
if (TYPE_FLOAT == type) {
|
||||
return (int)arg_getFloat(arg);
|
||||
}
|
||||
obj_setSysOut(self, "[error] convert to int type faild.");
|
||||
obj_setErrorCode(self, 1);
|
||||
return -999999999;
|
||||
}
|
@ -55,6 +55,36 @@ TEST(pikaMain, list_new) {
|
||||
EXPECT_EQ(a1, 7);
|
||||
EXPECT_STREQ(a2, "eee");
|
||||
|
||||
/* deinit */
|
||||
obj_deinit(globals);
|
||||
obj_deinit(pikaMain);
|
||||
EXPECT_EQ(pikaMemNow(), 0);
|
||||
}
|
||||
|
||||
TEST(pikaMain, int_float_convert) {
|
||||
/* init */
|
||||
pikaMemInfo.heapUsedMax = 0;
|
||||
PikaObj* pikaMain = newRootObj((char*)"pikaMain", New_PikaMain);
|
||||
/* run */
|
||||
Parameters* globals =
|
||||
obj_runDirect(pikaMain, (char*)
|
||||
"a = 1\n"
|
||||
"a_f = float(a)\n"
|
||||
"b = 1.3\n"
|
||||
"b_i = int(b)\n"
|
||||
);
|
||||
/* collect */
|
||||
int a = obj_getInt(globals, (char*)"a");
|
||||
float a_f = obj_getFloat(globals, (char*)"a_f");
|
||||
float b = obj_getFloat(globals, (char*)"b");
|
||||
int b_i = obj_getInt(globals, (char*)"b_i");
|
||||
|
||||
/* assert */
|
||||
EXPECT_EQ(a, 1);
|
||||
EXPECT_FLOAT_EQ(a_f, 1);
|
||||
EXPECT_FLOAT_EQ(b, 1.3);
|
||||
EXPECT_FLOAT_EQ(b_i, 1);
|
||||
|
||||
/* deinit */
|
||||
obj_deinit(globals);
|
||||
obj_deinit(pikaMain);
|
||||
|
Loading…
x
Reference in New Issue
Block a user