So after I included the function parse_input
I always get a segfault error. I have tried many things but none work. My current code is:
test.cpp
#include "gtest/gtest.h"
#include "Client.h"
#include "gmock/gmock.h"
using namespace ::testing;
TEST(ClientInputParse, RegularWeekdays) {
std::vector<Days> days = {Days::Mon, Days::Tues, Days::Wed};
auto client = Client();
client.parse_input("Regular: 16Mar2009(mon), 17Mar2009(tues), 18Mar2009(wed)");
Client expected(ClientType::Regular, days);
ASSERT_EQ(client, expected);
}
header:
#ifndef Client_h
#define Client_h
#include <vector>
#include <string>
#include <sstream>
#include <functional>
enum ClientType {
Regular,
Rewards,
};
enum Days {
Mon = 0,
Tues = 1,
Wed = 2,
Thur = 3,
Fri = 4,
Sat = 5,
Sun = 6,
};
class UnknownDayOfTheWeek
{
public:
UnknownDayOfTheWeek(){};
};
class UnknownClient
{
public:
UnknownClient(){};
};
struct Client {
std::vector<Days> days_;
ClientType type_;
public:
Client(ClientType client_type, std::vector<Days> days) : days_(days), type_(client_type) {};
Client() {};
void parse_input(const std::string input);
};
bool operator==(const Client& lhs, const Client& rhs);
#endif
impl:
#include "Client.h"
ClientType parse_client_type(const std::string& client_type_input);
std::vector<Days> parse_client_days(const std::string& client_days_input);
void Client::parse_input(const std::string input) {
std::string client_type_input = input.substr(0, input.find(":"));
std::string client_days_input = input.substr(input.find(":"));
ClientType client_type = parse_client_type(client_type_input);
std::vector<Days> days = parse_client_days(client_days_input);
type_ = client_type;
days_ = days;
}
ClientType parse_client_type(const std::string& client_type_input) {
if (client_type_input == "Regular") {
return ClientType::Regular;
} else if (client_type_input == "Rewards") {
return ClientType::Rewards;
} else {
throw UnknownClient();
}
}
std::vector<Days> parse_client_days(const std::string& client_days_input) {
std::vector<Days> days;
std::vector<std::string> days_tokens;
std::stringstream ss(client_days_input);
std::string intermediate;
while(getline(ss, intermediate, ','))
{
days_tokens.push_back(intermediate);
}
std::transform(days_tokens.begin(), days_tokens.end(), days.begin(), [](std::string token) {
if (token.find("mon") != std::string::npos) {
return Days::Mon;
} else if (token.find("tues") != std::string::npos) {
return Days::Tues;
} else if (token.find("wed") != std::string::npos) {
return Days::Tues;
} else if (token.find("thur") != std::string::npos) {
return Days::Tues;
} else if (token.find("fri") != std::string::npos) {
return Days::Tues;
} else if (token.find("sat") != std::string::npos) {
return Days::Tues;
} else if (token.find("sun") != std::string::npos) {
return Days::Tues;
}
throw UnknownDayOfTheWeek();
});
return days;
}
bool operator==(const Client& lhs, const Client& rhs) {
return lhs.type_ == rhs.type_ && (lhs.days_.size() == rhs.days_.size() &&
std::equal(lhs.days_.begin(), lhs.days_.end(), rhs.days_.begin()));
}
Terminal output:
[----------] 1 test from ClientInputParse
[ RUN ] ClientInputParse.RegularWeekdays
zsh: segmentation fault ./test
Aucun commentaire:
Enregistrer un commentaire