Skip to content

Commit

Permalink
feat: move binary function from string to number module (#923)
Browse files Browse the repository at this point in the history
* feat: move binary function from string to number module

* fix: fix tests

* fix: fix tests v2
  • Loading branch information
cieslarmichal authored Sep 27, 2024
1 parent eb1a88f commit dbfb31f
Show file tree
Hide file tree
Showing 15 changed files with 159 additions and 173 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@ All notable changes to this project will be documented in this file
### ⚠ BREAKING CHANGES

* moved `hexadecimal` function from `string` to `number` module
* moved `binary` function from `string` to `number` module
* moved `octal` function from `string` to `number` module

### Features

* added locale to `book` module
* added locale to `weather` module

## v3.0.0 (28.08.2024)

Expand Down
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ int main()
- 🌍 Location - countries, cities, zip codes, street addresses
- 📚 Lorem - lorem words, sentences, paragraphs
- 🏥 Medicine - conditions, medical tests, specialties
- 🎥 Movie - actors, actresses, genres, movie enUSTitles
- 🎥 Movie - actors, actresses, genres, movie titles
- 🎶 Music - artists, song names, genres
- 🔢 Number - random integers, floating point numbers
- 🧑 Person - first, last names, job enUSTitles, hobby, genders, sex, nationality, language
Expand All @@ -123,6 +123,7 @@ int main()
## Consuming the library with CMake

## With Git submodules and add_library

1. Add faker to git submodules (execute in project root):

```
Expand All @@ -142,14 +143,15 @@ int main()
target_link_libraries(main faker-cxx)
```
## With FetchContent
```cmake
set(BUILD_TESTING OFF)
FetchContent_Declare(faker
GIT_REPOSITORY https://github.com/cieslarmichal/faker-cxx.git
GIT_TAG main
GIT_REPOSITORY https://github.com/cieslarmichal/faker-cxx.git
GIT_TAG main
)
FetchContent_MakeAvailable(faker)
Expand Down
31 changes: 31 additions & 0 deletions include/faker-cxx/number.h
Original file line number Diff line number Diff line change
Expand Up @@ -241,4 +241,35 @@ FAKER_CXX_EXPORT std::string hexadecimal(std::optional<int> min = std::nullopt,
* @endcode
*/
FAKER_CXX_EXPORT std::string octal(unsigned length = 1);

/**
* @brief Generates a binary string of a specified length
*
* @param length The number of digits to generate. Defaults to `1`.
*
* @returns Binary string.
*
* @throws std::invalid_argument, if length is negative
*
* @code
* faker::string::binary(8) // "0b01110101"
* @endcode
*/
FAKER_CXX_EXPORT std::string binary(int length = 1);

/**
* @brief Generates a random binary string which has its decimal equivalent between min and max inclusive
*
* @param min the minimum possible decimal equivalent of the output
* @param max the maximum possible decimal equivalent of the output
*
* @returns Binary string.
*
* @throws std::invalid_argument, if min > max, std::invalid_argument if min or max are negative
*
* @code
* faker::string::binary(0, 1024) // "0b10110"
* @endcode
*/
FAKER_CXX_EXPORT std::string binary(int min, int max);
}
35 changes: 0 additions & 35 deletions include/faker-cxx/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,6 @@ FAKER_CXX_EXPORT bool isValidGuarantee(GuaranteeMap& guarantee, std::set<char>&
*/
FAKER_CXX_EXPORT std::string generateAtLeastString(const GuaranteeMap& guarantee);

// namespace {
// std::string generateStringWithGuarantee(GuaranteeMap& guarantee, std::set<char>& targetCharacters,
// unsigned int length);
// }
/**
* @brief Generates an Universally Unique Identifier with version 4.
*
Expand Down Expand Up @@ -289,35 +285,4 @@ FAKER_CXX_EXPORT std::string numeric(unsigned length = 1, bool allowLeadingZeros
* @endcode
*/
FAKER_CXX_EXPORT std::string numeric(GuaranteeMap&& guarantee, unsigned length = 1, bool allowLeadingZeros = true);

/**
* @brief Generates a binary string of a specified length
*
* @param length The number of digits to generate. Defaults to `1`.
*
* @returns Binary string.
*
* @throws std::invalid_argument, if length is negative
*
* @code
* faker::string::binary(8) // "0b01110101"
* @endcode
*/
FAKER_CXX_EXPORT std::string binary(int length = 1);

/**
* @brief Generates a random binary string which has its decimal equivalent between min and max inclusive
*
* @param min the minimum possible decimal equivalent of the output
* @param max the maximum possible decimal equivalent of the output
*
* @returns Binary string.
*
* @throws std::invalid_argument, if min > max, std::invalid_argument if min or max are negative
*
* @code
* faker::string::binary(0, 1024) // "0b10110"
* @endcode
*/
FAKER_CXX_EXPORT std::string binary(int min, int max);
}
48 changes: 48 additions & 0 deletions src/modules/number.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,52 @@ std::string octal(unsigned int length)

return "0o" + octalNumber;
}

std::string binary(int length)
{
if (length < 0)
{
throw std::invalid_argument("The length of a binary number cannot be negative");
}

std::string binaryNumber;

for (int i = 0; i < length; ++i)
{
binaryNumber += static_cast<char>(number::integer(1));
}

return "0b" + binaryNumber;
}

std::string binary(int min, int max)
{
if (min > max)
{
throw std::invalid_argument("min cannot be greater than max");
}

if (min < 0 || max < 0)
{
throw std::invalid_argument("The output binary string cannot be negative");
}

int num = number::integer<int>(min, max);

if (num == 0)
{
return "0b0";
}

std::string output;

while (num > 0)
{
int remainder = num % 2;
output = std::to_string(remainder) + output;
num /= 2;
}

return "0b" + output;
}
}
2 changes: 0 additions & 2 deletions src/modules/number_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,4 @@ namespace faker::number
{
const std::string hexUpperCharacters{"0123456789ABCDEF"};
const std::string hexLowerCharacters{"0123456789abcdef"};
const std::set<char> hexUpperCharSet{'A', 'B', 'C', 'D', 'E', 'F', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
const std::set<char> hexLowerCharSet{'a', 'b', 'c', 'd', 'e', 'f', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
}
61 changes: 0 additions & 61 deletions src/modules/string.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -290,65 +290,4 @@ std::string numeric(GuaranteeMap&& guarantee, const unsigned length, bool allowL
return firstChar + generateStringWithGuarantee(guarantee, targetCharacters, length - 1);
}
}

std::string binary(int length)
{
if (length < 0)
{
throw std::invalid_argument("The length of a binary number cannot be negative");
}

std::string binaryNumber;

for (int i = 0; i < length; ++i)
{
binaryNumber += static_cast<char>(number::integer(1));
}

return "0b" + binaryNumber;
}

std::string binary(int min, int max)
{
if (min > max)
{
throw std::invalid_argument("min cannot be greater than max");
}

if (min < 0 || max < 0)
{
throw std::invalid_argument("The output binary string cannot be negative");
}

int num = number::integer<int>(min, max);

if (num == 0)
{
return "0b0";
}

std::string output;

while (num > 0)
{
int remainder = num % 2;
output = std::to_string(remainder) + output;
num /= 2;
}

return "0b" + output;
}

std::string binary(GuaranteeMap&& guarantee, unsigned int length)
{
std::set<char> targetCharacters{'0', '1'};

if (!isValidGuarantee(guarantee, targetCharacters, length))
{
throw std::invalid_argument{"Invalid guarantee."};
}

return "0b" + generateStringWithGuarantee(guarantee, targetCharacters, length);
}

}
2 changes: 0 additions & 2 deletions src/modules/string_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ const std::string mixedAlphaCharacters{upperCharacters + lowerCharacters};
const std::string lowerAlphanumericCharacters{lowerCharacters + numericCharacters};
const std::string upperAlphanumericCharacters{upperCharacters + numericCharacters};
const std::string mixedAlphanumericCharacters{upperCharacters + lowerCharacters + numericCharacters};
const std::string hexUpperCharacters{"0123456789ABCDEF"};
const std::string hexLowerCharacters{"0123456789abcdef"};
const std::string symbolCharacters{"~`!@#$%^&*()_-+={[}]|:;\"'<,>.?/"};

const std::set<char> lowerCharSet{
Expand Down
6 changes: 3 additions & 3 deletions tests/modules/color_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include "common/string_helper.h"
#include "faker-cxx/color.h"
#include "faker-cxx/types/hex.h"
#include "string_data.h"
#include "number_data.h"

using namespace ::testing;
using namespace faker;
Expand Down Expand Up @@ -77,7 +77,7 @@ TEST_F(ColorTest, shouldGenerateHexColorWithoutAlpha)
ASSERT_EQ(prefix, "#");
ASSERT_TRUE(
std::ranges::any_of(hexNumber, [hexNumber](char hexNumberCharacter)
{ return string::hexLowerCharacters.find(hexNumberCharacter) != std::string::npos; }));
{ return number::hexLowerCharacters.find(hexNumberCharacter) != std::string::npos; }));
}

