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

changed Password.cpp #16

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
9 changes: 9 additions & 0 deletions Password.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ using std::string;
'z' and any ASCII characters are allowed.
*/
int Password::count_leading_characters(string phrase){
if (phrase.empty()) return 0;

int repetition = 1;
int index = 0;
while( index < phrase.length()-1 && phrase[index] == phrase[index+1] ){
Expand All @@ -25,5 +27,12 @@ int Password::count_leading_characters(string phrase){
letter and at least one lower-case letter
*/
bool Password::has_mixed_case(string pass){
bool has_upper = false;
bool has_lower = false;
for (char c : pass) {
if (isupper(c)) has_upper = true;
if (islower(c)) has_lower = true;
if (has_upper && has_lower) return true;
}
return false;
}
51 changes: 45 additions & 6 deletions PasswordTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,57 @@
#include <gtest/gtest.h>
#include "Password.h"

class PracticeTest : public ::testing::Test
class PasswordTest : public ::testing::Test
{
protected:
PracticeTest(){} //constructor runs before each test
virtual ~PracticeTest(){} //destructor cleans up after tests
PasswordTest(){} //constructor runs before each test
virtual ~PasswordTest(){} //destructor cleans up after tests
virtual void SetUp(){} //sets up before each test (after constructor)
virtual void TearDown(){} //clean up after each test, (before destructor)
};

TEST(PasswordTest, single_letter_password)
TEST(PasswordTest, one_letter_password)
{
Password my_password;
int actual = my_password.count_leading_characters("Z");
int actual = my_password.count_leading_characters("z");
ASSERT_EQ(1, actual);
}
}

TEST(PasswordTest, two_letter_password)
{
Password my_password;
int actual = my_password.count_leading_characters("zz");
ASSERT_EQ(1, actual);
}


TEST(PasswordTest, no_leading_characters)
{
Password my_password;
int actual = my_password.count_leading_characters("ab");
ASSERT_EQ(1, actual); // "ab" has only 1 leading character 'a'
}

TEST(PasswordTest, empty_password)
{
Password my_password;
int actual = my_password.count_leading_characters("");
ASSERT_EQ(0, actual); // empty string should return 0
}

TEST(PasswordTest, has_mixed_case_true)
{
Password my_password;
bool actual = my_password.has_mixed_case("PassWord");
ASSERT_TRUE(actual); // "Password" has both upper and lower case letters
}

TEST(PasswordTest, has_mixed_case_false)
{
Password my_password;
bool actual = my_password.has_mixed_case("password");
ASSERT_FALSE(actual); // "password" has only lower case letters

actual = my_password.has_mixed_case("PASSWORD");
ASSERT_FALSE(actual); // "PASSWORD" has only upper case letters
}