Skip to content

Commit 34578b5

Browse files
author
rstam
committed
Standardized implementation of IEquatable, !=, ==, Equals and GetHashCode for SafeMode.
1 parent 35966ae commit 34578b5

File tree

2 files changed

+156
-67
lines changed

2 files changed

+156
-67
lines changed

Driver/Core/SafeMode.cs

Lines changed: 68 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,14 @@ public bool Enabled
199199
set
200200
{
201201
if (_isFrozen) { ThrowFrozenException(); }
202-
_enabled = value;
202+
if (value)
203+
{
204+
_enabled = true;
205+
}
206+
else
207+
{
208+
ResetValues();
209+
}
203210
}
204211
}
205212

@@ -276,25 +283,25 @@ public TimeSpan WTimeout
276283

277284
// public operators
278285
/// <summary>
279-
/// Compares two SafeMode values.
286+
/// Determines whether two specified SafeMode objects have different values.
280287
/// </summary>
281-
/// <param name="lhs">The first SafeMode value.</param>
282-
/// <param name="rhs">The other SafeMode value.</param>
283-
/// <returns>True if the values are equal (or both null).</returns>
284-
public static bool operator ==(SafeMode lhs, SafeMode rhs)
288+
/// <param name="lhs">The first value to compare, or null.</param>
289+
/// <param name="rhs">The second value to compare, or null.</param>
290+
/// <returns>True if the value of lhs is different from the value of rhs; otherwise, false.</returns>
291+
public static bool operator !=(SafeMode lhs, SafeMode rhs)
285292
{
286-
return object.Equals(lhs, rhs);
293+
return !SafeMode.Equals(lhs, rhs);
287294
}
288295

289296
/// <summary>
290-
/// Compares two SafeMode values.
297+
/// Determines whether two specified SafeMode objects have the same value.
291298
/// </summary>
292-
/// <param name="lhs">The first SafeMode value.</param>
293-
/// <param name="rhs">The other SafeMode value.</param>
294-
/// <returns>True if the values are not equal (or one is null and the other is not).</returns>
295-
public static bool operator !=(SafeMode lhs, SafeMode rhs)
299+
/// <param name="lhs">The first value to compare, or null.</param>
300+
/// <param name="rhs">The second value to compare, or null.</param>
301+
/// <returns>True if the value of lhs is the same as the value of rhs; otherwise, false.</returns>
302+
public static bool operator ==(SafeMode lhs, SafeMode rhs)
296303
{
297-
return !(lhs == rhs);
304+
return SafeMode.Equals(lhs, rhs);
298305
}
299306

300307
// public static methods
@@ -382,6 +389,18 @@ public static SafeMode Create(int w, TimeSpan wtimeout)
382389
return Create(true, false, w, wtimeout);
383390
}
384391

392+
/// <summary>
393+
/// Determines whether two specified SafeMode objects have the same value.
394+
/// </summary>
395+
/// <param name="lhs">The first value to compare, or null.</param>
396+
/// <param name="rhs">The second value to compare, or null.</param>
397+
/// <returns>True if the value of lhs is the same as the value of rhs; otherwise, false.</returns>
398+
public static bool Equals(SafeMode lhs, SafeMode rhs)
399+
{
400+
if ((object)lhs == null) { return (object)rhs == null; }
401+
return lhs.Equals(rhs);
402+
}
403+
385404
// public methods
386405
/// <summary>
387406
/// Creates a clone of the SafeMode.
@@ -393,23 +412,24 @@ public SafeMode Clone()
393412
}
394413

395414
/// <summary>
396-
/// Compares two SafeMode values.
415+
/// Determines whether this instance and a specified object, which must also be a SafeMode object, have the same value.
397416
/// </summary>
398-
/// <param name="obj">The other SafeMode value.</param>
399-
/// <returns>True if the values are equal.</returns>
417+
/// <param name="obj">The SafeMode object to compare to this instance.</param>
418+
/// <returns>True if obj is a SafeMode object and its value is the same as this instance; otherwise, false.</returns>
400419
public override bool Equals(object obj)
401420
{
402421
return Equals(obj as SafeMode); // works even if obj is null or of a different type
403422
}
404423

