public void ComplexOperationTests() { var gapBuffer = new GapBuffer(); const string lorem = "Lorem"; gapBuffer.Insert(' ', 0); foreach (var character in lorem) { gapBuffer.Insert(character, gapBuffer.GetLength()); } var actualResult = gapBuffer.GetText(0, gapBuffer.GetLength() - 1); Assert.AreEqual(" Lorem", actualResult, "The returned text differs."); foreach (var character in lorem) { gapBuffer.Insert(character, 0); } actualResult = gapBuffer.GetText(0, gapBuffer.GetLength() - 1); Assert.AreEqual("meroL Lorem", actualResult, "The returned text differs."); gapBuffer.Delete(3, 3); actualResult = gapBuffer.GetText(0, gapBuffer.GetLength() - 1); Assert.AreEqual("merL Lorem", actualResult, "The returned text differs."); gapBuffer.Delete(0, gapBuffer.GetLength() - 1); actualResult = gapBuffer.GetText(0, 0); Assert.AreEqual(default, actualResult, "The returned text differs.");
public void TestSearchBackward() { GapBuffer buffer = new GapBuffer(); for (int i = 0; i < 100; i++) { buffer.Insert(0, "a"); } var idx = new List <int> (new [] { 0, buffer.Length / 2, buffer.Length }); idx.ForEach(i => buffer.Insert(i, "test")); // move gap to the beginning buffer.Replace(idx[0], 1, buffer.GetCharAt(idx[0]).ToString()); List <int> results = new List <int> (buffer.SearchBackward("test", buffer.Length)); Assert.AreEqual(idx.Count, results.Count, "matches != " + idx.Count + " - found:" + results.Count); for (int i = 0; i < idx.Count; i++) { Assert.AreEqual(idx[idx.Count - 1 - i], results[i], (i + 1) + ". match != " + idx[idx.Count - 1 - i] + " was " + results[i]); } // move gap to the middle buffer.Replace(idx[1], 1, buffer.GetCharAt(idx[1]).ToString()); results = new List <int> (buffer.SearchBackward("test", buffer.Length)); Assert.AreEqual(idx.Count, results.Count, "matches != " + idx.Count + " - found:" + results.Count); for (int i = 0; i < idx.Count; i++) { Assert.AreEqual(idx[idx.Count - 1 - i], results[i], (i + 1) + ". match != " + idx[idx.Count - 1 - i] + " was " + results[i]); } // move gap to the end buffer.Replace(idx[2], 1, buffer.GetCharAt(idx[2]).ToString()); results = new List <int> (buffer.SearchBackward("test", buffer.Length)); Assert.AreEqual(idx.Count, results.Count, "matches != " + idx.Count + " - found:" + results.Count); for (int i = 0; i < idx.Count; i++) { Assert.AreEqual(idx[idx.Count - 1 - i], results[i], (i + 1) + ". match != " + idx[idx.Count - 1 - i] + " was " + results[i]); } // move gap to the end buffer.Replace(buffer.Length - 1, 1, buffer.GetCharAt(buffer.Length - 1).ToString()); results = new List <int> (buffer.SearchBackward("test", buffer.Length)); Assert.AreEqual(idx.Count, results.Count, "matches != " + idx.Count + " - found:" + results.Count); for (int i = 0; i < idx.Count; i++) { Assert.AreEqual(idx[idx.Count - 1 - i], results[i], (i + 1) + ". match != " + idx[idx.Count - 1 - i] + " was " + results[i]); } }
public void EnlargeTest() { var gb = new GapBuffer <int>(2); gb.Insert(0, 3); gb.Insert(1, 4); gb.Insert(0, 1); gb.Insert(3, 5); gb.Insert(1, 2); Assert.Equal(5, gb.Count); Assert.Equal(1, gb[0]); Assert.Equal(2, gb[1]); Assert.Equal(3, gb[2]); Assert.Equal(4, gb[3]); Assert.Equal(5, gb[4]); }
public void InsertTest() { var gb = new GapBuffer <int>(4); gb.Insert(0, 1); Assert.Equal(1, gb.Count); Assert.Equal(1, gb[0]); }
public void TestSearchForward () { GapBuffer buffer = new GapBuffer (); for (int i = 0; i < 100; i++) { buffer.Insert (0, "a"); } var idx = new List<int> (new [] { 0, buffer.Length / 2, buffer.Length }); idx.ForEach (i => buffer.Insert (i, "test")); // move gap to the beginning buffer.Replace (idx[0], 1, buffer.GetCharAt (idx[0]).ToString ()); List<int> results = new List<int> (buffer.SearchForward ("test", 0)); Assert.AreEqual (idx.Count, results.Count, "matches != " + idx.Count + " - found:" + results.Count); for (int i = 0; i < idx.Count; i++) Assert.AreEqual (idx[i], results[i], (i + 1) +". match != " + idx[i] + " was " + results[i]); // move gap to the middle buffer.Replace (idx[1], 1, buffer.GetCharAt (idx[1]).ToString ()); results = new List<int> (buffer.SearchForward ("test", 0)); Assert.AreEqual (idx.Count, results.Count, "matches != " + idx.Count + " - found:" + results.Count); for (int i = 0; i < idx.Count; i++) Assert.AreEqual (idx[i], results[i], (i + 1) +". match != " + idx[i] + " was " + results[i]); // move gap to the end buffer.Replace (idx[2], 1, buffer.GetCharAt (idx[2]).ToString ()); results = new List<int> (buffer.SearchForward ("test", 0)); Assert.AreEqual (idx.Count, results.Count, "matches != " + idx.Count + " - found:" + results.Count); for (int i = 0; i < idx.Count; i++) Assert.AreEqual (idx[i], results[i], (i + 1) +". match != " + idx[i] + " was " + results[i]); // move gap to the end buffer.Replace (buffer.Length - 1, 1, buffer.GetCharAt (buffer.Length - 1).ToString ()); results = new List<int> (buffer.SearchForward ("test", 0)); Assert.AreEqual (idx.Count, results.Count, "matches != " + idx.Count + " - found:" + results.Count); for (int i = 0; i < idx.Count; i++) Assert.AreEqual (idx[i], results[i], (i + 1) +". match != " + idx[i] + " was " + results[i]); }
private void Fill(GapBuffer <int> gb, List <int> l, int count) { var rng = new Random(0); for (int i = 0; i < count; i++) { var n = rng.Next(i); l.Insert(n, i); gb.Insert(n, i); } }
private static GapBuffer <int> CreateTestGapBuffer() { GapBuffer <int> buf = new GapBuffer <int>(); for (int i = 100; i < 125; i++) { buf.Add(i); } buf.Insert(16, 999); return(buf); }
public void InsertStringTest() { // Arrange. var gapBuffer = new GapBuffer(); var expectedResult = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Suspendisse nisl."; // Act. gapBuffer.Insert(expectedResult, 0); var actualResult = gapBuffer.GetText(0, gapBuffer.GetLength() - 1); // Assert. Assert.AreEqual(expectedResult, actualResult, "The buffer storage content differs."); }
public void GetLengthInsertTest() { // Arrange. var gapBuffer = new GapBuffer(); var expectedResult = 5; // Act. gapBuffer.Insert("Lorem", 0); var actualResult = gapBuffer.GetLength(); // Assert. Assert.AreEqual(expectedResult, actualResult, "The buffer storage length differs."); }
public void GetTextMiddleStringTest() { // Arrange. var gapBuffer = new GapBuffer(); var expectedResult = "ipsum"; gapBuffer.Insert("Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Suspendisse nisl.", 0); // Act. var actualResult = gapBuffer.GetText(6, 10); // Assert. Assert.AreEqual(expectedResult, actualResult, "The returned text differs."); }
public void RemoveTest2() { var gb = new GapBuffer <int>(4); gb.InsertRange(0, new int[] { 1, 2, 3, 4, 6, 7, 8, 9 }, 0, 8); gb.Insert(4, 5); gb.RemoveRange(3, 4); Assert.Equal(5, gb.Count); Assert.Equal(1, gb[0]); Assert.Equal(2, gb[1]); Assert.Equal(3, gb[2]); Assert.Equal(8, gb[3]); Assert.Equal(9, gb[4]); }
public void InsertCharTest() { // Arrange. var gapBuffer = new GapBuffer(); var expectedResult = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Suspendisse nisl."; // Act. var index = 0; foreach (var character in expectedResult) { gapBuffer.Insert(character, index); index++; } var actualResult = gapBuffer.GetText(0, gapBuffer.GetLength() - 1); // Assert. Assert.AreEqual(expectedResult, actualResult, "The buffer storage content differs."); }
public void TestSearchBackward() { GapBuffer buffer = new GapBuffer (); for (int i = 0; i < 100; i++) { buffer.Insert (0, "a"); } var idx = new List<int> (new [] { 0, buffer.TextLength / 2, buffer.TextLength }); idx.ForEach (i => buffer.Insert (i, "test")); // move gap to the beginning buffer.Replace (idx [0], 1, buffer.GetCharAt (idx [0]).ToString ()); List<int> results = new List<int> (); int o = buffer.TextLength; while (o > 0 && (o = buffer.LastIndexOf ("test", o - 1, o, StringComparison.Ordinal)) != -1) { results.Add (o); } Assert.AreEqual (idx.Count, results.Count, "matches != " + idx.Count + " - found:" + results.Count); for (int i = 0; i < idx.Count; i++) Assert.AreEqual (idx [idx.Count - 1 - i], results [i], (i + 1) + ". match != " + idx [idx.Count - 1 - i] + " was " + results [i]); // move gap to the middle buffer.Replace (idx [1], 1, buffer.GetCharAt (idx [1]).ToString ()); results = new List<int> (); o = buffer.TextLength - 1; while (o > 0 && (o = buffer.LastIndexOf ("test", o - 1, o, StringComparison.Ordinal)) != -1) { results.Add (o); } Assert.AreEqual (idx.Count, results.Count, "matches != " + idx.Count + " - found:" + results.Count); for (int i = 0; i < idx.Count; i++) Assert.AreEqual (idx [idx.Count - 1 - i], results [i], (i + 1) + ". match != " + idx [idx.Count - 1 - i] + " was " + results [i]); // move gap to the end buffer.Replace (idx [2], 1, buffer.GetCharAt (idx [2]).ToString ()); results = new List<int> (); o = buffer.TextLength - 1; while (o > 0 && (o = buffer.LastIndexOf ("test", o - 1, o, StringComparison.Ordinal)) != -1) { results.Add (o); } Assert.AreEqual (idx.Count, results.Count, "matches != " + idx.Count + " - found:" + results.Count); for (int i = 0; i < idx.Count; i++) Assert.AreEqual (idx [idx.Count - 1 - i], results [i], (i + 1) + ". match != " + idx [idx.Count - 1 - i] + " was " + results [i]); // move gap to the end buffer.Replace (buffer.TextLength - 1, 1, buffer.GetCharAt (buffer.TextLength - 1).ToString ()); results = new List<int> (); o = buffer.TextLength - 1; while (o > 0 && (o = buffer.LastIndexOf ("test", o - 1, o, StringComparison.Ordinal)) != -1) { results.Add (o); } Assert.AreEqual (idx.Count, results.Count, "matches != " + idx.Count + " - found:" + results.Count); for (int i = 0; i < idx.Count; i++) Assert.AreEqual (idx[idx.Count - 1 - i], results[i], (i + 1) +". match != " + idx[idx.Count - 1 - i] + " was " + results[i]); }
public void TestSearchForward() { GapBuffer buffer = new GapBuffer(); for (int i = 0; i < 100; i++) { buffer.Insert(0, "a"); } var idx = new List <int> (new [] { 0, buffer.TextLength / 2, buffer.TextLength }); idx.ForEach(i => buffer.Insert(i, "test")); // move gap to the beginning buffer.Replace(idx [0], 1, buffer.GetCharAt(idx [0]).ToString()); List <int> results = new List <int> (); int o = 0; while ((o = buffer.IndexOf("test", o, buffer.TextLength - o, StringComparison.Ordinal)) >= 0) { results.Add(o); o++; } Assert.AreEqual(idx.Count, results.Count, "matches != " + idx.Count + " - found:" + results.Count); for (int i = 0; i < idx.Count; i++) { Assert.AreEqual(idx [i], results [i], (i + 1) + ". match != " + idx [i] + " was " + results [i]); } // move gap to the middle buffer.Replace(idx [1], 1, buffer.GetCharAt(idx [1]).ToString()); results = new List <int> (); o = 0; while ((o = buffer.IndexOf("test", o, buffer.TextLength - o, StringComparison.Ordinal)) >= 0) { results.Add(o); o++; } Assert.AreEqual(idx.Count, results.Count, "matches != " + idx.Count + " - found:" + results.Count); for (int i = 0; i < idx.Count; i++) { Assert.AreEqual(idx [i], results [i], (i + 1) + ". match != " + idx [i] + " was " + results [i]); } // move gap to the end buffer.Replace(idx [2], 1, buffer.GetCharAt(idx [2]).ToString()); results = new List <int> (); o = 0; while ((o = buffer.IndexOf("test", o, buffer.TextLength - o, StringComparison.Ordinal)) >= 0) { results.Add(o); o++; } Assert.AreEqual(idx.Count, results.Count, "matches != " + idx.Count + " - found:" + results.Count); for (int i = 0; i < idx.Count; i++) { Assert.AreEqual(idx [i], results [i], (i + 1) + ". match != " + idx [i] + " was " + results [i]); } // move gap to the end buffer.Replace(buffer.TextLength - 1, 1, buffer.GetCharAt(buffer.TextLength - 1).ToString()); results = new List <int> (); o = 0; while ((o = buffer.IndexOf("test", o, buffer.TextLength - o, StringComparison.Ordinal)) >= 0) { results.Add(o); o++; } Assert.AreEqual(idx.Count, results.Count, "matches != " + idx.Count + " - found:" + results.Count); for (int i = 0; i < idx.Count; i++) { Assert.AreEqual(idx[i], results[i], (i + 1) + ". match != " + idx[i] + " was " + results[i]); } }
private void Manager_OnTextInput(object sender, TextInputEventArgs e) { bool NewLine = false; //Was a new line created? if (m_HasFocus) { if (m_Mode != TextEditMode.ReadOnly) { if (m_NumLines > 1) { Vector2 MeasuredString = m_Font.MeasureString(GetCurrentLine()); //Check that text doesn't go beyond width of control... if (((Position.X + MeasuredString.X) >= m_Size.X) && !m_RemovingTxt && !m_MovingCursor) { if (m_TextPosition.Y <= Position.Y + ((m_NumLines - 2) * m_Font.LineSpacing)) { m_TextPosition.Y += m_Font.LineSpacing; RenderableText2 RenderTxt = new RenderableText2(); RenderTxt.Position = m_TextPosition; RenderTxt.Text = GetCurrentLine(); RenderTxt.Visible = true; m_Lines.Add(RenderTxt); m_CurrentLine.Clear(); m_Cursor.Position.Y += m_Font.LineSpacing; m_Cursor.LineIndex++; m_Cursor.CharacterIndex = 0; m_Cursor.Position.X = Position.X; NewLine = true; } else //Text went beyond the borders of the control... { RenderableText2 RenderTxt = new RenderableText2(); RenderTxt.Position = m_TextPosition; RenderTxt.Text = GetCurrentLine(); RenderTxt.Visible = true; m_Lines.Add(RenderTxt); m_CurrentLine.Clear(); m_ScrollbarHeight -= m_Font.LineSpacing; //TODO: Resize scrollbar... m_Cursor.LineIndex++; m_Cursor.CharacterIndex = 0; m_Cursor.Position.X = Position.X; NewLine = true; } } } else { //Text went beyond the borders of the control... if (m_Font.MeasureString(Text).X >= (m_Size.X - m_Font.MeasureString(e.Character.ToString()).X) && !m_RemovingTxt) { m_Cursor.Position.X = m_Size.X; m_Renderer.ScrollTextLeft(); } } if (!m_IsUpperCase) { //If the cursor is in the middle of a line, replace the character. if (m_Cursor.CharacterIndex < GetCurrentLine().Length) { m_CurrentLine.RemoveAt(m_Cursor.CharacterIndex); m_Renderer.RemoveAt(m_Cursor.CharacterIndex); m_CurrentLine.Insert(m_Cursor.CharacterIndex, e.Character.ToString()); m_Renderer.Insert(m_Cursor.CharacterIndex, e.Character.ToString()); } else { m_CurrentLine.Add(e.Character.ToString()); m_Renderer.Insert(m_Cursor.CharacterIndex, e.Character.ToString()); } } else { //If the cursor is in the middle of a line, replace the character. if (m_Cursor.CharacterIndex < GetCurrentLine().Length) { m_CurrentLine.RemoveAt(m_Cursor.CharacterIndex); m_Renderer.RemoveAt(m_Cursor.CharacterIndex); m_CurrentLine.Insert(m_Cursor.CharacterIndex, e.Character.ToString().ToUpper()); m_Renderer.Insert(m_Cursor.CharacterIndex, e.Character.ToString().ToUpper()); } else { m_CurrentLine.Add(e.Character.ToString().ToUpper()); m_Renderer.Insert(m_Cursor.CharacterIndex, e.Character.ToString()); } } } if (!NewLine) { m_Cursor.CharacterIndex++; } m_RemovingTxt = false; m_MovingCursor = false; m_Cursor.Position.X += m_Font.MeasureString(e.Character.ToString()).X; } }