Tests.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. using NUnit.Framework;
  2. using System;
  3. using System.Runtime.InteropServices;
  4. namespace Ryujinx.Memory.Tests
  5. {
  6. public class Tests
  7. {
  8. private const ulong MemorySize = 0x8000;
  9. private MemoryBlock _memoryBlock;
  10. [SetUp]
  11. public void Setup()
  12. {
  13. _memoryBlock = new MemoryBlock(MemorySize);
  14. }
  15. [TearDown]
  16. public void Teardown()
  17. {
  18. _memoryBlock.Dispose();
  19. }
  20. [Test]
  21. public void Test_Read()
  22. {
  23. Marshal.WriteInt32(_memoryBlock.Pointer, 0x2020, 0x1234abcd);
  24. Assert.AreEqual(_memoryBlock.Read<int>(0x2020), 0x1234abcd);
  25. }
  26. [Test]
  27. public void Test_Write()
  28. {
  29. _memoryBlock.Write(0x2040, 0xbadc0de);
  30. Assert.AreEqual(Marshal.ReadInt32(_memoryBlock.Pointer, 0x2040), 0xbadc0de);
  31. }
  32. [Test, Explicit]
  33. public void Test_Alias()
  34. {
  35. using MemoryBlock backing = new MemoryBlock(0x10000, MemoryAllocationFlags.Mirrorable);
  36. using MemoryBlock toAlias = new MemoryBlock(0x10000, MemoryAllocationFlags.Reserve | MemoryAllocationFlags.ViewCompatible);
  37. toAlias.MapView(backing, 0x1000, 0, 0x4000);
  38. toAlias.UnmapView(backing, 0x3000, 0x1000);
  39. toAlias.Write(0, 0xbadc0de);
  40. Assert.AreEqual(Marshal.ReadInt32(backing.Pointer, 0x1000), 0xbadc0de);
  41. }
  42. [Test, Explicit]
  43. public void Test_AliasRandom()
  44. {
  45. using MemoryBlock backing = new MemoryBlock(0x80000, MemoryAllocationFlags.Mirrorable);
  46. using MemoryBlock toAlias = new MemoryBlock(0x80000, MemoryAllocationFlags.Reserve | MemoryAllocationFlags.ViewCompatible);
  47. Random rng = new Random(123);
  48. for (int i = 0; i < 20000; i++)
  49. {
  50. int srcPage = rng.Next(0, 64);
  51. int dstPage = rng.Next(0, 64);
  52. int pages = rng.Next(1, 65);
  53. if ((rng.Next() & 1) != 0)
  54. {
  55. toAlias.MapView(backing, (ulong)srcPage << 12, (ulong)dstPage << 12, (ulong)pages << 12);
  56. int offset = rng.Next(0, 0x1000 - sizeof(int));
  57. toAlias.Write((ulong)((dstPage << 12) + offset), 0xbadc0de);
  58. Assert.AreEqual(Marshal.ReadInt32(backing.Pointer, (srcPage << 12) + offset), 0xbadc0de);
  59. }
  60. else
  61. {
  62. toAlias.UnmapView(backing, (ulong)dstPage << 12, (ulong)pages << 12);
  63. }
  64. }
  65. }
  66. }
  67. }