Beispiel #1
0
    static void Main()
    {
        Tuple <Class> tupleWithClass = new Tuple <Class>(new Class());

        Tuple <Interface> tupleWithInterfaceIncorrect =
            TupleHelper <Interface> .From(tupleWithClass);
    }
    /**
     * Find the nearest location to a location passed by parameter, from within a list of locations.
     * Returns a tuple, or null if it doesn't find any near location.
     */
    public static Tuple <int, int> FindNearestLocationInLocations(Tuple <int, int> location,
                                                                  List <Tuple <int, int> > locations)
    {
        Tuple <int, int> nearestLocation         = null;
        float            previousMinimumDistance = int.MaxValue;

        foreach (var currentLocation in locations)
        {
            if (nearestLocation == null)
            {
                nearestLocation = currentLocation;
            }
            else
            {
                float distanceBetweenLocations =
                    TupleHelper.GetEuclideanDistanceBetweenTuples(location, currentLocation);
                if (distanceBetweenLocations < previousMinimumDistance)
                {
                    nearestLocation         = currentLocation;
                    previousMinimumDistance = distanceBetweenLocations;
                }
            }
        }
        return(nearestLocation);
    }
        public static int GetHashCode <TTuple>(TTuple values)
#if HasITuple
            where TTuple : struct, ITuple, IEquatable <TTuple>
#else
            where TTuple : struct, IEquatable <TTuple>
#endif
        {
            return(TupleHelper <TTuple> .GetTupleHashCode(values));
        }
        public static TTuple FromArray <TTuple>(IReadOnlyList <string> values)
#if HasITuple
            where TTuple : struct, ITuple, IEquatable <TTuple>
#else
            where TTuple : struct, IEquatable <TTuple>
#endif
        {
            return(TupleHelper <TTuple> .FromArray(values));
        }
        public static string[] ToArray <TTuple>(TTuple values)
#if HasITuple
            where TTuple : struct, ITuple, IEquatable <TTuple>
#else
            where TTuple : struct, IEquatable <TTuple>
#endif
        {
            if (!Validate(typeof(TTuple)))
            {
                throw new NotSupportedException("Invalid labels tuple. Use strings only as a labels.");
            }

            return(TupleHelper <TTuple> .ToArray(values));
        }