TEST_F(ColorTest, shouldGenerateHexColorWithAlpha)
Expand All @@ -91,7 +91,7 @@ TEST_F(ColorTest, shouldGenerateHexColorWithAlpha)
ASSERT_EQ(prefix, "0x");
ASSERT_TRUE(
std::ranges::any_of(hexNumber, [hexNumber](char hexNumberCharacter)
{ return string::hexUpperCharacters.find(hexNumberCharacter) != std::string::npos; }));
{ return number::hexUpperCharacters.find(hexNumberCharacter) != std::string::npos; }));
}

TEST_F(ColorTest, shouldGenerateHslWithoutAlpha)
Expand Down
4 changes: 2 additions & 2 deletions tests/modules/database_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

#include "database_data.h"
#include "faker-cxx/database.h"
#include "string_data.h"
#include "number_data.h"

using namespace ::testing;
using namespace faker;
Expand Down Expand Up @@ -55,5 +55,5 @@ TEST_F(DatabaseTest, shouldGenerateMongoDbObjectId)
ASSERT_EQ(generatedMongoDbObjectId.size(), 24);
ASSERT_TRUE(
std::ranges::any_of(generatedMongoDbObjectId, [](char hexNumberCharacter)
{ return string::hexLowerCharacters.find(hexNumberCharacter) != std::string::npos; }));
{ return number::hexLowerCharacters.find(hexNumberCharacter) != std::string::npos; }));
}
3 changes: 2 additions & 1 deletion tests/modules/finance_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "faker-cxx/types/precision.h"
#include "finance_data.h"
#include "gmock/gmock.h"
#include "number_data.h"
#include "string_data.h"

