pikapython/port/linux/test/args-test.cpp

261 lines
7.3 KiB
C
Raw Normal View History

2021-10-01 00:21:50 +08:00
#include "gtest/gtest.h"
extern "C" {
2022-03-16 12:16:09 +08:00
#include "PikaVM.h"
2021-10-01 00:21:50 +08:00
#include "dataArgs.h"
#include "dataString.h"
}
static int mem;
TEST(args, test1) {
2021-11-13 16:42:01 +08:00
mem = pikaMemNow();
Args* args = New_args(NULL);
2022-04-30 21:17:19 +08:00
args_setInt(args, "a", 1);
int a = args_getInt(args, "a");
2021-11-13 16:42:01 +08:00
EXPECT_EQ(a, 1);
args_deinit(args);
EXPECT_EQ(pikaMemNow(), 0);
2021-10-01 00:21:50 +08:00
}
TEST(args, test4) {
2021-11-13 16:42:01 +08:00
int32_t testint = 12333;
Args* args = New_args(NULL);
2022-04-30 21:17:19 +08:00
args_setInt(args, "testint", testint);
EXPECT_EQ(12333, args_getInt(args, "testint"));
2021-11-13 16:42:01 +08:00
args_deinit(args);
EXPECT_EQ(pikaMemNow(), 0);
2021-10-01 00:21:50 +08:00
}
TEST(args, test2) {
2021-11-13 16:42:01 +08:00
Args* args = New_args(NULL);
int64_t int64Out = 0;
void* pointer = NULL;
char* strOut = NULL;
2022-04-30 21:17:19 +08:00
args_setInt(args, "int64Test", (int64_t)22221);
args_setPtr(args, "pointerTest", (void*)2222322);
args_setStr(args, "strTest", "teeeds");
2021-11-13 16:42:01 +08:00
2022-04-30 21:17:19 +08:00
int64Out = args_getInt(args, "int64Test");
pointer = args_getPtr(args, "pointerTest");
strOut = args_getStr(args, "strTest");
2021-11-13 16:42:01 +08:00
EXPECT_EQ(int64Out, 22221);
EXPECT_EQ((uint64_t)pointer, 2222322);
2022-04-30 21:17:19 +08:00
EXPECT_EQ(1, strEqu("teeeds", strOut));
EXPECT_EQ(args_getType(args, "int64Test"), ARG_TYPE_INT);
EXPECT_EQ(args_getType(args, "pointerTest"), ARG_TYPE_POINTER);
EXPECT_EQ(args_getType(args, "strTest"), ARG_TYPE_STRING);
2021-11-13 16:42:01 +08:00
args_deinit(args);
EXPECT_EQ(pikaMemNow(), 0);
2021-10-01 00:21:50 +08:00
}
TEST(args, test3) {
2021-11-13 16:42:01 +08:00
Args* args1 = New_args(NULL);
Args* args2 = New_args(NULL);
2022-04-30 21:17:19 +08:00
args_setFloat(args1, "argtest1", 2.883);
args_setStr(args1, "strtest1", "teststr");
args_copyArgByName(args1, "argtest1", args2);
args_copyArgByName(args1, "strtest1", args2);
2021-11-13 16:42:01 +08:00
2022-04-30 21:17:19 +08:00
EXPECT_EQ(2.8830f, args_getFloat(args1, "argtest1"));
2021-11-13 16:42:01 +08:00
args_deinit(args1);
args_deinit(args2);
EXPECT_EQ(pikaMemNow(), 0);
2021-10-01 00:21:50 +08:00
}
TEST(args, buff) {
2021-11-13 16:42:01 +08:00
Args* buffs = New_args(NULL);
2021-11-14 18:39:08 +08:00
args_getBuff(buffs, 256);
2021-11-13 16:42:01 +08:00
args_deinit(buffs);
EXPECT_EQ(pikaMemNow(), 0);
2021-10-01 00:21:50 +08:00
}
TEST(args, update) {
2021-11-13 16:42:01 +08:00
Args* args = New_args(NULL);
2022-04-30 21:17:19 +08:00
args_setInt(args, "testInt", 124);
args_setInt(args, "testInt", 125);
EXPECT_EQ(125, args_getInt(args, "testInt"));
2021-11-13 16:42:01 +08:00
args_deinit(args);
EXPECT_EQ(pikaMemNow(), 0);
2021-10-01 00:21:50 +08:00
}
TEST(args, print_int) {
2021-11-13 16:42:01 +08:00
Args* args = New_args(NULL);
2022-04-30 21:17:19 +08:00
args_setInt(args, "testInt", 124);
EXPECT_STREQ("124", args_print(args, "testInt"));
2021-11-13 16:42:01 +08:00
args_deinit(args);
EXPECT_EQ(pikaMemNow(), 0);
2021-10-01 00:21:50 +08:00
}
// TEST(args, test5) {
// int32_t testInt = 124;
// Args* args = New_args(NULL);
2022-04-30 21:17:19 +08:00
// args_bind(args, "i", "testInt", &testInt);
// char* type = args_getType(args, "testInt");
// args_print(args, "testInt");
// EXPECT_STREQ("124", args_print(args, "testInt"));
// args_deinit(args);
// EXPECT_EQ(pikaMemNow(), 0);
// }
2021-10-01 00:21:50 +08:00
TEST(args, test6) {
2021-11-13 16:42:01 +08:00
Args* args = New_args(NULL);
2022-04-30 21:17:19 +08:00
args_setFloat(args, "testfloat", 1.42);
EXPECT_STREQ("1.420000", args_print(args, "testfloat"));
2021-11-13 16:42:01 +08:00
args_deinit(args);
EXPECT_EQ(pikaMemNow(), 0);
2021-10-01 00:21:50 +08:00
}
// TEST(args, test7) {
// Args* args = New_args(NULL);
// float floatBindTest = 2.314;
2022-04-30 21:17:19 +08:00
// args_bind(args, "f", "floatBind", &floatBindTest);
// EXPECT_STREQ("2.314000", args_print(args, "floatBind"));
// args_deinit(args);
// EXPECT_EQ(pikaMemNow(), 0);
// }
2021-10-01 00:21:50 +08:00
TEST(args, test8) {
2021-11-13 16:42:01 +08:00
Args* args = New_args(NULL);
2022-04-30 21:17:19 +08:00
args_setStr(args, "testString", "test string print");
EXPECT_STREQ("test string print", args_print(args, "testString"));
2021-11-13 16:42:01 +08:00
args_deinit(args);
EXPECT_EQ(pikaMemNow(), 0);
2021-10-01 00:21:50 +08:00
}
// TEST(args, test9) {
// Args* args = New_args(NULL);
// char strBindTest[] = "test string bind";
2022-04-30 21:17:19 +08:00
// args_bindStr(args, "testStringBind", (char**)&strBindTest);
// EXPECT_STREQ("test string bind",
// args_print(args, "testStringBind"));
// args_deinit(args);
// EXPECT_EQ(pikaMemNow(), 0);
// }
2021-10-01 00:21:50 +08:00
TEST(args, test12) {
2021-11-13 16:42:01 +08:00
Args* args = New_args(NULL);
2022-04-30 21:17:19 +08:00
args_setInt(args, "test", 13);
args_setInt(args, "kw", 25);
2021-11-13 16:42:01 +08:00
EXPECT_EQ(2, args_getSize(args));
2022-04-30 21:17:19 +08:00
EXPECT_EQ(13, (int)args_getInt(args, "test"));
args_removeArg(args, args_getArg(args, "test"));
2021-11-13 16:42:01 +08:00
EXPECT_EQ(1, args_getSize(args));
2022-04-30 21:17:19 +08:00
EXPECT_EQ(-999999999, (int)args_getInt(args, "test"));
2021-11-13 16:42:01 +08:00
args_deinit(args);
EXPECT_EQ(pikaMemNow(), 0);
2021-10-01 00:21:50 +08:00
}
2021-11-14 10:43:15 +08:00
struct test {
int a;
float b;
2022-03-16 12:16:09 +08:00
int c;
2021-11-14 10:43:15 +08:00
};
TEST(args, struct_) {
Args* args = New_args(NULL);
2022-03-16 12:16:09 +08:00
struct test test_struct = {1, 2.2, 4};
2022-04-30 21:17:19 +08:00
args_setStruct(args, "test", test_struct);
struct test* out_struct = (struct test*)args_getStruct(args, "test");
2022-03-16 12:16:09 +08:00
EXPECT_EQ(out_struct->a, 1);
EXPECT_FLOAT_EQ(out_struct->b, 2.2);
EXPECT_EQ(out_struct->c, 4);
2021-11-14 10:43:15 +08:00
args_deinit(args);
EXPECT_EQ(pikaMemNow(), 0);
}
2021-10-01 00:21:50 +08:00
TEST(args, mem) {
2021-11-13 16:42:01 +08:00
EXPECT_EQ(pikaMemNow(), mem);
EXPECT_EQ(pikaMemNow(), 0);
2021-10-01 00:21:50 +08:00
}
2022-01-04 20:18:23 +08:00
TEST(args, index) {
Args* args = New_args(NULL);
2022-04-30 21:17:19 +08:00
args_pushArg(args, arg_setInt(NULL, "", 1));
args_pushArg(args, arg_setFloat(NULL, "", 2.4));
2022-01-04 20:18:23 +08:00
int a = arg_getInt(args_getArg_index(args, 1));
float b = arg_getFloat(args_getArg_index(args, 0));
/* assert */
EXPECT_EQ(a, 1);
EXPECT_FLOAT_EQ(b, 2.4);
/* check memory */
args_deinit(args);
EXPECT_EQ(pikaMemNow(), 0);
2022-01-11 22:22:53 +08:00
}
TEST(args, int_float_convert) {
Args* args = New_args(NULL);
2022-04-30 21:17:19 +08:00
args_setInt(args, "a", 10);
args_setFloat(args, "b", 2.333);
2022-01-11 22:22:53 +08:00
2022-04-30 21:17:19 +08:00
float a = args_getFloat(args, "a");
int b = args_getInt(args, "b");
2022-01-11 22:22:53 +08:00
/* assert */
EXPECT_EQ(a, 10);
EXPECT_FLOAT_EQ(b, 2);
/* check memory */
args_deinit(args);
EXPECT_EQ(pikaMemNow(), 0);
2022-03-16 12:16:09 +08:00
}
TEST(args, heap_struct) {
ByteCodeFrame bytecode_frame;
byteCodeFrame_init(&bytecode_frame);
bytecode_frame.const_pool.size = 100;
Args* args = New_args(NULL);
2022-04-30 21:17:19 +08:00
args_setHeapStruct(args, "a", bytecode_frame, byteCodeFrame_deinit);
ByteCodeFrame* bf_out = (ByteCodeFrame*)args_getHeapStruct(args, "a");
2022-03-16 12:16:09 +08:00
/* assert */
EXPECT_EQ(bf_out->const_pool.size, 100);
/* check memory */
args_deinit(args);
EXPECT_EQ(pikaMemNow(), 0);
}
2022-04-11 22:27:18 +08:00
TEST(args, heap_struct_override) {
ByteCodeFrame bytecode_frame;
byteCodeFrame_init(&bytecode_frame);
bytecode_frame.const_pool.size = 100;
ByteCodeFrame bytecode_frame_1;
byteCodeFrame_init(&bytecode_frame_1);
bytecode_frame_1.const_pool.size = 100;
Args* args = New_args(NULL);
2022-04-30 21:17:19 +08:00
args_setHeapStruct(args, "a", bytecode_frame, byteCodeFrame_deinit);
args_setHeapStruct(args, "a", bytecode_frame_1, byteCodeFrame_deinit);
ByteCodeFrame* bf_out = (ByteCodeFrame*)args_getHeapStruct(args, "a");
2022-04-11 22:27:18 +08:00
/* assert */
EXPECT_EQ(bf_out->const_pool.size, 100);
/* check memory */
args_deinit(args);
EXPECT_EQ(pikaMemNow(), 0);
}
TEST(args, args_move) {
Args* args = New_args(NULL);
Args* args2 = New_args(NULL);
2022-04-30 21:17:19 +08:00
args_setInt(args, "a", 100);
args_moveArg(args, args2, args_getArg(args, "a"));
/* assert */
2022-04-30 21:17:19 +08:00
EXPECT_EQ(-999999999, args_getInt(args, "a"));
EXPECT_EQ(100, args_getInt(args2, "a"));
/* deinit */
args_deinit(args);
args_deinit(args2);
EXPECT_EQ(pikaMemNow(), 0);
}
2022-04-18 11:45:59 +08:00
TEST(args, args_mem) {
2022-04-20 17:05:21 +08:00
uint8_t mem_test[] = {0x33, 0x55, 0x00, 0x15};
2022-04-18 11:45:59 +08:00
Args* args = New_args(NULL);
2022-04-30 21:17:19 +08:00
args_setBytes(args, "mem", mem_test, sizeof(mem_test));
size_t mem_size = args_getBytesSize(args, "mem");
char* mem_test_out = (char*)args_getBytes(args, "mem");
2022-04-18 11:45:59 +08:00
EXPECT_EQ(mem_size, sizeof(mem_test));
EXPECT_EQ(mem_test_out[0], 0x33);
EXPECT_EQ(mem_test_out[1], 0x55);
EXPECT_EQ(mem_test_out[2], 0x00);
EXPECT_EQ(mem_test_out[3], 0x15);
args_deinit(args);
}