Advertisement
Guest User

Untitled

a guest
Jun 16th, 2017
540
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.56 KB | None | 0 0
  1. #define CATCH_CONFIG_MAIN
  2.  
  3. #include "catch.hpp"
  4. #include <string>
  5. #include <algorithm>
  6. #include <vector>
  7.  
  8. inline bool correctDomain(const std::string& email)
  9. {
  10.     auto at = std::find(begin(email), end(email), '@');
  11.     if (at != end(email))
  12.         return (std::equal(at, end(email), "@pwr.wroc.pl") || std::equal(at, end(email), "@pwr.edu.pl"));
  13.     return false;
  14. }
  15.  
  16. inline bool correctPredLength(const std::string& email)
  17. {
  18.     return std::distance(begin(email), std::find(begin(email), end(email), '@')) < 64;
  19. }
  20.  
  21. inline bool correctLength(const std::string& email)
  22. {
  23.     return std::distance(begin(email), end(email)) < 256;
  24. }
  25.  
  26. inline bool numberOfDotsAndDashes(const std::string& email)
  27. {
  28.     auto at = std::find(begin(email), end(email), '@');
  29.     return (std::count(begin(email), at, '.') == 1 &&
  30.         std::count(begin(email), at, '-') <= 1);
  31. }
  32.  
  33. inline bool correctPredChars(const std::string& email)
  34. {
  35.     auto at = std::find(begin(email), end(email), '@');
  36.     return std::find_if(begin(email), at, [](const char& c)
  37.     {
  38.         int ascii = int(c);
  39.         if (ascii == 45 || ascii == 46) return false;
  40.         if (ascii < 65 || ascii > 122 || (ascii > 90 && ascii < 97)) return true;
  41.         return false;
  42.     }) == at;
  43. }
  44.  
  45. inline bool correctEmail(const std::string& email)
  46. {
  47.     if (!correctDomain(email)) return false;
  48.     if (!correctPredLength(email)) return false;
  49.     if (!correctLength(email)) return false;
  50.     if (!numberOfDotsAndDashes(email)) return false;
  51.     if (!correctPredChars(email)) return false;
  52.     return true;
  53. }
  54.  
  55. TEST_CASE("email is correct", "[email], [pwr]")
  56. {
  57.     SECTION("emails are correct")
  58.     {
  59.         std::vector<std::string> correct = { "Zygmunt.Wojciechowski@pwr.edu.pl",
  60.                                             "krzysztof.nowak@pwr.wroc.pl",
  61.                                             "Janina.Kowalska-Wysocka@pwr.wroc.pl",
  62.                                             "Marcin.tomaszek@pwr.edu.pl",
  63.                                             "anna.nowak-kozlowska@pwr.edu.pl" };
  64.  
  65.         std::for_each(begin(correct), end(correct), [](const std::string& email)
  66.         {
  67.             INFO("Email " << email << " was expected to be correct");
  68.             CHECK(correctEmail(email));
  69.         });
  70.     }
  71.  
  72.     SECTION("emails are incorrect")
  73.     {
  74.         std::vector<std::string> incorrect = { "Paweł.Królikowski@pwr.wroc.pl",
  75.                                             "Katarzyna.Anna.Kowalska@pwr.edu.pl",
  76.                                             "marzena-nowak-kozlowska@pwr.wroc.pl",
  77.                                             "anna.drawska@gmail.com",
  78.                                             "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaa@pwr.wroc.pl" };
  79.  
  80.         std::for_each(begin(incorrect), end(incorrect), [](const std::string& email)
  81.         {
  82.             INFO("Email " << email << " was expected to be incorrect");
  83.             CHECK_FALSE(correctEmail(email));
  84.         });
  85.     }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement