Exemplo n.º 1
0
        public void FromPrimitiveToArray_CanBeConvertedReturnsFalse()
        {
            var typeFrom = FunnyType.PrimitiveOf(BaseFunnyType.Int32);
            var typeTo   = FunnyType.ArrayOf(FunnyType.PrimitiveOf(BaseFunnyType.Int32));

            Assert.IsFalse(typeFrom.CanBeConvertedTo(typeTo));
        }
Exemplo n.º 2
0
        public void SingleMultiDimVariable_OutputEqualsTwoInputs()
        {
            var x = new int[3][];

            x[0] = new[] { 1, 2 };
            x[1] = new[] { 3, 4 };
            x[2] = new[] { 5 };

            var expectedType   = FunnyType.ArrayOf(FunnyType.ArrayOf(FunnyType.Int32));
            var expectedOutput = new int[6][];

            expectedOutput[0] = new[] { 1, 2 };
            expectedOutput[1] = new[] { 3, 4 };
            expectedOutput[2] = new[] { 5 };
            expectedOutput[3] = new[] { 1, 2 };
            expectedOutput[4] = new[] { 3, 4 };
            expectedOutput[5] = new[] { 5 };

            var expression = "x:int[][]\r y= x.concat(x)";

            var res = expression.Calc("x", x).Get("y");

            Assert.IsInstanceOf <int[][]>(res);
            AssertMultiDimentionalEquals(res as int[][], expectedOutput);
        }
Exemplo n.º 3
0
        public void OutputEqualsInputArray(string type, object expected, BaseFunnyType arrayType)
        {
            var res = $"x:{type}\r  y = x".Calc("x", expected);

            res.AssertReturns(expected);
            res.AssertResultIs(("y", GetClrType(FunnyType.ArrayOf(FunnyType.PrimitiveOf(arrayType)))));
        }
Exemplo n.º 4
0
 public SetGenericFunctionDefinition() : base("set",
                                              FunnyType.ArrayOf(FunnyType.Generic(0)),
                                              FunnyType.ArrayOf(FunnyType.Generic(0)),
                                              FunnyType.Int32,
                                              FunnyType.Generic(0))
 {
 }
Exemplo n.º 5
0
        public void ArrayAndPrimitiveTypes_Equals_ReturnsFalse()
        {
            var typeA = FunnyType.ArrayOf(FunnyType.Int32);
            var typeB = FunnyType.Int32;

            Assert.IsFalse(typeA == typeB);
        }
Exemplo n.º 6
0
        public void ArrayTypes_CanBeConverted(BaseFunnyType from, BaseFunnyType to, bool canBeConverted)
        {
            var typeFrom = FunnyType.ArrayOf(FunnyType.PrimitiveOf(from));
            var typeTo   = FunnyType.ArrayOf(FunnyType.PrimitiveOf(to));

            Assert.AreEqual(canBeConverted, typeFrom.CanBeConvertedTo(typeTo));
        }
Exemplo n.º 7
0
        public void TwoNotEqualArrayTypes_Equals_ReturnsFalse()
        {
            var typeA = FunnyType.ArrayOf(FunnyType.Int32);
            var typeB = FunnyType.ArrayOf(FunnyType.Real);

            Assert.IsFalse(typeA == typeB);
        }
Exemplo n.º 8
0
        public static FunnyType ReadType(this TokFlow flow)
        {
            var cur      = flow.Current;
            var readType = ToFunnyType(cur);

            flow.MoveNext();
            var lastPosition = cur.Finish;

            while (flow.IsCurrent(TokType.ArrOBr))
            {
                if (flow.Current.Start != lastPosition)
                {
                    throw FunParseException.ErrorStubToDo("unexpected space before []");
                }

                flow.MoveNext();
                lastPosition = flow.Current.Finish;
                if (!flow.MoveIf(TokType.ArrCBr))
                {
                    throw ErrorFactory.ArrTypeCbrMissed(new Interval(cur.Start, flow.Current.Start));
                }
                readType = FunnyType.ArrayOf(readType);
            }

            return(readType);
        }
Exemplo n.º 9
0
 public SliceGenericFunctionDefinition() : base(CoreFunNames.SliceName,
                                                FunnyType.ArrayOf(FunnyType.Generic(0)),
                                                FunnyType.ArrayOf(FunnyType.Generic(0)),
                                                FunnyType.Int32,
                                                FunnyType.Int32)
 {
 }
Exemplo n.º 10
0
        public void TwoComplexHiOrderEqualFunTypes_Equals_ReturnsTrue()
        {
            var typeA = FunnyType.Fun(
                returnType: FunnyType.ArrayOf(FunnyType.Fun(FunnyType.Int32, FunnyType.Text)),
                inputTypes:
                new [] {
                FunnyType.ArrayOf(FunnyType.Anything),
                FunnyType.Fun(
                    returnType: FunnyType.ArrayOf(FunnyType.Real),
                    inputTypes: FunnyType.Fun(
                        returnType: FunnyType.Fun(
                            returnType: FunnyType.ArrayOf(FunnyType.Bool),
                            inputTypes: FunnyType.Bool),
                        inputTypes: FunnyType.Text))
            });
            var typeB = FunnyType.Fun(
                returnType: FunnyType.ArrayOf(FunnyType.Fun(FunnyType.Int32, FunnyType.Text)),
                inputTypes:
                new [] {
                FunnyType.ArrayOf(FunnyType.Anything),
                FunnyType.Fun(
                    returnType: FunnyType.ArrayOf(FunnyType.Real),
                    inputTypes: FunnyType.Fun(
                        returnType: FunnyType.Fun(
                            returnType: FunnyType.ArrayOf(FunnyType.Bool),
                            inputTypes: FunnyType.Bool),
                        inputTypes: FunnyType.Text))
            });

            Assert.IsTrue(typeA == typeB);
        }
Exemplo n.º 11
0
        public MapFunction() : base("map",
                                    FunnyType.ArrayOf(FunnyType.Generic(1)),

                                    FunnyType.ArrayOf(FunnyType.Generic(0)),
                                    FunnyType.Fun(FunnyType.Generic(1), FunnyType.Generic(0)))
        {
        }
Exemplo n.º 12
0
        public void TwoEqualArrayTypes_Equals_ReturnsTrue()
        {
            var typeA = FunnyType.ArrayOf(FunnyType.Int32);
            var typeB = FunnyType.ArrayOf(FunnyType.Int32);

            Assert.IsTrue(typeA == typeB);
        }
Exemplo n.º 13
0
        public void FromArrayOfInt32ToPrimitive_CanBeConvertedReturnsFalse(BaseFunnyType to, bool canBeConverted)
        {
            var typeFrom = FunnyType.ArrayOf(FunnyType.PrimitiveOf(BaseFunnyType.Int32));
            var typeTo   = FunnyType.PrimitiveOf(to);

            Assert.AreEqual(canBeConverted, typeFrom.CanBeConvertedTo(typeTo));
        }
Exemplo n.º 14
0
 public FoldWithDefaultsGenericFunctionDefinition() : base("fold",
                                                           returnType: FunnyType.Generic(1),
                                                           FunnyType.ArrayOf(FunnyType.Generic(0)),
                                                           FunnyType.Generic(1),
                                                           FunnyType.Fun(
                                                               returnType: FunnyType.Generic(1), FunnyType.Generic(1), FunnyType.Generic(0)))
 {
 }
Exemplo n.º 15
0
        public void SubstituteConcreteTypes_PrimitiveGenericType_RetutnsConcrete()
        {
            var someSolving = new[] { FunnyType.Int32, FunnyType.ArrayOf(FunnyType.Text) };
            var genericType = FunnyType.Generic(0);
            var expected    = someSolving[0];

            var result = FunnyType.SubstituteConcreteTypes(genericType, someSolving);

            Assert.AreEqual(expected, result);
        }
Exemplo n.º 16
0
        public void ConvertPrimitiveTypeArrays(object primitiveValue, BaseFunnyType expectedTypeName)
        {
            var clrType   = primitiveValue.GetType();
            var converter = FunnyTypeConverters.GetInputConverter(clrType);

            Assert.AreEqual(FunnyType.ArrayOf(FunnyType.PrimitiveOf(expectedTypeName)), converter.FunnyType);
            var convertedValue = converter.ToFunObject(primitiveValue);

            Assert.AreEqual(primitiveValue, convertedValue);
        }
