public void GetBytes_should_throw_when_value_contains_unmatched_surrogate_pair()
        {
            var value = "\ud800";
            var bytes = new byte[CStringUtf8Encoding.GetMaxByteCount(value.Length)];

            Action action = () => CStringUtf8Encoding.GetBytes(value, bytes, 0, Utf8Encodings.Strict);

            action.ShouldThrow <ArgumentException>();
        }
        public void GetBytes_should_throw_when_value_contains_null_and_fallback_encoding_is_used()
        {
            var value = "\ud800\udc00\u0000";
            var bytes = new byte[CStringUtf8Encoding.GetMaxByteCount(value.Length)];

            Action action = () => CStringUtf8Encoding.GetBytes(value, bytes, 0, Utf8Encodings.Strict);

            action.ShouldThrow <ArgumentException>().And.ParamName.Should().Be("value");
        }
        public void GetBytes_should_throw_when_value_contains_null(
            [Values("\0", "a\0", "ab\0")] string value)
        {
            var bytes = new byte[CStringUtf8Encoding.GetMaxByteCount(value.Length)];

            Action action = () => CStringUtf8Encoding.GetBytes(value, bytes, 0, Utf8Encodings.Strict);

            action.ShouldThrow <ArgumentException>().And.ParamName.Should().Be("value");
        }
        public void GetMaxByteCount_should_return_expected_result(
            [Values(0, 1, 2)] int charCount)
        {
            var expectedResult = charCount * 3;

            var result = CStringUtf8Encoding.GetMaxByteCount(charCount);

            result.Should().Be(expectedResult);
        }
        [TestCase("\ud800\udc00", 0, 4, new byte[] { 0xf0, 0x90, 0x80, 0x80 })] // surrogate pair
        public void GetBytes_should_return_expected_result(
            string value,
            int byteIndex,
            int expectedResult,
            byte[] expectedBytes)
        {
            var bytes = new byte[CStringUtf8Encoding.GetMaxByteCount(value.Length) + byteIndex];

            var result = CStringUtf8Encoding.GetBytes(value, bytes, byteIndex, Utf8Encodings.Strict);

            result.Should().Be(expectedResult);
            bytes.Take(byteIndex).All(b => b == 0).Should().BeTrue();
            bytes.Skip(byteIndex).Take(result).Should().Equal(expectedBytes);
            bytes.Skip(byteIndex + result).All(b => b == 0).Should().BeTrue();
        }