Skip to content

Commit 536df40

Browse files
Merge pull request #43 from sharpcode-it/engineering87-develop
Develop alignment
2 parents 2dfea79 + 581b918 commit 536df40

File tree

11 files changed

+674
-9
lines changed

11 files changed

+674
-9
lines changed

.whitesource

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"scanSettings": {
3+
"baseBranches": []
4+
},
5+
"checkRunSettings": {
6+
"vulnerableCheckRunConclusionLevel": "failure",
7+
"displayMode": "diff",
8+
"useMendCheckNames": true
9+
},
10+
"issueSettings": {
11+
"minSeverityLevel": "LOW",
12+
"issueType": "DEPENDENCY"
13+
}
14+
}
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+
}

SharpHelpers/SharpHelpers.UnitTest/DateAndTime/DateTimeTest.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
// (c) 2023 SharpCoding
1+
// (c) 2023 SharpCoding
22
// This code is licensed under MIT license (see LICENSE.txt for details)
3-
43
using System;
54
using Microsoft.VisualStudio.TestTools.UnitTesting;
65
using SharpCoding.SharpHelpers;
@@ -30,7 +29,7 @@ public void TestDateTimeSmart()
3029
}
3130

3231
DateTime dt1 = (DateTimeSmart)"01-05-2023 22:30:55";
33-
Assert.IsTrue(dt1 == new DateTime( 2023, 5, 1, 22, 30, 55, DateTimeKind.Local));
32+
Assert.IsTrue(dt1 == new DateTime(2023, 5, 1, 22, 30, 55, DateTimeKind.Local));
3433
}
3534

3635
[TestMethod]
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
// (c) 2019 SharpCoding
2+
// This code is licensed under MIT license (see LICENSE.txt for details)
3+
using Microsoft.VisualStudio.TestTools.UnitTesting;
4+
using SharpCoding.SharpHelpers;
5+
6+
namespace SharpHelpers.UnitTest.Regex
7+
{
8+
[TestClass]
9+
public class RegexTests
10+
{
11+
[TestMethod]
12+
public void IsMatch_ShouldReturnTrue_WhenPatternMatches()
13+
{
14+
// Arrange
15+
string input = "hello world";
16+
string pattern = @"hello";
17+
18+
// Act
19+
bool result = input.IsMatchRegex(pattern);
20+
21+
// Assert
22+
Assert.IsTrue(result, "Expected the pattern to match the input string.");
23+
}
24+
25+
[TestMethod]
26+
public void IsMatch_ShouldReturnFalse_WhenPatternDoesNotMatch()
27+
{
28+
// Arrange
29+
string input = "hello world";
30+
string pattern = @"goodbye";
31+
32+
// Act
33+
bool result = input.IsMatchRegex(pattern);
34+
35+
// Assert
36+
Assert.IsFalse(result, "Expected the pattern not to match the input string.");
37+
}
38+
39+
[TestMethod]
40+
public void Match_ShouldReturnFirstMatch_WhenPatternMatches()
41+
{
42+
// Arrange
43+
string input = "hello world";
44+
string pattern = @"hello";
45+
46+
// Act
47+
string result = input.Match(pattern);
48+
49+
// Assert
50+
Assert.AreEqual("hello", result, "Expected the first match to be 'hello'.");
51+
}
52+
53+
[TestMethod]
54+
public void Match_ShouldReturnNull_WhenPatternDoesNotMatch()
55+
{
56+
// Arrange
57+
string input = "hello world";
58+
string pattern = @"goodbye";
59+
60+
// Act
61+
string result = input.Match(pattern);
62+
63+
// Assert
64+
Assert.IsNull(result, "Expected no match to be found.");
65+
}
66+
67+
[TestMethod]
68+
public void Replace_ShouldReplaceAllOccurrences_WhenPatternMatches()
69+
{
70+
// Arrange
71+
string input = "hello world world";
72+
string pattern = @"world";
73+
string replacement = "universe";
74+
75+
// Act
76+
string result = input.Replace(pattern, replacement);
77+
78+
// Assert
79+
Assert.AreEqual("hello universe universe", result, "Expected 'world' to be replaced with 'universe'.");
80+
}
81+
82+
[TestMethod]
83+
public void Split_ShouldReturnArray_WhenPatternIsUsedAsDelimiter()
84+
{
85+
// Arrange
86+
string input = "hello world universe";
87+
string pattern = @" ";
88+
89+
// Act
90+
string[] result = input.Split(pattern);
91+
92+
// Assert
93+
CollectionAssert.AreEqual(new[] { "hello", "world", "universe" }, result, "Expected the input string to be split by spaces.");
94+
}
95+
}
96+
}

SharpHelpers/SharpHelpers.UnitTest/SharpHelpers.UnitTest.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
</PropertyGroup>
88

99
<ItemGroup>
10-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.2" />
11-
<PackageReference Include="MSTest.TestAdapter" Version="3.1.1" />
12-
<PackageReference Include="MSTest.TestFramework" Version="3.1.1" />
10+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
11+
<PackageReference Include="MSTest.TestAdapter" Version="3.5.2" />
12+
<PackageReference Include="MSTest.TestFramework" Version="3.5.2" />
1313
</ItemGroup>
1414

1515
<ItemGroup>
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)