ByteStringTest.cs 5.0 KB

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