Jon Skeet | 6803686 | 2008-10-22 13:30:34 +0100 | [diff] [blame] | 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 | |
| 19 | namespace Google.ProtocolBuffers { |
| 20 | [TestFixture] |
| 21 | public class ByteStringTest { |
| 22 | [Test] |
| 23 | public void EmptyByteStringHasZeroSize() { |
| 24 | Assert.AreEqual(0, ByteString.Empty.Length); |
| 25 | } |
| 26 | |
| 27 | [Test] |
| 28 | public void CopyFromStringWithExplicitEncoding() { |
| 29 | ByteString bs = ByteString.CopyFrom("AB", Encoding.Unicode); |
| 30 | Assert.AreEqual(4, bs.Length); |
| 31 | Assert.AreEqual(65, bs[0]); |
| 32 | Assert.AreEqual(0, bs[1]); |
| 33 | Assert.AreEqual(66, bs[2]); |
| 34 | Assert.AreEqual(0, bs[3]); |
| 35 | } |
| 36 | |
| 37 | [Test] |
| 38 | public void IsEmptyWhenEmpty() { |
| 39 | Assert.IsTrue(ByteString.CopyFromUtf8("").IsEmpty); |
| 40 | } |
| 41 | |
| 42 | [Test] |
| 43 | public void IsEmptyWhenNotEmpty() { |
| 44 | Assert.IsFalse(ByteString.CopyFromUtf8("X").IsEmpty); |
| 45 | } |
| 46 | |
| 47 | [Test] |
| 48 | public void CopyFromByteArrayCopiesContents() { |
| 49 | byte[] data = new byte[1]; |
| 50 | data[0] = 10; |
| 51 | ByteString bs = ByteString.CopyFrom(data); |
| 52 | Assert.AreEqual(10, bs[0]); |
| 53 | data[0] = 5; |
| 54 | Assert.AreEqual(10, bs[0]); |
| 55 | } |
| 56 | |
| 57 | [Test] |
| 58 | public void ToByteArrayCopiesContents() { |
| 59 | ByteString bs = ByteString.CopyFromUtf8("Hello"); |
| 60 | byte[] data = bs.ToByteArray(); |
| 61 | Assert.AreEqual('H', data[0]); |
| 62 | Assert.AreEqual('H', bs[0]); |
| 63 | data[0] = 0; |
| 64 | Assert.AreEqual(0, data[0]); |
| 65 | Assert.AreEqual('H', bs[0]); |
| 66 | } |
| 67 | |
| 68 | [Test] |
| 69 | public void CopyFromUtf8UsesUtf8() { |
| 70 | ByteString bs = ByteString.CopyFromUtf8("\u20ac"); |
| 71 | Assert.AreEqual(3, bs.Length); |
| 72 | Assert.AreEqual(0xe2, bs[0]); |
| 73 | Assert.AreEqual(0x82, bs[1]); |
| 74 | Assert.AreEqual(0xac, bs[2]); |
| 75 | } |
| 76 | |
| 77 | [Test] |
| 78 | public void CopyFromPortion() { |
| 79 | byte[] data = new byte[]{0, 1, 2, 3, 4, 5, 6}; |
| 80 | ByteString bs = ByteString.CopyFrom(data, 2, 3); |
| 81 | Assert.AreEqual(3, bs.Length); |
| 82 | Assert.AreEqual(2, bs[0]); |
| 83 | Assert.AreEqual(3, bs[1]); |
| 84 | } |
| 85 | |
| 86 | [Test] |
| 87 | public void ToStringUtf8() { |
| 88 | ByteString bs = ByteString.CopyFromUtf8("\u20ac"); |
| 89 | Assert.AreEqual("\u20ac", bs.ToStringUtf8()); |
| 90 | } |
| 91 | |
| 92 | [Test] |
| 93 | public void ToStringWithExplicitEncoding() { |
| 94 | ByteString bs = ByteString.CopyFrom("\u20ac", Encoding.Unicode); |
| 95 | Assert.AreEqual("\u20ac", bs.ToString(Encoding.Unicode)); |
| 96 | } |
| 97 | } |
| 98 | } |