PRNG.t.sol 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // SPDX-License-Identifier: Apache 2
  2. pragma solidity ^0.8.0;
  3. import "@pythnetwork/entropy-sdk-solidity/PRNG.sol";
  4. import "forge-std/Test.sol";
  5. contract PRNGTestHelper is PRNG {
  6. constructor(bytes32 _seed) PRNG(_seed) {}
  7. function publicNextBytes32() public returns (bytes32) {
  8. return nextBytes32();
  9. }
  10. function publicRandUint() public returns (uint256) {
  11. return randUint();
  12. }
  13. function publicRandUint64() public returns (uint64) {
  14. return randUint64();
  15. }
  16. function publicRandUintRange(
  17. uint256 min,
  18. uint256 max
  19. ) public returns (uint256) {
  20. return randUintRange(min, max);
  21. }
  22. function publicRandomPermutation(
  23. uint256 length
  24. ) public returns (uint256[] memory) {
  25. return randomPermutation(length);
  26. }
  27. }
  28. contract PRNGTest is Test {
  29. PRNGTestHelper prng;
  30. function setUp() public {
  31. prng = new PRNGTestHelper(keccak256(abi.encode("initial seed")));
  32. }
  33. function testNextBytes32() public {
  34. bytes32 randomValue1 = prng.publicNextBytes32();
  35. bytes32 randomValue2 = prng.publicNextBytes32();
  36. assertNotEq(
  37. randomValue1,
  38. randomValue2,
  39. "Random values should not be equal"
  40. );
  41. }
  42. function testRandUint() public {
  43. uint256 randomValue1 = prng.publicRandUint();
  44. uint256 randomValue2 = prng.publicRandUint();
  45. assertNotEq(
  46. randomValue1,
  47. randomValue2,
  48. "Random values should not be equal"
  49. );
  50. }
  51. function testRandUint64() public {
  52. uint64 randomValue1 = prng.publicRandUint64();
  53. uint64 randomValue2 = prng.publicRandUint64();
  54. assertNotEq(
  55. randomValue1,
  56. randomValue2,
  57. "Random values should not be equal"
  58. );
  59. }
  60. function testRandUintRange() public {
  61. uint256 min = 10;
  62. uint256 max = 20;
  63. for (uint256 i = 0; i < 100; i++) {
  64. uint256 randomValue = prng.publicRandUintRange(min, max);
  65. assertGe(
  66. randomValue,
  67. min,
  68. "Random value should be greater than or equal to min"
  69. );
  70. assertLt(randomValue, max, "Random value should be less than max");
  71. }
  72. }
  73. function testRandomPermutation() public {
  74. uint256 length = 5;
  75. uint256[] memory permutation = prng.publicRandomPermutation(length);
  76. assertEq(permutation.length, length, "Permutation length should match");
  77. bool[] memory found = new bool[](length);
  78. for (uint256 i = 0; i < length; i++) {
  79. assertLt(
  80. permutation[i],
  81. length,
  82. "Permutation value should be within range"
  83. );
  84. found[permutation[i]] = true;
  85. }
  86. for (uint256 i = 0; i < length; i++) {
  87. assertTrue(found[i], "Permutation should contain all values");
  88. }
  89. }
  90. }