EncodeString() public static method

Encode specified string by default encoding.
/// is null. ///
public static EncodeString ( string value ) : byte[]
value string String value.
return byte[]
        private static Tuple <double, double, double, double> TestEqualsCore()
        {
            Assert.IsTrue(
                new MessagePackString(new byte[] { ( byte )'A', ( byte )'B', ( byte )'C' }, false).Equals(
                    new MessagePackString(new byte[] { ( byte )'A', ( byte )'B', ( byte )'C' }, false)
                    ),
                "Binary-Binary-True"
                );

            Assert.IsTrue(
                new MessagePackString(new byte[] { ( byte )'A', ( byte )'B', ( byte )'C' }, false).Equals(
                    new MessagePackString("ABC")
                    ),
                "Binary-String-True"
                );

            Assert.IsTrue(
                new MessagePackString("ABC").Equals(
                    new MessagePackString(new byte[] { ( byte )'A', ( byte )'B', ( byte )'C' }, false)
                    ),
                "String-Binary-True"
                );

            Assert.IsTrue(
                new MessagePackString("ABC").Equals(
                    new MessagePackString("ABC")
                    ),
                "String-String-True"
                );

            Assert.IsFalse(
                new MessagePackString(new byte[] { ( byte )'A', ( byte )'B', ( byte )'C' }, false).Equals(
                    new MessagePackString(new byte[] { ( byte )'A', ( byte )'B', ( byte )'D' }, false)
                    ),
                "Binary-Binary-False"
                );

            Assert.IsFalse(
                new MessagePackString(new byte[] { ( byte )'A', ( byte )'B', ( byte )'C' }, false).Equals(
                    new MessagePackString("ABD")
                    ),
                "Binary-String-False"
                );

            Assert.IsFalse(
                new MessagePackString("ABD").Equals(
                    new MessagePackString(new byte[] { ( byte )'A', ( byte )'B', ( byte )'C' }, false)
                    ),
                "String-Binary-False"
                );

            Assert.IsFalse(
                new MessagePackString("ABC").Equals(
                    new MessagePackString("ABD")
                    ),
                "String-String-False"
                );

            var values =
                new[]
            {
                new MessagePackString(new byte[0], false),
                new MessagePackString(new byte[] { 0x20 }, false),
                new MessagePackString(new byte[] { 0xff }, false),
                new MessagePackString(new byte[] { 1, 2, 3 }, false),
                new MessagePackString(new byte[] { 3, 2, 1 }, false)
            };

            const int iteration = 10;
            double    tinyAvg   = Double.MaxValue;
            double    smallAvg  = Double.MaxValue;
            double    mediumAvg = Double.MaxValue;
            double    largeAvg  = Double.MaxValue;

            var sw = new Stopwatch();

            for (int i = 0; i < iteration; i++)
            {
                sw.Restart();
                for (int x = 0; x < values.Length; x++)
                {
                    Assert.That(values[x].Equals(null), Is.False);

                    for (int y = 0; y < values.Length; y++)
                    {
                        Assert.That(values[x].Equals(values[y]), Is.EqualTo(x == y));
                    }
                }
                sw.Stop();
                tinyAvg = Math.Min(tinyAvg, sw.Elapsed.Ticks * 10.0 / (values.Length * values.Length));
            }

            var smallX = new MessagePackString(new String('A', 16));
            var smallY = new MessagePackString(MessagePackConvert.EncodeString(new String('A', 16)), false);

            for (int i = 0; i < iteration; i++)
            {
                sw.Restart();
                Assert.That(smallX.Equals(smallY), Is.True);
                sw.Stop();
                smallAvg = Math.Min(smallAvg, sw.Elapsed.Ticks * 10.0);
            }

            var mediumX = new MessagePackString(new String('A', 1000));
            var mediumY = new MessagePackString(MessagePackConvert.EncodeString(new String('A', 1000)), false);

            for (int i = 0; i < iteration; i++)
            {
                sw.Restart();
                Assert.That(mediumX.Equals(mediumY), Is.True);
                sw.Stop();
                mediumAvg = Math.Min(mediumAvg, sw.Elapsed.Ticks * 10.0);
            }

            var largeX = new MessagePackString(new String('A', 100000));
            var largeY = new MessagePackString(MessagePackConvert.EncodeString(new String('A', 100000)), false);

            for (int i = 0; i < iteration; i++)
            {
                sw.Restart();
                Assert.That(largeX.Equals(largeY), Is.True);
                sw.Stop();
                largeAvg = Math.Min(largeAvg, sw.Elapsed.Ticks * 10.0);
            }

            return(Tuple.Create(tinyAvg, smallAvg, mediumAvg, largeAvg));
        }
Exemplo n.º 2
0
 public void TestEncodeString_Empty_EncodedAsEmpty()
 {
     Assert.That(MessagePackConvert.EncodeString(String.Empty), Is.Empty);
 }
Exemplo n.º 3
0
 public void TestEncodeString_Null()
 {
     Assert.Throws <ArgumentNullException>(() => MessagePackConvert.EncodeString(null));
 }
Exemplo n.º 4
0
        public void TestEncodeString_Normal_EncodedAsUtf8NonBom()
        {
            var encoding = new UTF8Encoding(encoderShouldEmitUTF8Identifier: false);

            Assert.AreEqual(encoding.GetBytes(_testValue), MessagePackConvert.EncodeString(_testValue));
        }