Last active
September 12, 2020 05:58
-
-
Save sakapon/b104bcf4af76f5131fb2cfdb25cc4da0 to your computer and use it in GitHub Desktop.
OperatorsSample/Vector0 struct
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
namespace OperatorsLib.Structs | |
{ | |
public struct Vector0 | |
{ | |
public double X { get; } | |
public double Y { get; } | |
public Vector0(double x, double y) => (X, Y) = (x, y); | |
} | |
} |
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
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using Microsoft.VisualStudio.TestTools.UnitTesting; | |
using OperatorsLib.Structs; | |
namespace UnitTest.Structs | |
{ | |
[TestClass] | |
public class Vector0Test | |
{ | |
[TestMethod] | |
public void Equals() | |
{ | |
var v1 = new Vector0(3, 4); | |
var v2 = new Vector0(3, 4); | |
var ec = EqualityComparer<Vector0>.Default; | |
// 値型では、参照としての比較結果はつねに false です。 | |
// == および != 演算子は定義されていませんが、Equals で各プロパティを比較できます。 | |
Assert.IsFalse(ReferenceEquals(v1, v1)); | |
Assert.IsFalse((object)v1 == (object)v1); // ReferenceEquals と同じ | |
Assert.IsTrue(Equals(v1, v1)); | |
Assert.IsTrue(v1.Equals(v1)); | |
Assert.IsTrue(ec.Equals(v1, v1)); | |
//Assert.IsTrue(v1 == v1); // コンパイル エラー | |
Assert.IsFalse(ReferenceEquals(v1, v2)); | |
Assert.IsFalse((object)v1 == (object)v2); | |
Assert.IsTrue(Equals(v1, v2)); | |
Assert.IsTrue(v1.Equals(v2)); | |
Assert.IsTrue(ec.Equals(v1, v2)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment