Skip to content

Commit

Permalink
Added Google Test for Utils rucio#15
Browse files Browse the repository at this point in the history
  • Loading branch information
viveknigam3003 committed May 24, 2020
1 parent 0dce9c1 commit 19436bb
Show file tree
Hide file tree
Showing 5 changed files with 200 additions and 81 deletions.
205 changes: 127 additions & 78 deletions source/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,188 +9,224 @@
#include <iostream>
#include <constants.h>


std::string to_string(char* contents, size_t size){
std::string s;
for (size_t i = 0; i < size; i++) {
s = s + contents[i];
}
return s;
std::string to_string(char *contents, size_t size)
{
std::string s;
for (size_t i = 0; i < size; i++)
{
s = s + contents[i];
}
return s;
}

// Returns true if and only if path is root path
bool is_root_path(const char *path){
return (strcmp(path, rucio_root_path.c_str()) == 0 || strcmp(path, (rucio_root_path+"/").c_str()) == 0);
bool is_root_path(const char *path)
{
return (strcmp(path, rucio_root_path.c_str()) == 0 || strcmp(path, (rucio_root_path + "/").c_str()) == 0);
}



// This function returns -1 if path contains no token
// 0 if path is root
// POSIX format depth in other cases
int path_depth(const char *path, const char token){
int path_depth(const char *path, const char token)
{
// If path is root return depth=0 immediately
if(is_root_path(path)) {
if (is_root_path(path))
{
return 0;
}
// Otherwise compute depth
else {
else
{
// Count token occurrences
auto s = path;
int16_t i = 0;
size_t size = 0;
for (i = 0; s[i];){
for (i = 0; s[i];)
{
size++;
if(s[i] == token) i++;
else s++;
if (s[i] == token)
i++;
else
s++;
}

// Remove one to count if just the last char is equal to token (spurious token)
// '/scope1' and '/scope1/' should behave the same
if(path[size-1] == token) i--;
if (path[size - 1] == token)
i--;
// Returns depth
return i;
}
}

bool is_server_mountpoint(const char *path){
bool is_server_mountpoint(const char *path)
{
return path_depth(path) == 1;
};

// This function returns true is the depth is 1 (e.g. /scope1 or /scope1/)
bool is_main_scope(const char *path){
bool is_main_scope(const char *path)
{
return path_depth(path) == 2;
}

std::vector<std::string> split(const std::string &s, char delim) {
std::vector<std::string> elems;
std::stringstream ss(s);
std::string key_or_value;
while(std::getline(ss, key_or_value, delim)) {
elems.emplace_back(key_or_value);
}
return elems;
std::vector<std::string> split(const std::string &s, char delim)
{
std::vector<std::string> elems;
std::stringstream ss(s);
std::string key_or_value;
while (std::getline(ss, key_or_value, delim))
{
elems.emplace_back(key_or_value);
}
return elems;
}

void remove_trailing_token(std::string& path, std::string token){
if(path.length() - 1 > 0) {
if (path.substr(path.length() - 1) == token) {
void remove_trailing_token(std::string &path, std::string token)
{
if (path.length() - 1 > 0)
{
if (path.substr(path.length() - 1) == token)
{
path.pop_back();
}
}
}

void remove_leading_token(std::string& path, std::string token){
// std::cout << "received: " << path <<std::endl;
if(path.length() > 0) {
if (path.substr(1) == token) {
void remove_leading_token(std::string &path, std::string token)
{
// std::cout << "received: " << path <<std::endl;
if (path.length() > 0)
{
if (path.substr(1) == token)
{
path.erase(path.begin());
}
}
}

std::string remove_substring(const std::string& path, const std::string& subs){
std::string remove_substring(const std::string &path, const std::string &subs)
{
auto path_copy = path;

// Search for the rucio root path with trailing "/"
size_t pos = path_copy.find(subs);
size_t pos = path_copy.find(subs);

if (pos != std::string::npos)
{
if (pos != std::string::npos)
{
// Erase root path from string
path_copy.erase(pos, subs.length());
}
path_copy.erase(pos, subs.length());
}

return path_copy;
return path_copy;
}

std::string remove_root_path(const std::string& path){
std::string remove_root_path(const std::string &path)
{
return remove_substring(path, rucio_root_path);
}

std::string extract_server_name(const std::string& path){
std::string extract_server_name(const std::string &path)
{
auto path_copy = remove_root_path(path);

size_t pos = path_copy.find('/');

if (pos != std::string::npos)
{
{
// Erase everything after the first "/"
path_copy.erase(pos, path_copy.length());
}
path_copy.erase(pos, path_copy.length());
}

return std::move(path_copy);
}

std::string extract_scope(const std::string& path){
std::string extract_scope(const std::string &path)
{
auto path_copy = remove_root_path(path);
remove_trailing_token(path_copy);
path_copy = remove_substring(path_copy, extract_server_name(path)+'/');
path_copy = remove_substring(path_copy, extract_server_name(path) + '/');

size_t pos = path_copy.find('/');

if (pos != std::string::npos)
{
{
// Erase everything after the first "/"
path_copy.erase(pos, path_copy.length());
}
path_copy.erase(pos, path_copy.length());
}

return std::move(path_copy);
}

std::string extract_name(const std::string& path){
std::string extract_name(const std::string &path)
{
auto path_copy = path;
remove_trailing_token(path_copy);

size_t pos = path_copy.find_last_of('/');

if (pos != std::string::npos)
{
{
// Erase everything after the first "/"
path_copy.erase(0, pos+1);
}
path_copy.erase(0, pos + 1);
}

return std::move(path_copy);
}

std::string get_did(const std::string& path){
return extract_scope(path)+":"+extract_name(path);
std::string get_did(const std::string &path)
{
return extract_scope(path) + ":" + extract_name(path);
}

void split_dids(const std::string &line, std::vector<std::string>& did_strings){
void split_dids(const std::string &line, std::vector<std::string> &did_strings)
{
did_strings.reserve(std::count(line.begin(), line.end(), '{'));

std::stringstream stream(line);
std::string buffer;
while(getline(stream, buffer, '\n')){
while (getline(stream, buffer, '\n'))
{
coherentize_dids(buffer);
if(not buffer.empty()) {
if (not buffer.empty())
{
did_strings.emplace_back(std::move(buffer));
}
}
}

void coherentize_dids(std::string &did_string){
if(did_string.back() != '}'){
if(did_string_remainder.empty()) {
void coherentize_dids(std::string &did_string)
{
if (did_string.back() != '}')
{
if (did_string_remainder.empty())
{
did_string_remainder = std::move(did_string);
} else {
}
else
{
did_string_remainder.append(std::move(did_string));
}
did_string = "";
}else if(did_string.front() != '{'){
}
else if (did_string.front() != '{')
{
did_string = did_string_remainder + did_string;
did_string_remainder = "";
}
}

void structurize_did(const std::string& did_str, std::vector<rucio_did>& target) {
void structurize_did(const std::string &did_str, std::vector<rucio_did> &target)
{
std::vector<std::string> did_strings_vect;
split_dids(did_str, did_strings_vect);

for (auto &sdid : did_strings_vect) {
for (auto &sdid : did_strings_vect)
{

for (const auto &ch : {' ', '}', '{', '"'}) {
for (const auto &ch : {' ', '}', '{', '"'})
{
sdid.erase(std::remove(sdid.begin(), sdid.end(), ch), sdid.end());
}

Expand All @@ -202,11 +238,16 @@ void structurize_did(const std::string& did_str, std::vector<rucio_did>& target)

did.scope = key_values[1];

if (key_values[3] == "FILE") {
if (key_values[3] == "FILE")
{
did.type = rucio_data_type::rucio_file;
} else if (key_values[3] == "CONTAINER") {
}
else if (key_values[3] == "CONTAINER")
{
did.type = rucio_data_type::rucio_container;
} else if (key_values[3] == "DATASET") {
}
else if (key_values[3] == "DATASET")
{
did.type = rucio_data_type::rucio_dataset;
}

Expand All @@ -218,12 +259,15 @@ void structurize_did(const std::string& did_str, std::vector<rucio_did>& target)
}
}

void structurize_container_did(const std::string& did_str, std::vector<rucio_did>& target){
void structurize_container_did(const std::string &did_str, std::vector<rucio_did> &target)
{
std::vector<std::string> did_strings_vect;
split_dids(did_str, did_strings_vect);

for (auto &sdid : did_strings_vect) {
for (const auto &ch : {' ', '}', '{', '"'}) {
for (auto &sdid : did_strings_vect)
{
for (const auto &ch : {' ', '}', '{', '"'})
{
sdid.erase(std::remove(sdid.begin(), sdid.end(), ch), sdid.end());
}

Expand All @@ -235,11 +279,16 @@ void structurize_container_did(const std::string& did_str, std::vector<rucio_did

did.scope = key_values[7];

if (key_values[9] == "FILE") {
if (key_values[9] == "FILE")
{
did.type = rucio_data_type::rucio_file;
} else if (key_values[9] == "CONTAINER") {
}
else if (key_values[9] == "CONTAINER")
{
did.type = rucio_data_type::rucio_container;
} else if (key_values[9] == "DATASET") {
}
else if (key_values[9] == "DATASET")
{
did.type = rucio_data_type::rucio_dataset;
}

Expand Down
2 changes: 1 addition & 1 deletion submodules/fastlog
2 changes: 1 addition & 1 deletion submodules/nlohmann_json
Submodule nlohmann_json updated 93 files
+3 −0 .circleci/config.yml
+3 −0 .clang-tidy
+6 −0 .github/CODEOWNERS
+5 −0 .github/SECURITY.md
+19 −0 .github/workflows/ccpp.yml
+23 −9 .travis.yml
+8 −0 .whitesource
+1 −1 CMakeLists.txt
+160 −4 ChangeLog.md
+3 −3 Makefile
+27 −9 README.md
+33 −18 appveyor.yml
+1 −1 doc/Doxyfile
+ doc/avatars.png
+1 −1 doc/examples/README.link
+1 −1 doc/examples/json_pointer__operator_add.cpp
+1 −1 doc/examples/json_pointer__operator_add.link
+1 −1 doc/examples/json_pointer__operator_add_binary.cpp
+1 −1 doc/examples/json_pointer__operator_add_binary.link
+3 −3 doc/examples/meta.output
+1 −1 doc/index.md
+ doc/json.gif
+1 −1 include/nlohmann/detail/conversions/from_json.hpp
+1 −1 include/nlohmann/detail/conversions/to_chars.hpp
+9 −6 include/nlohmann/detail/conversions/to_json.hpp
+2 −2 include/nlohmann/detail/input/binary_reader.hpp
+2 −2 include/nlohmann/detail/input/input_adapters.hpp
+2 −2 include/nlohmann/detail/iterators/iter_impl.hpp
+10 −4 include/nlohmann/detail/iterators/iteration_proxy.hpp
+2 −2 include/nlohmann/detail/json_pointer.hpp
+24 −24 include/nlohmann/detail/macro_scope.hpp
+13 −0 include/nlohmann/detail/meta/type_traits.hpp
+3 −4 include/nlohmann/detail/output/binary_writer.hpp
+27 −1 include/nlohmann/detail/output/serializer.hpp
+55 −8 include/nlohmann/json.hpp
+152 −62 include/nlohmann/thirdparty/hedley/hedley.hpp
+9 −2 include/nlohmann/thirdparty/hedley/hedley_undef.hpp
+1 −1 meson.build
+312 −121 single_include/nlohmann/json.hpp
+47 −4 test/CMakeLists.txt
+1 −0 test/cmake_import/CMakeLists.txt
+1 −0 test/cmake_import_minver/CMakeLists.txt
+1 −0 test/src/UBSAN.supp
+1 −1 test/src/fuzzer-driver_afl.cpp
+1 −1 test/src/fuzzer-parse_bson.cpp
+1 −1 test/src/fuzzer-parse_cbor.cpp
+1 −1 test/src/fuzzer-parse_json.cpp
+1 −1 test/src/fuzzer-parse_msgpack.cpp
+1 −1 test/src/fuzzer-parse_ubjson.cpp
+1 −1 test/src/unit-algorithms.cpp
+1 −1 test/src/unit-allocator.cpp
+36 −1 test/src/unit-alt-string.cpp
+3 −4 test/src/unit-bson.cpp
+1 −1 test/src/unit-capacity.cpp
+3 −3 test/src/unit-cbor.cpp
+1 −1 test/src/unit-class_const_iterator.cpp
+1 −1 test/src/unit-class_iterator.cpp
+1 −1 test/src/unit-class_lexer.cpp
+2 −2 test/src/unit-class_parser.cpp
+1 −1 test/src/unit-comparison.cpp
+1 −1 test/src/unit-concepts.cpp
+1 −1 test/src/unit-constructor1.cpp
+1 −1 test/src/unit-constructor2.cpp
+1 −1 test/src/unit-convenience.cpp
+1 −1 test/src/unit-conversions.cpp
+1 −1 test/src/unit-deserialization.cpp
+1 −1 test/src/unit-element_access1.cpp
+1 −1 test/src/unit-element_access2.cpp
+1 −1 test/src/unit-inspection.cpp
+1 −1 test/src/unit-items.cpp
+1 −1 test/src/unit-iterators1.cpp
+1 −1 test/src/unit-iterators2.cpp
+1 −1 test/src/unit-json_patch.cpp
+5 −1 test/src/unit-json_pointer.cpp
+49 −0 test/src/unit-large_json.cpp
+1 −1 test/src/unit-merge_patch.cpp
+3 −3 test/src/unit-meta.cpp
+1 −1 test/src/unit-modifiers.cpp
+3 −3 test/src/unit-msgpack.cpp
+1 −1 test/src/unit-noexcept.cpp
+1 −1 test/src/unit-pointer_access.cpp
+1 −1 test/src/unit-readme.cpp
+1 −1 test/src/unit-reference_access.cpp
+116 −22 test/src/unit-regression.cpp
+18 −1 test/src/unit-serialization.cpp
+1 −1 test/src/unit-testsuites.cpp
+3 −3 test/src/unit-to_chars.cpp
+1 −1 test/src/unit-ubjson.cpp
+2 −2 test/src/unit-udt.cpp
+2 −2 test/src/unit-unicode.cpp
+1 −1 test/src/unit-wstring.cpp
+1 −1 test/src/unit.cpp
+371 −455 test/thirdparty/doctest/doctest.h
15 changes: 14 additions & 1 deletion tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
cmake_minimum_required(VERSION 3.8)

add_executable(
test_curl-REST
test_curl-REST.cpp
Expand All @@ -14,4 +16,15 @@ add_executable(
test_REST-API
test_REST-API.cpp
)
target_link_libraries(test_REST-API PUBLIC rucio-rest-api-wrapper globals)
target_link_libraries(test_REST-API PUBLIC rucio-rest-api-wrapper globals)

# Locate GTest
find_package(GTest REQUIRED)
include_directories(${GTEST_INCLUDE_DIRS})

# Link gtest_utils with what we want to test and the GTest and pthread library
add_executable(
gtest_utils
gtest_utils.cpp)

target_link_libraries(gtest_utils PUBLIC utils ${GTEST_LIBRARIES} pthread)
Loading

0 comments on commit 19436bb

Please sign in to comment.