Skip to content
29 changes: 29 additions & 0 deletions tdd_intro/homework/02_ternary_numbers/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,32 @@ 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 ConvertTernaryToDecimal(const std::string& ternaryNumber)
{
int sum = 0;
long value = 1;
for(int i = ternaryNumber.length() - 1; i >= 0; --i)
Copy link
Collaborator

Choose a reason for hiding this comment

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

нет теста на пустую строку - следовательно не покрыт тестом кейс, когда цикл не выполнится ни разу

{
sum += (ternaryNumber[i] - 48) * value;
value *= 3;
}

return sum;
}


TEST(TernaryNumber, OneConvertsToOneTest)
{
EXPECT_EQ(1, ConvertTernaryToDecimal("1"));
}

TEST(TernaryNumber, ConvertValidTernaryNumberTest)
{
EXPECT_EQ(5, ConvertTernaryToDecimal("012"));
Copy link
Collaborator

Choose a reason for hiding this comment

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

Можно было сначала добавить тест на невалидное значение.
Шаг выглядит великоватым.

Copy link
Collaborator

Choose a reason for hiding this comment

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

  • перед этим тестом нужен был тест с пустой строкой

}

TEST(TernaryNumber, ConvertAnotherValidTernaryNumberTest)
{
EXPECT_EQ(302, ConvertTernaryToDecimal("102012"));
}