Skip to content

test: update word-count exercise #820

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions exercises/practice/word-count/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
# This is an auto-generated file. Regular comments will be removed when this
# file is regenerated. Regenerating will not touch any manually added keys,
# so comments can be added in a "comment" key.
# This is an auto-generated file.
#
# Regenerating this file via `configlet sync` will:
# - Recreate every `description` key/value pair
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
# - Preserve any other key/value pair
#
# As user-added comments (using the # character) will be removed when this file
# is regenerated, comments can be added via a `comment` key.

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

[4185a902-bdb0-4074-864c-f416e42a0f19]
description = "with apostrophes"
include = false

[4ff6c7d7-fcfc-43ef-b8e7-34ff1837a2d3]
description = "with apostrophes"
reimplements = "4185a902-bdb0-4074-864c-f416e42a0f19"

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

[50176e8a-fe8e-4f4c-b6b6-aa9cf8f20360]
description = "alternating word separators not detected as a word"

[6d00f1db-901c-4bec-9829-d20eb3044557]
description = "quotation for word with apostrophe"
139 changes: 47 additions & 92 deletions exercises/practice/word-count/word_count_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,128 +4,83 @@
#else
#include "test/catch.hpp"
#endif
#include <map>

using namespace std;

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

TEST_CASE("counts_one_word")
{
const map<string, int> expected{{"word", 1}};

const auto actual = word_count::words("word");

REQUIRE(expected == actual);
TEST_CASE("count one word", "[61559d5f-2cad-48fb-af53-d3973a9ee9ef]") {
const std::map<std::string, int> expected{{"word", 1}};
REQUIRE(expected == word_count::words("word"));
}

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

const auto actual = word_count::words("one of each");

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

TEST_CASE("counts_multiple_occurrences")
{
const map<string, int> expected{{"one", 1}, {"fish", 4}, {"two", 1}, {"red", 1}, {"blue", 1}};

const auto actual = word_count::words("one fish two fish red fish blue fish");

REQUIRE(expected == actual);
TEST_CASE("multiple occurrences of a word", "[2a3091e5-952e-4099-9fac-8f85d9655c0e]") {
const std::map<std::string, int> expected{{"one", 1}, {"fish", 4}, {"two", 1}, {"red", 1}, {"blue", 1}};
REQUIRE(expected == word_count::words("one fish two fish red fish blue fish"));
}

TEST_CASE("handles_cramped_list")
{
const map<string, int> expected{{"one", 1}, {"two", 1}, {"three", 1}};

const auto actual = word_count::words("one,two,three");

REQUIRE(expected == actual);
TEST_CASE("handles cramped lists", "[e81877ae-d4da-4af4-931c-d923cd621ca6]") {
const std::map<std::string, int> expected{{"one", 1}, {"two", 1}, {"three", 1}};
REQUIRE(expected == word_count::words("one,two,three"));
}

TEST_CASE("handles_expanded_list")
{
const map<string, int> expected{{"one", 1}, {"two", 1}, {"three", 1}};

const auto actual = word_count::words("one,\ntwo,\nthree");

REQUIRE(expected == actual);
TEST_CASE("handles expanded lists", "[7349f682-9707-47c0-a9af-be56e1e7ff30]") {
const std::map<std::string, int> expected{{"one", 1}, {"two", 1}, {"three", 1}};
REQUIRE(expected == word_count::words("one,\ntwo,\nthree"));
}

TEST_CASE("ignores_punctuation")
{
const map<string, int> expected{{"car", 1}, {"carpet", 1}, {"as", 1}, {"java", 1}, {"javascript", 1}};

const auto actual = word_count::words("car: carpet as java: javascript!!&@$%^&");

REQUIRE(expected == actual);
TEST_CASE("ignore punctuation", "[a514a0f2-8589-4279-8892-887f76a14c82]") {
const std::map<std::string, int> expected{{"car", 1}, {"carpet", 1}, {"as", 1}, {"java", 1}, {"javascript", 1}};
REQUIRE(expected == word_count::words("car: carpet as java: javascript!!&@$%^&"));
}

TEST_CASE("includes_numbers")
{
const map<string, int> expected{{"testing", 2}, {"1", 1}, {"2", 1}};

const auto actual = word_count::words("testing, 1, 2 testing");

REQUIRE(expected == actual);
TEST_CASE("include numbers", "[d2e5cee6-d2ec-497b-bdc9-3ebe092ce55e]") {
const std::map<std::string, int> expected{{"testing", 2}, {"1", 1}, {"2", 1}};
REQUIRE(expected == word_count::words("testing, 1, 2 testing"));
}

TEST_CASE("normalizes_case")
{
const map<string, int> expected{{"go", 3}, {"stop", 2}};

const auto actual = word_count::words("go Go GO Stop stop");

REQUIRE(expected == actual);
TEST_CASE("normalize case", "[dac6bc6a-21ae-4954-945d-d7f716392dbf]") {
const std::map<std::string, int> expected{{"go", 3}, {"stop", 2}};
REQUIRE(expected == word_count::words("go Go GO Stop stop"));
}

TEST_CASE("with_apostrophes")
{
const map<string, int> expected{{"first", 1}, {"don't", 2}, {"laugh", 1}, {"then", 1}, {"cry", 1}};

const auto actual = word_count::words("First: don't laugh. Then: don't cry.");

REQUIRE(expected == actual);
TEST_CASE("with apostrophes", "[4ff6c7d7-fcfc-43ef-b8e7-34ff1837a2d3]") {
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}};
REQUIRE(expected == word_count::words("'First: don't laugh. Then: don't cry. You're getting it.'"));
}

TEST_CASE("with_quotations")
{
const map<string, int> expected{{"joe", 1}, {"can't", 1}, {"tell", 1}, {"between", 1}, {"large", 2}, {"and", 1}};

const auto actual = word_count::words("Joe can't tell between 'large' and large.");

REQUIRE(expected == actual);
TEST_CASE("with quotations", "[be72af2b-8afe-4337-b151-b297202e4a7b]") {
const std::map<std::string, int> expected{{"joe", 1}, {"can't", 1}, {"tell", 1}, {"between", 1}, {"large", 2}, {"and", 1}};
REQUIRE(expected == word_count::words("Joe can't tell between 'large' and large."));
}

TEST_CASE("substrings_from_the_beginning")
{
const map<string, int> expected{{ "joe", 1 }, { "can't", 1 }, { "tell", 1 }, { "between", 1 }, { "app", 1 }, { "apple", 1 }, { "and", 1 }, { "a", 1 }};

const auto actual = word_count::words("Joe can't tell between app, apple and a.");

REQUIRE(expected == actual);
TEST_CASE("substrings from the beginning", "[8d6815fe-8a51-4a65-96f9-2fb3f6dc6ed6]") {
const std::map<std::string, int> expected{{"joe", 1}, {"can't", 1}, {"tell", 1}, {"between", 1}, {"app", 1}, {"apple", 1}, {"and", 1}, {"a", 1}};
REQUIRE(expected == word_count::words("Joe can't tell between app, apple and a."));
}

TEST_CASE("multiple_spaces_not_detected_as_a_word")
{
const map<string, int> expected{{ "multiple", 1 }, { "whitespaces", 1 }};

const auto actual = word_count::words(" multiple whitespaces");

REQUIRE(expected == actual);
TEST_CASE("multiple spaces not detected as a word", "[c5f4ef26-f3f7-4725-b314-855c04fb4c13]") {
const std::map<std::string, int> expected{{"multiple", 1}, {"whitespaces", 1}};
REQUIRE(expected == word_count::words(" multiple whitespaces"));
}

TEST_CASE("alternating_word_separators_not_detected_as_a_word")
{
const map<string, int> expected{{ "one", 1 }, { "two", 1 }, { "three", 1 }};

const auto actual = word_count::words(",\n,one,\n ,two \n 'three'");
TEST_CASE("alternating word separators not detected as a word", "[50176e8a-fe8e-4f4c-b6b6-aa9cf8f20360]") {
const std::map<std::string, int> expected{{"one", 1}, {"two", 1}, {"three", 1}};
REQUIRE(expected == word_count::words(",\n,one,\n ,two \n 'three'"));
}

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

#endif