Skip to content

Commit 538e147

Browse files
author
Alexander Drozdov
authored
Merge pull request #1 from qqeekk/develop
lab-4
2 parents 7c8b67a + 2b23e72 commit 538e147

20 files changed

+723
-5
lines changed

lab-2

lab-3/src/SetParsing/Parser.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public static T Parse<T>(string input)
5757
/// <list type="number">
5858
/// <item><see cref="decimal"/></item>
5959
/// <item><see cref="Complex"/></item>
60-
/// <item><see cref="Array"/></item>
60+
/// <item><see cref="IReadOnlySet{object}"/></item>
6161
/// </list>
6262
/// </summary>
6363
/// <param name="token">Token tree root.</param>
@@ -72,7 +72,7 @@ public static object Translate(TokenBase token, ICollection<ArgumentException> e
7272
{
7373
RealNumberToken num => num.Value,
7474
ComplexNumberToken cnum => new Complex(real: (double)cnum.Real, imaginary: (double)cnum.Imaginary),
75-
SetToken set => Array.ConvertAll(set.Tokens, t => Translate(t, errors)),
75+
SetToken set => MakeSet(Array.ConvertAll(set.Tokens, t => Translate(t, errors))),
7676
ErrorToken val => SetError(val.Message),
7777
{ } => throw new NotImplementedException(),
7878
null => throw new ArgumentNullException(nameof(token)),
@@ -140,5 +140,10 @@ private static (bool, decimal) TryParseDecimal(string number)
140140
result: out var parsed), parsed)
141141
};
142142
}
143+
144+
private static IReadOnlySet<object> MakeSet(IEnumerable<object> seq)
145+
{
146+
return new HashSet<object>(seq, new StructuralEqualityComparer());
147+
}
143148
}
144149
}

lab-3/src/SetParsing.Tests/StructuralEqualityComparer.cs renamed to lab-3/src/SetParsing/StructuralEqualityComparer.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
using System.Diagnostics.CodeAnalysis;
44
using System.Linq;
55

