From bf2cd23f84bead6cc46f2292f311b178ed172051 Mon Sep 17 00:00:00 2001 From: TSR Berry <20988865+TSRBerry@users.noreply.github.com> Date: Sun, 16 Jul 2023 04:09:51 +0200 Subject: [PATCH] Add test utils --- src/Ryujinx.Tests.Memory/RandomUtils.cs | 77 +++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 src/Ryujinx.Tests.Memory/RandomUtils.cs diff --git a/src/Ryujinx.Tests.Memory/RandomUtils.cs b/src/Ryujinx.Tests.Memory/RandomUtils.cs new file mode 100644 index 000000000..030001b62 --- /dev/null +++ b/src/Ryujinx.Tests.Memory/RandomUtils.cs @@ -0,0 +1,77 @@ +using System; +using System.Collections.Generic; + +namespace Ryujinx.Tests +{ + public static class RandomUtils + { + public static bool NextBool(this Random random) + { + return random.Next(2) == 1; + } + + public static ushort NextUShort(this Random random) + { + return (ushort)random.Next(ushort.MaxValue); + } + + public static uint NextUInt(this Random random) + { + return (uint)random.NextInt64(uint.MaxValue); + } + + public static uint NextUInt(this Random random, uint to) + { + return (uint)random.NextInt64(to); + } + + public static uint NextUInt(this Random random, uint from, uint to) + { + return (uint)random.NextInt64(from, to); + } + + public static ulong NextULong(this Random random) + { + byte[] buffer = new byte[8]; + + random.NextBytes(buffer); + return BitConverter.ToUInt64(buffer); + } + + public static ulong NextULong(this Random random, ulong from, ulong to) + { + byte[] buffer = new byte[8]; + + random.NextBytes(buffer); + // NOTE: The result won't be perfectly random, but it should be random enough for tests + return BitConverter.ToUInt64(buffer) % (to + 1 - from) + from; + } + + public static byte NextByte(this Random random) + { + return (byte)random.Next(byte.MinValue, byte.MaxValue); + } + + public static byte NextByte(this Random random, byte to) + { + return (byte)random.Next(byte.MinValue, to); + } + + public static byte NextByte(this Random random, byte from, byte to) + { + return (byte)random.Next(from, to); + } + + public static IEnumerable NextUIntEnumerable(this Random random, int count) + { + List list = new(); + + for (int i = 0; i < count; i++) + { + list.Add(random.NextUInt()); + } + + return list.AsReadOnly(); + } + } +}