Skip to content

Commit

Permalink
Merge pull request protocolbuffers#908 from jskeet/oneof-equality
Browse files Browse the repository at this point in the history
Use oneof case in equality tests
  • Loading branch information
jtattermusch committed Oct 24, 2015
2 parents dfae9e3 + 55313c9 commit d4569d1
Show file tree
Hide file tree
Showing 8 changed files with 151 additions and 81 deletions.
6 changes: 6 additions & 0 deletions csharp/src/Google.Protobuf.Conformance/Conformance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ public bool Equals(ConformanceRequest other) {
if (ProtobufPayload != other.ProtobufPayload) return false;
if (JsonPayload != other.JsonPayload) return false;
if (RequestedOutputFormat != other.RequestedOutputFormat) return false;
if (PayloadCase != other.PayloadCase) return false;
return true;
}

Expand All @@ -277,6 +278,7 @@ public override int GetHashCode() {
if (payloadCase_ == PayloadOneofCase.ProtobufPayload) hash ^= ProtobufPayload.GetHashCode();
if (payloadCase_ == PayloadOneofCase.JsonPayload) hash ^= JsonPayload.GetHashCode();
if (RequestedOutputFormat != global::Conformance.WireFormat.UNSPECIFIED) hash ^= RequestedOutputFormat.GetHashCode();
hash ^= (int) payloadCase_;
return hash;
}

Expand Down Expand Up @@ -512,6 +514,7 @@ public bool Equals(ConformanceResponse other) {
if (ProtobufPayload != other.ProtobufPayload) return false;
if (JsonPayload != other.JsonPayload) return false;
if (Skipped != other.Skipped) return false;
if (ResultCase != other.ResultCase) return false;
return true;
}

Expand All @@ -522,6 +525,7 @@ public override int GetHashCode() {
if (resultCase_ == ResultOneofCase.ProtobufPayload) hash ^= ProtobufPayload.GetHashCode();
if (resultCase_ == ResultOneofCase.JsonPayload) hash ^= JsonPayload.GetHashCode();
if (resultCase_ == ResultOneofCase.Skipped) hash ^= Skipped.GetHashCode();
hash ^= (int) resultCase_;
return hash;
}

Expand Down Expand Up @@ -1461,6 +1465,7 @@ public bool Equals(TestAllTypes other) {
if (!object.Equals(OneofNestedMessage, other.OneofNestedMessage)) return false;
if (OneofString != other.OneofString) return false;
if (OneofBytes != other.OneofBytes) return false;
if (OneofFieldCase != other.OneofFieldCase) return false;
return true;
}

Expand Down Expand Up @@ -1532,6 +1537,7 @@ public override int GetHashCode() {
if (oneofFieldCase_ == OneofFieldOneofCase.OneofNestedMessage) hash ^= OneofNestedMessage.GetHashCode();
if (oneofFieldCase_ == OneofFieldOneofCase.OneofString) hash ^= OneofString.GetHashCode();
if (oneofFieldCase_ == OneofFieldOneofCase.OneofBytes) hash ^= OneofBytes.GetHashCode();
hash ^= (int) oneofFieldCase_;
return hash;
}

Expand Down
10 changes: 10 additions & 0 deletions csharp/src/Google.Protobuf.Test/GeneratedMessageTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,16 @@ public void OneofProperties()
Assert.AreEqual(TestAllTypes.OneofFieldOneofCase.None, message.OneofFieldCase);
}

[Test]
public void Oneof_DefaultValuesNotEqual()
{
var message1 = new TestAllTypes { OneofString = "" };
var message2 = new TestAllTypes { OneofUint32 = 0 };
Assert.AreEqual(TestAllTypes.OneofFieldOneofCase.OneofString, message1.OneofFieldCase);
Assert.AreEqual(TestAllTypes.OneofFieldOneofCase.OneofUint32, message2.OneofFieldCase);
Assert.AreNotEqual(message1, message2);
}

[Test]
public void OneofSerialization_NonDefaultValue()
{
Expand Down
4 changes: 4 additions & 0 deletions csharp/src/Google.Protobuf.Test/TestProtos/UnittestIssues.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1260,6 +1260,8 @@ public bool Equals(TestJsonFieldOrdering other) {
if (PlainString != other.PlainString) return false;
if (O2Int32 != other.O2Int32) return false;
if (O2String != other.O2String) return false;
if (O1Case != other.O1Case) return false;
if (O2Case != other.O2Case) return false;
return true;
}

Expand All @@ -1271,6 +1273,8 @@ public override int GetHashCode() {
if (PlainString.Length != 0) hash ^= PlainString.GetHashCode();
if (o2Case_ == O2OneofCase.O2Int32) hash ^= O2Int32.GetHashCode();
if (o2Case_ == O2OneofCase.O2String) hash ^= O2String.GetHashCode();
hash ^= (int) o1Case_;
hash ^= (int) o2Case_;
return hash;
}

Expand Down
4 changes: 4 additions & 0 deletions csharp/src/Google.Protobuf.Test/TestProtos/UnittestProto3.cs
Original file line number Diff line number Diff line change
Expand Up @@ -868,6 +868,7 @@ public bool Equals(TestAllTypes other) {
if (!object.Equals(OneofNestedMessage, other.OneofNestedMessage)) return false;
if (OneofString != other.OneofString) return false;
if (OneofBytes != other.OneofBytes) return false;
if (OneofFieldCase != other.OneofFieldCase) return false;
return true;
}

Expand Down Expand Up @@ -921,6 +922,7 @@ public override int GetHashCode() {
if (oneofFieldCase_ == OneofFieldOneofCase.OneofNestedMessage) hash ^= OneofNestedMessage.GetHashCode();
if (oneofFieldCase_ == OneofFieldOneofCase.OneofString) hash ^= OneofString.GetHashCode();
if (oneofFieldCase_ == OneofFieldOneofCase.OneofBytes) hash ^= OneofBytes.GetHashCode();
hash ^= (int) oneofFieldCase_;
return hash;
}

Expand Down Expand Up @@ -4470,6 +4472,7 @@ public bool Equals(TestOneof other) {
if (FooInt != other.FooInt) return false;
if (FooString != other.FooString) return false;
if (!object.Equals(FooMessage, other.FooMessage)) return false;
if (FooCase != other.FooCase) return false;
return true;
}

Expand All @@ -4478,6 +4481,7 @@ public override int GetHashCode() {
if (fooCase_ == FooOneofCase.FooInt) hash ^= FooInt.GetHashCode();
if (fooCase_ == FooOneofCase.FooString) hash ^= FooString.GetHashCode();
if (fooCase_ == FooOneofCase.FooMessage) hash ^= FooMessage.GetHashCode();
hash ^= (int) fooCase_;
return hash;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1596,6 +1596,7 @@ public bool Equals(OneofWellKnownTypes other) {
if (BoolField != other.BoolField) return false;
if (StringField != other.StringField) return false;
if (BytesField != other.BytesField) return false;
if (OneofFieldCase != other.OneofFieldCase) return false;
return true;
}

Expand All @@ -1619,6 +1620,7 @@ public override int GetHashCode() {
if (oneofFieldCase_ == OneofFieldOneofCase.BoolField) hash ^= BoolField.GetHashCode();
if (oneofFieldCase_ == OneofFieldOneofCase.StringField) hash ^= StringField.GetHashCode();
if (oneofFieldCase_ == OneofFieldOneofCase.BytesField) hash ^= BytesField.GetHashCode();
hash ^= (int) oneofFieldCase_;
return hash;
}

Expand Down
Loading

0 comments on commit d4569d1

Please sign in to comment.