Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
/ corefx Public archive

Commit

Permalink
Random span-based API tests (#23692)
Browse files Browse the repository at this point in the history
* Random span extensions

* Random span tests

* random span tests

* Test configurations

* Fixed item group condition
  • Loading branch information
MaggieTsang authored Sep 7, 2017
1 parent 4341d5c commit 94a3498
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -907,6 +907,7 @@ public Random(int Seed) { }
public virtual int Next(int maxValue) { throw null; }
public virtual int Next(int minValue, int maxValue) { throw null; }
public virtual void NextBytes(byte[] buffer) { }
public virtual void NextBytes(Span<byte> buffer) { }
public virtual double NextDouble() { throw null; }
protected virtual double Sample() { throw null; }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@
<Compile Include="System\Reflection\AssemblyNameProxyTests.cs" />
<Compile Include="System\MarshalByRefObjectTest.cs" />
</ItemGroup>
<ItemGroup Condition="'$(TargetGroup)'=='netcoreapp'">
<Compile Include="System\Random.netcoreapp.cs" />
</ItemGroup>
<ItemGroup Condition="'$(TargetGroup)'!='netstandard'">
<Compile Include="System\BitConverterSpan.cs" />
<Compile Include="System\BitConverter.netcoreapp.cs" />
Expand Down Expand Up @@ -123,4 +126,4 @@
</ProjectReference>
</ItemGroup>
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.targets))\dir.targets" />
</Project>
</Project>
61 changes: 53 additions & 8 deletions src/System.Runtime.Extensions/tests/System/Random.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace System.Tests
{
public static class RandomTests
public static partial class RandomTests
{
[Fact]
public static void Unseeded()
Expand Down Expand Up @@ -53,14 +53,12 @@ public static void Seeded()
}
}

[Fact]
public static void ExpectedValues()
// Random has a predictable sequence of values it generates based on its seed.
// So that we'll be made aware if a change to the implementation causes these
// sequences to change, this test verifies the first few numbers for a few seeds.
private static int[][] Values()
{
// Random has a predictable sequence of values it generates based on its seed.
// So that we'll be made aware if a change to the implementation causes these
// sequences to change, this test verifies the first few numbers for a few seeds.

var expectedValues = new int[][]
var expectedValues = new int[][]
{
new int[] {1559595546, 1755192844, 1649316166, 1198642031, 442452829, 1200195957, 1945678308, 949569752, 2099272109, 587775847},
new int[] {534011718, 237820880, 1002897798, 1657007234, 1412011072, 929393559, 760389092, 2026928803, 217468053, 1379662799},
Expand All @@ -83,7 +81,13 @@ public static void ExpectedValues()
new int[] {278955818, 212301256, 751203777, 859281097, 714632027, 620720087, 2085308890, 1014679847, 439053806, 1956839101},
new int[] {1400855637, 842412939, 104785409, 1317646300, 1684190270, 349917689, 900019674, 2092038898, 704733397, 601242406},
};
return (expectedValues);
}

[Fact]
public static void ExpectedValues()
{
int[][] expectedValues = Values();
for (int seed = 0; seed < expectedValues.Length; seed++)
{
var r = new Random(seed);
Expand All @@ -94,6 +98,47 @@ public static void ExpectedValues()
}
}

