public void BufferTest_WithError()
        {
            string json = @"{
              ""CPU"": ""Intel?\nYes"",
              ""Description"": ""Amazin";

            FakeArrayPool arrayPool = new FakeArrayPool();

            try
            {
                // dispose will free used buffers
                using (JsonTextReader reader = new JsonTextReader(new StringReader(json)))
                {
                    reader.ArrayPool = arrayPool;

                    while (reader.Read())
                    {
                    }
                }

                Assert.Fail();
            }
            catch
            {
            }

            Assert.AreEqual(0, arrayPool.UsedArrays.Count);
            Assert.AreEqual(2, arrayPool.FreeArrays.Count);
        }
        public void BufferTest()
        {
            string json = @"{
              ""CPU"": ""Intel"",
              ""Description"": ""Amazing!\nBuy now!"",
              ""Drives"": [
                ""DVD read/writer"",
                ""500 gigabyte hard drive"",
                ""Amazing Drive" + new string('!', 9000) + @"""
              ]
            }";

            FakeArrayPool arrayPool = new FakeArrayPool();

            for (int i = 0; i < 1000; i++)
            {
                using (JsonTextReader reader = new JsonTextReader(new StringReader(json)))
                {
                    reader.ArrayPool = arrayPool;

                    while (reader.Read())
                    {
                    }
                }

                if ((i + 1) % 100 == 0)
                {
                    Console.WriteLine("Allocated buffers: " + arrayPool.FreeArrays.Count);
                }
            }

            Assert.AreEqual(0, arrayPool.UsedArrays.Count);
            Assert.AreEqual(6, arrayPool.FreeArrays.Count);
        }
        public void BufferTest()
        {
            FakeArrayPool arrayPool = new FakeArrayPool();

            string longString = new string('A', 2000);
            string longEscapedString = "Hello!" + new string('!', 50) + new string('\n', 1000) + "Good bye!";
            string longerEscapedString = "Hello!" + new string('!', 2000) + new string('\n', 1000) + "Good bye!";

            for (int i = 0; i < 1000; i++)
            {
                StringWriter sw = new StringWriter(CultureInfo.InvariantCulture);

                using (JsonTextWriter writer = new JsonTextWriter(sw))
                {
                    writer.ArrayPool = arrayPool;

                    writer.WriteStartObject();

                    writer.WritePropertyName("Prop1");
                    writer.WriteValue(new DateTime(2000, 12, 12, 12, 12, 12, DateTimeKind.Utc));

                    writer.WritePropertyName("Prop2");
                    writer.WriteValue(longString);

                    writer.WritePropertyName("Prop3");
                    writer.WriteValue(longEscapedString);

                    writer.WritePropertyName("Prop4");
                    writer.WriteValue(longerEscapedString);

                    writer.WriteEndObject();
                }

                if ((i + 1) % 100 == 0)
                {
                    Console.WriteLine("Allocated buffers: " + arrayPool.FreeArrays.Count);
                }
            }

            Assert.AreEqual(0, arrayPool.UsedArrays.Count);
            Assert.AreEqual(3, arrayPool.FreeArrays.Count);
        }
        public void BufferTest_WithError()
        {
            FakeArrayPool arrayPool = new FakeArrayPool();

            StringWriter sw = new StringWriter(CultureInfo.InvariantCulture);

            try
            {
                // dispose will free used buffers
                using (JsonTextWriter writer = new JsonTextWriter(sw))
                {
                    writer.ArrayPool = arrayPool;

                    writer.WriteStartObject();

                    writer.WritePropertyName("Prop1");
                    writer.WriteValue(new DateTime(2000, 12, 12, 12, 12, 12, DateTimeKind.Utc));

                    writer.WritePropertyName("Prop2");
                    writer.WriteValue("This is an escaped \n string!");

                    writer.WriteValue("Error!");
                }


                Assert.Fail();
            }
            catch
            {
            }

            Assert.AreEqual(0, arrayPool.UsedArrays.Count);
            Assert.AreEqual(1, arrayPool.FreeArrays.Count);
        }