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

Чечулин Антон #224

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
6 changes: 3 additions & 3 deletions cs/HomeExercises/HomeExercises.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFramework>net7.0</TargetFramework>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
<RootNamespace>HomeExercises</RootNamespace>
<AssemblyName>ObjectComparison</AssemblyName>
Expand All @@ -13,8 +13,8 @@
<PackageReference Include="FluentAssertions" Version="5.10.3" />
<PackageReference Include="JetBrains.Annotations" Version="2020.1.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.1" />
<PackageReference Include="NUnit" Version="3.12.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.17.0" />
<PackageReference Include="NUnit" Version="3.14.0" />
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
LevShisterov marked this conversation as resolved.
Show resolved Hide resolved
</ItemGroup>

</Project>
52 changes: 52 additions & 0 deletions cs/HomeExercises/NumberValidator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using System;
using System.Text.RegularExpressions;

namespace HomeExercises
{
public class NumberValidator
{
private readonly static Regex numberRegex = new Regex(@"^([+-]?)(\d+)([.,](\d+))?$", RegexOptions.Compiled);
private readonly bool onlyPositive;
private readonly int precision;
private readonly int scale;

public NumberValidator(int precision, int scale = 0, bool onlyPositive = false)
{
this.precision = precision;
this.scale = scale;
this.onlyPositive = onlyPositive;
if (precision <= 0)
throw new ArgumentException("precision must be a positive number");
if (scale < 0 || scale >= precision)
throw new ArgumentException("scale must be a non-negative number, scale must be less or equal than precision");
}

public bool IsValidNumber(string value)
{
// Проверяем соответствие входного значения формату N(m,k), в соответствии с правилом,
// описанным в Формате описи документов, направляемых в налоговый орган в электронном виде по телекоммуникационным каналам связи:
// Формат числового значения указывается в виде N(m.к), где m – максимальное количество знаков в числе, включая знак (для отрицательного числа),
// целую и дробную часть числа без разделяющей десятичной точки, k – максимальное число знаков дробной части числа.
// Если число знаков дробной части числа равно 0 (т.е. число целое), то формат числового значения имеет вид N(m).

if (string.IsNullOrEmpty(value))
return false;

var match = numberRegex.Match(value);
if (!match.Success)
return false;

// Знак и целая часть
var intPart = match.Groups[1].Value.Length + match.Groups[2].Value.Length;
// Дробная часть
var fracPart = match.Groups[4].Value.Length;

if (intPart + fracPart > precision || fracPart > scale)
return false;

if (onlyPositive && match.Groups[1].Value == "-")
return false;
return true;
}
}
}
80 changes: 0 additions & 80 deletions cs/HomeExercises/NumberValidatorTests.cs

This file was deleted.

83 changes: 0 additions & 83 deletions cs/HomeExercises/ObjectComparison.cs

This file was deleted.

30 changes: 30 additions & 0 deletions cs/HomeExercises/Person.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
namespace HomeExercises
{
public class TsarRegistry
{
public static Person GetCurrentTsar()
{
return new Person("Ivan IV The Terrible", 54, 170, 70,
new Person("Vasili III of Russia", 28, 170, 60, null));
}
}

public class Person
{
public static int IdCounter = 0;
public int Age, Height, Weight;
public string Name;
public Person? Parent;
public int Id;

public Person(string name, int age, int height, int weight, Person? parent)
{
Id = IdCounter++;
Name = name;
Age = age;
Height = height;
Weight = weight;
Parent = parent;
}
}
}
23 changes: 23 additions & 0 deletions cs/HomeExercisesTests/HomeExercisesTests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="5.10.3" />
<PackageReference Include="NUnit" Version="3.14.0" />
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.0" />
</ItemGroup>

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

</Project>
59 changes: 59 additions & 0 deletions cs/HomeExercisesTests/NumberValidatorTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using FluentAssertions;
using HomeExercises;
using NUnit.Framework;

