2018-06-27 13:46:42 +08:00
|
|
|
|
#include <string>
|
2018-02-21 19:36:38 +08:00
|
|
|
|
#include "base/utils.h"
|
|
|
|
|
#include "gtest/gtest.h"
|
|
|
|
|
|
2018-06-21 17:42:18 +08:00
|
|
|
|
using std::string;
|
|
|
|
|
|
2018-02-21 19:36:38 +08:00
|
|
|
|
TEST(Utils, basic) {
|
|
|
|
|
char str[32];
|
|
|
|
|
|
2018-05-26 17:48:14 +08:00
|
|
|
|
ASSERT_EQ(tk_atoi("100"), 100);
|
|
|
|
|
ASSERT_EQ(strcmp(tk_itoa(str, sizeof(str), tk_atoi("100")), "100"), 0);
|
2018-02-21 19:36:38 +08:00
|
|
|
|
}
|
2018-05-26 08:02:44 +08:00
|
|
|
|
|
2018-05-27 17:27:27 +08:00
|
|
|
|
static void check_buff16(uint16_t* buff, uint16_t val, uint32_t size) {
|
2018-05-26 08:02:44 +08:00
|
|
|
|
uint32_t i = 0;
|
2018-05-27 07:02:50 +08:00
|
|
|
|
for (i = 0; i < size; i++) {
|
2018-05-27 17:27:27 +08:00
|
|
|
|
ASSERT_EQ(buff[i], val);
|
2018-05-26 08:02:44 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST(Utils, tk_memset16) {
|
|
|
|
|
uint16_t buff[32];
|
|
|
|
|
uint16_t val = 0x1234;
|
|
|
|
|
|
|
|
|
|
memset(buff, 0x00, sizeof(buff));
|
|
|
|
|
tk_memset16(buff, val, 1);
|
2018-05-27 17:27:27 +08:00
|
|
|
|
check_buff16(buff, val, 1);
|
2018-05-27 07:02:50 +08:00
|
|
|
|
|
2018-05-26 08:02:44 +08:00
|
|
|
|
memset(buff, 0x00, sizeof(buff));
|
|
|
|
|
tk_memset16(buff, val, 7);
|
2018-05-27 17:27:27 +08:00
|
|
|
|
check_buff16(buff, val, 7);
|
2018-05-26 08:02:44 +08:00
|
|
|
|
|
|
|
|
|
memset(buff, 0x00, sizeof(buff));
|
2018-05-27 07:02:50 +08:00
|
|
|
|
tk_memset16(buff + 1, val, 10);
|
2018-05-27 17:27:27 +08:00
|
|
|
|
check_buff16(buff + 1, val, 10);
|
2018-05-26 08:02:44 +08:00
|
|
|
|
|
|
|
|
|
memset(buff, 0x00, sizeof(buff));
|
2018-05-27 07:02:50 +08:00
|
|
|
|
tk_memset16(buff + 1, val, 11);
|
2018-05-27 17:27:27 +08:00
|
|
|
|
check_buff16(buff + 1, val, 11);
|
2018-05-26 08:02:44 +08:00
|
|
|
|
|
|
|
|
|
memset(buff, 0x00, sizeof(buff));
|
|
|
|
|
tk_memset16(buff, val, 10);
|
2018-05-27 17:27:27 +08:00
|
|
|
|
check_buff16(buff, val, 10);
|
2018-05-26 08:02:44 +08:00
|
|
|
|
|
|
|
|
|
memset(buff, 0x00, sizeof(buff));
|
|
|
|
|
tk_memset16(buff, val, 11);
|
2018-05-27 17:27:27 +08:00
|
|
|
|
check_buff16(buff, val, 11);
|
2018-05-26 08:02:44 +08:00
|
|
|
|
}
|
2018-05-27 17:27:27 +08:00
|
|
|
|
|
|
|
|
|
static void check_buff32(uint32_t* buff, uint32_t val, uint32_t size) {
|
|
|
|
|
uint32_t i = 0;
|
|
|
|
|
for (i = 0; i < size; i++) {
|
|
|
|
|
ASSERT_EQ(buff[i], val);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST(Utils, tk_memset32) {
|
|
|
|
|
uint32_t buff[32];
|
|
|
|
|
uint32_t val = 0x1234;
|
|
|
|
|
|
|
|
|
|
memset(buff, 0x00, sizeof(buff));
|
|
|
|
|
tk_memset32(buff, val, 1);
|
|
|
|
|
check_buff32(buff, val, 1);
|
|
|
|
|
|
|
|
|
|
memset(buff, 0x00, sizeof(buff));
|
|
|
|
|
tk_memset32(buff, val, 7);
|
|
|
|
|
check_buff32(buff, val, 7);
|
|
|
|
|
|
|
|
|
|
memset(buff, 0x00, sizeof(buff));
|
|
|
|
|
tk_memset32(buff + 1, val, 10);
|
|
|
|
|
check_buff32(buff + 1, val, 10);
|
|
|
|
|
|
|
|
|
|
memset(buff, 0x00, sizeof(buff));
|
|
|
|
|
tk_memset32(buff + 1, val, 11);
|
|
|
|
|
check_buff32(buff + 1, val, 11);
|
|
|
|
|
|
|
|
|
|
memset(buff, 0x00, sizeof(buff));
|
|
|
|
|
tk_memset32(buff, val, 10);
|
|
|
|
|
check_buff32(buff, val, 10);
|
|
|
|
|
|
|
|
|
|
memset(buff, 0x00, sizeof(buff));
|
|
|
|
|
tk_memset32(buff, val, 11);
|
|
|
|
|
check_buff32(buff, val, 11);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST(Utils, tk_memcpy16) {
|
|
|
|
|
uint32_t i = 0;
|
|
|
|
|
uint16_t dst[100];
|
|
|
|
|
uint16_t src[100];
|
|
|
|
|
|
|
|
|
|
tk_memset16(src, 0x1234, ARRAY_SIZE(src));
|
2018-05-28 06:38:26 +08:00
|
|
|
|
for (i = 1; i < ARRAY_SIZE(src); i++) {
|
2018-05-27 17:27:27 +08:00
|
|
|
|
tk_memcpy16(dst, src, i);
|
|
|
|
|
ASSERT_EQ(memcmp(dst, src, i * 2), 0);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST(Utils, tk_memcpy32) {
|
|
|
|
|
uint32_t i = 0;
|
|
|
|
|
uint32_t dst[100];
|
|
|
|
|
uint32_t src[100];
|
|
|
|
|
|
|
|
|
|
tk_memset32(src, 0x12345678, ARRAY_SIZE(src));
|
2018-05-28 06:38:26 +08:00
|
|
|
|
for (i = 1; i < ARRAY_SIZE(src); i++) {
|
2018-05-27 17:27:27 +08:00
|
|
|
|
tk_memcpy32(dst, src, i);
|
|
|
|
|
ASSERT_EQ(memcmp(dst, src, i * 4), 0);
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-06-17 12:03:18 +08:00
|
|
|
|
|
2018-06-21 17:42:18 +08:00
|
|
|
|
TEST(Utils, tk_strncpy) {
|
|
|
|
|
char dst[32];
|
|
|
|
|
const char* str = "hello world";
|
|
|
|
|
|
|
|
|
|
ASSERT_EQ(string(tk_strncpy(dst, str, 1)), string("h"));
|
|
|
|
|
ASSERT_EQ(string(tk_strncpy(dst, str, strlen(str))), string(str));
|
|
|
|
|
ASSERT_EQ(string(tk_strncpy(dst, str, strlen(str) + 1)), string(str));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST(Utils, filename_to_name) {
|
|
|
|
|
char name[NAME_LEN + 1];
|
|
|
|
|
|
|
|
|
|
filename_to_name("test.png", name, NAME_LEN);
|
|
|
|
|
ASSERT_EQ(string(name), string("test"));
|
|
|
|
|
|
|
|
|
|
filename_to_name("/a/test.png", name, NAME_LEN);
|
|
|
|
|
ASSERT_EQ(string(name), string("test"));
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-17 12:03:18 +08:00
|
|
|
|
TEST(Utils, tk_skip_to_num) {
|
|
|
|
|
ASSERT_EQ(tk_atoi(tk_skip_to_num("123")), 123);
|
|
|
|
|
ASSERT_EQ(tk_atoi(tk_skip_to_num("abc123")), 123);
|
|
|
|
|
ASSERT_EQ(tk_atoi(tk_skip_to_num("abc:@#123")), 123);
|
|
|
|
|
}
|