Skip to content

Commit 9ff448a

Browse files
authored
RandomizerBytes (StefH#12)
* Bytes * ## Supported Random Data
1 parent b8cd385 commit 9ff448a

File tree

11 files changed

+108
-3
lines changed

11 files changed

+108
-3
lines changed

Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@
44
</PropertyGroup>
55

66
<PropertyGroup>
7-
<VersionPrefix>1.0.9.0</VersionPrefix>
7+
<VersionPrefix>1.0.10.0</VersionPrefix>
88
</PropertyGroup>
99
</Project>

GitHubReleaseNotes.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
https://github.com/StefH/GitHubReleaseNotes
22

3-
GitHubReleaseNotes.exe --output ReleaseNotes.md --skip-empty-releases --version 1.0.9.0
3+
GitHubReleaseNotes.exe --output ReleaseNotes.md --skip-empty-releases --version 1.0.10.0

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ This is a simple generator to create random data.
1919
- Email Addresses
2020
- Guids
2121
- DateTime
22-
- Numbers (integer, long, float, double, ...)
22+
- Numbers (integer, long, float, double, byte, ...)
23+
- Bytes
2324

2425
## Usage
2526

src/ConsoleAppClassic/MainTest.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.Linq;
44
using System.Reflection;
5+
using System.Text;
56
using RandomDataGenerator.FieldOptions;
67
using RandomDataGenerator.Randomizers;
78

@@ -11,6 +12,10 @@ public static class MainTest
1112
{
1213
public static void Run()
1314
{
15+
var randomizerBytes = RandomizerFactory.GetRandomizer(new FieldOptionsBytes { Min = 10, Max = 20 });
16+
var base64 = randomizerBytes.GenerateAsBase64String();
17+
Write(randomizerBytes, base64);
18+
1419
var randomizerTextRegex = RandomizerFactory.GetRandomizer(new FieldOptionsTextRegex { Pattern = @"^[1-9][0-9]{3}([A-RT-Z][A-Z]|[S][BCE-RT-Z])$" });
1520
string textRegex = randomizerTextRegex.Generate();
1621
Write(randomizerTextRegex, textRegex);

src/RandomDataGenerator/FieldOptions/FieldOptionsAbstract.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace RandomDataGenerator.FieldOptions
66
{
77
#if !NETSTANDARD1_3
8+
[XmlInclude(typeof(FieldOptionsBytes))]
89
[XmlInclude(typeof(FieldOptionsCity))]
910
[XmlInclude(typeof(FieldOptionsCountry))]
1011
[XmlInclude(typeof(FieldOptionsDateTime))]
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+

2+
namespace RandomDataGenerator.FieldOptions
3+
{
4+
public class FieldOptionsBytes : FieldOptionsAbstract, IFieldOptionsBytes
5+
{
6+
public int Min { get; set; }
7+
8+
public int Max { get; set; } = 1024;
9+
}
10+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace RandomDataGenerator.FieldOptions
2+
{
3+
public interface IFieldOptionsBytes
4+
{
5+
int Min { get; set; }
6+
7+
int Max { get; set; }
8+
}
9+
}

src/RandomDataGenerator/Generators/RandomValueGenerator.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,22 @@ public static double NextDouble()
5959
return _rnf.NextDouble();
6060
}
6161

62+
/// <summary>
63+
/// Generates an array of bytes with random numbers.
64+
/// </summary>
65+
/// <param name="min">The minimum number of bytes</param>
66+
/// <param name="max">The maximum number of bytes</param>
67+
/// <returns>Random byte array</returns>
68+
public static byte[] NextBytes(int min, int max)
69+
{
70+
int arrayLength = Next(min, max);
71+
72+
byte[] bytes = new byte[arrayLength];
73+
_rnf.NextBytes(bytes);
74+
75+
return bytes;
76+
}
77+
6278
/// <summary>
6379
/// Return a random T in the range [min, max]
6480
/// </summary>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using System.Text;
2+
using JetBrains.Annotations;
3+
4+
namespace RandomDataGenerator.Randomizers
5+
{
6+
public interface IRandomizerBytes
7+
{
8+
byte[] Generate();
9+
10+
string GenerateAsUTF8String();
11+
12+
string GenerateAsASCIIString();
13+
14+
string GenerateAsBase64String();
15+
16+
string GenerateAsString([CanBeNull] Encoding encoding = null);
17+
}
18+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using RandomDataGenerator.FieldOptions;
2+
using RandomDataGenerator.Generators;
3+
using System;
4+
using System.Text;
5+
6+
namespace RandomDataGenerator.Randomizers
7+
{
8+
public class RandomizerBytes : RandomizerAbstract<FieldOptionsBytes>, IRandomizerBytes
9+
{
10+
public RandomizerBytes(FieldOptionsBytes options)
11+
: base(options)
12+
{
13+
}
14+
15+
public byte[] Generate()
16+
{
17+
return RandomValueGenerator.NextBytes(Options.Min, Options.Max);
18+
}
19+
20+
public string GenerateAsString(Encoding encoding)
21+
{
22+
return (encoding ?? Encoding.UTF8).GetString(Generate());
23+
}
24+
25+
public string GenerateAsUTF8String()
26+
{
27+
return GenerateAsString(Encoding.UTF8);
28+
}
29+
30+
public string GenerateAsASCIIString()
31+
{
32+
return GenerateAsString(Encoding.ASCII);
33+
}
34+
35+
public string GenerateAsBase64String()
36+
{
37+
return Convert.ToBase64String(Generate());
38+
}
39+
}
40+
}

src/RandomDataGenerator/Randomizers/RandomizerFactory.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ public static class RandomizerFactory
1111
{
1212
private static readonly ConcurrentDictionary<string, object> Cache = new ConcurrentDictionary<string, object>();
1313

14+
public static IRandomizerBytes GetRandomizer(IFieldOptionsBytes fieldOptions)
15+
{
16+
return Create<IRandomizerBytes>(fieldOptions);
17+
}
18+
1419
public static IRandomizerString GetRandomizer(IFieldOptionsString fieldOptions)
1520
{
1621
return Create<IRandomizerString>(fieldOptions);

0 commit comments

Comments
 (0)