Beispiel #6
0
        public static IEnumerable <object[]> convertClassAndInterfaceTest_data()
        {
            var testcases = new List <(Array, Type)>();

            void addTestCase(Array data, Type[] fromTypes, Type[] toTypes)
            {
                Array[] fromTypeArrays = new Array[fromTypes.Length];
                Type    dataArrayType  = data.GetType().GetElementType();

                for (int i = 0; i < fromTypes.Length; i++)
                {
                    if (fromTypes[i] == dataArrayType)
                    {
                        fromTypeArrays[i] = data;
                    }
                    else
                    {
                        fromTypeArrays[i] = Array.CreateInstance(fromTypes[i], data.Length);
                        for (int j = 0; j < data.Length; j++)
                        {
                            fromTypeArrays[i].SetValue(data.GetValue(j), j);
                        }
                    }
                }

                for (int i = 0; i < fromTypeArrays.Length; i++)
                {
                    for (int j = 0; j < toTypes.Length; j++)
                    {
                        testcases.Add((fromTypeArrays[i], toTypes[j]));
                    }
                }
            }

            addTestCase(
                data: Array.Empty <object>(),
                fromTypes: new[] {
                typeof(ASObject),
                typeof(GenericTypeConvertersTest_CA),
                typeof(GenericTypeConvertersTest_CC),
                typeof(GenericTypeConvertersTest_CD),
                typeof(GenericTypeConvertersTest_IA),
                typeof(GenericTypeConvertersTest_IC),
                typeof(GenericTypeConvertersTest_NonASType1),
                typeof(GenericTypeConvertersTest_NonASType2),
            },
                toTypes: new[] {
                typeof(ASObject),
                typeof(GenericTypeConvertersTest_CA),
                typeof(GenericTypeConvertersTest_CB),
                typeof(GenericTypeConvertersTest_CD),
                typeof(GenericTypeConvertersTest_IB),
                typeof(GenericTypeConvertersTest_IC),
                typeof(GenericTypeConvertersTest_NonASType1),
                typeof(GenericTypeConvertersTest_NonASType2),
            }
                );

            addTestCase(
                data: new object[4],
                fromTypes: new[] {
                typeof(ASObject),
                typeof(GenericTypeConvertersTest_CA),
                typeof(GenericTypeConvertersTest_CC),
                typeof(GenericTypeConvertersTest_CD),
                typeof(GenericTypeConvertersTest_IA),
                typeof(GenericTypeConvertersTest_IC),
                typeof(GenericTypeConvertersTest_NonASType1),
                typeof(GenericTypeConvertersTest_NonASType2),
            },
                toTypes: new[] {
                typeof(ASObject),
                typeof(GenericTypeConvertersTest_CA),
                typeof(GenericTypeConvertersTest_CB),
                typeof(GenericTypeConvertersTest_CD),
                typeof(GenericTypeConvertersTest_IB),
                typeof(GenericTypeConvertersTest_IC),
                typeof(GenericTypeConvertersTest_NonASType1),
                typeof(GenericTypeConvertersTest_NonASType2),
            }
                );

            addTestCase(
                data: new GenericTypeConvertersTest_CA[] {
                new GenericTypeConvertersTest_CA(),
                null, new GenericTypeConvertersTest_CA()
            },
                fromTypes: new[] {
                typeof(ASObject),
                typeof(GenericTypeConvertersTest_CA)
            },
                toTypes: new[] {
                typeof(ASObject),
                typeof(GenericTypeConvertersTest_CA),
                typeof(GenericTypeConvertersTest_CB),
                typeof(GenericTypeConvertersTest_IA)
            }
                );

            addTestCase(
                data: new GenericTypeConvertersTest_CB[] {
                null,
                new GenericTypeConvertersTest_CC(),
                null,
                new GenericTypeConvertersTest_CC(),
                new GenericTypeConvertersTest_CB(),
                null,
                new GenericTypeConvertersTest_CB(),
            },
                fromTypes: new[] {
                typeof(ASObject),
                typeof(GenericTypeConvertersTest_CB),
                typeof(GenericTypeConvertersTest_IA)
            },
                toTypes: new[] {
                typeof(ASObject),
                typeof(GenericTypeConvertersTest_CA),
                typeof(GenericTypeConvertersTest_CB),
                typeof(GenericTypeConvertersTest_CC),
                typeof(GenericTypeConvertersTest_IA),
                typeof(GenericTypeConvertersTest_IC)
            }
                );

            addTestCase(
                data: new GenericTypeConvertersTest_CC[] {
                new GenericTypeConvertersTest_CC(),
                new GenericTypeConvertersTest_CC(),
            },
                fromTypes: new[] {
                typeof(GenericTypeConvertersTest_CB),
                typeof(GenericTypeConvertersTest_CC),
                typeof(GenericTypeConvertersTest_IA),
            },
                toTypes: new[] {
                typeof(ASObject),
                typeof(GenericTypeConvertersTest_CA),
                typeof(GenericTypeConvertersTest_CB),
                typeof(GenericTypeConvertersTest_CC),
                typeof(GenericTypeConvertersTest_IA),
                typeof(GenericTypeConvertersTest_IC)
            }
                );

            addTestCase(
                data: new GenericTypeConvertersTest_CD[] {
                null,
                new GenericTypeConvertersTest_CD(),
                new GenericTypeConvertersTest_CD(),
            },
                fromTypes: new[] {
                typeof(GenericTypeConvertersTest_IA),
                typeof(GenericTypeConvertersTest_IB),
                typeof(GenericTypeConvertersTest_IC),
            },
                toTypes: new[] {
                typeof(GenericTypeConvertersTest_IA),
                typeof(GenericTypeConvertersTest_IB),
                typeof(GenericTypeConvertersTest_IC),
            }
                );

            addTestCase(
                data: new ASObject[] {
                null,
                new GenericTypeConvertersTest_CD(),
                new GenericTypeConvertersTest_CD(),
                new GenericTypeConvertersTest_CD(),
                null,
                null,
                new GenericTypeConvertersTest_CC(),
                new GenericTypeConvertersTest_CC(),
                new GenericTypeConvertersTest_CB(),
                new GenericTypeConvertersTest_CB(),
                null,
            },
                fromTypes: new[] {
                typeof(ASObject),
                typeof(GenericTypeConvertersTest_IA),
            },
                toTypes: new[] {
                typeof(GenericTypeConvertersTest_CB),
                typeof(GenericTypeConvertersTest_CC),
                typeof(GenericTypeConvertersTest_CD),
                typeof(GenericTypeConvertersTest_IA),
                typeof(GenericTypeConvertersTest_IB),
                typeof(GenericTypeConvertersTest_IC),
                typeof(GenericTypeConvertersTest_NonASType1),
            }
                );

            addTestCase(
                data: new object[] {
                new GenericTypeConvertersTest_NonASType2(),
                null,
            },
                fromTypes: new[] {
                typeof(GenericTypeConvertersTest_NonASType1),
                typeof(GenericTypeConvertersTest_NonASType2),
                typeof(GenericTypeConvertersTest_IA)
            },
                toTypes: new[] {
                typeof(GenericTypeConvertersTest_NonASType1),
                typeof(GenericTypeConvertersTest_NonASType2),
                typeof(ASObject),
                typeof(GenericTypeConvertersTest_CA),
                typeof(GenericTypeConvertersTest_IA),
                typeof(GenericTypeConvertersTest_IB),
            }
                );

            return(testcases.Select(x => TupleHelper.toArray(x)));
        }