Exemplo n.º 17
0
        public void ConvertPrimitiveTypeArrays(object primitiveValue, BaseFunnyType expectedTypeName)
        {
            var clrType   = primitiveValue.GetType();
            var converter = FunnyTypeConverters.GetOutputConverter(clrType);

            Assert.AreEqual(FunnyType.ArrayOf(FunnyType.PrimitiveOf(expectedTypeName)), converter.FunnyType);
            var convertedValue = converter.ToClrObject(
                new ImmutableFunArray(primitiveValue as Array, FunnyType.PrimitiveOf(expectedTypeName)));

            Assert.AreEqual(primitiveValue, convertedValue);
        }
Exemplo n.º 18
0
        public void SearchMaxGenericTypeId_ComplexGenericTypeWithThreeArgs_FindAll()
        {
            // array of (T0[] fun<T0,T1,T2>(double, T1, T0,T2))
            var genericType = FunnyType.ArrayOf(
                FunnyType.Fun(
                    FunnyType.ArrayOf(FunnyType.Generic(0)),
                    FunnyType.Real, FunnyType.Generic(1), FunnyType.Generic(0), FunnyType.Generic(2)));
            var maxCount = genericType.SearchMaxGenericTypeId();

            Assert.AreEqual(2, maxCount);
        }
Exemplo n.º 19
0
        public override IConcreteFunction CreateConcrete(FunnyType[] concreteTypesMap)
        {
            var res = new ConcreteRepeat
            {
                Name       = Name,
                ArgTypes   = new[] { concreteTypesMap[0], FunnyType.Int32 },
                ReturnType = FunnyType.ArrayOf(concreteTypesMap[0])
            };

            return(res);
        }
Exemplo n.º 20
0
        public void SolveGenericTypes_ArrayOfT_SolvedTypeIsT()
        {
            //Solving  T[]
            var solvingTypes = new FunnyType[1];
            var concrete     = FunnyType.Int32;

            FunnyType.TrySolveGenericTypes(solvingTypes,
                                           FunnyType.ArrayOf(FunnyType.Generic(0)),
                                           FunnyType.ArrayOf(concrete));
            Assert.AreEqual(concrete, solvingTypes[0]);
        }
Exemplo n.º 21
0
        public void ArrayOfArrayOfAnything()
        {
            object[][] inputValue =
            {
                new object[] { 1, 2, "kate" },
                new object[] { 2, 1, "kate" },
                new object[] { }
            };
            var converter = FunnyTypeConverters.GetOutputConverter(inputValue.GetType());

            Assert.AreEqual(FunnyType.ArrayOf(FunnyType.ArrayOf(FunnyType.Anything)), converter.FunnyType);
        }
Exemplo n.º 22
0
        public void ConvertArrayOfStrings()
        {
            string[] clrValue = { "vasa", "kata", "" };

            var converter = FunnyTypeConverters.GetOutputConverter(clrValue.GetType());

            Assert.AreEqual(FunnyType.ArrayOf(FunnyType.Text), converter.FunnyType);
            var funValue = new ImmutableFunArray(
                clrValue.Select(i => new TextFunArray(i)).ToArray(), FunnyType.Text);

            Assert.AreEqual(clrValue, converter.ToClrObject(funValue));
        }
Exemplo n.º 23
0
        public void Repeat_PrimitiveType_ResultTypesAreCorrect()
        {
            var rpt      = new RepeatGenericFunctionDefinition();
            var function = rpt.CreateConcreteOrNull(FunnyType.ArrayOf(FunnyType.Bool), FunnyType.Bool, FunnyType.Int32);

            Assert.IsNotNull(function);
            Assert.Multiple(() =>
            {
                Assert.AreEqual(FunnyType.ArrayOf(FunnyType.Bool), function.ReturnType);
                CollectionAssert.AreEquivalent(new[] { FunnyType.Bool, FunnyType.Int32 }, function.ArgTypes);
            });
        }
