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

Неделин Дмитрий - Лабораторная Работа #2 #394

Open
wants to merge 5 commits into
base: main
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
2 changes: 1 addition & 1 deletion lab-guide/topics.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@
|---|---|---|
| [Игра "Жизнь" Конвея](https://github.com/garora/TDD-Katas/blob/master/KatasReadme.md#game-of-life-) |Кудинов Никита 3821Б1ПР1|Киселёв Игорь 3821Б1ПР1|
| [Правило 30](https://en.wikipedia.org/wiki/Rule_30) |||
| [Вычисление скидок на книги](https://github.com/garora/TDD-Katas/blob/master/KatasReadme.md#harry-potter-) |||
| [Вычисление скидок на книги](https://github.com/garora/TDD-Katas/blob/master/KatasReadme.md#harry-potter-) |Неделин Дмитрий 3821Б1ПР3|Неделин Дмитрий 3821Б1ПР3|
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
| [Вычисление скидок на книги](https://github.com/garora/TDD-Katas/blob/master/KatasReadme.md#harry-potter-) |Неделин Дмитрий 3821Б1ПР3|Неделин Дмитрий 3821Б1ПР3|
| [Вычисление скидок на книги](https://github.com/garora/TDD-Katas/blob/master/KatasReadme.md#harry-potter-) |Неделин Дмитрий 3821Б1ПР3|Неделин Дмитрий 3821Б1ПР3||

| [Перевод чисел в их словесное написание на английском](http://codingdojo.org/kata/NumbersInWords/) |||
| [Работа с числовыми промежутками](http://codingdojo.org/kata/Range/) |||
| [Реализовать печать цифр в псевдографике](https://github.com/garora/TDD-Katas/blob/master/KatasReadme.md#lcd-digits-) |||
Expand Down
12 changes: 12 additions & 0 deletions modules/nedelin_d_book_discount_lab2/CMakeLists.txt
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)
18 changes: 18 additions & 0 deletions modules/nedelin_d_book_discount_lab2/include/HarryPotterBooks.h
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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

struct?


#endif // MODULES_NEDELIN_D_BOOK_DISCOUNT_LAB2_INCLUDE_HARRYPOTTERBOOKS_H_
18 changes: 18 additions & 0 deletions modules/nedelin_d_book_discount_lab2/src/CMakeLists.txt
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)
26 changes: 26 additions & 0 deletions modules/nedelin_d_book_discount_lab2/src/HarryPotterBooks.cpp
Copy link
Collaborator

Choose a reason for hiding this comment

The 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;
}
27 changes: 27 additions & 0 deletions modules/nedelin_d_book_discount_lab2/test/CMakeLists.txt
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}")
Copy link
Collaborator

Choose a reason for hiding this comment

The 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);
}
7 changes: 7 additions & 0 deletions modules/nedelin_d_book_discount_lab2/test/test_main.cpp
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();
}
Loading