ByteStringTest.cs 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc.
  3. // http://code.google.com/p/protobuf/
  4. //
  5. // Licensed under the Apache License, Version 2.0 (the "License");
  6. // you may not use this file except in compliance with the License.
  7. // You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. using System.Text;
  17. using NUnit.Framework;
  18. namespace Google.ProtocolBuffers {
  19. [TestFixture]
  20. public class ByteStringTest {
  21. [Test]
  22. public void EmptyByteStringHasZeroSize() {
  23. Assert.AreEqual(0, ByteString.Empty.Length);
  24. }
  25. [Test]
  26. public void CopyFromStringWithExplicitEncoding() {
  27. ByteString bs = ByteString.CopyFrom("AB", Encoding.Unicode);
  28. Assert.AreEqual(4, bs.Length);
  29. Assert.AreEqual(65, bs[0]);
  30. Assert.AreEqual(0, bs[1]);
  31. Assert.AreEqual(66, bs[2]);
  32. Assert.AreEqual(0, bs[3]);
  33. }
  34. [Test]
  35. public void IsEmptyWhenEmpty() {
  36. Assert.IsTrue(ByteString.CopyFromUtf8("").IsEmpty);
  37. }
  38. [Test]
  39. public void IsEmptyWhenNotEmpty() {
  40. Assert.IsFalse(ByteString.CopyFromUtf8("X").IsEmpty);
  41. }
  42. [Test]
  43. public void CopyFromByteArrayCopiesContents() {
  44. byte[] data = new byte[1];
  45. data[0] = 10;
  46. ByteString bs = ByteString.CopyFrom(data);
  47. Assert.AreEqual(10, bs[0]);
  48. data[0] = 5;
  49. Assert.AreEqual(10, bs[0]);
  50. }
  51. [Test]
  52. public void ToByteArrayCopiesContents() {
  53. ByteString bs = ByteString.CopyFromUtf8("Hello");
  54. byte[] data = bs.ToByteArray();
  55. Assert.AreEqual('H', data[0]);
  56. Assert.AreEqual('H', bs[0]);
  57. data[0] = 0;
  58. Assert.AreEqual(0, data[0]);
  59. Assert.AreEqual('H', bs[0]);
  60. }
  61. [Test]
  62. public void CopyFromUtf8UsesUtf8() {
  63. ByteString bs = ByteString.CopyFromUtf8("\u20ac");
  64. Assert.AreEqual(3, bs.Length);
  65. Assert.AreEqual(0xe2, bs[0]);
  66. Assert.AreEqual(0x82, bs[1]);
  67. Assert.AreEqual(0xac, bs[2]);
  68. }
  69. [Test]
  70. public void CopyFromPortion() {
  71. byte[] data = new byte[]{0, 1, 2, 3, 4, 5, 6};
  72. ByteString bs = ByteString.CopyFrom(data, 2, 3);
  73. Assert.AreEqual(3, bs.Length);
  74. Assert.AreEqual(2, bs[0]);
  75. Assert.AreEqual(3, bs[1]);
  76. }
  77. [Test]
  78. public void ToStringUtf8() {
  79. ByteString bs = ByteString.CopyFromUtf8("\u20ac");
  80. Assert.AreEqual("\u20ac", bs.ToStringUtf8());
  81. }
  82. [Test]
  83. public void ToStringWithExplicitEncoding() {
  84. ByteString bs = ByteString.CopyFrom("\u20ac", Encoding.Unicode);
  85. Assert.AreEqual("\u20ac", bs.ToString(Encoding.Unicode));
  86. }
  87. }
  88. }