PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/src/NUnit/framework/Attributes/RandomAttribute.cs

#
C# | 102 lines | 60 code | 10 blank | 32 comment | 2 complexity | 6cdd6b04fc58d4d29784d9c4cdcde84f MD5 | raw file
Possible License(s): GPL-2.0
  1. // ****************************************************************
  2. // Copyright 2008, Charlie Poole
  3. // This is free software licensed under the NUnit license. You may
  4. // obtain a copy of the license at http://nunit.org
  5. // ****************************************************************
  6. using System;
  7. using System.Collections;
  8. using System.Reflection;
  9. namespace NUnit.Framework
  10. {
  11. /// <summary>
  12. /// RandomAttribute is used to supply a set of random values
  13. /// to a single parameter of a parameterized test.
  14. /// </summary>
  15. public class RandomAttribute : ValuesAttribute
  16. {
  17. enum SampleType
  18. {
  19. Raw,
  20. IntRange,
  21. DoubleRange
  22. }
  23. SampleType sampleType;
  24. private int count;
  25. private int min, max;
  26. private double dmin, dmax;
  27. /// <summary>
  28. /// Construct a set of doubles from 0.0 to 1.0,
  29. /// specifying only the count.
  30. /// </summary>
  31. /// <param name="count"></param>
  32. public RandomAttribute(int count)
  33. {
  34. this.count = count;
  35. this.sampleType = SampleType.Raw;
  36. }
  37. /// <summary>
  38. /// Construct a set of doubles from min to max
  39. /// </summary>
  40. /// <param name="min"></param>
  41. /// <param name="max"></param>
  42. /// <param name="count"></param>
  43. public RandomAttribute(double min, double max, int count)
  44. {
  45. this.count = count;
  46. this.dmin = min;
  47. this.dmax = max;
  48. this.sampleType = SampleType.DoubleRange;
  49. }
  50. /// <summary>
  51. /// Construct a set of ints from min to max
  52. /// </summary>
  53. /// <param name="min"></param>
  54. /// <param name="max"></param>
  55. /// <param name="count"></param>
  56. public RandomAttribute(int min, int max, int count)
  57. {
  58. this.count = count;
  59. this.min = min;
  60. this.max = max;
  61. this.sampleType = SampleType.IntRange;
  62. }
  63. /// <summary>
  64. /// Get the collection of values to be used as arguments
  65. /// </summary>
  66. public override IEnumerable GetData(ParameterInfo parameter)
  67. {
  68. Randomizer r = Randomizer.GetRandomizer(parameter);
  69. IList values;
  70. switch (sampleType)
  71. {
  72. default:
  73. case SampleType.Raw:
  74. values = r.GetDoubles(count);
  75. break;
  76. case SampleType.IntRange:
  77. values = r.GetInts(min, max, count);
  78. break;
  79. case SampleType.DoubleRange:
  80. values = r.GetDoubles(dmin, dmax, count);
  81. break;
  82. }
  83. // Copy the random values into the data array
  84. // and call the base class which may need to
  85. // convert them to another type.
  86. this.data = new object[values.Count];
  87. for (int i = 0; i < values.Count; i++)
  88. this.data[i] = values[i];
  89. return base.GetData(parameter);
  90. }
  91. }
  92. }