aboutsummaryrefslogtreecommitdiff
path: root/csharp/src
diff options
context:
space:
mode:
Diffstat (limited to 'csharp/src')
-rw-r--r--csharp/src/ProtocolBuffers.Test/ByteStringTest.cs26
-rw-r--r--csharp/src/ProtocolBuffers/ByteString.cs35
2 files changed, 41 insertions, 20 deletions
diff --git a/csharp/src/ProtocolBuffers.Test/ByteStringTest.cs b/csharp/src/ProtocolBuffers.Test/ByteStringTest.cs
index dadd0d33..f445fcfe 100644
--- a/csharp/src/ProtocolBuffers.Test/ByteStringTest.cs
+++ b/csharp/src/ProtocolBuffers.Test/ByteStringTest.cs
@@ -39,6 +39,32 @@ namespace Google.Protobuf
public class ByteStringTest
{
[Test]
+ public void Equality()
+ {
+ ByteString b1 = ByteString.CopyFrom(1, 2, 3);
+ ByteString b2 = ByteString.CopyFrom(1, 2, 3);
+ ByteString b3 = ByteString.CopyFrom(1, 2, 4);
+ ByteString b4 = ByteString.CopyFrom(1, 2, 3, 4);
+ EqualityTester.AssertEquality(b1, b1);
+ EqualityTester.AssertEquality(b1, b2);
+ EqualityTester.AssertInequality(b1, b3);
+ EqualityTester.AssertInequality(b1, b4);
+ EqualityTester.AssertInequality(b1, null);
+ Assert.IsTrue(b1 == b1);
+ Assert.IsTrue(b1 == b2);
+ Assert.IsFalse(b1 == b3);
+ Assert.IsFalse(b1 == b4);
+ Assert.IsFalse(b1 == null);
+ Assert.IsTrue((ByteString) null == null);
+ Assert.IsFalse(b1 != b1);
+ Assert.IsFalse(b1 != b2);
+ Assert.IsTrue(b1 != b3);
+ Assert.IsTrue(b1 != b4);
+ Assert.IsTrue(b1 != null);
+ Assert.IsFalse((ByteString) null != null);
+ }
+
+ [Test]
public void EmptyByteStringHasZeroSize()
{
Assert.AreEqual(0, ByteString.Empty.Length);
diff --git a/csharp/src/ProtocolBuffers/ByteString.cs b/csharp/src/ProtocolBuffers/ByteString.cs
index 738f020e..329f47f6 100644
--- a/csharp/src/ProtocolBuffers/ByteString.cs
+++ b/csharp/src/ProtocolBuffers/ByteString.cs
@@ -212,11 +212,22 @@ namespace Google.Protobuf
{
return true;
}
- if (ReferenceEquals(lhs, null))
+ if (ReferenceEquals(lhs, null) || ReferenceEquals(rhs, null))
{
return false;
}
- return lhs.Equals(rhs);
+ if (lhs.bytes.Length != rhs.bytes.Length)
+ {
+ return false;
+ }
+ for (int i = 0; i < lhs.Length; i++)
+ {
+ if (rhs.bytes[i] != lhs.bytes[i])
+ {
+ return false;
+ }
+ }
+ return true;
}
public static bool operator !=(ByteString lhs, ByteString rhs)
@@ -228,12 +239,7 @@ namespace Google.Protobuf
public override bool Equals(object obj)
{
- ByteString other = obj as ByteString;
- if (obj == null)
- {
- return false;
- }
- return Equals(other);
+ return this == (obj as ByteString);
}
public override int GetHashCode()
@@ -248,18 +254,7 @@ namespace Google.Protobuf
public bool Equals(ByteString other)
{
- if (other.bytes.Length != bytes.Length)
- {
- return false;
- }
- for (int i = 0; i < bytes.Length; i++)
- {
- if (other.bytes[i] != bytes[i])
- {
- return false;
- }
- }
- return true;
+ return this == other;
}
/// <summary>