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

107 lines
3.2 KiB
C
Raw Normal View History

2021-10-01 00:21:50 +08:00
#include "gtest/gtest.h"
#include "test_common.h"
2021-10-01 00:21:50 +08:00
extern "C" {
#include "dataArgs.h"
#include "dataMemory.h"
#include "dataString.h"
#include "dataStrs.h"
}
static void printInfo(const char* argName, char* argVal) {
2021-11-14 18:39:08 +08:00
printf("\t\t[info] %s: \"%s\"\r\n", argName, argVal);
2021-10-01 00:21:50 +08:00
}
static int mem;
TEST(strs, append) {
2021-11-14 18:39:08 +08:00
Args* buffs = New_strBuff();
2022-04-30 21:17:19 +08:00
char* res = strsAppend(buffs, "a", "b");
EXPECT_STREQ("ab", res);
2021-11-14 18:39:08 +08:00
args_deinit(buffs);
EXPECT_EQ(pikaMemNow(), 0);
2021-10-01 00:21:50 +08:00
}
TEST(strs, formatInt) {
2021-11-14 18:39:08 +08:00
Args* buffs = New_strBuff();
char* res = strsFormat(buffs, 32, "test: %d", 3);
2022-04-30 21:17:19 +08:00
EXPECT_STREQ("test: 3", res);
2021-11-14 18:39:08 +08:00
args_deinit(buffs);
EXPECT_EQ(pikaMemNow(), 0);
2021-10-01 00:21:50 +08:00
}
TEST(strs, analizeDef) {
2021-11-14 18:39:08 +08:00
mem = pikaMemNow();
Args* buffs = New_args(NULL);
char currentClassName[] = "Compiler";
char line[] = " def analizeFile(pythonApiPath: str):";
printInfo("currentClassName", currentClassName);
char* defSentenceWithBlock =
2022-04-30 21:17:19 +08:00
strsRemovePrefix(buffs, line, " def ");
2021-11-14 18:39:08 +08:00
char* defSentence = strsDeleteChar(buffs, defSentenceWithBlock, ' ');
printInfo("defSentence", defSentence);
char* methodName = strsGetFirstToken(buffs, defSentence, '(');
printInfo("methodName", methodName);
char* methodObjPath = strsAppend(
2022-04-30 21:17:19 +08:00
buffs, strsAppend(buffs, currentClassName, "."), methodName);
2021-11-14 18:39:08 +08:00
printInfo("methodObjPath", methodObjPath);
char* returnType = strsCut(buffs, defSentence, '>', ':');
printInfo("returnType", returnType);
2021-10-01 00:21:50 +08:00
2021-11-14 18:39:08 +08:00
char* typeList = strsCut(buffs, defSentence, '(', ')');
printInfo("typeList", typeList);
if (0 != strGetSize(typeList)) {
int argNum = strCountSign(typeList, ',') + 1;
char* typeListBuff = strsCopy(buffs, typeList);
for (int i = 0; i < argNum; i++) {
char* typeDeclearation = strsPopToken(buffs, typeListBuff, ',');
printInfo("typeDeclearation", typeDeclearation);
char* argName = strsGetFirstToken(buffs, typeDeclearation, ':');
printInfo("argName", argName);
2022-03-09 11:28:09 +08:00
char* argType = strPointToLastToken( typeDeclearation, ':');
2021-11-14 18:39:08 +08:00
printInfo("argType", argType);
}
2021-10-01 00:21:50 +08:00
}
2021-11-14 18:39:08 +08:00
args_deinit(buffs);
return;
2021-10-01 00:21:50 +08:00
}
TEST(strs, format) {
2021-11-14 18:39:08 +08:00
Args* buffs = New_args(NULL);
char* fmt = strsFormat(buffs, 128, "test int: %d, float %f", 1, 34.2);
2022-04-30 21:17:19 +08:00
EXPECT_TRUE(strEqu("test int: 1, float 34.200000", fmt));
2021-11-14 18:39:08 +08:00
args_deinit(buffs);
2021-10-01 00:21:50 +08:00
}
TEST(strs, mem) {
2021-11-14 18:39:08 +08:00
EXPECT_EQ(pikaMemNow(), mem);
2021-10-01 00:21:50 +08:00
}
TEST(strs, arg_strAppend) {
2022-04-30 21:17:19 +08:00
Arg* str_arg = arg_setStr(NULL, "", "a");
str_arg = arg_strAppend(str_arg, "b");
EXPECT_STREQ(arg_getStr(str_arg), "ab");
arg_deinit(str_arg);
EXPECT_EQ(pikaMemNow(), 0);
}
2022-01-10 21:49:52 +08:00
TEST(strs, strsReplace) {
Args* buffs = New_strBuff();
2022-04-30 21:17:19 +08:00
char* res = strsReplace(buffs, "abcdefg", "cd", "47");
EXPECT_STREQ(res, "ab47efg");
2022-01-10 21:49:52 +08:00
args_deinit(buffs);
EXPECT_EQ(pikaMemNow(), 0);
2022-02-21 15:13:47 +08:00
}
2022-03-09 11:28:09 +08:00
TEST(strs, cut_) {
2022-02-21 15:13:47 +08:00
Args* buffs = New_strBuff();
2022-04-30 21:17:19 +08:00
char* res = strsCut(buffs, "print('test,test')", '(', ')');
EXPECT_STREQ(res, "'test,test'");
2022-02-21 15:13:47 +08:00
args_deinit(buffs);
EXPECT_EQ(pikaMemNow(), 0);
}
2022-03-09 11:28:09 +08:00
TEST(str, strPointToLastToken) {
2022-04-30 21:17:19 +08:00
char* tokens = "abc.efg";
2022-03-09 11:28:09 +08:00
char* last_token = strPointToLastToken(tokens, '.');
2022-04-30 21:17:19 +08:00
EXPECT_STREQ(last_token, "efg");
2022-03-09 11:28:09 +08:00
}