Skip to content

Commit

Permalink
v1.5.11 refactoring and extra tests
Browse files Browse the repository at this point in the history
  • Loading branch information
smabuk committed Feb 15, 2024
1 parent 313f7f5 commit a8eebfb
Show file tree
Hide file tree
Showing 54 changed files with 887 additions and 671 deletions.
5 changes: 3 additions & 2 deletions src/Smab.Helpers/DateTimeHelpers/DateTimeExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
namespace Smab.Helpers;

public static class DateTimeExtensions {
internal static readonly string DD_MMM_YYYY = "dd MMM, yyyy";
internal static readonly string DD_MMMM_YYYY = "dd MMMM, yyyy";
private const string DD_MMM_YYYY = "dd MMM, yyyy";
private const string DD_MMMM_YYYY = "dd MMMM, yyyy";

public static string ToDateLongMonthYearString(this DateTime dateTime) => dateTime.ToString(DD_MMMM_YYYY);
public static string ToDateLongMonthYearString(this DateOnly dateOnly) => dateOnly.ToString(DD_MMMM_YYYY);

public static string ToDateShortMonthYearString(this DateTime dateTime) => dateTime.ToString(DD_MMM_YYYY);
public static string ToDateShortMonthYearString(this DateOnly dateOnly) => dateOnly.ToString(DD_MMM_YYYY);
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
namespace Smab.Helpers;

public static class HtmlHelper {

public static class HtmlHelpers {
public static bool HasClass(this string classString, string className)
=> classString.Split(' ').Contains(className, StringComparer.InvariantCultureIgnoreCase);

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System.Diagnostics.CodeAnalysis;

namespace Smab.Helpers;
namespace Smab.Helpers;
public interface ISimpleParsable<TSelf> : IParsable<TSelf> where TSelf: IParsable<TSelf> {

//
Expand Down
11 changes: 7 additions & 4 deletions src/Smab.Helpers/IoHelpers/DataInputCleanup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@ public static class DataInputCleanup {
/// </summary>
/// <param name="input"></param>
/// <returns>The input with the final string removed if it is empty or whitespace. Returns an empty array if null.</returns>
public static string StripTrailingBlankLineOrDefault(this string? input) => input.RemoveBlankLineFromEnd();
public static string[] StripTrailingBlankLineOrDefault(this string[]? input) => input.RemoveBlankLineFromEnd();
private static string[] RemoveBlankLineFromEnd(this string[]? input) {
public static string StripTrailingBlankLineOrDefault(this string? input, string defaultValue = "")
=> input is not null ? input.RemoveBlankLineFromEnd() : defaultValue;
public static string[] StripTrailingBlankLineOrDefault(this string[]? input, string[]? defaultValue = default)
=> input is not null ? input.RemoveBlankLineFromEnd() : (defaultValue is not null ? defaultValue : []);

public static string[] RemoveBlankLineFromEnd(this string[]? input) {
if (input is null) { return []; }

return string.IsNullOrWhiteSpace(input[^1]) ? input[..^1] : input;
}

private static string RemoveBlankLineFromEnd(this string? input) => input?.TrimEnd() ?? "";
public static string RemoveBlankLineFromEnd(this string? input) => input?.TrimEnd() ?? "";

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// Modified from https://source.dot.net/#System.Linq/System/Linq/Contains.cs
//
public static partial class ArrayHelpers {
public static partial class LinqHelpers {

public static bool DoesNotContain<TSource>(this IEnumerable<TSource> source, TSource value) =>
source is ICollection<TSource> collection ? !collection.Contains(value) :
Expand Down
4 changes: 4 additions & 0 deletions src/Smab.Helpers/LinqHelpers/IsIn.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
namespace Smab.Helpers;
public static partial class LinqHelpers {
public static bool IsIn<T>(this T value, IEnumerable<T> values) => values.Contains(value);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,9 @@

namespace Smab.Helpers;
public static partial class MathsHelpers {
public static bool IsIn<T>(this T value, IList<T> values) where T : INumber<T>
=> values.Contains(value);

public static bool IsIn<T>(this T value, HashSet<T> values) where T : INumber<T>
=> values.Contains(value);

public static bool IsIn<T>(this T value, IEnumerable<T> values) where T : INumber<T>
=> values.Contains(value);




public static bool IsInRange<T>(this T value, T minValue, T maxValueExclusive) where T : INumber<T>
=> minValue <= value && value < maxValueExclusive;

public static bool IsInRange<T>(this T value, (T MinValue, T MaxValueExclusive) range) where T : INumber<T>
=> range.MinValue <= value && value < range.MaxValueExclusive;

}
15 changes: 15 additions & 0 deletions src/Smab.Helpers/MathsHelpers/Mean.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System.Numerics;

namespace Smab.Helpers;

public static partial class MathsHelpers {

/// <summary>
/// Finds the mean average and returns it as a double
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="numbers"></param>
/// <returns></returns>
public static double Mean<T>(this IEnumerable<T> numbers) where T : INumber<T>
=> numbers.Select(n => Convert.ToDouble(n)).Average();
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,7 @@

namespace Smab.Helpers;

public static partial class AverageHelpers {

/// <summary>
/// Finds the mean average and returns it as a double
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="numbers"></param>
/// <returns></returns>
public static double Mean<T>(this IEnumerable<T> numbers) where T : INumber<T>
=> numbers.Select(n => Convert.ToDouble(n)).Average();

public static partial class MathsHelpers {
/// <summary>
/// Finds the Median value and returns it as double
/// </summary>
Expand Down Expand Up @@ -57,29 +47,4 @@ public static int Median(this IEnumerable<int> numbers) {
public static long Median(this IEnumerable<long> numbers) {
return Convert.ToInt64(numbers.ToArray().MedianAsDouble());
}


/// <summary>
/// Returns the values occuring the most times
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="array"></param>
/// <returns></returns>
public static IEnumerable<T> Modes<T>(this T[] array) {
(T Key, int Count)[] counts = array
.GroupBy(x => x)
.Select(g => (g.Key, Count: g.Count()))
.ToArray();

int maxCount = counts.Max(c => c.Count);

IEnumerable<T>? modes = counts
.Where(m => m.Count == maxCount)
.Select(item => item.Key);

foreach (T item in modes) {
yield return item;
}
}

}
27 changes: 27 additions & 0 deletions src/Smab.Helpers/MathsHelpers/Modes.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
namespace Smab.Helpers;

public static partial class MathsHelpers {
/// <summary>
/// Returns the values occurring the most times
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="array"></param>
/// <returns></returns>
public static IEnumerable<T> Modes<T>(this T[] array) {
(T Key, int Count)[] counts = array
.GroupBy(x => x)
.Select(g => (g.Key, Count: g.Count()))
.ToArray();

int maxCount = counts.Max(c => c.Count);

IEnumerable<T>? modes = counts
.Where(m => m.Count == maxCount)
.Select(item => item.Key);

foreach (T item in modes) {
yield return item;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Smab.Helpers;

public static class RangeHelpers {
public static partial class MathsHelpers {

public static bool TryGetOverlap<T>(this (T Start, T End) range, (T Start, T End) range2, out (T Start, T End) overlap) where T : INumber<T> {
overlap = default!;
Expand Down
2 changes: 1 addition & 1 deletion src/Smab.Helpers/MathsHelpers/Transpose.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace Smab.Helpers;
public static partial class MathsHelpers {
public static IEnumerable<Point> Transpose(this IEnumerable<Point> points) => points.Select(p => p.Transpose());
public static IEnumerable<Point> Transpose(this IEnumerable<Point> points) => points.Select(point => point.Transpose());

public static (T Item1, T Item2) Transpose<T>(this (T Item1, T Item2) item) => (item.Item2, item.Item1);
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
using System.Numerics;

namespace Smab.Helpers;
namespace Smab.Helpers;
public static partial class ParsingHelpers {
public static string AsBinaryFromHex(this string input)
=> string.Join(
string.Empty,
input.Select(c => Convert.ToString(Convert.ToInt32(c.ToString(), 16), 2).PadLeft(4, '0')));
public static string AsBinaryFromHex(this IEnumerable<string> input)
=> string.Join(string.Empty, input.Select(AsBinaryFromHex));


public static T FromBinaryAs<T>(this string input, char zeroChar = '.', char oneChar = '#') where T : INumber<T>
=> T.CreateChecked(T.Parse(input.Replace(zeroChar, '0').Replace(oneChar, '1'), System.Globalization.NumberStyles.BinaryNumber, null));
}
2 changes: 0 additions & 2 deletions src/Smab.Helpers/ParsingHelpers/AsCells.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,4 @@ public static IEnumerable<Cell<char>> AsCells(this IEnumerable<string> input, ch

public static IEnumerable<Cell<char>> AsCells(this string input, char[]? matches = null, int? cols = null, int? rows = null)
=> input.Split(Environment.NewLine).AsCells(matches, cols, rows);


}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace Smab.Helpers;

public static partial class RegexParsingHelpers {
public static partial class ParsingHelpers {
public static string As(this Match match, string groupName)
=> match.Groups[groupName].Value;

Expand All @@ -25,4 +25,3 @@ public static T As<T>(this Match match, string groupName, T? defaultIfInvalid =
public static T As<T>(this Match match, int index, T? defaultIfInvalid = default) where T : INumber<T>
=> T.TryParse(match.Groups[index].Value, null, out T? value) switch { true => value, false => defaultIfInvalid ?? T.Zero };
}

7 changes: 7 additions & 0 deletions src/Smab.Helpers/ParsingHelpers/FromBinaryAs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
using System.Numerics;

namespace Smab.Helpers;
public static partial class ParsingHelpers {
public static T FromBinaryAs<T>(this string input, char zeroChar = '.', char oneChar = '#') where T : INumber<T>
=> T.CreateChecked(T.Parse(input.Replace(zeroChar, '0').Replace(oneChar, '1'), System.Globalization.NumberStyles.BinaryNumber, null));
}
35 changes: 12 additions & 23 deletions src/Smab.Helpers/ParsingHelpers/TrimmedSplit.cs
Original file line number Diff line number Diff line change
@@ -1,26 +1,15 @@
namespace Smab.Helpers;
public static partial class ParsingHelpers {
public static string[] TrimmedSplit(this string s, string? separator = null) =>
s.Split(separator, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);

public static string[] TrimmedSplit(this string s, char[]? separator) =>
s.Split(separator, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);

public static string[] TrimmedSplit(this string s, char separator)
=> s.Split(separator, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);

public static string[] TrimmedSplit(this string s, char separator, int count)
=> s.Split(separator, count, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);

public static string[] TrimmedSplit(this string s, char[]? separator, int count)
=> s.Split(separator, count, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);

public static string[] TrimmedSplit(this string s, string[]? separator)
=> s.Split(separator, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);

public static string[] TrimmedSplit(this string s, string? separator, int count)
=> s.Split(separator, count, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);

public static string[] TrimmedSplit(this string s, string[]? separator, int count)
=> s.Split(separator, count, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
private const StringSplitOptions RemoveEmptyAndTrim = StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries;

public static string[] TrimmedSplit(this string s, string? separator = null) => s.Split(separator, RemoveEmptyAndTrim);

public static string[] TrimmedSplit(this string s, char separator) => s.Split(separator, RemoveEmptyAndTrim);
public static string[] TrimmedSplit(this string s, char[]? separator) => s.Split(separator, RemoveEmptyAndTrim);
public static string[] TrimmedSplit(this string s, string[]? separator) => s.Split(separator, RemoveEmptyAndTrim);

public static string[] TrimmedSplit(this string s, char separator, int count) => s.Split(separator, count, RemoveEmptyAndTrim);
public static string[] TrimmedSplit(this string s, char[]? separator, int count) => s.Split(separator, count, RemoveEmptyAndTrim);
public static string[] TrimmedSplit(this string s, string? separator, int count) => s.Split(separator, count, RemoveEmptyAndTrim);
public static string[] TrimmedSplit(this string s, string[]? separator, int count) => s.Split(separator, count, RemoveEmptyAndTrim);
}
4 changes: 0 additions & 4 deletions src/Smab.Helpers/ParsingHelpers/_ParsingHelpers.cs

This file was deleted.

2 changes: 1 addition & 1 deletion src/Smab.Helpers/Smab.Helpers.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

<PropertyGroup>
<PackageReleaseNotes>.NET 8.0</PackageReleaseNotes>
<VersionPrefix>1.5.10</VersionPrefix>
<VersionPrefix>1.5.11</VersionPrefix>
<Preview></Preview>
<VersionSuffix Condition="'$(Preview)' != '' And '$(BUILD_BUILDNUMBER)' == ''">$(Preview).$([System.DateTime]::get_Now().get_Year())$([System.DateTime]::get_Now().get_Month().ToString("D2"))$([System.DateTime]::get_Now().get_Day().ToString("D2"))-$([System.DateTime]::get_Now().get_Hour().ToString("D2"))$([System.DateTime]::get_Now().get_Minute().ToString("D2"))</VersionSuffix>
<VersionSuffix Condition="'$(Preview)' != '' And '$(BUILD_BUILDNUMBER)' != ''">$(Preview).$(BUILD_BUILDNUMBER)</VersionSuffix>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
namespace Smab.Helpers.Test.HelperMethodTests;
namespace Smab.Helpers.Test.AdventOfCodeHelperTests;

public class OcrHelperTests
{
public class OcrHelperTests {
[Theory]
[InlineData("""
.##..###...##..###..####.####..##..#..#.###...##.#..#.#.....##..###..###...###.#..#.#...#.####.
Expand Down Expand Up @@ -35,8 +34,7 @@ public class OcrHelperTests
█ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █
█ █ ███ ██ ███ ████ █ ███ █ █ ███ ██ █ █ ████ ██ █ █ █ ███ ██ █ ████
""", ' ', '█', "ABCDEFGHIJKLOPRSUYZ")]
public void OcrAlphabetString(string ocrAlphabet, char off, char on, string expected)
{
public void OcrAlphabetString(string ocrAlphabet, char off, char on, string expected) {
string actual = OcrHelpers.IdentifyMessage(ocrAlphabet, off, on, 1);
Assert.Equal(expected, actual);
}
Expand All @@ -54,8 +52,7 @@ public void OcrAlphabetString(string ocrAlphabet, char off, char on, string expe
█ █ █ █ █ ██ █ █ █ █ █ █ █ █ █
█ █ █████ ███ █ █ █ ███ █████ █ █ █
""", ' ', '█', "ABGXJBXF")]
public void OcrLargeAlphabetString(string ocrAlphabet, char off, char on, string expected)
{
public void OcrLargeAlphabetString(string ocrAlphabet, char off, char on, string expected) {
string actual = OcrHelpers.IdentifyMessage(ocrAlphabet, off, on, 2, OcrHelpers.OcrLetterSize.Large);
Assert.Equal(expected, actual);
}
Expand All @@ -81,8 +78,7 @@ public void OcrLargeAlphabetString(string ocrAlphabet, char off, char on, string
"#....#.#....#.#....#.#......#......#...##.#....#.#...#..#...#..#......#...##.#......#....#.#....#.#.....",
"#....#.#####...####..######.#.......###.#.#....#..###...#....#.######.#....#.#......#....#.#....#.######",
}, OcrHelpers.OcrLetterSize.Large, "ABCEFGHJKLNPRXZ")]
public void OcrAlphabetArray(string[] ocrAlphabet, OcrHelpers.OcrLetterSize letterSize, string expected)
{
public void OcrAlphabetArray(string[] ocrAlphabet, OcrHelpers.OcrLetterSize letterSize, string expected) {
string actual = OcrHelpers.IdentifyMessage(ocrAlphabet, '.', '#', 1, letterSize);
Assert.Equal(expected, actual);
}
Expand All @@ -96,8 +92,7 @@ public void OcrAlphabetArray(string[] ocrAlphabet, OcrHelpers.OcrLetterSize lett
#..#.#..#.#..#.#..#.#....#....#..#.#..#..#..#..#.#.#..#....#..#.#....#.#.....#.#..#...#...#..
#..#.###...##..###..####.#.....###.#..#.###..##..#..#.####..##..#....#..#.###...##....#...###
""", '.', '#', "ABCDEFGHIJKLOPRSUY")]
public void OcrAlphabetString_Bad_Length(string ocrAlphabet, char off, char on, string expected)
{
public void OcrAlphabetString_Bad_Length(string ocrAlphabet, char off, char on, string expected) {
string actual = OcrHelpers.IdentifyMessage(ocrAlphabet, off, on, 1);
Assert.Equal(expected, actual);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Smab.Helpers.Test.HelperMethodTests;
namespace Smab.Helpers.Test.AlgorithmHelperTests;
public class AlgorithmHelperTests(ITestOutputHelper testOutputHelper) {
[Theory]
[InlineData((int[])[2, 3], 0, 1)]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Smab.Helpers.Test.HelperMethodTests;
namespace Smab.Helpers.Test.AlgorithmHelperTests;

public class SequenceHelperTests {
[Theory]
Expand Down
14 changes: 14 additions & 0 deletions test/Smab.Helpers.Test/DateTimeHelpers/DateTimeHelperTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace Smab.Helpers.Test.DateTimeHelpers;
public class DateTimeHelperTests {
[Fact]
public void MonthYear_Formats_ShouldBe() {
DateTime dateTime = new(2024, 02, 15, 13, 01, 04);
DateOnly dateOnly = new(2024, 09, 15);

dateTime.ToDateLongMonthYearString().ShouldBe("15 February, 2024");
dateOnly.ToDateLongMonthYearString().ShouldBe("15 September, 2024");

dateTime.ToDateShortMonthYearString().ShouldBe("15 Feb, 2024");
dateOnly.ToDateShortMonthYearString().ShouldBe("15 Sept, 2024");
}
}
11 changes: 11 additions & 0 deletions test/Smab.Helpers.Test/GridHelperTests/Create2dArray.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace Smab.Helpers.Test.GridHelperTests;
public class Create2dArray {
[Fact]
public void Create2dArray_ShouldBe() {
char[,] array = ArrayHelpers.Create2dArray(5, 10, 'x');
Assert.Equal(5, array.GetUpperBound(0) + 1);
Assert.Equal(10, array.GetUpperBound(1) + 1);
Assert.Equal('x', array[0, 0]);
Assert.Equal('x', array[3, 5]);
}
}
Loading

0 comments on commit a8eebfb

Please sign in to comment.