-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #99 from MASSHUU12/create-more-random-extensions
Create more random extensions
- Loading branch information
Showing
9 changed files
with
355 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
using System; | ||
|
||
namespace Confirma.Extensions; | ||
|
||
public static class RandomBooleanExtensions | ||
{ | ||
public static bool NextBool(this Random rg) | ||
{ | ||
return rg.Next(0, 2) == 1; | ||
} | ||
|
||
public static bool? NextNullableBool(this Random rg) | ||
{ | ||
return rg.Next(0, 3) switch | ||
{ | ||
0 => false, | ||
1 => true, | ||
_ => null, | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Net; | ||
using System.Text; | ||
|
||
namespace Confirma.Extensions; | ||
|
||
public static class RandomNetworkExtensions | ||
{ | ||
private readonly static List<string> _domains = new() { | ||
"gmail.com", "yahoo.com", "hotmail.com", "outlook.com", "proton.me" | ||
}; | ||
|
||
public static IPAddress NextIPAddress(this Random rg) | ||
{ | ||
var data = new byte[4]; | ||
rg.NextBytes(data); | ||
|
||
data[0] |= 1; | ||
|
||
return new(data); | ||
} | ||
|
||
public static IPAddress NextIP6Address(this Random rg) | ||
{ | ||
var data = new byte[16]; | ||
rg.NextBytes(data); | ||
|
||
data[0] |= 1; | ||
|
||
return new(data); | ||
} | ||
|
||
public static string NextEmail(this Random rg, int minLength = 8, int maxLength = 12) | ||
{ | ||
if (minLength < 1 || maxLength < minLength) | ||
throw new ArgumentException("Invalid length parameters"); | ||
|
||
int length = rg.Next(minLength, maxLength + 1); | ||
var localPart = new StringBuilder(length); | ||
|
||
for (int i = 0; i < length; i++) | ||
{ | ||
if (i < length - 1 && rg.Next(6) == 0) | ||
localPart.Append(rg.Next(2) == 0 ? '-' : '_'); | ||
else | ||
{ | ||
if (rg.Next(2) == 0) | ||
localPart.Append((char)('a' + rg.Next(26))); | ||
else | ||
localPart.Append(rg.Next(10)); | ||
} | ||
} | ||
|
||
var domain = _domains.ElementAt(rg.Next(_domains.Count)); | ||
return $"{localPart}@{domain}"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
using System; | ||
using Confirma.Attributes; | ||
using Confirma.Extensions; | ||
|
||
namespace Confirma.Tests; | ||
|
||
[TestClass] | ||
[Parallelizable] | ||
public static class RandomBooleanTest | ||
{ | ||
private readonly static Random rg = new(); | ||
|
||
[Repeat(5)] | ||
[TestCase] | ||
public static void NextBool() | ||
{ | ||
const uint ITERATIONS = 100000; | ||
uint trueCount = 0; | ||
|
||
for (int i = 0; i < ITERATIONS; i++) if (rg.NextBool()) trueCount++; | ||
|
||
var truePercentage = (double)trueCount / ITERATIONS * 100; | ||
truePercentage.ConfirmCloseTo(50, 1); | ||
} | ||
|
||
[Repeat(5)] | ||
[TestCase] | ||
public static void NextNullableBool() | ||
{ | ||
const uint ITERATIONS = 100000; | ||
|
||
uint trueCount = 0; | ||
uint falseCount = 0; | ||
uint nullCount = 0; | ||
|
||
for (int i = 0; i < ITERATIONS; i++) | ||
{ | ||
bool? result = rg.NextNullableBool(); | ||
|
||
if (result == true) trueCount++; | ||
else if (result == false) falseCount++; | ||
else nullCount++; | ||
} | ||
|
||
var truePercentage = (double)trueCount / ITERATIONS * 100; | ||
var falsePercentage = (double)falseCount / ITERATIONS * 100; | ||
var nullPercentage = (double)nullCount / ITERATIONS * 100; | ||
|
||
nullPercentage.ConfirmCloseTo(33.33, 1); | ||
truePercentage.ConfirmCloseTo(33.33, 1); | ||
falsePercentage.ConfirmCloseTo(33.33, 1); | ||
} | ||
} |
Oops, something went wrong.