-
-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This adds `Equals()`, and `GetHashCode()` methods and `==/!=` operators for value structs and value unions. They can be enabled with the `--generate-methods` flag. These methods fix the analyzer warning: `CA1815: Override equals and operator equals on value types``
- Loading branch information
Showing
6 changed files
with
122 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
src/Tests/FlatSharpEndToEndTests/StructEquality/StructEquality.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
using FlatSharpEndToEndTests.Unions; | ||
|
||
namespace FlatSharpEndToEndTests.StructEquality; | ||
|
||
[TestClass] | ||
public class StructEqualityTests | ||
{ | ||
[TestMethod] | ||
public void ToTupleMethod() | ||
{ | ||
MixedValueStruct mixedValueStruct = new MixedValueStruct { X = 1, Y = 2f, Z = (ushort)5 }; | ||
|
||
var expectedValueStructTuple = (1, 2f, (ushort)5); | ||
|
||
Assert.AreEqual(expectedValueStructTuple, mixedValueStruct.ToTuple()); | ||
} | ||
|
||
[TestMethod] | ||
public void EqualsMethod() | ||
{ | ||
MixedValueStruct mixedValueStruct = new MixedValueStruct { X = 1, Y = 2f, Z = (ushort)5 }; | ||
StructUnion structUnion = new StructUnion(new A { V = 1 }); | ||
|
||
var mismatchedStruct = new MixedValueStruct { X = 20 }; | ||
var mismatchedUnion = new StructUnion(new B { V = 1 }); | ||
var mismatchedObject = "hi"; | ||
|
||
Assert.IsTrue(mixedValueStruct.Equals(new MixedValueStruct { X = 1, Y = 2f, Z = (ushort)5 })); | ||
Assert.IsFalse(mixedValueStruct.Equals(mismatchedStruct)); | ||
Assert.IsFalse(mixedValueStruct.Equals(mismatchedObject)); | ||
Assert.IsTrue(structUnion.Equals(new StructUnion(new A { V = 1 }))); | ||
Assert.IsFalse(structUnion.Equals(mismatchedUnion)); | ||
Assert.IsFalse(mixedValueStruct.Equals(mismatchedObject)); | ||
} | ||
|
||
[TestMethod] | ||
public void EqualityOperator() | ||
{ | ||
MixedValueStruct mixedValueStruct = new MixedValueStruct { X = 1, Y = 2f, Z = (ushort)5 }; | ||
StructUnion structUnion = new StructUnion(new A { V = 1 }); | ||
|
||
var mismatchedStruct = new MixedValueStruct { X = 10 }; | ||
var mismatchedUnion = new StructUnion(new B { V = 21 }); | ||
|
||
Assert.IsTrue(mixedValueStruct == new MixedValueStruct { X = 1, Y = 2f, Z = (ushort)5 }); | ||
Assert.IsFalse(mixedValueStruct == mismatchedStruct); | ||
Assert.IsTrue(structUnion == new StructUnion(new A { V = 1 })); | ||
Assert.IsFalse(structUnion == mismatchedUnion); | ||
} | ||
|
||
[TestMethod] | ||
public void InequalityOperator() | ||
{ | ||
MixedValueStruct mixedValueStruct = new MixedValueStruct { X = 1, Y = 2f, Z = (ushort)5 }; | ||
StructUnion structUnion = new StructUnion(new A { V = 1 }); | ||
|
||
var mismatchedStruct = new MixedValueStruct { Y = 13f }; | ||
var mismatchedUnion = new StructUnion(new C { V = 42 }); | ||
var mirrorStruct = mixedValueStruct; | ||
var mirrorUnion = structUnion; | ||
|
||
Assert.IsTrue(mixedValueStruct != mismatchedStruct); | ||
Assert.IsFalse(mixedValueStruct != mirrorStruct); | ||
Assert.IsTrue(structUnion != mismatchedUnion); | ||
Assert.IsFalse(structUnion != mirrorUnion); | ||
} | ||
|
||
[TestMethod] | ||
public void GetHashCodeMethod() | ||
{ | ||
MixedValueStruct mixedValueStruct = new MixedValueStruct { X = 1, Y = 2f, Z = (ushort)5 }; | ||
StructUnion structUnion = new StructUnion(new A { V = 1 }); | ||
|
||
Assert.AreEqual(new MixedValueStruct { X = 1, Y = 2f, Z = (ushort)5 }.GetHashCode(), mixedValueStruct.GetHashCode()); | ||
Assert.AreEqual(new StructUnion(new A { V = 1 }).GetHashCode(), structUnion.GetHashCode()); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/Tests/FlatSharpEndToEndTests/StructEquality/StructEquality.fbs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
attribute "fs_valueStruct"; | ||
|
||
namespace FlatSharpEndToEndTests.StructEquality; | ||
|
||
struct MixedValueStruct (fs_valueStruct) { X : int; Y : float; Z : ushort; } | ||
|
||
struct A (fs_valueStruct) { V : uint; } | ||
struct B (fs_valueStruct) { V : uint; } | ||
struct C (fs_valueStruct) { V : uint; } | ||
struct D (fs_valueStruct) { V : uint; } | ||
|
||
union StructUnion { A, B, C, D } |