-
Notifications
You must be signed in to change notification settings - Fork 69
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
Неделин Дмитрий - Лабораторная Работа #2 #394
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# Declare variables for binaries' names | ||
get_filename_component(DIR_NAME ${CMAKE_CURRENT_LIST_DIR} NAME) | ||
set(MODULE "${DIR_NAME}") | ||
set(LIBRARY "lib_${MODULE}") | ||
set(TESTS "test_${MODULE}") | ||
|
||
# Include directory with public headers | ||
include_directories(${CMAKE_CURRENT_SOURCE_DIR}) | ||
|
||
# Add all submodules | ||
add_subdirectory(src) | ||
add_subdirectory(test) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// Copyright 2024 Nedelin Dmitry | ||
|
||
#ifndef MODULES_NEDELIN_D_BOOK_DISCOUNT_LAB2_INCLUDE_HARRYPOTTERBOOKS_H_ | ||
#define MODULES_NEDELIN_D_BOOK_DISCOUNT_LAB2_INCLUDE_HARRYPOTTERBOOKS_H_ | ||
|
||
#include <vector> | ||
#include <algorithm> | ||
#include <numeric> | ||
|
||
class HarryPotterBooks { | ||
public: | ||
double calculateTotalPrice(std::vector<int> books); | ||
|
||
const double bookPrice = 8.0; | ||
const std::vector<double> discounts = { 0.0, 0.0, 0.05, 0.1, 0.2, 0.25 }; | ||
}; | ||
Comment on lines
+10
to
+16
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. struct? |
||
|
||
#endif // MODULES_NEDELIN_D_BOOK_DISCOUNT_LAB2_INCLUDE_HARRYPOTTERBOOKS_H_ |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
set(target ${LIBRARY}) | ||
|
||
file(GLOB srcs "*.cpp") | ||
file(GLOB hdrs "../include/*.h") | ||
set_source_files_properties(${srcs} ${hdrs} PROPERTIES | ||
LABELS "${MODULE};Library") | ||
|
||
add_library(${target} STATIC ${srcs} ${hdrs}) | ||
set_target_properties(${target} PROPERTIES | ||
OUTPUT_NAME ${MODULE} | ||
LABELS "${MODULE};Library") | ||
|
||
if (UNIX) | ||
target_link_libraries(${target} ${CMAKE_THREAD_LIBS_INIT}) | ||
endif (UNIX) | ||
target_link_libraries(${target} ${LIBRARY_DEPS}) | ||
|
||
set(LIBRARY_DEPS "${LIBRARY_DEPS};${target}" PARENT_SCOPE) |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Обработайте случай, если на вход подается пустой вектор |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// Copyright 2024 Nedelin Dmitry | ||
|
||
#include "include/HarryPotterBooks.h" | ||
|
||
double HarryPotterBooks::calculateTotalPrice(std::vector<int> books) { | ||
double total = 0.0; | ||
|
||
while (true) { | ||
int uniqueBooks = std::count_if(books.begin(), | ||
books.end(), [](int x) { return x > 0; }); | ||
|
||
if (uniqueBooks == 0) { | ||
break; | ||
} | ||
|
||
total += uniqueBooks * bookPrice * (1 - discounts[uniqueBooks]); | ||
|
||
for (size_t i = 0; i < books.size(); ++i) { | ||
if (books[i] > 0) { | ||
books[i]--; | ||
} | ||
} | ||
} | ||
|
||
return total; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
set(target ${TESTS}) | ||
|
||
file(GLOB srcs "*.cpp") | ||
set_source_files_properties(${srcs} PROPERTIES | ||
LABELS "${MODULE};Test") | ||
|
||
add_executable(${target} ${srcs} ${hdrs}) | ||
set_target_properties(${target} PROPERTIES | ||
LABELS "${MODULE};Test") | ||
|
||
if (UNIX) | ||
target_link_libraries(${target} gtest ${CMAKE_THREAD_LIBS_INIT} pthread) | ||
endif (UNIX) | ||
target_link_libraries(${target} gtest ${LIBRARY}) | ||
|
||
# VS2012 doesn't support correctly the tuples yet, | ||
# see http://code.google.com/p/googletest/issues/detail?id=412 | ||
if(MSVC) | ||
target_compile_definitions(${target} PUBLIC _VARIADIC_MAX=10) | ||
endif() | ||
|
||
add_test( | ||
NAME ${MODULE}_gtest | ||
COMMAND ${target} | ||
) | ||
set_tests_properties (${MODULE}_gtest PROPERTIES | ||
LABELS "${MODULE}") |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Добавьте тест, который проверяет случай, когда приходит пустой вектор |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// Copyright 2024 Nedelin Dmitry | ||
|
||
#include <gtest/gtest.h> | ||
#include "include/HarryPotterBooks.h" | ||
|
||
TEST(Nedelin_HarryPotterBooksTest, SingleBook) { | ||
HarryPotterBooks hpBooks; | ||
std::vector<int> books = { 1, 0, 0, 0, 0 }; | ||
EXPECT_DOUBLE_EQ(hpBooks.calculateTotalPrice(books), 8.0); | ||
} | ||
|
||
TEST(Nedelin_HarryPotterBooksTest, TwoDifferentBooks) { | ||
HarryPotterBooks hpBooks; | ||
std::vector<int> books = { 1, 1, 0, 0, 0 }; | ||
EXPECT_DOUBLE_EQ(hpBooks.calculateTotalPrice(books), 2 * 8.0 * 0.95); | ||
} | ||
|
||
TEST(Nedelin_HarryPotterBooksTest, ThreeDifferentBooks) { | ||
HarryPotterBooks hpBooks; | ||
std::vector<int> books = { 1, 1, 1, 0, 0 }; | ||
EXPECT_DOUBLE_EQ(hpBooks.calculateTotalPrice(books), 8.0 * 3 * 0.9); | ||
} | ||
|
||
TEST(Nedelin_HarryPotterBooksTest, FourDifferentBooks) { | ||
HarryPotterBooks hpBooks; | ||
std::vector<int> books = { 1, 1, 1, 1, 0 }; | ||
EXPECT_DOUBLE_EQ(hpBooks.calculateTotalPrice(books), 8.0 * 4 * 0.8); | ||
} | ||
|
||
TEST(Nedelin_HarryPotterBooksTest, FullSetOfFiveBooks) { | ||
HarryPotterBooks hpBooks; | ||
std::vector<int> books = { 1, 1, 1, 1, 1 }; | ||
EXPECT_DOUBLE_EQ(hpBooks.calculateTotalPrice(books), 8.0 * 5 * 0.75); | ||
} | ||
|
||
TEST(Nedelin_HarryPotterBooksTest, ComplexBasket) { | ||
HarryPotterBooks hpBooks; | ||
std::vector<int> books = { 2, 2, 2, 1, 1 }; | ||
EXPECT_DOUBLE_EQ(hpBooks.calculateTotalPrice(books), 51.6); | ||
} | ||
|
||
TEST(Nedelin_HarryPotterBooksTest, NoBooks) { | ||
HarryPotterBooks hpBooks; | ||
std::vector<int> books = { 0, 0, 0, 0, 0 }; | ||
EXPECT_DOUBLE_EQ(hpBooks.calculateTotalPrice(books), 0.0); | ||
} | ||
|
||
TEST(Nedelin_HarryPotterBooksTest, MultipleSameBooks) { | ||
HarryPotterBooks hpBooks; | ||
std::vector<int> books = { 3, 0, 0, 0, 0 }; | ||
EXPECT_DOUBLE_EQ(hpBooks.calculateTotalPrice(books), 8.0 * 3); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
// Copyright 2024 Nedelin Dmitry | ||
#include <gtest/gtest.h> | ||
|
||
int main(int argc, char** argv) { | ||
::testing::InitGoogleTest(&argc, argv); | ||
return RUN_ALL_TESTS(); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.