Skip to content

Commit

Permalink
Merge pull request #10 from felixschurk/1-add-createdrivercallsignature
Browse files Browse the repository at this point in the history
Adding createdrivercallsignature and getAssociationByNameSignature
  • Loading branch information
felixschurk authored Dec 12, 2023
2 parents f52986d + 1225ffa commit fdc8926
Show file tree
Hide file tree
Showing 6 changed files with 208 additions and 6 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ compile_commands.json
# IDE stuff
*.idea
*.cache
*.vscode

# Prerequisites
*.d
Expand Down
81 changes: 81 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
{
"files.associations": {
"any": "cpp",
"array": "cpp",
"atomic": "cpp",
"bit": "cpp",
"*.tcc": "cpp",
"bitset": "cpp",
"cctype": "cpp",
"cfenv": "cpp",
"charconv": "cpp",
"chrono": "cpp",
"cinttypes": "cpp",
"clocale": "cpp",
"cmath": "cpp",
"codecvt": "cpp",
"compare": "cpp",
"concepts": "cpp",
"condition_variable": "cpp",
"csignal": "cpp",
"cstdarg": "cpp",
"cstddef": "cpp",
"cstdint": "cpp",
"cstdio": "cpp",
"cstdlib": "cpp",
"cstring": "cpp",
"ctime": "cpp",
"cwchar": "cpp",
"cwctype": "cpp",
"deque": "cpp",
"forward_list": "cpp",
"list": "cpp",
"map": "cpp",
"set": "cpp",
"string": "cpp",
"unordered_map": "cpp",
"unordered_set": "cpp",
"vector": "cpp",
"exception": "cpp",
"algorithm": "cpp",
"functional": "cpp",
"iterator": "cpp",
"memory": "cpp",
"memory_resource": "cpp",
"numeric": "cpp",
"optional": "cpp",
"random": "cpp",
"ratio": "cpp",
"regex": "cpp",
"string_view": "cpp",
"system_error": "cpp",
"tuple": "cpp",
"type_traits": "cpp",
"utility": "cpp",
"format": "cpp",
"fstream": "cpp",
"future": "cpp",
"initializer_list": "cpp",
"iomanip": "cpp",
"iosfwd": "cpp",
"iostream": "cpp",
"istream": "cpp",
"limits": "cpp",
"mutex": "cpp",
"new": "cpp",
"numbers": "cpp",
"ostream": "cpp",
"scoped_allocator": "cpp",
"semaphore": "cpp",
"span": "cpp",
"sstream": "cpp",
"stdexcept": "cpp",
"stop_token": "cpp",
"streambuf": "cpp",
"thread": "cpp",
"typeindex": "cpp",
"typeinfo": "cpp",
"valarray": "cpp",
"variant": "cpp"
}
}
2 changes: 2 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ add_library(libutilities

target_link_libraries(libutilities
absl::strings
absl::algorithm
)


add_executable(utilities.test
utilities.test.cpp
)
Expand Down
47 changes: 43 additions & 4 deletions src/utilities.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@
#include <vector>

#include "absl/strings/str_split.h"

#include "utilities.hpp"
#include "absl/strings/str_cat.h"
#include "absl/algorithm/container.h"

std::string setSeedValue(std::string variable, std::string type_of_variable, std::string value_for_seeding)
{
throw std::runtime_error("Not implemented yet");
}


std::string getTypeOfVariable(const std::string &callSignature, const std::string &variableName)
{
std::vector<std::string> callSignatureSplitted = absl::StrSplit(callSignature, absl::ByAnyChar(" ,("), absl::SkipEmpty());
Expand Down Expand Up @@ -40,10 +39,50 @@ std::string getTypeOfVariable(const std::string &callSignature, const std::strin
}

std::string createLoopSignature(const std::string &activeVariable, int level) {
// create based on the level a multiplicaiton of i
// create based on the level a multiplication of i
char loopVariableChar = 'i';
std::string loopVariable;
for (auto i = 0; i < level; ++i){ loopVariable += loopVariableChar; }
std::string loopSignature = "for (size_t " + loopVariable + " = 0; " + loopVariable + " < " + activeVariable + ".size(); ++" + loopVariable + ")";
return loopSignature;
}

std::string getAssociationByNameSignatureCompute(const std::string &callSignature, const std::string &activeVariable){

// creates the function call with the _t at the end
std::vector<std::string> splittedCallSignature = absl::StrSplit(callSignature, absl::ByAnyChar(" ,()"), absl::SkipEmpty());
std::string functionCall = absl::StrCat(splittedCallSignature[1], "_t(");

// separates the arguments from the call signature and organize in a vector
std::vector<std::string> callSignatureArguments = absl::StrSplit(callSignature, absl::ByAnyChar(",()"), absl::SkipEmpty());
callSignatureArguments.erase(callSignatureArguments.begin());

// creates a vector with the active variables
std::vector<std::string> activeVariables = absl::StrSplit(activeVariable, absl::ByAnyChar(" ,"), absl::SkipEmpty());
std::vector<std::string> words;

// checks which arguments from call signature are active variables or parameters
for (int i = 0; i < callSignatureArguments.size(); i++) {
words = absl::StrSplit(callSignatureArguments[i], absl::ByAnyChar(" &"), absl::SkipEmpty());
functionCall = absl::StrCat(functionCall, words.back());

if (absl::c_linear_search(activeVariables, words.back())) {
functionCall = absl::StrCat(functionCall, ", ", words.back(), "_t");
}

if (i < callSignatureArguments.size() - 1) {
functionCall = absl::StrCat(functionCall, ", ");
} else {
functionCall = absl::StrCat(functionCall, ")");
}
}

return functionCall;
}

std::string createDriverCallSignature(const std::string &callSignature, const std::string &driver_type){
std::vector<std::string> splittedCallSignature = absl::StrSplit(callSignature, absl::ByAnyChar(" ,()"), absl::SkipEmpty());
std::vector<std::string> driverType = absl::StrSplit(driver_type, absl::ByAnyChar(" ,()"), absl::SkipEmpty());
std::string driverCallSignature = absl::StrCat(splittedCallSignature[0]," ", splittedCallSignature[1], "_", driverType[0]);
return driverCallSignature;
}
3 changes: 2 additions & 1 deletion src/utilities.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ std::string getTypeOfVariable(const std::string &callSignature, const std::strin

std::string createLoopSignature(const std::string &activeVariable, int level);

std::string getAssociationByNameSignature(std::string call_signature, std::string active_variable);
std::string getAssociationByNameSignatureCompute(const std::string &callSignature, const std::string &activeVariable);

std::string createDriverCallSignature(const std::string &callSignature, const std::string &driver_type);

#endif //SISC_LAB_UTILITIES_HPP
80 changes: 79 additions & 1 deletion src/utilities.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ TEST(GetTypeOfVariable, Variable_Passed_By_Reference)
EXPECT_EQ(actual, expected);
}

TEST(GetTypeOfVariable, Variable_Passed_By_Reference_Whitespaces_Seprated)
TEST(GetTypeOfVariable, Variable_Passed_By_Reference_Whitespaces_Separated)
{
// SETUP
std::string signature{"float & a"};
Expand Down Expand Up @@ -138,3 +138,81 @@ TEST(CreateLoopSignature, Loop_with_level_2)
// ASSERT
EXPECT_EQ(actual, expected);
}

TEST(GetAssociationByNameComputeSignature, OneVariableOneParameter){
// SETUP
std::string call_signature{"void f(double &x, const double p)"};
std::string active_variables{"x"};
std::string expected{"f_t(x, x_t, p)"};

// ACT
auto actual = getAssociationByNameSignatureCompute(call_signature, active_variables);

// ASSERT
EXPECT_EQ(actual, expected);
}

TEST(GetAssociationByNameComputeSignature, TwoVariables){
// SETUP
std::string call_signature{"void f(double &x, double &y)"};
std::string active_variables{"x, y"};
std::string expected{"f_t(x, x_t, y, y_t)"};

// ACT
auto actual = getAssociationByNameSignatureCompute(call_signature, active_variables);

// ASSERT
EXPECT_EQ(actual, expected);
}

TEST(GetAssociationByNameComputeSignature, TwoVariablesOneParameter){
// SETUP
std::string call_signature{"void f(double &x, double &y, const double p)"};
std::string active_variables{"x, y"};
std::string expected{"f_t(x, x_t, y, y_t, p)"};

// ACT
auto actual = getAssociationByNameSignatureCompute(call_signature, active_variables);

// ASSERT
EXPECT_EQ(actual, expected);
}

TEST(GetAssociationByNameComputeSignature, VectorVariable){
// SETUP
std::string call_signature{"void f(std::vector<double> &x)"};
std::string active_variables{"x"};
std::string expected{"f_t(x, x_t)"};

// ACT
auto actual = getAssociationByNameSignatureCompute(call_signature, active_variables);

// ASSERT
EXPECT_EQ(actual, expected);
}

TEST(CreateDriverCallSignature, GradientDriver){
// SETUP
std::string call_signature{"void f(double &x, double &y)"};
std::string driver_type{"gradient"};
std::string expected{"void f_gradient"};

// ACT
auto actual = createDriverCallSignature(call_signature, driver_type);

// ASSERT
EXPECT_EQ(actual, expected);
}

TEST(CreateDriverCallSignature, JacobianDriver){
// SETUP
std::string call_signature{"void fxfts(double &x, double &y)"};
std::string driver_type{"jacobian"};
std::string expected{"void fxfts_jacobian"};

// ACT
auto actual = createDriverCallSignature(call_signature, driver_type);

// ASSERT
EXPECT_EQ(actual, expected);
}

0 comments on commit fdc8926

Please sign in to comment.