Exemplo n.º 24
0
        public void SolveGenericTypes_ArrayOfFunOfT_SolvedTypeIsT()
        {
            //Solving  Array of int SomeFun<T>(T)

            var solvingTypes = new FunnyType[1];
            var concrete     = FunnyType.Text;

            FunnyType.TrySolveGenericTypes(solvingTypes,
                                           FunnyType.ArrayOf(FunnyType.Fun(FunnyType.Int32, FunnyType.Generic(0))),
                                           FunnyType.ArrayOf(FunnyType.Fun(FunnyType.Int32, concrete)));

            Assert.AreEqual(concrete, solvingTypes[0]);
        }
Exemplo n.º 25
0
        public void ReturnSelfFunction_ArrayType_ResultTypesAreCorrect()
        {
            var arrayOfBool = FunnyType.ArrayOf(FunnyType.Bool);
            var rpt         = new ReturnSelfGenericFunctionDefinition();
            var function    = rpt.CreateConcreteOrNull(arrayOfBool, arrayOfBool);

            Assert.IsNotNull(function);
            Assert.Multiple(() =>
            {
                Assert.AreEqual(arrayOfBool, function.ReturnType);
                CollectionAssert.AreEquivalent(new[] { arrayOfBool }, function.ArgTypes);
            });
        }
Exemplo n.º 26
0
        public void ConvertArrayOfStrings()
        {
            string[] inputValue =
            {
                "vasa", "kata", ""
            };

            var converter = FunnyTypeConverters.GetInputConverter(inputValue.GetType());

            Assert.AreEqual(FunnyType.ArrayOf(FunnyType.Text), converter.FunnyType);
            Assert.AreEqual(new ImmutableFunArray(
                                inputValue.Select(i => new TextFunArray(i)).ToArray(), FunnyType.Text),
                            converter.ToFunObject(inputValue));
        }
Exemplo n.º 27
0
        public void ConvertIntArrayToRealArray()
        {
            var intArray = new ImmutableFunArray(new[] { 1, 2, 3, 4 });
            var typeFrom = FunnyType.ArrayOf(FunnyType.Int32);
            var typeTo   = FunnyType.ArrayOf(FunnyType.Real);

            Assert.IsTrue(VarTypeConverter.CanBeConverted(typeFrom, typeTo));
            var coverter = VarTypeConverter.GetConverterOrNull(typeFrom, typeTo);
            var actual   = coverter(intArray) as IFunArray;

            Assert.IsNotNull(actual);
            Assert.AreEqual(typeTo.ArrayTypeSpecification.FunnyType, actual.ElementType);
            CollectionAssert.AreEqual(new double[] { 1, 2, 3, 4 }, actual);
        }
Exemplo n.º 28
0
        public void CustomGenericFunction_EachSecond_WorksFine(string arg, object expected)
        {
            string customName = "each_second";
            var    runtime    = Funny.Hardcore
                                .WithFunction(
                new GenericFunctionMock(
                    args => new EnumerableFunArray(((IEnumerable <object>)args[0])
                                                   .Where((_, i) => i % 2 == 0), FunnyType.Anything),
                    customName,
                    FunnyType.ArrayOf(FunnyType.Generic(0)),
                    FunnyType.ArrayOf(FunnyType.Generic(0))))
                                .Build($"y = {customName}({arg})");

            runtime.Calc().AssertReturns("y", expected);
        }
Exemplo n.º 29
0
        public override IConcreteFunction CreateConcrete(FunnyType[] concreteTypesMap)
        {
            var res = new ConcreteMap
            {
                Name     = Name,
                ArgTypes = new[]
                {
                    FunnyType.ArrayOf(concreteTypesMap[0]),
                    FunnyType.Fun(concreteTypesMap[1], concreteTypesMap[0])
                },
                ReturnType = FunnyType.ArrayOf(concreteTypesMap[1])
            };

            return(res);
        }
Exemplo n.º 30
0
        public void ArrayOfArrayOfAnything()
        {
            object[][] inputValue =
            {
                new object[] { 1, 2, "kate" },
                new object[] { 2, 1, "kate" },
                new object[] { }
            };
            var converter = FunnyTypeConverters.GetInputConverter(inputValue.GetType());

            Assert.AreEqual(FunnyType.ArrayOf(FunnyType.ArrayOf(FunnyType.Anything)), converter.FunnyType);
            var value = converter.ToFunObject(inputValue);

            Assert.IsInstanceOf <ImmutableFunArray>(value);
            Assert.AreEqual(3, ((ImmutableFunArray)value).Count);
        }