From fc590e5dec6e5e0af635406d22d53a3c902d97d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebasti=C3=A1n=20Ba=C4=8D=C3=ADk?= <56115264+sebastianbacik@users.noreply.github.com> Date: Wed, 8 Nov 2023 00:35:02 +0100 Subject: [PATCH 1/2] 163 feature/add barcode functionalities to commerce module (#221) * ean13 method added * remaining methods added with documentation * methods fixed + tests written --- include/faker-cxx/Commerce.h | 44 ++++++++++++ src/modules/commerce/Commerce.cpp | 96 +++++++++++++++++++++++++++ src/modules/commerce/CommerceTest.cpp | 82 +++++++++++++++++++++++ 3 files changed, 222 insertions(+) diff --git a/include/faker-cxx/Commerce.h b/include/faker-cxx/Commerce.h index 765ea6754..fdc149af7 100644 --- a/include/faker-cxx/Commerce.h +++ b/include/faker-cxx/Commerce.h @@ -85,5 +85,49 @@ class Commerce * @endcode */ static std::string productFullName(); + + /** + * @brief Returns a random valid ean13 code. + * + * @returns Ean13 code. + * + * @code + * Commerce::EAN13() // "1234567890128" + * @endcode + */ + static std::string EAN13(); + + /** + * @brief Returns a random valid ean8 code. + * + * @returns Ean8 code. + * + * @code + * Commerce::EAN8() // "90311017" + * @endcode + */ + static std::string EAN8(); + + /** + * @brief Returns a random valid isbn13 code. + * + * @returns Isbn13 code. + * + * @code + * Commerce::ISBN13() // "9781234567897" + * @endcode + */ + static std::string ISBN13(); + + /** + * @brief Returns a random valid ISBN10 code. + * + * @returns Isbn10 code. + * + * @code + * Commerce::ISBN10() // "0200716018" + * @endcode + */ + static std::string ISBN10(); }; } diff --git a/src/modules/commerce/Commerce.cpp b/src/modules/commerce/Commerce.cpp index b7f1e9443..f83385b5d 100644 --- a/src/modules/commerce/Commerce.cpp +++ b/src/modules/commerce/Commerce.cpp @@ -42,4 +42,100 @@ std::string Commerce::productFullName() { return fmt::format("{} {} {}", productAdjective(), productMaterial(), productName()); } + +std::string Commerce::EAN13() +{ + std::string ean13 = String::numeric(12, false); + + int sum = 0; + for (size_t i = 0; i < 12; i++) + { + if (i % 2 == 0) { + sum += (ean13[i] - '0'); + } + else { + sum += 3 * (ean13[i] - '0'); + } + } + + int checkDigit = sum % 10; + + if (checkDigit != 0) { + checkDigit = 10 - checkDigit; + } + + return ean13 + std::to_string(checkDigit); +} + +std::string Commerce::EAN8() +{ + std::string ean8 = String::numeric(7, false); + + int sum = 0; + for (size_t i = 0; i < 7; i++) + { + if (i % 2 == 0) { + sum += 3 * (ean8[i] - '0'); + } + else { + sum += (ean8[i] - '0'); + } + } + + int checkDigit = sum % 10; + + if (checkDigit != 0) { + checkDigit = 10 - checkDigit; + } + + return ean8 + std::to_string(checkDigit); +} + +std::string Commerce::ISBN13() +{ + std::string isbn13 = String::numeric(12, true); + + int sum = 0; + for (size_t i = 0; i < 12; i++) + { + if (i % 2 == 0) { + sum += (isbn13[i] - '0'); + } + else { + sum += 3 * (isbn13[i] - '0'); + } + } + + int checkDigit = sum % 10; + + if (checkDigit != 0) { + checkDigit = 10 - checkDigit; + } + + return isbn13 + std::to_string(checkDigit); +} + +std::string Commerce::ISBN10() +{ + std::string isbn10 = String::numeric(9, true); + + int sum = 0, weight = 10; + for (size_t i = 0; i < 9; i++) + { + sum += (isbn10[i] - '0') * weight; + weight--; + } + + int checkDigit = sum % 11; + + if (checkDigit != 0) { + checkDigit = 11 - checkDigit; + } + + if (checkDigit == 10) { + return isbn10 + "X"; + } + + return isbn10 + std::to_string(checkDigit); +} } diff --git a/src/modules/commerce/CommerceTest.cpp b/src/modules/commerce/CommerceTest.cpp index ed49b5248..7fb206efd 100644 --- a/src/modules/commerce/CommerceTest.cpp +++ b/src/modules/commerce/CommerceTest.cpp @@ -111,3 +111,85 @@ TEST_F(CommerceTest, shouldGenerateProductName) ASSERT_TRUE(std::ranges::any_of(productNames, [generatedProductName](const std::string& productName) { return productName == generatedProductName; })); } + +TEST_F(CommerceTest, shouldGenerateEan13) +{ + const auto generatedEan13 = Commerce::EAN13(); + + int sum = 0; + for (size_t i = 0; i < 13; i++) + { + if (i % 2 == 0) { + sum += (generatedEan13[i] - '0'); + } + else { + sum += 3 * (generatedEan13[i] - '0'); + } + } + + ASSERT_EQ(generatedEan13.size(), 13); + ASSERT_TRUE(sum % 10 == 0); +} + +TEST_F(CommerceTest, shouldGenerateEan8) +{ + const auto generatedEan8 = Commerce::EAN8(); + + int sum = 0; + for (size_t i = 0; i < 8; i++) + { + if (i % 2 == 0) { + sum += 3 * (generatedEan8[i] - '0'); + } + else { + sum += (generatedEan8[i] - '0'); + } + } + + ASSERT_EQ(generatedEan8.size(), 8); + ASSERT_TRUE(sum % 10 == 0); +} + +TEST_F(CommerceTest, shouldGenerateIsbn13) +{ + const auto generatedIsbn13 = Commerce::ISBN13(); + + int sum = 0; + for (size_t i = 0; i < 13; i++) + { + if (i % 2 == 0) { + sum += (generatedIsbn13[i] - '0'); + } + else { + sum += 3 * (generatedIsbn13[i] - '0'); + } + } + + ASSERT_EQ(generatedIsbn13.size(), 13); + ASSERT_TRUE(sum % 10 == 0); +} + +TEST_F(CommerceTest, shouldGenerateIsbn10) +{ + const auto generatedIsbn10 = Commerce::ISBN10(); + + int sum = 0, weight = 10; + if (generatedIsbn10[9] == 'X') { + for (size_t i = 0; i < 9; i++) + { + sum += (generatedIsbn10[i] - '0') * weight; + weight--; + } + sum += 10; + } + else { + for (size_t i = 0; i < 10; i++) + { + sum += (generatedIsbn10[i] - '0') * weight; + weight--; + } + } + + ASSERT_EQ(generatedIsbn10.size(), 10); + ASSERT_TRUE(sum % 11 == 0); +} From 52ca73851a66d77454e979ebda1e642bf28dfc79 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabi=C3=A1n=20Arancibia=20Caprile?= <101164415+AltairFromAquila@users.noreply.github.com> Date: Wed, 8 Nov 2023 15:20:26 -0300 Subject: [PATCH 2/2] CMYK function added (#223) * CMYK function added to Color module (#187) * CMYK test added (#187) * files formatted * CMYK function implemented (#187) --- include/faker-cxx/Color.h | 12 +++++++++++- src/modules/color/Color.cpp | 10 ++++++++++ src/modules/color/ColorTest.cpp | 23 +++++++++++++++++++++++ 3 files changed, 44 insertions(+), 1 deletion(-) diff --git a/include/faker-cxx/Color.h b/include/faker-cxx/Color.h index d44e8e485..6b6db8b79 100644 --- a/include/faker-cxx/Color.h +++ b/include/faker-cxx/Color.h @@ -73,6 +73,16 @@ class Color * Color::lch(true) // "lcha(0, 0, 100, 0.50)" * @endcode */ - static std::string lch(bool includeAlpha = false); + static std::string lch(bool includeAlpha = false); + + /** + * @brief Return a CMYK color + * + * @returns CMYK color formatted with cmyk(X,X,X,X) + * @code + * Color::cmyk() // "cmyk(0.72, 0.88, 0.00, 0.06)" + * @endcode + */ + static std::string cmyk(); }; } diff --git a/src/modules/color/Color.cpp b/src/modules/color/Color.cpp index e06ec376c..02519d22c 100644 --- a/src/modules/color/Color.cpp +++ b/src/modules/color/Color.cpp @@ -89,4 +89,14 @@ std::string Color::lch(bool includeAlpha) return fmt::format("lcha({}, {}, {}, {})", luminance, chroma, hue, formattedAlpha); } +std::string Color::cmyk() +{ + const std::floating_point auto cyan = Number::decimal(1.); + const std::floating_point auto magenta = Number::decimal(1.); + const std::floating_point auto yellow = Number::decimal(1.); + const std::floating_point auto key = Number::decimal(1.); + + return fmt::format("cmyk({:.2f}, {:.2f}, {:.2f}, {:.2f})", cyan, magenta, yellow, key); +} + } diff --git a/src/modules/color/ColorTest.cpp b/src/modules/color/ColorTest.cpp index 304ff0cbb..0125ed7f5 100644 --- a/src/modules/color/ColorTest.cpp +++ b/src/modules/color/ColorTest.cpp @@ -168,3 +168,26 @@ TEST_F(ColorTest, shouldGenerateLchWithAlpha) ASSERT_TRUE(hue >= 0 && hue <= 360); ASSERT_TRUE(alpha >= 0 && alpha <= 1); } + +TEST_F(ColorTest, shouldGenerateCmykColor) +{ + const auto generatedCmykColor = faker::Color::cmyk(); + const auto cmykValues = + faker::StringHelper::split(generatedCmykColor.substr(5, generatedCmykColor.size() - 1), " "); + + auto offset = cmykValues[0].size(); + const auto cyan = std::stod(cmykValues[0].data(), &offset); + offset = cmykValues[1].size(); + const auto magenta = std::stod(cmykValues[1].data(), &offset); + offset = cmykValues[2].size(); + const auto yellow = std::stod(cmykValues[2].data(), &offset); + offset = cmykValues[3].size(); + const auto key = std::stod(cmykValues[3].data(), &offset); + + ASSERT_TRUE(generatedCmykColor.starts_with("cmyk(")); + ASSERT_TRUE(generatedCmykColor.ends_with(")")); + ASSERT_TRUE(0. <= cyan && cyan <= 1.); + ASSERT_TRUE(0. <= magenta && magenta <= 1.); + ASSERT_TRUE(0. <= yellow && yellow <= 1.); + ASSERT_TRUE(0. <= key && key <= 1.); +}