diff --git a/tdd_intro/homework/06.Allergies/06.Allergies - Shortcut.lnk b/tdd_intro/homework/06.Allergies/06.Allergies - Shortcut.lnk new file mode 100644 index 0000000..c61bfb0 Binary files /dev/null and b/tdd_intro/homework/06.Allergies/06.Allergies - Shortcut.lnk differ diff --git a/tdd_intro/homework/06.Allergies/06.Allergies.pro b/tdd_intro/homework/06.Allergies/06.Allergies.pro new file mode 100644 index 0000000..ab8c016 --- /dev/null +++ b/tdd_intro/homework/06.Allergies/06.Allergies.pro @@ -0,0 +1,9 @@ +include(../../gtest.pri) + +TEMPLATE = app +CONFIG += console c++11 +CONFIG -= app_bundle +CONFIG -= qt + +SOURCES += test.cpp + diff --git a/tdd_intro/homework/06.Allergies/06.Allergies.pro.user b/tdd_intro/homework/06.Allergies/06.Allergies.pro.user new file mode 100644 index 0000000..9ecb317 --- /dev/null +++ b/tdd_intro/homework/06.Allergies/06.Allergies.pro.user @@ -0,0 +1,318 @@ + + + + + + EnvironmentId + {d40f95ee-3fbc-4856-b5dc-5ba27a967b64} + + + ProjectExplorer.Project.ActiveTarget + 0 + + + ProjectExplorer.Project.EditorSettings + + true + false + true + + Cpp + + CppGlobal + + + + QmlJS + + QmlJSGlobal + + + 2 + UTF-8 + false + 4 + false + 80 + true + true + 1 + true + false + 0 + true + true + 0 + 8 + true + 1 + true + true + true + false + + + + ProjectExplorer.Project.PluginSettings + + + + ProjectExplorer.Project.Target.0 + + Desktop Qt 5.11.0 MSVC2015 32bit + Desktop Qt 5.11.0 MSVC2015 32bit + qt.qt5.5110.win32_msvc2015_kit + 0 + 0 + 0 + + C:/MyWork/self_education/TDD/tdd-course-2/tdd_intro/homework/build-06.Allergies-Desktop_Qt_5_11_0_MSVC2015_32bit-Debug + + + true + qmake + + QtProjectManager.QMakeBuildStep + true + + false + false + false + + + true + Make + + Qt4ProjectManager.MakeStep + + false + + + + 2 + Build + + ProjectExplorer.BuildSteps.Build + + + + true + Make + + Qt4ProjectManager.MakeStep + + true + clean + + + 1 + Clean + + ProjectExplorer.BuildSteps.Clean + + 2 + false + + Debug + Debug + Qt4ProjectManager.Qt4BuildConfiguration + 2 + true + + + C:/MyWork/self_education/TDD/tdd-course-2/tdd_intro/homework/build-06.Allergies-Desktop_Qt_5_11_0_MSVC2015_32bit-Release + + + true + qmake + + QtProjectManager.QMakeBuildStep + false + + false + false + false + + + true + Make + + Qt4ProjectManager.MakeStep + + false + + + + 2 + Build + + ProjectExplorer.BuildSteps.Build + + + + true + Make + + Qt4ProjectManager.MakeStep + + true + clean + + + 1 + Clean + + ProjectExplorer.BuildSteps.Clean + + 2 + false + + Release + Release + Qt4ProjectManager.Qt4BuildConfiguration + 0 + true + + + C:/MyWork/self_education/TDD/tdd-course-2/tdd_intro/homework/build-06.Allergies-Desktop_Qt_5_11_0_MSVC2015_32bit-Profile + + + true + qmake + + QtProjectManager.QMakeBuildStep + true + + false + true + false + + + true + Make + + Qt4ProjectManager.MakeStep + + false + + + + 2 + Build + + ProjectExplorer.BuildSteps.Build + + + + true + Make + + Qt4ProjectManager.MakeStep + + true + clean + + + 1 + Clean + + ProjectExplorer.BuildSteps.Clean + + 2 + false + + Profile + Profile + Qt4ProjectManager.Qt4BuildConfiguration + 0 + true + + 3 + + + 0 + Deploy + + ProjectExplorer.BuildSteps.Deploy + + 1 + Deploy Configuration + + ProjectExplorer.DefaultDeployConfiguration + + 1 + + + false + false + 1000 + + true + + false + false + false + false + true + 0.01 + 10 + true + 1 + 25 + + 1 + true + false + true + valgrind + + 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 11 + 12 + 13 + 14 + + 2 + + 06.Allergies + + Qt4ProjectManager.Qt4RunConfiguration:C:/MyWork/self_education/TDD/tdd-course-2/tdd_intro/homework/06.Allergies/06.Allergies.pro + true + + 06.Allergies.pro + false + + C:/MyWork/self_education/TDD/tdd-course-2/tdd_intro/homework/build-06.Allergies-Desktop_Qt_5_11_0_MSVC2015_32bit-Debug + 3768 + false + true + false + false + true + + 1 + + + + ProjectExplorer.Project.TargetCount + 1 + + + ProjectExplorer.Project.Updater.FileVersion + 18 + + + Version + 18 + + diff --git a/tdd_intro/homework/06.Allergies/test.cpp b/tdd_intro/homework/06.Allergies/test.cpp new file mode 100644 index 0000000..e2a6c61 --- /dev/null +++ b/tdd_intro/homework/06.Allergies/test.cpp @@ -0,0 +1,137 @@ +/* +Given a person's allergy score, determine whether or not they're allergic to a given item, and their full list of allergies. +An allergy test produces a single numeric score which contains the information about all the allergies the person has (that they were tested for). +The list of items (and their value) that were tested are: + +eggs (1) peanuts (2) shellfish (4) strawberries (8) tomatoes (16) +chocolate (32) pollen (64) cats (128) + +So if Tom is allergic to peanuts and chocolate, he gets a score of 34. + +Now, given just that score of 34, your program should be able: + +To say have Tom allergic to specific product or not. +To give list of all allergens Tom is allergic to. +Note: a given score may include allergens not listed above (i.e. allergens that score 256, 512, 1024, etc.). Your program should ignore those components of the score. For example, if the allergy score is 257, your program should only report the eggs (1) allergy. +*/ +#include + +//tests list +/* + 1.Check no allergy + 2.Check eggs alergy + 3.Check peanuts alergy + 4.Check shellfish alergy + 5.Check tomatoes alergy + 9.Check eggs and peanuts + 10.Check eggs and strawberries and pollen + 11.Check random alergy + */ + + +const std::string s_notAlergy = "You don't have any alergy"; +const std::string s_alergyOn = "You have alergy:"; +std::map s_alergyList = { + {1, "eggs"}, + {2, "peanuts"}, + {4, "shellfish"}, + {8, "strawberries"}, + {16, "tomatoes"}, + {32, "chocolate"}, + {64, "pollen"}, + {128, "cats"} + }; + + +int GetDegreeCount(int number) +{ + int degree = 0; + + unsigned int power; + + while (static_cast(pow(2, degree)) <= number) + { + + ++degree; + } + return --degree; +} + +std::string GetAlergy(size_t score) +{ + if(!score) + { + return s_notAlergy; + } + + std::string result = s_alergyOn; + unsigned int maxDegree = s_alergyList.size() - 1;; + unsigned int foundPow; + unsigned int foundDegree; + + while (score != 0) + { + foundDegree = GetDegreeCount(score); + foundPow = static_cast(pow(2, foundDegree)); + + if (foundDegree < maxDegree) + { + result += s_alergyList.find(foundPow)->second + ","; + } + + score -= foundPow; + } + result.pop_back(); + return result; +} + +TEST(GetAlergy, Check_no_allergy) +{ + EXPECT_EQ(s_notAlergy, GetAlergy(0)); +} + +TEST(GetAlergy, Check_eggs_alergy) +{ + EXPECT_EQ(s_alergyOn + s_alergyList[1], GetAlergy(1)); +} + +TEST(GetAlergy, Check_peanuts_alergy) +{ + EXPECT_EQ(s_alergyOn + s_alergyList[2], GetAlergy(2)); +} + +TEST(GetAlergy, Check_shellfish_alergy) +{ + EXPECT_EQ(s_alergyOn + s_alergyList[4], GetAlergy(4)); +} + +TEST(GetAlergy, Check_tomatoes_alergy) +{ + EXPECT_EQ(s_alergyOn + s_alergyList[16], GetAlergy(16)); +} + +TEST(GetAlergy, Check_two_alergies) +{ + EXPECT_EQ(s_alergyOn + "shellfish,eggs", GetAlergy(5)); +} + +TEST(GetAlergy, AcceptanceTest) +{ + EXPECT_EQ(s_alergyOn + "chocolate,peanuts", GetAlergy(34)); +} +//---------------------------------------------------------------------- +TEST(GetAlergy, GetDegreeCount) +{ + EXPECT_EQ(1, GetDegreeCount(2)); +} + +TEST(GetAlergy, GetDegreeCountFrom4) +{ + EXPECT_EQ(2, GetDegreeCount(4)); +} + +TEST(GetAlergy, GetDegreeCountFrom8) +{ + EXPECT_EQ(3, GetDegreeCount(8)); +} +