public void AppendingOwnsBuffer() { var countBefore = _cache.Count; TextWriter writer1 = new SpoolWriter(); writer1.Write("hello"); TextWriter writer2 = new SpoolWriter(); writer2.Write("before"); writer2.Write(writer1); writer2.Write("after"); writer1.Write("world"); Assert.AreEqual("helloworld", writer1.ToString()); Assert.AreEqual("beforehelloafter", writer2.ToString()); var _first = typeof(SpoolWriter).GetField("_first", BindingFlags.NonPublic | BindingFlags.Instance); var _readonly = typeof(SpoolPage).GetField("_readonly", BindingFlags.NonPublic | BindingFlags.Instance); var _nonreusable = typeof(SpoolPage).GetField("_nonreusable", BindingFlags.NonPublic | BindingFlags.Instance); var pages1 = (SpoolPage)_first.GetValue(writer1); var pages2 = (SpoolPage)_first.GetValue(writer2); Assert.IsTrue((bool)_readonly.GetValue(pages1)); Assert.IsTrue((bool)_nonreusable.GetValue(pages1)); Assert.IsFalse((bool)_readonly.GetValue(pages1.Next)); Assert.IsFalse((bool)_nonreusable.GetValue(pages1.Next)); Assert.IsNull(pages1.Next.Next); Assert.IsTrue((bool)_readonly.GetValue(pages2)); Assert.IsFalse((bool)_nonreusable.GetValue(pages2)); Assert.IsTrue((bool)_readonly.GetValue(pages2.Next)); Assert.IsTrue((bool)_nonreusable.GetValue(pages2.Next)); Assert.IsFalse((bool)_readonly.GetValue(pages2.Next.Next)); Assert.IsFalse((bool)_nonreusable.GetValue(pages2.Next.Next)); Assert.IsNull(pages2.Next.Next.Next); var countBeforeCollect = _cache.Count; // ReSharper disable RedundantAssignment writer1 = null; writer2 = null; // ReSharper restore RedundantAssignment GC.Collect(); GC.WaitForPendingFinalizers(); var countAfterCollect = _cache.Count; Assert.AreEqual(0, countBefore); Assert.AreEqual(0, countBeforeCollect); Assert.AreEqual(3, countAfterCollect); }
public void ToStringCombinesResults() { TextWriter writer = new SpoolWriter(); writer.Write("hello"); writer.Write("world"); Assert.AreEqual("helloworld", writer.ToString()); }
public void WritingCharacters() { TextWriter writer = new SpoolWriter(); writer.Write('a'); writer.Write(new[] { 'b', 'c', 'd' }); writer.Write(new[] { 'x', 'e', 'f', 'g', 'x' }, 1, 3); Assert.AreEqual("abcdefg", writer.ToString()); }
public void MultiplePagesCombinedResult() { TextWriter writer = new SpoolWriter(); for (int index = 0; index != 5000; ++index) { writer.Write(index); } Assert.AreEqual(_FiveThousandNumbers, writer.ToString()); }
public void StringWriterToSpoolWriter() { TextWriter writer1 = new StringWriter(); TextWriter writer2 = new SpoolWriter(); for (int index = 0; index != 5000; ++index) { writer1.Write(index); } writer2.Write("before"); writer1.WriteTo(writer2); writer2.Write("after"); Assert.AreEqual("before" + _FiveThousandNumbers + "after", writer2.ToString()); }
public void SpoolWriterOriginatorBuildsMementoWithChanges() { var writer = new SpoolWriter(); var originator = TextWriterOriginator.Create(writer); writer.Write("Alpha"); originator.BeginMemento(); writer.Write("B"); writer.Write("e"); writer.Write("t"); writer.Write("a"); var memento = originator.EndMemento(); writer.Write("Gamma"); var writer2 = new SpoolWriter(); writer2.Write("Delta"); TextWriterOriginator.Create(writer2).DoMemento(memento); writer2.Write("Epsilon"); Assert.That(writer.ToString(), Is.EqualTo("AlphaBetaGamma")); Assert.That(writer2.ToString(), Is.EqualTo("DeltaBetaEpsilon")); }