[NUI] Rebase develnui (DevelNUI only patches --> master) (#3910)
[platform/core/csapi/tizenfx.git] / test / Tizen.NUI.Devel.Tests.Ubuntu / nunit.framework / Internal / NetCFExtensions.cs
1 // ***********************************************************************
2 // Copyright (c) 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 #if NETCF
30 using System;
31 using System.Collections.Generic;
32 using System.Linq;
33 using System.Text;
34 using System.Reflection;
35 using NUnit.Framework.Interfaces;
36
37 namespace NUnit.Framework.Internal
38 {
39     static class NetCFExtensions
40     {
41         public static IMethodInfo MakeGenericMethodEx(this IMethodInfo method, object[] arguments)
42         {
43             var newMi = method.MethodInfo.MakeGenericMethodEx(arguments);
44             if (newMi == null) return null;
45
46             return new MethodWrapper(method.TypeInfo.Type, newMi);
47         }
48
49         public static MethodInfo MakeGenericMethodEx(this MethodInfo mi, object[] arguments)
50         {
51             return mi.MakeGenericMethodEx(arguments.Select(a => a == null ? typeof(object) : (a is Type ? typeof(Type) : a.GetType())).ToArray());
52         }
53
54         public static MethodInfo MakeGenericMethodEx(this MethodInfo mi, Type[] types)
55         {
56             if(!mi.ContainsGenericParameters)
57                 return mi;
58
59             if(!mi.IsGenericMethodDefinition)
60                 return mi.MakeGenericMethod(types);
61
62             var args = mi.GetGenericArguments();
63
64             if(args.Length == types.Length)
65                 return mi.MakeGenericMethod(types);
66
67             if(args.Length > types.Length)
68                 return null;
69
70             var tai = new TypeArrayIterator(types, args.Length);
71
72             foreach(var ta in tai)
73             {
74                 var newMi = mi.MakeGenericMethod(ta);
75                 var pa = newMi.GetParameters();
76                 if(types.SequenceEqual(pa.Select(p => p.ParameterType)))
77                     return newMi;
78             }
79
80             foreach(var ta in tai)
81             {
82                 var newMi = mi.MakeGenericMethod(ta);
83                 var pa = newMi.GetParameters();
84                 if(types.Select((t,ix) => (Type)TypeHelper.BestCommonType(t, pa[ix].ParameterType)).SequenceEqual(pa.Select(p => p.ParameterType)))
85                     return newMi;
86             }
87
88             return null;
89         }
90
91     }
92
93     internal class TypeArrayIterator : IEnumerable<Type[]>
94     {
95         private Type[] _types;
96         private int _neededLength;
97
98         public TypeArrayIterator(Type[] types, int neededLength)
99         {
100             _types = types;
101             _neededLength = neededLength;
102         }
103
104 #region IEnumerable<CType[]> Members
105
106         public IEnumerator<Type[]> GetEnumerator()
107         {
108             var indices = new int[_neededLength];
109             while(true)
110             {
111                 var results = new List<Type> ();
112
113                 for(int i = 0; i < _neededLength; ++i)
114                     results.Add(_types[indices[i]]);
115
116                 if(indices.Distinct().Count() == _neededLength)
117                     yield return results.ToArray();
118
119                 for(int j = _neededLength - 1; j >= 0; --j)
120                 {
121                     if(++indices[j] < _types.Length)
122                         break;
123
124                     if(j == 0)
125                         yield break;
126
127                     indices[j] = 0;
128                 }
129             }
130         }
131
132 #endregion
133
134 #region IEnumerable Members
135
136         System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
137         {
138             return GetEnumerator();
139         }
140
141 #endregion
142     }
143 }
144 #endif