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