From aadcec52a0a5c4ed6e70bb90e68eee3ee6733ff1 Mon Sep 17 00:00:00 2001 From: Artem Date: Tue, 28 Mar 2023 20:56:11 +0300 Subject: [PATCH 1/6] =?UTF-8?q?+=D0=BB=D0=B8=D1=81=D1=82,=20+=D0=B4=D1=80.?= =?UTF-8?q?=20=D0=BB=D0=B8=D1=81=D1=82.=20=D0=A2=D0=B5=D1=81=D1=82=D1=8B?= =?UTF-8?q?=20=D0=B4=D0=BB=D1=8F=20=D0=BB=D0=B8=D1=81=D1=82=D0=B0=20=D0=B8?= =?UTF-8?q?=20=D0=B4=D1=80.=20=D0=BB=D0=B8=D1=81=D1=82=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ListAndUniqueList/ListAndUniqueList.sln | 37 ++++++ .../ListAndUniqueList/InvalidItemException.cs | 3 + .../InvalidPositionException.cs | 3 + ListAndUniqueList/ListAndUniqueList/List.cs | 116 ++++++++++++++++++ .../ListAndUniqueList.csproj | 10 ++ .../ListAndUniqueList/NullPointerException.cs | 3 + .../ListAndUniqueList/UniqueList.cs | 51 ++++++++ .../TestsForList/TestsForList.cs | 97 +++++++++++++++ .../TestsForUniqueList/TestsForUniqueList.cs | 114 +++++++++++++++++ 9 files changed, 434 insertions(+) create mode 100644 ListAndUniqueList/ListAndUniqueList.sln create mode 100644 ListAndUniqueList/ListAndUniqueList/InvalidItemException.cs create mode 100644 ListAndUniqueList/ListAndUniqueList/InvalidPositionException.cs create mode 100644 ListAndUniqueList/ListAndUniqueList/List.cs create mode 100644 ListAndUniqueList/ListAndUniqueList/ListAndUniqueList.csproj create mode 100644 ListAndUniqueList/ListAndUniqueList/NullPointerException.cs create mode 100644 ListAndUniqueList/ListAndUniqueList/UniqueList.cs create mode 100644 ListAndUniqueList/TestsForList/TestsForList.cs create mode 100644 ListAndUniqueList/TestsForUniqueList/TestsForUniqueList.cs diff --git a/ListAndUniqueList/ListAndUniqueList.sln b/ListAndUniqueList/ListAndUniqueList.sln new file mode 100644 index 0000000..e74949c --- /dev/null +++ b/ListAndUniqueList/ListAndUniqueList.sln @@ -0,0 +1,37 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.4.33403.182 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ListAndUniqueList", "ListAndUniqueList\ListAndUniqueList.csproj", "{7F7F9968-FB78-4353-AC51-E0D43F4A7557}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestsForList", "TestsForList\TestsForList.csproj", "{3D75ED9B-4A9D-4DF5-A0DF-8B2AEF2D01AF}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestsForUniqueList", "TestsForUniqueList\TestsForUniqueList.csproj", "{9F8F95B6-D03C-4272-B056-ACCD72870AD1}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {7F7F9968-FB78-4353-AC51-E0D43F4A7557}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {7F7F9968-FB78-4353-AC51-E0D43F4A7557}.Debug|Any CPU.Build.0 = Debug|Any CPU + {7F7F9968-FB78-4353-AC51-E0D43F4A7557}.Release|Any CPU.ActiveCfg = Release|Any CPU + {7F7F9968-FB78-4353-AC51-E0D43F4A7557}.Release|Any CPU.Build.0 = Release|Any CPU + {3D75ED9B-4A9D-4DF5-A0DF-8B2AEF2D01AF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {3D75ED9B-4A9D-4DF5-A0DF-8B2AEF2D01AF}.Debug|Any CPU.Build.0 = Debug|Any CPU + {3D75ED9B-4A9D-4DF5-A0DF-8B2AEF2D01AF}.Release|Any CPU.ActiveCfg = Release|Any CPU + {3D75ED9B-4A9D-4DF5-A0DF-8B2AEF2D01AF}.Release|Any CPU.Build.0 = Release|Any CPU + {9F8F95B6-D03C-4272-B056-ACCD72870AD1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {9F8F95B6-D03C-4272-B056-ACCD72870AD1}.Debug|Any CPU.Build.0 = Debug|Any CPU + {9F8F95B6-D03C-4272-B056-ACCD72870AD1}.Release|Any CPU.ActiveCfg = Release|Any CPU + {9F8F95B6-D03C-4272-B056-ACCD72870AD1}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {B598B7F0-1489-463C-B6BB-24CBDBDD7F83} + EndGlobalSection +EndGlobal diff --git a/ListAndUniqueList/ListAndUniqueList/InvalidItemException.cs b/ListAndUniqueList/ListAndUniqueList/InvalidItemException.cs new file mode 100644 index 0000000..b98dc06 --- /dev/null +++ b/ListAndUniqueList/ListAndUniqueList/InvalidItemException.cs @@ -0,0 +1,3 @@ +namespace ListAndUniqueList; + +public class InvalidItemException : Exception {} \ No newline at end of file diff --git a/ListAndUniqueList/ListAndUniqueList/InvalidPositionException.cs b/ListAndUniqueList/ListAndUniqueList/InvalidPositionException.cs new file mode 100644 index 0000000..aee732d --- /dev/null +++ b/ListAndUniqueList/ListAndUniqueList/InvalidPositionException.cs @@ -0,0 +1,3 @@ +namespace ListAndUniqueList; + +public class InvalidPositionException : Exception {} \ No newline at end of file diff --git a/ListAndUniqueList/ListAndUniqueList/List.cs b/ListAndUniqueList/ListAndUniqueList/List.cs new file mode 100644 index 0000000..61ccb69 --- /dev/null +++ b/ListAndUniqueList/ListAndUniqueList/List.cs @@ -0,0 +1,116 @@ +namespace ListAndUniqueList; + +public class List +{ + public ListElement? Head; + public ListElement? Tail; + + virtual public void AddElement(int value) + { + if (Head == null) + { + ListElement item = new ListElement(value, 0); + Head = item; + Tail = item; + ++Head.SizeList; + } + else + { + ListElement item = new ListElement(value, Head.SizeList); + Tail.Next = item; + Tail = item; + ++Head.SizeList; + } + } + + virtual public void RemoveElement(ref int item) + { + if (Head == null || Tail == null) + { + throw new NullPointerException(); + } + if (Tail == Head) + { + item = Tail.Value; + Tail = null; + Head = null; + return; + } + --Head.SizeList; + ListElement walker = Head; + while (walker.Next.Next != null) + { + walker = walker.Next; + } + item = walker.Next.Value; + Tail = walker; + walker.Next = null; + } + + virtual public void PrintTheElements() + { + ListElement walker = Head; + while (walker != null) + { + Console.Write(walker.Position); + Console.Write(' '); + Console.WriteLine(walker.Value); + walker = walker.Next; + } + } + + virtual public void ChangeValueByPosition(int position, int newValue) + { + if (Head == null || Tail == null) + { + throw new NullPointerException(); + } + ListElement? walker = Head; + while (walker != null && walker.Position != position) + { + walker = walker.Next; + } + if (walker != null) + { + walker.Value = newValue; + } + } + + virtual public bool IsEmpty() + { + return Head == null; + } + + virtual public int ReturnValueByPosition(int Position) + { + if (Head == null || Tail == null) + { + throw new NullPointerException(); + } + var walker = Head; + while (walker != null && walker.Position != Position) + { + walker = walker.Next; + } + if (walker == null) + { + throw new InvalidPositionException(); + } + return walker.Value; + } + + public class ListElement + { + public ListElement(int value, int position) + { + Value = value; + Position = position; + } + public int Value { get; set; } + public ListElement Next { get; set; } + + public int Position { get; set; } + + public int SizeList { get; set; } + } +} diff --git a/ListAndUniqueList/ListAndUniqueList/ListAndUniqueList.csproj b/ListAndUniqueList/ListAndUniqueList/ListAndUniqueList.csproj new file mode 100644 index 0000000..f02677b --- /dev/null +++ b/ListAndUniqueList/ListAndUniqueList/ListAndUniqueList.csproj @@ -0,0 +1,10 @@ + + + + Exe + net7.0 + enable + enable + + + diff --git a/ListAndUniqueList/ListAndUniqueList/NullPointerException.cs b/ListAndUniqueList/ListAndUniqueList/NullPointerException.cs new file mode 100644 index 0000000..110f756 --- /dev/null +++ b/ListAndUniqueList/ListAndUniqueList/NullPointerException.cs @@ -0,0 +1,3 @@ +namespace ListAndUniqueList; + +public class NullPointerException : Exception {} \ No newline at end of file diff --git a/ListAndUniqueList/ListAndUniqueList/UniqueList.cs b/ListAndUniqueList/ListAndUniqueList/UniqueList.cs new file mode 100644 index 0000000..ecb0ed4 --- /dev/null +++ b/ListAndUniqueList/ListAndUniqueList/UniqueList.cs @@ -0,0 +1,51 @@ +namespace ListAndUniqueList; + +public class UniqueList : List +{ + private bool Contains(int value) + { + var walker = Head; + while(walker != null) + { + if (walker.Value == value) + { + return true; + } + walker = walker.Next; + } + return false; + } + + public override void AddElement(int value) + { + if (Contains(value)) + { + throw new InvalidItemException(); + } + base.AddElement(value); + } + + public override void RemoveElement(ref int item) + { + base.RemoveElement(ref item); + } + + public override void ChangeValueByPosition(int position, int newValue) + { + if (Contains(newValue)) + { + throw new InvalidItemException(); + } + base.ChangeValueByPosition(position, newValue); + } + + public override bool IsEmpty() + { + return base.IsEmpty(); + } + + public override int ReturnValueByPosition(int Position) + { + return base.ReturnValueByPosition(Position); + } +} diff --git a/ListAndUniqueList/TestsForList/TestsForList.cs b/ListAndUniqueList/TestsForList/TestsForList.cs new file mode 100644 index 0000000..9c111dd --- /dev/null +++ b/ListAndUniqueList/TestsForList/TestsForList.cs @@ -0,0 +1,97 @@ +namespace TestList; + +using ListAndUniqueList; + +public class Tests +{ + List list; + + [SetUp] + public void Setup() + { + list = new List(); + } + + [TestCaseSource(nameof(ListForTest))] + public void ListShouldBeEmptyWhenCreated(List list) + { + Assert.True(list.IsEmpty()); + } + + [TestCaseSource(nameof(ListForTest))] + public void WhenAddedToTheListElementListShouldNotBeEmpty(List list) + { + list.AddElement(1); + Assert.False(list.IsEmpty()); + } + + [TestCaseSource(nameof(ListForTest))] + public void WhenAddingAnElementToTheListAndThenDeletingItTheSheetShouldbeEmpty(List list) + { + list.AddElement(1); + int item = 0; + list.RemoveElement(ref item); + Assert.True(list.IsEmpty()); + } + + [TestCaseSource(nameof(ListForTest))] + public void TheListShouldReturnTheCorrectDataAfterDeletion(List list) + { + list.AddElement(1); + list.AddElement(10); + int item = 0; + list.RemoveElement(ref item); + Assert.True(item == 10); + } + + [TestCaseSource(nameof(ListForTest))] + public void TheListShouldCorrectlyReplaceThePositionData(List list) + { + list.AddElement(1); + list.AddElement(10); + list.ChangeValueByPosition(1, 15); + int item = 0; + list.RemoveElement(ref item); + Assert.True(item == 15); + } + + [TestCaseSource(nameof(ListForTest))] + public void AnEmptyListShouldThrowAnExceptionWhenTryingToDelete(List list) + { + int item = 0; + Assert.Throws(() => list.RemoveElement(ref item)); + } + + [TestCaseSource(nameof(ListForTest))] + public void AnEmptyListShouldThrowAnExceptionWhenTryingToChangeValueByPosition(List list) + { + Assert.Throws(() => list.ChangeValueByPosition(10, 10)); + } + + [TestCaseSource(nameof(ListForTest))] + public void AnEmptyListShouldThrowAnExceptionWhenTryingToChangeCheckValueByPosition(List list) + { + Assert.Throws(() => list.ReturnValueByPosition(10)); + } + + [TestCaseSource(nameof(ListForTest))] + public void TheListShouldThrowAnExceptionIfThereIsNoPositionInTheListWhenReturningAnItemByPosition(List list) + { + list.AddElement(1); + Assert.Throws(() => list.ReturnValueByPosition(10)); + } + + [TestCaseSource(nameof(ListForTest))] + public void TheListShouldReturnTheCorrectItemByPosition(List list) + { + list.AddElement(15); + int item = list.ReturnValueByPosition(0); + Assert.True(item == 15); + } + + private static IEnumerable ListForTest + => new TestCaseData[] + { + new TestCaseData(new List()), + }; +} \ No newline at end of file diff --git a/ListAndUniqueList/TestsForUniqueList/TestsForUniqueList.cs b/ListAndUniqueList/TestsForUniqueList/TestsForUniqueList.cs new file mode 100644 index 0000000..d7d097c --- /dev/null +++ b/ListAndUniqueList/TestsForUniqueList/TestsForUniqueList.cs @@ -0,0 +1,114 @@ +namespace TestsForUniqueList; + +using ListAndUniqueList; +using Newtonsoft.Json.Linq; + +public class Tests +{ + UniqueList uniqueList; + + [SetUp] + public void Setup() + { + uniqueList = new UniqueList(); + } + + [TestCaseSource(nameof(ListForTest))] + public void UniqueListShouldBeEmptyWhenCreated(UniqueList uniqueList) + { + Assert.True(uniqueList.IsEmpty()); + } + + [TestCaseSource(nameof(ListForTest))] + public void WhenAddedToTheListElementUniqueListShouldNotBeEmpty(UniqueList uniqueList) + { + uniqueList.AddElement(1); + Assert.False(uniqueList.IsEmpty()); + } + + [TestCaseSource(nameof(ListForTest))] + public void WhenAddingAnElementToTheUniqueListAndThenDeletingItTheSheetShouldbeEmpty(UniqueList uniqueList) + { + uniqueList.AddElement(1); + int item = 0; + uniqueList.RemoveElement(ref item); + Assert.True(uniqueList.IsEmpty()); + } + + [TestCaseSource(nameof(ListForTest))] + public void TheUniqueListShouldReturnTheCorrectDataAfterDeletion(UniqueList uniqueList) + { + uniqueList.AddElement(1); + uniqueList.AddElement(10); + int item = 0; + uniqueList.RemoveElement(ref item); + Assert.True(item == 10); + } + + [TestCaseSource(nameof(ListForTest))] + public void TheUniqueListShouldCorrectlyReplaceThePositionData(UniqueList uniqueList) + { + uniqueList.AddElement(1); + uniqueList.AddElement(10); + uniqueList.ChangeValueByPosition(1, 15); + int item = 0; + uniqueList.RemoveElement(ref item); + Assert.True(item == 15); + } + + [TestCaseSource(nameof(ListForTest))] + public void AnEmptyUniqueListShouldThrowAnExceptionWhenTryingToDelete(UniqueList uniqueList) + { + int item = 0; + Assert.Throws(() => uniqueList.RemoveElement(ref item)); + } + + [TestCaseSource(nameof(ListForTest))] + public void AnEmptyUniqueListShouldThrowAnExceptionWhenTryingToChangeValueByPosition(UniqueList uniqueList) + { + Assert.Throws(() => uniqueList.ChangeValueByPosition(10, 10)); + } + + [TestCaseSource(nameof(ListForTest))] + public void AnEmptyUniqueListShouldThrowAnExceptionWhenTryingToChangeCheckValueByPosition(UniqueList uniqueList) + { + Assert.Throws(() => uniqueList.ReturnValueByPosition(10)); + } + + [TestCaseSource(nameof(ListForTest))] + public void TheUniqueListShouldThrowAnExceptionIfThereIsNoPositionInTheListWhenReturningAnItemByPosition(UniqueList uniqueList) + { + uniqueList.AddElement(1); + Assert.Throws(() => uniqueList.ReturnValueByPosition(10)); + } + + [TestCaseSource(nameof(ListForTest))] + public void TheUniqueListShouldReturnTheCorrectItemByPosition(UniqueList uniqueList) + { + uniqueList.AddElement(15); + int item = uniqueList.ReturnValueByPosition(0); + Assert.True(item == 15); + } + + [TestCaseSource(nameof(ListForTest))] + public void UniqueListShouldThrowAnExceptionWhenReceivingARepeatingValueWhenAdding(UniqueList uniqueList) + { + uniqueList.AddElement(15); + uniqueList.AddElement(10); + Assert.Throws(() => uniqueList.AddElement(15)); + } + + [TestCaseSource(nameof(ListForTest))] + public void UniqueListShouldThrowAnExceptionWhenReceivingARepeatingValueWhenChangingValue(UniqueList uniqueList) + { + uniqueList.AddElement(15); + uniqueList.AddElement(10); + Assert.Throws(() => uniqueList.ChangeValueByPosition(1, 15)); + } + + private static IEnumerable ListForTest + => new TestCaseData[] + { + new TestCaseData(new UniqueList()), + }; +} \ No newline at end of file From 5d23bb52ea344a8fd75f59fd7fa7ec46a9748af6 Mon Sep 17 00:00:00 2001 From: Artem Date: Thu, 30 Mar 2023 23:52:20 +0300 Subject: [PATCH 2/6] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D1=8B=D0=BB=20csproj?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ListAndUniqueList/ListAndUniqueList.sln | 22 +++++++++--------- ListAndUniqueList/ListAndUniqueList/List.cs | 7 ++++++ .../ListAndUniqueList/UniqueList.cs | 6 +++++ .../TestsList.cs} | 0 ListAndUniqueList/TestsList/TestsList.csproj | 23 +++++++++++++++++++ .../TestsUniqueList.cs} | 0 .../TestsUniqueList/TestsUniqueList.csproj | 23 +++++++++++++++++++ 7 files changed, 70 insertions(+), 11 deletions(-) rename ListAndUniqueList/{TestsForList/TestsForList.cs => TestsList/TestsList.cs} (100%) create mode 100644 ListAndUniqueList/TestsList/TestsList.csproj rename ListAndUniqueList/{TestsForUniqueList/TestsForUniqueList.cs => TestsUniqueList/TestsUniqueList.cs} (100%) create mode 100644 ListAndUniqueList/TestsUniqueList/TestsUniqueList.csproj diff --git a/ListAndUniqueList/ListAndUniqueList.sln b/ListAndUniqueList/ListAndUniqueList.sln index e74949c..1976afc 100644 --- a/ListAndUniqueList/ListAndUniqueList.sln +++ b/ListAndUniqueList/ListAndUniqueList.sln @@ -3,11 +3,11 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio Version 17 VisualStudioVersion = 17.4.33403.182 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ListAndUniqueList", "ListAndUniqueList\ListAndUniqueList.csproj", "{7F7F9968-FB78-4353-AC51-E0D43F4A7557}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ListAndUniqueList", "ListAndUniqueList\ListAndUniqueList.csproj", "{7F7F9968-FB78-4353-AC51-E0D43F4A7557}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestsForList", "TestsForList\TestsForList.csproj", "{3D75ED9B-4A9D-4DF5-A0DF-8B2AEF2D01AF}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestsList", "TestsList\TestsList.csproj", "{BB4EEB6F-6C72-4F81-A0BD-4AADA4AD9058}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestsForUniqueList", "TestsForUniqueList\TestsForUniqueList.csproj", "{9F8F95B6-D03C-4272-B056-ACCD72870AD1}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestsUniqueList", "TestsUniqueList\TestsUniqueList.csproj", "{6FA174A8-43FA-48FB-B3B7-E747E92E4A66}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -19,14 +19,14 @@ Global {7F7F9968-FB78-4353-AC51-E0D43F4A7557}.Debug|Any CPU.Build.0 = Debug|Any CPU {7F7F9968-FB78-4353-AC51-E0D43F4A7557}.Release|Any CPU.ActiveCfg = Release|Any CPU {7F7F9968-FB78-4353-AC51-E0D43F4A7557}.Release|Any CPU.Build.0 = Release|Any CPU - {3D75ED9B-4A9D-4DF5-A0DF-8B2AEF2D01AF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {3D75ED9B-4A9D-4DF5-A0DF-8B2AEF2D01AF}.Debug|Any CPU.Build.0 = Debug|Any CPU - {3D75ED9B-4A9D-4DF5-A0DF-8B2AEF2D01AF}.Release|Any CPU.ActiveCfg = Release|Any CPU - {3D75ED9B-4A9D-4DF5-A0DF-8B2AEF2D01AF}.Release|Any CPU.Build.0 = Release|Any CPU - {9F8F95B6-D03C-4272-B056-ACCD72870AD1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {9F8F95B6-D03C-4272-B056-ACCD72870AD1}.Debug|Any CPU.Build.0 = Debug|Any CPU - {9F8F95B6-D03C-4272-B056-ACCD72870AD1}.Release|Any CPU.ActiveCfg = Release|Any CPU - {9F8F95B6-D03C-4272-B056-ACCD72870AD1}.Release|Any CPU.Build.0 = Release|Any CPU + {BB4EEB6F-6C72-4F81-A0BD-4AADA4AD9058}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {BB4EEB6F-6C72-4F81-A0BD-4AADA4AD9058}.Debug|Any CPU.Build.0 = Debug|Any CPU + {BB4EEB6F-6C72-4F81-A0BD-4AADA4AD9058}.Release|Any CPU.ActiveCfg = Release|Any CPU + {BB4EEB6F-6C72-4F81-A0BD-4AADA4AD9058}.Release|Any CPU.Build.0 = Release|Any CPU + {6FA174A8-43FA-48FB-B3B7-E747E92E4A66}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {6FA174A8-43FA-48FB-B3B7-E747E92E4A66}.Debug|Any CPU.Build.0 = Debug|Any CPU + {6FA174A8-43FA-48FB-B3B7-E747E92E4A66}.Release|Any CPU.ActiveCfg = Release|Any CPU + {6FA174A8-43FA-48FB-B3B7-E747E92E4A66}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/ListAndUniqueList/ListAndUniqueList/List.cs b/ListAndUniqueList/ListAndUniqueList/List.cs index 61ccb69..21a9a79 100644 --- a/ListAndUniqueList/ListAndUniqueList/List.cs +++ b/ListAndUniqueList/ListAndUniqueList/List.cs @@ -1,10 +1,12 @@ namespace ListAndUniqueList; +// Container for storing values public class List { public ListElement? Head; public ListElement? Tail; + // Adding element to list virtual public void AddElement(int value) { if (Head == null) @@ -23,6 +25,7 @@ virtual public void AddElement(int value) } } + // Deleting element in list virtual public void RemoveElement(ref int item) { if (Head == null || Tail == null) @@ -47,6 +50,7 @@ virtual public void RemoveElement(ref int item) walker.Next = null; } + // Print all elements in list virtual public void PrintTheElements() { ListElement walker = Head; @@ -59,6 +63,7 @@ virtual public void PrintTheElements() } } + // Change value by position virtual public void ChangeValueByPosition(int position, int newValue) { if (Head == null || Tail == null) @@ -76,11 +81,13 @@ virtual public void ChangeValueByPosition(int position, int newValue) } } + // Checks if the list is empty virtual public bool IsEmpty() { return Head == null; } + // Returns the value by position virtual public int ReturnValueByPosition(int Position) { if (Head == null || Tail == null) diff --git a/ListAndUniqueList/ListAndUniqueList/UniqueList.cs b/ListAndUniqueList/ListAndUniqueList/UniqueList.cs index ecb0ed4..de74c6b 100644 --- a/ListAndUniqueList/ListAndUniqueList/UniqueList.cs +++ b/ListAndUniqueList/ListAndUniqueList/UniqueList.cs @@ -1,5 +1,6 @@ namespace ListAndUniqueList; +// The same list only without repetitions public class UniqueList : List { private bool Contains(int value) @@ -16,6 +17,7 @@ private bool Contains(int value) return false; } + // Adding element to unique list public override void AddElement(int value) { if (Contains(value)) @@ -25,11 +27,13 @@ public override void AddElement(int value) base.AddElement(value); } + // Deleting element in unique list public override void RemoveElement(ref int item) { base.RemoveElement(ref item); } + // Change value by position public override void ChangeValueByPosition(int position, int newValue) { if (Contains(newValue)) @@ -39,11 +43,13 @@ public override void ChangeValueByPosition(int position, int newValue) base.ChangeValueByPosition(position, newValue); } + // Checks if the list is unique empty public override bool IsEmpty() { return base.IsEmpty(); } + // Returns the value by position public override int ReturnValueByPosition(int Position) { return base.ReturnValueByPosition(Position); diff --git a/ListAndUniqueList/TestsForList/TestsForList.cs b/ListAndUniqueList/TestsList/TestsList.cs similarity index 100% rename from ListAndUniqueList/TestsForList/TestsForList.cs rename to ListAndUniqueList/TestsList/TestsList.cs diff --git a/ListAndUniqueList/TestsList/TestsList.csproj b/ListAndUniqueList/TestsList/TestsList.csproj new file mode 100644 index 0000000..16345a4 --- /dev/null +++ b/ListAndUniqueList/TestsList/TestsList.csproj @@ -0,0 +1,23 @@ + + + + net7.0 + enable + enable + + false + + + + + + + + + + + + + + + diff --git a/ListAndUniqueList/TestsForUniqueList/TestsForUniqueList.cs b/ListAndUniqueList/TestsUniqueList/TestsUniqueList.cs similarity index 100% rename from ListAndUniqueList/TestsForUniqueList/TestsForUniqueList.cs rename to ListAndUniqueList/TestsUniqueList/TestsUniqueList.cs diff --git a/ListAndUniqueList/TestsUniqueList/TestsUniqueList.csproj b/ListAndUniqueList/TestsUniqueList/TestsUniqueList.csproj new file mode 100644 index 0000000..16345a4 --- /dev/null +++ b/ListAndUniqueList/TestsUniqueList/TestsUniqueList.csproj @@ -0,0 +1,23 @@ + + + + net7.0 + enable + enable + + false + + + + + + + + + + + + + + + From 752a69f74803836f8f5ed5f943c0fbf1afbff6ea Mon Sep 17 00:00:00 2001 From: Artem Date: Thu, 30 Mar 2023 23:53:53 +0300 Subject: [PATCH 3/6] =?UTF-8?q?+main=20=D0=B4=D0=BB=D1=8F=20CI?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ListAndUniqueList/ListAndUniqueList/Program.cs | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 ListAndUniqueList/ListAndUniqueList/Program.cs diff --git a/ListAndUniqueList/ListAndUniqueList/Program.cs b/ListAndUniqueList/ListAndUniqueList/Program.cs new file mode 100644 index 0000000..5db597b --- /dev/null +++ b/ListAndUniqueList/ListAndUniqueList/Program.cs @@ -0,0 +1,11 @@ +using System; + +namespace ListAndUniqueList; + +class Program +{ + public static void Main(string[] args) + { + + } +} From 4369c93aa8e5e75850eec61fb59619dd7c3f891d Mon Sep 17 00:00:00 2001 From: Artem Date: Fri, 31 Mar 2023 00:55:59 +0300 Subject: [PATCH 4/6] +Unsigns --- ListAndUniqueList/TestsList/Usings.cs | 1 + ListAndUniqueList/TestsUniqueList/Usings.cs | 1 + 2 files changed, 2 insertions(+) create mode 100644 ListAndUniqueList/TestsList/Usings.cs create mode 100644 ListAndUniqueList/TestsUniqueList/Usings.cs diff --git a/ListAndUniqueList/TestsList/Usings.cs b/ListAndUniqueList/TestsList/Usings.cs new file mode 100644 index 0000000..cefced4 --- /dev/null +++ b/ListAndUniqueList/TestsList/Usings.cs @@ -0,0 +1 @@ +global using NUnit.Framework; \ No newline at end of file diff --git a/ListAndUniqueList/TestsUniqueList/Usings.cs b/ListAndUniqueList/TestsUniqueList/Usings.cs new file mode 100644 index 0000000..cefced4 --- /dev/null +++ b/ListAndUniqueList/TestsUniqueList/Usings.cs @@ -0,0 +1 @@ +global using NUnit.Framework; \ No newline at end of file From c34a0be3b0888900fda78bd564e9847675742652 Mon Sep 17 00:00:00 2001 From: Artem Date: Thu, 25 May 2023 22:21:15 +0300 Subject: [PATCH 5/6] =?UTF-8?q?=D0=A0=D0=B5=D0=B2=D1=8C=D1=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ListAndUniqueList/InvalidItemException.cs | 3 + .../InvalidPositionException.cs | 3 + ListAndUniqueList/ListAndUniqueList/List.cs | 184 ++++++++++++++---- .../ListAndUniqueList.csproj | 2 +- .../ListAndUniqueList/NullPointerException.cs | 5 +- .../ListAndUniqueList/Program.cs | 11 -- .../ListAndUniqueList/UniqueList.cs | 50 +---- ListAndUniqueList/TestsList/TestsList.cs | 47 ++--- .../TestsUniqueList/TestsUniqueList.cs | 106 ++-------- 9 files changed, 194 insertions(+), 217 deletions(-) delete mode 100644 ListAndUniqueList/ListAndUniqueList/Program.cs diff --git a/ListAndUniqueList/ListAndUniqueList/InvalidItemException.cs b/ListAndUniqueList/ListAndUniqueList/InvalidItemException.cs index b98dc06..db53bea 100644 --- a/ListAndUniqueList/ListAndUniqueList/InvalidItemException.cs +++ b/ListAndUniqueList/ListAndUniqueList/InvalidItemException.cs @@ -1,3 +1,6 @@ namespace ListAndUniqueList; +/// +/// Throw exception when item is already in unique list +/// public class InvalidItemException : Exception {} \ No newline at end of file diff --git a/ListAndUniqueList/ListAndUniqueList/InvalidPositionException.cs b/ListAndUniqueList/ListAndUniqueList/InvalidPositionException.cs index aee732d..29e3c68 100644 --- a/ListAndUniqueList/ListAndUniqueList/InvalidPositionException.cs +++ b/ListAndUniqueList/ListAndUniqueList/InvalidPositionException.cs @@ -1,3 +1,6 @@ namespace ListAndUniqueList; +/// +/// Throw exception when the position is incorrect +/// public class InvalidPositionException : Exception {} \ No newline at end of file diff --git a/ListAndUniqueList/ListAndUniqueList/List.cs b/ListAndUniqueList/ListAndUniqueList/List.cs index 21a9a79..d3e5a1a 100644 --- a/ListAndUniqueList/ListAndUniqueList/List.cs +++ b/ListAndUniqueList/ListAndUniqueList/List.cs @@ -1,103 +1,191 @@ namespace ListAndUniqueList; -// Container for storing values +/// +/// A list for storing and interacting with int type elements +/// public class List { - public ListElement? Head; - public ListElement? Tail; + private ListElement? Head; + private ListElement? Tail; - // Adding element to list - virtual public void AddElement(int value) + /// + /// Adding an item to the list + /// + virtual public void AddElement(int position, int value) { if (Head == null) { - ListElement item = new ListElement(value, 0); + var item = new ListElement(value); Head = item; Tail = item; ++Head.SizeList; } else { - ListElement item = new ListElement(value, Head.SizeList); - Tail.Next = item; - Tail = item; ++Head.SizeList; + ListElement item = new ListElement(value); + if (Tail == null) + { + throw new NullReferenceException(); + } + if (position == 0) + { + var temp = Head.Next; + Head = item; + Head.Next = temp; + return; + } + if (position == Head.SizeList - 1) + { + Tail.Next = item; + Tail = item; + return; + } + var walker = Head; + int counter = 0; + + while (walker != null && walker.Next != null && counter != position) + { + walker = walker.Next; + counter++; + } + if (walker == null || walker.Next == null) + { + throw new NullReferenceException(); + } + else + { + var temp = walker.Next.Next; + walker.Next = item; + item.Next = temp; + } } } - // Deleting element in list - virtual public void RemoveElement(ref int item) + /// + /// Deletes an item at the end of the list + /// + /// Throws an exception when the list is empty + /// Element which was deleted + virtual public int RemoveElement(int position) { if (Head == null || Tail == null) { - throw new NullPointerException(); + throw new NullListException(); + } + if (position > Head.SizeList) + { + throw new NullReferenceException(); } if (Tail == Head) { - item = Tail.Value; + var itemFromTail = Tail.Value; Tail = null; Head = null; - return; + return itemFromTail; } - --Head.SizeList; - ListElement walker = Head; - while (walker.Next.Next != null) + if (position == 0) + { + var itemFromHead = Head.Value; + Head = Head.Next; + return itemFromHead; + } + if (position == Head.SizeList) + { + var itemFromTail = Tail.Value; + var walkerForTail = Head; + while (walkerForTail != null && walkerForTail.Next != Tail) + { + walkerForTail = walkerForTail.Next; + } + if (walkerForTail == null) + { + throw new NullReferenceException(); + } + Tail = walkerForTail; + walkerForTail.Next = null; + } + + int counter = 0; + var walker = Head; + while (walker != null && walker.Next.Next != null && counter != position - 1) { walker = walker.Next; + counter++; + } + if (walker == null || walker.Next == null) + { + throw new NullReferenceException(); } - item = walker.Next.Value; - Tail = walker; - walker.Next = null; + var item = walker.Next.Value; + walker.Next = walker.Next.Next; + --Head.SizeList; + return item; } - // Print all elements in list + /// + /// Prints list items + /// virtual public void PrintTheElements() { ListElement walker = Head; + int counter = 0; while (walker != null) { - Console.Write(walker.Position); + Console.Write(counter); Console.Write(' '); Console.WriteLine(walker.Value); walker = walker.Next; + ++counter; } } - // Change value by position + /// + /// Changes values by position + /// + /// Throws an exception when the list is empty virtual public void ChangeValueByPosition(int position, int newValue) { if (Head == null || Tail == null) { - throw new NullPointerException(); + throw new NullListException(); } ListElement? walker = Head; - while (walker != null && walker.Position != position) + int counter = 0; + while (walker != null && counter != position) { walker = walker.Next; + counter++; } - if (walker != null) + if (walker == null) { - walker.Value = newValue; + throw new NullReferenceException(); } + walker.Value = newValue; } - // Checks if the list is empty - virtual public bool IsEmpty() - { - return Head == null; - } + /// + /// Checks if the list is empty + /// + virtual public bool IsEmpty() => Head == null; - // Returns the value by position - virtual public int ReturnValueByPosition(int Position) + /// + /// Returns the value by position + /// + /// Throws an exception when the list is empty + /// Throws an excwption when there is no such position in the list + virtual public int ReturnValueByPosition(int position) { if (Head == null || Tail == null) { - throw new NullPointerException(); + throw new NullListException(); } var walker = Head; - while (walker != null && walker.Position != Position) + int counter = 0; + while (walker != null && counter != position) { walker = walker.Next; + ++counter; } if (walker == null) { @@ -106,17 +194,33 @@ virtual public int ReturnValueByPosition(int Position) return walker.Value; } + /// + /// Checks if there is an item in the list + /// + /// Returns true if present and false if absent + public bool Contains(int value) + { + var walker = Head; + while (walker != null) + { + if (walker.Value == value) + { + return true; + } + walker = walker.Next; + } + return false; + } + public class ListElement { - public ListElement(int value, int position) + public ListElement(int value) { Value = value; - Position = position; } public int Value { get; set; } - public ListElement Next { get; set; } - public int Position { get; set; } + public ListElement Next { get; set; } public int SizeList { get; set; } } diff --git a/ListAndUniqueList/ListAndUniqueList/ListAndUniqueList.csproj b/ListAndUniqueList/ListAndUniqueList/ListAndUniqueList.csproj index f02677b..dc2bb05 100644 --- a/ListAndUniqueList/ListAndUniqueList/ListAndUniqueList.csproj +++ b/ListAndUniqueList/ListAndUniqueList/ListAndUniqueList.csproj @@ -1,7 +1,7 @@ - Exe + Library net7.0 enable enable diff --git a/ListAndUniqueList/ListAndUniqueList/NullPointerException.cs b/ListAndUniqueList/ListAndUniqueList/NullPointerException.cs index 110f756..6042e21 100644 --- a/ListAndUniqueList/ListAndUniqueList/NullPointerException.cs +++ b/ListAndUniqueList/ListAndUniqueList/NullPointerException.cs @@ -1,3 +1,6 @@ namespace ListAndUniqueList; -public class NullPointerException : Exception {} \ No newline at end of file +/// +/// Throw exception when list is empty +/// +public class NullListException : Exception {} \ No newline at end of file diff --git a/ListAndUniqueList/ListAndUniqueList/Program.cs b/ListAndUniqueList/ListAndUniqueList/Program.cs deleted file mode 100644 index 5db597b..0000000 --- a/ListAndUniqueList/ListAndUniqueList/Program.cs +++ /dev/null @@ -1,11 +0,0 @@ -using System; - -namespace ListAndUniqueList; - -class Program -{ - public static void Main(string[] args) - { - - } -} diff --git a/ListAndUniqueList/ListAndUniqueList/UniqueList.cs b/ListAndUniqueList/ListAndUniqueList/UniqueList.cs index de74c6b..613bee2 100644 --- a/ListAndUniqueList/ListAndUniqueList/UniqueList.cs +++ b/ListAndUniqueList/ListAndUniqueList/UniqueList.cs @@ -1,57 +1,23 @@ namespace ListAndUniqueList; -// The same list only without repetitions +/// +/// List only without repetitions +/// public class UniqueList : List { - private bool Contains(int value) + public override void AddElement(int position, int value) { - var walker = Head; - while(walker != null) + if (!Contains(value)) { - if (walker.Value == value) - { - return true; - } - walker = walker.Next; + base.AddElement(position, value); } - return false; } - // Adding element to unique list - public override void AddElement(int value) - { - if (Contains(value)) - { - throw new InvalidItemException(); - } - base.AddElement(value); - } - - // Deleting element in unique list - public override void RemoveElement(ref int item) - { - base.RemoveElement(ref item); - } - - // Change value by position public override void ChangeValueByPosition(int position, int newValue) { - if (Contains(newValue)) + if (!Contains(newValue)) { - throw new InvalidItemException(); + base.ChangeValueByPosition(position, newValue); } - base.ChangeValueByPosition(position, newValue); - } - - // Checks if the list is unique empty - public override bool IsEmpty() - { - return base.IsEmpty(); - } - - // Returns the value by position - public override int ReturnValueByPosition(int Position) - { - return base.ReturnValueByPosition(Position); } } diff --git a/ListAndUniqueList/TestsList/TestsList.cs b/ListAndUniqueList/TestsList/TestsList.cs index 9c111dd..9aec4d3 100644 --- a/ListAndUniqueList/TestsList/TestsList.cs +++ b/ListAndUniqueList/TestsList/TestsList.cs @@ -4,14 +4,6 @@ namespace TestList; public class Tests { - List list; - - [SetUp] - public void Setup() - { - list = new List(); - } - [TestCaseSource(nameof(ListForTest))] public void ListShouldBeEmptyWhenCreated(List list) { @@ -21,77 +13,76 @@ public void ListShouldBeEmptyWhenCreated(List list) [TestCaseSource(nameof(ListForTest))] public void WhenAddedToTheListElementListShouldNotBeEmpty(List list) { - list.AddElement(1); + list.AddElement(0, 1); Assert.False(list.IsEmpty()); } [TestCaseSource(nameof(ListForTest))] public void WhenAddingAnElementToTheListAndThenDeletingItTheSheetShouldbeEmpty(List list) { - list.AddElement(1); + list.AddElement(0, 1); int item = 0; - list.RemoveElement(ref item); + list.RemoveElement(item); Assert.True(list.IsEmpty()); } [TestCaseSource(nameof(ListForTest))] public void TheListShouldReturnTheCorrectDataAfterDeletion(List list) { - list.AddElement(1); - list.AddElement(10); - int item = 0; - list.RemoveElement(ref item); - Assert.True(item == 10); + list.AddElement(0, 1); + list.AddElement(1, 10); + list.AddElement(2, 12); + int item = list.RemoveElement(1); + Assert.That(10, Is.EqualTo(item)); } [TestCaseSource(nameof(ListForTest))] public void TheListShouldCorrectlyReplaceThePositionData(List list) { - list.AddElement(1); - list.AddElement(10); + list.AddElement(0, 1); + list.AddElement(1, 10); list.ChangeValueByPosition(1, 15); - int item = 0; - list.RemoveElement(ref item); - Assert.True(item == 15); + int item = list.RemoveElement(1); + Assert.That(15, Is.EqualTo(item)); } [TestCaseSource(nameof(ListForTest))] public void AnEmptyListShouldThrowAnExceptionWhenTryingToDelete(List list) { - int item = 0; - Assert.Throws(() => list.RemoveElement(ref item)); + Assert.Throws(() => list.RemoveElement(0)); } [TestCaseSource(nameof(ListForTest))] public void AnEmptyListShouldThrowAnExceptionWhenTryingToChangeValueByPosition(List list) { - Assert.Throws(() => list.ChangeValueByPosition(10, 10)); + Assert.Throws(() => list.ChangeValueByPosition(10, 10)); } [TestCaseSource(nameof(ListForTest))] public void AnEmptyListShouldThrowAnExceptionWhenTryingToChangeCheckValueByPosition(List list) { - Assert.Throws(() => list.ReturnValueByPosition(10)); + Assert.Throws(() => list.ReturnValueByPosition(10)); } [TestCaseSource(nameof(ListForTest))] public void TheListShouldThrowAnExceptionIfThereIsNoPositionInTheListWhenReturningAnItemByPosition(List list) { - list.AddElement(1); + list.AddElement(0, 1); Assert.Throws(() => list.ReturnValueByPosition(10)); } [TestCaseSource(nameof(ListForTest))] public void TheListShouldReturnTheCorrectItemByPosition(List list) { - list.AddElement(15); + list.AddElement(0, 15); int item = list.ReturnValueByPosition(0); - Assert.True(item == 15); + Assert.That(15, Is.EqualTo(item)); } private static IEnumerable ListForTest => new TestCaseData[] { new TestCaseData(new List()), + new TestCaseData(new UniqueList()), }; } \ No newline at end of file diff --git a/ListAndUniqueList/TestsUniqueList/TestsUniqueList.cs b/ListAndUniqueList/TestsUniqueList/TestsUniqueList.cs index d7d097c..db7c921 100644 --- a/ListAndUniqueList/TestsUniqueList/TestsUniqueList.cs +++ b/ListAndUniqueList/TestsUniqueList/TestsUniqueList.cs @@ -1,11 +1,10 @@ namespace TestsForUniqueList; using ListAndUniqueList; -using Newtonsoft.Json.Linq; public class Tests { - UniqueList uniqueList; + private UniqueList uniqueList; [SetUp] public void Setup() @@ -13,102 +12,21 @@ public void Setup() uniqueList = new UniqueList(); } - [TestCaseSource(nameof(ListForTest))] - public void UniqueListShouldBeEmptyWhenCreated(UniqueList uniqueList) + [Test] + public void UniqueListShouldThrowAnExceptionWhenReceivingARepeatingValueWhenAdding() { - Assert.True(uniqueList.IsEmpty()); + uniqueList.AddElement(0, 15); + uniqueList.AddElement(1, 10); + uniqueList.AddElement(2, 15); + Assert.Throws(() => uniqueList.ReturnValueByPosition(2)); } - [TestCaseSource(nameof(ListForTest))] - public void WhenAddedToTheListElementUniqueListShouldNotBeEmpty(UniqueList uniqueList) + [Test] + public void UniqueListShouldThrowAnExceptionWhenReceivingARepeatingValueWhenChangingValue() { - uniqueList.AddElement(1); - Assert.False(uniqueList.IsEmpty()); - } - - [TestCaseSource(nameof(ListForTest))] - public void WhenAddingAnElementToTheUniqueListAndThenDeletingItTheSheetShouldbeEmpty(UniqueList uniqueList) - { - uniqueList.AddElement(1); - int item = 0; - uniqueList.RemoveElement(ref item); - Assert.True(uniqueList.IsEmpty()); - } - - [TestCaseSource(nameof(ListForTest))] - public void TheUniqueListShouldReturnTheCorrectDataAfterDeletion(UniqueList uniqueList) - { - uniqueList.AddElement(1); - uniqueList.AddElement(10); - int item = 0; - uniqueList.RemoveElement(ref item); - Assert.True(item == 10); - } - - [TestCaseSource(nameof(ListForTest))] - public void TheUniqueListShouldCorrectlyReplaceThePositionData(UniqueList uniqueList) - { - uniqueList.AddElement(1); - uniqueList.AddElement(10); - uniqueList.ChangeValueByPosition(1, 15); - int item = 0; - uniqueList.RemoveElement(ref item); - Assert.True(item == 15); - } - - [TestCaseSource(nameof(ListForTest))] - public void AnEmptyUniqueListShouldThrowAnExceptionWhenTryingToDelete(UniqueList uniqueList) - { - int item = 0; - Assert.Throws(() => uniqueList.RemoveElement(ref item)); - } + uniqueList.AddElement(0, 15); + uniqueList.AddElement(1, 10); - [TestCaseSource(nameof(ListForTest))] - public void AnEmptyUniqueListShouldThrowAnExceptionWhenTryingToChangeValueByPosition(UniqueList uniqueList) - { - Assert.Throws(() => uniqueList.ChangeValueByPosition(10, 10)); + Assert.That(uniqueList.ReturnValueByPosition(1), Is.EqualTo(10)); } - - [TestCaseSource(nameof(ListForTest))] - public void AnEmptyUniqueListShouldThrowAnExceptionWhenTryingToChangeCheckValueByPosition(UniqueList uniqueList) - { - Assert.Throws(() => uniqueList.ReturnValueByPosition(10)); - } - - [TestCaseSource(nameof(ListForTest))] - public void TheUniqueListShouldThrowAnExceptionIfThereIsNoPositionInTheListWhenReturningAnItemByPosition(UniqueList uniqueList) - { - uniqueList.AddElement(1); - Assert.Throws(() => uniqueList.ReturnValueByPosition(10)); - } - - [TestCaseSource(nameof(ListForTest))] - public void TheUniqueListShouldReturnTheCorrectItemByPosition(UniqueList uniqueList) - { - uniqueList.AddElement(15); - int item = uniqueList.ReturnValueByPosition(0); - Assert.True(item == 15); - } - - [TestCaseSource(nameof(ListForTest))] - public void UniqueListShouldThrowAnExceptionWhenReceivingARepeatingValueWhenAdding(UniqueList uniqueList) - { - uniqueList.AddElement(15); - uniqueList.AddElement(10); - Assert.Throws(() => uniqueList.AddElement(15)); - } - - [TestCaseSource(nameof(ListForTest))] - public void UniqueListShouldThrowAnExceptionWhenReceivingARepeatingValueWhenChangingValue(UniqueList uniqueList) - { - uniqueList.AddElement(15); - uniqueList.AddElement(10); - Assert.Throws(() => uniqueList.ChangeValueByPosition(1, 15)); - } - - private static IEnumerable ListForTest - => new TestCaseData[] - { - new TestCaseData(new UniqueList()), - }; } \ No newline at end of file From 3037d42295f5b5c04268eb5b17d0605edf5dfbcf Mon Sep 17 00:00:00 2001 From: Artem Date: Fri, 26 May 2023 13:34:56 +0300 Subject: [PATCH 6/6] =?UTF-8?q?=D0=A0=D0=B5=D0=B2=D1=8C=D1=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ListAndUniqueList/ListAndUniqueList/List.cs | 6 +++--- .../ListAndUniqueList/UniqueList.cs | 10 +++++++++- ListAndUniqueList/TestsList/TestsList.cs | 6 +++--- .../TestsUniqueList/TestsUniqueList.cs | 20 +++++++++++++++++-- 4 files changed, 33 insertions(+), 9 deletions(-) diff --git a/ListAndUniqueList/ListAndUniqueList/List.cs b/ListAndUniqueList/ListAndUniqueList/List.cs index d3e5a1a..d8ec533 100644 --- a/ListAndUniqueList/ListAndUniqueList/List.cs +++ b/ListAndUniqueList/ListAndUniqueList/List.cs @@ -108,7 +108,7 @@ virtual public int RemoveElement(int position) int counter = 0; var walker = Head; - while (walker != null && walker.Next.Next != null && counter != position - 1) + while (walker != null && walker.Next != null && walker.Next.Next != null && counter != position - 1) { walker = walker.Next; counter++; @@ -128,7 +128,7 @@ virtual public int RemoveElement(int position) /// virtual public void PrintTheElements() { - ListElement walker = Head; + ListElement? walker = Head; int counter = 0; while (walker != null) { @@ -220,7 +220,7 @@ public ListElement(int value) } public int Value { get; set; } - public ListElement Next { get; set; } + public ListElement? Next { get; set; } public int SizeList { get; set; } } diff --git a/ListAndUniqueList/ListAndUniqueList/UniqueList.cs b/ListAndUniqueList/ListAndUniqueList/UniqueList.cs index 613bee2..5f106f9 100644 --- a/ListAndUniqueList/ListAndUniqueList/UniqueList.cs +++ b/ListAndUniqueList/ListAndUniqueList/UniqueList.cs @@ -11,13 +11,21 @@ public override void AddElement(int position, int value) { base.AddElement(position, value); } + else + { + throw new InvalidItemException(); + } } public override void ChangeValueByPosition(int position, int newValue) { - if (!Contains(newValue)) + if (!Contains(newValue) || ReturnValueByPosition(position) == newValue) { base.ChangeValueByPosition(position, newValue); } + else + { + throw new InvalidItemException(); + } } } diff --git a/ListAndUniqueList/TestsList/TestsList.cs b/ListAndUniqueList/TestsList/TestsList.cs index 9aec4d3..790f5f5 100644 --- a/ListAndUniqueList/TestsList/TestsList.cs +++ b/ListAndUniqueList/TestsList/TestsList.cs @@ -33,7 +33,7 @@ public void TheListShouldReturnTheCorrectDataAfterDeletion(List list) list.AddElement(1, 10); list.AddElement(2, 12); int item = list.RemoveElement(1); - Assert.That(10, Is.EqualTo(item)); + Assert.That(item, Is.EqualTo(10)); } [TestCaseSource(nameof(ListForTest))] @@ -43,7 +43,7 @@ public void TheListShouldCorrectlyReplaceThePositionData(List list) list.AddElement(1, 10); list.ChangeValueByPosition(1, 15); int item = list.RemoveElement(1); - Assert.That(15, Is.EqualTo(item)); + Assert.That(item, Is.EqualTo(15)); } [TestCaseSource(nameof(ListForTest))] @@ -76,7 +76,7 @@ public void TheListShouldReturnTheCorrectItemByPosition(List list) { list.AddElement(0, 15); int item = list.ReturnValueByPosition(0); - Assert.That(15, Is.EqualTo(item)); + Assert.That(item, Is.EqualTo(15)); } private static IEnumerable ListForTest diff --git a/ListAndUniqueList/TestsUniqueList/TestsUniqueList.cs b/ListAndUniqueList/TestsUniqueList/TestsUniqueList.cs index db7c921..b329b10 100644 --- a/ListAndUniqueList/TestsUniqueList/TestsUniqueList.cs +++ b/ListAndUniqueList/TestsUniqueList/TestsUniqueList.cs @@ -17,8 +17,7 @@ public void UniqueListShouldThrowAnExceptionWhenReceivingARepeatingValueWhenAddi { uniqueList.AddElement(0, 15); uniqueList.AddElement(1, 10); - uniqueList.AddElement(2, 15); - Assert.Throws(() => uniqueList.ReturnValueByPosition(2)); + Assert.Throws(() => uniqueList.AddElement(2, 15)); } [Test] @@ -29,4 +28,21 @@ public void UniqueListShouldThrowAnExceptionWhenReceivingARepeatingValueWhenChan Assert.That(uniqueList.ReturnValueByPosition(1), Is.EqualTo(10)); } + + [Test] + public void ChangeValueByPositionWhenValueWithChangeValueShouldWorkCorrectly() + { + uniqueList.AddElement(0, 15); + uniqueList.AddElement(1, 10); + uniqueList.ChangeValueByPosition(1, 10); + } + + [Test] + public void ChangeValueByPositionWhenValueWithChangeValueShouldThrowException() + { + uniqueList.AddElement(0, 15); + uniqueList.AddElement(1, 10); + uniqueList.ChangeValueByPosition(1, 10); + Assert.Throws(() => uniqueList.ChangeValueByPosition(0, 10)); + } } \ No newline at end of file