Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Diamond #74

Open
wants to merge 2 commits into
base: diamond
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,36 @@ cmake_minimum_required(VERSION 3.2)

set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
#set(CMAKE_CXX_COMPILER g++-10)

project(Cars)
include(FetchContent)
FetchContent_Declare(
googletest
GIT_REPOSITORY https://github.com/google/googletest.git
GIT_TAG main
)
# For Windows: Prevent overriding the parent project's compiler/linker settings
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(googletest)

set(SRC_LIST
Car.cpp
ElectricCar.cpp
ElectricEngine.cpp
HybridCar.cpp
main.cpp
PetrolCar.cpp
PetrolEngine.cpp
Car.cpp
)

add_executable(${PROJECT_NAME} ${SRC_LIST})
target_compile_options(${PROJECT_NAME} PUBLIC -Wall -Wpedantic -Wextra)
target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_SOURCE_DIR})
add_executable(${PROJECT_NAME} ${SRC_LIST} main.cpp)

#target_compile_options(${PROJECT_NAME} PUBLIC -Wall -Wpedantic -Wextra)
#target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_SOURCE_DIR})

add_executable(${PROJECT_NAME}-ut ${SRC_LIST} test.cpp)
target_link_libraries(${PROJECT_NAME}-ut gtest_main)

enable_testing()
add_test(NAME ${PROJECT_NAME}_test COMMAND ${PROJECT_NAME})
21 changes: 19 additions & 2 deletions Car.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,22 @@

void Car::turnLeft() { std::cout << __FUNCTION__ << std::endl; }
void Car::turnRight() { std::cout << __FUNCTION__ << std::endl; }
void Car::brake() { std::cout << __FUNCTION__ << std::endl; }
void Car::accelerate(int) { std::cout << __FUNCTION__ << std::endl; }

void Car::brake() {
speed_ = 0.0;
}
void Car::accelerate(int speed) {
if(speed < 0.0) {
throw std::range_error("Speed can't be below zero!");
}
if(speed > maxSpeed) {
throw std::range_error("Speed can't be over 300.0 km/h!");
}
speed_ = speed;
}
void Car::changeEngine(std::unique_ptr<PetrolEngine>) {
throw std::invalid_argument("This is not a petrol car!");
}
void Car::changeEngine(std::unique_ptr<ElectricEngine>) {
throw std::invalid_argument("This is not an electric car!");
}
23 changes: 22 additions & 1 deletion Car.hpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,33 @@
#pragma once
#include <exception>
#include <memory>
#include "ElectricEngine.hpp"
#include "PetrolEngine.hpp"

constexpr float maxSpeed = 300.0;

class Car
{
public:
virtual ~Car(){}

void turnLeft();
void turnRight();
void brake();
void accelerate(int speed);
virtual void refill() = 0;
virtual ~Car(){}
virtual void changeEngine(std::unique_ptr<PetrolEngine> enginePtr);
virtual void changeEngine(std::unique_ptr<ElectricEngine> enginePtr);

// getters
float getSpeed() const { return speed_; }

// execption
class CarMoving : public std::logic_error {
public:
CarMoving(const std::string &msg) :
std::logic_error(msg) {}
};
private:
float speed_;
};
13 changes: 8 additions & 5 deletions ElectricCar.cpp
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
#include "ElectricCar.hpp"
#include <iostream>

ElectricCar::ElectricCar(ElectricEngine* engine)
: engine_(engine)
ElectricCar::ElectricCar(std::unique_ptr<ElectricEngine> engine)
: engine_(std::move(engine))
{
std::cout << __FUNCTION__ << std::endl;
}

