public void AddToCellTest() { Partition p = new Partition(); p.AddToCell(0, 0); Assert.AreEqual(1, p.Count); Assert.AreEqual(1, p.NumberOfElements()); p.AddToCell(0, 1); Assert.AreEqual(1, p.Count); Assert.AreEqual(2, p.NumberOfElements()); }
/// <summary> /// Parse a string like "[0,2|1,3]" to form the partition; cells are /// separated by '|' characters and elements within the cell by commas. /// </summary> /// <param name="strForm">the partition in string form</param> /// <returns>the partition corresponding to the string</returns> /// <exception cref="ArgumentException">thrown if the provided strFrom is null or empty</exception> public static Partition FromString(string strForm) { if (strForm == null || strForm.Length == 0) { throw new ArgumentException("null or empty string provided"); } Partition p = new Partition(); int index = 0; if (strForm[0] == '[') { index++; } int endIndex; if (strForm[strForm.Length - 1] == ']') { endIndex = strForm.Length - 2; } else { endIndex = strForm.Length - 1; } int currentCell = -1; int numStart = -1; while (index <= endIndex) { char c = strForm[index]; if (char.IsDigit(c)) { if (numStart == -1) { numStart = index; } } else if (c == ',') { int element = int.Parse(strForm.Substring(numStart, index - numStart), NumberFormatInfo.InvariantInfo); if (currentCell == -1) { p.AddCell(element); currentCell = 0; } else { p.AddToCell(currentCell, element); } numStart = -1; } else if (c == '|') { int element = int.Parse(strForm.Substring(numStart, index - numStart), NumberFormatInfo.InvariantInfo); if (currentCell == -1) { p.AddCell(element); currentCell = 0; } else { p.AddToCell(currentCell, element); } currentCell++; p.AddCell(); numStart = -1; } index++; } int lastElement = int.Parse(strForm.Substring(numStart, endIndex + 1 - numStart), NumberFormatInfo.InvariantInfo); p.AddToCell(currentCell, lastElement); return(p); }