debut de test (mais fonctionne pas encore a cause de == overloads

This commit is contained in:
eleonore12345 2024-08-05 16:32:18 +02:00
parent 20a144eea1
commit c8f976e272
8 changed files with 257 additions and 0 deletions

View File

@ -0,0 +1,20 @@
ECHO = @echo
GCC = g++
CCFLAGS = -c -O3 -g -std=c++11 -pedantic -Wall
SRC = $(wildcard *.cpp)
OBJECTS = $(SRC:.cpp=.o)
EXE = test
$(EXE) : $(OBJECTS)
$(ECHO) "-Linking $(EXE)-"
$(GCC) -o $@ $^
$(OBJECTS):$(SRC)
$(ECHO) "-Compilation $<- "
$(GCC) $(CCFLAGS) -o $@ $<
.PHONY: clean
clean:
$(ECHO) "-Cleaning-"
$(RM) $(OBJECTS) $(EXE)

View File

@ -0,0 +1,41 @@
//deployer Service implementation
// Copyright (C) 2024 Jean-Cloud
// GNU General Public License v3
#include "Service.h"
//constructor and destructor defined inline
//public methods
int Service::getUserID () const
{
return userID;
}
string Service::getUsername() const
{
return username;
}
list<string> Service::getServers() const
{
return servers;
}
//operator == override
bool Service::operator == (const Service & service) const
{
if (service.getServers()==servers && service.getUserID()==userID && service.getUsername()==username){
return true;
}else{
return false;
}
}
//operator << override
ostream & operator << (ostream & out, const Service & s)
{
out << "userID: " << s.userID << endl << "username: " << s.username << endl << "servers: ";
for (string server : s.servers){
out << server << " " << endl;
}
return out;
}

View File

@ -0,0 +1,29 @@
//deployer Service header
// Copyright (C) 2024 Jean-Cloud
// GNU General Public License v3
#if !defined(SERVICE_H)
#define SERVICE_H
#include <iostream>
#include <string>
#include <string>
#include <list>
using namespace std;
class Service {
public:
Service(int aUserID, string aUsername, list <string> aServers):userID(aUserID),username(aUsername),servers(aServers){}
~Service(){}
int getUserID () const;
string getUsername() const;
list<string> getServers() const;
bool operator == (const Service & service) const;
friend ostream & operator<<(ostream & out, const Service & s);
private:
int userID;
string username;
list <string> servers;
};
#endif

View File

@ -0,0 +1,92 @@
// deployer Services implementation
// Copyright (C) 2024 Jean-Cloud
// GNU General Public License v3
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <string>
#include "Services.h"
//constructor
Services::Services(string ServicesCSV)
{
services=readServicesFromCSV(ServicesCSV);
}
//destructor
Services::~Services(){}
//public methods
vector<Service> Services::getServices()const
{
return services;
}
const Service* Services::findByUsername(string aUsername) const
{
for (const Service & service : services){
if (service.getUsername().compare(aUsername)==0){
return &service;
}
}
return nullptr;
}
const Service * Services::findByID(int aUserID) const
{
for (const Service & service : services){
if (service.getUserID()==aUserID){
return &service;
}
}
return nullptr;
}
list<const Service*> Services::findByServer(string aServer) const
{
list<const Service*> result;
for (const Service & service : services){
for (string server : service.getServers()){
if(server.compare(aServer)==0){
result.push_back(&service);
}
}
}
return result;
}
//private methods
vector <Service> Services::readServicesFromCSV (string CSV) const
{
//this method extracts the list of uid|username|servers from the services.csv file
//and returns them in a vector <Service>, with Service a structure defined in the header
vector <Service> result;
ifstream streamServices(CSV);
if (!streamServices){
cout << "Invalid services.csv file." << endl;
}else{
string line;
string tmpUserID; //used before converting to int
int userID;
string username;
string server;
list <string> servers;
while(getline(streamServices,line)){
if (line.empty() || line[0] == '#') { //not taking comments and empty lines into account
continue;
}
stringstream streamLine(line);
getline(streamLine,tmpUserID,';'); //extracting the userID
userID=stoi(tmpUserID);
getline(streamLine,username,';'); //extracting the username
while(getline(streamLine,server,';')){ //extracting the server(s)
servers.push_back(server);
}
Service entry = {userID,username,servers};
result.push_back(entry);
}
}
return result;
}

View File

@ -0,0 +1,31 @@
// deployer Services header
// Copyright (C) 2024 Jean-Cloud
// GNU General Public License v3
#if !defined(SERVICES_H)
#define SERVICES_H
#include <iostream>
#include <cstring>
#include <list>
#include <vector>
#include "Service.h"
using namespace std;
class Services
//extracts the list of uid|username|service from the services.csv file
{
public:
Services(string servicesCSV="../src/services.csv");
vector<Service> getServices() const;
const Service * findByUsername(string aUsername) const;
const Service * findByID(int aUserID) const;
list<const Service*> findByServer(string aServer) const;
~Services();
private:
vector <Service> readServicesFromCSV (string CSV) const;
vector <Service> services;
};
#endif

View File

@ -0,0 +1,35 @@
//test file for class Services
#include "Services.h"
int main()
{
Services aServices = Services("testSample.csv");
vector <Service> result = aServices.getServices();
vector <Service> expectedResult;
list <string> s1={"server1","server1bis"};
list <string> s3={"server3"};
list <string> s4={""};
list <string> s5={""};
list <string> s6={""};
list <string> s7={""};
list <string> s8={"server8","server8bis","server8ter"};
expectedResult.push_back(Service(1,"username1",s1));
expectedResult.push_back(Service(3,"username3",s3));
expectedResult.push_back(Service(4,"username4",s4));
expectedResult.push_back(Service(5,"username5",s5));
expectedResult.push_back(Service(6,"",s6));
expectedResult.push_back(Service(7,"username7",s7));
if(expectedResult==result){
cout << "Services test: OK" << endl;
return 0;
}else {
cout << "Services test: FAIL" << endl;
return 1;
}
}

View File

@ -0,0 +1,9 @@
1;username1;server1;server1bis
#2;comment;comment
3;username3;server3
4;username4
5;username5;
6;;;
7;username7
1 1;username1;server1;server1bis
2 #2;comment;comment
3 3;username3;server3
4 4;username4
5 5;username5;
6 6;;;
7 7;username7

BIN
test/deployer Executable file

Binary file not shown.