Skip to content

Commit cb4b807

Browse files
authored
test: update word-count exercise (#820)
1 parent 563f99c commit cb4b807

File tree

2 files changed

+65
-95
lines changed

2 files changed

+65
-95
lines changed

exercises/practice/word-count/.meta/tests.toml

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
1-
# This is an auto-generated file. Regular comments will be removed when this
2-
# file is regenerated. Regenerating will not touch any manually added keys,
3-
# so comments can be added in a "comment" key.
1+
# This is an auto-generated file.
2+
#
3+
# Regenerating this file via `configlet sync` will:
4+
# - Recreate every `description` key/value pair
5+
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
6+
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
7+
# - Preserve any other key/value pair
8+
#
9+
# As user-added comments (using the # character) will be removed when this file
10+
# is regenerated, comments can be added via a `comment` key.
411

512
[61559d5f-2cad-48fb-af53-d3973a9ee9ef]
613
description = "count one word"
@@ -28,6 +35,11 @@ description = "normalize case"
2835

2936
[4185a902-bdb0-4074-864c-f416e42a0f19]
3037
description = "with apostrophes"
38+
include = false
39+
40+
[4ff6c7d7-fcfc-43ef-b8e7-34ff1837a2d3]
41+
description = "with apostrophes"
42+
reimplements = "4185a902-bdb0-4074-864c-f416e42a0f19"
3143

3244
[be72af2b-8afe-4337-b151-b297202e4a7b]
3345
description = "with quotations"
@@ -40,3 +52,6 @@ description = "multiple spaces not detected as a word"
4052

4153
[50176e8a-fe8e-4f4c-b6b6-aa9cf8f20360]
4254
description = "alternating word separators not detected as a word"
55+
56+
[6d00f1db-901c-4bec-9829-d20eb3044557]
57+
description = "quotation for word with apostrophe"

exercises/practice/word-count/word_count_test.cpp

Lines changed: 47 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -4,128 +4,83 @@
44
#else
55
#include "test/catch.hpp"
66
#endif
7-
#include <map>
87

9-
using namespace std;
108

11-
// Word-count exercise test case data version 1.4.0
9+
/*
10+
For each word in the input, count the number of times it appears in the
11+
entire sentence.
12+
*/
1213

13-
TEST_CASE("counts_one_word")
14-
{
15-
const map<string, int> expected{{"word", 1}};
16-
17-
const auto actual = word_count::words("word");
18-
19-
REQUIRE(expected == actual);
14+
TEST_CASE("count one word", "[61559d5f-2cad-48fb-af53-d3973a9ee9ef]") {
15+
const std::map<std::string, int> expected{{"word", 1}};
16+
REQUIRE(expected == word_count::words("word"));
2017
}
2118

2219
#if defined(EXERCISM_RUN_ALL_TESTS)
23-
TEST_CASE("counts_one_of_each")
24-
{
25-
const map<string, int> expected{{"one", 1}, {"of", 1}, {"each", 1}};
2620

27-
const auto actual = word_count::words("one of each");
28-
29-
REQUIRE(expected == actual);
21+
TEST_CASE("count one of each word", "[5abd53a3-1aed-43a4-a15a-29f88c09cbbd]") {
22+
const std::map<std::string, int> expected{{"one", 1}, {"of", 1}, {"each", 1}};
23+
REQUIRE(expected == word_count::words("one of each"));
3024
}
3125

32-
TEST_CASE("counts_multiple_occurrences")
33-
{
34-
const map<string, int> expected{{"one", 1}, {"fish", 4}, {"two", 1}, {"red", 1}, {"blue", 1}};
35-
36-
const auto actual = word_count::words("one fish two fish red fish blue fish");
37-
38-
REQUIRE(expected == actual);
26+
TEST_CASE("multiple occurrences of a word", "[2a3091e5-952e-4099-9fac-8f85d9655c0e]") {
27+
const std::map<std::string, int> expected{{"one", 1}, {"fish", 4}, {"two", 1}, {"red", 1}, {"blue", 1}};
28+
REQUIRE(expected == word_count::words("one fish two fish red fish blue fish"));
3929
}
4030

41-
TEST_CASE("handles_cramped_list")
42-
{
43-
const map<string, int> expected{{"one", 1}, {"two", 1}, {"three", 1}};
44-
45-
const auto actual = word_count::words("one,two,three");
46-
47-
REQUIRE(expected == actual);
31+
TEST_CASE("handles cramped lists", "[e81877ae-d4da-4af4-931c-d923cd621ca6]") {
32+
const std::map<std::string, int> expected{{"one", 1}, {"two", 1}, {"three", 1}};
33+
REQUIRE(expected == word_count::words("one,two,three"));
4834
}
4935

50-
TEST_CASE("handles_expanded_list")
51-
{
52-
const map<string, int> expected{{"one", 1}, {"two", 1}, {"three", 1}};
53-
54-
const auto actual = word_count::words("one,\ntwo,\nthree");
55-
56-
REQUIRE(expected == actual);
36+
TEST_CASE("handles expanded lists", "[7349f682-9707-47c0-a9af-be56e1e7ff30]") {
37+
const std::map<std::string, int> expected{{"one", 1}, {"two", 1}, {"three", 1}};
38+
REQUIRE(expected == word_count::words("one,\ntwo,\nthree"));
5739
}
5840

59-
TEST_CASE("ignores_punctuation")
60-
{
61-
const map<string, int> expected{{"car", 1}, {"carpet", 1}, {"as", 1}, {"java", 1}, {"javascript", 1}};
62-
63-
const auto actual = word_count::words("car: carpet as java: javascript!!&@$%^&");
64-
65-
REQUIRE(expected == actual);
41+
TEST_CASE("ignore punctuation", "[a514a0f2-8589-4279-8892-887f76a14c82]") {
42+
const std::map<std::string, int> expected{{"car", 1}, {"carpet", 1}, {"as", 1}, {"java", 1}, {"javascript", 1}};
43+
REQUIRE(expected == word_count::words("car: carpet as java: javascript!!&@$%^&"));
6644
}
6745

68-
TEST_CASE("includes_numbers")
69-
{
70-
const map<string, int> expected{{"testing", 2}, {"1", 1}, {"2", 1}};
71-
72-
const auto actual = word_count::words("testing, 1, 2 testing");
73-
74-
REQUIRE(expected == actual);
46+
TEST_CASE("include numbers", "[d2e5cee6-d2ec-497b-bdc9-3ebe092ce55e]") {
47+
const std::map<std::string, int> expected{{"testing", 2}, {"1", 1}, {"2", 1}};
48+
REQUIRE(expected == word_count::words("testing, 1, 2 testing"));
7549
}
7650

77-
TEST_CASE("normalizes_case")
78-
{
79-
const map<string, int> expected{{"go", 3}, {"stop", 2}};
80-
81-
const auto actual = word_count::words("go Go GO Stop stop");
82-
83-
REQUIRE(expected == actual);
51+
TEST_CASE("normalize case", "[dac6bc6a-21ae-4954-945d-d7f716392dbf]") {
52+
const std::map<std::string, int> expected{{"go", 3}, {"stop", 2}};
53+
REQUIRE(expected == word_count::words("go Go GO Stop stop"));
8454
}
8555

86-
TEST_CASE("with_apostrophes")
87-
{
88-
const map<string, int> expected{{"first", 1}, {"don't", 2}, {"laugh", 1}, {"then", 1}, {"cry", 1}};
89-
90-
const auto actual = word_count::words("First: don't laugh. Then: don't cry.");
91-
92-
REQUIRE(expected == actual);
56+
TEST_CASE("with apostrophes", "[4ff6c7d7-fcfc-43ef-b8e7-34ff1837a2d3]") {
57+
const std::map<std::string, int> expected{{"first", 1}, {"don't", 2}, {"laugh", 1}, {"then", 1}, {"cry", 1}, {"you're", 1}, {"getting", 1}, {"it", 1}};
58+
REQUIRE(expected == word_count::words("'First: don't laugh. Then: don't cry. You're getting it.'"));
9359
}
9460

95-
TEST_CASE("with_quotations")
96-
{
97-
const map<string, int> expected{{"joe", 1}, {"can't", 1}, {"tell", 1}, {"between", 1}, {"large", 2}, {"and", 1}};
98-
99-
const auto actual = word_count::words("Joe can't tell between 'large' and large.");
100-
101-
REQUIRE(expected == actual);
61+
TEST_CASE("with quotations", "[be72af2b-8afe-4337-b151-b297202e4a7b]") {
62+
const std::map<std::string, int> expected{{"joe", 1}, {"can't", 1}, {"tell", 1}, {"between", 1}, {"large", 2}, {"and", 1}};
63+
REQUIRE(expected == word_count::words("Joe can't tell between 'large' and large."));
10264
}
10365

104-
TEST_CASE("substrings_from_the_beginning")
105-
{
106-
const map<string, int> expected{{ "joe", 1 }, { "can't", 1 }, { "tell", 1 }, { "between", 1 }, { "app", 1 }, { "apple", 1 }, { "and", 1 }, { "a", 1 }};
107-
108-
const auto actual = word_count::words("Joe can't tell between app, apple and a.");
109-
110-
REQUIRE(expected == actual);
66+
TEST_CASE("substrings from the beginning", "[8d6815fe-8a51-4a65-96f9-2fb3f6dc6ed6]") {
67+
const std::map<std::string, int> expected{{"joe", 1}, {"can't", 1}, {"tell", 1}, {"between", 1}, {"app", 1}, {"apple", 1}, {"and", 1}, {"a", 1}};
68+
REQUIRE(expected == word_count::words("Joe can't tell between app, apple and a."));
11169
}
11270

113-
TEST_CASE("multiple_spaces_not_detected_as_a_word")
114-
{
115-
const map<string, int> expected{{ "multiple", 1 }, { "whitespaces", 1 }};
116-
117-
const auto actual = word_count::words(" multiple whitespaces");
118-
119-
REQUIRE(expected == actual);
71+
TEST_CASE("multiple spaces not detected as a word", "[c5f4ef26-f3f7-4725-b314-855c04fb4c13]") {
72+
const std::map<std::string, int> expected{{"multiple", 1}, {"whitespaces", 1}};
73+
REQUIRE(expected == word_count::words(" multiple whitespaces"));
12074
}
12175

122-
TEST_CASE("alternating_word_separators_not_detected_as_a_word")
123-
{
124-
const map<string, int> expected{{ "one", 1 }, { "two", 1 }, { "three", 1 }};
125-
126-
const auto actual = word_count::words(",\n,one,\n ,two \n 'three'");
76+
TEST_CASE("alternating word separators not detected as a word", "[50176e8a-fe8e-4f4c-b6b6-aa9cf8f20360]") {
77+
const std::map<std::string, int> expected{{"one", 1}, {"two", 1}, {"three", 1}};
78+
REQUIRE(expected == word_count::words(",\n,one,\n ,two \n 'three'"));
79+
}
12780

128-
REQUIRE(expected == actual);
81+
TEST_CASE("quotation for word with apostrophe", "[6d00f1db-901c-4bec-9829-d20eb3044557]") {
82+
const std::map<std::string, int> expected{{"can", 1}, {"can't", 2}};
83+
REQUIRE(expected == word_count::words("can, can't, 'can't'"));
12984
}
13085

13186
#endif

0 commit comments

Comments
 (0)