• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #region Copyright notice and license
2 // Protocol Buffers - Google's data interchange format
3 // Copyright 2008 Google Inc.  All rights reserved.
4 //
5 // Use of this source code is governed by a BSD-style
6 // license that can be found in the LICENSE file or at
7 // https://developers.google.com/open-source/licenses/bsd
8 #endregion
9 
10 using System;
11 using System.Text;
12 using NUnit.Framework;
13 
14 namespace Google.Protobuf
15 {
16     public class ByteStringTest
17     {
18         [Test]
Equality()19         public void Equality()
20         {
21             ByteString b1 = ByteString.CopyFrom(1, 2, 3);
22             ByteString b2 = ByteString.CopyFrom(1, 2, 3);
23             ByteString b3 = ByteString.CopyFrom(1, 2, 4);
24             ByteString b4 = ByteString.CopyFrom(1, 2, 3, 4);
25             EqualityTester.AssertEquality(b1, b1);
26             EqualityTester.AssertEquality(b1, b2);
27             EqualityTester.AssertInequality(b1, b3);
28             EqualityTester.AssertInequality(b1, b4);
29             EqualityTester.AssertInequality(b1, null);
30 #pragma warning disable 1718 // Deliberately calling ==(b1, b1) and !=(b1, b1)
31             Assert.IsTrue(b1 == b1);
32             Assert.IsTrue(b1 == b2);
33             Assert.IsFalse(b1 == b3);
34             Assert.IsFalse(b1 == b4);
35             Assert.IsFalse(b1 == null);
36             Assert.IsTrue((ByteString) null == null);
37             Assert.IsFalse(b1 != b1);
38             Assert.IsFalse(b1 != b2);
39 #pragma warning disable 1718
40             Assert.IsTrue(b1 != b3);
41             Assert.IsTrue(b1 != b4);
42             Assert.IsTrue(b1 != null);
43             Assert.IsFalse((ByteString) null != null);
44         }
45 
46         [Test]
EmptyByteStringHasZeroSize()47         public void EmptyByteStringHasZeroSize()
48         {
49             Assert.AreEqual(0, ByteString.Empty.Length);
50         }
51 
52         [Test]
CopyFromStringWithExplicitEncoding()53         public void CopyFromStringWithExplicitEncoding()
54         {
55             ByteString bs = ByteString.CopyFrom("AB", Encoding.Unicode);
56             Assert.AreEqual(4, bs.Length);
57             Assert.AreEqual(65, bs[0]);
58             Assert.AreEqual(0, bs[1]);
59             Assert.AreEqual(66, bs[2]);
60             Assert.AreEqual(0, bs[3]);
61         }
62 
63         [Test]
IsEmptyWhenEmpty()64         public void IsEmptyWhenEmpty()
65         {
66             Assert.IsTrue(ByteString.CopyFromUtf8("").IsEmpty);
67         }
68 
69         [Test]
IsEmptyWhenNotEmpty()70         public void IsEmptyWhenNotEmpty()
71         {
72             Assert.IsFalse(ByteString.CopyFromUtf8("X").IsEmpty);
73         }
74 
75         [Test]
CopyFromByteArrayCopiesContents()76         public void CopyFromByteArrayCopiesContents()
77         {
78             byte[] data = new byte[1];
79             data[0] = 10;
80             ByteString bs = ByteString.CopyFrom(data);
81             Assert.AreEqual(10, bs[0]);
82             data[0] = 5;
83             Assert.AreEqual(10, bs[0]);
84         }
85 
86         [Test]
ToByteArrayCopiesContents()87         public void ToByteArrayCopiesContents()
88         {
89             ByteString bs = ByteString.CopyFromUtf8("Hello");
90             byte[] data = bs.ToByteArray();
91             Assert.AreEqual((byte)'H', data[0]);
92             Assert.AreEqual((byte)'H', bs[0]);
93             data[0] = 0;
94             Assert.AreEqual(0, data[0]);
95             Assert.AreEqual((byte)'H', bs[0]);
96         }
97 
98         [Test]
CopyFromUtf8UsesUtf8()99         public void CopyFromUtf8UsesUtf8()
100         {
101             ByteString bs = ByteString.CopyFromUtf8("\u20ac");
102             Assert.AreEqual(3, bs.Length);
103             Assert.AreEqual(0xe2, bs[0]);
104             Assert.AreEqual(0x82, bs[1]);
105             Assert.AreEqual(0xac, bs[2]);
106         }
107 
108         [Test]
CopyFromPortion()109         public void CopyFromPortion()
110         {
111             byte[] data = new byte[] {0, 1, 2, 3, 4, 5, 6};
112             ByteString bs = ByteString.CopyFrom(data, 2, 3);
113             Assert.AreEqual(3, bs.Length);
114             Assert.AreEqual(2, bs[0]);
115             Assert.AreEqual(3, bs[1]);
116         }
117 
118         [Test]
ToStringUtf8()119         public void ToStringUtf8()
120         {
121             ByteString bs = ByteString.CopyFromUtf8("\u20ac");
122             Assert.AreEqual("\u20ac", bs.ToStringUtf8());
123         }
124 
125         [Test]
ToStringWithExplicitEncoding()126         public void ToStringWithExplicitEncoding()
127         {
128             ByteString bs = ByteString.CopyFrom("\u20ac", Encoding.Unicode);
129             Assert.AreEqual("\u20ac", bs.ToString(Encoding.Unicode));
130         }
131 
132         [Test]
FromBase64_WithText()133         public void FromBase64_WithText()
134         {
135             byte[] data = new byte[] {0, 1, 2, 3, 4, 5, 6};
136             string base64 = Convert.ToBase64String(data);
137             ByteString bs = ByteString.FromBase64(base64);
138             Assert.AreEqual(data, bs.ToByteArray());
139         }
140 
141         [Test]
FromBase64_Empty()142         public void FromBase64_Empty()
143         {
144             // Optimization which also fixes issue 61.
145             Assert.AreSame(ByteString.Empty, ByteString.FromBase64(""));
146         }
147     }
148 }