405424
/// <summary>
406-
/// Compares two SafeMode values.
425+
/// Determines whether this instance and another specified SafeMode object have the same value.
407426
/// </summary>
408-
/// <param name="rhs">The other SafeMode value.</param>
409-
/// <returns>True if the values are equal.</returns>
427+
/// <param name="rhs">The SafeMode object to compare to this instance.</param>
428+
/// <returns>True if the value of the rhs parameter is the same as this instance; otherwise, false.</returns>
410429
public bool Equals(SafeMode rhs)
411430
{
412-
if (object.ReferenceEquals(rhs, null) || GetType() != rhs.GetType()) { return false; }
431+
if ((object)rhs == null || GetType() != rhs.GetType()) { return false; }
432+
if ((object)this == (object)rhs) { return true; }
413433
return
414434
_enabled == rhs._enabled &&
415435
_fsync == rhs._fsync &&
@@ -477,41 +497,45 @@ public override int GetHashCode()
477497
/// <returns>A string representation of the SafeMode.</returns>
478498
public override string ToString()
479499
{
480-
if (_enabled)
500+
var sb = new StringBuilder();
501+
sb.AppendFormat("safe={0}", _enabled ? "true" : "false");
502+
if (_fsync)
481503
{
482-
var sb = new StringBuilder("safe=true");
483-
if (_fsync)
484-
{
485-
sb.Append(",fsync=true");
486-
}
487-
if (_j)
504+
sb.Append(",fsync=true");
505+
}
506+
if (_j)
507+
{
508+
sb.Append(",j=true");
509+
}
510+
if (_w != 0 || _wmode != null)
511+
{
512+
if (_w != 0)
488513
{
489-
sb.Append(",j=true");
514+
sb.AppendFormat(",w={0}", _w);
490515
}
491-
if (_w != 0 || _wmode != null)
516+
if (_wmode != null)
492517
{
493-
if (_w != 0)
494-
{
495-
sb.AppendFormat(",w={0}", _w);
496-
}
497-
if (_wmode != null)
498-
{
499-
sb.AppendFormat(",wmode=\"{0}\"", _wmode);
500-
}
501-
if (_wtimeout != TimeSpan.Zero)
502-
{
503-
sb.AppendFormat(",wtimeout={0}", _wtimeout);
504-
}
518+
sb.AppendFormat(",wmode=\"{0}\"", _wmode);
505519
}
506-
return sb.ToString();
507520
}
508-
else
521+
if (_wtimeout != TimeSpan.Zero)
509522
{
510-
return "safe=false";
523+
sb.AppendFormat(",wtimeout={0}", _wtimeout);
511524
}
525+
return sb.ToString();
512526
}
513527

514528
// private methods
529+
private void ResetValues()
530+
{
531+
_enabled = false;
532+
_fsync = false;
533+
_j = false;
534+
_w = 0;
535+
_wmode = null;
536+
_wtimeout = TimeSpan.Zero;
537+
}
538+
515539
private void ThrowFrozenException()
516540
{
517541
throw new InvalidOperationException("SafeMode has been frozen and no further changes are allowed.");

DriverUnitTests/Core/SafeModeTests.cs

Lines changed: 88 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -122,29 +122,94 @@ public void TestCreateWithWMode()
122122
[Test]
123123
public void TestEquals()
124124
{
125-
var a = new SafeMode(false);
126-
var b = new SafeMode(false);
127-
var c = new SafeMode(true);
128-
var n = (SafeMode)null;
129-
130-
Assert.IsTrue(object.Equals(a, b));
131-
Assert.IsFalse(object.Equals(a, c));
132-
Assert.IsFalse(a.Equals(n));
133-
Assert.IsFalse(a.Equals(null));
134-
135-
Assert.IsTrue(a == b);
136-
Assert.IsFalse(a == c);
137-
Assert.IsFalse(a == null);
138-
Assert.IsFalse(null == a);
139-
Assert.IsTrue(n == null);
140-
Assert.IsTrue(null == n);
141-
142-
Assert.IsFalse(a != b);
143-
Assert.IsTrue(a != c);
144-
Assert.IsTrue(a != null);
145-
Assert.IsTrue(null != a);
146-
Assert.IsFalse(n != null);
147-
Assert.IsFalse(null != n);
125+
var a1 = new SafeMode(false);
126+
var a2 = new SafeMode(true) { Enabled = false };
127+
var a3 = a2;
128+
var b = new SafeMode(false) { Enabled = true };
129+
var c = new SafeMode(false) { FSync = true };
130+
var d = new SafeMode(false) { J = true };
131+
var e = new SafeMode(false) { W = 2 };
132+
var f = new SafeMode(false) { WMode = "mode" };
133+
var g = new SafeMode(false) { WTimeout = TimeSpan.FromMinutes(1) };
134+
var null1 = (SafeMode)null;
135+
var null2 = (SafeMode)null;
136+
137+
Assert.AreNotSame(a1, a2);
138+
Assert.AreSame(a2, a3);
139+
Assert.IsTrue(a1.Equals((object)a2));
140+
Assert.IsFalse(a1.Equals((object)null));
141+
Assert.IsFalse(a1.Equals((object)"x"));
142+
143+
Assert.IsTrue(a1 == a2);
144+
Assert.IsTrue(a2 == a3);
145+
Assert.IsFalse(a1 == b);
146+
Assert.IsFalse(a1 == c);
147+
Assert.IsFalse(a1 == d);
148+
Assert.IsFalse(a1 == e);
149+
Assert.IsFalse(a1 == f);
150+
Assert.IsFalse(a1 == g);
151+
Assert.IsFalse(a1 == null1);
152+
Assert.IsFalse(null1 == a1);
153+
Assert.IsTrue(null1 == null2);
154+
155+
Assert.IsFalse(a1 != a2);
156+
Assert.IsFalse(a2 != a3);
157+
Assert.IsTrue(a1 != b);
158+
Assert.IsTrue(a1 != c);
159+
Assert.IsTrue(a1 != d);
160+
Assert.IsTrue(a1 != e);
161+
Assert.IsTrue(a1 != f);
162+
Assert.IsTrue(a1 != g);
163+
Assert.IsTrue(a1 != null1);
164+
Assert.IsTrue(null1 != a1);
165+
Assert.IsFalse(null1 != null2);
166+
167+
var hash = a1.GetHashCode();
168+
Assert.AreEqual(hash, a2.GetHashCode());
169+
170+
// check that all tests still pass after objects are Frozen
171+
a1.Freeze();
172+
a2.Freeze();
173+
a3.Freeze();
174+
b.Freeze();
175+
c.Freeze();
176+
d.Freeze();
177+
e.Freeze();
178+
f.Freeze();
179+
g.Freeze();
180+
181+
Assert.AreNotSame(a1, a2);
182+
Assert.AreSame(a2, a3);
183+
Assert.IsTrue(a1.Equals((object)a2));
184+
Assert.IsFalse(a1.Equals((object)null));
185+
Assert.IsFalse(a1.Equals((object)"x"));
186+
187+
Assert.IsTrue(a1 == a2);
188+
Assert.IsTrue(a2 == a3);
189+
Assert.IsFalse(a1 == b);
190+
Assert.IsFalse(a1 == c);
191+
Assert.IsFalse(a1 == d);
192+
Assert.IsFalse(a1 == e);
193+
Assert.IsFalse(a1 == f);
194+
Assert.IsFalse(a1 == g);
195+
Assert.IsFalse(a1 == null1);
196+
Assert.IsFalse(null1 == a1);
197+
Assert.IsTrue(null1 == null2);
198+
199+
Assert.IsFalse(a1 != a2);
200+
Assert.IsFalse(a2 != a3);
201+
Assert.IsTrue(a1 != b);
202+
Assert.IsTrue(a1 != c);
203+
Assert.IsTrue(a1 != d);
204+
Assert.IsTrue(a1 != e);
205+
Assert.IsTrue(a1 != f);
206+
Assert.IsTrue(a1 != g);
207+
Assert.IsTrue(a1 != null1);
208+
Assert.IsTrue(null1 != a1);
209+
Assert.IsFalse(null1 != null2);
210+
211+
Assert.AreEqual(hash, a1.GetHashCode());
212+
Assert.AreEqual(hash, a2.GetHashCode());
148213
}
149214

150215
// CSHARP-386

0 commit comments

Comments
 (0)