Skip to content

Commit

Permalink
RandomizerBytes (#12)
Browse files Browse the repository at this point in the history
* Bytes

* ## Supported Random Data
  • Loading branch information
StefH committed Oct 10, 2019
1 parent b8cd385 commit 9ff448a
Show file tree
Hide file tree
Showing 11 changed files with 108 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
</PropertyGroup>

<PropertyGroup>
<VersionPrefix>1.0.9.0</VersionPrefix>
<VersionPrefix>1.0.10.0</VersionPrefix>
</PropertyGroup>
</Project>
2 changes: 1 addition & 1 deletion GitHubReleaseNotes.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
https://github.com/StefH/GitHubReleaseNotes

GitHubReleaseNotes.exe --output ReleaseNotes.md --skip-empty-releases --version 1.0.9.0
GitHubReleaseNotes.exe --output ReleaseNotes.md --skip-empty-releases --version 1.0.10.0
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ This is a simple generator to create random data.
- Email Addresses
- Guids
- DateTime
- Numbers (integer, long, float, double, ...)
- Numbers (integer, long, float, double, byte, ...)
- Bytes

## Usage

Expand Down
5 changes: 5 additions & 0 deletions src/ConsoleAppClassic/MainTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using RandomDataGenerator.FieldOptions;
using RandomDataGenerator.Randomizers;

Expand All @@ -11,6 +12,10 @@ public static class MainTest
{
public static void Run()
{
var randomizerBytes = RandomizerFactory.GetRandomizer(new FieldOptionsBytes { Min = 10, Max = 20 });
var base64 = randomizerBytes.GenerateAsBase64String();
Write(randomizerBytes, base64);

var randomizerTextRegex = RandomizerFactory.GetRandomizer(new FieldOptionsTextRegex { Pattern = @"^[1-9][0-9]{3}([A-RT-Z][A-Z]|[S][BCE-RT-Z])$" });
string textRegex = randomizerTextRegex.Generate();
Write(randomizerTextRegex, textRegex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace RandomDataGenerator.FieldOptions
{
#if !NETSTANDARD1_3
[XmlInclude(typeof(FieldOptionsBytes))]
[XmlInclude(typeof(FieldOptionsCity))]
[XmlInclude(typeof(FieldOptionsCountry))]
[XmlInclude(typeof(FieldOptionsDateTime))]
Expand Down
10 changes: 10 additions & 0 deletions src/RandomDataGenerator/FieldOptions/FieldOptionsBytes.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

namespace RandomDataGenerator.FieldOptions
{
public class FieldOptionsBytes : FieldOptionsAbstract, IFieldOptionsBytes
{
public int Min { get; set; }

public int Max { get; set; } = 1024;
}
}
9 changes: 9 additions & 0 deletions src/RandomDataGenerator/FieldOptions/IFieldOptionsBytes.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace RandomDataGenerator.FieldOptions
{
public interface IFieldOptionsBytes
{
int Min { get; set; }

int Max { get; set; }
}
}
16 changes: 16 additions & 0 deletions src/RandomDataGenerator/Generators/RandomValueGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,22 @@ public static double NextDouble()
return _rnf.NextDouble();
}

/// <summary>
/// Generates an array of bytes with random numbers.
/// </summary>
/// <param name="min">The minimum number of bytes</param>
/// <param name="max">The maximum number of bytes</param>
/// <returns>Random byte array</returns>
public static byte[] NextBytes(int min, int max)
{
int arrayLength = Next(min, max);

byte[] bytes = new byte[arrayLength];
_rnf.NextBytes(bytes);

return bytes;
}

/// <summary>
/// Return a random T in the range [min, max]
/// </summary>
Expand Down
18 changes: 18 additions & 0 deletions src/RandomDataGenerator/Randomizers/IRandomizerBytes.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System.Text;
using JetBrains.Annotations;

namespace RandomDataGenerator.Randomizers
{
public interface IRandomizerBytes
{
byte[] Generate();

string GenerateAsUTF8String();

string GenerateAsASCIIString();

string GenerateAsBase64String();

string GenerateAsString([CanBeNull] Encoding encoding = null);
}
}
40 changes: 40 additions & 0 deletions src/RandomDataGenerator/Randomizers/RandomizerBytes.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using RandomDataGenerator.FieldOptions;
using RandomDataGenerator.Generators;
using System;
using System.Text;

namespace RandomDataGenerator.Randomizers
{
public class RandomizerBytes : RandomizerAbstract<FieldOptionsBytes>, IRandomizerBytes
{
public RandomizerBytes(FieldOptionsBytes options)
: base(options)
{
}

public byte[] Generate()
{
return RandomValueGenerator.NextBytes(Options.Min, Options.Max);
}

public string GenerateAsString(Encoding encoding)
{
return (encoding ?? Encoding.UTF8).GetString(Generate());
}

public string GenerateAsUTF8String()
{
return GenerateAsString(Encoding.UTF8);
}

public string GenerateAsASCIIString()
{
return GenerateAsString(Encoding.ASCII);
}

public string GenerateAsBase64String()
{
return Convert.ToBase64String(Generate());
}
}
}
5 changes: 5 additions & 0 deletions src/RandomDataGenerator/Randomizers/RandomizerFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ public static class RandomizerFactory
{
private static readonly ConcurrentDictionary<string, object> Cache = new ConcurrentDictionary<string, object>();

public static IRandomizerBytes GetRandomizer(IFieldOptionsBytes fieldOptions)
{
return Create<IRandomizerBytes>(fieldOptions);
}

public static IRandomizerString GetRandomizer(IFieldOptionsString fieldOptions)
{
return Create<IRandomizerString>(fieldOptions);
Expand Down

0 comments on commit 9ff448a

Please sign in to comment.