From 4e127dea61b7b4da61e5e2a450811cae138f394a Mon Sep 17 00:00:00 2001 From: Gega Anatoly Date: Wed, 28 Nov 2018 14:30:07 +0200 Subject: [PATCH 1/8] 02 - Ternary Numbers - Initial convert implementation --- tdd_intro/homework/02_ternary_numbers/test.cpp | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tdd_intro/homework/02_ternary_numbers/test.cpp b/tdd_intro/homework/02_ternary_numbers/test.cpp index 1750302..2d7361c 100644 --- a/tdd_intro/homework/02_ternary_numbers/test.cpp +++ b/tdd_intro/homework/02_ternary_numbers/test.cpp @@ -16,3 +16,13 @@ The last place in a ternary number is the 1's place. The second to last is the 3 If your language provides a method in the standard library to perform the conversion, pretend it doesn't exist and implement it yourself. */ + +int convert(const std::string &number) +{ + return 302; +} + +TEST (ternary_numbers, check_302) +{ + EXPECT_EQ(302, convert("102012")); +} From 676214571eaa61deb8abe43961d0d753ddcf3a5b Mon Sep 17 00:00:00 2001 From: Gega Anatoly Date: Wed, 28 Nov 2018 14:32:18 +0200 Subject: [PATCH 2/8] 02 - Ternary Numbers - Refactoring --- tdd_intro/homework/02_ternary_numbers/test.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tdd_intro/homework/02_ternary_numbers/test.cpp b/tdd_intro/homework/02_ternary_numbers/test.cpp index 2d7361c..0ad075d 100644 --- a/tdd_intro/homework/02_ternary_numbers/test.cpp +++ b/tdd_intro/homework/02_ternary_numbers/test.cpp @@ -17,6 +17,8 @@ The last place in a ternary number is the 1's place. The second to last is the 3 If your language provides a method in the standard library to perform the conversion, pretend it doesn't exist and implement it yourself. */ +const std::string g_ternary302 = "102012"; + int convert(const std::string &number) { return 302; @@ -24,5 +26,5 @@ int convert(const std::string &number) TEST (ternary_numbers, check_302) { - EXPECT_EQ(302, convert("102012")); + EXPECT_EQ(302, convert(g_ternary302)); } From 112311c8c52370ecefbddb0409a931d2595c7619 Mon Sep 17 00:00:00 2001 From: Gega Anatoly Date: Wed, 28 Nov 2018 14:33:34 +0200 Subject: [PATCH 3/8] 02 - Ternary Numbers - Added test for 303 --- tdd_intro/homework/02_ternary_numbers/test.cpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tdd_intro/homework/02_ternary_numbers/test.cpp b/tdd_intro/homework/02_ternary_numbers/test.cpp index 0ad075d..bb647bd 100644 --- a/tdd_intro/homework/02_ternary_numbers/test.cpp +++ b/tdd_intro/homework/02_ternary_numbers/test.cpp @@ -18,6 +18,7 @@ If your language provides a method in the standard library to perform the conver */ const std::string g_ternary302 = "102012"; +const std::string g_ternary303 = "102020"; int convert(const std::string &number) { @@ -28,3 +29,8 @@ TEST (ternary_numbers, check_302) { EXPECT_EQ(302, convert(g_ternary302)); } + +TEST (ternary_numbers, check_303) +{ + EXPECT_EQ(303, convert(g_ternary303)); +} From 53e09f3223a6938435d8fc2b4c7bc3e024c56c76 Mon Sep 17 00:00:00 2001 From: Gega Anatoly Date: Wed, 28 Nov 2018 14:34:25 +0200 Subject: [PATCH 4/8] 02 - Ternary Numbers - Fixed test for 303 --- tdd_intro/homework/02_ternary_numbers/test.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tdd_intro/homework/02_ternary_numbers/test.cpp b/tdd_intro/homework/02_ternary_numbers/test.cpp index bb647bd..ca9ae16 100644 --- a/tdd_intro/homework/02_ternary_numbers/test.cpp +++ b/tdd_intro/homework/02_ternary_numbers/test.cpp @@ -22,6 +22,8 @@ const std::string g_ternary303 = "102020"; int convert(const std::string &number) { + if (number == g_ternary303) + return 303; return 302; } From d8301dde838a0d4056a71d9618230d8e7c9f756a Mon Sep 17 00:00:00 2001 From: Gega Anatoly Date: Wed, 28 Nov 2018 14:37:34 +0200 Subject: [PATCH 5/8] 02 - Ternary Numbers - Added test for 304 --- tdd_intro/homework/02_ternary_numbers/test.cpp | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tdd_intro/homework/02_ternary_numbers/test.cpp b/tdd_intro/homework/02_ternary_numbers/test.cpp index ca9ae16..2adec13 100644 --- a/tdd_intro/homework/02_ternary_numbers/test.cpp +++ b/tdd_intro/homework/02_ternary_numbers/test.cpp @@ -19,6 +19,7 @@ If your language provides a method in the standard library to perform the conver const std::string g_ternary302 = "102012"; const std::string g_ternary303 = "102020"; +const std::string g_ternary304 = "102021"; int convert(const std::string &number) { @@ -36,3 +37,10 @@ TEST (ternary_numbers, check_303) { EXPECT_EQ(303, convert(g_ternary303)); } + +TEST (ternary_numbers, check_304) +{ + EXPECT_EQ(304, convert(g_ternary304)); +} + + From 4a6e526a9a29068a193ecfc5f54169c1ed1abcf9 Mon Sep 17 00:00:00 2001 From: Gega Anatoly Date: Wed, 28 Nov 2018 14:40:03 +0200 Subject: [PATCH 6/8] 02 - Ternary Numbers - Added test for 305 --- tdd_intro/homework/02_ternary_numbers/test.cpp | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tdd_intro/homework/02_ternary_numbers/test.cpp b/tdd_intro/homework/02_ternary_numbers/test.cpp index 2adec13..5a1d2fd 100644 --- a/tdd_intro/homework/02_ternary_numbers/test.cpp +++ b/tdd_intro/homework/02_ternary_numbers/test.cpp @@ -20,11 +20,14 @@ If your language provides a method in the standard library to perform the conver const std::string g_ternary302 = "102012"; const std::string g_ternary303 = "102020"; const std::string g_ternary304 = "102021"; +const std::string g_ternary305 = "102022"; int convert(const std::string &number) { if (number == g_ternary303) return 303; + if (number == g_ternary304) + return 304; return 302; } @@ -43,4 +46,9 @@ TEST (ternary_numbers, check_304) EXPECT_EQ(304, convert(g_ternary304)); } +TEST (ternary_numbers, check_305) +{ + EXPECT_EQ(305, convert(g_ternary305)); +} + From 553de47b6c060428e91c9a3d59ffd83991d33a17 Mon Sep 17 00:00:00 2001 From: Gega Anatoly Date: Wed, 28 Nov 2018 15:03:57 +0200 Subject: [PATCH 7/8] 02 - Ternary Numbers - Fixed test for 305 and refactoring --- tdd_intro/homework/02_ternary_numbers/test.cpp | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/tdd_intro/homework/02_ternary_numbers/test.cpp b/tdd_intro/homework/02_ternary_numbers/test.cpp index 5a1d2fd..2aaefd7 100644 --- a/tdd_intro/homework/02_ternary_numbers/test.cpp +++ b/tdd_intro/homework/02_ternary_numbers/test.cpp @@ -1,4 +1,5 @@ #include +#include /* Convert a ternary number, represented as a string (e.g. '102012'), to its decimal equivalent using first principles. @@ -24,11 +25,13 @@ const std::string g_ternary305 = "102022"; int convert(const std::string &number) { - if (number == g_ternary303) - return 303; - if (number == g_ternary304) - return 304; - return 302; + int result = 0; + int place = number.size(); + for (const char& a: number) + { + result += (a - '0') * std::pow(3, --place); + } + return result; } TEST (ternary_numbers, check_302) From 43e8e046437d0b905e8e5178b9d291affd9f2923 Mon Sep 17 00:00:00 2001 From: Gega Anatoly Date: Wed, 28 Nov 2018 15:14:16 +0200 Subject: [PATCH 8/8] 02 - Ternary Numbers - Refactoring and acceptance test --- .../homework/02_ternary_numbers/test.cpp | 21 +++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/tdd_intro/homework/02_ternary_numbers/test.cpp b/tdd_intro/homework/02_ternary_numbers/test.cpp index 2aaefd7..c84aabd 100644 --- a/tdd_intro/homework/02_ternary_numbers/test.cpp +++ b/tdd_intro/homework/02_ternary_numbers/test.cpp @@ -23,8 +23,18 @@ const std::string g_ternary303 = "102020"; const std::string g_ternary304 = "102021"; const std::string g_ternary305 = "102022"; -int convert(const std::string &number) + +const std::string g_ternary222 = "22020"; +const std::string g_ternary333 = "110100"; +const std::string g_ternary765 = "1001100"; +const std::string g_ternary345 = "110210"; + + +uint32_t convert(const std::string &number) { + if (number.empty()) + return -1; + int result = 0; int place = number.size(); for (const char& a: number) @@ -54,4 +64,11 @@ TEST (ternary_numbers, check_305) EXPECT_EQ(305, convert(g_ternary305)); } - +TEST (ternary_numbers, acceptance) +{ + EXPECT_EQ(222, convert(g_ternary222)); + EXPECT_EQ(333, convert(g_ternary333)); + EXPECT_EQ(765, convert(g_ternary765)); + EXPECT_EQ(345, convert(g_ternary345)); + EXPECT_EQ(-1, convert("")); +}