ByteStringTest.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. #region Copyright notice and license
  2. // Protocol Buffers - Google's data interchange format
  3. // Copyright 2008 Google Inc. All rights reserved.
  4. // https://developers.google.com/protocol-buffers/
  5. //
  6. // Redistribution and use in source and binary forms, with or without
  7. // modification, are permitted provided that the following conditions are
  8. // met:
  9. //
  10. // * Redistributions of source code must retain the above copyright
  11. // notice, this list of conditions and the following disclaimer.
  12. // * Redistributions in binary form must reproduce the above
  13. // copyright notice, this list of conditions and the following disclaimer
  14. // in the documentation and/or other materials provided with the
  15. // distribution.
  16. // * Neither the name of Google Inc. nor the names of its
  17. // contributors may be used to endorse or promote products derived from
  18. // this software without specific prior written permission.
  19. //
  20. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. #endregion
  32. using System;
  33. using System.Text;
  34. using NUnit.Framework;
  35. namespace Google.Protobuf
  36. {
  37. public class ByteStringTest
  38. {
  39. [Test]
  40. public void EmptyByteStringHasZeroSize()
  41. {
  42. Assert.AreEqual(0, ByteString.Empty.Length);
  43. }
  44. [Test]
  45. public void CopyFromStringWithExplicitEncoding()
  46. {
  47. ByteString bs = ByteString.CopyFrom("AB", Encoding.Unicode);
  48. Assert.AreEqual(4, bs.Length);
  49. Assert.AreEqual(65, bs[0]);
  50. Assert.AreEqual(0, bs[1]);
  51. Assert.AreEqual(66, bs[2]);
  52. Assert.AreEqual(0, bs[3]);
  53. }
  54. [Test]
  55. public void IsEmptyWhenEmpty()
  56. {
  57. Assert.IsTrue(ByteString.CopyFromUtf8("").IsEmpty);
  58. }
  59. [Test]
  60. public void IsEmptyWhenNotEmpty()
  61. {
  62. Assert.IsFalse(ByteString.CopyFromUtf8("X").IsEmpty);
  63. }
  64. [Test]
  65. public void CopyFromByteArrayCopiesContents()
  66. {
  67. byte[] data = new byte[1];
  68. data[0] = 10;
  69. ByteString bs = ByteString.CopyFrom(data);
  70. Assert.AreEqual(10, bs[0]);
  71. data[0] = 5;
  72. Assert.AreEqual(10, bs[0]);
  73. }
  74. [Test]
  75. public void ToByteArrayCopiesContents()
  76. {
  77. ByteString bs = ByteString.CopyFromUtf8("Hello");
  78. byte[] data = bs.ToByteArray();
  79. Assert.AreEqual((byte)'H', data[0]);
  80. Assert.AreEqual((byte)'H', bs[0]);
  81. data[0] = 0;
  82. Assert.AreEqual(0, data[0]);
  83. Assert.AreEqual((byte)'H', bs[0]);
  84. }
  85. [Test]
  86. public void CopyFromUtf8UsesUtf8()
  87. {
  88. ByteString bs = ByteString.CopyFromUtf8("\u20ac");
  89. Assert.AreEqual(3, bs.Length);
  90. Assert.AreEqual(0xe2, bs[0]);
  91. Assert.AreEqual(0x82, bs[1]);
  92. Assert.AreEqual(0xac, bs[2]);
  93. }
  94. [Test]
  95. public void CopyFromPortion()
  96. {
  97. byte[] data = new byte[] {0, 1, 2, 3, 4, 5, 6};
  98. ByteString bs = ByteString.CopyFrom(data, 2, 3);
  99. Assert.AreEqual(3, bs.Length);
  100. Assert.AreEqual(2, bs[0]);
  101. Assert.AreEqual(3, bs[1]);
  102. }
  103. [Test]
  104. public void ToStringUtf8()
  105. {
  106. ByteString bs = ByteString.CopyFromUtf8("\u20ac");
  107. Assert.AreEqual("\u20ac", bs.ToStringUtf8());
  108. }
  109. [Test]
  110. public void ToStringWithExplicitEncoding()
  111. {
  112. ByteString bs = ByteString.CopyFrom("\u20ac", Encoding.Unicode);
  113. Assert.AreEqual("\u20ac", bs.ToString(Encoding.Unicode));
  114. }
  115. [Test]
  116. public void FromBase64_WithText()
  117. {
  118. byte[] data = new byte[] {0, 1, 2, 3, 4, 5, 6};
  119. string base64 = Convert.ToBase64String(data);
  120. ByteString bs = ByteString.FromBase64(base64);
  121. Assert.AreEqual(data, bs.ToByteArray());
  122. }
  123. [Test]
  124. public void FromBase64_Empty()
  125. {
  126. // Optimization which also fixes issue 61.
  127. Assert.AreSame(ByteString.Empty, ByteString.FromBase64(""));
  128. }
  129. }
  130. }