Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Develop alignment #43

Merged
merged 15 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions .whitesource
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"scanSettings": {
"baseBranches": []
},
"checkRunSettings": {
"vulnerableCheckRunConclusionLevel": "failure",
"displayMode": "diff",
"useMendCheckNames": true
},
"issueSettings": {
"minSeverityLevel": "LOW",
"issueType": "DEPENDENCY"
}
}
75 changes: 75 additions & 0 deletions SharpHelpers/SharpHelpers.UnitTest/Boolean/AtomicBooleanTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// (c) 2023 SharpCoding
// This code is licensed under MIT license (see LICENSE.txt for details)using System;

using System.Runtime.InteropServices.ComTypes;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using SharpCoding.SharpHelpers;

namespace SharpHelpers.UnitTest.Boolean
{
[TestClass]
public class AtomicBooleanTest
{
[TestMethod]
public void TestBool()
{
var ab1 = new AtomicBoolean();

var result = ab1 == true;
Assert.IsFalse(result);

ab1 = true;
result = ab1 == false;
Assert.IsFalse(result);

result = ab1 == true;
Assert.IsTrue(result);

ab1 = false;
result = true == ab1;
Assert.IsFalse(result);

var ab2 = new AtomicBoolean();

result = ab2 == ab1;
Assert.IsTrue(result);

}

[TestMethod]
public void TestAnd()
{
var t = new AtomicBoolean(true);
var f = new AtomicBoolean(false);

Assert.IsTrue(t.And(t));
Assert.IsFalse(f.And(f));
Assert.IsFalse(t.And(f));
Assert.IsFalse(f.And(t));
}

[TestMethod]
public void TestOr()
{
var t = new AtomicBoolean(true);
var f = new AtomicBoolean(false);

Assert.IsFalse(f.Or(f));
Assert.IsTrue(t.Or(t));
Assert.IsTrue(t.Or(f));
Assert.IsTrue(f.Or(t));
}

[TestMethod]
public void TestXor()
{
var t = new AtomicBoolean(true);
var f = new AtomicBoolean(false);

Assert.IsFalse(t.Xor(t));
Assert.IsFalse(f.Xor(f));
Assert.IsTrue(t.Xor(f));
Assert.IsTrue(f.Xor(t));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// (c) 2023 SharpCoding
// (c) 2023 SharpCoding
// This code is licensed under MIT license (see LICENSE.txt for details)

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using SharpCoding.SharpHelpers;
Expand Down Expand Up @@ -30,7 +29,7 @@ public void TestDateTimeSmart()
}

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

[TestMethod]
Expand Down
96 changes: 96 additions & 0 deletions SharpHelpers/SharpHelpers.UnitTest/Regex/RegexTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
// (c) 2019 SharpCoding
// This code is licensed under MIT license (see LICENSE.txt for details)
using Microsoft.VisualStudio.TestTools.UnitTesting;
using SharpCoding.SharpHelpers;

namespace SharpHelpers.UnitTest.Regex
{
[TestClass]
public class RegexTests
{
[TestMethod]
public void IsMatch_ShouldReturnTrue_WhenPatternMatches()
{
// Arrange
string input = "hello world";
string pattern = @"hello";

// Act
bool result = input.IsMatchRegex(pattern);

// Assert
Assert.IsTrue(result, "Expected the pattern to match the input string.");
}

[TestMethod]
public void IsMatch_ShouldReturnFalse_WhenPatternDoesNotMatch()
{
// Arrange
string input = "hello world";
string pattern = @"goodbye";

// Act
bool result = input.IsMatchRegex(pattern);

// Assert
Assert.IsFalse(result, "Expected the pattern not to match the input string.");
}

[TestMethod]
public void Match_ShouldReturnFirstMatch_WhenPatternMatches()
{
// Arrange
string input = "hello world";
string pattern = @"hello";

// Act
string result = input.Match(pattern);

// Assert
Assert.AreEqual("hello", result, "Expected the first match to be 'hello'.");
}

[TestMethod]
public void Match_ShouldReturnNull_WhenPatternDoesNotMatch()
{
// Arrange
string input = "hello world";
string pattern = @"goodbye";

// Act
string result = input.Match(pattern);

// Assert
Assert.IsNull(result, "Expected no match to be found.");
}

[TestMethod]
public void Replace_ShouldReplaceAllOccurrences_WhenPatternMatches()
{
// Arrange
string input = "hello world world";
string pattern = @"world";
string replacement = "universe";

// Act
string result = input.Replace(pattern, replacement);

// Assert
Assert.AreEqual("hello universe universe", result, "Expected 'world' to be replaced with 'universe'.");
}

[TestMethod]
public void Split_ShouldReturnArray_WhenPatternIsUsedAsDelimiter()
{
// Arrange
string input = "hello world universe";
string pattern = @" ";

// Act
string[] result = input.Split(pattern);

// Assert
CollectionAssert.AreEqual(new[] { "hello", "world", "universe" }, result, "Expected the input string to be split by spaces.");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.2" />
<PackageReference Include="MSTest.TestAdapter" Version="3.1.1" />
<PackageReference Include="MSTest.TestFramework" Version="3.1.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
<PackageReference Include="MSTest.TestAdapter" Version="3.5.2" />
<PackageReference Include="MSTest.TestFramework" Version="3.5.2" />
</ItemGroup>

<ItemGroup>
Expand Down
94 changes: 94 additions & 0 deletions SharpHelpers/SharpHelpers/AtomicBoolean.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// (c) 2023 SharpCoding
// This code is licensed under MIT license (see LICENSE.txt for details)using System;

using System;
using System.Data.Common;
using System.Threading;

namespace SharpCoding.SharpHelpers
{
/// <summary>
/// This class represents a boolean value that may be updated atomically.
/// It is designed to be thread-safe and can be used in multi-threaded environments
/// where atomic operations are required.
/// </summary>
public class AtomicBoolean : IEquatable<AtomicBoolean>
{
public static readonly AtomicBoolean False = new AtomicBoolean(false);
public static readonly AtomicBoolean True = new AtomicBoolean(true);

/// <summary>
/// A private integer field that holds the atomic boolean value. It uses 0 for false and 1 for true.
/// </summary>
private int _value;
/// <summary>
/// FALSE and TRUE: Private constants representing the integer values of false and true respectively.
/// </summary>
private const int FALSE = 0;
private const int TRUE = 1;

/// <summary>
/// The constructor that initializes the atomic boolean with a specified value.
/// It uses the Interlocked.Exchange method to safely set the initial value in a thread-safe manner.
/// </summary>
/// <param name="value">Initial value of the instance. Is false by default.</param>
public AtomicBoolean(bool value = false)
{
Interlocked.Exchange(ref _value, value ? TRUE : FALSE);
}

/// <summary>
/// A private property that gets the current boolean value of the atomic boolean.
/// It uses the Interlocked.CompareExchange method to safely get the current value in a thread-safe manner.
/// </summary>
private bool Value => Interlocked.CompareExchange(ref _value, FALSE, FALSE) == TRUE;

/// <summary>
/// An implicit conversion operator that allows an AtomicBoolean to be used where a bool is expected.
/// </summary>
/// <param name="abool"></param>
public static implicit operator bool(AtomicBoolean abool) => abool.Value;

/// <summary>
/// An implicit conversion operator that allows a bool to be used where an AtomicBoolean is expected.
/// </summary>
/// <param name="v"></param>
public static implicit operator AtomicBoolean(bool v) => new AtomicBoolean(v);

#region Equality members

/// <inheritdoc />
public bool Equals(AtomicBoolean other)
{
if (other is null)
return false;

if (ReferenceEquals(this, other))
return true;

return _value == other._value;
}

/// <inheritdoc />
public override bool Equals(object obj)
{
if (obj is null)
return false;

if (ReferenceEquals(this, obj))
return true;

return obj.GetType() == GetType() && Equals((AtomicBoolean)obj);
}

/// <inheritdoc />
// ReSharper disable once NonReadonlyMemberInGetHashCode
public override int GetHashCode() => _value;

public static bool operator ==(AtomicBoolean left, AtomicBoolean right) => Equals(left, right);

public static bool operator !=(AtomicBoolean left, AtomicBoolean right) => !Equals(left, right);

#endregion
}
}
65 changes: 65 additions & 0 deletions SharpHelpers/SharpHelpers/AtomicBooleanHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// (c) 2023 SharpCoding
// This code is licensed under MIT license (see LICENSE.txt for details)using System;

namespace SharpCoding.SharpHelpers
{
/// <summary>
/// The AtomicBooleanHelper class is a static class that provides extension methods for the AtomicBoolean class.
/// </summary>
public static class AtomicBooleanHelper
{
/// <summary>
/// This extension method performs a logical XOR operation on the instance and op2.
/// It returns true if the instance is different from op2.
/// </summary>
/// <param name="instance"></param>
/// <param name="op2"></param>
/// <returns></returns>
public static bool Xor(this AtomicBoolean instance, AtomicBoolean op2) => ((bool)instance).Xor(op2);

/// <summary>
/// This extension method performs a logical AND operation on the instance and op2.
/// It returns true if both the instance and op2 are true.
/// </summary>
/// <param name="instance"></param>
/// <param name="op2"></param>
/// <returns></returns>
public static bool And(this AtomicBoolean instance, AtomicBoolean op2) => ((bool)instance).And(op2);

/// <summary>
/// This extension method performs a logical OR operation on the instance and op2.
/// It returns true if either the instance that invokes the method or op2 is true.
/// It also returns true when both are true.
/// </summary>
/// <param name="instance"></param>
/// <param name="op2"></param>
/// <returns></returns>
public static bool Or(this AtomicBoolean instance, AtomicBoolean op2) => ((bool)instance).Or(op2);

/// <summary>
/// This extension method maps the instance value to one of the two parameters.
/// If the value is false or null, it returns falseValue; otherwise, it returns trueValue.
/// </summary>
/// <param name="value"></param>
/// <param name="trueValue"></param>
/// <param name="falseValue"></param>
/// <returns></returns>
//public static string ToStringValues(this AtomicBoolean value, string trueValue, string falseValue) => (value == null || value == false) ? falseValue : trueValue;
public static string ToStringValues(this AtomicBoolean value, string trueValue, string falseValue) => (value.GetValueOrDefault() == false) ? falseValue : trueValue;

/// <summary>
/// This extension method retrieves the value of the current AtomicBoolean object or the false value if the object is null.
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static AtomicBoolean GetValueOrDefault(this AtomicBoolean value) => value.GetValueOrDefault(AtomicBoolean.False);

/// <summary>
/// This extension method retrieves the value of the current AtomicBoolean object or the defaultValue if the object is null.
/// </summary>
/// <param name="value"></param>
/// <param name="defaultValue"></param>
/// <returns></returns>
public static AtomicBoolean GetValueOrDefault(this AtomicBoolean value, AtomicBoolean defaultValue) => value ?? defaultValue;
}
}
Loading
Loading