Exemple #1
0
        public void EndsWithCodeUnit(string s, char c)
        {
            Utf8String   u8s      = new Utf8String(s);
            Utf8CodeUnit codeUnit = (Utf8CodeUnit)(byte)c;

            Assert.Equal(s.EndsWith(c.ToString()), u8s.EndsWith(codeUnit));
        }
Exemple #2
0
        public void IndexOfUtf8Character(string s, char character)
        {
            int expected = s.IndexOf(character);

            Utf8String   u8s        = new Utf8String(s);
            Utf8CodeUnit u8codeUnit = (Utf8CodeUnit)(byte)(character);

            Assert.Equal(expected, u8s.IndexOf(u8codeUnit));
        }
 public void ConvertToChar(bool shouldThrow, Utf8CodeUnit codeUnit)
 {
     if (shouldThrow)
     {
         Assert.Throws <ArgumentOutOfRangeException>(() => { var c = (char)codeUnit; });
     }
     else
     {
         Assert.Equal((int)codeUnit.Value, (int)((char)codeUnit));
     }
 }
 public void CtorChar(bool shouldThrow, char value)
 {
     if (shouldThrow)
     {
         Assert.Throws <ArgumentOutOfRangeException>(() => { var codeUnit = new Utf8CodeUnit(value); });
     }
     else
     {
         Assert.Equal((byte)value, (new Utf8CodeUnit(value)).Value);
     }
 }
        public void TryCreateFrom(bool expectedNegated, char value)
        {
            bool expected = !expectedNegated;

            Utf8CodeUnit codeUnit;

            Assert.Equal(expected, Utf8CodeUnit.TryCreateFrom(value, out codeUnit));
            if (expected)
            {
                Assert.Equal((int)value, (int)codeUnit.Value);
            }
        }
        public void GetHashCodeIsDifferentForEveryPossibleCodeUnit()
        {
            HashSet<int> hashes = new HashSet<int>();
            for (int i = byte.MinValue; i <= byte.MaxValue; i++)
            {
                Utf8CodeUnit codeUnit = new Utf8CodeUnit((byte)i);
                Assert.True(hashes.Add(codeUnit.GetHashCode()));
            }

            // sanity
            Assert.Equal(256, hashes.Count);
        }
        public void GetHashCodeIsDifferentForEveryPossibleCodeUnit()
        {
            HashSet <int> hashes = new HashSet <int>();

            for (int i = byte.MinValue; i <= byte.MaxValue; i++)
            {
                Utf8CodeUnit codeUnit = new Utf8CodeUnit((byte)i);
                Assert.True(hashes.Add(codeUnit.GetHashCode()));
            }

            // sanity
            Assert.Equal(256, hashes.Count);
        }
