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