Skip to content
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

Feature/hacker #196

Merged
merged 3 commits into from
Oct 8, 2023
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
6 changes: 4 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ set(FAKER_SOURCES
src/modules/database/Database.cpp
src/modules/music/Music.cpp
src/modules/movie/Movie.cpp
)
src/modules/hacker/Hacker.cpp
)

set(FAKER_UT_SOURCES
src/modules/animal/AnimalTest.cpp
Expand All @@ -68,7 +69,8 @@ set(FAKER_UT_SOURCES
src/modules/database/DatabaseTest.cpp
src/modules/music/MusicTest.cpp
src/modules/movie/MovieTest.cpp
)
src/modules/hacker/HackerTest.cpp
)

add_library(${LIBRARY_NAME} ${FAKER_SOURCES})

Expand Down
74 changes: 74 additions & 0 deletions include/faker-cxx/Hacker.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#pragma once

#include <string>

namespace faker {
class Hacker {
public:
/**
* @brief Returns a hacker abbreviation.
*
* @returns abbreviation.
*
* @code
* Hacker::abbreviation() // "TCP"
* @endcode
*/
static std::string abbreviation();

/**
* @brief Returns a random adjective.
*
* @returns adjective.
*
* @code
* Hacker::adjective() // "open-source"
* @endcode
*/
static std::string adjective();

/**
* @brief Returns a random noun.
*
* @returns noun.
*
* @code
* Hacker::noun() // "coder"
* @endcode
*/
static std::string noun();

/**
* @brief Returns a random verb.
*
* @returns verb.
*
* @code
* Hacker::verb() // "run"
* @endcode
*/
static std::string verb();

/**
* @brief Returns a random ingverb.
*
* @returns ingverb.
*
* @code
* Hacker::ingverb() // "backing up"
* @endcode
*/
static std::string ingverb();

/**
* @brief Returns a random phrase.
*
* @return phrase.
*
* @code
* Hacker::phrase() // "If we bypass the monitor, we can get to the TCP monitor through the neural EXE monitor!"
* @endcode
*/
static std::string phrase();
};
}
42 changes: 42 additions & 0 deletions include/faker-cxx/Helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#include <functional>
#include <span>
#include <string>
#include <vector>

#include "Datatype.h"
#include "Number.h"
Expand Down Expand Up @@ -177,6 +179,46 @@ class Helper
return TResult();
}

/*
* @brief Returns a split string into words.
*
* @param input The string to split.
*
* @return A vector of words.
*
* @code
* Helper::splitIntoWords("Hello World!") // {"Hello", "World!"}
* @endcode
*/
static std::vector<std::string> splitIntoWords(const std::string& input);

/*
* @brief Returns true if the given character is punctuation.
*
* @param c The character to check.
*
* @return True if the given character is punctuation.
*
* @code
* Helper::isPunctuation('!') // true
* Helper::isPunctuation('a') // false
* @endcode
*/
static bool isPunctuation(char c);

/*
* @brief Returns the given word without punctuation.
*
* @param word The word to remove punctuation.
*
* @return The given word without punctuation.
*
* @code
* Helper::removePunctuation("Hello!") // "Hello"
* @endcode
*/
static std::string removePunctuation(const std::string& word);

private:
static std::random_device randomDevice;
static std::mt19937 pseudoRandomGenerator;
Expand Down
74 changes: 74 additions & 0 deletions src/modules/hacker/Hacker.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#include "faker-cxx/Hacker.h"

#include <string>
#include <vector>

#include "data/Abbreviations.h"
#include "data/Adjectives.h"
#include "data/Ingverbs.h"
#include "data/Nouns.h"
#include "data/Phrases.h"
#include "data/Verbs.h"

namespace faker
{
std::string Hacker::abbreviation()
{
return faker::Helper::arrayElement<std::string>(faker::abbreviations);
}

std::string Hacker::adjective()
{
return faker::Helper::arrayElement<std::string>(faker::adjectives);
}

std::string Hacker::noun()
{
return faker::Helper::arrayElement<std::string>(faker::nouns);
}

std::string Hacker::verb()
{
return faker::Helper::arrayElement<std::string>(faker::verbs);
}

std::string Hacker::ingverb()
{
return faker::Helper::arrayElement<std::string>(faker::ingverbs);
}

std::string Hacker::phrase()
{
auto splitRandomPhrase = Helper::splitIntoWords(faker::Helper::arrayElement<std::string>(faker::phrases));
std::string ret;

for (auto& word : splitRandomPhrase)
{
word = Helper::removePunctuation(word);
if (word == "{abbreviation}")
{
word = abbreviation();
}
else if (word == "{adjective}")
{
word = adjective();
}
else if (word == "{noun}")
{
word = noun();
}
else if (word == "{verb}")
{
word = verb();
}
else if (word == "{ingverb}")
{
word = ingverb();
}

ret += word + " ";
}

return ret;
}
}
109 changes: 109 additions & 0 deletions src/modules/hacker/HackerTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
#include "faker-cxx/Hacker.h"