ElectricCar::~ElectricCar() { std::cout << __FUNCTION__ << std::endl; }
void ElectricCar::charge() { std::cout << __FUNCTION__ << std::endl; }
void ElectricCar::changeEngine(ElectricEngine* ee) {
delete engine_;
engine_ = ee;
void ElectricCar::changeEngine(std::unique_ptr<ElectricEngine> electricEngine) {
if(this->getSpeed() != 0.0) {
engine_ = std::move(electricEngine);
return;
}
throw Car::CarMoving{"Can't change an engine when car is moving!"};
}
void ElectricCar::refill() {
charge();
Expand Down
6 changes: 3 additions & 3 deletions ElectricCar.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
class ElectricCar : public virtual Car
{
public:
ElectricCar(ElectricEngine* engine);
ElectricCar(std::unique_ptr<ElectricEngine> engine);
~ElectricCar();

void changeEngine(ElectricEngine* ee);
void changeEngine(std::unique_ptr<ElectricEngine> electricEngine);
void refill() override;

private:
void charge();
ElectricEngine* engine_;
std::unique_ptr<ElectricEngine> engine_;
};
9 changes: 5 additions & 4 deletions HybridCar.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#include "HybridCar.hpp"
#include <iostream>

HybridCar::HybridCar(PetrolEngine* petrolEng, ElectricEngine* electricEng)
: ElectricCar(electricEng)
, PetrolCar(petrolEng)
HybridCar::HybridCar(std::unique_ptr<PetrolEngine> petrolEng, std::unique_ptr<ElectricEngine> electricEng)
: ElectricCar(std::move(electricEng))
, PetrolCar(std::move(petrolEng))
{
std::cout << __FUNCTION__ << std::endl;
}
Expand All @@ -12,4 +12,5 @@ HybridCar::~HybridCar() { std::cout << __FUNCTION__ << std::endl; }
void HybridCar::refill() {
ElectricCar::refill();
PetrolCar::refill();
}
}

2 changes: 1 addition & 1 deletion HybridCar.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
class HybridCar : public ElectricCar, public PetrolCar
{
public:
HybridCar(PetrolEngine* petrolEng, ElectricEngine* electricEng);
HybridCar(std::unique_ptr<PetrolEngine> petrolEng, std::unique_ptr<ElectricEngine> electricEng);
~HybridCar();
void refill() override;
};
6 changes: 6 additions & 0 deletions InvalidGear.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#pragma once
#include <stdexcept>

struct InvalidGear : public std::logic_error {
InvalidGear(std::string msg) : std::logic_error(msg) {}
};
18 changes: 13 additions & 5 deletions PetrolCar.cpp
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
#include "PetrolCar.hpp"
#include <iostream>

PetrolCar::PetrolCar(PetrolEngine* engine)
: engine_(engine)
PetrolCar::PetrolCar(std::unique_ptr<PetrolEngine> engine)
: engine_(std::move(engine))
{
std::cout << __FUNCTION__ << std::endl;
}

PetrolCar::~PetrolCar() { std::cout << __FUNCTION__ << std::endl; }
void PetrolCar::refuel() { std::cout << __FUNCTION__ << std::endl; }
void PetrolCar::changeEngine(PetrolEngine* pe) {
delete engine_;
engine_ = pe;

void PetrolCar::changeGear(int gear) {
engine_->changeGear(gear);
}
void PetrolCar::changeEngine(std::unique_ptr<PetrolEngine> petrolEngine) {
if(this->getSpeed() != 0.0) {
engine_ = std::move(petrolEngine);
return;
}
throw Car::CarMoving{"Can't change an engine when car is moving!"};
}

void PetrolCar::refill() {
refuel();
}
9 changes: 6 additions & 3 deletions PetrolCar.hpp
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
#pragma once
#include <memory>
#include <utility>
#include "PetrolEngine.hpp"
#include "Car.hpp"


class PetrolCar : virtual public Car
{
public:
PetrolCar(PetrolEngine* engine);
PetrolCar(std::unique_ptr<PetrolEngine> petrolEngine);
~PetrolCar();

void changeEngine(PetrolEngine* pe);
void changeGear(int gear);
void changeEngine(std::unique_ptr<PetrolEngine> petrolEngine);
void refill() override;

private:
void refuel();
PetrolEngine* engine_;
std::unique_ptr<PetrolEngine> engine_;
};
9 changes: 9 additions & 0 deletions PetrolEngine.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "PetrolEngine.hpp"
#include <iostream>
#include <string>


PetrolEngine::PetrolEngine(int power, float capacity, int gears)
: power_(power)
Expand All @@ -15,6 +17,13 @@ void PetrolEngine::changeGear(int gear)
// TODO: Add checking if gear is between -1 and gears_
// -1 is for REAR
// 0 is for NEUTRAL
if(gear < -1 || gear > gears_) {
throw InvalidGear("Invalid gear. Gear needs to be between 1 and " + std::to_string(gear));
}
if(gear != 0 && abs(gear - currentGear_) != 1) {
throw InvalidGear("Invalid gear change from: " + std::to_string(currentGear_)+ " to: " + std::to_string(gear));
}
currentGear_ = gear;

std::cout << __FUNCTION__ << std::endl;
}
2 changes: 2 additions & 0 deletions PetrolEngine.hpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#pragma once
#include "InvalidGear.hpp"

class PetrolEngine
{
public:

PetrolEngine(int power, float capacity, int gears);
void changeGear(int gear);

Expand Down
39 changes: 17 additions & 22 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,24 @@

int main()
{
// std::cout << std::endl << "OPEL" << std::endl;
// PetrolCar opel(new PetrolEngine(120, 1800, 6));
// opel.accelerate(50);
// opel.brake();
// opel.accelerate(-900);
// opel.refuel();

std::cout << std::endl << "OPEL" << std::endl;
PetrolCar opel(new PetrolEngine(120, 1800, 6));
Car* car = &opel;
car->accelerate(50);
car->brake();
car->accelerate(-900);
car->refill();
// car->changeEngine(new PetrolEngine(150, 700, 7));

std::cout << std::endl << "NISSAN" << std::endl;
ElectricCar nissan(new ElectricEngine(130, 650));
car = &nissan;
car->refill();
car->accelerate(80);
// std::cout << std::endl << "NISSAN" << std::endl;
// ElectricCar nissan(new ElectricEngine(130, 650));
// nissan.charge();
// nissan.accelerate(80);
// nissan.engine_ = new ElectricEngine(150, 700); // Changing an engine during driving is not safe
// car->changeEngine(new ElectricEngine(150, 700));
car->turnLeft();
// nissan.turnLeft();

std::cout << std::endl << "TOYOTA" << std::endl;
HybridCar toyota(new PetrolEngine(80, 1400, 5), new ElectricEngine(100, 540));
car = &toyota;
car->accelerate(100);
car->brake();
car->refill();
// std::cout << std::endl << "TOYOTA" << std::endl;
// HybridCar toyota(new PetrolEngine(80, 1400, 5), new ElectricEngine(100, 540));
// toyota.accelerate(100);
// toyota.brake();
// toyota.charge();
// toyota.refuel();
}
52 changes: 52 additions & 0 deletions test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include "gtest/gtest.h"
#include "Car.hpp"
#include "PetrolCar.hpp"
#include "ElectricCar.hpp"
#include "HybridCar.hpp"
#include "InvalidGear.hpp""


// Tests throwing std::range_error when accelerate to speed = -999 or speed > maxSpeed is invoked
TEST(PetrolCarClassTest, ShouldThrowRangeError) {
PetrolCar opel(std::make_unique<PetrolEngine>(120, 1800, 6));
EXPECT_THROW(opel.accelerate(-999), std::range_error);
EXPECT_THROW(opel.accelerate(350), std::range_error);

}
// Tests throwing InvalidGear when wrong gear is set
TEST(PetrolCarChangeGearTest, ShouldThrowIvalidGear) {
PetrolCar opel(std::make_unique<PetrolEngine>(120, 1800, 6));
EXPECT_THROW(opel.changeGear(-2), InvalidGear);
EXPECT_THROW(opel.changeGear(7), InvalidGear);
EXPECT_THROW(opel.changeGear(2), InvalidGear);
opel.changeGear(1);
EXPECT_THROW(opel.changeGear(5), InvalidGear);
}
// Tests if changeGear doesn't throw an exception
TEST(PetrolCarChangeGearTest, ShouldNotThrowException) {
PetrolCar opel(std::make_unique<PetrolEngine>(120, 1800, 6));
EXPECT_NO_THROW(opel.changeGear(1));
EXPECT_NO_THROW(opel.changeGear(2));
EXPECT_NO_THROW(opel.changeGear(3));
EXPECT_NO_THROW(opel.changeGear(2));
EXPECT_NO_THROW(opel.changeGear(1));
EXPECT_NO_THROW(opel.changeGear(0));
EXPECT_NO_THROW(opel.changeGear(-1));
}
// Tests throwing std::range_error when accelerate to speed = -999 or speed > maxSpeed is invoked
TEST(ElectricCarClassTest, ShouldThrowRangeError) {
ElectricCar nissan(std::make_unique<ElectricEngine>(130, 650));
EXPECT_THROW(nissan.accelerate(-999), std::range_error);
EXPECT_THROW(nissan.accelerate(350), std::range_error);
}
// Tests throwing std::invalid_argument when changing engine for the wrong one
TEST(CarClassPointerTest, ShouldThrowInvalidArgument) {
ElectricCar nissan(std::make_unique<ElectricEngine>(130, 650));
Car * car = &nissan;
// Electric car can't have petrol engine
EXPECT_THROW(car->changeEngine(std::make_unique<PetrolEngine>(150, 700, 7)), std::invalid_argument);
PetrolCar opel(std::make_unique<PetrolEngine>(120, 1800, 6));
car = &opel;
// Petrol car can't have electric engine
EXPECT_THROW(car->changeEngine(std::make_unique<ElectricEngine>(130, 650)), std::invalid_argument);
}