Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define CATCH_CONFIG_MAIN
- #include "catch.hpp"
- #include <string>
- #include <algorithm>
- #include <vector>
- inline bool correctDomain(const std::string& email)
- {
- auto at = std::find(begin(email), end(email), '@');
- if (at != end(email))
- return (std::equal(at, end(email), "@pwr.wroc.pl") || std::equal(at, end(email), "@pwr.edu.pl"));
- return false;
- }
- inline bool correctPredLength(const std::string& email)
- {
- return std::distance(begin(email), std::find(begin(email), end(email), '@')) < 64;
- }
- inline bool correctLength(const std::string& email)
- {
- return std::distance(begin(email), end(email)) < 256;
- }
- inline bool numberOfDotsAndDashes(const std::string& email)
- {
- auto at = std::find(begin(email), end(email), '@');
- return (std::count(begin(email), at, '.') == 1 &&
- std::count(begin(email), at, '-') <= 1);
- }
- inline bool correctPredChars(const std::string& email)
- {
- auto at = std::find(begin(email), end(email), '@');
- return std::find_if(begin(email), at, [](const char& c)
- {
- int ascii = int(c);
- if (ascii == 45 || ascii == 46) return false;
- if (ascii < 65 || ascii > 122 || (ascii > 90 && ascii < 97)) return true;
- return false;
- }) == at;
- }
- inline bool correctEmail(const std::string& email)
- {
- if (!correctDomain(email)) return false;
- if (!correctPredLength(email)) return false;
- if (!correctLength(email)) return false;
- if (!numberOfDotsAndDashes(email)) return false;
- if (!correctPredChars(email)) return false;
- return true;
- }
- TEST_CASE("email is correct", "[email], [pwr]")
- {
- SECTION("emails are correct")
- {
- std::vector<std::string> correct = { "Zygmunt.Wojciechowski@pwr.edu.pl",
- "krzysztof.nowak@pwr.wroc.pl",
- "Janina.Kowalska-Wysocka@pwr.wroc.pl",
- "Marcin.tomaszek@pwr.edu.pl",
- "anna.nowak-kozlowska@pwr.edu.pl" };
- std::for_each(begin(correct), end(correct), [](const std::string& email)
- {
- INFO("Email " << email << " was expected to be correct");
- CHECK(correctEmail(email));
- });
- }
- SECTION("emails are incorrect")
- {
- std::vector<std::string> incorrect = { "Paweł.Królikowski@pwr.wroc.pl",
- "Katarzyna.Anna.Kowalska@pwr.edu.pl",
- "marzena-nowak-kozlowska@pwr.wroc.pl",
- "anna.drawska@gmail.com",
- "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaa@pwr.wroc.pl" };
- std::for_each(begin(incorrect), end(incorrect), [](const std::string& email)
- {
- INFO("Email " << email << " was expected to be incorrect");
- CHECK_FALSE(correctEmail(email));
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement