From 636d2b21e57ab2807428609ab0c7df97cfd557fe Mon Sep 17 00:00:00 2001 From: Rokas Puzonas Date: Thu, 7 Apr 2022 01:53:24 +0300 Subject: [PATCH] tests: add more test cases for LinkedList --- L3/LD_24/App_Data/Rezultatai.txt | 44 +-- L3/LD_24/Code/InOutUtils.cs | 108 ++++---- L3/LD_24/Code/LinkedList.cs | 180 ++++++++++++- L3/LD_24/Code/Order.cs | 27 +- L3/LD_24/Code/Product.cs | 21 +- L3/LD_24/Forma1.aspx.cs | 10 + L3/LD_24/Web.config | 229 +++++++++++++++- L3/LD_24Tests/IntLinkedList.cs | 17 ++ L3/LD_24Tests/LD_24Tests.csproj | 2 +- L3/LD_24Tests/LinkedListTests.cs | 353 ++++++++++++++++++++++--- L3/LD_24Tests/OrderLinkedListTests.cs | 3 +- L3/LD_24Tests/StringLinkedListTests.cs | 17 ++ 12 files changed, 875 insertions(+), 136 deletions(-) create mode 100644 L3/LD_24Tests/IntLinkedList.cs create mode 100644 L3/LD_24Tests/StringLinkedListTests.cs diff --git a/L3/LD_24/App_Data/Rezultatai.txt b/L3/LD_24/App_Data/Rezultatai.txt index c501991..b8c7c7a 100644 --- a/L3/LD_24/App_Data/Rezultatai.txt +++ b/L3/LD_24/App_Data/Rezultatai.txt @@ -1,45 +1,45 @@ ---------------------------- | Įtaisai | ---------------------------- -| ID | Vardas | Kaina | +| ID | Vardas | Kaina | ---------------------------- -| 0 | Atsuktuvas | 0.99 | -| 1 | Varztas | 0.05 | -| 2 | Laidas | 2.00 | -| 3 | Plaktukas | 2.99 | +| 0 | Atsuktuvas | 0.99 | +| 1 | Varztas | 0.05 | +| 2 | Laidas | 2.00 | +| 3 | Plaktukas | 2.99 | ---------------------------- -------------------------------------------------- | Pirkėjai | -------------------------------------------------- -| Pavardė | Vardas | Įtaisas | Įtaiso kiekis | +| Pavardė | Vardas | Įtaisas | Įtaiso kiekis | -------------------------------------------------- | Petraitis | Petras | 0 | 10 | | Petraitis | Petras | 1 | 10 | -| Jonaitis | Jonas | 1 | 20 | -| Jonaitis | Jonas | 1 | 20 | -| Jonaitis | Jonas | 1 | 20 | -| Onaite | Ona | 2 | 200 | -| Jonaitis | Brolis | 1 | 100 | -| Jonaitis | Brolis | 0 | 100 | -| Jonaitis | Jonas | 1 | 20 | -| Jonaitis | Jonas | 1 | 20 | -| Jonaitis | Jonas | 2 | 20 | -| Onaite | Ona | 0 | 20 | +| Jonaitis | Jonas | 1 | 20 | +| Jonaitis | Jonas | 1 | 20 | +| Jonaitis | Jonas | 1 | 20 | +| Onaite | Ona | 2 | 200 | +| Jonaitis | Brolis | 1 | 100 | +| Jonaitis | Brolis | 0 | 100 | +| Jonaitis | Jonas | 1 | 20 | +| Jonaitis | Jonas | 1 | 20 | +| Jonaitis | Jonas | 2 | 20 | +| Onaite | Ona | 0 | 20 | -------------------------------------------------- ------------------------------------------------------------ | Populiariausi įtaisai | ------------------------------------------------------------ -| ID | Vardas | Įtaisų kiekis, vnt. | Įtaisų kaina, eur. | +| ID | Vardas | Įtaisų kiekis, vnt. | Įtaisų kaina, eur. | ------------------------------------------------------------ -| 2 | Laidas | 220 | 440.00 | +| 2 | Laidas | 220 | 440.00 | ------------------------------------------------------------ ---------------------------------------------------------- | Vienos rūšies pirkėjai | ---------------------------------------------------------- -| Pavardė | Vardas | Įtaiso kiekis, vnt. | Kaina, eur. | +| Pavardė | Vardas | Įtaiso kiekis, vnt. | Kaina, eur. | ---------------------------------------------------------- | Nėra | ---------------------------------------------------------- @@ -47,9 +47,9 @@ ---------------------------------- | Atrinkti įtaisai (n=1, k=1.00) | ---------------------------------- -| ID | Vardas | Kaina | +| ID | Vardas | Kaina | ---------------------------------- -| 0 | Atsuktuvas | 0.99 | -| 1 | Varztas | 0.05 | +| 0 | Atsuktuvas | 0.99 | +| 1 | Varztas | 0.05 | ---------------------------------- diff --git a/L3/LD_24/Code/InOutUtils.cs b/L3/LD_24/Code/InOutUtils.cs index f8bda0a..05a7a9d 100644 --- a/L3/LD_24/Code/InOutUtils.cs +++ b/L3/LD_24/Code/InOutUtils.cs @@ -1,6 +1,7 @@ using System; using System.Collections; using System.Collections.Generic; +using System.Diagnostics; using System.Globalization; using System.IO; using System.Linq; @@ -124,18 +125,59 @@ namespace LD_24.Code /// Target file /// Cell data /// Cell widths - private static void PrintTableRow(StreamWriter writer, List cells, List widths) + private static void PrintTableRow(StreamWriter writer, LinkedList cells, LinkedList widths) { - for (int i = 0; i < widths.Count; i++) + foreach (var tuple in cells.Zip(widths, (a, b) => Tuple.Create(a, b))) { - if (widths[i] > 0) - writer.Write("| {0} ", cells[i].PadRight(widths[i])); + if (tuple.Item1[0] == '-') + writer.Write("| {0} ", tuple.Item1.Substring(1).PadRight(tuple.Item2)); else - writer.Write("| {0} ", cells[i].PadLeft(-widths[i])); + writer.Write("| {0} ", tuple.Item1.PadLeft(tuple.Item2)); } writer.WriteLine("|"); } + private static LinkedList FindTableWidths(LinkedList> rows, string header, string[] columns) + { + var allWidths = new Dictionary>(); + + int o = 0; + foreach (var column in columns) + { + allWidths.Add(o, new LinkedList { column.Length }); + o++; + } + + foreach (var row in rows) + { + int p = 0; + foreach (var cell in row) + { + allWidths[p].Add(cell.Length); + p++; + } + } + + var widths = new LinkedList(); + int totalWidth = 3 * (columns.Length - 1); + foreach (var columnWidths in allWidths.Values) + { + int width = columnWidths.Max(); + totalWidth += width; + widths.Add(width); + } + + // If the header is longer than the body, make the last column wider. + // So the table is a nice rectangle when output to the file + if (header.Length > totalWidth) + { + // Make the last column a bit wider so everything lines up + widths.Add(widths.RemoveLast() + header.Length - totalWidth); + } + + return widths; + } + /// /// Print a table to a file /// @@ -144,58 +186,28 @@ namespace LD_24.Code /// Target list /// Column names /// A IEnumerable for inserting values for each row - private static IEnumerable>> PrintTable(StreamWriter writer, string header, IEnumerable list, params string[] columns) + private static IEnumerable>> PrintTable(StreamWriter writer, string header, IEnumerable list, params string[] columns) { - // 0. Collect all the rows - List> rows = new List>(); + // 1. Collect all the rows + LinkedList> rows = new LinkedList>(); foreach (T item in list) { - List row = new List(); + var row = new LinkedList(); yield return Tuple.Create(item, row); rows.Add(row); } - // 1. Determine the width of each column - List widths = new List(); - int totalWidth = 3*(columns.Length - 1); - for (int i = 0; i < columns.Length; i++) - { - int width = columns[i].Length; - foreach (var row in rows) - { - width = Math.Max(row[i].Length, width); - } - widths.Add(width); - totalWidth += width; - } - - // If the header is longer than the body, make the last column wider. - // So the table is a nice rectangle when output to the file - if (header.Length > totalWidth) - { - widths[widths.Count - 1] += (header.Length - totalWidth); - totalWidth = header.Length; - } - - totalWidth += 2 * 2; - - // 2. Adjust widths to account for aligning - for (int i = 0; i < columns.Length; i++) - { - if (columns[i][0] == '-') - { - widths[i] = -widths[i]; - columns[i] = columns[i].Substring(1); - } - } + // 2. Determine the width of each column + var widths = FindTableWidths(rows, header, columns); + int totalWidth = 3 * (columns.Length - 1) + 2 * 2 + widths.Sum(); // 3. Display the table writer.WriteLine(new string('-', totalWidth)); writer.WriteLine("| {0} |", header.PadRight(totalWidth - 4)); writer.WriteLine(new string('-', totalWidth)); - PrintTableRow(writer, new List(columns), widths); + PrintTableRow(writer, new LinkedList(columns), widths); writer.WriteLine(new string('-', totalWidth)); - if (rows.Count > 0) + if (!rows.IsEmpty()) { foreach (var row in rows) { @@ -221,7 +233,7 @@ namespace LD_24.Code foreach (var tuple in PrintTable(writer, header, orders, "Pavardė", "Vardas", "-Įtaisas", "-Įtaiso kiekis")) { Order order = tuple.Item1; - List row = tuple.Item2; + var row = tuple.Item2; row.Add(order.CustomerSurname); row.Add(order.CustomerName); row.Add(order.ProductID); @@ -241,7 +253,7 @@ namespace LD_24.Code foreach (var tuple in PrintTable(writer, header, orders, "Pavardė", "Vardas", "-Įtaiso kiekis, vnt.", "-Kaina, eur.")) { Order order = tuple.Item1; - List row = tuple.Item2; + var row = tuple.Item2; Product product = TaskUtils.FindByID(products, order.ProductID); row.Add(order.CustomerSurname); @@ -262,7 +274,7 @@ namespace LD_24.Code foreach (var tuple in PrintTable(writer, header, products, "ID", "Vardas", "-Kaina")) { Product product = tuple.Item1; - List row = tuple.Item2; + var row = tuple.Item2; row.Add(product.ID); row.Add(product.Name); row.Add(string.Format("{0:f2}", product.Price)); @@ -281,7 +293,7 @@ namespace LD_24.Code foreach (var tuple in PrintTable(writer, header, popularProducts, "ID", "Vardas", "-Įtaisų kiekis, vnt.", "-Įtaisų kaina, eur.")) { Product product = tuple.Item1; - List row = tuple.Item2; + var row = tuple.Item2; int sales = TaskUtils.CountProductSales(orders, product.ID); row.Add(product.ID); row.Add(product.Name); diff --git a/L3/LD_24/Code/LinkedList.cs b/L3/LD_24/Code/LinkedList.cs index bbe51b4..0ec0b43 100644 --- a/L3/LD_24/Code/LinkedList.cs +++ b/L3/LD_24/Code/LinkedList.cs @@ -6,8 +6,12 @@ using System.Web; namespace LD_24.Code { - public class LinkedList : IEnumerable - where T: IComparable, IEquatable + /// + /// Generic linked list class. Used for storing a list of values. + /// + /// Target type + public class LinkedList : IEnumerable, IComparable>, IEquatable> + where T : IComparable, IEquatable { class Node { @@ -24,9 +28,32 @@ namespace LD_24.Code private Node head; private Node tail; - public void Add(T customer) + /// + /// Creates an empty linked list + /// + public LinkedList() { - Node node = new Node(customer); + } + + /// + /// Creates an clone of a given enumerable value/list + /// + /// Cloned enumerable + public LinkedList(IEnumerable other) + { + foreach (var value in other) + { + Add(value); + } + } + + /// + /// Add a single element to the linked list + /// + /// + public void Add(T value) + { + Node node = new Node(value); if (tail != null && head != null) { tail.Next = node; @@ -39,6 +66,62 @@ namespace LD_24.Code } } + /// + /// Removes the last element from this list + /// + /// The last elements values or the default value if empty + public T RemoveLast() + { + if (head == null) + { + return default; + } + + Node prev = null; + Node current = head; + while (current.Next != null) + { + prev = current; + current = current.Next; + } + + if (prev != null) + { + prev.Next = null; + tail = prev; + } + + if (prev == null) + { + head = null; + tail = null; + } + + return current.Data; + } + + /// + /// Gets the first element + /// + /// The first element + public T GetFirst() + { + return head != null ? head.Data : default; + } + + /// + /// Gets the first element + /// + /// The first element + public T GetLast() + { + return tail != null ? tail.Data : default; + } + + /// + /// Count how many elemnts are stored in this list + /// + /// Number of stored elements public int Count() { int count = 0; @@ -49,11 +132,19 @@ namespace LD_24.Code return count; } + /// + /// Returns true if the linked list is empty, + /// what did you expect? + /// + /// A boolean public bool IsEmpty() { return head == null; } + /// + /// Sorts this linked list + /// public void Sort() { for (Node nodeA = head; nodeA != null; nodeA = nodeA.Next) @@ -73,20 +164,99 @@ namespace LD_24.Code } } + /// + /// A human-readable string for debuging purposes + /// + /// Debug string public override string ToString() { - return String.Format("LinkedList<{0}>{ Count = '{0}' }", Count()); + return string.Format("LinkedList<{0}>( Count = {0} )", Count()); } + /// + /// Enumerate over all the elements of the list + /// + /// An enumerator public IEnumerator GetEnumerator() { for (Node d = head; d != null; d = d.Next) yield return d.Data; } + /// + /// Enumerate over all the elements of the list + /// + /// An enumerator IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } + + /// + /// Compares the ordering 2 lists + /// + /// Other list + /// + public int CompareTo(LinkedList other) + { + var otherEnumerator = other.GetEnumerator(); + foreach (var value in this) + { + if (otherEnumerator.MoveNext()) + { + int comparison = value.CompareTo(otherEnumerator.Current); + if (comparison != 0) + { + return comparison; + } + } else + { + return -1; + } + } + + if (otherEnumerator.MoveNext()) + { + return 1; + } + + return 0; + } + + /// + /// Checks if 2 linked lists have the same values stored + /// + /// Other linked list + /// True if they both store the same values + public bool Equals(LinkedList other) + { + var otherEnumerator = other.GetEnumerator(); + foreach (var value in this) + { + if (otherEnumerator.MoveNext()) + { + if (!value.Equals(otherEnumerator.Current)) + { + // This is the case, when some elements in between the lists are different + return false; + } + } + else + { + // This is the case when the other linked list runs out of values, while this still has more. + return false; + } + } + + if (otherEnumerator.MoveNext()) + { + // This is the case, when the other linked list still had some left over values. + // That means it had more elements that this one. + return false; + } + + // This is the case, when everything is the same. + return true; + } } } \ No newline at end of file diff --git a/L3/LD_24/Code/Order.cs b/L3/LD_24/Code/Order.cs index a99f300..ff91470 100644 --- a/L3/LD_24/Code/Order.cs +++ b/L3/LD_24/Code/Order.cs @@ -27,6 +27,13 @@ namespace LD_24.Code /// public int ProductAmount { get; set; } + /// + /// Creates a new order + /// + /// The customers surname + /// The customers name + /// Product ID + /// Product amount public Order(string customerSurname, string customerName, string productID, int productAmount) { CustomerSurname = customerSurname; @@ -35,11 +42,11 @@ namespace LD_24.Code ProductAmount = productAmount; } - public override string ToString() - { - return String.Format("Order{Name = '{0}'}", CustomerName); - } - + /// + /// Compares the sorting order of this and other. + /// + /// + /// public int CompareTo(Order other) { if (ProductAmount > other.ProductAmount) @@ -62,6 +69,11 @@ namespace LD_24.Code return Equals(other) ? 0 : -1; } + /// + /// Compare if this order has the same sorting order as another one. + /// + /// + /// public bool Equals(Order other) { return CustomerSurname == other.CustomerSurname && @@ -79,5 +91,10 @@ namespace LD_24.Code hashCode = hashCode * -1521134295 + ProductAmount.GetHashCode(); return hashCode; } + + public override string ToString() + { + return String.Format("Order{Name = '{0}'}", CustomerName); + } } } \ No newline at end of file diff --git a/L3/LD_24/Code/Product.cs b/L3/LD_24/Code/Product.cs index de7a900..9a83856 100644 --- a/L3/LD_24/Code/Product.cs +++ b/L3/LD_24/Code/Product.cs @@ -23,6 +23,12 @@ namespace LD_24.Code /// public decimal Price { get; set; } + /// + /// Creates a new product + /// + /// ID + /// Name + /// Price public Product(string iD, string name, decimal price) { ID = iD; @@ -30,11 +36,11 @@ namespace LD_24.Code Price = price; } - public override string ToString() - { - return String.Format("Product{ID = '{0}'}", ID); - } - + /// + /// Check if this has the same sorting order as othe product + /// + /// + /// public bool Equals(Product other) { return ID == other.ID && @@ -55,5 +61,10 @@ namespace LD_24.Code { return ID.CompareTo(other.ID); } + + public override string ToString() + { + return String.Format("Product{ID = '{0}'}", ID); + } } } diff --git a/L3/LD_24/Forma1.aspx.cs b/L3/LD_24/Forma1.aspx.cs index dc973c5..398d400 100644 --- a/L3/LD_24/Forma1.aspx.cs +++ b/L3/LD_24/Forma1.aspx.cs @@ -17,11 +17,21 @@ namespace LD_24 { private const string outputFilename = "App_Data/Rezultatai.txt"; + /// + /// Function that runs when the page loads + /// + /// + /// protected void Page_Load(object sender, EventArgs e) { FindControl("ResultsDiv").Visible = false; } + /// + /// Functions that rusn when the button is clicked + /// + /// + /// protected void Button1_Click(object sender, EventArgs e) { int n = int.Parse(TextBox1.Text); diff --git a/L3/LD_24/Web.config b/L3/LD_24/Web.config index 6b31877..93be01c 100644 --- a/L3/LD_24/Web.config +++ b/L3/LD_24/Web.config @@ -1,27 +1,238 @@ - - + - + - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/L3/LD_24Tests/IntLinkedList.cs b/L3/LD_24Tests/IntLinkedList.cs new file mode 100644 index 0000000..7898269 --- /dev/null +++ b/L3/LD_24Tests/IntLinkedList.cs @@ -0,0 +1,17 @@ +using System; +using System.Text; +using LD_24.Code; +using Bogus; + +namespace LD_24Tests +{ + public class IntLinkedListTests : LinkedListTests + { + private readonly Faker faker = new Faker("en"); + + protected override int CreateItem() + { + return faker.Random.Number(100); + } + } +} diff --git a/L3/LD_24Tests/LD_24Tests.csproj b/L3/LD_24Tests/LD_24Tests.csproj index 5ac2fb0..b831153 100644 --- a/L3/LD_24Tests/LD_24Tests.csproj +++ b/L3/LD_24Tests/LD_24Tests.csproj @@ -8,7 +8,7 @@ - + diff --git a/L3/LD_24Tests/LinkedListTests.cs b/L3/LD_24Tests/LinkedListTests.cs index 5574f5d..ae37504 100644 --- a/L3/LD_24Tests/LinkedListTests.cs +++ b/L3/LD_24Tests/LinkedListTests.cs @@ -3,71 +3,346 @@ using Xunit; using FluentAssertions; using LD_24.Code; using System.Collections; +using System.Linq; namespace LD_24Tests { public abstract class LinkedListTests where T : IComparable, IEquatable { - protected abstract LinkedList CreateNewList(); protected abstract T CreateItem(); - [Fact] - public void New_List_Is_Empty() + private LinkedList CreateLoadedList(int count) { - LinkedList list = CreateNewList(); - - list.IsEmpty().Should().BeTrue(); - } - - [Fact] - public void Assert_List_Is_Not_Empty() - { - LinkedList list = CreateNewList(); - - list.Add(CreateItem()); - - list.IsEmpty().Should().BeFalse(); - } - - [Theory] - [InlineData(0)] - [InlineData(1)] - [InlineData(5)] - public void Retrieve_How_Many_Were_Added(int count) - { - LinkedList list = CreateNewList(); + var list = new LinkedList(); for (int i = 0; i < count; i++) { list.Add(CreateItem()); } + return list; + } + + + [Fact] + [Trait("TestedMethod", "Constructor")] + public void CreatingList_Shouldnt_Throw() + { + Action action = () => new LinkedList(); + + action.Should().NotThrow(); + } + + [Theory] + [Trait("TestedMethod", "Constructor")] + [InlineData(0)] + [InlineData(1)] + [InlineData(5)] + public void CreateList_From_Other_List(int count) + { + var list = CreateLoadedList(count); + var newList = new LinkedList(list); + + list.Equals(newList).Should().BeTrue(); + } + + [Theory] + [Trait("TestedMethod", "Add")] + [InlineData(0)] + [InlineData(1)] + [InlineData(5)] + public void Adding_Should_Not_Throw(int count) + { + LinkedList list = CreateLoadedList(count); + Action task = () => list.Add(CreateItem()); + + task.Should().NotThrow(); + } + + + [Fact] + [Trait("TestedMethod", "IsEmpty")] + public void New_List_Is_Empty() + { + LinkedList list = new LinkedList(); + + list.IsEmpty().Should().BeTrue(); + } + + [Fact] + [Trait("TestedMethod", "IsEmpty")] + public void Assert_List_Is_Not_Empty() + { + LinkedList list = new LinkedList(); + list.Add(CreateItem()); + + list.IsEmpty().Should().BeFalse(); + } + + [Theory] + [Trait("TestedMethod", "IsEmpty")] + [InlineData(0)] + [InlineData(1)] + [InlineData(5)] + public void Is_Empty_Should_Not_Throw(int count) + { + LinkedList list = CreateLoadedList(count); + Action task = () => list.Add(CreateItem()); + + task.Should().NotThrow(); + } + + [Theory] + [Trait("TestedMethod", "Count")] + [InlineData(0)] + [InlineData(1)] + [InlineData(5)] + public void Retrieve_How_Many_Were_Added(int count) + { + LinkedList list = CreateLoadedList(count); + list.Count().Should().Be(count); } [Fact] - public void IEnumerable_Is_Implemented_Correctly() + [Trait("TestedMethod", "GetLast")] + public void Assert_Last_Element_When_Empty_Doesnt_Throw() { - LinkedList list = CreateNewList(); + LinkedList list = new LinkedList(); + Func task = () => list.GetLast(); - var item1 = CreateItem(); - var item2 = CreateItem(); - var item3 = CreateItem(); + task.Should().NotThrow(); + } - list.Add(item1); - list.Add(item2); - list.Add(item3); + [Theory] + [Trait("TestedMethod", "GetLast")] + [InlineData(0)] + [InlineData(1)] + [InlineData(5)] + public void Assert_Last_Element_Value(int count) + { + var list = CreateLoadedList(count); + var last = CreateItem(); + list.Add(last); - var newList = new System.Collections.Generic.List(); - foreach (var item in list) + list.GetLast().Should().Be(last); + } + + [Fact] + [Trait("TestedMethod", "GetFirst")] + public void Assert_First_Element_When_Empty() + { + LinkedList list = new LinkedList(); + + list.GetFirst().Should().Be(default); + } + + [Fact] + [Trait("TestedMethod", "GetFirst")] + public void Assert_First_Element_When_Empty_Doesnt_Throw() + { + LinkedList list = new LinkedList(); + Func task = () => list.GetFirst(); + + task.Should().NotThrow(); + } + + [Theory] + [Trait("TestedMethod", "GetFirst")] + [InlineData(0)] + [InlineData(1)] + [InlineData(5)] + public void Assert_First_Element_Value(int count) + { + var list = new LinkedList(); + var first = CreateItem(); + list.Add(first); + + for (int i = 0; i < count; i++) + list.Add(CreateItem()); + + list.GetFirst().Should().Be(first); + } + + [Theory] + [Trait("TestedMethod", "RemoveLast")] + [InlineData(0)] + [InlineData(1)] + [InlineData(5)] + public void RemoveLast_Shouldnt_Throw(int count) + { + var list = CreateLoadedList(count); + Action action = () => list.RemoveLast(); + + action.Should().NotThrow(); + } + + [Theory] + [Trait("TestedMethod", "RemoveLast")] + [InlineData(1)] + [InlineData(5)] + public void RemoveLast_Returns_Removed_Value(int count) + { + var list = CreateLoadedList(count); + var last = list.GetLast(); + + list.RemoveLast().Should().Be(last); + } + + [Theory] + [Trait("TestedMethod", "RemoveLast")] + [InlineData(1)] + [InlineData(5)] + public void After_RemoveLast_Length_Should_Decrease(int count) + { + var list = CreateLoadedList(count); + + list.RemoveLast(); + list.Count().Should().Be(count-1); + } + + [Fact] + [Trait("TestedMethod", "RemoveLast")] + public void RemoveLast_Does_Nothing_In_Empty_List() + { + var list = new LinkedList(); + + list.RemoveLast(); + list.Count().Should().Be(0); + } + + [Fact] + [Trait("TestedMethod", "RemoveLast")] + public void RemoveLast_Returns_Default_In_Empty_List() + { + var list = new LinkedList(); + + list.RemoveLast().Should().Be(default); + } + + [Theory] + [Trait("TestedMethod", "IEnumerable")] + [InlineData(0)] + [InlineData(1)] + [InlineData(5)] + public void IEnumerable_Is_Implemented_Correctly(int count) + { + var genericList = new System.Collections.Generic.List(); + LinkedList list = new LinkedList(); + + for (int i = 0; i < count; i++) { - newList.Add(item); + var item = CreateItem(); + list.Add(item); + genericList.Add(item); } - newList[0].Should().Be(item1); - newList[1].Should().Be(item2); - newList[2].Should().Be(item3); + // Checks the enumerators "list" and "genericList" contain the same values + genericList.Zip(list, (a, b) => a.Equals(b)).All((t) => t).Should(); + } + + [Fact] + [Trait("TestedMethod", "ToString")] + public void Assert_ToString_Is_Overridden() + { + var list = new LinkedList(); + Assert.True(list.ToString() != list.GetType().ToString()); + } + + [Theory] + [Trait("TestedMethod", "Equals")] + [InlineData(0)] + [InlineData(1)] + [InlineData(5)] + public void Check_If_Lists_Are_Equal(int count) + { + var list1 = CreateLoadedList(count); + var list2 = new LinkedList(list1); + + list1.Equals(list2).Should().BeTrue(); + } + + [Theory] + [Trait("TestedMethod", "Equals")] + [InlineData(1)] + [InlineData(5)] + public void Check_If_Lists_Arent_Equal(int count) + { + var list1 = CreateLoadedList(count); + var list2 = CreateLoadedList(count); + + list1.Equals(list2).Should().BeFalse(); + } + + [Theory] + [Trait("TestedMethod", "Equals")] + [InlineData(1)] + [InlineData(5)] + public void Check_If_Lists_Arent_Equal_By_Size(int count) + { + var list1 = CreateLoadedList(count); + var list2 = new LinkedList(list1); + list2.RemoveLast(); + + list1.Equals(list2).Should().BeFalse(); + } + + [Theory] + [Trait("TestedMethod", "Sort")] + [InlineData(0)] + [InlineData(1)] + [InlineData(5)] + public void Sort_Shouldnt_Throw(int count) + { + var list = CreateLoadedList(count); + Action action = () => list.Sort(); + + action.Should().NotThrow(); + } + + [Fact] + [Trait("TestedMethod", "Sort")] + public void Sort_Does_Nothing_On_Empty_List() + { + var list = new LinkedList(); + var listCopy = new LinkedList(list); + list.Sort(); + + list.Equals(listCopy).Should().BeTrue(); + } + + [Theory] + [Trait("TestedMethod", "Sort")] + [InlineData(0)] + [InlineData(1)] + [InlineData(5)] + public void Items_In_Correct_Order_After_Sort(int count) + { + var list = CreateLoadedList(count); + list.Sort(); + + var itemPairs = list.Zip(list.Skip(1), (a, b) => Tuple.Create(a, b)); + foreach (var tuple in itemPairs) + { + Assert.True(tuple.Item1.CompareTo(tuple.Item2) >= 0); + } + } + + [Theory] + [Trait("TestedMethod", "Sort")] + [InlineData(0)] + [InlineData(1)] + [InlineData(5)] + public void Does_Nothing_If_Already_Sorted(int count) + { + var list = CreateLoadedList(count); + list.Sort(); + + var listCopy = new LinkedList(list); + list.Sort(); + + list.Equals(listCopy).Should().BeTrue(); } } } diff --git a/L3/LD_24Tests/OrderLinkedListTests.cs b/L3/LD_24Tests/OrderLinkedListTests.cs index d816569..4b6154c 100644 --- a/L3/LD_24Tests/OrderLinkedListTests.cs +++ b/L3/LD_24Tests/OrderLinkedListTests.cs @@ -7,9 +7,8 @@ namespace LD_24Tests { public class OrderLinkedListTests : LinkedListTests { - private Faker faker = new Faker("en"); + private readonly Faker faker = new Faker("en"); - protected override LinkedList CreateNewList() => new LinkedList(); protected override Order CreateItem() { string customerSurname = faker.Name.FindName(); diff --git a/L3/LD_24Tests/StringLinkedListTests.cs b/L3/LD_24Tests/StringLinkedListTests.cs new file mode 100644 index 0000000..d0553de --- /dev/null +++ b/L3/LD_24Tests/StringLinkedListTests.cs @@ -0,0 +1,17 @@ +using System; +using System.Text; +using Bogus; +using LD_24.Code; + +namespace LD_24Tests +{ + public class StringLinkedListTests : LinkedListTests + { + private readonly Faker faker = new Faker("en"); + + protected override string CreateItem() + { + return faker.Name.FullName(); + } + } +}