private static byte[][] ByteValues()
{
var expectedValues = new byte[][]
{
new byte[] { 0x1A, 0xC, 0x46, 0x6F, 0x5D, 0x75, 0xE4, 0xD8, 0xAD, 0x67 },
new byte[] { 0x46, 0xD0, 0x86, 0x82, 0x40, 0x97, 0xE4, 0xA3, 0x95, 0xCF },
new byte[] { 0x71, 0x93, 0xC6, 0x95, 0x24, 0xB9, 0xE3, 0x6F, 0x7C, 0x38 },
new byte[] { 0x9D, 0x56, 0x5, 0xA9, 0x7, 0xDB, 0xE3, 0x3A, 0x63, 0xA0 },
new byte[] { 0xC8, 0x19, 0x45, 0xBC, 0xEB, 0xFD, 0xE2, 0x6, 0x4A, 0x8 },
new byte[] { 0xF4, 0xDD, 0x85, 0xCF, 0xCE, 0x1E, 0xE2, 0xD1, 0x31, 0x71 },
new byte[] { 0x1F, 0xA0, 0xC4, 0xE2, 0xB1, 0x40, 0xE1, 0x9D, 0x18, 0xD9 },
new byte[] { 0x4B, 0x63, 0x4, 0xF6, 0x95, 0x62, 0xE1, 0x68, 0xFF, 0x41 },
new byte[] { 0x76, 0x27, 0x44, 0x9, 0x78, 0x84, 0xE0, 0x34, 0xE6, 0xAA },
new byte[] { 0xA2, 0xEA, 0x84, 0x1C, 0x5C, 0xA6, 0xDF, 0xFF, 0xCE, 0x12 },
new byte[] { 0xCD, 0xAD, 0xC3, 0x2F, 0x3F, 0xC8, 0xDF, 0xCB, 0xB5, 0x7A },
new byte[] { 0xF9, 0x71, 0x3, 0x42, 0x23, 0xEA, 0xDE, 0x96, 0x9C, 0xE3 },
new byte[] { 0x24, 0x34, 0x43, 0x56, 0x6, 0xC, 0xDE, 0x62, 0x83, 0x4B },
new byte[] { 0x50, 0xF7, 0x82, 0x69, 0xEA, 0x2D, 0xDD, 0x2D, 0x6A, 0xB4 },
new byte[] { 0x7C, 0xBA, 0xC2, 0x7C, 0xCD, 0x4F, 0xDD, 0xF9, 0x51, 0x1C },
new byte[] { 0xA7, 0x7E, 0x2, 0x8F, 0xB0, 0x71, 0xDC, 0xC4, 0x38, 0x84 },
new byte[] { 0xD3, 0x41, 0x41, 0xA2, 0x94, 0x93, 0xDC, 0x90, 0x1F, 0xED },
new byte[] { 0xFE, 0x4, 0x81, 0xB6, 0x77, 0xB5, 0xDB, 0x5B, 0x7, 0x55 },
new byte[] { 0x2A, 0xC8, 0xC1, 0xC9, 0x5B, 0xD7, 0xDA, 0x27, 0xEE, 0xBD },
new byte[] { 0x55, 0x8B, 0x1, 0xDC, 0x3E, 0xF9, 0xDA, 0xF2, 0xD5, 0x26 }
};
return (expectedValues);
}

[Fact]
public static void ExpectedValues_NextBytesArray()
{
byte[][] expectedValues = ByteValues();
for (int seed = 0; seed < expectedValues.Length; seed++)
{
byte[] actualValues = new byte[expectedValues[seed].Length];
var r = new Random(seed);
r.NextBytes(actualValues);
Assert.Equal(expectedValues[seed], actualValues);
}
}

[Fact]
public static void Sample()
{
Expand Down
56 changes: 56 additions & 0 deletions src/System.Runtime.Extensions/tests/System/Random.netcoreapp.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using Xunit;

namespace System.Tests
{
public static partial class RandomTests
{
[Fact]
public static void Empty_Span()
{
int seed = Environment.TickCount;
Random r = new Random(seed);
r.NextBytes(Span<byte>.Empty);
}

[Fact]
public static void Seeded_Span()
{
int seed = Environment.TickCount;

Random r1 = new Random(seed);
Random r2 = new Random(seed);

Span<byte> s1 = new Span<byte>(new byte[1000]);
r1.NextBytes(s1);
Span<byte> s2 = new Span<byte>(new byte[1000]);
r2.NextBytes(s2);
for (int i = 0; i < s1.Length; i++)
{
Assert.Equal(s1[i], s2[i]);
}
for (int i = 0; i < s1.Length; i++)
{
int x1 = r1.Next();
int x2 = r2.Next();
Assert.Equal(x1, x2);
}
}

[Fact]
public static void ExpectedValues_NextBytesSpan()
{
byte[][] expectedValues = ByteValues();
for (int seed = 0; seed < expectedValues.Length; seed++)
{
byte[] actualValues = new byte[expectedValues[seed].Length];
var r = new Random(seed);
r.NextBytes(new Span<byte>(actualValues));
Assert.Equal(expectedValues[seed], actualValues);
}
}
}
}

0 comments on commit 94a3498

Please sign in to comment.