[NUI] Rebase develnui (DevelNUI only patches --> master) (#3910)
[platform/core/csapi/tizenfx.git] / test / Tizen.NUI.Devel.Tests.Ubuntu / nunit.framework / Attributes / RandomAttribute.cs
1 // ***********************************************************************
2 // Copyright (c) 2008-2015 Charlie Poole
3 //
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:
11 // 
12 // The above copyright notice and this permission notice shall be
13 // included in all copies or substantial portions of the Software.
14 // 
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 // ***********************************************************************
23 #define PORTABLE
24 #define TIZEN
25 #define NUNIT_FRAMEWORK
26 #define NUNITLITE
27 #define NET_4_5
28 #define PARALLEL
29 using System;
30 using System.Collections;
31 using System.Collections.Generic;
32 using System.Globalization;
33 using System.Reflection;
34 using NUnit.Compatibility;
35 using NUnit.Framework.Interfaces;
36 using NUnit.Framework.Internal;
37
38 namespace NUnit.Framework
39 {
40     /// <summary>
41     /// RandomAttribute is used to supply a set of random _values
42     /// to a single parameter of a parameterized test.
43     /// </summary>
44     public class RandomAttribute : DataAttribute, IParameterDataSource
45     {
46         private RandomDataSource _source;
47         private int _count;
48
49         #region Constructors
50
51         /// <summary>
52         /// Construct a random set of values appropriate for the Type of the 
53         /// parameter on which the attribute appears, specifying only the count.
54         /// </summary>
55         /// <param name="count"></param>
56         public RandomAttribute(int count)
57         {
58             _count = count;
59         }
60
61         /// <summary>
62         /// Construct a set of ints within a specified range
63         /// </summary>
64         public RandomAttribute(int min, int max, int count)
65         {
66             _source = new IntDataSource(min, max, count);
67         }
68
69         /// <summary>
70         /// Construct a set of unsigned ints within a specified range
71         /// </summary>
72         //[CLSCompliant(false)]
73         public RandomAttribute(uint min, uint max, int count)
74         {
75             _source = new UIntDataSource(min, max, count);
76         }
77
78         /// <summary>
79         /// Construct a set of longs within a specified range
80         /// </summary>
81         public RandomAttribute(long min, long max, int count)
82         {
83             _source = new LongDataSource(min, max, count);
84         }
85
86         /// <summary>
87         /// Construct a set of unsigned longs within a specified range
88         /// </summary>
89         //[CLSCompliant(false)]
90         public RandomAttribute(ulong min, ulong max, int count)
91         {
92             _source = new ULongDataSource(min, max, count);
93         }
94
95         /// <summary>
96         /// Construct a set of shorts within a specified range
97         /// </summary>
98         public RandomAttribute(short min, short max, int count)
99         {
100             _source = new ShortDataSource(min, max, count);
101         }
102
103         /// <summary>
104         /// Construct a set of unsigned shorts within a specified range
105         /// </summary>
106         //[CLSCompliant(false)]
107         public RandomAttribute(ushort min, ushort max, int count)
108         {
109             _source = new UShortDataSource(min, max, count);
110         }
111
112         /// <summary>
113         /// Construct a set of doubles within a specified range
114         /// </summary>
115         public RandomAttribute(double min, double max, int count)
116         {
117             _source = new DoubleDataSource(min, max, count);
118         }
119
120         /// <summary>
121         /// Construct a set of floats within a specified range
122         /// </summary>
123         public RandomAttribute(float min, float max, int count)
124         {
125             _source = new FloatDataSource(min, max, count);
126         }
127
128         /// <summary>
129         /// Construct a set of bytes within a specified range
130         /// </summary>
131         public RandomAttribute(byte min, byte max, int count)
132         {
133             _source = new ByteDataSource(min, max, count);
134         }
135
136         /// <summary>
137         /// Construct a set of sbytes within a specified range
138         /// </summary>
139         //[CLSCompliant(false)]
140         public RandomAttribute(sbyte min, sbyte max, int count)
141         {
142             _source = new SByteDataSource(min, max, count);
143         }
144
145         #endregion
146
147         #region IParameterDataSource Interface
148
149         /// <summary>
150         /// Get the collection of _values to be used as arguments.
151         /// </summary>
152         public IEnumerable GetData(IParameterInfo parameter)
153         {
154             // Since a separate Randomizer is used for each parameter,
155             // we can't fill in the data in the constructor of the
156             // attribute. Only now, when GetData is called, do we have
157             // sufficient information to create the values in a 
158             // repeatable manner.
159
160             Type parmType = parameter.ParameterType;
161
162             if (_source == null)
163             {
164                 if (parmType == typeof(int))
165                     _source = new IntDataSource(_count);
166                 else if (parmType == typeof(uint))
167                     _source = new UIntDataSource(_count);
168                 else if (parmType == typeof(long))
169                     _source = new LongDataSource(_count);
170                 else if (parmType == typeof(ulong))
171                     _source = new ULongDataSource(_count);
172                 else if (parmType == typeof(short))
173                     _source = new ShortDataSource(_count);
174                 else if (parmType == typeof(ushort))
175                     _source = new UShortDataSource(_count);
176                 else if (parmType == typeof(double))
177                     _source = new DoubleDataSource(_count);
178                 else if (parmType == typeof(float))
179                     _source = new FloatDataSource(_count);
180                 else if (parmType == typeof(byte))
181                     _source = new ByteDataSource(_count);
182                 else if (parmType == typeof(sbyte))
183                     _source = new SByteDataSource(_count);
184                 else if (parmType == typeof(decimal))
185                     _source = new DecimalDataSource(_count);
186                 else if (parmType.GetTypeInfo().IsEnum)
187                     _source = new EnumDataSource(_count);
188                 else // Default
189                     _source = new IntDataSource(_count);
190             }
191             else if (_source.DataType != parmType && WeConvert(_source.DataType, parmType))
192             {
193                 _source = new RandomDataConverter(_source);
194             }
195
196             return _source.GetData(parameter);
197
198             //// Copy the random _values into the data array
199             //// and call the base class which may need to
200             //// convert them to another type.
201             //this.data = new object[values.Count];
202             //for (int i = 0; i < values.Count; i++)
203             //    this.data[i] = values[i];
204
205             //return base.GetData(parameter);
206         }
207
208         private bool WeConvert(Type sourceType, Type targetType)
209         {
210             if (targetType == typeof(short) || targetType == typeof(ushort) || targetType == typeof(byte) || targetType == typeof(sbyte))
211                 return sourceType == typeof(int);
212
213             if (targetType == typeof(decimal))
214                 return sourceType == typeof(int) || sourceType == typeof(double);
215             
216             return false;
217         }
218
219         #endregion
220
221         #region Nested DataSource Classes
222
223         #region RandomDataSource
224
225         abstract class RandomDataSource : IParameterDataSource
226         {
227             public Type DataType { get; protected set; }
228
229             public abstract IEnumerable GetData(IParameterInfo parameter);
230         }
231
232         abstract class RandomDataSource<T> : RandomDataSource
233         {
234             private T _min;
235             private T _max;
236             private int _count;
237             private bool _inRange;
238
239             protected Randomizer _randomizer;
240
241             protected RandomDataSource(int count)
242             {
243                 _count = count;
244                 _inRange = false;
245
246                 DataType = typeof(T);
247             }
248
249             protected RandomDataSource(T min, T max, int count)
250             {
251                 _min = min;
252                 _max = max;
253                 _count = count;
254                 _inRange = true;
255
256                 DataType = typeof(T);
257             }
258
259             public override IEnumerable GetData(IParameterInfo parameter)
260             {
261                 //Guard.ArgumentValid(parameter.ParameterType == typeof(T), "Parameter type must be " + typeof(T).Name, "parameter");
262
263                 _randomizer = Randomizer.GetRandomizer(parameter.ParameterInfo);
264
265                 for (int i = 0; i < _count; i++)
266                     yield return _inRange
267                         ? GetNext(_min, _max)
268                         : GetNext();
269             }
270
271             protected abstract T GetNext();
272             protected abstract T GetNext(T min, T max);
273         }
274
275         #endregion
276
277         #region RandomDataConverter
278
279         class RandomDataConverter : RandomDataSource
280         {
281             IParameterDataSource _source;
282
283             public RandomDataConverter(IParameterDataSource source)
284             {
285                 _source = source;
286             }
287
288             public override IEnumerable GetData(IParameterInfo parameter)
289             {
290                 Type parmType = parameter.ParameterType;
291
292                 foreach (object obj in _source.GetData(parameter))
293                 {
294                     if (obj is int)
295                     {
296                         int ival = (int)obj; // unbox first
297                         if (parmType == typeof(short))
298                             yield return (short)ival;
299                         else if (parmType == typeof(ushort))
300                             yield return (ushort)ival;
301                         else if (parmType == typeof(byte))
302                             yield return (byte)ival;
303                         else if (parmType == typeof(sbyte))
304                             yield return (sbyte)ival;
305                         else if (parmType == typeof(decimal))
306                             yield return (decimal)ival;
307                     }
308                     else if (obj is double)
309                     {
310                         double d = (double)obj; // unbox first
311                         if (parmType == typeof(decimal))
312                             yield return (decimal)d;
313                     }
314                 }
315             }
316         }
317
318         #endregion
319
320         #region IntDataSource
321
322         class IntDataSource : RandomDataSource<int>
323         {
324             public IntDataSource(int count) : base(count) { }
325
326             public IntDataSource(int min, int max, int count) : base(min, max, count) { }
327
328             protected override int GetNext()
329             {
330                 return _randomizer.Next();
331             }
332
333             protected override int GetNext(int min, int max)
334             {
335                 return _randomizer.Next(min, max);
336             }
337         }
338
339         #endregion
340
341         #region UIntDataSource
342
343         class UIntDataSource : RandomDataSource<uint>
344         {
345             public UIntDataSource(int count) : base(count) { }
346
347             public UIntDataSource(uint min, uint max, int count) : base(min, max, count) { }
348
349             protected override uint GetNext()
350             {
351                 return _randomizer.NextUInt();
352             }
353
354             protected override uint GetNext(uint min, uint max)
355             {
356                 return _randomizer.NextUInt(min, max);
357             }
358         }
359
360         #endregion
361
362         #region LongDataSource
363
364         class LongDataSource : RandomDataSource<long>
365         {
366             public LongDataSource(int count) : base(count) { }
367
368             public LongDataSource(long min, long max, int count) : base(min, max, count) { }
369
370             protected override long GetNext()
371             {
372                 return _randomizer.NextLong();
373             }
374
375             protected override long GetNext(long min, long max)
376             {
377                 return _randomizer.NextLong(min, max);
378             }
379         }
380
381         #endregion
382
383         #region ULongDataSource
384
385         class ULongDataSource : RandomDataSource<ulong>
386         {
387             public ULongDataSource(int count) : base(count) { }
388
389             public ULongDataSource(ulong min, ulong max, int count) : base(min, max, count) { }
390
391             protected override ulong GetNext()
392             {
393                 return _randomizer.NextULong();
394             }
395
396             protected override ulong GetNext(ulong min, ulong max)
397             {
398                 return _randomizer.NextULong(min, max);
399             }
400         }
401
402         #endregion
403
404         #region ShortDataSource
405
406         class ShortDataSource : RandomDataSource<short>
407         {
408             public ShortDataSource(int count) : base(count) { }
409
410             public ShortDataSource(short min, short max, int count) : base(min, max, count) { }
411
412             protected override short GetNext()
413             {
414                 return _randomizer.NextShort();
415             }
416
417             protected override short GetNext(short min, short max)
418             {
419                 return _randomizer.NextShort(min, max);
420             }
421         }
422
423         #endregion
424
425         #region UShortDataSource
426
427         class UShortDataSource : RandomDataSource<ushort>
428         {
429             public UShortDataSource(int count) : base(count) { }
430
431             public UShortDataSource(ushort min, ushort max, int count) : base(min, max, count) { }
432
433             protected override ushort GetNext()
434             {
435                 return _randomizer.NextUShort();
436             }
437
438             protected override ushort GetNext(ushort min, ushort max)
439             {
440                 return _randomizer.NextUShort(min, max);
441             }
442         }
443
444         #endregion
445
446         #region DoubleDataSource
447
448         class DoubleDataSource : RandomDataSource<double>
449         {
450             public DoubleDataSource(int count) : base(count) { }
451
452             public DoubleDataSource(double min, double max, int count) : base(min, max, count) { }
453
454             protected override double GetNext()
455             {
456                 return _randomizer.NextDouble();
457             }
458
459             protected override double GetNext(double min, double max)
460             {
461                 return _randomizer.NextDouble(min, max);
462             }
463         }
464
465         #endregion
466
467         #region FloatDataSource
468
469         class FloatDataSource : RandomDataSource<float>
470         {
471             public FloatDataSource(int count) : base(count) { }
472
473             public FloatDataSource(float min, float max, int count) : base(min, max, count) { }
474
475             protected override float GetNext()
476             {
477                 return _randomizer.NextFloat();
478             }
479
480             protected override float GetNext(float min, float max)
481             {
482                 return _randomizer.NextFloat(min, max);
483             }
484         }
485
486         #endregion
487
488         #region ByteDataSource
489
490         class ByteDataSource : RandomDataSource<byte>
491         {
492             public ByteDataSource(int count) : base(count) { }
493
494             public ByteDataSource(byte min, byte max, int count) : base(min, max, count) { }
495
496             protected override byte GetNext()
497             {
498                 return _randomizer.NextByte();
499             }
500
501             protected override byte GetNext(byte min, byte max)
502             {
503                 return _randomizer.NextByte(min, max);
504             }
505         }
506
507         #endregion
508
509         #region SByteDataSource
510
511         class SByteDataSource : RandomDataSource<sbyte>
512         {
513             public SByteDataSource(int count) : base(count) { }
514
515             public SByteDataSource(sbyte min, sbyte max, int count) : base(min, max, count) { }
516
517             protected override sbyte GetNext()
518             {
519                 return _randomizer.NextSByte();
520             }
521
522             protected override sbyte GetNext(sbyte min, sbyte max)
523             {
524                 return _randomizer.NextSByte(min, max);
525             }
526         }
527
528         #endregion
529
530         #region EnumDataSource
531
532         class EnumDataSource : RandomDataSource
533         {
534             private int _count;
535
536             public EnumDataSource(int count)
537             {
538                 _count = count;
539                 DataType = typeof(Enum);
540             }
541
542             public override IEnumerable GetData(IParameterInfo parameter)
543             {
544                 Guard.ArgumentValid(parameter.ParameterType.GetTypeInfo().IsEnum, "EnumDataSource requires an enum parameter", "parameter");
545
546                 Randomizer randomizer = Randomizer.GetRandomizer(parameter.ParameterInfo);
547                 DataType = parameter.ParameterType;
548
549                 for (int i = 0; i < _count; i++ )
550                     yield return randomizer.NextEnum(parameter.ParameterType);
551             }
552         }
553
554         #endregion
555
556         #region DecimalDataSource
557
558         // Currently, Randomizer doesn't implement methods for decimal
559         // so we use random Ulongs and convert them. This doesn't cover
560         // the full range of decimal, so it's temporary.
561         class DecimalDataSource : RandomDataSource<decimal>
562         {
563             public DecimalDataSource(int count) : base(count) { }
564
565             public DecimalDataSource(decimal min, decimal max, int count) : base(min, max, count) { }
566
567             protected override decimal GetNext()
568             {
569                 return _randomizer.NextDecimal();
570             }
571
572             protected override decimal GetNext(decimal min, decimal max)
573             {
574                 return _randomizer.NextDecimal(min, max);
575             }
576         }
577
578         #endregion
579
580         #endregion
581     }
582 }