Exemple #8
0
        public void SubstringToCodeUnit(string s, char c)
        {
            Utf8String   u8s      = new Utf8String(s);
            Utf8CodeUnit codeUnit = (Utf8CodeUnit)(byte)(c);
            Utf8String   u8result;

            int  idx            = s.IndexOf(c);
            bool expectedToFind = idx != -1;

            Assert.Equal(expectedToFind, u8s.TrySubstringTo(codeUnit, out u8result));
            if (expectedToFind)
            {
                string expected = s.Substring(0, idx);
                Assert.Equal(new Utf8String(expected), u8result);
                Assert.Equal(expected, u8result.ToString());
            }
        }
        private static bool IsFalse(Utf8String text)
        {
            if (text.Length < 5)
            {
                return(false);
            }

            Utf8CodeUnit firstChar = text[0];

            if (firstChar != 'f' && firstChar != 'F')
            {
                return(false);
            }

            Utf8CodeUnit secondChar = text[1];

            if (secondChar != 'a' && secondChar != 'A')
            {
                return(false);
            }

            Utf8CodeUnit thirdChar = text[2];

            if (thirdChar != 'l' && thirdChar != 'L')
            {
                return(false);
            }

            Utf8CodeUnit fourthChar = text[3];

            if (fourthChar != 's' && fourthChar != 'S')
            {
                return(false);
            }

            Utf8CodeUnit fifthChar = text[4];

            if (fifthChar != 'e' && fifthChar != 'E')
            {
                return(false);
            }

            return(true);
        }
        public static bool TryParseBoolean(Utf8String text, Format.Parsed format,
                                           out bool value, out int bytesConsumed)
        {
            bytesConsumed = 0;
            value         = default(bool);
            if (text.Length < 1)
            {
                return(false);
            }

            Utf8CodeUnit firstCodeUnit = text[0];

            if (firstCodeUnit == '1')
            {
                bytesConsumed = 1;
                value         = true;
                return(true);
            }
            else if (firstCodeUnit == '0')
            {
                bytesConsumed = 1;
                value         = false;
                return(true);
            }
            else if (IsTrue(text))
            {
                bytesConsumed = 4;
                value         = true;
                return(true);
            }
            else if (IsFalse(text))
            {
                bytesConsumed = 5;
                value         = false;
                return(true);
            }
            else
            {
                return(false);
            }
        }
        private static bool IsTrue(Utf8String text)
        {
            if (text.Length < 4)
            {
                return(false);
            }

            Utf8CodeUnit firstChar = text[0];

            if (firstChar != 't' && firstChar != 'T')
            {
                return(false);
            }

            Utf8CodeUnit secondChar = text[1];

            if (secondChar != 'r' && secondChar != 'R')
            {
                return(false);
            }

            Utf8CodeUnit thirdChar = text[2];

            if (thirdChar != 'u' && thirdChar != 'U')
            {
                return(false);
            }

            Utf8CodeUnit fourthChar = text[3];

            if (fourthChar != 'e' && fourthChar != 'E')
            {
                return(false);
            }

            return(true);
        }
 public void EqualsChar(bool expected, Utf8CodeUnit codeUnit, char c)
 {
     Assert.Equal(expected, codeUnit.Equals(c));
 }
 public void EqualsBoxedChar(bool expected, Utf8CodeUnit codeUnit, char c)
 {
     Assert.Equal(expected, codeUnit.Equals((object)c));
 }
 public void EqualsAnyOtherObject(bool expected, Utf8CodeUnit codeUnit, object obj)
 {
     Assert.Equal(expected, codeUnit.Equals(obj));
 }
 public void EqualsAnyOtherObject(bool expected, Utf8CodeUnit codeUnit, object obj)
 {
     Assert.Equal(expected, codeUnit.Equals(obj));
 }
 public void EqualsBoxedUtf8CodeUnit(bool expected, Utf8CodeUnit a, Utf8CodeUnit b)
 {
     Assert.Equal(expected, a.Equals((object)b));
     Assert.Equal(expected, b.Equals((object)a));
 }
 public void IsFirstCodeUnitInEncodedCodePoint(bool expected, Utf8CodeUnit codeUnit)
 {
     Assert.Equal(expected, Utf8CodeUnit.IsFirstCodeUnitInEncodedCodePoint(codeUnit));
 }
 public void EqualsOperator(bool expected, Utf8CodeUnit a, Utf8CodeUnit b)
 {
     Assert.Equal(expected, a == b);
     Assert.Equal(expected, b == a);
 }
 public void EqualsBoxedUtf8CodeUnit(bool expected, Utf8CodeUnit a, Utf8CodeUnit b)
 {
     Assert.Equal(expected, a.Equals((object)b));
     Assert.Equal(expected, b.Equals((object)a));
 }
 public void EqualsChar(bool expected, Utf8CodeUnit codeUnit, char c)
 {
     Assert.Equal(expected, codeUnit.Equals(c));
 }
 public void ConvertToByte(Utf8CodeUnit codeUnit)
 {
     Assert.Equal(codeUnit.Value, (byte)codeUnit);
 }
 public void IsAscii(bool expected, Utf8CodeUnit codeUnit)
 {
     Assert.Equal(expected, Utf8CodeUnit.IsAscii(codeUnit));
 }
 public void NotEqualsOperator(bool expectedNegated, Utf8CodeUnit a, Utf8CodeUnit b)
 {
     Assert.Equal(!expectedNegated, a != b);
     Assert.Equal(!expectedNegated, b != a);
 }
 public void EqualsOperator(bool expected, Utf8CodeUnit a, Utf8CodeUnit b)
 {
     Assert.Equal(expected, a == b);
     Assert.Equal(expected, b == a);
 }
 public void ConvertToByte(Utf8CodeUnit codeUnit)
 {
     Assert.Equal(codeUnit.Value, (byte)codeUnit);
 }
 public void CtorChar(bool shouldThrow, char value)
 {
     if (shouldThrow)
     {
         Assert.Throws<ArgumentOutOfRangeException>(() => { var codeUnit = new Utf8CodeUnit(value); });
     }
     else
     {
         Assert.Equal((byte)value, (new Utf8CodeUnit(value)).Value);
     }
 }
 public void EqualsUtf8CodeUnit(bool expected, Utf8CodeUnit a, Utf8CodeUnit b)
 {
     Assert.Equal(expected, a.Equals(b));
     Assert.Equal(expected, b.Equals(a));
 }
 public void IsAscii(bool expected, Utf8CodeUnit codeUnit)
 {
     Assert.Equal(expected, Utf8CodeUnit.IsAscii(codeUnit));
 }
 public void ConvertToChar(bool shouldThrow, Utf8CodeUnit codeUnit)
 {
     if (shouldThrow)
     {
         Assert.Throws<ArgumentOutOfRangeException>(() => { var c = (char)codeUnit; });
     }
     else
     {
         Assert.Equal((int)codeUnit.Value, (int)((char)codeUnit));
     }
 }
 public void NotEqualsOperator(bool expectedNegated, Utf8CodeUnit a, Utf8CodeUnit b)
 {
     Assert.Equal(!expectedNegated, a != b);
     Assert.Equal(!expectedNegated, b != a);
 }
 public void EqualsUtf8CodeUnit(bool expected, Utf8CodeUnit a, Utf8CodeUnit b)
 {
     Assert.Equal(expected, a.Equals(b));
     Assert.Equal(expected, b.Equals(a));
 }
 public void IsFirstCodeUnitInEncodedCodePoint(bool expected, Utf8CodeUnit codeUnit)
 {
     Assert.Equal(expected, Utf8CodeUnit.IsFirstCodeUnitInEncodedCodePoint(codeUnit));
 }
 public void EqualsBoxedChar(bool expected, Utf8CodeUnit codeUnit, char c)
 {
     Assert.Equal(expected, codeUnit.Equals((object)c));
 }