6-
namespace SetParsing.Tests
6+
namespace SetParsing
77
{
8-
internal class StructuralEqualityComparer : IEqualityComparer<object>
8+
public class StructuralEqualityComparer : IEqualityComparer<object>
99
{
1010
public new bool Equals(object x, object y)
1111
{

lab-4/README.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
Set Expressions Test Project
2+
===
3+
4+
### Вариант 3: Множество.
5+
6+
Реализованы и протестированы следующие операции:
7+
- Лексико-синтаксический разбор выражений, содержащих множества, в арифметические выражения над этими множествами;
8+
- Конечный автомат переходов между состояниями парсера (StateMachine, Machine/);
9+
10+
Исходный код тестов расположен в файлах SetCalculationTest.cs / SetCalculationTest.Data.cs
11+
12+
## Системные требования:
13+
- Одна из поддерживаемых платформ:
14+
- Wndows 7+ (Any CPU)
15+
- Linux (Arm32 | Arm64 | x64 | x64 Alpine)
16+
- macOS (x64)
17+
- .NET Core 3.1.x SDK (https://dotnet.microsoft.com/download/dotnet/3.1)
18+
19+
---
20+
Выполните следующую комманду чтобы убедиться, что указанные пакеты установленны корректно.
21+
```bash
22+
> dotnet --list-sdks
23+
```
24+
25+
Для генерации отчетов по качеству тестового покрытия установите следующую утилиту:
26+
```bash
27+
> dotnet tool install -g dotnet-reportgenerator-globaltool
28+
```
29+
30+
---
31+
## Инструкции по запуску
32+
Для запуска тестов выполните:
33+
34+
```bash
35+
> cd ./src
36+
> dotnet test -l:"console;verbosity=normal" /p:CollectCoverage=true /p:CoverletOutputFormat=cobertura
37+
> reportgenerator.exe "-reports:SetParsing.Tests/coverage.cobertura.xml" "-targetdir:report" -reporttypes:Html
38+
> ./report/index.html
39+
```
Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
using SetCalculations.Machine;
2+
using System;
3+
using System.Collections.Generic;
4+
5+
namespace SetCalculations.Tests
6+
{
7+
public partial class SetCalculationTest
8+
{
9+
public static IEnumerable<object[]> CalculationTestData()
10+
{
11+
yield return new object[]
12+
{
13+
Set(),
14+
string.Empty,
15+
MockParser(),
16+
};
17+
18+
yield return new object[]
19+
{
20+
Set(5M),
21+
"$0",
22+
MockParser(5M)
23+
};
24+
yield return new object[]
25+
{
26+
Set(Set(4M, 5M), 8M),
27+
"$0 + $1",
28+
MockParser(
29+
Set(8M, Set(4M, 5M)),
30+
Set(Set(4M, 5M))
31+
)
32+
};
33+
34+
yield return new object[]
35+
{
36+
Set(Set(4M, 5M), 8M, 3M),
37+
"$0 + $1 + $2",
38+
MockParser(
39+
Set(3M, 8M),
40+
Set(8M, Set(4M, 5M)),
41+
Set(Set(4M, 5M))
42+
)
43+
};
44+
45+
yield return new object[]
46+
{
47+
Set(Set(4M, 5M)),
48+
"$0 & $1",
49+
MockParser(
50+
Set(8M, Set(4M, 5M)),
51+
Set(Set(4M, 5M))
52+
)
53+
};
54+
55+
yield return new object[]
56+
{
57+
Set(8M, Set(4M, 5M)),
58+
"$0 & $1 + $2",
59+
MockParser(
60+
Set(3M, 8M),
61+
Set(8M, Set(4M, 5M)),
62+
Set(Set(4M, 5M))
63+
)
64+
};
65+
yield return new object[]
66+
{
67+
Set(8M),
68+
"$0 / $1",
69+
MockParser(
70+
Set(8M, Set(4M, 5M)),
71+
Set(Set(4M, 5M))
72+
)
73+
};
74+
yield return new object[]
75+
{
76+
Set(8M, Set(1M)),
77+
"$0 // $1",
78+
MockParser(
79+
Set(8M, Set(4M, 5M)),
80+
Set(Set(1M), Set(4M, 5M))
81+
)
82+
};
83+
}
84+
85+
public static IEnumerable<object[]> ExceptionTestData()
86+
{
87+
yield return new object[]
88+
{
89+
new[] { "+", null },
90+
typeof(InvalidOperationException),
91+
typeof(LeftArgumentState),
92+
};
93+
94+
yield return new object[]
95+
{
96+
new[] { "$0", "+", null },
97+
typeof(InvalidOperationException),
98+
typeof(RightArgumentState),
99+
};
100+
101+
yield return new object[]
102+
{
103+
new[] { "##", null },
104+
typeof(AggregateException),
105+
typeof(LeftArgumentState),
106+
};
107+
108+
yield return new object[]
109+
{
110+
new[] { "##", "+", null },
111+
typeof(AggregateException),
112+
typeof(LeftArgumentState),
113+
};
114+
115+
yield return new object[]
116+
{
117+
new[] { "$0", "x", null },
118+
typeof(InvalidOperationException),
119+
typeof(OperatorState),
120+
};
121+
122+
yield return new object[]
123+
{
124+
new[] { "$0", "$0", null },
125+
typeof(InvalidOperationException),
126+
typeof(OperatorState),
127+
};
128+
129+
yield return new object[]
130+
{
131+
new[] { "$0", "/", "/", null },
132+
typeof(InvalidOperationException),
133+
typeof(RightArgumentState),
134+
};
135+
136+
yield return new object[]
137+
{
138+
new[] { "$0", "/", "##", null },
139+
typeof(AggregateException),
140+
typeof(RightArgumentState),
141+
};
142+
143+
yield return new object[]
144+
{
145+
new[] { "$0" },
146+
typeof(InvalidOperationException),
147+
typeof(OperatorState),
148+
};
149+
150+
yield return new object[]
151+
{
152+
new[] { "$0", "+" },
153+
typeof(InvalidOperationException),
154+
typeof(RightArgumentState),
155+
};
156+
}
157+
158+
private static IReadOnlySet<object> Set(params object[] p)
159+
{
160+
return new HashSet<object>(p, new SetParsing.StructuralEqualityComparer());
161+
}
162+
}
163+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using Moq;
2+
using SetCalculations.Adapters;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using Xunit;
7+
8+
namespace SetCalculations.Tests
9+
{
10+
public partial class SetCalculationTest
11+
{
12+
[Theory]
13+
[MemberData(nameof(CalculationTestData))]
14+
public void CalculationTest(IReadOnlySet<object> result, string expression, Parser parser)
15+
{
16+
var tokens = expression.Split().Append(string.Empty);
17+
var set = new StateMachine(parser).Calculate(tokens);
18+
19+
Assert.True(result.SetEquals(set));
20+
}
21+
22+
[Theory]
23+
[MemberData(nameof(ExceptionTestData))]
24+
public void ExceptionTest(string[] expression, Type exceptionType, Type state)
25+
{
26+
var parser = MockParser(Set());
27+
var machine = new StateMachine(parser);
28+
29+
var ex = Assert.Throws(exceptionType, () => machine.Calculate(expression));
30+
Assert.StartsWith(state.Name, ex.Message);
31+
}
32+
33+
private static Parser MockParser(params object[] setups)
34+
{
35+
var parser = new Mock<Parser>(MockBehavior.Strict);
36+
for (var i = 0; i < setups.Length; i++)
37+
{
38+
parser.Setup(p => p.Parse("$" + i)).Returns(setups[i]);
39+
}
40+
41+
parser.Setup(p => p.Parse(It.Is<string>(s => !s.StartsWith("$"))))
42+
.Throws(new AggregateException(new ArgumentException("Error")));
43+
44+
return parser.Object;
45+
}
46+
}
47+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net5.0</TargetFramework>
5+
6+
<IsPackable>false</IsPackable>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<Compile Update="SetCalculationTest.Data.cs">
11+
<DependentUpon>SetCalculationTest.cs</DependentUpon>
12+
</Compile>
13+
</ItemGroup>
14+
15+
<ItemGroup>
16+
<PackageReference Include="coverlet.msbuild" Version="3.0.3">
17+
<PrivateAssets>all</PrivateAssets>
18+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
19+
</PackageReference>
20+
<PackageReference Include="moq" Version="4.16.1" />
21+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.4" />
22+
<PackageReference Include="xunit" Version="2.4.1" />
23+
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
24+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
25+
<PrivateAssets>all</PrivateAssets>
26+
</PackageReference>
27+
<PackageReference Include="coverlet.collector" Version="3.0.3">
28+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
29+
<PrivateAssets>all</PrivateAssets>
30+
</PackageReference>
31+
</ItemGroup>
32+
33+
<ItemGroup>
34+
<ProjectReference Include="..\SetCalculations\SetCalculations.csproj" />
35+
</ItemGroup>
36+
37+
</Project>

0 commit comments

Comments
 (0)