From 7fd738b7d2d7a509ccc3999683f0cde2cbe61641 Mon Sep 17 00:00:00 2001 From: Mark <112826355+NemirovichMark@users.noreply.github.com> Date: Fri, 20 Mar 2026 14:12:40 +0300 Subject: [PATCH 1/6] Create dotnet-desktop.yml --- .github/workflows/dotnet-desktop.yml | 127 +++++++++++++++++++++++++++ 1 file changed, 127 insertions(+) create mode 100644 .github/workflows/dotnet-desktop.yml diff --git a/.github/workflows/dotnet-desktop.yml b/.github/workflows/dotnet-desktop.yml new file mode 100644 index 0000000..f49809b --- /dev/null +++ b/.github/workflows/dotnet-desktop.yml @@ -0,0 +1,127 @@ +name: .NET Tests & Auto-commit + +on: + pull_request: + types: [opened, synchronize, reopened] + push: + branches-ignore: [main] + workflow_dispatch: + +permissions: + contents: write + pull-requests: write + issues: read + +jobs: + + check-target: + runs-on: ubuntu-latest + outputs: + pr_valid: ${{ steps.check.outputs.pr_valid }} + steps: + - name: 🚫 Проверка PR в main + id: check + shell: bash + run: | + PR_BASE="${{ github.event.pull_request.base.ref }}" + echo "Base branch: $PR_BASE" + if [ "$PR_BASE" = "main" ]; then + echo "❌ Нельзя отправлять работу в main ветку." + echo "pr_valid=false" >> $GITHUB_OUTPUT + exit 1 + else + echo "✅ PR направлен в '$PR_BASE', можно запускать тесты." + echo "pr_valid=true" >> $GITHUB_OUTPUT + fi + + tests: + name: Run test + runs-on: windows-latest + needs: check-target + if: | + (github.event_name == 'pull_request' && needs.check-target.outputs.pr_valid == 'true') || + github.event_name == 'push' || + github.event_name == 'workflow_dispatch' + strategy: + matrix: + configuration: [Debug] + + steps: + - name: Checkout code safely + uses: actions/checkout@v4 + with: + repository: ${{ github.event.pull_request.head.repo.full_name || github.repository }} + ref: ${{ github.event.pull_request.head.sha || github.ref }} + fetch-depth: 0 + + - name: Install .NET Core + uses: actions/setup-dotnet@v4 + with: + dotnet-version: 8.0.x + + - name: Setup MSBuild.exe + uses: microsoft/setup-msbuild@v2 + + - name: Restore + run: dotnet restore Lab9/Lab9.sln + + - name: Build + run: dotnet build Lab9/Lab9.sln --configuration ${{ matrix.configuration }} --no-restore + + - name: Run tests (league cascade) + id: cascade + shell: pwsh + env: + CFG: ${{ matrix.configuration }} + run: | + $proj = "Lab9test/Lab9test.csproj" + Write-Host "Building test project..." + dotnet build $proj -c $Env:CFG + + $dll = Join-Path $PWD "Lab9test\bin\$Env:CFG\net8.0\Lab9test.dll" + if (-not (Test-Path $dll)) { + Write-Error "DLL не найдена: $dll" + exit 1 + } + + $leagues = @{ + "Purple" = @("Task1","Task2","Task3","Task4","Task5") + "Blue" = @("Task1","Task2","Task3","Task4","Task5") + "Green" = @("Task1","Task2","Task3","Task4","Task5") + "White" = @("Task1","Task2","Task3","Task4","Task5") + } + + foreach ($leagueName in $leagues.Keys) { + Write-Host "Лига: $leagueName" + $allPassed = $true + + foreach ($cls in $leagues[$leagueName]) { + $filter = "FullyQualifiedName~$leagueName.$cls" + Write-Host "Запуск тестов класса: $leagueName.$cls" + dotnet vstest $dll --TestCaseFilter:"$filter" --Logger:"trx;LogFileName=test-$leagueName-$cls.trx" + + if ($LASTEXITCODE -ne 0) { + Write-Host "❌ Тест $leagueName.$cls не прошёл." + $allPassed = $false + } else { + Write-Host "✅ Тест $leagueName.$cls прошёл." + } + } + + if ($allPassed) { + Write-Host "✅ Все тесты лиги $leagueName прошли! Останавливаем каскад." + exit 0 + } else { + Write-Host "⚠️ Не все тесты лиги $leagueName прошли. Переходим к следующей лиге." + } + } + + Write-Host "❌ Ни одна лига полностью не прошла. Проверьте корректность загруженных файлов." + exit 1 + + - name: Upload TRX + if: always() + uses: actions/upload-artifact@v4 + with: + name: trx-${{ matrix.configuration }} + path: '**/test-*.trx' From 64457068c43d2a93a31665d6f3def083b31cba4d Mon Sep 17 00:00:00 2001 From: triplesixblast <128070058+triplesixblast@users.noreply.github.com> Date: Mon, 6 Apr 2026 10:25:17 +0300 Subject: [PATCH 2/6] Create practice_06_04.cs --- practice_06_04.cs | 125 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 125 insertions(+) create mode 100644 practice_06_04.cs diff --git a/practice_06_04.cs b/practice_06_04.cs new file mode 100644 index 0000000..e04b0d6 --- /dev/null +++ b/practice_06_04.cs @@ -0,0 +1,125 @@ +using System.Text; +using System.Text.RegularExpressions; + +namespace Indexators +{ + internal class Program + { + static void Main(string[] args) + { + var students = new Student[] + { + new Student("A", "B", new int[,] {{1,2,3}, {5, 5, 5}}), + new Student("A", "b", new int[,] {{4,4,5}, {5, 4, 5}}), + new Student("A", "c", new int[,] {{2,2,2}, {2, 3, 2}}) + + }; + + // foreach(var s in students) + // { + // System.Console.WriteLine(s[0]); + // } + + string str = "im a good student"; + string str2 = "no! im!"; + str = "no! im!"; + str2 = str; + + System.Console.WriteLine(str2); + // str2 = str.Substring(4, 3); + //str2 = str.Replace("no", "yes"); //StringComparison.InvariantCultureIgnoreCase + // int a = str.IndexOf('a'); + var strings = str.Split(new char[]{'.','?','!'}, StringSplitOptions.RemoveEmptyEntries); + + string text = "sdaljfhsajfhas"; + + foreach (var c in text) + { + bool isLetter = Char.IsLetter(c); + bool isDigit = Char.IsDigit(c); + bool isSpaceTabNewLine = Char.IsSeparator(c); + bool isPunctuation = Char.IsPunctuation(c); + } + + string output = $"new\ntext\ron\r\neach{Environment.NewLine}line"; + System.Console.WriteLine(output); + + StringBuilder s = new StringBuilder(); + s.Append("fsadf"); + s.Remove(1, 5); + + s.ToString(); + + Regex regex = new Regex(@"\d+1"); + } + } + + public class Student + { + string _name; + string _surname; + + int[,] _marks; + + public int[,] Marks => (int[,])_marks.Clone(); + public double[] avgMarks + { + get + { + if (_marks == null || _marks.GetLength(0) == 0 || _marks.GetLength(1) == 0) + return null; + var avg = new double[_marks.GetLength(0)]; + for (int i = 0; i < avg.Length; i++) + { + for (int j = 0; j < _marks.GetLength(1); j++) + { + avg[i] += (double)_marks[i, j] / _marks.GetLength(1); + } + } + return avg; + } + } + + // public double this[int idx] => avgMarks[idx]; + + public char this[int idx] => _name[idx]; + + public int this[int i, int j] => _marks[i, j]; + + public Student(string name, string surname, int[,] marks = null) + { + _name = name; + _surname = surname; + if (_marks != null) + _marks = marks; + } + + public override string ToString() + { + var output = _name + " " + _surname; + for (int i = 0; i < _marks.GetLength(0); i++) + { + for (int j = 0; j < _marks.GetLength(1); j++) + { + output += _marks[i, j] + " "; + } + output = output.TrimEnd(); + output += Environment.NewLine; + } + return output; + + // StringBuilder sb = new StringBuilder(_name); + // sb.Append(" ").Append(_surname); + // for (int i = 0; i < _marks.GetLength(0); i++) + // { + // for (int j = 0; j < _marks.GetLength(1); j++) + // { + // sb.Append(_marks[i, j]).Append(" "); + // } + // sb = sb.Remove(sb.Length-1, 1); + // sb.AppendLine(); + // } + // sb.ToString(); + } + } +} From ae0396bfea856eea063804d9125fccb0f27de720 Mon Sep 17 00:00:00 2001 From: Mark <112826355+NemirovichMark@users.noreply.github.com> Date: Fri, 10 Apr 2026 15:44:18 +0300 Subject: [PATCH 3/6] Update dotnet-desktop version.yml --- .github/workflows/dotnet-desktop.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.github/workflows/dotnet-desktop.yml b/.github/workflows/dotnet-desktop.yml index f49809b..2e34a78 100644 --- a/.github/workflows/dotnet-desktop.yml +++ b/.github/workflows/dotnet-desktop.yml @@ -57,7 +57,9 @@ jobs: - name: Install .NET Core uses: actions/setup-dotnet@v4 with: - dotnet-version: 8.0.x + dotnet-version: | + 8.0.x + 9.0.x - name: Setup MSBuild.exe uses: microsoft/setup-msbuild@v2 From 1c8e053d827d16742edeaa64024d652b0f958d13 Mon Sep 17 00:00:00 2001 From: Mark <112826355+NemirovichMark@users.noreply.github.com> Date: Fri, 10 Apr 2026 16:09:24 +0300 Subject: [PATCH 4/6] Update dotnet-desktop.yml --- .github/workflows/dotnet-desktop.yml | 54 +++++++++++++++++----------- 1 file changed, 34 insertions(+), 20 deletions(-) diff --git a/.github/workflows/dotnet-desktop.yml b/.github/workflows/dotnet-desktop.yml index 2e34a78..fff9e2e 100644 --- a/.github/workflows/dotnet-desktop.yml +++ b/.github/workflows/dotnet-desktop.yml @@ -76,49 +76,63 @@ jobs: env: CFG: ${{ matrix.configuration }} run: | - $proj = "Lab9test/Lab9test.csproj" + $testProjectPath = "Lab9Test/Lab9Test.csproj" + $testDir = "Lab9Test" + Write-Host "Building test project..." - dotnet build $proj -c $Env:CFG - - $dll = Join-Path $PWD "Lab9test\bin\$Env:CFG\net8.0\Lab9test.dll" + dotnet build $testProjectPath -c $Env:CFG --no-restore + + $tfm = (dotnet list $testProjectPath framework --framework net9.0 | Select-Object -Last 1).Trim() + if (-not $tfm) { $tfm = "net9.0" } + + $dll = Join-Path $PWD "$testDir\bin\$Env:CFG\$tfm\Lab9Test.dll" + + Write-Host "Ожидаемая DLL: $dll" + if (-not (Test-Path $dll)) { Write-Error "DLL не найдена: $dll" + Write-Host "`nСодержимое bin папки:" + Get-ChildItem "$testDir\bin" -Recurse -ErrorAction SilentlyContinue | + Select-Object FullName | Out-Host exit 1 } - + + Write-Host "DLL успешно найдена`n" + $leagues = @{ - "Purple" = @("Task1","Task2","Task3","Task4","Task5") - "Blue" = @("Task1","Task2","Task3","Task4","Task5") - "Green" = @("Task1","Task2","Task3","Task4","Task5") - "White" = @("Task1","Task2","Task3","Task4","Task5") + "Purple" = @("Task1","Task2","Task3","Task4") + "Blue" = @("Task1","Task2","Task3","Task4") + "Green" = @("Task1","Task2","Task3","Task4") + "White" = @("Task1","Task2","Task3","Task4") } - + foreach ($leagueName in $leagues.Keys) { Write-Host "Лига: $leagueName" $allPassed = $true - + foreach ($cls in $leagues[$leagueName]) { $filter = "FullyQualifiedName~$leagueName.$cls" - Write-Host "Запуск тестов класса: $leagueName.$cls" + Write-Host " Запуск: $leagueName.$cls" + dotnet vstest $dll --TestCaseFilter:"$filter" --Logger:"trx;LogFileName=test-$leagueName-$cls.trx" - + if ($LASTEXITCODE -ne 0) { - Write-Host "❌ Тест $leagueName.$cls не прошёл." + Write-Host " ❌ $leagueName.$cls не прошёл" -ForegroundColor Red $allPassed = $false } else { - Write-Host "✅ Тест $leagueName.$cls прошёл." + Write-Host " ✅ $leagueName.$cls прошёл" -ForegroundColor Green } } - + if ($allPassed) { - Write-Host "✅ Все тесты лиги $leagueName прошли! Останавливаем каскад." + Write-Host "✅ Все тесты лиги $leagueName прошли! Останавливаем каскад." -ForegroundColor Green exit 0 } else { - Write-Host "⚠️ Не все тесты лиги $leagueName прошли. Переходим к следующей лиге." + Write-Host "⚠️ Лига $leagueName не прошла полностью. Переходим к следующей..." -ForegroundColor Yellow } } - - Write-Host "❌ Ни одна лига полностью не прошла. Проверьте корректность загруженных файлов." + + Write-Host "❌ Ни одна лига полностью не прошла." -ForegroundColor Red exit 1 - name: Upload TRX From 3dddb5ecb3ebf28032e697278084c44d68908c86 Mon Sep 17 00:00:00 2001 From: triplesixblast Date: Tue, 14 Apr 2026 02:20:44 +0300 Subject: [PATCH 5/6] done --- Lab9Test/Purple/Task1.cs | 354 ++++++++++++++++++------------------ Lab9Test/Purple/Task2.cs | 374 +++++++++++++++++++-------------------- Lab9Test/Purple/Task3.cs | 344 +++++++++++++++++------------------ Lab9Test/Purple/Task4.cs | 306 ++++++++++++++++---------------- 4 files changed, 689 insertions(+), 689 deletions(-) diff --git a/Lab9Test/Purple/Task1.cs b/Lab9Test/Purple/Task1.cs index 53908f0..be22901 100644 --- a/Lab9Test/Purple/Task1.cs +++ b/Lab9Test/Purple/Task1.cs @@ -1,177 +1,177 @@ -//using System.Text.Json; -//using System.Linq; - -//namespace Lab9Test.Purple -//{ -// [TestClass] -// public sealed class Task1 -// { -// private Lab9.Purple.Task1 _student; - -// private string[] _input; -// private string[] _output; - -// [TestInitialize] -// public void LoadData() -// { -// var folder = Directory.GetParent(Directory.GetCurrentDirectory()).Parent.Parent.Parent.FullName; -// var file = Path.Combine(folder, "Lab9Test", "Purple", "data.json"); - -// var json = JsonSerializer.Deserialize( -// File.ReadAllText(file)); - -// _input = json.GetProperty("Purple1") -// .GetProperty("input") -// .Deserialize(); - -// _output = json.GetProperty("Purple1") -// .GetProperty("output") -// .Deserialize(); -// } - -// [TestMethod] -// public void Test_00_OOP() -// { -// var type = typeof(Lab9.Purple.Task1); - -// Assert.IsTrue(type.IsClass, "Task1 must be a class"); -// Assert.IsTrue(type.IsSubclassOf(typeof(Lab9.Purple.Purple)), -// "Task1 must inherit from Purple"); - -// Assert.IsNotNull( -// type.GetConstructor(new[] { typeof(string) }), -// "Constructor Task1(string input) not found" -// ); - -// Assert.IsNotNull(type.GetMethod("Review"), -// "Method Review() not found"); - -// Assert.IsNotNull(type.GetMethod("ToString"), -// "Method ToString() not found"); -// } - -// [TestMethod] -// public void Test_01_Input() -// { -// for (int i = 0; i < _input.Length; i++) -// { -// Init(i); - -// Assert.AreEqual(_input[i], _student.Input, -// $"Input stored incorrectly\nTest: {i}"); -// } -// } - -// [TestMethod] -// public void Test_02_Output() -// { -// for (int i = 0; i < _input.Length; i++) -// { -// Init(i); -// _student.Review(); - -// Assert.AreEqual(_output[i], _student.Output, -// $"Output mismatch\nTest: {i}"); -// } -// } - -// [TestMethod] -// public void Test_03_ToString() -// { -// for (int i = 0; i < _input.Length; i++) -// { -// Init(i); -// _student.Review(); - -// Assert.AreEqual(_output[i], _student.ToString(), -// $"ToString mismatch\nTest: {i}"); -// } -// } - -// [TestMethod] -// public void Test_04_ChangeText() -// { -// for (int i = 0; i < _input.Length; i++) -// { -// Init(i); -// _student.Review(); - -// var oldOutput = _student.Output; - -// var newText = _input[(i + 1) % _input.Length]; -// _student.ChangeText(newText); - -// Assert.AreEqual(newText, _student.Input, -// $"ChangeText failed\nTest: {i}"); - -// Assert.AreNotEqual(oldOutput, _student.Output, -// $"Output not updated after ChangeText\nTest: {i}"); -// } -// } - -// [TestMethod] -// public void Test_05_TypeSafety() -// { -// Init(0); -// _student.Review(); - -// Assert.IsInstanceOfType(_student.Output, typeof(string), -// $"Output must be string\nActual: {_student.Output.GetType()}"); -// } - -// [TestMethod] -// public void Test_06_Length() -// { -// for (int i = 0; i < _input.Length; i++) -// { -// Init(i); -// _student.Review(); - -// Assert.AreEqual(_output[i].Length, _student.ToString().Length, -// $"Length mismatch\nTest: {i}"); -// } -// } - -// [TestMethod] -// public void Test_07_PunctuationPreserved() -// { -// Init(0); -// _student.Review(); - -// var input = _student.Input; -// var output = _student.Output; - -// var inputPunct = new string(input.Where(c => !char.IsLetterOrDigit(c) && c != ' ').ToArray()); -// var outputPunct = new string(output.Where(c => !char.IsLetterOrDigit(c) && c != ' ').ToArray()); - -// Assert.AreEqual(inputPunct, outputPunct, -// "Punctuation must stay in the same places"); -// } - -// [TestMethod] -// public void Test_08_WordsReversed() -// { -// Init(0); -// _student.Review(); - -// var wordsIn = _student.Input.Split(' ', StringSplitOptions.RemoveEmptyEntries); -// var wordsOut = _student.Output.Split(' ', StringSplitOptions.RemoveEmptyEntries); - -// for (int i = 0; i < Math.Min(wordsIn.Length, wordsOut.Length); i++) -// { -// var cleanIn = new string(wordsIn[i].Where(char.IsLetter).ToArray()); -// var cleanOut = new string(wordsOut[i].Where(char.IsLetter).ToArray()); - -// var reversed = new string(cleanIn.Reverse().ToArray()); - -// Assert.AreEqual(reversed, cleanOut, -// $"Word not reversed\nIndex: {i}\nExpected: {reversed}\nActual: {cleanOut}"); -// } -// } - -// private void Init(int i) -// { -// _student = new Lab9.Purple.Task1(_input[i]); -// } -// } -//} \ No newline at end of file +using System.Text.Json; +using System.Linq; + +namespace Lab9Test.Purple +{ + [TestClass] + public sealed class Task1 + { + private Lab9.Purple.Task1 _student; + + private string[] _input; + private string[] _output; + + [TestInitialize] + public void LoadData() + { + var folder = Directory.GetParent(Directory.GetCurrentDirectory()).Parent.Parent.Parent.FullName; + var file = Path.Combine(folder, "Lab9Test", "Purple", "data.json"); + + var json = JsonSerializer.Deserialize( + File.ReadAllText(file)); + + _input = json.GetProperty("Purple1") + .GetProperty("input") + .Deserialize(); + + _output = json.GetProperty("Purple1") + .GetProperty("output") + .Deserialize(); + } + + [TestMethod] + public void Test_00_OOP() + { + var type = typeof(Lab9.Purple.Task1); + + Assert.IsTrue(type.IsClass, "Task1 must be a class"); + Assert.IsTrue(type.IsSubclassOf(typeof(Lab9.Purple.Purple)), + "Task1 must inherit from Purple"); + + Assert.IsNotNull( + type.GetConstructor(new[] { typeof(string) }), + "Constructor Task1(string input) not found" + ); + + Assert.IsNotNull(type.GetMethod("Review"), + "Method Review() not found"); + + Assert.IsNotNull(type.GetMethod("ToString"), + "Method ToString() not found"); + } + + [TestMethod] + public void Test_01_Input() + { + for (int i = 0; i < _input.Length; i++) + { + Init(i); + + Assert.AreEqual(_input[i], _student.Input, + $"Input stored incorrectly\nTest: {i}"); + } + } + + [TestMethod] + public void Test_02_Output() + { + for (int i = 0; i < _input.Length; i++) + { + Init(i); + _student.Review(); + + Assert.AreEqual(_output[i], _student.Output, + $"Output mismatch\nTest: {i}"); + } + } + + [TestMethod] + public void Test_03_ToString() + { + for (int i = 0; i < _input.Length; i++) + { + Init(i); + _student.Review(); + + Assert.AreEqual(_output[i], _student.ToString(), + $"ToString mismatch\nTest: {i}"); + } + } + + [TestMethod] + public void Test_04_ChangeText() + { + for (int i = 0; i < _input.Length; i++) + { + Init(i); + _student.Review(); + + var oldOutput = _student.Output; + + var newText = _input[(i + 1) % _input.Length]; + _student.ChangeText(newText); + + Assert.AreEqual(newText, _student.Input, + $"ChangeText failed\nTest: {i}"); + + Assert.AreNotEqual(oldOutput, _student.Output, + $"Output not updated after ChangeText\nTest: {i}"); + } + } + + [TestMethod] + public void Test_05_TypeSafety() + { + Init(0); + _student.Review(); + + Assert.IsInstanceOfType(_student.Output, typeof(string), + $"Output must be string\nActual: {_student.Output.GetType()}"); + } + + [TestMethod] + public void Test_06_Length() + { + for (int i = 0; i < _input.Length; i++) + { + Init(i); + _student.Review(); + + Assert.AreEqual(_output[i].Length, _student.ToString().Length, + $"Length mismatch\nTest: {i}"); + } + } + + [TestMethod] + public void Test_07_PunctuationPreserved() + { + Init(0); + _student.Review(); + + var input = _student.Input; + var output = _student.Output; + + var inputPunct = new string(input.Where(c => !char.IsLetterOrDigit(c) && c != ' ').ToArray()); + var outputPunct = new string(output.Where(c => !char.IsLetterOrDigit(c) && c != ' ').ToArray()); + + Assert.AreEqual(inputPunct, outputPunct, + "Punctuation must stay in the same places"); + } + + [TestMethod] + public void Test_08_WordsReversed() + { + Init(0); + _student.Review(); + + var wordsIn = _student.Input.Split(' ', StringSplitOptions.RemoveEmptyEntries); + var wordsOut = _student.Output.Split(' ', StringSplitOptions.RemoveEmptyEntries); + + for (int i = 0; i < Math.Min(wordsIn.Length, wordsOut.Length); i++) + { + var cleanIn = new string(wordsIn[i].Where(char.IsLetter).ToArray()); + var cleanOut = new string(wordsOut[i].Where(char.IsLetter).ToArray()); + + var reversed = new string(cleanIn.Reverse().ToArray()); + + Assert.AreEqual(reversed, cleanOut, + $"Word not reversed\nIndex: {i}\nExpected: {reversed}\nActual: {cleanOut}"); + } + } + + private void Init(int i) + { + _student = new Lab9.Purple.Task1(_input[i]); + } + } +} \ No newline at end of file diff --git a/Lab9Test/Purple/Task2.cs b/Lab9Test/Purple/Task2.cs index dafb515..a4f41ed 100644 --- a/Lab9Test/Purple/Task2.cs +++ b/Lab9Test/Purple/Task2.cs @@ -1,187 +1,187 @@ -//using System.Text.Json; -//using System.Linq; - -//namespace Lab9Test.Purple -//{ -// [TestClass] -// public sealed class Task2 -// { -// private Lab9.Purple.Task2 _student; - -// private string[] _input; -// private string[][] _output; - -// [TestInitialize] -// public void LoadData() -// { -// var folder = Directory.GetParent(Directory.GetCurrentDirectory()).Parent.Parent.Parent.FullName; -// var file = Path.Combine(folder, "Lab9Test", "Purple", "data.json"); - -// var json = JsonSerializer.Deserialize( -// File.ReadAllText(file)); - -// _input = json.GetProperty("Purple2") -// .GetProperty("input") -// .Deserialize(); - -// _output = json.GetProperty("Purple2") -// .GetProperty("output") -// .Deserialize(); -// } - -// [TestMethod] -// public void Test_00_OOP() -// { -// var type = typeof(Lab9.Purple.Task2); - -// Assert.IsTrue(type.IsClass); -// Assert.IsTrue(type.IsSubclassOf(typeof(Lab9.Purple.Purple))); - -// Assert.IsNotNull(type.GetConstructor(new[] { typeof(string) })); -// Assert.IsNotNull(type.GetMethod("Review")); -// Assert.IsNotNull(type.GetMethod("ToString")); -// } - -// [TestMethod] -// public void Test_01_Output() -// { -// for (int i = 0; i < _input.Length; i++) -// { -// Init(i); -// _student.Review(); - -// var actual = _student.Output; -// var expected = _output[i]; - -// Assert.AreEqual(expected.Length, actual.Length, -// $"Line count mismatch\nTest: {i}"); - -// for (int j = 0; j < expected.Length; j++) -// { -// Assert.AreEqual(expected[j], actual[j], -// $"Line mismatch\nTest: {i}, Line: {j}"); -// } -// } -// } - -// [TestMethod] -// public void Test_02_LineLength() -// { -// for (int i = 0; i < _input.Length; i++) -// { -// Init(i); -// _student.Review(); - -// for (int j = 0; j < _output[i].Length; j++) -// { -// Assert.AreEqual(_output[i][j].Length, _student.Output[j].Length, -// $"Line length must be 50\nActual: {_student.Output[j].Length}\nLine: {_student.Output[j]}"); -// } -// } -// } - -// [TestMethod] -// public void Test_03_NoWordBreak() -// { -// Init(0); -// _student.Review(); - -// foreach (var line in _student.Output) -// { -// Assert.IsFalse(line.StartsWith(" "), -// "Line starts with space"); - -// Assert.IsFalse(line.EndsWith(" "), -// "Line ends with space"); -// } -// } - -// [TestMethod] -// public void Test_04_SpacesDistributed() -// { -// Init(0); -// _student.Review(); - -// foreach (var line in _student.Output) -// { -// var words = line.Split(' ', StringSplitOptions.RemoveEmptyEntries); - -// if (words.Length <= 1) continue; - -// int spaces = line.Count(c => c == ' '); -// int minSpaces = (50 - words.Sum(w => w.Length)) / (words.Length - 1); - -// Assert.IsTrue(spaces >= minSpaces, -// $"Spaces distributed incorrectly\nLine: {line}"); -// } -// } - -// [TestMethod] -// public void Test_05_ToString() -// { -// for (int i = 0; i < _input.Length; i++) -// { -// Init(i); -// _student.Review(); - -// var expected = string.Join(Environment.NewLine, _output[i]); -// var actual = _student.ToString(); - -// Assert.AreEqual(expected, actual, -// $"ToString mismatch\nTest: {i}"); -// } -// } - -// [TestMethod] -// public void Test_06_ChangeText() -// { -// for (int i = 0; i < _input.Length; i++) -// { -// Init(i); -// _student.Review(); - -// var old = _student.Output; - -// var newText = _input[(i + 1) % _input.Length]; -// _student.ChangeText(newText); - -// Assert.AreEqual(newText, _student.Input, -// $"ChangeText failed\nTest: {i}"); - -// Assert.IsFalse(old.SequenceEqual(_student.Output), -// $"Output not updated\nTest: {i}"); -// } -// } - -// [TestMethod] -// public void Test_07_TypeSafety() -// { -// Init(0); -// _student.Review(); - -// Assert.IsInstanceOfType(_student.Output, typeof(string[]), -// $"Output must be string[]\nActual: {_student.Output.GetType()}"); -// } - -// [TestMethod] -// public void Test_08_ToStringLength() -// { -// for (int i = 0; i < _input.Length; i++) -// { -// Init(i); -// _student.Review(); - -// var expectedLength = string.Join(Environment.NewLine, _output[i]).Length; -// var actualLength = _student.ToString().Length; - -// Assert.AreEqual(expectedLength, actualLength, -// $"Wrong ToString length\nTest: {i}"); -// } -// } - -// private void Init(int i) -// { -// _student = new Lab9.Purple.Task2(_input[i]); -// } -// } -//} \ No newline at end of file +using System.Text.Json; +using System.Linq; + +namespace Lab9Test.Purple +{ + [TestClass] + public sealed class Task2 + { + private Lab9.Purple.Task2 _student; + + private string[] _input; + private string[][] _output; + + [TestInitialize] + public void LoadData() + { + var folder = Directory.GetParent(Directory.GetCurrentDirectory()).Parent.Parent.Parent.FullName; + var file = Path.Combine(folder, "Lab9Test", "Purple", "data.json"); + + var json = JsonSerializer.Deserialize( + File.ReadAllText(file)); + + _input = json.GetProperty("Purple2") + .GetProperty("input") + .Deserialize(); + + _output = json.GetProperty("Purple2") + .GetProperty("output") + .Deserialize(); + } + + [TestMethod] + public void Test_00_OOP() + { + var type = typeof(Lab9.Purple.Task2); + + Assert.IsTrue(type.IsClass); + Assert.IsTrue(type.IsSubclassOf(typeof(Lab9.Purple.Purple))); + + Assert.IsNotNull(type.GetConstructor(new[] { typeof(string) })); + Assert.IsNotNull(type.GetMethod("Review")); + Assert.IsNotNull(type.GetMethod("ToString")); + } + + [TestMethod] + public void Test_01_Output() + { + for (int i = 0; i < _input.Length; i++) + { + Init(i); + _student.Review(); + + var actual = _student.Output; + var expected = _output[i]; + + Assert.AreEqual(expected.Length, actual.Length, + $"Line count mismatch\nTest: {i}"); + + for (int j = 0; j < expected.Length; j++) + { + Assert.AreEqual(expected[j], actual[j], + $"Line mismatch\nTest: {i}, Line: {j}"); + } + } + } + + [TestMethod] + public void Test_02_LineLength() + { + for (int i = 0; i < _input.Length; i++) + { + Init(i); + _student.Review(); + + for (int j = 0; j < _output[i].Length; j++) + { + Assert.AreEqual(_output[i][j].Length, _student.Output[j].Length, + $"Line length must be 50\nActual: {_student.Output[j].Length}\nLine: {_student.Output[j]}"); + } + } + } + + [TestMethod] + public void Test_03_NoWordBreak() + { + Init(0); + _student.Review(); + + foreach (var line in _student.Output) + { + Assert.IsFalse(line.StartsWith(" "), + "Line starts with space"); + + Assert.IsFalse(line.EndsWith(" "), + "Line ends with space"); + } + } + + [TestMethod] + public void Test_04_SpacesDistributed() + { + Init(0); + _student.Review(); + + foreach (var line in _student.Output) + { + var words = line.Split(' ', StringSplitOptions.RemoveEmptyEntries); + + if (words.Length <= 1) continue; + + int spaces = line.Count(c => c == ' '); + int minSpaces = (50 - words.Sum(w => w.Length)) / (words.Length - 1); + + Assert.IsTrue(spaces >= minSpaces, + $"Spaces distributed incorrectly\nLine: {line}"); + } + } + + [TestMethod] + public void Test_05_ToString() + { + for (int i = 0; i < _input.Length; i++) + { + Init(i); + _student.Review(); + + var expected = string.Join(Environment.NewLine, _output[i]); + var actual = _student.ToString(); + + Assert.AreEqual(expected, actual, + $"ToString mismatch\nTest: {i}"); + } + } + + [TestMethod] + public void Test_06_ChangeText() + { + for (int i = 0; i < _input.Length; i++) + { + Init(i); + _student.Review(); + + var old = _student.Output; + + var newText = _input[(i + 1) % _input.Length]; + _student.ChangeText(newText); + + Assert.AreEqual(newText, _student.Input, + $"ChangeText failed\nTest: {i}"); + + Assert.IsFalse(old.SequenceEqual(_student.Output), + $"Output not updated\nTest: {i}"); + } + } + + [TestMethod] + public void Test_07_TypeSafety() + { + Init(0); + _student.Review(); + + Assert.IsInstanceOfType(_student.Output, typeof(string[]), + $"Output must be string[]\nActual: {_student.Output.GetType()}"); + } + + [TestMethod] + public void Test_08_ToStringLength() + { + for (int i = 0; i < _input.Length; i++) + { + Init(i); + _student.Review(); + + var expectedLength = string.Join(Environment.NewLine, _output[i]).Length; + var actualLength = _student.ToString().Length; + + Assert.AreEqual(expectedLength, actualLength, + $"Wrong ToString length\nTest: {i}"); + } + } + + private void Init(int i) + { + _student = new Lab9.Purple.Task2(_input[i]); + } + } +} \ No newline at end of file diff --git a/Lab9Test/Purple/Task3.cs b/Lab9Test/Purple/Task3.cs index 07799d1..079018c 100644 --- a/Lab9Test/Purple/Task3.cs +++ b/Lab9Test/Purple/Task3.cs @@ -1,172 +1,172 @@ -//using System.Text.Json; -//using System.Linq; - -//namespace Lab9Test.Purple -//{ -// [TestClass] -// public sealed class Task3 -// { -// private Lab9.Purple.Task3 _student; - -// private string[] _input; -// private string[] _output; - -// [TestInitialize] -// public void LoadData() -// { -// var folder = Directory.GetParent(Directory.GetCurrentDirectory()).Parent.Parent.Parent.FullName; -// var file = Path.Combine(folder, "Lab9Test", "Purple", "data.json"); - -// var json = JsonSerializer.Deserialize( -// File.ReadAllText(file)); - -// _input = json.GetProperty("Purple3") -// .GetProperty("input") -// .Deserialize(); - -// _output = json.GetProperty("Purple3") -// .GetProperty("output") -// .Deserialize(); -// } - -// [TestMethod] -// public void Test_00_OOP() -// { -// var type = typeof(Lab9.Purple.Task3); - -// Assert.IsTrue(type.IsClass); -// Assert.IsTrue(type.IsSubclassOf(typeof(Lab9.Purple.Purple))); - -// Assert.IsNotNull(type.GetConstructor(new[] { typeof(string) })); -// Assert.IsNotNull(type.GetMethod("Review")); -// Assert.IsNotNull(type.GetMethod("ToString")); -// } - -// [TestMethod] -// public void Test_01_Output() -// { -// for (int i = 0; i < _input.Length; i++) -// { -// Init(i); -// _student.Review(); - -// Assert.AreEqual(_output[i], _student.Output, -// $"Output mismatch\nTest: {i}"); -// } -// } - -// [TestMethod] -// public void Test_02_ToString() -// { -// for (int i = 0; i < _input.Length; i++) -// { -// Init(i); -// _student.Review(); - -// Assert.AreEqual(_output[i], _student.ToString(), -// $"ToString mismatch\nTest: {i}"); -// } -// } - -// //[TestMethod] -// //public void Test_03_Codes() -// //{ -// // for (int i = 0; i < _input.Length; i++) -// // { -// // Init(i); -// // _student.Review(); - -// // var actual = _student.Codes; -// // var expected = _codes[i]; - -// // Assert.AreEqual(expected.Length, actual.Length, -// // $"Codes length mismatch\nTest: {i}"); - -// // for (int j = 0; j < expected.Length; j++) -// // { -// // Assert.AreEqual(expected[j].Item1, actual[j].Item1, -// // $"Pair mismatch\nTest: {i}, Index: {j}"); - -// // Assert.AreEqual(expected[j].Item2, actual[j].Item2, -// // $"Code mismatch\nTest: {i}, Index: {j}"); -// // } -// // } -// //} - -// [TestMethod] -// public void Test_04_CodesLimit() -// { -// Init(0); -// _student.Review(); - -// Assert.IsTrue(_student.Codes.Length <= 5); -// } - -// [TestMethod] -// public void Test_05_NoOriginalPairsInOutput() -// { -// Init(0); -// _student.Review(); - -// var output = _student.Output; - -// foreach (var pair in _student.Codes) -// { -// Assert.IsFalse(output.Contains(pair.Item1), -// $"Pair not replaced: {pair.Item1}"); -// } -// } - -// [TestMethod] -// public void Test_06_ChangeText() -// { -// for (int i = 0; i < _input.Length; i++) -// { -// Init(i); -// _student.Review(); - -// foreach (var pair in _student.Codes) -// Console.WriteLine($"{pair.Item1}:{pair.Item2}"); -// Console.WriteLine(); - -// var oldOutput = _student.Output; - -// var newText = _input[(i + 1) % _input.Length]; -// _student.ChangeText(newText); - -// Assert.AreEqual(newText, _student.Input); -// Assert.AreNotEqual(oldOutput, _student.Output); -// } -// } - -// [TestMethod] -// public void Test_07_TypeSafety() -// { -// Init(0); -// _student.Review(); - -// Assert.IsInstanceOfType(_student.Output, typeof(string)); -// Assert.IsInstanceOfType(_student.Codes, typeof((string, char)[])); -// } - -// [TestMethod] -// public void Test_08_CodeSymbolsNotInOriginal() -// { -// Init(0); -// _student.Review(); - -// var input = _student.Input; - -// foreach (var code in _student.Codes) -// { -// Assert.IsFalse(input.Contains(code.Item2), -// $"Code symbol already exists in input: {code.Item2}"); -// } -// } - -// private void Init(int i) -// { -// _student = new Lab9.Purple.Task3(_input[i]); -// } -// } -//} \ No newline at end of file +using System.Text.Json; +using System.Linq; + +namespace Lab9Test.Purple +{ + [TestClass] + public sealed class Task3 + { + private Lab9.Purple.Task3 _student; + + private string[] _input; + private string[] _output; + + [TestInitialize] + public void LoadData() + { + var folder = Directory.GetParent(Directory.GetCurrentDirectory()).Parent.Parent.Parent.FullName; + var file = Path.Combine(folder, "Lab9Test", "Purple", "data.json"); + + var json = JsonSerializer.Deserialize( + File.ReadAllText(file)); + + _input = json.GetProperty("Purple3") + .GetProperty("input") + .Deserialize(); + + _output = json.GetProperty("Purple3") + .GetProperty("output") + .Deserialize(); + } + + [TestMethod] + public void Test_00_OOP() + { + var type = typeof(Lab9.Purple.Task3); + + Assert.IsTrue(type.IsClass); + Assert.IsTrue(type.IsSubclassOf(typeof(Lab9.Purple.Purple))); + + Assert.IsNotNull(type.GetConstructor(new[] { typeof(string) })); + Assert.IsNotNull(type.GetMethod("Review")); + Assert.IsNotNull(type.GetMethod("ToString")); + } + + [TestMethod] + public void Test_01_Output() + { + for (int i = 0; i < _input.Length; i++) + { + Init(i); + _student.Review(); + + Assert.AreEqual(_output[i], _student.Output, + $"Output mismatch\nTest: {i}"); + } + } + + [TestMethod] + public void Test_02_ToString() + { + for (int i = 0; i < _input.Length; i++) + { + Init(i); + _student.Review(); + + Assert.AreEqual(_output[i], _student.ToString(), + $"ToString mismatch\nTest: {i}"); + } + } + + //[TestMethod] + //public void Test_03_Codes() + //{ + // for (int i = 0; i < _input.Length; i++) + // { + // Init(i); + // _student.Review(); + + // var actual = _student.Codes; + // var expected = _codes[i]; + + // Assert.AreEqual(expected.Length, actual.Length, + // $"Codes length mismatch\nTest: {i}"); + + // for (int j = 0; j < expected.Length; j++) + // { + // Assert.AreEqual(expected[j].Item1, actual[j].Item1, + // $"Pair mismatch\nTest: {i}, Index: {j}"); + + // Assert.AreEqual(expected[j].Item2, actual[j].Item2, + // $"Code mismatch\nTest: {i}, Index: {j}"); + // } + // } + //} + + [TestMethod] + public void Test_04_CodesLimit() + { + Init(0); + _student.Review(); + + Assert.IsTrue(_student.Codes.Length <= 5); + } + + [TestMethod] + public void Test_05_NoOriginalPairsInOutput() + { + Init(0); + _student.Review(); + + var output = _student.Output; + + foreach (var pair in _student.Codes) + { + Assert.IsFalse(output.Contains(pair.Item1), + $"Pair not replaced: {pair.Item1}"); + } + } + + [TestMethod] + public void Test_06_ChangeText() + { + for (int i = 0; i < _input.Length; i++) + { + Init(i); + _student.Review(); + + foreach (var pair in _student.Codes) + Console.WriteLine($"{pair.Item1}:{pair.Item2}"); + Console.WriteLine(); + + var oldOutput = _student.Output; + + var newText = _input[(i + 1) % _input.Length]; + _student.ChangeText(newText); + + Assert.AreEqual(newText, _student.Input); + Assert.AreNotEqual(oldOutput, _student.Output); + } + } + + [TestMethod] + public void Test_07_TypeSafety() + { + Init(0); + _student.Review(); + + Assert.IsInstanceOfType(_student.Output, typeof(string)); + Assert.IsInstanceOfType(_student.Codes, typeof((string, char)[])); + } + + [TestMethod] + public void Test_08_CodeSymbolsNotInOriginal() + { + Init(0); + _student.Review(); + + var input = _student.Input; + + foreach (var code in _student.Codes) + { + Assert.IsFalse(input.Contains(code.Item2), + $"Code symbol already exists in input: {code.Item2}"); + } + } + + private void Init(int i) + { + _student = new Lab9.Purple.Task3(_input[i]); + } + } +} \ No newline at end of file diff --git a/Lab9Test/Purple/Task4.cs b/Lab9Test/Purple/Task4.cs index c027307..3e3ee98 100644 --- a/Lab9Test/Purple/Task4.cs +++ b/Lab9Test/Purple/Task4.cs @@ -1,153 +1,153 @@ -//using System.Text.Json; -//using System.Linq; - -//namespace Lab9Test.Purple -//{ -// [TestClass] -// public sealed class Task4 -// { -// private Lab9.Purple.Task4 _student; - -// private string[] _input; -// private (string, char)[][] _codes; -// private string[] _output; -// [TestInitialize] -// public void LoadData() -// { -// var folder = Directory.GetParent(Directory.GetCurrentDirectory()).Parent.Parent.Parent.FullName; -// var file = Path.Combine(folder, "Lab9Test", "Purple", "data.json"); -// var jsonText = File.ReadAllText(file); -// var json = JsonSerializer.Deserialize(jsonText); - -// var purple4 = json.GetProperty("Purple4"); - -// _input = purple4.GetProperty("input").Deserialize(); -// _output = purple4.GetProperty("output").Deserialize(); - -// // Десериализация кодов -// var codeGroups = purple4.GetProperty("codes").EnumerateArray(); -// var listOfArrays = new List<(string, char)[]>(); - -// foreach (var group in codeGroups) -// { -// var pairs = new List<(string, char)>(); -// foreach (var entry in group.EnumerateArray()) -// { -// string pair = entry.GetProperty("pair").GetString() ?? ""; -// string codeStr = entry.GetProperty("code").GetString() ?? ""; -// char code = string.IsNullOrEmpty(codeStr) ? '\0' : codeStr[0]; -// pairs.Add((pair, code)); -// } -// listOfArrays.Add(pairs.ToArray()); -// } - -// _codes = listOfArrays.ToArray(); -// } - -// [TestMethod] -// public void Test_00_OOP() -// { -// var type = typeof(Lab9.Purple.Task4); - -// Assert.IsTrue(type.IsClass); -// Assert.IsTrue(type.IsSubclassOf(typeof(Lab9.Purple.Purple))); - -// Assert.IsNotNull(type.GetConstructor(new[] { typeof(string), typeof((string, char)[]) })); -// Assert.IsNotNull(type.GetMethod("Review")); -// Assert.IsNotNull(type.GetMethod("ToString")); -// } - -// [TestMethod] -// public void Test_01_Output() -// { -// for (int i = 0; i < _input.Length; i++) -// { -// Init(i); -// _student.Review(); - -// Assert.AreEqual(_output[i], _student.Output, -// $"Output mismatch\nTest: {i}"); -// } -// } - -// [TestMethod] -// public void Test_02_ToString() -// { -// for (int i = 0; i < _input.Length; i++) -// { -// Init(i); -// _student.Review(); - -// Assert.AreEqual(_output[i], _student.ToString(), -// $"ToString mismatch\nTest: {i}"); -// } -// } - -// [TestMethod] -// public void Test_03_DecodeAllCodes() -// { -// Init(0); -// _student.Review(); - -// var output = _student.Output; - -// foreach (var code in _codes[0]) -// { -// Assert.IsFalse(output.Contains(code.Item2), -// $"Code symbol not decoded: {code.Item2}"); - -// Assert.IsTrue(output.Contains(code.Item1), -// $"Pair not restored: {code.Item1}"); -// } -// } - -// [TestMethod] -// public void Test_04_ChangeText() -// { -// for (int i = 0; i < _input.Length; i++) -// { -// Init(i); -// _student.Review(); - -// var old = _student.Output; - -// var newText = _input[(i + 1) % _input.Length]; -// var newCodes = _codes[(i + 1) % _codes.Length]; - -// _student.ChangeText(newText); -// _student = new Lab9.Purple.Task4(newText, newCodes); -// _student.Review(); - -// Assert.AreEqual(newText, _student.Input); -// Assert.AreNotEqual(old, _student.Output); -// } -// } - -// [TestMethod] -// public void Test_05_TypeSafety() -// { -// Init(0); -// _student.Review(); - -// Assert.IsInstanceOfType(_student.Output, typeof(string)); -// } - -// [TestMethod] -// public void Test_06_Length() -// { -// for (int i = 0; i < _input.Length; i++) -// { -// Init(i); -// _student.Review(); - -// Assert.AreEqual(_output[i].Length, _student.ToString().Length, -// $"Length mismatch\nTest: {i}"); -// } -// } - -// private void Init(int i) -// { -// _student = new Lab9.Purple.Task4(_input[i], _codes[i]); -// } -// } -//} \ No newline at end of file +using System.Text.Json; +using System.Linq; + +namespace Lab9Test.Purple +{ + [TestClass] + public sealed class Task4 + { + private Lab9.Purple.Task4 _student; + + private string[] _input; + private (string, char)[][] _codes; + private string[] _output; + [TestInitialize] + public void LoadData() + { + var folder = Directory.GetParent(Directory.GetCurrentDirectory()).Parent.Parent.Parent.FullName; + var file = Path.Combine(folder, "Lab9Test", "Purple", "data.json"); + var jsonText = File.ReadAllText(file); + var json = JsonSerializer.Deserialize(jsonText); + + var purple4 = json.GetProperty("Purple4"); + + _input = purple4.GetProperty("input").Deserialize(); + _output = purple4.GetProperty("output").Deserialize(); + + // Десериализация кодов + var codeGroups = purple4.GetProperty("codes").EnumerateArray(); + var listOfArrays = new List<(string, char)[]>(); + + foreach (var group in codeGroups) + { + var pairs = new List<(string, char)>(); + foreach (var entry in group.EnumerateArray()) + { + string pair = entry.GetProperty("pair").GetString() ?? ""; + string codeStr = entry.GetProperty("code").GetString() ?? ""; + char code = string.IsNullOrEmpty(codeStr) ? '\0' : codeStr[0]; + pairs.Add((pair, code)); + } + listOfArrays.Add(pairs.ToArray()); + } + + _codes = listOfArrays.ToArray(); + } + + [TestMethod] + public void Test_00_OOP() + { + var type = typeof(Lab9.Purple.Task4); + + Assert.IsTrue(type.IsClass); + Assert.IsTrue(type.IsSubclassOf(typeof(Lab9.Purple.Purple))); + + Assert.IsNotNull(type.GetConstructor(new[] { typeof(string), typeof((string, char)[]) })); + Assert.IsNotNull(type.GetMethod("Review")); + Assert.IsNotNull(type.GetMethod("ToString")); + } + + [TestMethod] + public void Test_01_Output() + { + for (int i = 0; i < _input.Length; i++) + { + Init(i); + _student.Review(); + + Assert.AreEqual(_output[i], _student.Output, + $"Output mismatch\nTest: {i}"); + } + } + + [TestMethod] + public void Test_02_ToString() + { + for (int i = 0; i < _input.Length; i++) + { + Init(i); + _student.Review(); + + Assert.AreEqual(_output[i], _student.ToString(), + $"ToString mismatch\nTest: {i}"); + } + } + + [TestMethod] + public void Test_03_DecodeAllCodes() + { + Init(0); + _student.Review(); + + var output = _student.Output; + + foreach (var code in _codes[0]) + { + Assert.IsFalse(output.Contains(code.Item2), + $"Code symbol not decoded: {code.Item2}"); + + Assert.IsTrue(output.Contains(code.Item1), + $"Pair not restored: {code.Item1}"); + } + } + + [TestMethod] + public void Test_04_ChangeText() + { + for (int i = 0; i < _input.Length; i++) + { + Init(i); + _student.Review(); + + var old = _student.Output; + + var newText = _input[(i + 1) % _input.Length]; + var newCodes = _codes[(i + 1) % _codes.Length]; + + _student.ChangeText(newText); + _student = new Lab9.Purple.Task4(newText, newCodes); + _student.Review(); + + Assert.AreEqual(newText, _student.Input); + Assert.AreNotEqual(old, _student.Output); + } + } + + [TestMethod] + public void Test_05_TypeSafety() + { + Init(0); + _student.Review(); + + Assert.IsInstanceOfType(_student.Output, typeof(string)); + } + + [TestMethod] + public void Test_06_Length() + { + for (int i = 0; i < _input.Length; i++) + { + Init(i); + _student.Review(); + + Assert.AreEqual(_output[i].Length, _student.ToString().Length, + $"Length mismatch\nTest: {i}"); + } + } + + private void Init(int i) + { + _student = new Lab9.Purple.Task4(_input[i], _codes[i]); + } + } +} \ No newline at end of file From 9f2918aba3807161b6caeb68a30451426ba60d44 Mon Sep 17 00:00:00 2001 From: triplesixblast Date: Tue, 14 Apr 2026 02:30:14 +0300 Subject: [PATCH 6/6] done1 --- .gitignore | 2 ++ Lab9/Purple/Purple.cs | 17 ++++++++++++ Lab9/Purple/Task1.cs | 43 +++++++++++++++++++++++++++++ Lab9/Purple/Task2.cs | 64 +++++++++++++++++++++++++++++++++++++++++++ Lab9/Purple/Task3.cs | 44 +++++++++++++++++++++++++++++ Lab9/Purple/Task4.cs | 31 +++++++++++++++++++++ 6 files changed, 201 insertions(+) create mode 100644 .gitignore create mode 100644 Lab9/Purple/Task1.cs create mode 100644 Lab9/Purple/Task2.cs create mode 100644 Lab9/Purple/Task3.cs create mode 100644 Lab9/Purple/Task4.cs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f181d71 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +**/bin +**/obj diff --git a/Lab9/Purple/Purple.cs b/Lab9/Purple/Purple.cs index c00ca3e..57526dc 100644 --- a/Lab9/Purple/Purple.cs +++ b/Lab9/Purple/Purple.cs @@ -1,3 +1,20 @@ namespace Lab9.Purple { + public abstract class Purple + { + public string Input { get; private set; } + + protected Purple(string text) + { + Input = text; + } + + public abstract void Review(); + + public virtual void ChangeText(string text) + { + Input = text; + Review(); + } + } } \ No newline at end of file diff --git a/Lab9/Purple/Task1.cs b/Lab9/Purple/Task1.cs new file mode 100644 index 0000000..3559e2f --- /dev/null +++ b/Lab9/Purple/Task1.cs @@ -0,0 +1,43 @@ +using System.Text; + +namespace Lab9.Purple; + +public class Task1 : Purple +{ + public string Output { get; private set; } + + public Task1(string text) : base(text) + { + Output = string.Empty; + } + + public override void Review() + { + Output = string.Join(" ", Input.Split().Select(Reverse)); + } + + private static string Reverse(string text) + { + if (string.IsNullOrEmpty(text)) return text; + + if (text.Any(char.IsDigit)) return text; + + int start = 0; + int end = text.Length-1; + + while (start <= end && !char.IsLetter(text[start])) start++; + while (end >= start && !char.IsLetter(text[end])) end--; + + if (start > end) return text; + + var sb = new StringBuilder(text.Length); + sb.Append(text, 0, start); + sb.Append(new string(text[start..(end+1)] + .Reverse() + .ToArray())); + sb.Append(text, end+1, text.Length-end-1); + return sb.ToString(); + } + + public override string ToString() => Output ?? string.Empty; +} diff --git a/Lab9/Purple/Task2.cs b/Lab9/Purple/Task2.cs new file mode 100644 index 0000000..89c9e96 --- /dev/null +++ b/Lab9/Purple/Task2.cs @@ -0,0 +1,64 @@ +using System.Text; + +namespace Lab9.Purple; + +public class Task2 : Purple +{ + public string[] Output { get; private set; } + + public Task2(string text) : base(text) + { + Output = Array.Empty(); + } + + public override void Review() + { + if (string.IsNullOrEmpty(Input)) return; + + var words = Input.Split(' ', StringSplitOptions.RemoveEmptyEntries); + string[] tempLines = new string[words.Length]; + + int lineCount = 0, start = 0, len = 0; + + for (int i = 0; i < words.Length; i++) + { + if (len == 0) + len = words[i].Length; + else if (len + 1 +words[i].Length <= 50) + len += 1 + words[i].Length; + else + { + tempLines[lineCount++] = JustifyLine(words, start, i-1, 50); + start = i; + len = words[i].Length; + } + } + + if (len > 0) + tempLines[lineCount++] = JustifyLine(words, start, words.Length - 1, 50); + + Output = tempLines.Take(lineCount).ToArray(); + } + + private static string JustifyLine(string[] words, int start, int end, int targetWidth) + { + if (start == end) return words[start]; + + var lineWords = words.Skip(start).Take(end-start+1).ToArray(); + int lettersCount = lineWords.Sum(w => w.Length); + + int totalSpacesNeeded = targetWidth - lettersCount; + int gapsCount = lineWords.Length-1; + int spacesPerGap = totalSpacesNeeded / gapsCount; + int extraSpaces = totalSpacesNeeded % gapsCount; + + string result = string.Empty; + for (int i = 0; i < gapsCount; i++) + result += lineWords[i] + new string(' ', spacesPerGap + (i < extraSpaces ? 1 : 0)); + result += lineWords.Last(); + + return result; + } + + public override string ToString() => Output != null ? string.Join(Environment.NewLine, Output) : string.Empty; +} \ No newline at end of file diff --git a/Lab9/Purple/Task3.cs b/Lab9/Purple/Task3.cs new file mode 100644 index 0000000..b9bb9f5 --- /dev/null +++ b/Lab9/Purple/Task3.cs @@ -0,0 +1,44 @@ +namespace Lab9.Purple; + +public class Task3 : Purple +{ + public string Output { get; private set;} + public (string, char)[] Codes { get; private set;} + + public Task3(string text) : base(text) + { + Output = string.Empty; + Codes = Array.Empty<(string, char)>(); + } + + public override void Review() + { + if (string.IsNullOrEmpty(Input)) return; + + var topPairs = Enumerable.Range(0, Input.Length-1) + .Where(i => char.IsLetter(Input[i]) && char.IsLetter(Input[i+1])) + .GroupBy(i => Input.Substring(i, 2)) + .OrderByDescending(g => g.Count()) + .ThenBy(g => g.Min()) + .Select(g => g.Key) + .Take(5) + .ToArray(); + + var usedChars = Input.Distinct().ToArray(); + var avialableChars = Enumerable.Range(32, 95) + .Select(i => (char)i) + .Except(usedChars) + .Take(topPairs.Length) + .ToArray(); + + Codes = topPairs.Zip(avialableChars, (pair, c) => (pair, c)).ToArray(); + + string result = Input; + foreach (var code in Codes) + result = result.Replace(code.Item1, code.Item2.ToString()); + + Output = result; + } + + public override string ToString() => Output ?? string.Empty; +} \ No newline at end of file diff --git a/Lab9/Purple/Task4.cs b/Lab9/Purple/Task4.cs new file mode 100644 index 0000000..e9bb13f --- /dev/null +++ b/Lab9/Purple/Task4.cs @@ -0,0 +1,31 @@ +namespace Lab9.Purple; + +public class Task4 : Purple +{ + public string Output { get; private set; } + public (string, char)[] Codes { get; private set;} + + public Task4(string text, (string, char)[] codes) : base(text) + { + Codes = codes; + Output = string.Empty; + } + + public override void Review() + { + if (string.IsNullOrEmpty(Input)) + { + Output = Input ?? string.Empty; + return; + } + + string result = Input; + + foreach (var code in Codes) + result = result.Replace(code.Item2.ToString(), code.Item1); + + Output = result; + } + + public override string ToString() => Output ?? string.Empty; +} \ No newline at end of file