From 3c7d162f36e47007ffda46f7c9a97c41f616ebf9 Mon Sep 17 00:00:00 2001 From: safaryan elen Date: Thu, 2 Apr 2026 21:50:27 +0300 Subject: [PATCH 1/5] Update Task1.cs --- Lab8Test/Green/Task1.cs | 430 ++++++++++++++++++++-------------------- 1 file changed, 215 insertions(+), 215 deletions(-) diff --git a/Lab8Test/Green/Task1.cs b/Lab8Test/Green/Task1.cs index cd884d9f..af099407 100644 --- a/Lab8Test/Green/Task1.cs +++ b/Lab8Test/Green/Task1.cs @@ -1,215 +1,215 @@ -//using System.Reflection; -//using System.Text.Json; - -//namespace Lab8Test.Green -//{ -// [TestClass] -// public sealed class Task1 -// { -// record InputRow(string Surname, string Group, string Trainer, double Result); -// record OutputRow(string Surname, string Group, string Trainer, double Result, bool HasPassed); - -// private InputRow[] _input; -// private OutputRow[] _output; -// private Lab8.Green.Task1.Participant[] _participants; - -// [TestInitialize] -// public void LoadData() -// { -// var folder = Directory.GetParent(Directory.GetCurrentDirectory()) -// .Parent.Parent.Parent.FullName; -// folder = Path.Combine(folder, "Lab8Test", "Green"); - -// var input = JsonSerializer.Deserialize( -// File.ReadAllText(Path.Combine(folder, "input.json")))!; -// var output = JsonSerializer.Deserialize( -// File.ReadAllText(Path.Combine(folder, "output.json")))!; - -// _input = input.GetProperty("Task1").Deserialize()!; -// _output = output.GetProperty("Task1").Deserialize()!; - -// _participants = new Lab8.Green.Task1.Participant[_input.Length]; -// } - -// [TestMethod] -// public void Test_00_OOP() -// { -// var baseType = typeof(Lab8.Green.Task1.Participant); -// var t100 = typeof(Lab8.Green.Task1.Participant100M); -// var t500 = typeof(Lab8.Green.Task1.Participant500M); - -// Assert.IsTrue(baseType.IsClass); -// Assert.IsTrue(baseType.IsAbstract); - -// Assert.IsTrue(t100.IsSubclassOf(baseType)); -// Assert.IsTrue(t500.IsSubclassOf(baseType)); - -// Assert.AreEqual(0, baseType.GetFields(BindingFlags.Public | BindingFlags.Instance).Length); -// Assert.AreEqual(0, t100.GetFields(BindingFlags.Public | BindingFlags.Instance).Length); -// Assert.AreEqual(0, t500.GetFields(BindingFlags.Public | BindingFlags.Instance).Length); - -// Assert.IsTrue(baseType.GetProperty("Surname")?.CanRead ?? false); -// Assert.IsTrue(baseType.GetProperty("Group")?.CanRead ?? false); -// Assert.IsTrue(baseType.GetProperty("Trainer")?.CanRead ?? false); -// Assert.IsTrue(baseType.GetProperty("Result")?.CanRead ?? false); -// Assert.IsTrue(baseType.GetProperty("HasPassed")?.CanRead ?? false); - -// Assert.IsFalse(baseType.GetProperty("Surname")?.CanWrite ?? true); -// Assert.IsFalse(baseType.GetProperty("Group")?.CanWrite ?? true); -// Assert.IsFalse(baseType.GetProperty("Trainer")?.CanWrite ?? true); -// Assert.IsFalse(baseType.GetProperty("Result")?.CanWrite ?? true); -// Assert.IsFalse(baseType.GetProperty("HasPassed")?.CanWrite ?? true); - -// Assert.IsNotNull(baseType.GetConstructor( -// BindingFlags.Instance | BindingFlags.Public, -// null, -// new[] { typeof(string), typeof(string), typeof(string) }, -// null)); - -// Assert.IsNotNull(t100.GetConstructor( -// BindingFlags.Instance | BindingFlags.Public, -// null, -// new[] { typeof(string), typeof(string), typeof(string) }, -// null)); - -// Assert.IsNotNull(t500.GetConstructor( -// BindingFlags.Instance | BindingFlags.Public, -// null, -// new[] { typeof(string), typeof(string), typeof(string) }, -// null)); - -// Assert.IsNotNull(baseType.GetMethod("Run", BindingFlags.Instance | BindingFlags.Public)); -// Assert.IsNotNull(baseType.GetMethod("GetTrainerParticipants", BindingFlags.Static | BindingFlags.Public)); - -// Assert.AreEqual(0, baseType.GetFields().Count(f => f.IsPublic)); -// Assert.AreEqual(baseType.GetProperties().Count(f => f.PropertyType.IsPublic), 6); -// Assert.AreEqual(baseType.GetConstructors().Count(f => f.IsPublic), 1); -// Assert.AreEqual(baseType.GetMethods(BindingFlags.Instance | BindingFlags.Public).Length, 11); - -// Assert.IsNotNull(t100.GetMethod("Run", BindingFlags.Instance | BindingFlags.Public)); -// Assert.IsNull(t100.GetMethod("GetTrainerParticipants", BindingFlags.Static | BindingFlags.Public)); - -// Assert.AreEqual(0, t100.GetFields().Count(f => f.IsPublic)); -// Assert.AreEqual(t100.GetProperties().Count(f => f.PropertyType.IsPublic), 5); -// Assert.AreEqual(t100.GetConstructors().Count(f => f.IsPublic), 1); -// Assert.AreEqual(t100.GetMethods(BindingFlags.Instance | BindingFlags.Public).Length, 11); - -// Assert.IsNotNull(t500.GetMethod("Run", BindingFlags.Instance | BindingFlags.Public)); -// Assert.IsNull(t500.GetMethod("GetTrainerParticipants", BindingFlags.Static | BindingFlags.Public)); - -// Assert.AreEqual(0, t500.GetFields().Count(f => f.IsPublic)); -// Assert.AreEqual(t500.GetProperties().Count(f => f.PropertyType.IsPublic), 5); -// Assert.AreEqual(t500.GetConstructors().Count(f => f.IsPublic), 1); -// Assert.AreEqual(t500.GetMethods(BindingFlags.Instance | BindingFlags.Public).Length, 11); -// } -// [TestMethod] -// public void Test_01_Create() -// { -// Init(); -// } - -// [TestMethod] -// public void Test_02_Run() -// { -// Init(); -// Run(); -// Check(runExpected: true); -// } - -// [TestMethod] -// public void Test_03_PassedCount() -// { -// Init(); -// Run(); - -// int expected = _output.Count(o => o.HasPassed); -// Assert.AreEqual(expected, Lab8.Green.Task1.Participant.PassedTheStandard); -// } - -// [TestMethod] -// public void Test_04_GetTrainerParticipants() -// { -// Init(); -// Run(); - -// var result = Lab8.Green.Task1.Participant.GetTrainerParticipants( -// _participants, -// typeof(Lab8.Green.Task1.Participant500M), -// "Свиридов"); - -// Assert.IsTrue(result.All(p => -// p.Trainer == "Свиридов" && -// p is Lab8.Green.Task1.Participant500M)); -// } -// private void ResetAllParticipantStatics() -// { -// var baseType = typeof(Lab8.Green.Task1.Participant); - -// var allTypes = baseType.Assembly -// .GetTypes() -// .Where(t => baseType.IsAssignableFrom(t)); - -// foreach (var type in allTypes) -// { -// var staticFields = type.GetFields( -// BindingFlags.Static | -// BindingFlags.Public | -// BindingFlags.NonPublic); - -// foreach (var field in staticFields) -// { -// if (field.FieldType == typeof(int)) -// field.SetValue(null, 0); -// else if (field.FieldType == typeof(double)) -// field.SetValue(null, 0.0); -// else if (field.FieldType == typeof(bool)) -// field.SetValue(null, false); -// else -// field.SetValue(null, null); -// } -// } -// } -// private void Init() -// { -// ResetAllParticipantStatics(); -// for (int i = 0; i < _input.Length; i++) -// { -// _participants[i] = -// i % 2 == 0 -// ? new Lab8.Green.Task1.Participant100M( -// _input[i].Surname, -// _input[i].Group, -// _input[i].Trainer) -// : new Lab8.Green.Task1.Participant500M( -// _input[i].Surname, -// _input[i].Group, -// _input[i].Trainer); -// } -// } - -// private void Run() -// { -// for (int i = 0; i < _input.Length; i++) -// { -// _participants[i].Run(_input[i].Result); -// _participants[i].Run(-1); -// } -// } - -// private void Check(bool runExpected) -// { -// for (int i = 0; i < _participants.Length; i++) -// { -// Assert.AreEqual(_output[i].Surname, _participants[i].Surname); -// Assert.AreEqual(_output[i].Group, _participants[i].Group); -// Assert.AreEqual(_output[i].Trainer, _participants[i].Trainer); - -// if (runExpected) -// { -// Assert.AreEqual(_output[i].Result, _participants[i].Result, 0.0001); -// Assert.AreEqual(_output[i].HasPassed, _participants[i].HasPassed); -// } -// } -// } -// } -//} +using System.Reflection; +using System.Text.Json; + +namespace Lab8Test.Green +{ + [TestClass] + public sealed class Task1 + { + record InputRow(string Surname, string Group, string Trainer, double Result); + record OutputRow(string Surname, string Group, string Trainer, double Result, bool HasPassed); + + private InputRow[] _input; + private OutputRow[] _output; + private Lab8.Green.Task1.Participant[] _participants; + + [TestInitialize] + public void LoadData() + { + var folder = Directory.GetParent(Directory.GetCurrentDirectory()) + .Parent.Parent.Parent.FullName; + folder = Path.Combine(folder, "Lab8Test", "Green"); + + var input = JsonSerializer.Deserialize( + File.ReadAllText(Path.Combine(folder, "input.json")))!; + var output = JsonSerializer.Deserialize( + File.ReadAllText(Path.Combine(folder, "output.json")))!; + + _input = input.GetProperty("Task1").Deserialize()!; + _output = output.GetProperty("Task1").Deserialize()!; + + _participants = new Lab8.Green.Task1.Participant[_input.Length]; + } + + [TestMethod] + public void Test_00_OOP() + { + var baseType = typeof(Lab8.Green.Task1.Participant); + var t100 = typeof(Lab8.Green.Task1.Participant100M); + var t500 = typeof(Lab8.Green.Task1.Participant500M); + + Assert.IsTrue(baseType.IsClass); + Assert.IsTrue(baseType.IsAbstract); + + Assert.IsTrue(t100.IsSubclassOf(baseType)); + Assert.IsTrue(t500.IsSubclassOf(baseType)); + + Assert.AreEqual(0, baseType.GetFields(BindingFlags.Public | BindingFlags.Instance).Length); + Assert.AreEqual(0, t100.GetFields(BindingFlags.Public | BindingFlags.Instance).Length); + Assert.AreEqual(0, t500.GetFields(BindingFlags.Public | BindingFlags.Instance).Length); + + Assert.IsTrue(baseType.GetProperty("Surname")?.CanRead ?? false); + Assert.IsTrue(baseType.GetProperty("Group")?.CanRead ?? false); + Assert.IsTrue(baseType.GetProperty("Trainer")?.CanRead ?? false); + Assert.IsTrue(baseType.GetProperty("Result")?.CanRead ?? false); + Assert.IsTrue(baseType.GetProperty("HasPassed")?.CanRead ?? false); + + Assert.IsFalse(baseType.GetProperty("Surname")?.CanWrite ?? true); + Assert.IsFalse(baseType.GetProperty("Group")?.CanWrite ?? true); + Assert.IsFalse(baseType.GetProperty("Trainer")?.CanWrite ?? true); + Assert.IsFalse(baseType.GetProperty("Result")?.CanWrite ?? true); + Assert.IsFalse(baseType.GetProperty("HasPassed")?.CanWrite ?? true); + + Assert.IsNotNull(baseType.GetConstructor( + BindingFlags.Instance | BindingFlags.Public, + null, + new[] { typeof(string), typeof(string), typeof(string) }, + null)); + + Assert.IsNotNull(t100.GetConstructor( + BindingFlags.Instance | BindingFlags.Public, + null, + new[] { typeof(string), typeof(string), typeof(string) }, + null)); + + Assert.IsNotNull(t500.GetConstructor( + BindingFlags.Instance | BindingFlags.Public, + null, + new[] { typeof(string), typeof(string), typeof(string) }, + null)); + + Assert.IsNotNull(baseType.GetMethod("Run", BindingFlags.Instance | BindingFlags.Public)); + Assert.IsNotNull(baseType.GetMethod("GetTrainerParticipants", BindingFlags.Static | BindingFlags.Public)); + + Assert.AreEqual(0, baseType.GetFields().Count(f => f.IsPublic)); + Assert.AreEqual(baseType.GetProperties().Count(f => f.PropertyType.IsPublic), 6); + Assert.AreEqual(baseType.GetConstructors().Count(f => f.IsPublic), 1); + Assert.AreEqual(baseType.GetMethods(BindingFlags.Instance | BindingFlags.Public).Length, 11); + + Assert.IsNotNull(t100.GetMethod("Run", BindingFlags.Instance | BindingFlags.Public)); + Assert.IsNull(t100.GetMethod("GetTrainerParticipants", BindingFlags.Static | BindingFlags.Public)); + + Assert.AreEqual(0, t100.GetFields().Count(f => f.IsPublic)); + Assert.AreEqual(t100.GetProperties().Count(f => f.PropertyType.IsPublic), 5); + Assert.AreEqual(t100.GetConstructors().Count(f => f.IsPublic), 1); + Assert.AreEqual(t100.GetMethods(BindingFlags.Instance | BindingFlags.Public).Length, 11); + + Assert.IsNotNull(t500.GetMethod("Run", BindingFlags.Instance | BindingFlags.Public)); + Assert.IsNull(t500.GetMethod("GetTrainerParticipants", BindingFlags.Static | BindingFlags.Public)); + + Assert.AreEqual(0, t500.GetFields().Count(f => f.IsPublic)); + Assert.AreEqual(t500.GetProperties().Count(f => f.PropertyType.IsPublic), 5); + Assert.AreEqual(t500.GetConstructors().Count(f => f.IsPublic), 1); + Assert.AreEqual(t500.GetMethods(BindingFlags.Instance | BindingFlags.Public).Length, 11); + } + [TestMethod] + public void Test_01_Create() + { + Init(); + } + + [TestMethod] + public void Test_02_Run() + { + Init(); + Run(); + Check(runExpected: true); + } + + [TestMethod] + public void Test_03_PassedCount() + { + Init(); + Run(); + + int expected = _output.Count(o => o.HasPassed); + Assert.AreEqual(expected, Lab8.Green.Task1.Participant.PassedTheStandard); + } + + [TestMethod] + public void Test_04_GetTrainerParticipants() + { + Init(); + Run(); + + var result = Lab8.Green.Task1.Participant.GetTrainerParticipants( + _participants, + typeof(Lab8.Green.Task1.Participant500M), + "Свиридов"); + + Assert.IsTrue(result.All(p => + p.Trainer == "Свиридов" && + p is Lab8.Green.Task1.Participant500M)); + } + private void ResetAllParticipantStatics() + { + var baseType = typeof(Lab8.Green.Task1.Participant); + + var allTypes = baseType.Assembly + .GetTypes() + .Where(t => baseType.IsAssignableFrom(t)); + + foreach (var type in allTypes) + { + var staticFields = type.GetFields( + BindingFlags.Static | + BindingFlags.Public | + BindingFlags.NonPublic); + + foreach (var field in staticFields) + { + if (field.FieldType == typeof(int)) + field.SetValue(null, 0); + else if (field.FieldType == typeof(double)) + field.SetValue(null, 0.0); + else if (field.FieldType == typeof(bool)) + field.SetValue(null, false); + else + field.SetValue(null, null); + } + } + } + private void Init() + { + ResetAllParticipantStatics(); + for (int i = 0; i < _input.Length; i++) + { + _participants[i] = + i % 2 == 0 + ? new Lab8.Green.Task1.Participant100M( + _input[i].Surname, + _input[i].Group, + _input[i].Trainer) + : new Lab8.Green.Task1.Participant500M( + _input[i].Surname, + _input[i].Group, + _input[i].Trainer); + } + } + + private void Run() + { + for (int i = 0; i < _input.Length; i++) + { + _participants[i].Run(_input[i].Result); + _participants[i].Run(-1); + } + } + + private void Check(bool runExpected) + { + for (int i = 0; i < _participants.Length; i++) + { + Assert.AreEqual(_output[i].Surname, _participants[i].Surname); + Assert.AreEqual(_output[i].Group, _participants[i].Group); + Assert.AreEqual(_output[i].Trainer, _participants[i].Trainer); + + if (runExpected) + { + Assert.AreEqual(_output[i].Result, _participants[i].Result, 0.0001); + Assert.AreEqual(_output[i].HasPassed, _participants[i].HasPassed); + } + } + } + } +} From 02a993962253ab1cd4c39396bbf4e00271300332 Mon Sep 17 00:00:00 2001 From: safaryan elen Date: Thu, 2 Apr 2026 21:50:49 +0300 Subject: [PATCH 2/5] Update Task2.cs --- Lab8Test/Green/Task2.cs | 448 ++++++++++++++++++++-------------------- 1 file changed, 224 insertions(+), 224 deletions(-) diff --git a/Lab8Test/Green/Task2.cs b/Lab8Test/Green/Task2.cs index 3a648eff..eb8e62b9 100644 --- a/Lab8Test/Green/Task2.cs +++ b/Lab8Test/Green/Task2.cs @@ -1,224 +1,224 @@ -//using System.Reflection; -//using System.Security.AccessControl; -//using System.Text.Json; - -//namespace Lab8Test.Green -//{ -// [TestClass] -// public sealed class Task2 -// { -// record InputRow(string Name, string Surname, int[] Marks); -// record OutputRow(string Name, string Surname, double AverageMark, bool IsExcellent); - -// private InputRow[] _input; -// private OutputRow[] _output; -// private Lab8.Green.Task2.Student[] _students; - -// [TestInitialize] -// public void LoadData() -// { -// var folder = Directory.GetParent(Directory.GetCurrentDirectory()) -// .Parent.Parent.Parent.FullName; -// folder = Path.Combine(folder, "Lab8Test", "Green"); - -// var input = JsonSerializer.Deserialize( -// File.ReadAllText(Path.Combine(folder, "input.json")))!; -// var output = JsonSerializer.Deserialize( -// File.ReadAllText(Path.Combine(folder, "output.json")))!; - -// _input = input.GetProperty("Task2").Deserialize()!; -// _output = output.GetProperty("Task2").Deserialize()!; - -// _students = new Lab8.Green.Task2.Student[_input.Length]; -// } - -// [TestMethod] -// public void Test_00_OOP() -// { -// var human = typeof(Lab8.Green.Task2.Human); -// var student = typeof(Lab8.Green.Task2.Student); - -// Assert.IsTrue(human.IsClass); -// Assert.IsTrue(student.IsClass); -// Assert.IsTrue(student.IsSubclassOf(human)); - -// Assert.AreEqual(0, human.GetFields(BindingFlags.Public | BindingFlags.Instance).Length); -// Assert.AreEqual(0, student.GetFields(BindingFlags.Public | BindingFlags.Instance).Length); - -// Assert.IsTrue(human.GetProperty("Name")?.CanRead ?? false); -// Assert.IsTrue(human.GetProperty("Surname")?.CanRead ?? false); - -// Assert.IsFalse(human.GetProperty("Name")?.CanWrite ?? true); -// Assert.IsFalse(human.GetProperty("Surname")?.CanWrite ?? true); - -// Assert.IsTrue(student.GetProperty("AverageMark")?.CanRead ?? false); -// Assert.IsTrue(student.GetProperty("IsExcellent")?.CanRead ?? false); -// Assert.IsTrue(student.GetProperty("Marks")?.CanRead ?? false); - -// Assert.IsFalse(student.GetProperty("AverageMark")?.CanWrite ?? true); -// Assert.IsFalse(student.GetProperty("IsExcellent")?.CanWrite ?? true); -// Assert.IsFalse(student.GetProperty("Marks")?.CanWrite ?? true); - -// Assert.IsNotNull(human.GetConstructor( -// BindingFlags.Instance | BindingFlags.Public, -// null, -// new[] { typeof(string), typeof(string) }, -// null)); - - -// Assert.AreEqual(0, human.GetFields().Count(f => f.IsPublic)); -// Assert.AreEqual(2, human.GetProperties().Count(f => f.PropertyType.IsPublic)); -// Assert.AreEqual(1, human.GetConstructors().Count(f => f.IsPublic)); -// Assert.AreEqual(human.GetMethods(BindingFlags.Instance | BindingFlags.Public).Length, 7); - -// Assert.IsNotNull(student.GetConstructor( -// BindingFlags.Instance | BindingFlags.Public, -// null, -// new[] { typeof(string), typeof(string) }, -// null)); - -// Assert.IsNotNull(student.GetMethod("Exam", BindingFlags.Instance | BindingFlags.Public)); -// Assert.IsNotNull(student.GetMethod("SortByAverageMark", BindingFlags.Static | BindingFlags.Public)); -// Assert.IsNotNull(student.GetMethod("Print", BindingFlags.Instance | BindingFlags.Public)); - -// Assert.AreEqual(0, student.GetFields().Count(f => f.IsPublic)); -// Assert.AreEqual(6, student.GetProperties().Count(f => f.PropertyType.IsPublic)); -// Assert.AreEqual(1, student.GetConstructors().Count(f => f.IsPublic)); -// Assert.AreEqual(student.GetMethods(BindingFlags.Instance | BindingFlags.Public).Length, 12); -// } - -// [TestMethod] -// public void Test_01_Create() -// { -// Init(); -// } - -// [TestMethod] -// public void Test_02_Exam() -// { -// Init(); -// Exam(); -// CheckBase(examExpected: true); -// } - -// [TestMethod] -// public void Test_03_Sort() -// { -// Init(); -// Exam(); -// Lab8.Green.Task2.Student.SortByAverageMark(_students); -// CheckSorted(); -// } - -// [TestMethod] -// public void Test_04_ArrayIsolation() -// { -// Init(); -// Exam(); - -// for (int i = 0; i < _students.Length; i++) -// { -// var marks = _students[i].Marks; -// for (int j = 0; j < marks.Length; j++) -// marks[j] = -100; -// } - -// CheckBase(examExpected: true); -// } - -// [TestMethod] -// public void Test_05_ExcellentCount() -// { -// Init(); -// Exam(); - -// int expected = _output.Count(o => o.IsExcellent); -// Assert.AreEqual(expected, Lab8.Green.Task2.Student.ExcellentAmount); -// } - -// private void ResetAllParticipantStatics() -// { -// var baseType = typeof(Lab8.Green.Task2.Human); - -// var allTypes = baseType.Assembly -// .GetTypes() -// .Where(t => baseType.IsAssignableFrom(t)); - -// foreach (var type in allTypes) -// { -// var staticFields = type.GetFields( -// BindingFlags.Static | -// BindingFlags.Public | -// BindingFlags.NonPublic); - -// foreach (var field in staticFields) -// { -// if (field.FieldType == typeof(int)) -// field.SetValue(null, 0); -// else if (field.FieldType == typeof(double)) -// field.SetValue(null, 0.0); -// else if (field.FieldType == typeof(bool)) -// field.SetValue(null, false); -// else -// field.SetValue(null, null); -// } -// } -// } -// private void Init() -// { -// ResetAllParticipantStatics(); -// for (int i = 0; i < _input.Length; i++) -// { -// _students[i] = new Lab8.Green.Task2.Student( -// _input[i].Name, -// _input[i].Surname); -// } -// } - -// private void Exam() -// { -// for (int i = 0; i < _input.Length; i++) -// { -// foreach (var mark in _input[i].Marks) -// _students[i].Exam(mark); -// } -// } - -// private void CheckBase(bool examExpected) -// { -// for (int i = 0; i < _students.Length; i++) -// { -// Assert.AreEqual(_input[i].Name, _students[i].Name); -// Assert.AreEqual(_input[i].Surname, _students[i].Surname); - -// if (examExpected) -// { -// Assert.AreEqual( -// _input[i].Marks.Average(), -// _students[i].AverageMark, -// 0.0001); - -// Assert.AreEqual( -// _input[i].Marks.All(m => m >= 4), -// _students[i].IsExcellent); -// } -// else -// { -// Assert.AreEqual(0, _students[i].AverageMark, 0.0001); -// Assert.IsFalse(_students[i].IsExcellent); -// } -// } -// } - -// private void CheckSorted() -// { -// for (int i = 0; i < _students.Length; i++) -// { -// Assert.AreEqual(_output[i].Name, _students[i].Name); -// Assert.AreEqual(_output[i].Surname, _students[i].Surname); -// Assert.AreEqual(_output[i].AverageMark, _students[i].AverageMark, 0.0001); -// Assert.AreEqual(_output[i].IsExcellent, _students[i].IsExcellent); -// } -// } -// } -//} \ No newline at end of file +using System.Reflection; +using System.Security.AccessControl; +using System.Text.Json; + +namespace Lab8Test.Green +{ + [TestClass] + public sealed class Task2 + { + record InputRow(string Name, string Surname, int[] Marks); + record OutputRow(string Name, string Surname, double AverageMark, bool IsExcellent); + + private InputRow[] _input; + private OutputRow[] _output; + private Lab8.Green.Task2.Student[] _students; + + [TestInitialize] + public void LoadData() + { + var folder = Directory.GetParent(Directory.GetCurrentDirectory()) + .Parent.Parent.Parent.FullName; + folder = Path.Combine(folder, "Lab8Test", "Green"); + + var input = JsonSerializer.Deserialize( + File.ReadAllText(Path.Combine(folder, "input.json")))!; + var output = JsonSerializer.Deserialize( + File.ReadAllText(Path.Combine(folder, "output.json")))!; + + _input = input.GetProperty("Task2").Deserialize()!; + _output = output.GetProperty("Task2").Deserialize()!; + + _students = new Lab8.Green.Task2.Student[_input.Length]; + } + + [TestMethod] + public void Test_00_OOP() + { + var human = typeof(Lab8.Green.Task2.Human); + var student = typeof(Lab8.Green.Task2.Student); + + Assert.IsTrue(human.IsClass); + Assert.IsTrue(student.IsClass); + Assert.IsTrue(student.IsSubclassOf(human)); + + Assert.AreEqual(0, human.GetFields(BindingFlags.Public | BindingFlags.Instance).Length); + Assert.AreEqual(0, student.GetFields(BindingFlags.Public | BindingFlags.Instance).Length); + + Assert.IsTrue(human.GetProperty("Name")?.CanRead ?? false); + Assert.IsTrue(human.GetProperty("Surname")?.CanRead ?? false); + + Assert.IsFalse(human.GetProperty("Name")?.CanWrite ?? true); + Assert.IsFalse(human.GetProperty("Surname")?.CanWrite ?? true); + + Assert.IsTrue(student.GetProperty("AverageMark")?.CanRead ?? false); + Assert.IsTrue(student.GetProperty("IsExcellent")?.CanRead ?? false); + Assert.IsTrue(student.GetProperty("Marks")?.CanRead ?? false); + + Assert.IsFalse(student.GetProperty("AverageMark")?.CanWrite ?? true); + Assert.IsFalse(student.GetProperty("IsExcellent")?.CanWrite ?? true); + Assert.IsFalse(student.GetProperty("Marks")?.CanWrite ?? true); + + Assert.IsNotNull(human.GetConstructor( + BindingFlags.Instance | BindingFlags.Public, + null, + new[] { typeof(string), typeof(string) }, + null)); + + + Assert.AreEqual(0, human.GetFields().Count(f => f.IsPublic)); + Assert.AreEqual(2, human.GetProperties().Count(f => f.PropertyType.IsPublic)); + Assert.AreEqual(1, human.GetConstructors().Count(f => f.IsPublic)); + Assert.AreEqual(human.GetMethods(BindingFlags.Instance | BindingFlags.Public).Length, 7); + + Assert.IsNotNull(student.GetConstructor( + BindingFlags.Instance | BindingFlags.Public, + null, + new[] { typeof(string), typeof(string) }, + null)); + + Assert.IsNotNull(student.GetMethod("Exam", BindingFlags.Instance | BindingFlags.Public)); + Assert.IsNotNull(student.GetMethod("SortByAverageMark", BindingFlags.Static | BindingFlags.Public)); + Assert.IsNotNull(student.GetMethod("Print", BindingFlags.Instance | BindingFlags.Public)); + + Assert.AreEqual(0, student.GetFields().Count(f => f.IsPublic)); + Assert.AreEqual(6, student.GetProperties().Count(f => f.PropertyType.IsPublic)); + Assert.AreEqual(1, student.GetConstructors().Count(f => f.IsPublic)); + Assert.AreEqual(student.GetMethods(BindingFlags.Instance | BindingFlags.Public).Length, 12); + } + + [TestMethod] + public void Test_01_Create() + { + Init(); + } + + [TestMethod] + public void Test_02_Exam() + { + Init(); + Exam(); + CheckBase(examExpected: true); + } + + [TestMethod] + public void Test_03_Sort() + { + Init(); + Exam(); + Lab8.Green.Task2.Student.SortByAverageMark(_students); + CheckSorted(); + } + + [TestMethod] + public void Test_04_ArrayIsolation() + { + Init(); + Exam(); + + for (int i = 0; i < _students.Length; i++) + { + var marks = _students[i].Marks; + for (int j = 0; j < marks.Length; j++) + marks[j] = -100; + } + + CheckBase(examExpected: true); + } + + [TestMethod] + public void Test_05_ExcellentCount() + { + Init(); + Exam(); + + int expected = _output.Count(o => o.IsExcellent); + Assert.AreEqual(expected, Lab8.Green.Task2.Student.ExcellentAmount); + } + + private void ResetAllParticipantStatics() + { + var baseType = typeof(Lab8.Green.Task2.Human); + + var allTypes = baseType.Assembly + .GetTypes() + .Where(t => baseType.IsAssignableFrom(t)); + + foreach (var type in allTypes) + { + var staticFields = type.GetFields( + BindingFlags.Static | + BindingFlags.Public | + BindingFlags.NonPublic); + + foreach (var field in staticFields) + { + if (field.FieldType == typeof(int)) + field.SetValue(null, 0); + else if (field.FieldType == typeof(double)) + field.SetValue(null, 0.0); + else if (field.FieldType == typeof(bool)) + field.SetValue(null, false); + else + field.SetValue(null, null); + } + } + } + private void Init() + { + ResetAllParticipantStatics(); + for (int i = 0; i < _input.Length; i++) + { + _students[i] = new Lab8.Green.Task2.Student( + _input[i].Name, + _input[i].Surname); + } + } + + private void Exam() + { + for (int i = 0; i < _input.Length; i++) + { + foreach (var mark in _input[i].Marks) + _students[i].Exam(mark); + } + } + + private void CheckBase(bool examExpected) + { + for (int i = 0; i < _students.Length; i++) + { + Assert.AreEqual(_input[i].Name, _students[i].Name); + Assert.AreEqual(_input[i].Surname, _students[i].Surname); + + if (examExpected) + { + Assert.AreEqual( + _input[i].Marks.Average(), + _students[i].AverageMark, + 0.0001); + + Assert.AreEqual( + _input[i].Marks.All(m => m >= 4), + _students[i].IsExcellent); + } + else + { + Assert.AreEqual(0, _students[i].AverageMark, 0.0001); + Assert.IsFalse(_students[i].IsExcellent); + } + } + } + + private void CheckSorted() + { + for (int i = 0; i < _students.Length; i++) + { + Assert.AreEqual(_output[i].Name, _students[i].Name); + Assert.AreEqual(_output[i].Surname, _students[i].Surname); + Assert.AreEqual(_output[i].AverageMark, _students[i].AverageMark, 0.0001); + Assert.AreEqual(_output[i].IsExcellent, _students[i].IsExcellent); + } + } + } +} From d4acdd2990f49a59d4ee17233cb2b2f941d30593 Mon Sep 17 00:00:00 2001 From: safaryan elen Date: Thu, 2 Apr 2026 21:51:23 +0300 Subject: [PATCH 3/5] Update Task3.cs --- Lab8Test/Green/Task3.cs | 245 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 245 insertions(+) diff --git a/Lab8Test/Green/Task3.cs b/Lab8Test/Green/Task3.cs index cd03bdd4..75001656 100644 --- a/Lab8Test/Green/Task3.cs +++ b/Lab8Test/Green/Task3.cs @@ -1,3 +1,248 @@ +using System.Reflection; +using System.Security.AccessControl; +using System.Text.Json; + +namespace Lab8Test.Green +{ + [TestClass] + public sealed class Task3 + { + record InputRow(string Name, string Surname, int[] Marks); + record OutputRow(string Name, string Surname, double AverageMark, bool IsExpelled, int ID); + + private InputRow[] _input; + private OutputRow[] _outputOriginal; + private OutputRow[] _outputSorted; + private Lab8.Green.Task3.Student[] _students; + + [TestInitialize] + public void LoadData() + { + var folder = Directory.GetParent(Directory.GetCurrentDirectory()) + .Parent.Parent.Parent.FullName; + folder = Path.Combine(folder, "Lab8Test", "Green"); + + var input = JsonSerializer.Deserialize( + File.ReadAllText(Path.Combine(folder, "input.json")))!; + var output = JsonSerializer.Deserialize( + File.ReadAllText(Path.Combine(folder, "output.json")))!; + + _input = input.GetProperty("Task3").Deserialize(); + _outputOriginal = output.GetProperty("Task3").GetProperty("Original").Deserialize(); + _outputSorted = output.GetProperty("Task3").GetProperty("Sorted").Deserialize(); + + _students = new Lab8.Green.Task3.Student[_input.Length]; + } + [TestMethod] + public void Test_00_OOP() + { + var type = typeof(Lab8.Green.Task3.Student); + + Assert.AreEqual(type.GetFields(BindingFlags.Public | BindingFlags.Instance).Length, 0); + Assert.IsTrue(type.IsClass); + + Assert.IsTrue(type.GetProperty("Name")?.CanRead ?? false); + Assert.IsTrue(type.GetProperty("Surname")?.CanRead ?? false); + Assert.IsTrue(type.GetProperty("AverageMark")?.CanRead ?? false); + Assert.IsTrue(type.GetProperty("IsExpelled")?.CanRead ?? false); + Assert.IsTrue(type.GetProperty("Marks")?.CanRead ?? false); + Assert.IsTrue(type.GetProperty("ID")?.CanRead ?? false); + + Assert.IsFalse(type.GetProperty("Name")?.CanWrite ?? true); + Assert.IsFalse(type.GetProperty("Surname")?.CanWrite ?? true); + Assert.IsFalse(type.GetProperty("AverageMark")?.CanWrite ?? true); + Assert.IsFalse(type.GetProperty("IsExpelled")?.CanWrite ?? true); + Assert.IsFalse(type.GetProperty("Marks")?.CanWrite ?? true); + Assert.IsFalse(type.GetProperty("ID")?.CanWrite ?? true); + + Assert.IsNotNull(type.GetConstructor( + BindingFlags.Instance | BindingFlags.Public, + null, + new[] { typeof(string), typeof(string) }, + null)); + + Assert.IsNotNull(type.GetMethod("Exam", BindingFlags.Instance | BindingFlags.Public)); + Assert.IsNotNull(type.GetMethod("Restore", BindingFlags.Instance | BindingFlags.Public)); + Assert.IsNotNull(type.GetMethod("SortByAverageMark", BindingFlags.Static | BindingFlags.Public)); + Assert.IsNotNull(type.GetMethod("Print", BindingFlags.Instance | BindingFlags.Public)); + + Assert.AreEqual(0, type.GetFields().Count(f => f.IsPublic)); + Assert.AreEqual(type.GetProperties().Count(f => f.PropertyType.IsPublic), 6); + Assert.AreEqual(type.GetConstructors().Count(f => f.IsPublic), 1); + Assert.AreEqual(type.GetMethods(BindingFlags.Instance | BindingFlags.Public).Length, 13); + + var commission = typeof(Lab8.Green.Task3.Commission); + Assert.IsTrue(commission.IsClass); + + Assert.IsNotNull(commission.GetMethod("Sort", BindingFlags.Static | BindingFlags.Public)); + Assert.IsNotNull(commission.GetMethod("Expel", BindingFlags.Static | BindingFlags.Public)); + Assert.IsNotNull(commission.GetMethod("Restore", BindingFlags.Static | BindingFlags.Public)); + + Assert.AreEqual(0, commission.GetFields().Count(f => f.IsPublic)); + Assert.AreEqual(commission.GetProperties().Count(f => f.PropertyType.IsPublic), 0); + Assert.AreEqual(commission.GetConstructors().Count(f => f.IsPublic), 1); + Assert.AreEqual(type.GetMethods(BindingFlags.Instance | BindingFlags.Public).Length, 13); + } + + [TestMethod] + public void Test_02_CreateStudents() + { + InitStudents(); + for (int i = 0; i < _students.Length; i++) + { + Assert.AreEqual(_input[i].Name, _students[i].Name); + Assert.AreEqual(_input[i].Surname, _students[i].Surname); + Assert.AreEqual(0, _students[i].AverageMark, 0.0001); + Assert.IsFalse(_students[i].IsExpelled); + Assert.AreEqual(i + 1, _students[i].ID); + } + } + + [TestMethod] + public void Test_03_ExamStudents() + { + InitStudents(); + ApplyExams(); + + for (int i = 0; i < _students.Length; i++) + { + var marks = _input[i].Marks.Where(m => m > 0).ToArray(); + double avg = marks.Length == 0 ? 0 : marks.Average(); + bool expelled = _input[i].Marks.Any(m => m == 2); + + Assert.AreEqual(avg, _students[i].AverageMark, 0.0001); + Assert.AreEqual(expelled, _students[i].IsExpelled); + } + } + + [TestMethod] + public void Test_04_SortByAverageMark() + { + InitStudents(); + ApplyExams(); + + Lab8.Green.Task3.Student.SortByAverageMark(_students); + + for (int i = 0; i < _students.Length; i++) + { + Assert.AreEqual(_outputSorted[i].Name, _students[i].Name); + Assert.AreEqual(_outputSorted[i].Surname, _students[i].Surname); + Assert.AreEqual(_outputSorted[i].AverageMark, _students[i].AverageMark, 0.0001); + Assert.AreEqual(_outputSorted[i].IsExpelled, _students[i].IsExpelled); + } + } + + [TestMethod] + public void Test_05_CommissionSortByID() + { + InitStudents(); + ApplyExams(); + int startId = _students[0].ID; + var shuffled = _students.ToArray(); + Array.Reverse(shuffled); + Lab8.Green.Task3.Commission.Sort(shuffled); + + for (int i = 0; i < shuffled.Length; i++) + { + Assert.AreEqual(i + startId, shuffled[i].ID); + } + } + + [TestMethod] + public void Test_06_CommissionExpel() + { + InitStudents(); + ApplyExams(); + + var expelled = Lab8.Green.Task3.Commission.Expel(ref _students); + + foreach (var s in expelled) + { + Assert.IsTrue(s.IsExpelled); + } + foreach (var s in _students) + { + Assert.IsFalse(s.IsExpelled); + } + } + + [TestMethod] + public void Test_07_CommissionRestore() + { + InitStudents(); + ApplyExams(); + + var expelled = Lab8.Green.Task3.Commission.Expel(ref _students); + if (expelled.Length > 0) + { + Lab8.Green.Task3.Commission.Restore(ref _students, expelled[0]); + Assert.IsTrue(_students.Any(s => s.ID == expelled[0].ID)); + } + } + + [TestMethod] + public void Test_08_RestoreMethod_Student() + { + InitStudents(); + ApplyExams(); + + var s = _students.FirstOrDefault(st => st.IsExpelled); + if (s != null) + { + s.Restore(); + Assert.IsFalse(s.IsExpelled); + } + } + + private void ResetAllParticipantStatics() + { + var baseType = typeof(Lab8.Green.Task3.Student); + + var allTypes = baseType.Assembly + .GetTypes() + .Where(t => baseType.IsAssignableFrom(t)); + + foreach (var type in allTypes) + { + var staticFields = type.GetFields( + BindingFlags.Static | + BindingFlags.Public | + BindingFlags.NonPublic); + + foreach (var field in staticFields) + { + if (field.FieldType == typeof(int)) + field.SetValue(null, 1); + else if (field.FieldType == typeof(double)) + field.SetValue(null, 0.0); + else if (field.FieldType == typeof(bool)) + field.SetValue(null, false); + else + field.SetValue(null, null); + } + } + } + private void InitStudents() + { + ResetAllParticipantStatics(); + for (int i = 0; i < _input.Length; i++) + { + _students[i] = new Lab8.Green.Task3.Student(_input[i].Name, _input[i].Surname); + } + } + + private void ApplyExams() + { + for (int i = 0; i < _input.Length; i++) + { + foreach (var mark in _input[i].Marks) + { + _students[i].Exam(mark); + } + } + } + } +} //using System.Reflection; //using System.Security.AccessControl; //using System.Text.Json; From 9a9be1308da286ebe55313b2a987f967140ffd3a Mon Sep 17 00:00:00 2001 From: safaryan elen Date: Thu, 2 Apr 2026 21:51:41 +0300 Subject: [PATCH 4/5] Update Task4.cs --- Lab8Test/Green/Task4.cs | 516 ++++++++++++++++++++-------------------- 1 file changed, 258 insertions(+), 258 deletions(-) diff --git a/Lab8Test/Green/Task4.cs b/Lab8Test/Green/Task4.cs index 2d0f8c6a..9c876114 100644 --- a/Lab8Test/Green/Task4.cs +++ b/Lab8Test/Green/Task4.cs @@ -1,258 +1,258 @@ -//using System.Reflection; -//using System.Text.Json; - -//namespace Lab8Test.Green -//{ -// [TestClass] -// public sealed class Task4 -// { -// record InputRow(string Name, string Surname, double[] Jumps); -// record OutputRow(string Name, string Surname, double BestJump); - -// private InputRow[] _input; -// private OutputRow[] _outputSorted; - -// private Lab8.Green.Task4.Participant[] _participants; -// private Lab8.Green.Task4.Discipline _longJump; -// private Lab8.Green.Task4.Discipline _highJump; - -// [TestInitialize] -// public void LoadData() -// { -// var folder = Directory.GetParent(Directory.GetCurrentDirectory()) -// .Parent.Parent.Parent.FullName; -// folder = Path.Combine(folder, "Lab8Test", "Green"); - -// var input = JsonSerializer.Deserialize( -// File.ReadAllText(Path.Combine(folder, "input.json")))!; -// var output = JsonSerializer.Deserialize( -// File.ReadAllText(Path.Combine(folder, "output.json")))!; - -// _input = input.GetProperty("Task4").Deserialize()!; -// _outputSorted = output.GetProperty("Task4").GetProperty("Sorted").Deserialize()!; - -// _participants = new Lab8.Green.Task4.Participant[_input.Length]; -// _longJump = new Lab8.Green.Task4.LongJump(); -// _highJump = new Lab8.Green.Task4.HighJump(); -// } -// [TestMethod] -// public void Test_00_OOP() -// { -// var participant = typeof(Lab8.Green.Task4.Participant); -// var discipline = typeof(Lab8.Green.Task4.Discipline); -// var longJump = typeof(Lab8.Green.Task4.LongJump); -// var highJump = typeof(Lab8.Green.Task4.HighJump); - -// Assert.IsTrue(participant.IsValueType); -// Assert.AreEqual(0, participant.GetFields(BindingFlags.Public | BindingFlags.Instance).Length); - -// Assert.IsTrue(participant.GetProperty("Name")?.CanRead ?? false); -// Assert.IsTrue(participant.GetProperty("Surname")?.CanRead ?? false); -// Assert.IsTrue(participant.GetProperty("Jumps")?.CanRead ?? false); -// Assert.IsTrue(participant.GetProperty("BestJump")?.CanRead ?? false); - -// Assert.IsFalse(participant.GetProperty("Name")?.CanWrite ?? true); -// Assert.IsFalse(participant.GetProperty("Surname")?.CanWrite ?? true); -// Assert.IsFalse(participant.GetProperty("Jumps")?.CanWrite ?? true); -// Assert.IsFalse(participant.GetProperty("BestJump")?.CanWrite ?? true); - -// Assert.IsNotNull(participant.GetConstructor( -// BindingFlags.Instance | BindingFlags.Public, -// null, -// new[] { typeof(string), typeof(string) }, -// null)); - -// Assert.IsNotNull(participant.GetMethod("Jump")); -// Assert.IsNotNull(participant.GetMethod("Sort", BindingFlags.Static | BindingFlags.Public)); -// Assert.IsNotNull(participant.GetMethod("Print")); - -// Assert.AreEqual(participant.GetConstructors().Count(c => c.IsPublic), 1); -// Assert.IsTrue(participant.GetMethods(BindingFlags.Instance | BindingFlags.Public).Length >= 2); - -// Assert.IsTrue(discipline.IsClass); -// Assert.IsTrue(discipline.IsAbstract); - -// Assert.AreEqual(0, discipline.GetFields(BindingFlags.Public | BindingFlags.Instance).Length); - -// Assert.IsTrue(discipline.GetProperty("Name")?.CanRead ?? false); -// Assert.IsTrue(discipline.GetProperty("Participants")?.CanRead ?? false); - -// Assert.IsNotNull(discipline.GetMethod("Add", new[] { typeof(Lab8.Green.Task4.Participant) })); -// Assert.IsNotNull(discipline.GetMethod("Add", new[] { typeof(Lab8.Green.Task4.Participant[]) })); -// Assert.IsNotNull(discipline.GetMethod("Sort")); -// Assert.IsNotNull(discipline.GetMethod("Retry")); -// Assert.IsNotNull(discipline.GetMethod("Print")); - -// Assert.AreEqual(0, discipline.GetConstructors().Count(c => c.IsPublic)); -// Assert.AreEqual(discipline.GetMethods(BindingFlags.Instance | BindingFlags.Public).Length, 11); -// Assert.AreEqual(discipline.GetProperties().Count(p => p.PropertyType.IsPublic), 2); - -// Assert.IsTrue(longJump.IsSubclassOf(discipline)); -// Assert.IsNotNull(longJump.GetConstructor(Type.EmptyTypes)); - -// Assert.AreEqual(0, longJump.GetFields(BindingFlags.Public | BindingFlags.Instance).Length); -// Assert.AreEqual(longJump.GetConstructors().Count(c => c.IsPublic), 1); -// Assert.AreEqual(longJump.GetMethods(BindingFlags.Instance | BindingFlags.Public).Length, 11); -// Assert.AreEqual(longJump.GetProperties().Count(p => p.PropertyType.IsPublic), 2); - -// Assert.IsTrue(highJump.IsSubclassOf(discipline)); -// Assert.IsNotNull(highJump.GetConstructor(Type.EmptyTypes)); - -// Assert.AreEqual(0, highJump.GetFields(BindingFlags.Public | BindingFlags.Instance).Length); -// Assert.AreEqual(highJump.GetConstructors().Count(c => c.IsPublic), 1); -// Assert.AreEqual(highJump.GetMethods(BindingFlags.Instance | BindingFlags.Public).Length, 11); -// Assert.AreEqual(highJump.GetProperties().Count(p => p.PropertyType.IsPublic), 2); -// } - - -// [TestMethod] -// public void Test_01_CreateParticipants() -// { -// InitParticipants(); - -// for (int i = 0; i < _participants.Length; i++) -// { -// Assert.AreEqual(_input[i].Name, _participants[i].Name); -// Assert.AreEqual(_input[i].Surname, _participants[i].Surname); -// Assert.AreEqual(0, _participants[i].BestJump, 0.0001); -// } -// } - -// [TestMethod] -// public void Test_02_Jumps() -// { -// InitParticipants(); -// ApplyJumps(); - -// for (int i = 0; i < _participants.Length; i++) -// { -// Assert.AreEqual(_input[i].Jumps.Max(), _participants[i].BestJump, 0.0001); -// } -// } - -// [TestMethod] -// public void Test_03_ArrayIsolation() -// { -// InitParticipants(); -// ApplyJumps(); - -// for (int i = 0; i < _participants.Length; i++) -// { -// var arr = _participants[i].Jumps; -// for (int j = 0; j < arr.Length; j++) -// arr[j] = -1; -// } - -// for (int i = 0; i < _participants.Length; i++) -// { -// Assert.AreEqual(_input[i].Jumps.Max(), _participants[i].BestJump, 0.0001); -// } -// } - -// [TestMethod] -// public void Test_04_DisciplineAddAndSort() -// { -// InitParticipants(); -// ApplyJumps(); - -// _longJump.Add(_participants); -// _longJump.Sort(); - -// var arr = _longJump.Participants; - -// for (int i = 0; i < arr.Length; i++) -// { -// Assert.AreEqual(_outputSorted[i].Name, arr[i].Name); -// Assert.AreEqual(_outputSorted[i].Surname, arr[i].Surname); -// Assert.AreEqual(_outputSorted[i].BestJump, arr[i].BestJump, 0.0001); -// } -// } - -// [TestMethod] -// public void Test_05_LongJumpRetry() -// { -// InitParticipants(); -// ApplyJumps(); - -// _longJump.Add(_participants); - -// int idx = 0; -// double best = _participants[idx].BestJump; -// var before = new double[3]; -// Array.Copy(_participants[idx].Jumps, before, 3); - -// _longJump.Retry(idx); -// _longJump.Participants[idx].Jump(before[0]); -// _longJump.Participants[idx].Jump(before[1]); - - -// Assert.AreEqual(best, _longJump.Participants[idx].Jumps[0], 0.0001); -// Assert.AreEqual(before[0], _longJump.Participants[idx].Jumps[1], 0.0001); -// Assert.AreEqual(before[1], _longJump.Participants[idx].Jumps[2], 0.0001); -// } - -// [TestMethod] -// public void Test_06_HighJumpRetry() -// { -// InitParticipants(); -// ApplyJumps(); - -// _highJump.Add(_participants); - -// int idx = 0; -// var before = _participants[idx].Jumps; - -// _highJump.Retry(idx); -// _highJump.Participants[idx].Jump(10); - -// Assert.AreEqual(before[0], _highJump.Participants[idx].Jumps[0], 0.0001); -// Assert.AreEqual(before[1], _highJump.Participants[idx].Jumps[1], 0.0001); -// Assert.AreEqual(10, _highJump.Participants[idx].Jumps[2], 0.0001); -// } -// private void ResetAllParticipantStatics() -// { -// var baseType = typeof(Lab8.Green.Task4.Discipline); - -// var allTypes = baseType.Assembly -// .GetTypes() -// .Where(t => baseType.IsAssignableFrom(t)); - -// foreach (var type in allTypes) -// { -// var staticFields = type.GetFields( -// BindingFlags.Static | -// BindingFlags.Public | -// BindingFlags.NonPublic); - -// foreach (var field in staticFields) -// { -// if (field.FieldType == typeof(int)) -// field.SetValue(null, 0); -// else if (field.FieldType == typeof(double)) -// field.SetValue(null, 0.0); -// else if (field.FieldType == typeof(bool)) -// field.SetValue(null, false); -// else -// field.SetValue(null, null); -// } -// } -// } -// private void InitParticipants() -// { -// ResetAllParticipantStatics(); -// for (int i = 0; i < _input.Length; i++) -// { -// _participants[i] = new Lab8.Green.Task4.Participant( -// _input[i].Name, -// _input[i].Surname); -// } -// } - -// private void ApplyJumps() -// { -// for (int i = 0; i < _input.Length; i++) -// foreach (var j in _input[i].Jumps) -// _participants[i].Jump(j); -// } -// } -//} +using System.Reflection; +using System.Text.Json; + +namespace Lab8Test.Green +{ + [TestClass] + public sealed class Task4 + { + record InputRow(string Name, string Surname, double[] Jumps); + record OutputRow(string Name, string Surname, double BestJump); + + private InputRow[] _input; + private OutputRow[] _outputSorted; + + private Lab8.Green.Task4.Participant[] _participants; + private Lab8.Green.Task4.Discipline _longJump; + private Lab8.Green.Task4.Discipline _highJump; + + [TestInitialize] + public void LoadData() + { + var folder = Directory.GetParent(Directory.GetCurrentDirectory()) + .Parent.Parent.Parent.FullName; + folder = Path.Combine(folder, "Lab8Test", "Green"); + + var input = JsonSerializer.Deserialize( + File.ReadAllText(Path.Combine(folder, "input.json")))!; + var output = JsonSerializer.Deserialize( + File.ReadAllText(Path.Combine(folder, "output.json")))!; + + _input = input.GetProperty("Task4").Deserialize()!; + _outputSorted = output.GetProperty("Task4").GetProperty("Sorted").Deserialize()!; + + _participants = new Lab8.Green.Task4.Participant[_input.Length]; + _longJump = new Lab8.Green.Task4.LongJump(); + _highJump = new Lab8.Green.Task4.HighJump(); + } + [TestMethod] + public void Test_00_OOP() + { + var participant = typeof(Lab8.Green.Task4.Participant); + var discipline = typeof(Lab8.Green.Task4.Discipline); + var longJump = typeof(Lab8.Green.Task4.LongJump); + var highJump = typeof(Lab8.Green.Task4.HighJump); + + Assert.IsTrue(participant.IsValueType); + Assert.AreEqual(0, participant.GetFields(BindingFlags.Public | BindingFlags.Instance).Length); + + Assert.IsTrue(participant.GetProperty("Name")?.CanRead ?? false); + Assert.IsTrue(participant.GetProperty("Surname")?.CanRead ?? false); + Assert.IsTrue(participant.GetProperty("Jumps")?.CanRead ?? false); + Assert.IsTrue(participant.GetProperty("BestJump")?.CanRead ?? false); + + Assert.IsFalse(participant.GetProperty("Name")?.CanWrite ?? true); + Assert.IsFalse(participant.GetProperty("Surname")?.CanWrite ?? true); + Assert.IsFalse(participant.GetProperty("Jumps")?.CanWrite ?? true); + Assert.IsFalse(participant.GetProperty("BestJump")?.CanWrite ?? true); + + Assert.IsNotNull(participant.GetConstructor( + BindingFlags.Instance | BindingFlags.Public, + null, + new[] { typeof(string), typeof(string) }, + null)); + + Assert.IsNotNull(participant.GetMethod("Jump")); + Assert.IsNotNull(participant.GetMethod("Sort", BindingFlags.Static | BindingFlags.Public)); + Assert.IsNotNull(participant.GetMethod("Print")); + + Assert.AreEqual(participant.GetConstructors().Count(c => c.IsPublic), 1); + Assert.IsTrue(participant.GetMethods(BindingFlags.Instance | BindingFlags.Public).Length >= 2); + + Assert.IsTrue(discipline.IsClass); + Assert.IsTrue(discipline.IsAbstract); + + Assert.AreEqual(0, discipline.GetFields(BindingFlags.Public | BindingFlags.Instance).Length); + + Assert.IsTrue(discipline.GetProperty("Name")?.CanRead ?? false); + Assert.IsTrue(discipline.GetProperty("Participants")?.CanRead ?? false); + + Assert.IsNotNull(discipline.GetMethod("Add", new[] { typeof(Lab8.Green.Task4.Participant) })); + Assert.IsNotNull(discipline.GetMethod("Add", new[] { typeof(Lab8.Green.Task4.Participant[]) })); + Assert.IsNotNull(discipline.GetMethod("Sort")); + Assert.IsNotNull(discipline.GetMethod("Retry")); + Assert.IsNotNull(discipline.GetMethod("Print")); + + Assert.AreEqual(0, discipline.GetConstructors().Count(c => c.IsPublic)); + Assert.AreEqual(discipline.GetMethods(BindingFlags.Instance | BindingFlags.Public).Length, 11); + Assert.AreEqual(discipline.GetProperties().Count(p => p.PropertyType.IsPublic), 2); + + Assert.IsTrue(longJump.IsSubclassOf(discipline)); + Assert.IsNotNull(longJump.GetConstructor(Type.EmptyTypes)); + + Assert.AreEqual(0, longJump.GetFields(BindingFlags.Public | BindingFlags.Instance).Length); + Assert.AreEqual(longJump.GetConstructors().Count(c => c.IsPublic), 1); + Assert.AreEqual(longJump.GetMethods(BindingFlags.Instance | BindingFlags.Public).Length, 11); + Assert.AreEqual(longJump.GetProperties().Count(p => p.PropertyType.IsPublic), 2); + + Assert.IsTrue(highJump.IsSubclassOf(discipline)); + Assert.IsNotNull(highJump.GetConstructor(Type.EmptyTypes)); + + Assert.AreEqual(0, highJump.GetFields(BindingFlags.Public | BindingFlags.Instance).Length); + Assert.AreEqual(highJump.GetConstructors().Count(c => c.IsPublic), 1); + Assert.AreEqual(highJump.GetMethods(BindingFlags.Instance | BindingFlags.Public).Length, 11); + Assert.AreEqual(highJump.GetProperties().Count(p => p.PropertyType.IsPublic), 2); + } + + + [TestMethod] + public void Test_01_CreateParticipants() + { + InitParticipants(); + + for (int i = 0; i < _participants.Length; i++) + { + Assert.AreEqual(_input[i].Name, _participants[i].Name); + Assert.AreEqual(_input[i].Surname, _participants[i].Surname); + Assert.AreEqual(0, _participants[i].BestJump, 0.0001); + } + } + + [TestMethod] + public void Test_02_Jumps() + { + InitParticipants(); + ApplyJumps(); + + for (int i = 0; i < _participants.Length; i++) + { + Assert.AreEqual(_input[i].Jumps.Max(), _participants[i].BestJump, 0.0001); + } + } + + [TestMethod] + public void Test_03_ArrayIsolation() + { + InitParticipants(); + ApplyJumps(); + + for (int i = 0; i < _participants.Length; i++) + { + var arr = _participants[i].Jumps; + for (int j = 0; j < arr.Length; j++) + arr[j] = -1; + } + + for (int i = 0; i < _participants.Length; i++) + { + Assert.AreEqual(_input[i].Jumps.Max(), _participants[i].BestJump, 0.0001); + } + } + + [TestMethod] + public void Test_04_DisciplineAddAndSort() + { + InitParticipants(); + ApplyJumps(); + + _longJump.Add(_participants); + _longJump.Sort(); + + var arr = _longJump.Participants; + + for (int i = 0; i < arr.Length; i++) + { + Assert.AreEqual(_outputSorted[i].Name, arr[i].Name); + Assert.AreEqual(_outputSorted[i].Surname, arr[i].Surname); + Assert.AreEqual(_outputSorted[i].BestJump, arr[i].BestJump, 0.0001); + } + } + + [TestMethod] + public void Test_05_LongJumpRetry() + { + InitParticipants(); + ApplyJumps(); + + _longJump.Add(_participants); + + int idx = 0; + double best = _participants[idx].BestJump; + var before = new double[3]; + Array.Copy(_participants[idx].Jumps, before, 3); + + _longJump.Retry(idx); + _longJump.Participants[idx].Jump(before[0]); + _longJump.Participants[idx].Jump(before[1]); + + + Assert.AreEqual(best, _longJump.Participants[idx].Jumps[0], 0.0001); + Assert.AreEqual(before[0], _longJump.Participants[idx].Jumps[1], 0.0001); + Assert.AreEqual(before[1], _longJump.Participants[idx].Jumps[2], 0.0001); + } + + [TestMethod] + public void Test_06_HighJumpRetry() + { + InitParticipants(); + ApplyJumps(); + + _highJump.Add(_participants); + + int idx = 0; + var before = _participants[idx].Jumps; + + _highJump.Retry(idx); + _highJump.Participants[idx].Jump(10); + + Assert.AreEqual(before[0], _highJump.Participants[idx].Jumps[0], 0.0001); + Assert.AreEqual(before[1], _highJump.Participants[idx].Jumps[1], 0.0001); + Assert.AreEqual(10, _highJump.Participants[idx].Jumps[2], 0.0001); + } + private void ResetAllParticipantStatics() + { + var baseType = typeof(Lab8.Green.Task4.Discipline); + + var allTypes = baseType.Assembly + .GetTypes() + .Where(t => baseType.IsAssignableFrom(t)); + + foreach (var type in allTypes) + { + var staticFields = type.GetFields( + BindingFlags.Static | + BindingFlags.Public | + BindingFlags.NonPublic); + + foreach (var field in staticFields) + { + if (field.FieldType == typeof(int)) + field.SetValue(null, 0); + else if (field.FieldType == typeof(double)) + field.SetValue(null, 0.0); + else if (field.FieldType == typeof(bool)) + field.SetValue(null, false); + else + field.SetValue(null, null); + } + } + } + private void InitParticipants() + { + ResetAllParticipantStatics(); + for (int i = 0; i < _input.Length; i++) + { + _participants[i] = new Lab8.Green.Task4.Participant( + _input[i].Name, + _input[i].Surname); + } + } + + private void ApplyJumps() + { + for (int i = 0; i < _input.Length; i++) + foreach (var j in _input[i].Jumps) + _participants[i].Jump(j); + } + } +} From e602660e8bfa55461282d1bd9922a16c9c49653e Mon Sep 17 00:00:00 2001 From: safaryan elen Date: Thu, 2 Apr 2026 21:51:58 +0300 Subject: [PATCH 5/5] Update Task5.cs --- Lab8Test/Green/Task5.cs | 562 ++++++++++++++++++++-------------------- 1 file changed, 281 insertions(+), 281 deletions(-) diff --git a/Lab8Test/Green/Task5.cs b/Lab8Test/Green/Task5.cs index 1a322a76..aa2bb78d 100644 --- a/Lab8Test/Green/Task5.cs +++ b/Lab8Test/Green/Task5.cs @@ -1,281 +1,281 @@ -//using System.Reflection; -//using System.Text.Json; - -//namespace Lab8Test.Green -//{ -// [TestClass] -// public sealed class Task5 -// { -// record InputStudent(string Name, string Surname, int[] Marks); -// record InputGroup(string Name, InputStudent[] Students); -// record OutputGroup(string Name, double AverageMark); - -// private InputGroup[] _inputGroups; -// private OutputGroup[] _outputBase; -// private OutputGroup[] _outputElite; -// private OutputGroup[] _outputSpecial; - -// private Lab8.Green.Task5.Student[] _students; -// private Lab8.Green.Task5.Group[] _groups; - -// [TestInitialize] -// public void LoadData() -// { -// var folder = Directory.GetParent(Directory.GetCurrentDirectory()) -// .Parent.Parent.Parent.FullName; -// folder = Path.Combine(folder, "Lab8Test", "Green"); - -// var input = JsonSerializer.Deserialize( -// File.ReadAllText(Path.Combine(folder, "input.json")))!; -// var output = JsonSerializer.Deserialize( -// File.ReadAllText(Path.Combine(folder, "output.json")))!; - -// _inputGroups = input.GetProperty("Task5").Deserialize(); - -// _outputBase = output.GetProperty("Task5").GetProperty("Base").Deserialize(); -// _outputElite = output.GetProperty("Task5").GetProperty("Elite").Deserialize(); -// _outputSpecial = output.GetProperty("Task5").GetProperty("Special").Deserialize(); - -// _students = _inputGroups -// .SelectMany(g => g.Students) -// .Select(s => new Lab8.Green.Task5.Student(s.Name, s.Surname)) -// .ToArray(); -// } - -// [TestMethod] -// public void Test_00_OOP() -// { -// var student = typeof(Lab8.Green.Task5.Student); - -// Assert.IsTrue(student.IsValueType); -// Assert.AreEqual(0, student.GetFields(BindingFlags.Public | BindingFlags.Instance).Length); - -// Assert.IsTrue(student.GetProperty("Name")?.CanRead ?? false); -// Assert.IsTrue(student.GetProperty("Surname")?.CanRead ?? false); -// Assert.IsTrue(student.GetProperty("Marks")?.CanRead ?? false); -// Assert.IsTrue(student.GetProperty("AverageMark")?.CanRead ?? false); - -// Assert.IsFalse(student.GetProperty("Name")?.CanWrite ?? true); -// Assert.IsFalse(student.GetProperty("Surname")?.CanWrite ?? true); -// Assert.IsFalse(student.GetProperty("Marks")?.CanWrite ?? true); -// Assert.IsFalse(student.GetProperty("AverageMark")?.CanWrite ?? true); - -// Assert.IsNotNull(student.GetConstructor( -// BindingFlags.Instance | BindingFlags.Public, -// null, -// new[] { typeof(string), typeof(string) }, -// null)); - -// Assert.IsNotNull(student.GetMethod("Exam")); -// Assert.IsNotNull(student.GetMethod("Print")); - -// Assert.AreEqual(0, student.GetFields(BindingFlags.Public | BindingFlags.Instance).Length); -// Assert.AreEqual(student.GetConstructors().Count(c => c.IsPublic), 1); -// Assert.AreEqual(student.GetMethods(BindingFlags.Instance | BindingFlags.Public).Length, 10); -// Assert.AreEqual(student.GetProperties().Count(p => p.PropertyType.IsPublic), 4); - -// var group = typeof(Lab8.Green.Task5.Group); - -// Assert.IsTrue(group.IsClass); -// Assert.AreEqual(0, group.GetFields(BindingFlags.Public | BindingFlags.Instance).Length); - -// Assert.IsTrue(group.GetProperty("Name")?.CanRead ?? false); -// Assert.IsTrue(group.GetProperty("Students")?.CanRead ?? false); -// Assert.IsTrue(group.GetProperty("AverageMark")?.CanRead ?? false); - -// Assert.IsTrue(group.GetProperty("AverageMark")!.GetGetMethod()!.IsVirtual); - -// Assert.IsNotNull(group.GetConstructor( -// BindingFlags.Instance | BindingFlags.Public, -// null, -// new[] { typeof(string) }, -// null)); - -// Assert.IsNotNull(group.GetMethod("Add", new[] { typeof(Lab8.Green.Task5.Student) })); -// Assert.IsNotNull(group.GetMethod("Add", new[] { typeof(Lab8.Green.Task5.Student[]) })); -// Assert.IsNotNull(group.GetMethod("Print")); -// Assert.IsNotNull(group.GetMethod("SortByAverageMark")); - -// Assert.AreEqual(0, group.GetFields(BindingFlags.Public | BindingFlags.Instance).Length); -// Assert.AreEqual(group.GetConstructors().Count(c => c.IsPublic), 1); -// Assert.AreEqual(group.GetMethods(BindingFlags.Instance | BindingFlags.Public).Length, 10); -// Assert.AreEqual(group.GetProperties().Count(p => p.PropertyType.IsPublic), 3); - -// Assert.IsTrue(typeof(Lab8.Green.Task5.EliteGroup).IsSubclassOf(group)); -// Assert.IsTrue(typeof(Lab8.Green.Task5.SpecialGroup).IsSubclassOf(group)); - -// group = typeof(Lab8.Green.Task5.EliteGroup); -// Assert.AreEqual(0, group.GetFields(BindingFlags.Public | BindingFlags.Instance).Length); -// Assert.AreEqual(group.GetConstructors().Count(c => c.IsPublic), 1); -// Assert.AreEqual(group.GetMethods(BindingFlags.Instance | BindingFlags.Public).Length, 10); -// Assert.AreEqual(group.GetProperties().Count(p => p.PropertyType.IsPublic), 3); - -// group = typeof(Lab8.Green.Task5.SpecialGroup); -// Assert.AreEqual(0, group.GetFields(BindingFlags.Public | BindingFlags.Instance).Length); -// Assert.AreEqual(group.GetConstructors().Count(c => c.IsPublic), 1); -// Assert.AreEqual(group.GetMethods(BindingFlags.Instance | BindingFlags.Public).Length, 10); -// Assert.AreEqual(group.GetProperties().Count(p => p.PropertyType.IsPublic), 3); - -// } - -// [TestMethod] -// public void Test_01_CreateStudents() -// { -// Assert.AreEqual( -// _inputGroups.Sum(g => g.Students.Length), -// _students.Length); -// } - -// [TestMethod] -// public void Test_02_Exams() -// { -// RunExams(); - -// int idx = 0; -// foreach (var g in _inputGroups) -// { -// foreach (var s in g.Students) -// { -// var marks = s.Marks.Where(m => m > 0).ToArray(); -// double avg = marks.Length == 0 ? 0 : marks.Average(); - -// Assert.AreEqual(avg, _students[idx].AverageMark, 0.0001); -// idx++; -// } -// } -// } - -// [TestMethod] -// public void Test_03_BaseGroup_Average() -// { -// RunExams(); -// InitGroups(); - -// CheckGroups(_outputBase); -// } - -// [TestMethod] -// public void Test_04_EliteGroup_Average() -// { -// RunExams(); -// InitGroups(); - -// CheckGroups(_outputElite); -// } - -// [TestMethod] -// public void Test_05_SpecialGroup_Average() -// { -// RunExams(); -// InitGroups(); - -// CheckGroups(_outputSpecial); -// } - -// [TestMethod] -// public void Test_06_Sort() -// { -// RunExams(); -// InitGroups(); - -// Lab8.Green.Task5.Group.SortByAverageMark(_groups); - -// for (int i = 0; i < _groups.Length - 1; i++) -// { -// Assert.IsTrue(_groups[i].AverageMark >= _groups[i + 1].AverageMark); -// } -// } - -// private void RunExams() -// { -// int idx = 0; -// foreach (var g in _inputGroups) -// { -// foreach (var s in g.Students) -// { -// foreach (var m in s.Marks) -// _students[idx].Exam(m); -// idx++; -// } -// } -// } - -// private void ResetAllParticipantStatics() -// { -// var baseType = typeof(Lab8.Green.Task5.Group); - -// var allTypes = baseType.Assembly -// .GetTypes() -// .Where(t => baseType.IsAssignableFrom(t)); - -// foreach (var type in allTypes) -// { -// var staticFields = type.GetFields( -// BindingFlags.Static | -// BindingFlags.Public | -// BindingFlags.NonPublic); - -// foreach (var field in staticFields) -// { -// if (field.FieldType == typeof(int)) -// field.SetValue(null, 0); -// else if (field.FieldType == typeof(double)) -// field.SetValue(null, 0.0); -// else if (field.FieldType == typeof(bool)) -// field.SetValue(null, false); -// else -// field.SetValue(null, null); -// } -// } -// } -// private void InitGroups() where T : IGroupFactory, new() -// { -// ResetAllParticipantStatics(); -// var factory = new T(); - -// _groups = _inputGroups -// .Select(g => factory.Create(g.Name)) -// .ToArray(); - -// for (int i = 0, k = 0; i < _groups.Length; i++) -// { -// for (int j = 0; j < _inputGroups[i].Students.Length; j++) -// { -// _groups[i].Add(_students[k++]); -// } -// } -// } - -// private void CheckGroups(OutputGroup[] expected) -// { -// for (int i = 0; i < _groups.Length; i++) -// { -// Assert.AreEqual(expected[i].Name, _groups[i].Name); -// Assert.AreEqual(expected[i].AverageMark, _groups[i].AverageMark, 0.0001); -// } -// } - -// interface IGroupFactory -// { -// Lab8.Green.Task5.Group Create(string name); -// } - -// class BaseGroupFactory : IGroupFactory -// { -// public Lab8.Green.Task5.Group Create(string name) => -// new Lab8.Green.Task5.Group(name); -// } - -// class EliteGroupFactory : IGroupFactory -// { -// public Lab8.Green.Task5.Group Create(string name) => -// new Lab8.Green.Task5.EliteGroup(name); -// } - -// class SpecialGroupFactory : IGroupFactory -// { -// public Lab8.Green.Task5.Group Create(string name) => -// new Lab8.Green.Task5.SpecialGroup(name); -// } -// } -//} +using System.Reflection; +using System.Text.Json; + +namespace Lab8Test.Green +{ + [TestClass] + public sealed class Task5 + { + record InputStudent(string Name, string Surname, int[] Marks); + record InputGroup(string Name, InputStudent[] Students); + record OutputGroup(string Name, double AverageMark); + + private InputGroup[] _inputGroups; + private OutputGroup[] _outputBase; + private OutputGroup[] _outputElite; + private OutputGroup[] _outputSpecial; + + private Lab8.Green.Task5.Student[] _students; + private Lab8.Green.Task5.Group[] _groups; + + [TestInitialize] + public void LoadData() + { + var folder = Directory.GetParent(Directory.GetCurrentDirectory()) + .Parent.Parent.Parent.FullName; + folder = Path.Combine(folder, "Lab8Test", "Green"); + + var input = JsonSerializer.Deserialize( + File.ReadAllText(Path.Combine(folder, "input.json")))!; + var output = JsonSerializer.Deserialize( + File.ReadAllText(Path.Combine(folder, "output.json")))!; + + _inputGroups = input.GetProperty("Task5").Deserialize(); + + _outputBase = output.GetProperty("Task5").GetProperty("Base").Deserialize(); + _outputElite = output.GetProperty("Task5").GetProperty("Elite").Deserialize(); + _outputSpecial = output.GetProperty("Task5").GetProperty("Special").Deserialize(); + + _students = _inputGroups + .SelectMany(g => g.Students) + .Select(s => new Lab8.Green.Task5.Student(s.Name, s.Surname)) + .ToArray(); + } + + [TestMethod] + public void Test_00_OOP() + { + var student = typeof(Lab8.Green.Task5.Student); + + Assert.IsTrue(student.IsValueType); + Assert.AreEqual(0, student.GetFields(BindingFlags.Public | BindingFlags.Instance).Length); + + Assert.IsTrue(student.GetProperty("Name")?.CanRead ?? false); + Assert.IsTrue(student.GetProperty("Surname")?.CanRead ?? false); + Assert.IsTrue(student.GetProperty("Marks")?.CanRead ?? false); + Assert.IsTrue(student.GetProperty("AverageMark")?.CanRead ?? false); + + Assert.IsFalse(student.GetProperty("Name")?.CanWrite ?? true); + Assert.IsFalse(student.GetProperty("Surname")?.CanWrite ?? true); + Assert.IsFalse(student.GetProperty("Marks")?.CanWrite ?? true); + Assert.IsFalse(student.GetProperty("AverageMark")?.CanWrite ?? true); + + Assert.IsNotNull(student.GetConstructor( + BindingFlags.Instance | BindingFlags.Public, + null, + new[] { typeof(string), typeof(string) }, + null)); + + Assert.IsNotNull(student.GetMethod("Exam")); + Assert.IsNotNull(student.GetMethod("Print")); + + Assert.AreEqual(0, student.GetFields(BindingFlags.Public | BindingFlags.Instance).Length); + Assert.AreEqual(student.GetConstructors().Count(c => c.IsPublic), 1); + Assert.AreEqual(student.GetMethods(BindingFlags.Instance | BindingFlags.Public).Length, 10); + Assert.AreEqual(student.GetProperties().Count(p => p.PropertyType.IsPublic), 4); + + var group = typeof(Lab8.Green.Task5.Group); + + Assert.IsTrue(group.IsClass); + Assert.AreEqual(0, group.GetFields(BindingFlags.Public | BindingFlags.Instance).Length); + + Assert.IsTrue(group.GetProperty("Name")?.CanRead ?? false); + Assert.IsTrue(group.GetProperty("Students")?.CanRead ?? false); + Assert.IsTrue(group.GetProperty("AverageMark")?.CanRead ?? false); + + Assert.IsTrue(group.GetProperty("AverageMark")!.GetGetMethod()!.IsVirtual); + + Assert.IsNotNull(group.GetConstructor( + BindingFlags.Instance | BindingFlags.Public, + null, + new[] { typeof(string) }, + null)); + + Assert.IsNotNull(group.GetMethod("Add", new[] { typeof(Lab8.Green.Task5.Student) })); + Assert.IsNotNull(group.GetMethod("Add", new[] { typeof(Lab8.Green.Task5.Student[]) })); + Assert.IsNotNull(group.GetMethod("Print")); + Assert.IsNotNull(group.GetMethod("SortByAverageMark")); + + Assert.AreEqual(0, group.GetFields(BindingFlags.Public | BindingFlags.Instance).Length); + Assert.AreEqual(group.GetConstructors().Count(c => c.IsPublic), 1); + Assert.AreEqual(group.GetMethods(BindingFlags.Instance | BindingFlags.Public).Length, 10); + Assert.AreEqual(group.GetProperties().Count(p => p.PropertyType.IsPublic), 3); + + Assert.IsTrue(typeof(Lab8.Green.Task5.EliteGroup).IsSubclassOf(group)); + Assert.IsTrue(typeof(Lab8.Green.Task5.SpecialGroup).IsSubclassOf(group)); + + group = typeof(Lab8.Green.Task5.EliteGroup); + Assert.AreEqual(0, group.GetFields(BindingFlags.Public | BindingFlags.Instance).Length); + Assert.AreEqual(group.GetConstructors().Count(c => c.IsPublic), 1); + Assert.AreEqual(group.GetMethods(BindingFlags.Instance | BindingFlags.Public).Length, 10); + Assert.AreEqual(group.GetProperties().Count(p => p.PropertyType.IsPublic), 3); + + group = typeof(Lab8.Green.Task5.SpecialGroup); + Assert.AreEqual(0, group.GetFields(BindingFlags.Public | BindingFlags.Instance).Length); + Assert.AreEqual(group.GetConstructors().Count(c => c.IsPublic), 1); + Assert.AreEqual(group.GetMethods(BindingFlags.Instance | BindingFlags.Public).Length, 10); + Assert.AreEqual(group.GetProperties().Count(p => p.PropertyType.IsPublic), 3); + + } + + [TestMethod] + public void Test_01_CreateStudents() + { + Assert.AreEqual( + _inputGroups.Sum(g => g.Students.Length), + _students.Length); + } + + [TestMethod] + public void Test_02_Exams() + { + RunExams(); + + int idx = 0; + foreach (var g in _inputGroups) + { + foreach (var s in g.Students) + { + var marks = s.Marks.Where(m => m > 0).ToArray(); + double avg = marks.Length == 0 ? 0 : marks.Average(); + + Assert.AreEqual(avg, _students[idx].AverageMark, 0.0001); + idx++; + } + } + } + + [TestMethod] + public void Test_03_BaseGroup_Average() + { + RunExams(); + InitGroups(); + + CheckGroups(_outputBase); + } + + [TestMethod] + public void Test_04_EliteGroup_Average() + { + RunExams(); + InitGroups(); + + CheckGroups(_outputElite); + } + + [TestMethod] + public void Test_05_SpecialGroup_Average() + { + RunExams(); + InitGroups(); + + CheckGroups(_outputSpecial); + } + + [TestMethod] + public void Test_06_Sort() + { + RunExams(); + InitGroups(); + + Lab8.Green.Task5.Group.SortByAverageMark(_groups); + + for (int i = 0; i < _groups.Length - 1; i++) + { + Assert.IsTrue(_groups[i].AverageMark >= _groups[i + 1].AverageMark); + } + } + + private void RunExams() + { + int idx = 0; + foreach (var g in _inputGroups) + { + foreach (var s in g.Students) + { + foreach (var m in s.Marks) + _students[idx].Exam(m); + idx++; + } + } + } + + private void ResetAllParticipantStatics() + { + var baseType = typeof(Lab8.Green.Task5.Group); + + var allTypes = baseType.Assembly + .GetTypes() + .Where(t => baseType.IsAssignableFrom(t)); + + foreach (var type in allTypes) + { + var staticFields = type.GetFields( + BindingFlags.Static | + BindingFlags.Public | + BindingFlags.NonPublic); + + foreach (var field in staticFields) + { + if (field.FieldType == typeof(int)) + field.SetValue(null, 0); + else if (field.FieldType == typeof(double)) + field.SetValue(null, 0.0); + else if (field.FieldType == typeof(bool)) + field.SetValue(null, false); + else + field.SetValue(null, null); + } + } + } + private void InitGroups() where T : IGroupFactory, new() + { + ResetAllParticipantStatics(); + var factory = new T(); + + _groups = _inputGroups + .Select(g => factory.Create(g.Name)) + .ToArray(); + + for (int i = 0, k = 0; i < _groups.Length; i++) + { + for (int j = 0; j < _inputGroups[i].Students.Length; j++) + { + _groups[i].Add(_students[k++]); + } + } + } + + private void CheckGroups(OutputGroup[] expected) + { + for (int i = 0; i < _groups.Length; i++) + { + Assert.AreEqual(expected[i].Name, _groups[i].Name); + Assert.AreEqual(expected[i].AverageMark, _groups[i].AverageMark, 0.0001); + } + } + + interface IGroupFactory + { + Lab8.Green.Task5.Group Create(string name); + } + + class BaseGroupFactory : IGroupFactory + { + public Lab8.Green.Task5.Group Create(string name) => + new Lab8.Green.Task5.Group(name); + } + + class EliteGroupFactory : IGroupFactory + { + public Lab8.Green.Task5.Group Create(string name) => + new Lab8.Green.Task5.EliteGroup(name); + } + + class SpecialGroupFactory : IGroupFactory + { + public Lab8.Green.Task5.Group Create(string name) => + new Lab8.Green.Task5.SpecialGroup(name); + } + } +}