Skip to content

Commit 2ed0a55

Browse files
committed
Update crypto-square tests.
1 parent 845b8f2 commit 2ed0a55

File tree

2 files changed

+61
-43
lines changed

2 files changed

+61
-43
lines changed

exercises/crypto-square/crypto_square_test.cpp

Lines changed: 41 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,71 +2,85 @@
22
#define BOOST_TEST_MAIN
33
#include <boost/test/unit_test.hpp>
44

5-
BOOST_AUTO_TEST_CASE(normalize_strange_characters)
5+
BOOST_AUTO_TEST_CASE(normalize_capitals)
66
{
7-
BOOST_REQUIRE_EQUAL("splunk", crypto_square::cipher("s#$%^&plunk").normalize_plain_text());
7+
BOOST_REQUIRE_EQUAL("hello", crypto_square::cipher("Hello").normalize_plain_text());
88
}
99

1010
#if defined(EXERCISM_RUN_ALL_TESTS)
11-
BOOST_AUTO_TEST_CASE(normalize_numbers)
11+
BOOST_AUTO_TEST_CASE(normalize_spaces)
1212
{
13-
BOOST_REQUIRE_EQUAL("123go", crypto_square::cipher("1, 2, 3 GO!").normalize_plain_text());
13+
BOOST_REQUIRE_EQUAL("hithere", crypto_square::cipher("Hi there").normalize_plain_text());
1414
}
1515

16-
BOOST_AUTO_TEST_CASE(size_of_small_square)
16+
BOOST_AUTO_TEST_CASE(normalize_numbers)
1717
{
18-
BOOST_REQUIRE_EQUAL(2U, crypto_square::cipher("1234").size());
18+
BOOST_REQUIRE_EQUAL("123go", crypto_square::cipher("1, 2, 3 GO!").normalize_plain_text());
1919
}
2020

21-
BOOST_AUTO_TEST_CASE(size_of_slightly_larger_square)
21+
BOOST_AUTO_TEST_CASE(plain_text_empty)
2222
{
23-
BOOST_REQUIRE_EQUAL(3U, crypto_square::cipher("123456789").size());
23+
const std::vector<std::string> expected{};
24+
25+
const auto actual = crypto_square::cipher("").plain_text_segments();
26+
27+
BOOST_REQUIRE_EQUAL_COLLECTIONS(expected.begin(), expected.end(), actual.begin(), actual.end());
2428
}
2529

26-
BOOST_AUTO_TEST_CASE(size_of_non_perfect_square)
30+
BOOST_AUTO_TEST_CASE(plain_text_4_characters)
2731
{
28-
BOOST_REQUIRE_EQUAL(4U, crypto_square::cipher("123456789abc").size());
32+
const std::vector<std::string> expected{"ab", "cd"};
33+
34+
const auto actual = crypto_square::cipher("Ab Cd").plain_text_segments();
35+
36+
BOOST_REQUIRE_EQUAL_COLLECTIONS(expected.begin(), expected.end(), actual.begin(), actual.end());
2937
}
3038

31-
BOOST_AUTO_TEST_CASE(plain_text_segments_from_phrase)
39+
BOOST_AUTO_TEST_CASE(plain_text_9_characters)
3240
{
33-
const std::vector<std::string> expected{"neverv", "exthin", "eheart", "withid", "lewoes"};
41+
const std::vector<std::string> expected{"thi", "sis", "fun"};
3442

35-
const auto actual = crypto_square::cipher("Never vex thine heart with idle woes").plain_text_segments();
43+
const auto actual = crypto_square::cipher("This is fun!").plain_text_segments();
3644

3745
BOOST_REQUIRE_EQUAL_COLLECTIONS(expected.begin(), expected.end(), actual.begin(), actual.end());
3846
}
3947

40-
BOOST_AUTO_TEST_CASE(plain_text_segments_from_complex_phrase)
48+
BOOST_AUTO_TEST_CASE(plain_text_segments_from_phrase)
4149
{
42-
const std::vector<std::string> expected{"zomg", "zomb", "ies"};
50+
const std::vector<std::string> expected{"ifmanwas", "meanttos", "tayonthe", "groundgo", "dwouldha", "vegivenu", "sroots"};
4351

44-
const auto actual = crypto_square::cipher("ZOMG! ZOMBIES!!!").plain_text_segments();
52+
const auto actual = crypto_square::cipher("If man was meant to stay on the ground, god would have given us roots.").plain_text_segments();
4553

4654
BOOST_REQUIRE_EQUAL_COLLECTIONS(expected.begin(), expected.end(), actual.begin(), actual.end());
4755
}
4856

