0% found this document useful (0 votes)
0 views

DemoFile

The document contains a C# application with a calculator and login validation functionality. The Calculator class supports basic arithmetic operations, while the LoginValidator class checks for valid email and password formats. Additionally, there are unit tests for both classes to ensure their correctness and reliability.

Uploaded by

Hira Shahbaz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

DemoFile

The document contains a C# application with a calculator and login validation functionality. The Calculator class supports basic arithmetic operations, while the LoginValidator class checks for valid email and password formats. Additionally, there are unit tests for both classes to ensure their correctness and reliability.

Uploaded by

Hira Shahbaz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Calculator.

cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CalcLoginApp
{
public class Calculator
{
public int Add(int a, int b) => a + b;
public int Subtract(int a, int b) => a - b;
public int Multiply(int a, int b) => a * b;

public double Divide(int a, int b)


{
if (b == 0) throw new DivideByZeroException("Cannot divide by zero.");
return (double)a / b; // Now returns double for precise division
}

public int Modulus(int a, int b)


{
if (b == 0) throw new DivideByZeroException("Cannot modulus by zero.");
return a % b;
}
}

}
LoginValidator.cs

using System;
using System.Net.Mail;

namespace CalcLoginApp
{
public class LoginValidator
{
public bool IsValidEmail(string email)
{
if (string.IsNullOrWhiteSpace(email))
return false;

try
{
var addr = new MailAddress(email);
return addr.Address == email;
}
catch
{
return false;
}
}

public bool IsValidPassword(string password)


{
if (string.IsNullOrWhiteSpace(password) || password.Length < 6)
return false;

bool hasLetter = false;


bool hasDigit = false;

foreach (char c in password)


{
if (char.IsLetter(c))
hasLetter = true;
else if (char.IsDigit(c))
hasDigit = true;

if (hasLetter && hasDigit)


return true;
}

return false;
}

// ✅ Add this method to support login validation


public bool IsLoginValid(string email, string password)
{
return IsValidEmail(email) && IsValidPassword(password);
}
}
Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CalcLoginApp
{
class Program
{
static void Main(string[] args)
{
// Calculator demo
Calculator calc = new Calculator();
Console.WriteLine("Calculator Demo:");
Console.WriteLine($"5 + 3 = {calc.Add(5, 3)}");
Console.WriteLine($"10 / 3 = {calc.Divide(10, 3):0.00}");

// Login validation demo


LoginValidator validator = new LoginValidator();
Console.WriteLine("\nLogin Validation Demo:");

Console.Write("Enter email: ");


string email = Console.ReadLine();

Console.Write("Enter password: ");


string password = Console.ReadLine();

bool isValid = validator.IsLoginValid(email, password);


Console.WriteLine($"Login valid: {isValid}");

Console.WriteLine("\nPress any key to exit...");


Console.ReadKey();
}
}
}
CalculatorTests.cs

using NUnit.Framework;
using CalcLoginApp;

namespace CalcLoginApp.Tests
{
[TestFixture]
public class CalculatorTests
{
private Calculator _calculator;

[SetUp]
public void Setup()
{
_calculator = new Calculator();
}

[Test]
[TestCase(4, 6, 10)]
[TestCase(-2, -3, -5)]
[TestCase(0, 5, 5)]
public void Add_VariousNumbers_ReturnsCorrectSum(int a, int b, int expected)
{
Assert.That(_calculator.Add(a, b), Is.EqualTo(expected));
}

[Test]
public void Divide_ByNonZero_ReturnsPreciseResult()
{
double result = _calculator.Divide(10, 3);
Assert.That(result, Is.EqualTo(3.33).Within(0.01));
}

[Test]
public void Divide_ByZero_ThrowsException()
{
Assert.That(() => _calculator.Divide(10, 0), Throws.TypeOf<DivideByZeroException>());
}

[Test]
public void Modulus_ValidInput_ReturnsCorrectRemainder()
{
Assert.That(_calculator.Modulus(10, 3), Is.EqualTo(1));
}
}
}
LoginValidatorTests.cs

using NUnit.Framework;
using System.Text.RegularExpressions;
using CalcLoginApp;

namespace CalcLoginApp.Tests
{
[TestFixture]
public class LoginValidatorTests
{
private LoginValidator _validator;

[SetUp]
public void Setup()
{
_validator = new LoginValidator();
}

[Test]
[TestCase("[email protected]", true)]
[TestCase("[email protected]", true)]
[TestCase("[email protected]", true)]
[TestCase("[email protected]", true)]
[TestCase("[email protected]", true)]
[TestCase("invalid.email@", false)]
[TestCase("missing.at.sign.com", false)]
[TestCase("plainstring", false)]
[TestCase("@no-local-part.com", false)]
[TestCase("", false)]
public void IsValidEmail_VariousInputs_ReturnsCorrectResult(string email, bool expected)
{
Assert.That(_validator.IsValidEmail(email), Is.EqualTo(expected),
$"Email validation failed for: {email}");
}

[Test]
public void IsValidEmail_NullInput_ReturnsFalse()
{
Assert.That(_validator.IsValidEmail(null), Is.False);
}

[Test]
[TestCase("abc123", true)]
[TestCase("P@ssw0rd", true)]
[TestCase("LongPassword1", true)]
[TestCase("12345", false)]
[TestCase("password", false)]
[TestCase("123456", false)]
[TestCase("", false)]
public void IsValidPassword_VariousInputs_ReturnsCorrectResult(string password, bool
expected)
{
Assert.That(_validator.IsValidPassword(password), Is.EqualTo(expected),
$"Password validation failed for: {password}");
}

[Test]
public void IsValidPassword_NullInput_ReturnsFalse()
{
Assert.That(_validator.IsValidPassword(null), Is.False);
}
}
}
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net472</TargetFramework>
<!-- This is the key change -->
<LangVersion>latest</LangVersion>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
<PackageReference Include="NUnit" Version="4.2.2" />
<PackageReference Include="NUnit.Analyzers" Version="4.4.0" />
<PackageReference Include="NUnit3TestAdapter" Version="4.6.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\CalcLoginApp\CalcLoginApp.csproj" />
</ItemGroup>

</Project>

You might also like