RandomTest.cs 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. //
  2. // System.Random Test Cases
  3. //
  4. // Author: Bob Smith <[email protected]>
  5. //
  6. using NUnit.Framework;
  7. using System;
  8. namespace MonoTests.System {
  9. public class RandomTest : TestCase
  10. {
  11. public RandomTest() {}
  12. public void TestDouble()
  13. {
  14. Random r = new Random();
  15. int i;
  16. double c=0;
  17. for (i=0; i<20; i++) c+=r.NextDouble();
  18. c/=i;
  19. Assert (c.ToString() + " is out of range.", c < .7 && c > .3);
  20. }
  21. public void TestSeed()
  22. {
  23. Random r = new Random(42);
  24. Random r2 = new Random(42);
  25. int i;
  26. double c=0, c2=0;
  27. for (i=0; i<20; i++)
  28. {
  29. c += r.NextDouble();
  30. c2 += r2.NextDouble();
  31. }
  32. AssertEquals(c, c2);
  33. }
  34. public void TestNext()
  35. {
  36. Random r = new Random();
  37. int i;
  38. long c;
  39. for (i=0; i<20; i++)
  40. {
  41. c = r.Next();
  42. Assert (c < Int32.MaxValue && c >= 0);
  43. }
  44. }
  45. public void TestNextMax()
  46. {
  47. Random r = new Random();
  48. int i;
  49. long c;
  50. for (i=0; i<20; i++)
  51. {
  52. c = r.Next(10);
  53. Assert (c < 10 && c >= 0);
  54. }
  55. }
  56. public void TestNextMinMax()
  57. {
  58. Random r = new Random();
  59. int i;
  60. long c;
  61. AssertEquals ("#1 Failed where min == max", 42, r.Next (42, 42));
  62. AssertEquals ("#2 Failed where min == max", Int32.MaxValue, r.Next (Int32.MaxValue,Int32.MaxValue));
  63. AssertEquals ("#3 Failed where min == max", Int32.MinValue, r.Next (Int32.MinValue,Int32.MinValue));
  64. AssertEquals ("#4 Failed where min == max", 0, r.Next (0, 0));
  65. for (i = 1; i <= Int32.MaxValue / 2; i *= 2)
  66. {
  67. c = r.Next (i, i * 2);
  68. Assert ("At i=" + i + " c < i*2 failed", c < i * 2);
  69. Assert ("At i=" + i + " c >= i failed", c >= i);
  70. }
  71. for (i = -1; i >= Int32.MinValue / 2; i *= 2)
  72. {
  73. c = r.Next (i * 2, i);
  74. Assert ("At i=" + i + " c < i*2 failed", c < i);
  75. Assert ("At i=" + i + " c >= i failed", c >= i * 2);
  76. }
  77. }
  78. }
  79. }