blob: 97425fe894d6af95dc6d4d981d3d1e8c5b09df2c [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;
csharptesteac64a52011-10-04 13:43:26 -050038using Microsoft.VisualStudio.TestTools.UnitTesting;
csharptest71f662c2011-05-20 15:15:34 -050039
40namespace Google.ProtocolBuffers
41{
csharptesteac64a52011-10-04 13:43:26 -050042 [TestClass]
csharptest71f662c2011-05-20 15:15:34 -050043 public class ByteStringTest
44 {
csharptesteac64a52011-10-04 13:43:26 -050045 [TestMethod]
csharptest71f662c2011-05-20 15:15:34 -050046 public void EmptyByteStringHasZeroSize()
47 {
48 Assert.AreEqual(0, ByteString.Empty.Length);
49 }
50
csharptesteac64a52011-10-04 13:43:26 -050051 [TestMethod]
csharptest71f662c2011-05-20 15:15:34 -050052 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
csharptesteac64a52011-10-04 13:43:26 -050062 [TestMethod]
csharptest71f662c2011-05-20 15:15:34 -050063 public void IsEmptyWhenEmpty()
64 {
65 Assert.IsTrue(ByteString.CopyFromUtf8("").IsEmpty);
66 }
67
csharptesteac64a52011-10-04 13:43:26 -050068 [TestMethod]
csharptest71f662c2011-05-20 15:15:34 -050069 public void IsEmptyWhenNotEmpty()
70 {
71 Assert.IsFalse(ByteString.CopyFromUtf8("X").IsEmpty);
72 }
73
csharptesteac64a52011-10-04 13:43:26 -050074 [TestMethod]
csharptest71f662c2011-05-20 15:15:34 -050075 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
csharptesteac64a52011-10-04 13:43:26 -050085 [TestMethod]
csharptest71f662c2011-05-20 15:15:34 -050086 public void ToByteArrayCopiesContents()
87 {
88 ByteString bs = ByteString.CopyFromUtf8("Hello");
89 byte[] data = bs.ToByteArray();
csharptesteac64a52011-10-04 13:43:26 -050090 Assert.AreEqual((byte)'H', data[0]);
91 Assert.AreEqual((byte)'H', bs[0]);
csharptest71f662c2011-05-20 15:15:34 -050092 data[0] = 0;
93 Assert.AreEqual(0, data[0]);
csharptesteac64a52011-10-04 13:43:26 -050094 Assert.AreEqual((byte)'H', bs[0]);
csharptest71f662c2011-05-20 15:15:34 -050095 }
96
csharptesteac64a52011-10-04 13:43:26 -050097 [TestMethod]
csharptest71f662c2011-05-20 15:15:34 -050098 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
csharptesteac64a52011-10-04 13:43:26 -0500107 [TestMethod]
csharptest71f662c2011-05-20 15:15:34 -0500108 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
csharptesteac64a52011-10-04 13:43:26 -0500117 [TestMethod]
csharptest71f662c2011-05-20 15:15:34 -0500118 public void ToStringUtf8()
119 {
120 ByteString bs = ByteString.CopyFromUtf8("\u20ac");
121 Assert.AreEqual("\u20ac", bs.ToStringUtf8());
122 }
123
csharptesteac64a52011-10-04 13:43:26 -0500124 [TestMethod]
csharptest71f662c2011-05-20 15:15:34 -0500125 public void ToStringWithExplicitEncoding()
126 {
127 ByteString bs = ByteString.CopyFrom("\u20ac", Encoding.Unicode);
128 Assert.AreEqual("\u20ac", bs.ToString(Encoding.Unicode));
129 }
130 }
131}