49-
BOOST_AUTO_TEST_CASE(cipher_text_short_phrase)
57+
BOOST_AUTO_TEST_CASE(cipher_text_empty_phrase)
5058
{
51-
BOOST_REQUIRE_EQUAL("tasneyinicdsmiohooelntuillibsuuml",
52-
crypto_square::cipher("Time is an illusion. Lunchtime doubly so.").cipher_text());
59+
BOOST_REQUIRE_EQUAL("",
60+
crypto_square::cipher("").cipher_text());
5361
}
5462

5563
BOOST_AUTO_TEST_CASE(cipher_text_long_phrase)
5664
{
57-
BOOST_REQUIRE_EQUAL("wneiaweoreneawssciliprerlneoidktcms",
58-
crypto_square::cipher("We all know interspecies romance is weird.").cipher_text());
65+
BOOST_REQUIRE_EQUAL("imtgdvsfearwermayoogoanouuiontnnlvtwttddesaohghnsseoau",
66+
crypto_square::cipher("If man was meant to stay on the ground, god would have given us roots.").cipher_text());
67+
}
68+
69+
BOOST_AUTO_TEST_CASE(normalized_cipher_text_empty)
70+
{
71+
BOOST_REQUIRE_EQUAL("",
72+
crypto_square::cipher("").normalized_cipher_text());
5973
}
6074

61-
BOOST_AUTO_TEST_CASE(normalized_cipher_text1)
75+
BOOST_AUTO_TEST_CASE(normalized_cipher_text_fun)
6276
{
63-
BOOST_REQUIRE_EQUAL("msemoa anindn inndla etltsh ui",
64-
crypto_square::cipher("Madness, and then illumination.").normalized_cipher_text());
77+
BOOST_REQUIRE_EQUAL("tsf hiu isn",
78+
crypto_square::cipher("This is fun!").normalized_cipher_text());
6579
}
6680

67-
BOOST_AUTO_TEST_CASE(normalized_cipher_text2)
81+
BOOST_AUTO_TEST_CASE(normalized_cipher_text_long_phrase)
6882
{
69-
BOOST_REQUIRE_EQUAL("vrela epems etpao oirpo",
70-
crypto_square::cipher("Vampires are people too!").normalized_cipher_text());
83+
BOOST_REQUIRE_EQUAL("imtgdvs fearwer mayoogo anouuio ntnnlvt wttddes aohghn sseoau ",
84+
crypto_square::cipher("If man was meant to stay on the ground, god would have given us roots.").normalized_cipher_text());
7185
}
7286
#endif

exercises/crypto-square/example.cpp

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ string cipher::normalize_plain_text() const
3434

3535
size_t cipher::size() const
3636
{
37-
size_t length = 1;
37+
size_t length = 0;
3838
while (length*length < text_.size()) {
3939
++length;
4040
}
@@ -53,27 +53,31 @@ vector<string> cipher::plain_text_segments() const
5353

5454
string cipher::cipher_text() const
5555
{
56-
string result;
57-
const vector<string> segments{plain_text_segments()};
58-
const size_t rows{size()};
59-
const size_t num_segments{segments.size()};
60-
for (size_t i = 0; i < rows; ++i) {
61-
for (size_t j = 0; j < num_segments; ++j) {
62-
if (i < segments[j].length()) {
63-
result += segments[j][i];
64-
}
65-
}
56+
string s{normalized_cipher_text()};
57+
if (!s.empty()) {
58+
s.erase(remove(s.begin(), s.end(), ' '), s.end());
6659
}
67-
return result;
60+
return s;
6861
}
6962

7063
string cipher::normalized_cipher_text() const
7164
{
72-
const string encoded{cipher_text()};
7365
const auto num_rows = size();
74-
string result{encoded.substr(0, num_rows)};
75-
for (auto i = num_rows; i < encoded.length(); i += num_rows) {
76-
result += ' ' + encoded.substr(i, num_rows);
66+
const auto plain{plain_text_segments()};
67+
string result;
68+
for (size_t i = 0; i < num_rows; ++i) {
69+
for (const auto s : plain) {
70+
if (s.length() > i) {
71+
result += s[i];
72+
}
73+
else {
74+
result += ' ';
75+
}
76+
}
77+
result += ' ';
78+
}
79+
if (!result.empty()) {
80+
result.pop_back();
7781
}
7882
return result;
7983
}

0 commit comments

Comments
 (0)