コード例 #1
0
        public void TestThreadSafetyNestedLayoutLongLine()
        {
            using (StringWriter stringWriter = new StringWriter())
                using (FormatTextWriter formatTextWriter = new FormatTextWriter(stringWriter))
                {
                    Stopwatch watch = Stopwatch.StartNew();
                    Parallel.For(
                        0,
                        1000,
                        new ParallelOptions {
                        MaxDegreeOfParallelism = 8
                    },
                        i => new FormatBuilder()
                        .Append(FormatResources.ButIMustExplain)
                        .WriteTo(formatTextWriter));
                    watch.Stop();
                    Trace.WriteLine(watch.Elapsed.TotalMilliseconds);
                    Assert.AreEqual(970000, formatTextWriter.Position);
                    string result = stringWriter.ToString();

                    string[] lines = result
                                     .Split(new[] { Environment.NewLine }, StringSplitOptions.None);

                    // Check number of lines and maximum line length, if we have any race conditions we expect these to change.
                    Assert.AreEqual(1, lines.Length);
                }
        }
コード例 #2
0
        public void TestThreadSafety()
        {
            const ushort width = 80;

            using (StringWriter stringWriter = new StringWriter())
                using (FormatTextWriter formatTextWriter = new FormatTextWriter(
                           stringWriter,
                           width,
                           alignment: Alignment.Justify))
                {
                    Stopwatch watch = Stopwatch.StartNew();
                    Parallel.For(
                        0,
                        1000,
                        new ParallelOptions {
                        MaxDegreeOfParallelism = 8
                    },
                        i => formatTextWriter.Write(FormatResources.ButIMustExplain));
                    watch.Stop();
                    Trace.WriteLine(watch.Elapsed.TotalMilliseconds);
                    Assert.AreEqual(50, formatTextWriter.Position);
                    string result = stringWriter.ToString();

                    string[] lines = result
                                     .Split(new[] { Environment.NewLine }, StringSplitOptions.None);

                    // Check number of lines and maximum line length, if we have any race conditions we expect these to change.
                    Assert.AreEqual(12500, lines.Length);
                    Assert.AreEqual(width, lines.Select(l => l.Length).Max());
                }
        }
コード例 #3
0
        public void TestLayoutWriter()
        {
            const ushort width = 15;

            using (StringWriter stringWriter = new StringWriter())
                using (FormatTextWriter formatTextWriter = new FormatTextWriter(
                           stringWriter,
                           width,
                           alignment: Alignment.Justify))
                {
                    formatTextWriter.Write("A short string that requires justification ");
                    formatTextWriter.Write("but is written in multiple steps ");
                    formatTextWriter.Write("whicher prevents any kind of justification on the resultant join points.");

                    string result = stringWriter.ToString();
                    Trace.WriteLine(result);

                    string[] lines = result
                                     .Split(new[] { Environment.NewLine }, StringSplitOptions.None);

                    // Check number of lines and maximum line length, if we have any race conditions we expect these to change.
                    Assert.AreEqual(12, lines.Length);
                    Assert.AreEqual(width, lines.Select(l => l.Length).Max());
                }
        }
コード例 #4
0
 public void TestAutoPositioning()
 {
     using (StringWriter writer = new StringWriter())
         using (FormatTextWriter fw = new FormatTextWriter(writer, 5))
         {
             fw.WriteLine();
             Assert.AreEqual(0, fw.Position);
             fw.WriteLine("12345");
             Assert.AreEqual(0, fw.Position);
             fw.Write("12345\r\n");
             Assert.AreEqual(0, fw.Position);
             fw.Write("1234\r\n");
             Assert.AreEqual(0, fw.Position);
             fw.Write("1234");
             Assert.AreEqual(4, fw.Position);
             fw.Write("123456");
             Assert.AreEqual(1, fw.Position);
             fw.Write("123456");
             Assert.AreEqual(1, fw.Position);
             fw.Write("12345\r\n1");
             Assert.AreEqual(1, fw.Position);
         }
 }