Skip to content

Commit 1a5a3d4

Browse files
Merge pull request #40 from Pepi/Fix-&-add
Fix & add
2 parents 1e395f9 + b92fbaf commit 1a5a3d4

File tree

6 files changed

+430
-2
lines changed

6 files changed

+430
-2
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// (c) 2023 SharpCoding
2+
// This code is licensed under MIT license (see LICENSE.txt for details)using System;
3+
4+
using System.Runtime.InteropServices.ComTypes;
5+
using Microsoft.VisualStudio.TestTools.UnitTesting;
6+
using SharpCoding.SharpHelpers;
7+
8+
namespace SharpHelpers.UnitTest.Boolean
9+
{
10+
[TestClass]
11+
public class AtomicBooleanTest
12+
{
13+
[TestMethod]
14+
public void TestBool()
15+
{
16+
var ab1 = new AtomicBoolean();
17+
18+
var result = ab1 == true;
19+
Assert.IsFalse(result);
20+
21+
ab1 = true;
22+
result = ab1 == false;
23+
Assert.IsFalse(result);
24+
25+
result = ab1 == true;
26+
Assert.IsTrue(result);
27+
28+
ab1 = false;
29+
result = true == ab1;
30+
Assert.IsFalse(result);
31+
32+
var ab2 = new AtomicBoolean();
33+
34+
result = ab2 == ab1;
35+
Assert.IsTrue(result);
36+
37+
}
38+
39+
[TestMethod]
40+
public void TestAnd()
41+
{
42+
var t = new AtomicBoolean(true);
43+
var f = new AtomicBoolean(false);
44+
45+
Assert.IsTrue(t.And(t));
46+
Assert.IsFalse(f.And(f));
47+
Assert.IsFalse(t.And(f));
48+
Assert.IsFalse(f.And(t));
49+
}
50+
51+
[TestMethod]
52+
public void TestOr()
53+
{
54+
var t = new AtomicBoolean(true);
55+
var f = new AtomicBoolean(false);
56+
57+
Assert.IsFalse(f.Or(f));
58+
Assert.IsTrue(t.Or(t));
59+
Assert.IsTrue(t.Or(f));
60+
Assert.IsTrue(f.Or(t));
61+
}
62+
63+
[TestMethod]
64+
public void TestXor()
65+
{
66+
var t = new AtomicBoolean(true);
67+
var f = new AtomicBoolean(false);
68+
69+
Assert.IsFalse(t.Xor(t));
70+
Assert.IsFalse(f.Xor(f));
71+
Assert.IsTrue(t.Xor(f));
72+
Assert.IsTrue(f.Xor(t));
73+
}
74+
}
75+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// (c) 2023 SharpCoding
2+
// This code is licensed under MIT license (see LICENSE.txt for details)
3+
4+
using System;
5+
using Microsoft.VisualStudio.TestTools.UnitTesting;
6+
using SharpCoding.SharpHelpers;
7+
using System.Linq;
8+
9+
10+
namespace SharpHelpers.UnitTest.DateAndTime
11+
{
12+
13+
14+
[TestClass]
15+
public class DateTimeTest
16+
{
17+
[TestMethod]
18+
public void TestDateTimeSmart()
19+
{
20+
var dtDictionary = new System.Collections.Generic.Dictionary<string, string>()
21+
{
22+
{"M/dd/yyyy hh:mm", "10/11/2023 09:00"},
23+
{"MM/dd/yyyy hh:mm:ss tt zzz", "05/01/2009 01:30:42 PM -05:00"},
24+
{"g", "10/11/2023 09:00"}
25+
};
26+
27+
DateTimeSmart.AddFormats(dtDictionary.Keys.ToArray());
28+
29+
foreach (var key in dtDictionary.Keys)
30+
{
31+
DateTime dt = (DateTimeSmart)dtDictionary[key];
32+
Assert.IsFalse(dt == DateTime.MinValue);
33+
}
34+
35+
DateTime dt1 = (DateTimeSmart)"01-05-2023 22:30:55";
36+
Assert.IsTrue(dt1 == new DateTime( 2023, 5, 1, 22, 30, 55, DateTimeKind.Local));
37+
38+
}
39+
40+
}
41+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
// (c) 2023 SharpCoding
2+
// This code is licensed under MIT license (see LICENSE.txt for details)using System;
3+
4+
using System;
5+
using System.Data.Common;
6+
using System.Threading;
7+
8+
namespace SharpCoding.SharpHelpers
9+
{
10+
/// <summary>
11+
/// This class represents a boolean value that may be updated atomically.
12+
/// It is designed to be thread-safe and can be used in multi-threaded environments
13+
/// where atomic operations are required.
14+
/// </summary>
15+
public class AtomicBoolean : IEquatable<AtomicBoolean>
16+
{
17+
public static readonly AtomicBoolean False = new AtomicBoolean(false);
18+
public static readonly AtomicBoolean True = new AtomicBoolean(true);
19+
20+
/// <summary>
21+
/// A private integer field that holds the atomic boolean value. It uses 0 for false and 1 for true.
22+
/// </summary>
23+
private int _value;
24+
/// <summary>
25+
/// FALSE and TRUE: Private constants representing the integer values of false and true respectively.
26+
/// </summary>
27+
private const int FALSE = 0;
28+
private const int TRUE = 1;
29+
30+
/// <summary>
31+
/// The constructor that initializes the atomic boolean with a specified value.
32+
/// It uses the Interlocked.Exchange method to safely set the initial value in a thread-safe manner.
33+
/// </summary>
34+
/// <param name="value">Initial value of the instance. Is false by default.</param>
35+
public AtomicBoolean(bool value = false)
36+
{
37+
Interlocked.Exchange(ref _value, value ? TRUE : FALSE);
38+
}
39+
40+
/// <summary>
41+
/// A private property that gets the current boolean value of the atomic boolean.
42+
/// It uses the Interlocked.CompareExchange method to safely get the current value in a thread-safe manner.
43+
/// </summary>
44+
private bool Value => Interlocked.CompareExchange(ref _value, FALSE, FALSE) == TRUE;
45+
46+
/// <summary>
47+
/// An implicit conversion operator that allows an AtomicBoolean to be used where a bool is expected.
48+
/// </summary>
49+
/// <param name="abool"></param>
50+
public static implicit operator bool(AtomicBoolean abool) => abool.Value;
51+
52+
/// <summary>
53+
/// An implicit conversion operator that allows a bool to be used where an AtomicBoolean is expected.
54+
/// </summary>
55+
/// <param name="v"></param>
56+
public static implicit operator AtomicBoolean(bool v) => new AtomicBoolean(v);
57+
58+
#region Equality members
59+
60+
/// <inheritdoc />
61+
public bool Equals(AtomicBoolean other)
62+
{
63+
if (other is null)
64+
return false;
65+
66+
if (ReferenceEquals(this, other))
67+
return true;
68+
69+
return _value == other._value;
70+
}
71+
72+
/// <inheritdoc />
73+
public override bool Equals(object obj)
74+
{
75+
if (obj is null)
76+
return false;
77+
78+
if (ReferenceEquals(this, obj))
79+
return true;
80+
81+
return obj.GetType() == GetType() && Equals((AtomicBoolean)obj);
82+
}
83+
84+
/// <inheritdoc />
85+
// ReSharper disable once NonReadonlyMemberInGetHashCode
86+
public override int GetHashCode() => _value;
87+
88+
public static bool operator ==(AtomicBoolean left, AtomicBoolean right) => Equals(left, right);
89+
90+
public static bool operator !=(AtomicBoolean left, AtomicBoolean right) => !Equals(left, right);
91+
92+
#endregion
93+
}
94+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// (c) 2023 SharpCoding
2+
// This code is licensed under MIT license (see LICENSE.txt for details)using System;
3+
4+
namespace SharpCoding.SharpHelpers
5+
{
6+
/// <summary>
7+
/// The AtomicBooleanHelper class is a static class that provides extension methods for the AtomicBoolean class.
8+
/// </summary>
9+
public static class AtomicBooleanHelper
10+
{
11+
/// <summary>
12+
/// This extension method performs a logical XOR operation on the instance and op2.
13+
/// It returns true if the instance is different from op2.
14+
/// </summary>
15+
/// <param name="instance"></param>
16+
/// <param name="op2"></param>
17+
/// <returns></returns>
18+
public static bool Xor(this AtomicBoolean instance, AtomicBoolean op2) => ((bool)instance).Xor(op2);
19+
20+
/// <summary>
21+
/// This extension method performs a logical AND operation on the instance and op2.
22+
/// It returns true if both the instance and op2 are true.
23+
/// </summary>
24+
/// <param name="instance"></param>
25+
/// <param name="op2"></param>
26+
/// <returns></returns>
27+
public static bool And(this AtomicBoolean instance, AtomicBoolean op2) => ((bool)instance).And(op2);
28+
29+
/// <summary>
30+
/// This extension method performs a logical OR operation on the instance and op2.
31+
/// It returns true if either the instance that invokes the method or op2 is true.
32+
/// It also returns true when both are true.
33+
/// </summary>
34+
/// <param name="instance"></param>
35+
/// <param name="op2"></param>
36+
/// <returns></returns>
37+
public static bool Or(this AtomicBoolean instance, AtomicBoolean op2) => ((bool)instance).Or(op2);
38+
39+
/// <summary>
40+
/// This extension method maps the instance value to one of the two parameters.
41+
/// If the value is false or null, it returns falseValue; otherwise, it returns trueValue.
42+
/// </summary>
43+
/// <param name="value"></param>
44+
/// <param name="trueValue"></param>
45+
/// <param name="falseValue"></param>
46+
/// <returns></returns>
47+
//public static string ToStringValues(this AtomicBoolean value, string trueValue, string falseValue) => (value == null || value == false) ? falseValue : trueValue;
48+
public static string ToStringValues(this AtomicBoolean value, string trueValue, string falseValue) => (value.GetValueOrDefault() == false) ? falseValue : trueValue;
49+
50+
/// <summary>
51+
/// This extension method retrieves the value of the current AtomicBoolean object or the false value if the object is null.
52+
/// </summary>
53+
/// <param name="value"></param>
54+
/// <returns></returns>
55+
public static AtomicBoolean GetValueOrDefault(this AtomicBoolean value) => value.GetValueOrDefault(AtomicBoolean.False);
56+
57+
/// <summary>
58+
/// This extension method retrieves the value of the current AtomicBoolean object or the defaultValue if the object is null.
59+
/// </summary>
60+
/// <param name="value"></param>
61+
/// <param name="defaultValue"></param>
62+
/// <returns></returns>
63+
public static AtomicBoolean GetValueOrDefault(this AtomicBoolean value, AtomicBoolean defaultValue) => value ?? defaultValue;
64+
}
65+
}

0 commit comments

Comments
 (0)