int_float_convert is tested ok

This commit is contained in:
lyon 2021-11-20 09:28:21 +08:00
parent 3b4fa967e3
commit b56c2bcc68
3 changed files with 62 additions and 0 deletions

View File

@ -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():

View File

@ -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;
}

View File

@ -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);