Skip to content

Commit

Permalink
objectのメソッドをオーバーライド refs #2
Browse files Browse the repository at this point in the history
  • Loading branch information
bleis-tift committed Oct 8, 2013
1 parent 053e225 commit 272454b
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 1 deletion.
25 changes: 25 additions & 0 deletions SeqExt.Tests/Mutable/VarArrayTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -217,5 +217,30 @@ public void 要素を反転できる(int[] xs, int[] expected)
array.ReverseThis();
Assert.That(array.ToArray(), Is.EqualTo(expected));
}

[TestCase(new string[0], "VarArray.Empty()")]
[TestCase(new[] { "" }, "VarArray()")]
[TestCase(new[] { "1", "", "2" }, "VarArray(1, , 2)")]
public void 文字列化できる(string[] xs, string expected)
{
var array = VarArray.Create(xs);
Assert.That(array.ToString(), Is.EqualTo(expected));
}

[TestCase(new int[0], new int[0], true)]
[TestCase(new int[0], new[] { 1 }, false)]
[TestCase(new[] { 1 }, new int[0], false)]
[TestCase(new[] { 1 }, new[] { 1 }, true)]
[TestCase(new[] { 1 }, new[] { 2 }, false)]
[TestCase(new[] { 1 }, new[] { 1, 0 }, false)]
[TestCase(new[] { 1, 2, 3 }, new[] { 1, 2, 3 }, true)]
[TestCase(new[] { 1, 2, 3 }, new[] { 1, 2 }, false)]
[TestCase(new[] { 1, 2, 3 }, new[] { 1, 2, 3, 0 }, false)]
public void VarArrayどうしの比較ができる(int[] xs, int[] ys, bool expected)
{
var array1 = VarArray.Create(xs);
var array2 = VarArray.Create(ys);
Assert.That(array1.Equals(array2), Is.EqualTo(expected));
}
}
}
62 changes: 61 additions & 1 deletion SeqExt/Mutable/VarArray.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public static VarArray<T> ToVarArray<T>(this Seq<T> self)
/// <summary>
/// 可変長配列です。
/// </summary>
public sealed class VarArray<T> : SeqBase<T>, RandomAccessSeq<T>
public sealed class VarArray<T> : SeqBase<T>, RandomAccessSeq<T>, IEquatable<VarArray<T>>
{
readonly System.Collections.Generic.List<T> value;

Expand Down Expand Up @@ -228,5 +228,65 @@ public void ReverseThis()
{
this.value.Reverse();
}

/// <summary>
/// 現在のオブジェクトが、同じ型の別のオブジェクトと等しいかどうかを判定します。
/// </summary>
/// <param name="other">このオブジェクトと比較する可変長配列</param>
/// <returns>現在のオブジェクトがotherで指定されたオブジェクトと等しい場合はtrue、それ以外の場合はfalse</returns>
public bool Equals(VarArray<T> other)
{
if (this.Size != other.Size)
return false;

for (int i = 0; i < this.Size; i++)
{
if (object.Equals(this[i], other[i]) == false)
return false;
}
return true;
}

/// <summary>使用しません。</summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public override bool Equals(object obj)
{
var other = obj as VarArray<T>;
if (other == null)
return false;
return this.Equals(other);
}

/// <summary>使用しません。</summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public override int GetHashCode()
{
var hash = 31;
foreach (var x in this)
hash ^= x == null ? 0 : x.GetHashCode();
return hash;
}

/// <summary>
/// このオブジェクトの文字列表現を取得します。
/// </summary>
public override string ToString()
{
if (this.Size == 0)
return "VarArray.Empty()";
var buf = new System.Text.StringBuilder("VarArray(");
foreach (var x in this)
buf.Append(x.ToStr()).Append(", ");
buf.Remove(buf.Length - 2, 2);
return buf.Append(")").ToString();
}

/// <summary>使用しません。</summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public static new bool Equals(object a, object b) { return object.Equals(a, b); }

/// <summary>使用しません。</summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public static new bool ReferenceEquals(object a, object b) { return object.ReferenceEquals(a, b); }
}
}

0 comments on commit 272454b

Please sign in to comment.