-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add gtest, fix string trim function crash (#42)
* fix string trim function crash * add gtest * add timestamp test,fix Random range error * Optimized trim function,add TrimLeft and TrimRight * fix StringTrimRight range error * Resolve rocksdb and gtest dependency conflicts * fix rocksdb and gtest dependency conflicts,Streamlined rocksdb compilation
- Loading branch information
Showing
12 changed files
with
333 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# Copyright (c) 2023-present, Qihoo, Inc. All rights reserved. | ||
# This source code is licensed under the BSD-style license found in the | ||
# LICENSE file in the root directory of this source tree. An additional grant | ||
# of patent rights can be found in the PATENTS file in the same directory. | ||
|
||
FETCHCONTENT_DECLARE( | ||
gtest | ||
GIT_REPOSITORY https://github.com/google/googletest.git | ||
GIT_TAG v1.14.0 | ||
) | ||
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE) | ||
FETCHCONTENT_MAKEAVAILABLE(gtest) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,14 @@ | ||
# Copyright (c) 2023-present, Qihoo, Inc. All rights reserved. | ||
# This source code is licensed under the BSD-style license found in the | ||
# LICENSE file in the root directory of this source tree. An additional grant | ||
# of patent rights can be found in the PATENTS file in the same directory. | ||
|
||
AUX_SOURCE_DIRECTORY(. STD_SRC) | ||
SET(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin) | ||
ADD_LIBRARY(pstd ${STD_SRC}) | ||
|
||
ADD_SUBDIRECTORY(tests) | ||
|
||
TARGET_LINK_LIBRARIES(pstd; spdlog pthread) | ||
|
||
SET_TARGET_PROPERTIES(pstd PROPERTIES LINKER_LANGUAGE CXX) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
|
||
#pragma once | ||
|
||
#include <algorithm> | ||
#include <chrono> | ||
#include <random> | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# Copyright (c) 2023-present, Qihoo, Inc. All rights reserved. | ||
# This source code is licensed under the BSD-style license found in the | ||
# LICENSE file in the root directory of this source tree. An additional grant | ||
# of patent rights can be found in the PATENTS file in the same directory. | ||
|
||
cmake_minimum_required(VERSION 3.18) | ||
|
||
include(GoogleTest) | ||
set(CMAKE_CXX_STANDARD 20) | ||
|
||
aux_source_directory(.. DIR_SRCS) | ||
|
||
file(GLOB_RECURSE PSTD_TEST_SOURCE "${CMAKE_CURRENT_SOURCE_DIR}/*.cc") | ||
|
||
foreach (pstd_test_source ${PSTD_TEST_SOURCE}) | ||
get_filename_component(pstd_test_filename ${pstd_test_source} NAME) | ||
string(REPLACE ".cc" "" pstd_test_name ${pstd_test_filename}) | ||
|
||
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin) | ||
add_executable(${pstd_test_name} ${pstd_test_source}) | ||
target_include_directories(${pstd_test_name} | ||
PUBLIC ${PROJECT_SOURCE_DIR}/src | ||
) | ||
|
||
add_dependencies(${pstd_test_name} pstd gtest) | ||
target_link_libraries(${pstd_test_name} | ||
PUBLIC pstd | ||
PUBLIC gtest | ||
) | ||
gtest_discover_tests(${pstd_test_name} | ||
WORKING_DIRECTORY .) | ||
endforeach () |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
// Copyright (c) 2015-present, Qihoo, Inc. All rights reserved. | ||
// This source code is licensed under the BSD-style license found in the | ||
// LICENSE file in the root directory of this source tree. An additional grant | ||
// of patent rights can be found in the PATENTS file in the same directory. | ||
|
||
// Copyright (c) 2011 The LevelDB Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. See the AUTHORS file for names of contributors. | ||
|
||
#include <climits> | ||
|
||
#include <gtest/gtest.h> | ||
#include "pstd/pstd_string.h" | ||
|
||
class StringTest : public ::testing::Test {}; | ||
|
||
TEST_F(StringTest, StringTrim) { | ||
ASSERT_EQ(pstd::StringTrim(" computer "), "computer"); | ||
ASSERT_EQ(pstd::StringTrim(" comp uter "), "comp uter"); | ||
ASSERT_EQ(pstd::StringTrim(" \n computer \n ", "\n "), "computer"); | ||
ASSERT_EQ(pstd::StringTrim("\n", "\r\n "), ""); | ||
} | ||
|
||
TEST_F(StringTest, StringTrimLeft) { | ||
ASSERT_EQ(pstd::StringTrimLeft(" computer"), "computer"); | ||
ASSERT_EQ(pstd::StringTrimLeft(" computer "), "computer "); | ||
ASSERT_EQ(pstd::StringTrimLeft(" comp uter "), "comp uter "); | ||
ASSERT_EQ(pstd::StringTrimLeft(" comp uter"), "comp uter"); | ||
ASSERT_EQ(pstd::StringTrimLeft(" \n computer \n ", "\n "), "computer \n "); | ||
ASSERT_EQ(pstd::StringTrimLeft("\n", "\r\n "), ""); | ||
} | ||
|
||
TEST_F(StringTest, StringTrimRight) { | ||
ASSERT_EQ(pstd::StringTrimRight("computer "), "computer"); | ||
ASSERT_EQ(pstd::StringTrimRight(" computer "), " computer"); | ||
ASSERT_EQ(pstd::StringTrimRight(" comp uter "), " comp uter"); | ||
ASSERT_EQ(pstd::StringTrimRight("comp uter "), "comp uter"); | ||
ASSERT_EQ(pstd::StringTrimRight(" \n computer \n ", "\n "), " \n computer"); | ||
ASSERT_EQ(pstd::StringTrimRight("\n", "\r\n "), ""); | ||
} | ||
|
||
TEST_F(StringTest, ParseIpPort) { | ||
std::string ip; | ||
int port; | ||
ASSERT_TRUE(pstd::ParseIpPortString("192.168.1.1:9221", ip, port)); | ||
ASSERT_EQ(ip, "192.168.1.1"); | ||
ASSERT_EQ(port, 9221); | ||
} | ||
|
||
TEST_F(StringTest, test_string2ll) { | ||
char buf[32]; | ||
long long v; | ||
|
||
/* May not start with +. */ | ||
strcpy(buf, "+1"); | ||
ASSERT_EQ(pstd::String2int(buf, strlen(buf), &v), 0); | ||
|
||
/* Leading space. */ | ||
strcpy(buf, " 1"); | ||
ASSERT_EQ(pstd::String2int(buf, strlen(buf), &v), 0); | ||
|
||
/* Trailing space. */ | ||
strcpy(buf, "1 "); | ||
ASSERT_EQ(pstd::String2int(buf, strlen(buf), &v), 1); | ||
|
||
strcpy(buf, "-1"); | ||
ASSERT_EQ(pstd::String2int(buf, strlen(buf), &v), 1); | ||
ASSERT_EQ(v, -1); | ||
|
||
strcpy(buf, "0"); | ||
ASSERT_EQ(pstd::String2int(buf, strlen(buf), &v), 1); | ||
ASSERT_EQ(v, 0); | ||
|
||
strcpy(buf, "1"); | ||
ASSERT_EQ(pstd::String2int(buf, strlen(buf), &v), 1); | ||
ASSERT_EQ(v, 1); | ||
|
||
strcpy(buf, "99"); | ||
ASSERT_EQ(pstd::String2int(buf, strlen(buf), &v), 1); | ||
ASSERT_EQ(v, 99); | ||
|
||
strcpy(buf, "-99"); | ||
ASSERT_EQ(pstd::String2int(buf, strlen(buf), &v), 1); | ||
ASSERT_EQ(v, -99); | ||
|
||
strcpy(buf, "-9223372036854775808"); | ||
ASSERT_EQ(pstd::String2int(buf, strlen(buf), &v), 1); | ||
ASSERT_EQ(v, LLONG_MIN); | ||
|
||
strcpy(buf, "-9223372036854775809"); /* overflow */ | ||
ASSERT_EQ(pstd::String2int(buf, strlen(buf), &v), 0); | ||
|
||
strcpy(buf, "9223372036854775807"); | ||
ASSERT_EQ(pstd::String2int(buf, strlen(buf), &v), 1); | ||
ASSERT_EQ(v, LLONG_MAX); | ||
|
||
strcpy(buf, "9223372036854775808"); /* overflow */ | ||
ASSERT_EQ(pstd::String2int(buf, strlen(buf), &v), 0); | ||
} | ||
|
||
TEST_F(StringTest, test_string2l) { | ||
char buf[32]; | ||
long v; | ||
|
||
/* May not start with +. */ | ||
strcpy(buf, "+1"); | ||
ASSERT_EQ(pstd::String2int(buf, &v), 0); | ||
|
||
strcpy(buf, "-1"); | ||
ASSERT_EQ(pstd::String2int(buf, strlen(buf), &v), 1); | ||
ASSERT_EQ(v, -1); | ||
|
||
strcpy(buf, "0"); | ||
ASSERT_EQ(pstd::String2int(buf, strlen(buf), &v), 1); | ||
ASSERT_EQ(v, 0); | ||
|
||
strcpy(buf, "1"); | ||
ASSERT_EQ(pstd::String2int(buf, strlen(buf), &v), 1); | ||
ASSERT_EQ(v, 1); | ||
|
||
strcpy(buf, "99"); | ||
ASSERT_EQ(pstd::String2int(buf, strlen(buf), &v), 1); | ||
ASSERT_EQ(v, 99); | ||
|
||
strcpy(buf, "-99"); | ||
ASSERT_EQ(pstd::String2int(buf, strlen(buf), &v), 1); | ||
ASSERT_EQ(v, -99); | ||
|
||
#if LONG_MAX != LLONG_MAX | ||
strcpy(buf, "-2147483648"); | ||
ASSERT_EQ(pstd::String2int(buf, strlen(buf), &v), 1); | ||
ASSERT_EQ(v, LONG_MIN); | ||
|
||
strcpy(buf, "-2147483649"); /* overflow */ | ||
ASSERT_EQ(pstd::String2int(buf, strlen(buf), &v), 0); | ||
|
||
strcpy(buf, "2147483647"); | ||
ASSERT_EQ(pstd::String2int(buf, strlen(buf), &v), 1); | ||
ASSERT_EQ(v, LONG_MAX); | ||
|
||
strcpy(buf, "2147483648"); /* overflow */ | ||
ASSERT_EQ(pstd::String2int(buf, strlen(buf), &v), 0); | ||
#endif | ||
} | ||
|
||
int main(int argc, char** argv) { | ||
::testing::InitGoogleTest(&argc, argv); | ||
return RUN_ALL_TESTS(); | ||
} |
Oops, something went wrong.