using namespace ::testing;
Expand Down Expand Up @@ -353,7 +354,7 @@ TEST_F(FinanceTest, shouldGenerateEthereumAddress)
ASSERT_EQ(prefix, "0x");
ASSERT_TRUE(
std::ranges::any_of(hexNumber, [hexNumber](char hexNumberCharacter)
{ return string::hexLowerCharacters.find(hexNumberCharacter) != std::string::npos; }));
{ return number::hexLowerCharacters.find(hexNumberCharacter) != std::string::npos; }));
}

TEST_F(FinanceTest, shouldGenerateExpirationDate)
Expand Down
3 changes: 2 additions & 1 deletion tests/modules/internet_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "faker-cxx/internet.h"
#include "faker-cxx/number.h"
#include "internet_data.h"
#include "number_data.h"
#include "person_data.h"
#include "string_data.h"
#include "word_data.h"
Expand Down Expand Up @@ -661,7 +662,7 @@ TEST_F(InternetTest, shouldGenerateIpv6)
[](const std::string_view& generatedIpv6Part)
{
return std::ranges::all_of(generatedIpv6Part, [](char hexCharacter)
{ return string::hexLowerCharacters.find(hexCharacter) != std::string::npos; });
{ return number::hexLowerCharacters.find(hexCharacter) != std::string::npos; });
}));
}

Expand Down
Loading

0 comments on commit dbfb31f

Please sign in to comment.