#include <algorithm>
#include <string>

#include "gtest/gtest.h"

#include "data/Abbreviations.h"
#include "data/Adjectives.h"
#include "data/Ingverbs.h"
#include "data/Nouns.h"
#include "data/Phrases.h"
#include "data/Verbs.h"
#include "faker-cxx/Helper.h"

using namespace ::testing;
using namespace faker;

class HackerTest : public Test
{
public:
};

TEST_F(HackerTest, shouldGenerateAbbreviation)
{
std::string generatedAbbreviation = Hacker::abbreviation();

ASSERT_TRUE(std::ranges::any_of(abbreviations, [generatedAbbreviation](const std::string& abbreviation)
{ return abbreviation == generatedAbbreviation; }));
}

TEST_F(HackerTest, shouldGenerateAdjective)
{
std::string generatedAdjective = Hacker::adjective();

ASSERT_TRUE(std::ranges::any_of(adjectives, [generatedAdjective](const std::string& adjective)
{ return adjective == generatedAdjective; }));
}

TEST_F(HackerTest, shouldGenerateNoun)
{
std::string generatedNoun = Hacker::noun();

ASSERT_TRUE(std::ranges::any_of(nouns, [generatedNoun](const std::string& noun) { return noun == generatedNoun; }));
}

TEST_F(HackerTest, shouldGenerateVerb)
{
std::string generatedVerb = Hacker::verb();

ASSERT_TRUE(std::ranges::any_of(verbs, [generatedVerb](const std::string& verb) { return verb == generatedVerb; }));
}

TEST_F(HackerTest, shouldGenerateIngverb)
{
std::string generatedIngverb = Hacker::ingverb();

ASSERT_TRUE(std::ranges::any_of(ingverbs, [generatedIngverb](const std::string& ingverb)
{ return ingverb == generatedIngverb; }));
}

TEST_F(HackerTest, shouldGeneratePhrase)
{
std::string generatedPhrase = Hacker::phrase();
bool hasAdjective, hasNoun, hasVerb, hasAbbreviation;
hasAdjective = hasNoun = hasVerb = hasAbbreviation = false;

// Check for adjectives
for (const std::string& adj : adjectives)
{
if (generatedPhrase.find(adj) != std::string::npos)
{
hasAdjective = true;
break;
}
}

// Check for nouns
for (const std::string& noun : nouns)
{
if (generatedPhrase.find(noun) != std::string::npos)
{
hasNoun = true;
break;
}
}

// Check for verbs
for (const std::string& verb : verbs)
{
if (generatedPhrase.find(verb) != std::string::npos)
{
hasVerb = true;
break;
}
}

// Check for abbreviations
for (const std::string& abbreviation : abbreviations)
{
if (generatedPhrase.find(abbreviation) != std::string::npos)
{
hasAbbreviation = true;
break;
}
}

ASSERT_TRUE((hasAdjective && hasNoun && hasVerb && hasAbbreviation));
}
12 changes: 12 additions & 0 deletions src/modules/hacker/data/Abbreviations.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#pragma once

#include <string>
#include <vector>

namespace faker
{
const std::vector<std::string> abbreviations = {
"e.g.", "i.e.", "etc.", "Mr.", "Mrs.", "Ms.", "Dr.", "Prof.",
"Ave.", "St.", "A.M.", "P.M.", "USA", "UK", "CEO", "CFO",
};
}
12 changes: 12 additions & 0 deletions src/modules/hacker/data/Adjectives.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#pragma once

#include <string>
#include <vector>

namespace faker
{
const std::vector<std::string> adjectives = {
"auxiliary", "primary", "back-end", "digital", "open-source", "virtual", "cross-platform", "redundant",
"online", "haptic", "multi-byte", "bluetooth", "wireless", "1080p", "neural", "optical",
};
}
12 changes: 12 additions & 0 deletions src/modules/hacker/data/Ingverbs.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#pragma once

#include <string>
#include <vector>

namespace faker
{
const std::vector<std::string> ingverbs = {
"backing up", "bypassing", "hacking", "overriding", "compressing", "copying", "navigating", "indexing",
"connecting", "generating", "quantifying", "calculating", "synthesizing", "transmitting", "programming", "parsing",
};
}
12 changes: 12 additions & 0 deletions src/modules/hacker/data/Nouns.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#pragma once

#include <string>
#include <vector>

namespace faker
{
const std::vector<std::string> nouns = {
"driver", "protocol", "bandwidth", "panel", "microchip", "program", "port", "card",
"array", "interface", "system", "sensor", "firewall", "hard drive", "pixel", "alarm",
};
}
Loading