Beispiel #1
0
        public unsafe void StringsNullable()
        {
            var s = new ReusableStream(40);

            var          expectedLength = 0;
            const string emptyString    = "";

            s.WriteString(null, true);
            expectedLength += 2;
            Assert.AreEqual(expectedLength, s.Length);

            s.WriteString(null, 0, 0, true);
            expectedLength += 2;
            Assert.AreEqual(expectedLength, s.Length);

            s.WriteString(null, 0, true);
            expectedLength += 2;
            Assert.AreEqual(expectedLength, s.Length);

            s.WriteString(emptyString, true);
            expectedLength += 2;
            Assert.AreEqual(expectedLength, s.Length);

            s.WriteString(Array.Empty <char>(), 0, 0, true);
            expectedLength += 2;
            Assert.AreEqual(expectedLength, s.Length);

            fixed(char *ptr = emptyString)
            s.WriteString(ptr, 0, true);

            expectedLength += 2;
            Assert.AreEqual(expectedLength, s.Length);

            s.ResetForReading();

            Assert.AreEqual(null, s.ReadString(true));
            Assert.AreEqual(null, s.ReadString(true));
            Assert.AreEqual(null, s.ReadString(true));
            Assert.AreEqual(emptyString, s.ReadString(true));
            Assert.AreEqual(emptyString, s.ReadString(true));
            Assert.AreEqual(emptyString, s.ReadString(true));

            Assert.Throws <ArgumentNullException>(() => s.WriteString(null, false));
            Assert.Throws <ArgumentNullException>(() => s.WriteString(null, 0, 0, false));
            Assert.Throws <ArgumentNullException>(() => s.WriteString(null, 0, false));
        }
Beispiel #2
0
        public void StringEdgeCases()
        {
            // The purpose of this method is to trigger some edge cases in the string writing code where the max encoded size of given string length is greater
            // than the remaining bytes in the stream; however, the *actual* encoded string might still fit. If the real encoded size will fit, the stream
            // should not grow.

            var s = new ReusableStream(8);

            var sevenAscii   = "seven c"; // should fit
            var sevenUnicode = "seven 瀬"; // won't fit

            s.WriteString(sevenAscii, false);
            Assert.AreEqual(8, s.Length);
            Assert.AreEqual(8, s.Capacity);

            s.ResetForWriting();

            s.WriteString(sevenUnicode, false);
            Assert.AreEqual(10, s.Length);
            Assert.IsTrue(s.Capacity >= 10);
        }
Beispiel #3
0
        public void StringEncodingLengths()
        {
            var s = new ReusableStream(100);

            var expectedLength = 0;

            var strings = new[]
            {
                "a",
                "瀬",
                "𐐷",
            };

            var encodings = new[]
            {
                Encoding.ASCII,
                Encoding.UTF7,
                Encoding.UTF8,
                Encoding.Unicode,
                Encoding.BigEndianUnicode,
                Encoding.UTF32,
            };

            foreach (var str in strings)
            {
                foreach (var e in encodings)
                {
                    s.WriteString(str, false, e);
                    expectedLength += e.GetByteCount(str) + 1;
                    Assert.AreEqual(expectedLength, s.Length);
                }
            }

            s.ResetForReading();

            foreach (var str in strings)
            {
                foreach (var e in encodings)
                {
                    var read = s.ReadString(false, e);

                    if (e == Encoding.ASCII)
                    {
                        continue;
                    }

                    Assert.AreEqual(str, read);
                }
            }
        }
Beispiel #4
0
        public void StringInterning()
        {
            var s = new ReusableStream(100);

            var          strings = new[] { "cat", "deer", "snail", "dog", "frog", "human" };
            const int    max     = 4;
            const string exclude = "frog";

            foreach (var str in strings)
            {
                s.WriteString(str, false);
            }

            s.ResetForReading();

            // we're not interning yet - make sure new strings were returned
            foreach (var str in strings)
            {
                var read = s.ReadString(false);
                Assert.AreEqual(str, read);
                Assert.AreNotSame(str, read);
            }

            s.ResetForReading();

            var options = new StringSetOptions();

            options.MaxEncodedSizeToLookupInSet = max;

            Assert.Throws <InvalidOperationException>(() => s.SetDefaultStringSetOptions(options));

            var set = new StringSet(10);

            s.StringSet = set;
            s.SetDefaultStringSetOptions(options);
            s.StringSet = null;

            // should throw because no StringSet has been provided
            Assert.Throws <InvalidOperationException>(() => s.ReadString(false));

            foreach (var str in strings)
            {
                if (str != exclude)
                {
                    set.Add(str);
                }
            }

            s.StringSet = set;

            // read with interning (but no auto-interning)
            foreach (var str in strings)
            {
                var read = s.ReadString(false);
                Assert.AreEqual(str, read);

                if (str.Length <= max && str != exclude)
                {
                    Assert.AreSame(str, read);
                }
                else
                {
                    Assert.AreNotSame(str, read);
                }
            }

            // make sure the excluded string didn't get added to the set
            Assert.AreEqual(null, set.GetExistingString(exclude.ToCharArray(), 0, exclude.Length));

            s.ResetForReading();
            options.PerformDangerousAutoAddToSet = true;
            s.SetDefaultStringSetOptions(options);

            // read with auto-interning
            foreach (var str in strings)
            {
                var read = s.ReadString(false);
                Assert.AreEqual(str, read);

                if (str.Length <= max && str != exclude)
                {
                    Assert.AreSame(str, read);
                }
                else
                {
                    Assert.AreNotSame(str, read);
                }
            }

            // make sure the excluded string got added to the set
            Assert.AreEqual(exclude, set.GetExistingString(exclude.ToCharArray(), 0, exclude.Length));
        }
