Skip to content

Commit a7a9510

Browse files
committed
styled
1 parent b3e69ec commit a7a9510

File tree

16 files changed

+82
-85
lines changed

16 files changed

+82
-85
lines changed

tests/fuzzing/FuzzingTests.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,8 @@
2929
<ProjectReference Include="..\..\src\SpanExtensions.csproj" />
3030
</ItemGroup>
3131

32+
<ItemGroup>
33+
<Using Include="Xunit" />
34+
</ItemGroup>
35+
3236
</Project>

tests/fuzzing/GlobalUsings.cs

Lines changed: 0 additions & 1 deletion
This file was deleted.

tests/fuzzing/TestHelper.cs

Lines changed: 63 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
using SpanExtensions;
2-
using System.Collections;
1+
using System.Collections;
32
using System.Diagnostics;
43
using System.Globalization;
54
using System.Text;
5+
using SpanExtensions;
66

77
namespace SpanExtensions.Tests.Fuzzing
88
{
99
public static class TestHelper
1010
{
11-
public static readonly Random random = new();
11+
static readonly Random random = new();
1212

1313
/// <summary>
1414
/// Generates a sequence of a specified number of random integers that are in the specified range.
@@ -32,13 +32,13 @@ public static IEnumerable<int> GenerateRandomIntegers(int count, int minValue, i
3232
/// <returns>A random string.</returns>
3333
public static string GenerateRandomString(int length)
3434
{
35-
const string alphabet = "abcdefghijklmnopqrstuvwxyz0123456789 ";
35+
const string letters = "abcdefghijklmnopqrstuvwxyz0123456789 ";
3636

3737
StringBuilder builder = new(length);
3838
for(int i = 0; i < length; i++)
3939
{
40-
int alphabetIndex = random.Next(alphabet.Length);
41-
builder.Append(alphabet[alphabetIndex]);
40+
int alphabetIndex = random.Next(letters.Length);
41+
builder.Append(letters[alphabetIndex]);
4242
}
4343

4444
return builder.ToString();
@@ -49,34 +49,38 @@ public static string GenerateRandomString(int length)
4949
/// </summary>
5050
/// <typeparam name="T">The type of the sequence array.</typeparam>
5151
/// <param name="source">The target sequence.</param>
52-
/// <param name="default">The value to return if <paramref name="source"/> is empty.</param>
53-
/// <returns>A random element from <paramref name="source"/>, or <paramref name="default"/> if <paramref name="source"/> is empty.</returns>
54-
public static T RandomElementOrDefault<T>(this ReadOnlySpan<T> source, T @default = default) where T : struct
52+
/// <param name="defaultValue">The value to return if <paramref name="source"/> is empty.</param>
53+
/// <returns>A random element from <paramref name="source"/>, or <paramref name="defaultValue"/> if <paramref name="source"/> is empty.</returns>
54+
public static T RandomElementOrDefault<T>(this ReadOnlySpan<T> source, T defaultValue = default) where T : struct
5555
{
56-
return !source.IsEmpty ? source[random.Next(source.Length)] : @default;
56+
if(source.IsEmpty)
57+
{
58+
return defaultValue;
59+
}
60+
return source[random.Next(source.Length)];
5761
}
5862

5963
/// <summary>
6064
/// Get a random element from the specified array, or a specified default value if the array is empty.
6165
/// </summary>
6266
/// <typeparam name="T">The type of the target array.</typeparam>
6367
/// <param name="array">The target array.</param>
64-
/// <param name="default">The value to return if <paramref name="array"/> is empty.</param>
65-
/// <returns>A random element from <paramref name="array"/>, or <paramref name="default"/> if the array is empty.</returns>
66-
public static T RandomElementOrDefault<T>(this T[] array, T @default = default) where T : struct
68+
/// <param name="defaultValue">The value to return if <paramref name="array"/> is empty.</param>
69+
/// <returns>A random element from <paramref name="array"/>, or <paramref name="defaultValue"/> if the array is empty.</returns>
70+
public static T RandomElementOrDefault<T>(this T[] array, T defaultValue = default) where T : struct
6771
{
68-
return RandomElementOrDefault(array.AsReadOnlySpan(), @default);
72+
return RandomElementOrDefault(array.AsReadOnlySpan(), defaultValue);
6973
}
7074

7175
/// <summary>
7276
/// Get a random element from the specified string, or a specified default character if the string is empty.
7377
/// </summary>
74-
/// <param name="string">The target string.</param>
75-
/// <param name="default">The value to return if <paramref name="string"/> is empty.</param>
76-
/// <returns>A random element from <paramref name="string"/>, or <paramref name="default"/> if <paramref name="string"/> is empty.</returns>
77-
public static char RandomElementOrDefault(this string @string, char @default = default)
78+
/// <param name="source">The target string.</param>
79+
/// <param name="defaultValue">The value to return if <paramref name="source"/> is empty.</param>
80+
/// <returns>A random element from <paramref name="source"/>, or <paramref name="defaultValue"/> if <paramref name="source"/> is empty.</returns>
81+
public static char RandomElementOrDefault(this string source, char defaultValue = default)
7882
{
79-
return RandomElementOrDefault(@string.AsSpan(), @default);
83+
return RandomElementOrDefault(source.AsSpan(), defaultValue);
8084
}
8185

8286
/// <summary>
@@ -86,24 +90,24 @@ public static char RandomElementOrDefault(this string @string, char @default = d
8690
/// <typeparam name="T">The type of the target sequence.</typeparam>
8791
/// <param name="source">The target sequence.</param>
8892
/// <param name="length">The length of the subsequence.</param>
89-
/// <param name="default">The value to return if <paramref name="source"/> is empty or shorter than <paramref name="length"/>.</param>
90-
/// <returns>A random subsequence of length <paramref name="length"/>, or <paramref name="default"/> if <paramref name="source"/> is empty or shorted than that length.</returns>
93+
/// <param name="defaultValue">The value to return if <paramref name="source"/> is empty or shorter than <paramref name="length"/>.</param>
94+
/// <returns>A random subsequence of length <paramref name="length"/>, or <paramref name="defaultValue"/> if <paramref name="source"/> is empty or shorted than that length.</returns>
9195
/// <exception cref="ArgumentOutOfRangeException">If <paramref name="length"/> is negative.</exception>
92-
/// <exception cref="ArgumentException">If the length of <paramref name="default"/> did not match <paramref name="length"/>.</exception>
93-
public static T[] RandomSubsequenceOrDefault<T>(this ReadOnlySpan<T> source, int length, T[]? @default = null) where T : struct
96+
/// <exception cref="ArgumentException">If the length of <paramref name="defaultValue"/> did not match <paramref name="length"/>.</exception>
97+
public static T[] RandomSubsequenceOrDefault<T>(this ReadOnlySpan<T> source, int length, T[]? defaultValue = null) where T : struct
9498
{
9599
if(length < 0)
96100
{
97101
throw new ArgumentOutOfRangeException(nameof(length), "Can't be negative.");
98102
}
99-
if(@default != null && @default.Length != length)
103+
if(defaultValue != null && defaultValue.Length != length)
100104
{
101-
throw new ArgumentException("The length must match.", nameof(@default));
105+
throw new ArgumentException("The length must match.", nameof(defaultValue));
102106
}
103107

104108
if(source.Length < length)
105109
{
106-
return @default ?? new T[length];
110+
return defaultValue ?? new T[length];
107111
}
108112

109113
int startIndex = random.Next(source.Length - length);
@@ -117,28 +121,28 @@ public static T[] RandomSubsequenceOrDefault<T>(this ReadOnlySpan<T> source, int
117121
/// <typeparam name="T">The type of the target array.</typeparam>
118122
/// <param name="array">The target array.</param>
119123
/// <param name="length">The length of the subsequence.</param>
120-
/// <param name="default">The value to return if <paramref name="array"/> is empty or shorter than <paramref name="length"/>.</param>
121-
/// <returns>A random subsequence of length <paramref name="length"/>, or <paramref name="default"/> if the array is empty or shorted than that length.</returns>
124+
/// <param name="defaultValue">The value to return if <paramref name="array"/> is empty or shorter than <paramref name="length"/>.</param>
125+
/// <returns>A random subsequence of length <paramref name="length"/>, or <paramref name="defaultValue"/> if the array is empty or shorted than that length.</returns>
122126
/// <exception cref="ArgumentOutOfRangeException">If <paramref name="length"/> is negative.</exception>
123-
/// <exception cref="ArgumentException">If the length of <paramref name="default"/> did not match <paramref name="length"/>.</exception>
124-
public static T[] RandomSubsequenceOrDefault<T>(this T[] array, int length, T[]? @default = null) where T : struct
127+
/// <exception cref="ArgumentException">If the length of <paramref name="defaultValue"/> did not match <paramref name="length"/>.</exception>
128+
public static T[] RandomSubsequenceOrDefault<T>(this T[] array, int length, T[]? defaultValue = null) where T : struct
125129
{
126-
return RandomSubsequenceOrDefault(array.AsReadOnlySpan(), length, @default);
130+
return RandomSubsequenceOrDefault(array.AsReadOnlySpan(), length, defaultValue);
127131
}
128132

129133
/// <summary>
130134
/// Get a random element subsequence of the specified length from the specified string,
131135
/// or a specified default sequence if the source is empty or shorter than the specified length.
132136
/// </summary>
133-
/// <param name="string">The target string.</param>
137+
/// <param name="source">The target string.</param>
134138
/// <param name="length">The length of the string.</param>
135-
/// <param name="default">The value to return if <paramref name="string"/> is empty or shorter than <paramref name="length"/>.</param>
136-
/// <returns>A random subsequence of length <paramref name="length"/>, or <paramref name="default"/> if <paramref name="string"/> is empty or shorted than that length.</returns>
139+
/// <param name="defaultValue">The value to return if <paramref name="source"/> is empty or shorter than <paramref name="length"/>.</param>
140+
/// <returns>A random subsequence of length <paramref name="length"/>, or <paramref name="defaultValue"/> if <paramref name="source"/> is empty or shorted than that length.</returns>
137141
/// <exception cref="ArgumentOutOfRangeException">If <paramref name="length"/> is negative.</exception>
138-
/// <exception cref="ArgumentException">If the length of <paramref name="default"/> did not match <paramref name="length"/>.</exception>
139-
public static char[] RandomSubsequenceOrDefault(this string @string, int length, char[]? @default = null)
142+
/// <exception cref="ArgumentException">If the length of <paramref name="defaultValue"/> did not match <paramref name="length"/>.</exception>
143+
public static char[] RandomSubsequenceOrDefault(this string source, int length, char[]? defaultValue = null)
140144
{
141-
return RandomSubsequenceOrDefault(@string.AsSpan(), length, @default);
145+
return RandomSubsequenceOrDefault(source.AsSpan(), length, defaultValue);
142146
}
143147

144148
/// <summary>
@@ -421,13 +425,13 @@ public static int Count<T>(this T[] array, T value) where T : IEquatable<T>
421425
return array.AsSpan().Count(value);
422426
}
423427

424-
/// <summary>Counts the number of times the specified <paramref name="value"/> occurs in the <paramref name="string"/>.</summary>
425-
/// <param name="string">The string to search.</param>
428+
/// <summary>Counts the number of times the specified <paramref name="value"/> occurs in the <paramref name="source"/>.</summary>
429+
/// <param name="source">The string to search.</param>
426430
/// <param name="value">The value for which to search.</param>
427-
/// <returns>The number of times <paramref name="value"/> was found in the <paramref name="string"/>.</returns>
428-
public static int Count(this string @string, char value)
431+
/// <returns>The number of times <paramref name="value"/> was found in the <paramref name="source"/>.</returns>
432+
public static int Count(this string source, char value)
429433
{
430-
return @string.AsSpan().Count(value);
434+
return source.AsSpan().Count(value);
431435
}
432436

433437
#if !NET8_0_OR_GREATER
@@ -470,13 +474,13 @@ public static int Count<T>(this T[] array, ReadOnlySpan<T> values) where T : IEq
470474
return array.AsSpan().Count(values);
471475
}
472476

473-
/// <summary>Counts the number of times any of the specified <paramref name="values"/> occur in the <paramref name="string"/>.</summary>
474-
/// <param name="string">The string to search.</param>
477+
/// <summary>Counts the number of times any of the specified <paramref name="values"/> occur in the <paramref name="source"/>.</summary>
478+
/// <param name="source">The string to search.</param>
475479
/// <param name="values">The values for which to search.</param>
476-
/// <returns>The number of times any of the <paramref name="values"/> was found in the <paramref name="string"/>.</returns>
477-
public static int Count(this string @string, ReadOnlySpan<char> values)
480+
/// <returns>The number of times any of the <paramref name="values"/> was found in the <paramref name="source"/>.</returns>
481+
public static int Count(this string source, ReadOnlySpan<char> values)
478482
{
479-
return @string.AsSpan().Count(values);
483+
return source.AsSpan().Count(values);
480484
}
481485

482486
/// <summary>
@@ -517,12 +521,12 @@ public static int CountSubsequence<T>(this T[] array, ReadOnlySpan<T> subsequenc
517521
/// <summary>
518522
/// Counts the (non-overlaping) occurrences of a subsequence in the target string.
519523
/// </summary>
520-
/// <param name="string">The target string.</param>
524+
/// <param name="source">The target string.</param>
521525
/// <param name="subsequence">The subsequence to count.</param>
522-
/// <returns>The number of occurrences of <paramref name="subsequence"/> in <paramref name="string"/>.</returns>
523-
public static int CountSubsequence(this string @string, ReadOnlySpan<char> subsequence)
526+
/// <returns>The number of occurrences of <paramref name="subsequence"/> in <paramref name="source"/>.</returns>
527+
public static int CountSubsequence(this string source, ReadOnlySpan<char> subsequence)
524528
{
525-
return CountSubsequence(@string.AsSpan(), subsequence);
529+
return CountSubsequence(source.AsSpan(), subsequence);
526530
}
527531

528532
/// <summary>
@@ -531,13 +535,12 @@ public static int CountSubsequence(this string @string, ReadOnlySpan<char> subse
531535
/// <param name="countExceedingBehaviour">The <see cref="CountExceedingBehaviour"/> value that wasn't handled.</param>
532536
public static Exception UnhandledCaseException(CountExceedingBehaviour countExceedingBehaviour)
533537
{
534-
return new
535538
#if NET7_0_OR_GREATER
536-
UnreachableException
539+
return new UnreachableException($"Unhandled {nameof(CountExceedingBehaviour)} enum value: {countExceedingBehaviour}.");
537540
#else
538-
NotImplementedException
541+
return new InvalidOperationException($"Unhandled {nameof(CountExceedingBehaviour)} enum value: {countExceedingBehaviour}.");
539542
#endif
540-
($"Unhandled {nameof(CountExceedingBehaviour)} enum value: {countExceedingBehaviour}.");
543+
541544
}
542545

543546
/// <summary>
@@ -550,7 +553,11 @@ public static Exception UnhandledCaseException(CountExceedingBehaviour countExce
550553
/// <returns>The cut array.</returns>
551554
public static T[] UpTo<T>(this T[] source, int count)
552555
{
553-
return source.Length <= count ? source : source[..count];
556+
if(source.Length <= count)
557+
{
558+
return source;
559+
}
560+
return source[..count];
554561
}
555562

556563
/// <summary>

tests/fuzzing/Tests/ReadOnlySpan/Linq/Sum.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
using static SpanExtensions.Tests.Fuzzing.TestHelper;
2-
3-
namespace SpanExtensions.Tests.Fuzzing
1+
namespace SpanExtensions.Tests.Fuzzing
42
{
53
public static partial class ReadOnlySpanLinqTests
64
{

tests/fuzzing/Tests/ReadOnlySpan/Split/ReadOnlySpanSplitTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ namespace SpanExtensions.Tests.Fuzzing
55
public static partial class ReadOnlySpanSplitTests
66
{
77
static readonly IEnumerable<StringSplitOptions> stringSplitOptions = GetAllStringSplitOptions();
8-
static readonly CountExceedingBehaviour[] countExceedingBehaviours = (CountExceedingBehaviour[])Enum.GetValues(typeof(CountExceedingBehaviour));
8+
static readonly CountExceedingBehaviour[] countExceedingBehaviours = (CountExceedingBehaviour[]) Enum.GetValues(typeof(CountExceedingBehaviour));
99
}
1010
}

tests/fuzzing/Tests/ReadOnlySpan/Split/Split.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public static TheoryData<int, int, int, int> SplitData(int iterations)
1111
const int minValue = 0;
1212
const int maxValue = 100;
1313

14-
TheoryData<int, int, int, int> data = new();
14+
TheoryData<int, int, int, int> data = new TheoryData<int, int, int, int>();
1515

1616
foreach(int length in new MultiplierRange(1, 1000, 10).And([0]))
1717
{

tests/fuzzing/Tests/ReadOnlySpan/Split/SplitAny.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public static TheoryData<int, int, int, int, int, float> SplitAnyData(int iterat
1111
const int minValue = 0;
1212
const int maxValue = 100;
1313

14-
TheoryData<int, int, int, int, int, float> data = new();
14+
TheoryData<int, int, int, int, int, float> data = new TheoryData<int, int, int, int, int, float>();
1515

1616
foreach(int length in new MultiplierRange(1, 1000, 10).And([0]))
1717
{

tests/fuzzing/Tests/ReadOnlySpan/String/Remove.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
using static SpanExtensions.Tests.Fuzzing.TestHelper;
2-
3-
namespace SpanExtensions.Tests.Fuzzing
1+
namespace SpanExtensions.Tests.Fuzzing
42
{
53
public static partial class ReadOnlySpanStringTests
64
{

tests/fuzzing/Tests/Span/Linq/Sum.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
using static SpanExtensions.Tests.Fuzzing.TestHelper;
2-
3-
namespace SpanExtensions.Tests.Fuzzing
1+
namespace SpanExtensions.Tests.Fuzzing
42
{
53
public static partial class SpanLinqTests
64
{

tests/fuzzing/Tests/Span/String/Remove.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
using static SpanExtensions.Tests.Fuzzing.TestHelper;
2-
3-
namespace SpanExtensions.Tests.Fuzzing
1+
namespace SpanExtensions.Tests.Fuzzing
42
{
53
public static partial class SpanStringTests
64
{

0 commit comments

Comments
 (0)