namespace HomeExercisesTests
{
public class NumberValidatorTests
{
[TestCase(-1, 2, true, TestName = "Constructor_PrecisionNotPositive_ThrowsArgumentException")]
[TestCase(0, 1, true, TestName = "Constructor_PrecisionIsZero_ThrowsArgumentException")]
[TestCase(1, -1, true, TestName = "Constructor_ScaleNegative_ThrowsArgumentException")]
[TestCase(1, 2, true, TestName = "Constructor_SacleBiggerPrecision_ThrowsArgumentException")]
[TestCase(1, 1, true, TestName = "Constructor_ScaleEqualsPrecision_ThrowsArgumentException")]
public void Throws(int precision, int scale, bool onlyPostive)
{
Action action = () => new NumberValidator(precision, scale, onlyPostive);
action.Should().Throw<ArgumentException>();
}

[TestCase(2, 1, true, TestName = "Constructor_PrecisionBiggerScale_NotTrow")]
[TestCase(17, 2, false, TestName = "Constructor_OnlyPositiveFalse_NotTrow")]
public void NotTrow(int precision, int scale, bool onlyPostive)
{
Action action = () => new NumberValidator(precision, scale, onlyPostive);
action.Should().NotThrow<ArgumentException>();
}

[TestCase(17, 2, true, " ", TestName = "IsValidNumber_NumberEmpty_Fails")]
[TestCase(8, 2, true, "1/43", TestName = "IsValidNumber_ValueNotMatch_Fails")]
[TestCase(3, 2, true, "00.00", TestName = "IsValidNumber_PrecisionLessCountNumbers_Fails")]
[TestCase(3, 2, false, "-0.00", TestName = "IsValidNumber_PrecisionLessCountNumbersAndSign_Fails")]
[TestCase(17, 2, true, "0.001", TestName = "IsValidNumber_ScaleLessFractPartLength_Fails")]
[TestCase(5, 4, true, "a.12", TestName = "IsValidNumber_LettersInValue_Fails")]
[TestCase(3, 2, true, ".12", TestName = "IsValidNumber_WithoutIntPart_Fails")]
[TestCase(3, 2, true, "-1.2", TestName = "IsValidNumber_OnlyPositiveTrueAndNegativeNumber_Fails")]
[TestCase(3, 2, true, null, TestName = "IsValidNumber_ValueIsNull_Fails")]
public void Fails(int precision, int scale, bool onlyPostive, string value)
{
var validator = new NumberValidator(precision, scale, onlyPostive);
var result = validator.IsValidNumber(value);
result.Should().BeFalse();
}

[TestCase(4, 2, true, "+1.23", TestName = "IsValidNumber_PrecisionEqualsCountNumbersAndSign_Success")]
[TestCase(3, 2, true, "5.13", TestName = "IsValidNumber_PrecisionEqualsCountNumbers_Success")]
[TestCase(4, 2, true, "2.25", TestName = "IsValidNumber_PrecisionBiggerCountNumbers_Success")]
[TestCase(4, 2, true, "9999", TestName = "IsValidNumber_PrecisionEqualsIntPart_Success")]
[TestCase(17, 2, true, "0.23", TestName = "IsValidNumber_ScaleEqualsFractPart_Success")]
[TestCase(17, 3, true, "0.12", TestName = "IsValidNumber_ScaleBiggerFractPart_Success")]
[TestCase(4, 3, true, "1,1", TestName = "IsValidNumber_SeparatorIsComma_Success")]
[TestCase(5, 4, false, "-1.5", TestName = "IsValidNumber_NumberIsNegativeAndOnlyPositiveIsFalse_Success")]
public void Success(int precision, int scale, bool onlyPostive, string value)
{
var validator = new NumberValidator(precision, scale, onlyPostive);
var result = validator.IsValidNumber(value);
result.Should().BeTrue();
}
}
}
Loading