Beispiel #5
0
        public unsafe void Strings()
        {
            // not including ASCII since it won't round-trip for the unicode strings
            var encodings = new[]
            {
                Encoding.UTF7,
                Encoding.UTF8,
                Encoding.Unicode,
                Encoding.BigEndianUnicode,
                Encoding.UTF32,
            };

            var strings = new[]
            {
                "",
                "a",
                "cat",
                @"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc odio ligula, pharetra eget eros et, blandit luctus justo. Fusce ultricies id tortor sit amet laoreet. Cras posuere tellus vel aliquam tristique. Duis et quam sit amet sapien ullamcorper blandit a sed dolor. Duis placerat nisl ac egestas suscipit. Sed lacus tellus, convallis placerat sodales vitae, tempor et urna. Duis ut leo dictum, tempus ante ut, lacinia nisi. Nunc consectetur orci nisl, vitae fringilla justo vestibulum vitae. Proin orci velit, iaculis ut ornare quis, rhoncus in lectus.

Donec volutpat convallis faucibus. Donec finibus erat sit amet tortor rhoncus suscipit. Quisque facilisis lacus risus, sit amet pretium ipsum ornare et. Donec nec elementum dui. Nunc volutpat commodo metus ac tincidunt. Aenean eget lectus lacus. Donec mauris libero, accumsan id ex a, mollis rutrum lacus. Praesent mollis pretium ipsum eu faucibus. Fusce posuere congue libero, ut aliquam nulla dapibus vitae. Etiam sit amet convallis justo, sit amet hendrerit diam.

Suspendisse eleifend vitae quam ac tempor. Sed fringilla tempus enim in fringilla. Fusce at leo metus. Vestibulum sollicitudin tempus odio, non posuere leo congue lobortis. Maecenas aliquam diam eget urna finibus, vitae egestas ipsum ultrices. Sed risus enim, tempor eget tortor non, malesuada auctor nulla. Aliquam egestas posuere erat, quis interdum elit efficitur sed. Suspendisse malesuada, ipsum id iaculis semper, nunc tortor congue nulla, non auctor est magna vitae ligula. Nulla sit amet orci nisl. Nulla non bibendum felis, id tempor tortor.

Nam neque neque, tempus quis semper id, porttitor vitae tortor. Nulla sit amet leo eros. Orci varius natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Fusce varius ipsum non risus lobortis, et pretium elit malesuada. Sed luctus fermentum orci, et rutrum lacus faucibus in. Integer sodales leo aliquet mattis tristique. Nam consequat ornare lorem eget fermentum. Vestibulum a lectus sit amet nibh egestas fermentum sit amet at est. Suspendisse quis dapibus metus, sed imperdiet felis. Nulla nec consequat elit. Pellentesque pretium erat ornare felis semper tincidunt. Nullam nec magna lectus. Donec feugiat ligula urna, eget sodales ipsum fermentum sed. Morbi nec metus sit amet neque finibus hendrerit in quis ligula. Morbi lectus nulla, porta sed eros commodo, aliquam scelerisque felis.

Proin fringilla pellentesque odio. Sed finibus in dolor non laoreet. Mauris in magna eget ex faucibus varius sed nec arcu. Sed tincidunt ut nulla sit amet rutrum. Mauris at neque neque. Nulla vel urna rhoncus, ultricies quam aliquet, congue nunc. Sed lectus dolor, placerat at tempor at, lacinia non ipsum. Maecenas commodo, lectus eget blandit laoreet, massa nunc egestas leo, non iaculis felis felis at lorem. Ut ut sagittis arcu, et sollicitudin nisi. Aliquam auctor porta rhoncus. Ut est enim, dictum at sollicitudin vitae, feugiat sed urna. Pellentesque pretium mi id nunc commodo, id efficitur metus vulputate. Donec rhoncus, lectus sed tincidunt accumsan, metus odio vehicula lectus, vitae tristique tellus ex id enim.",
                "っつ雲日御へ保瀬とれろなほニミャメ",
                "𐐷",
                "END"
            };

            foreach (var encoding in encodings)
            {
                // try writing strings
                var s = new ReusableStream(10000);
                s.DefaultEncoding = encoding;

                foreach (var t in strings)
                {
                    s.WriteString(t, false);
                }

                s.ResetForReading();

                foreach (var t in strings)
                {
                    var str = s.ReadString(false);
                    Assert.AreEqual(t, str);
                }

                // try writing char[]
                s = new ReusableStream(10000);
                s.DefaultEncoding = encoding;

                foreach (var t in strings)
                {
                    s.WriteString(t.ToCharArray(), 0, t.Length, false);
                }

                s.ResetForReading();

                foreach (var t in strings)
                {
                    var str = s.ReadString(false);
                    Assert.AreEqual(t, str);
                }

                // try writing char*
                s = new ReusableStream(10000);
                s.DefaultEncoding = encoding;

                foreach (var t in strings)
                {
                    fixed(char *ptr = t)
                    {
                        s.WriteString(ptr, t.Length, false);
                    }
                }

                s.ResetForReading();

                foreach (var t in strings)
                {
                    var str = s.ReadString(false);
                    Assert.AreEqual(t, str);
                }
            }
        }