1 // ***********************************************************************
2 // Copyright (c) 2008-2015 Charlie Poole
4 // Permission is hereby granted, free of charge, to any person obtaining
5 // a copy of this software and associated documentation files (the
6 // "Software"), to deal in the Software without restriction, including
7 // without limitation the rights to use, copy, modify, merge, publish,
8 // distribute, sublicense, and/or sell copies of the Software, and to
9 // permit persons to whom the Software is furnished to do so, subject to
10 // the following conditions:
12 // The above copyright notice and this permission notice shall be
13 // included in all copies or substantial portions of the Software.
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 // ***********************************************************************
25 #define NUNIT_FRAMEWORK
30 using System.Collections;
32 namespace NUnit.Framework
35 /// RangeAttribute is used to supply a range of _values to an
36 /// individual parameter of a parameterized test.
38 public class RangeAttribute : ValuesAttribute
43 /// Construct a range of ints using default step of 1
45 /// <param name="from"></param>
46 /// <param name="to"></param>
47 public RangeAttribute(int from, int to) : this(from, to, from > to ? -1 : 1) { }
50 /// Construct a range of ints specifying the step size
52 /// <param name="from"></param>
53 /// <param name="to"></param>
54 /// <param name="step"></param>
55 public RangeAttribute(int from, int to, int step)
57 Guard.ArgumentValid(step > 0 && to >= from || step < 0 && to <= from,
58 "Step must be positive with to >= from or negative with to <= from", "step");
60 int count = (to - from) / step + 1;
61 this.data = new object[count];
63 for (int val = from; index < count; val += step)
64 this.data[index++] = val;
72 /// Construct a range of unsigned ints using default step of 1
74 /// <param name="from"></param>
75 /// <param name="to"></param>
76 //[CLSCompliant(false)]
77 public RangeAttribute(uint from, uint to) : this(from, to, 1u) { }
80 /// Construct a range of unsigned ints specifying the step size
82 /// <param name="from"></param>
83 /// <param name="to"></param>
84 /// <param name="step"></param>
85 //[CLSCompliant(false)]
86 public RangeAttribute(uint from, uint to, uint step)
88 Guard.ArgumentValid(step > 0, "Step must be greater than zero", "step");
89 Guard.ArgumentValid(to >= from, "Value of to must be greater than or equal to from", "to");
91 uint count = (to - from) / step + 1;
92 this.data = new object[count];
94 for (uint val = from; index < count; val += step)
95 this.data[index++] = val;
103 /// Construct a range of longs using a default step of 1
105 /// <param name="from"></param>
106 /// <param name="to"></param>
107 public RangeAttribute(long from, long to) : this(from, to, from > to ? -1L : 1L) { }
110 /// Construct a range of longs
112 /// <param name="from"></param>
113 /// <param name="to"></param>
114 /// <param name="step"></param>
115 public RangeAttribute(long from, long to, long step)
117 Guard.ArgumentValid(step > 0L && to >= from || step < 0L && to <= from,
118 "Step must be positive with to >= from or negative with to <= from", "step");
120 long count = (to - from) / step + 1;
121 this.data = new object[count];
123 for (long val = from; index < count; val += step)
124 this.data[index++] = val;
129 #region Unsigned Longs
132 /// Construct a range of unsigned longs using default step of 1
134 /// <param name="from"></param>
135 /// <param name="to"></param>
136 //[CLSCompliant(false)]
137 public RangeAttribute(ulong from, ulong to) : this(from, to, 1ul) { }
140 /// Construct a range of unsigned longs specifying the step size
142 /// <param name="from"></param>
143 /// <param name="to"></param>
144 /// <param name="step"></param>
145 //[CLSCompliant(false)]
146 public RangeAttribute(ulong from, ulong to, ulong step)
148 Guard.ArgumentValid(step > 0, "Step must be greater than zero", "step");
149 Guard.ArgumentValid(to >= from, "Value of to must be greater than or equal to from", "to");
151 ulong count = (to - from) / step + 1;
152 this.data = new object[count];
154 for (ulong val = from; index < count; val += step)
155 this.data[index++] = val;
163 /// Construct a range of doubles
165 /// <param name="from"></param>
166 /// <param name="to"></param>
167 /// <param name="step"></param>
168 public RangeAttribute(double from, double to, double step)
170 Guard.ArgumentValid(step > 0.0D && to >= from || step < 0.0D && to <= from,
171 "Step must be positive with to >= from or negative with to <= from", "step");
173 double aStep = Math.Abs(step);
174 double tol = aStep / 1000;
175 int count = (int)(Math.Abs(to - from) / aStep + tol + 1);
176 this.data = new object[count];
178 for (double val = from; index < count; val += step)
179 this.data[index++] = val;
187 /// Construct a range of floats
189 /// <param name="from"></param>
190 /// <param name="to"></param>
191 /// <param name="step"></param>
192 public RangeAttribute(float from, float to, float step)
194 Guard.ArgumentValid(step > 0.0F && to >= from || step < 0.0F && to <= from,
195 "Step must be positive with to >= from or negative with to <= from", "step");
197 float aStep = Math.Abs(step);
198 float tol = aStep / 1000;
199 int count = (int)(Math.Abs(to - from) / aStep + tol + 1);
200 this.data = new object[count];
202 for (float val = from; index < count; val += step)
203 this.data[index++] = val;