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

Смирнов Павел - Лабораторная работа №3 #405

Merged
8 changes: 8 additions & 0 deletions modules/nedelin_d_book_discount_lab2/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,18 @@ get_filename_component(DIR_NAME ${CMAKE_CURRENT_LIST_DIR} NAME)
set(MODULE "${DIR_NAME}")
set(LIBRARY "lib_${MODULE}")
set(TESTS "test_${MODULE}")
set(APPLICATION "app_${MODULE}")

# Include directory with public headers
include_directories(${CMAKE_CURRENT_SOURCE_DIR})

# Add all submodules
add_subdirectory(src)
add_subdirectory(test)
add_subdirectory(application)

#############################################
##### Testing
#############################################

include("CTestTests.txt")
69 changes: 69 additions & 0 deletions modules/nedelin_d_book_discount_lab2/CTestTests.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#############################################
##### Testing
#############################################

set(prefix "${MODULE}")

# Базовый тест: проверка запуска
add_test(
NAME ${prefix}_can_Run
COMMAND ${APPLICATION} 8.0 1 1 1 1 1
)
set_tests_properties(${prefix}_can_Run PROPERTIES LABELS "${MODULE}")

add_test(
NAME ${prefix}_insufficient_arguments
COMMAND ${APPLICATION} 8.0 1 1 1 1
)
set_tests_properties(${prefix}_insufficient_arguments PROPERTIES
PASS_REGULAR_EXPRESSION "Not enough values. Please provide 6 numbers for the book price, counts"
LABELS "${MODULE}")

add_test(
NAME ${prefix}_valid_arguments_one_book
COMMAND ${APPLICATION} 8.0 1 0 0 0 0
)
set_tests_properties(${prefix}_valid_arguments_one_book PROPERTIES
PASS_REGULAR_EXPRESSION "Total price: 8"
LABELS "${MODULE}")

add_test(
NAME ${prefix}_valid_arguments_two_books
COMMAND ${APPLICATION} 8.0 1 1 0 0 0
)
set_tests_properties(${prefix}_valid_arguments_two_books PROPERTIES
PASS_REGULAR_EXPRESSION "Total price: 15.2"
LABELS "${MODULE}")

add_test(
NAME ${prefix}_valid_arguments_three_books
COMMAND ${APPLICATION} 8.0 1 1 1 0 0
)
set_tests_properties(${prefix}_valid_arguments_three_books PROPERTIES
PASS_REGULAR_EXPRESSION "Total price: 21.6"
LABELS "${MODULE}")

add_test(
NAME ${prefix}_valid_arguments_four_books
COMMAND ${APPLICATION} 8.0 1 1 1 1 0
)
set_tests_properties(${prefix}_valid_arguments_four_books PROPERTIES
PASS_REGULAR_EXPRESSION "Total price: 25.6"
LABELS "${MODULE}")

add_test(
NAME ${prefix}_valid_arguments_five_books
COMMAND ${APPLICATION} 8.0 1 1 1 1 1
)
set_tests_properties(${prefix}_valid_arguments_five_books PROPERTIES
PASS_REGULAR_EXPRESSION "Total price: 30"
LABELS "${MODULE}")

# Тест для случая, когда количество книг одного вида больше 5
add_test(
NAME ${prefix}_more_than_five_books_one_type
COMMAND ${APPLICATION} 8.0 6 0 0 0 0
)
set_tests_properties(${prefix}_more_than_five_books_one_type PROPERTIES
PASS_REGULAR_EXPRESSION "Total price: 48"
LABELS "${MODULE}")
15 changes: 15 additions & 0 deletions modules/nedelin_d_book_discount_lab2/application/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
set(target ${APPLICATION})

file(GLOB srcs "*.cpp")
set_source_files_properties(${srcs} PROPERTIES
LABELS "${MODULE};Application")

add_executable(${target} ${srcs})
set_target_properties(${target} PROPERTIES
OUTPUT_NAME ${MODULE}
LABELS "${MODULE};Application")

target_link_libraries(${target} ${LIBRARY})
if (UNIX)
target_link_libraries(${target} ${CMAKE_THREAD_LIBS_INIT})
endif (UNIX)
14 changes: 14 additions & 0 deletions modules/nedelin_d_book_discount_lab2/application/application.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright 2024 Смирнов Павел

#include <iostream>

#include "include/HarryPotterBooksApp.h"

int main(int argc, char *argv[]) {
HarryPotterBooksApp app;
auto output = app.run(argc, argv);

std::cout << output << '\n';

return 0;
}
16 changes: 16 additions & 0 deletions modules/nedelin_d_book_discount_lab2/include/HarryPotterBooksApp.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright 2024 Смирнов Павел
#ifndef MODULES_NEDELIN_D_BOOK_DISCOUNT_LAB2_INCLUDE_HARRYPOTTERBOOKSAPP_H_
#define MODULES_NEDELIN_D_BOOK_DISCOUNT_LAB2_INCLUDE_HARRYPOTTERBOOKSAPP_H_

#include <vector>
#include <string>

class HarryPotterBooksApp {
public:
std::string run(int argc, char* argv[]);

private:
std::string calculatePrice(double price, const std::vector<int>& books);
};

#endif // MODULES_NEDELIN_D_BOOK_DISCOUNT_LAB2_INCLUDE_HARRYPOTTERBOOKSAPP_H_
34 changes: 34 additions & 0 deletions modules/nedelin_d_book_discount_lab2/src/HarryPotterBooksApp.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright 2024 Smirnov Pavel
#include <sstream>
#include <iostream>
#include <string>
#include <vector>

#include "include/HarryPotterBooksApp.h"
#include "include/HarryPotterBooks.h"


std::string HarryPotterBooksApp::calculatePrice(
double price, const std::vector<int>& books) {
HarryPotterBooks hpBooks(price, books);
double totalPrice = hpBooks.calculateTotalPrice();
std::stringstream ss;
ss << "Total price: " << totalPrice;
return ss.str();
}

std::string HarryPotterBooksApp::run(int argc, char* argv[]) {
std::vector<int> books;
double price;
if (argc < 7) {
return "Not enough values. Please provide 6 numbers for the book price, counts";
}
for (int i = 1; i < 7; ++i) {
if (i == 1) {
price = std::stoi(argv[i]);
continue;
}
books.push_back(std::stoi(argv[i]));
}
return calculatePrice(price, books);
}
Loading