提交 bc8f7c8c 编写于 作者: S starlord 提交者: jinhai

MS-129 Add more unitest


Former-commit-id: 8103255d0c3725d3fa58a35f167748788da69c79
上级 0f46d463
......@@ -7,7 +7,6 @@
#include "RequestScheduler.h"
#include "utils/Error.h"
#include "utils/AttributeSerializer.h"
#include "db/Types.h"
#include "milvus_types.h"
......
/*******************************************************************************
* Copyright 上海赜睿信息科技有限公司(Zilliz) - All Rights Reserved
* Unauthorized copying of this file, via any medium is strictly prohibited.
* Proprietary and confidential.
******************************************************************************/
#include "AttributeSerializer.h"
#include "StringHelpFunctions.h"
namespace zilliz {
namespace milvus {
namespace server {
ServerError AttributeSerializer::Encode(const AttribMap& attrib_map, std::string& attrib_str) {
attrib_str = "";
for(auto iter : attrib_map) {
attrib_str += iter.first;
attrib_str += ":\"";
attrib_str += iter.second;
attrib_str += "\";";
}
return SERVER_SUCCESS;
}
ServerError AttributeSerializer::Decode(const std::string& attrib_str, AttribMap& attrib_map) {
attrib_map.clear();
std::vector<std::string> kv_pairs;
StringHelpFunctions::SplitStringByQuote(attrib_str, ";", "\"", kv_pairs);
for(std::string& str : kv_pairs) {
std::string key, val;
size_t index = str.find_first_of(":", 0);
if (index != std::string::npos) {
key = str.substr(0, index);
val = str.substr(index + 1);
} else {
key = str;
}
attrib_map.insert(std::make_pair(key, val));
}
return SERVER_SUCCESS;
}
}
}
}
/*******************************************************************************
* Copyright 上海赜睿信息科技有限公司(Zilliz) - All Rights Reserved
* Unauthorized copying of this file, via any medium is strictly prohibited.
* Proprietary and confidential.
******************************************************************************/
#pragma once
#include <map>
#include "Error.h"
namespace zilliz {
namespace milvus {
namespace server {
using AttribMap = std::map<std::string, std::string>;
class AttributeSerializer {
public:
static ServerError Encode(const AttribMap& attrib_map, std::string& attrib_str);
static ServerError Decode(const std::string& attrib_str, AttribMap& attrib_map);
};
}
}
}
......@@ -9,13 +9,6 @@ namespace zilliz {
namespace milvus {
namespace server {
void StringHelpFunctions::TrimStringLineBreak(std::string &string) {
if (!string.empty()) {
static std::string s_format("\n\r");
string.erase(string.find_last_not_of(s_format) + 1);
}
}
void StringHelpFunctions::TrimStringBlank(std::string &string) {
if (!string.empty()) {
static std::string s_format(" \n\r\t");
......
......@@ -18,8 +18,6 @@ private:
StringHelpFunctions() = default;
public:
static void TrimStringLineBreak(std::string &string);
static void TrimStringBlank(std::string &string);
static void TrimStringQuote(std::string &string, const std::string &qoute);
......
......@@ -14,9 +14,10 @@ aux_source_directory(../../src/cache cache_srcs)
aux_source_directory(../../src/wrapper wrapper_src)
aux_source_directory(./ test_srcs)
set(server_src_files
set(utils_srcs
${MILVUS_ENGINE_SRC}/utils/StringHelpFunctions.cpp
${MILVUS_ENGINE_SRC}/utils/AttributeSerializer.cpp
${MILVUS_ENGINE_SRC}/utils/TimeRecorder.cpp
${MILVUS_ENGINE_SRC}/utils/CommonUtil.cpp
)
cuda_add_executable(server_test
......@@ -25,7 +26,7 @@ cuda_add_executable(server_test
${cache_srcs}
${wrapper_src}
${test_srcs}
${server_src_files}
${utils_srcs}
${require_files}
)
......
////////////////////////////////////////////////////////////////////////////////
// Copyright 上海赜睿信息科技有限公司(Zilliz) - All Rights Reserved
// Unauthorized copying of this file, via any medium is strictly prohibited.
// Proprietary and confidential.
////////////////////////////////////////////////////////////////////////////////
#include <gtest/gtest.h>
#include "utils/CommonUtil.h"
#include "utils/Error.h"
using namespace zilliz::milvus;
TEST(CommonTest, COMMON_TEST) {
std::string path1 = "/tmp/milvus_test/";
std::string path2 = path1 + "common_test_12345/";
std::string path3 = path2 + "abcdef";
server::ServerError err = server::CommonUtil::CreateDirectory(path3);
ASSERT_EQ(err, server::SERVER_SUCCESS);
ASSERT_TRUE(server::CommonUtil::IsDirectoryExit(path3));
err = server::CommonUtil::DeleteDirectory(path1);
ASSERT_EQ(err, server::SERVER_SUCCESS);
ASSERT_FALSE(server::CommonUtil::IsDirectoryExit(path1));
}
......@@ -4,31 +4,99 @@
// Proprietary and confidential.
////////////////////////////////////////////////////////////////////////////////
#include <gtest/gtest.h>
#include <thread>
#include "utils/AttributeSerializer.h"
#include "utils/CommonUtil.h"
#include "utils/Error.h"
#include "utils/StringHelpFunctions.h"
#include "utils/TimeRecorder.h"
#include "utils/BlockingQueue.h"
using namespace zilliz::milvus;
TEST(AttribSerializeTest, ATTRIBSERIAL_TEST) {
std::map<std::string, std::string> attrib;
attrib["uid"] = "ABCDEF";
attrib["color"] = "red";
attrib["number"] = "9900";
attrib["comment"] = "please note: it is a car, not a ship";
attrib["address"] = " china;shanghai ";
namespace {
std::string attri_str;
server::AttributeSerializer::Encode(attrib, attri_str);
using TimeUnit = server::TimeRecorder::TimeDisplayUnit;
double TestTimeRecorder(TimeUnit unit, int64_t log_level) {
server::TimeRecorder rc("test rc", unit, log_level);
rc.Record("begin");
std::this_thread::sleep_for(std::chrono::microseconds(10));
rc.Elapse("end");
return rc.Span();
}
}
TEST(CommonTest, COMMON_TEST) {
std::string path1 = "/tmp/milvus_test/";
std::string path2 = path1 + "common_test_12345/";
std::string path3 = path2 + "abcdef";
server::ServerError err = server::CommonUtil::CreateDirectory(path3);
ASSERT_EQ(err, server::SERVER_SUCCESS);
std::map<std::string, std::string> attrib_out;
server::ServerError err = server::AttributeSerializer::Decode(attri_str, attrib_out);
ASSERT_TRUE(server::CommonUtil::IsDirectoryExit(path3));
err = server::CommonUtil::DeleteDirectory(path1);
ASSERT_EQ(err, server::SERVER_SUCCESS);
ASSERT_EQ(attrib_out.size(), attrib.size());
for(auto iter : attrib) {
ASSERT_EQ(attrib_out[iter.first], attrib_out[iter.first]);
ASSERT_FALSE(server::CommonUtil::IsDirectoryExit(path1));
}
TEST(UtilTest, STRINGFUNCTIONS_TEST) {
std::string str = " test zilliz";
server::StringHelpFunctions::TrimStringBlank(str);
ASSERT_EQ(str, "test zilliz");
str = "a,b,c";
std::vector<std::string> result;
server::StringHelpFunctions::SplitStringByDelimeter(str , ",", result);
ASSERT_EQ(result.size(), 3UL);
str = "55,\"aa,gg,yy\",b";
result.clear();
server::StringHelpFunctions::SplitStringByQuote(str , ",", "\"", result);
ASSERT_EQ(result.size(), 3UL);
}
TEST(UtilTest, TIMERECORDER_TEST) {
double span = TestTimeRecorder(TimeUnit::eTimeAutoUnit, 0);
ASSERT_GT(span, 0.0);
span = TestTimeRecorder(TimeUnit::eTimeHourUnit, 1);
ASSERT_GT(span, 0.0);
span = TestTimeRecorder(TimeUnit::eTimeMinuteUnit, 2);
ASSERT_GT(span, 0.0);
span = TestTimeRecorder(TimeUnit::eTimeSecondUnit, 3);
ASSERT_GT(span, 0.0);
span = TestTimeRecorder(TimeUnit::eTimeMilliSecUnit, 4);
ASSERT_GT(span, 0.0);
span = TestTimeRecorder(TimeUnit::eTimeMicroSecUnit, -1);
ASSERT_GT(span, 0.0);
}
TEST(UtilTest, BLOCKINGQUEUE_TEST) {
server::BlockingQueue<std::string> bq;
static const size_t count = 10;
bq.SetCapacity(count);
for(size_t i = 1; i <= count; i++) {
std::string id = "No." + std::to_string(i);
bq.Put(id);
}
ASSERT_EQ(bq.Size(), count);
ASSERT_FALSE(bq.Empty());
std::string str = bq.Front();
ASSERT_EQ(str, "No.1");
str = bq.Back();
ASSERT_EQ(str, "No." + std::to_string(count));
for(size_t i = 1; i <= count; i++) {
std::string id = "No." + std::to_string(i);
str = bq.Take();
ASSERT_EQ(id, str);
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册