public void TestString() { CorePackage.Entity.DataType my_string = CorePackage.Entity.Type.Scalar.String; //Different, Equal, Greater, GreaterEqual, Less, LessEqual TestAuxiliary.HandleOperations <bool>( new List <CorePackage.Execution.Operator> { new CorePackage.Execution.Operators.Different(my_string, my_string), new CorePackage.Execution.Operators.Equal(my_string, my_string) }, new List <Func <CorePackage.Execution.Instruction, bool> > { (CorePackage.Execution.Instruction i) => { i.SetInputValue(CorePackage.Global.Operator.Left, "duly"); i.SetInputValue(CorePackage.Global.Operator.Right, "apero"); return(true); }, (CorePackage.Execution.Instruction i) => { i.SetInputValue(CorePackage.Global.Operator.Left, "duly"); i.SetInputValue(CorePackage.Global.Operator.Right, "duly"); return(true); }, (CorePackage.Execution.Instruction i) => { i.SetInputValue(CorePackage.Global.Operator.Left, "apero"); i.SetInputValue(CorePackage.Global.Operator.Right, "duly"); return(true); } }, new List <List <bool> > { new List <bool> { true, false }, new List <bool> { false, true }, new List <bool> { true, false } }); //Add }
public void ScalarOperators() { CorePackage.Entity.DataType integer = CorePackage.Entity.Type.Scalar.Integer, floating = CorePackage.Entity.Type.Scalar.Floating, character = CorePackage.Entity.Type.Scalar.Character, boolean = CorePackage.Entity.Type.Scalar.Boolean, stringc = CorePackage.Entity.Type.Scalar.String; //Test integer Assert.IsTrue(integer.OperatorAdd(3, 4) == 7); Assert.IsTrue(integer.OperatorSub(3, 4) == -1); Assert.IsTrue(integer.OperatorMul(3, 4) == 12); Assert.IsTrue(integer.OperatorDiv(8, 4) == 2); Assert.IsTrue(integer.OperatorMod(7, 3) == 1); Assert.IsTrue(integer.OperatorGt(4, 3)); Assert.IsTrue(integer.OperatorGtEq(4, 3) && integer.OperatorGtEq(4, 4)); Assert.IsTrue(integer.OperatorLt(3, 4)); Assert.IsTrue(integer.OperatorLtEq(3, 4) && integer.OperatorLtEq(4, 4)); Assert.IsTrue(integer.OperatorEqual(4, 4)); Assert.IsTrue(integer.OperatorBAnd(0b1111, 0b1010) == 0b1010); Assert.IsTrue(integer.OperatorBOr(0b0000, 0b1010) == 0b1010); Assert.IsTrue(integer.OperatorRightShift(0b1000, 3) == 0b1); Assert.IsTrue(integer.OperatorLeftShift(0b1, 3) == 0b1000); Assert.IsTrue(integer.OperatorXor(0b1100, 0b1010) == 0b0110); Assert.IsTrue(integer.OperatorBNot(0xFFFFFFFF) == 0b0); //Test floating Assert.IsTrue(floating.OperatorAdd(3.14, 4.13) == 7.27); Assert.IsTrue(Math.Round(floating.OperatorSub(3.14, 0.28), 2) == 2.86); Assert.IsTrue(Math.Round(floating.OperatorMul(3.14, 4.13), 4) == 12.9682); Assert.IsTrue(Math.Round(floating.OperatorDiv(3.14, 3.2), 5) == 0.98125); Assert.IsTrue(floating.OperatorGt(4.28, 3.14)); Assert.IsTrue(floating.OperatorGtEq(4.28, 3.14) && floating.OperatorGtEq(4.28, 4.28)); Assert.IsTrue(floating.OperatorLt(3.14, 4.28)); Assert.IsTrue(floating.OperatorLtEq(3.14, 4.28) && floating.OperatorLtEq(3.14, 3.14)); Assert.IsTrue(floating.OperatorEqual(3.14, 3.14)); //Test character Assert.IsTrue(character.OperatorGt('c', 'a')); Assert.IsTrue(character.OperatorGtEq('c', 'a') && character.OperatorGtEq('c', 'c')); Assert.IsTrue(character.OperatorLt('a', 'c')); Assert.IsTrue(character.OperatorLtEq('a', 'c') && character.OperatorLtEq('a', 'a')); Assert.IsTrue(character.OperatorEqual('a', 'a')); //Test boolean Assert.IsTrue( boolean.OperatorBAnd(true, true) && !boolean.OperatorBAnd(true, false) && !boolean.OperatorBAnd(false, true) && !boolean.OperatorBAnd(false, false)); Assert.IsTrue( boolean.OperatorBOr(true, true) && boolean.OperatorBOr(true, false) && boolean.OperatorBOr(false, true) && !boolean.OperatorBOr(false, false)); Assert.IsTrue( !boolean.OperatorXor(true, true) && boolean.OperatorXor(true, false) && boolean.OperatorXor(false, true) && !boolean.OperatorXor(false, false)); //Test string Assert.IsTrue(stringc.OperatorAdd("salut", "salut") == "salutsalut"); Assert.IsTrue(stringc.OperatorEqual("hello", "hello")); Assert.IsTrue(stringc.OperatorAccess("salut", 3) == 'u'); }
public void TestCharacter() { CorePackage.Entity.DataType character = CorePackage.Entity.Type.Scalar.Character; //Different, Equal, Greater, GreaterEqual, Less, LessEqual TestAuxiliary.HandleOperations <bool>( new List <CorePackage.Execution.Operator> { new CorePackage.Execution.Operators.Different(character, character), new CorePackage.Execution.Operators.Equal(character, character), new CorePackage.Execution.Operators.Greater(character, character), new CorePackage.Execution.Operators.GreaterEqual(character, character), new CorePackage.Execution.Operators.Less(character, character), new CorePackage.Execution.Operators.LessEqual(character, character) }, new List <Func <CorePackage.Execution.Instruction, bool> > { (CorePackage.Execution.Instruction i) => { i.SetInputValue(CorePackage.Global.Operator.Left, 'A'); i.SetInputValue(CorePackage.Global.Operator.Right, 'R'); return(true); }, (CorePackage.Execution.Instruction i) => { i.SetInputValue(CorePackage.Global.Operator.Left, 'A'); i.SetInputValue(CorePackage.Global.Operator.Right, 'A'); return(true); }, (CorePackage.Execution.Instruction i) => { i.SetInputValue(CorePackage.Global.Operator.Left, 'R'); i.SetInputValue(CorePackage.Global.Operator.Right, 'A'); return(true); } }, new List <List <bool> > { new List <bool> { true, false, false, false, true, true }, new List <bool> { false, true, false, true, false, true }, new List <bool> { true, false, true, true, false, false } }); }
public void TestFloating() { CorePackage.Entity.DataType floating = CorePackage.Entity.Type.Scalar.Floating; //Different, Equal, Greater, GreaterEqual, Less, LessEqual TestAuxiliary.HandleOperations <bool>( new List <CorePackage.Execution.Operator> { new CorePackage.Execution.Operators.Different(floating, floating), new CorePackage.Execution.Operators.Equal(floating, floating), new CorePackage.Execution.Operators.Greater(floating, floating), new CorePackage.Execution.Operators.GreaterEqual(floating, floating), new CorePackage.Execution.Operators.Less(floating, floating), new CorePackage.Execution.Operators.LessEqual(floating, floating) }, new List <Func <CorePackage.Execution.Instruction, bool> > { (CorePackage.Execution.Instruction i) => { i.SetInputValue(CorePackage.Global.Operator.Left, 3.14); i.SetInputValue(CorePackage.Global.Operator.Right, 4.2); return(true); }, (CorePackage.Execution.Instruction i) => { i.SetInputValue(CorePackage.Global.Operator.Left, 4.2); i.SetInputValue(CorePackage.Global.Operator.Right, 4.2); return(true); }, (CorePackage.Execution.Instruction i) => { i.SetInputValue(CorePackage.Global.Operator.Left, 4.2); i.SetInputValue(CorePackage.Global.Operator.Right, 3.14); return(true); } }, new List <List <bool> > { new List <bool> { true, false, false, false, true, true }, new List <bool> { false, true, false, true, false, true }, new List <bool> { true, false, true, true, false, false } }); //Add, Divide, Multiplicate, Substract, Modulo TestAuxiliary.HandleOperations <double>( new List <CorePackage.Execution.Operator> { new CorePackage.Execution.Operators.Add(floating, floating, floating), new CorePackage.Execution.Operators.Divide(floating, floating, floating), new CorePackage.Execution.Operators.Multiplicate(floating, floating, floating), new CorePackage.Execution.Operators.Substract(floating, floating, floating), new CorePackage.Execution.Operators.Modulo(floating, floating, floating) }, new List <Func <CorePackage.Execution.Instruction, bool> > { (CorePackage.Execution.Instruction i) => { i.SetInputValue(CorePackage.Global.Operator.Left, 3.14); i.SetInputValue(CorePackage.Global.Operator.Right, 4.2); return(true); }, (CorePackage.Execution.Instruction i) => { i.SetInputValue(CorePackage.Global.Operator.Left, 0.0); i.SetInputValue(CorePackage.Global.Operator.Right, 4.2); return(true); }, (CorePackage.Execution.Instruction i) => { i.SetInputValue(CorePackage.Global.Operator.Left, 3.14); i.SetInputValue(CorePackage.Global.Operator.Right, -4.2); return(true); }, (CorePackage.Execution.Instruction i) => { i.SetInputValue(CorePackage.Global.Operator.Left, -3.14); i.SetInputValue(CorePackage.Global.Operator.Right, -4.2); return(true); } }, new List <List <double> > { new List <double> { 3.14 + 4.2, 3.14 / 4.2, 3.14 * 4.2, 3.14 - 4.2, 3.14 % 4.2 }, new List <double> { 0 + 4.2, 0 / 4.2, 0 * 4.2, 0 - 4.2, 0 % 4.2 }, new List <double> { 3.14 + -4.2, 3.14 / -4.2, 3.14 * -4.2, 3.14 - -4.2, 3.14 % -4.2 }, new List <double> { -3.14 + -4.2, -3.14 / -4.2, -3.14 * -4.2, -3.14 - -4.2, -3.14 % -4.2 } }); //Decrement, Increment, Inverse TestAuxiliary.HandleOperations <double>( new List <CorePackage.Execution.Operator> { new CorePackage.Execution.Operators.Decrement(floating, floating), new CorePackage.Execution.Operators.Increment(floating, floating), new CorePackage.Execution.Operators.Inverse(floating, floating) }, new List <Func <CorePackage.Execution.Instruction, bool> > { (CorePackage.Execution.Instruction i) => { i.SetInputValue("Operand", 4.2); return(true); }, (CorePackage.Execution.Instruction i) => { i.SetInputValue("Operand", -4.2); return(true); } }, new List <List <double> > { new List <double> { 3.2, 5.2, -4.2 }, new List <double> { -5.2, -3.2, 4.2 } }); }
public void TestInteger() { CorePackage.Entity.DataType integer = CorePackage.Entity.Type.Scalar.Integer; //Different, Equal, Greater, GreaterEqual, Less, LessEqual TestAuxiliary.HandleOperations <bool>( new List <CorePackage.Execution.Operator> { new CorePackage.Execution.Operators.Different(integer, integer), new CorePackage.Execution.Operators.Equal(integer, integer), new CorePackage.Execution.Operators.Greater(integer, integer), new CorePackage.Execution.Operators.GreaterEqual(integer, integer), new CorePackage.Execution.Operators.Less(integer, integer), new CorePackage.Execution.Operators.LessEqual(integer, integer) }, new List <Func <CorePackage.Execution.Instruction, bool> > { //greater (CorePackage.Execution.Instruction i) => { i.SetInputValue(CorePackage.Global.Operator.Left, 42); i.SetInputValue(CorePackage.Global.Operator.Right, -42); return(true); }, //equal (CorePackage.Execution.Instruction i) => { i.SetInputValue(CorePackage.Global.Operator.Left, 42); i.SetInputValue(CorePackage.Global.Operator.Right, 42); return(true); }, //less (CorePackage.Execution.Instruction i) => { i.SetInputValue(CorePackage.Global.Operator.Left, -42); i.SetInputValue(CorePackage.Global.Operator.Right, 42); return(true); } }, new List <List <bool> > { new List <bool> { true, false, true, true, false, false }, new List <bool> { false, true, false, true, false, true }, new List <bool> { true, false, false, false, true, true } } ); //Add, BinaryAnd, BinaryOr, Divide, LeftShift, Modulo, Multiplicate, RightShift, Substract, Xor TestAuxiliary.HandleOperations <int>( new List <CorePackage.Execution.Operator> { new CorePackage.Execution.Operators.Add(integer, integer, integer), new CorePackage.Execution.Operators.BinaryAnd(integer, integer, integer), new CorePackage.Execution.Operators.BinaryOr(integer, integer, integer), new CorePackage.Execution.Operators.Divide(integer, integer, integer), new CorePackage.Execution.Operators.LeftShift(integer, integer, integer), new CorePackage.Execution.Operators.Modulo(integer, integer, integer), new CorePackage.Execution.Operators.Multiplicate(integer, integer, integer), new CorePackage.Execution.Operators.RightShift(integer, integer, integer), new CorePackage.Execution.Operators.Substract(integer, integer, integer), new CorePackage.Execution.Operators.Xor(integer, integer, integer) }, new List <Func <CorePackage.Execution.Instruction, bool> > { (CorePackage.Execution.Instruction i) => { i.SetInputValue(CorePackage.Global.Operator.Left, 42); i.SetInputValue(CorePackage.Global.Operator.Right, -42); return(true); }, (CorePackage.Execution.Instruction i) => { i.SetInputValue(CorePackage.Global.Operator.Left, 0); i.SetInputValue(CorePackage.Global.Operator.Right, 42); return(true); }, (CorePackage.Execution.Instruction i) => { i.SetInputValue(CorePackage.Global.Operator.Left, 42); i.SetInputValue(CorePackage.Global.Operator.Right, 42); return(true); }, (CorePackage.Execution.Instruction i) => { i.SetInputValue(CorePackage.Global.Operator.Left, -42); i.SetInputValue(CorePackage.Global.Operator.Right, -42); return(true); } }, new List <List <int> > { new List <int> { 0, 2, -2, -1, 176160768, 0, -1764, 0, 84, -4 }, new List <int> { 42, 0, 42, 0, 0, 0, 0, 0, -42, 42 }, new List <int> { 84, 42, 42, 1, 43008, 0, 1764, 0, 0, 0 }, new List <int> { -84, -42, -42, 1, -176160768, 0, 1764, -1, 0, 0 } } ); //BinaryNot, Decrement, Increment, Inverse TestAuxiliary.HandleOperations <int>( new List <CorePackage.Execution.Operator> { new CorePackage.Execution.Operators.BinaryNot(integer, integer), new CorePackage.Execution.Operators.Decrement(integer, integer), new CorePackage.Execution.Operators.Increment(integer, integer), new CorePackage.Execution.Operators.Inverse(integer, integer) }, new List <Func <CorePackage.Execution.Instruction, bool> > { (CorePackage.Execution.Instruction i) => { i.SetInputValue("Operand", 42); return(true); }, (CorePackage.Execution.Instruction i) => { i.SetInputValue("Operand", -42); return(true); } }, new List <List <int> > { new List <int> { -43, 41, 43, -42 }, new List <int> { 41, -43, -41, 42 } }); }
public void TestObjectActions() { CorePackage.Entity.Type.ObjectType type = new CorePackage.Entity.Type.ObjectType(); CorePackage.Entity.DataType integer = CorePackage.Entity.Type.Scalar.Integer; type.AddAttribute("x", integer, CorePackage.Global.AccessMode.EXTERNAL); type.AddAttribute("y", integer, CorePackage.Global.AccessMode.EXTERNAL); type.AddAttribute("z", integer, CorePackage.Global.AccessMode.EXTERNAL); CorePackage.Entity.Function getAttrSum = new CorePackage.Entity.Function(); type.Declare(getAttrSum, "getAttrSum", CorePackage.Global.AccessMode.EXTERNAL); type.SetFunctionAsMember("getAttrSum"); CorePackage.Entity.Variable res = new CorePackage.Entity.Variable(CorePackage.Entity.Type.Scalar.Integer); res.Name = "res"; getAttrSum.Declare(res, "res", CorePackage.Global.AccessMode.EXTERNAL); getAttrSum.SetVariableAs("res", CorePackage.Entity.Function.VariableRole.RETURN); Dictionary <string, dynamic> t = type.Instantiate(); Assert.IsTrue(t.ContainsKey("x") && t.ContainsKey("y") && t.ContainsKey("z")); CorePackage.Entity.Variable tvar = new CorePackage.Entity.Variable(type); tvar.Value["x"] = 3; tvar.Value["y"] = 42; tvar.Value["z"] = -29; //show fields uint getT = getAttrSum.addInstruction(new CorePackage.Execution.Getter(getAttrSum.GetParameter("this"))); uint getAttrs = getAttrSum.addInstruction(new CorePackage.Execution.GetAttributes(type)); getAttrSum.LinkInstructionData(getT, "reference", getAttrs, "this"); uint setRes = getAttrSum.addInstruction(new CorePackage.Execution.Setter(res)); //x + y uint xPy = getAttrSum.addInstruction(new CorePackage.Execution.Operators.Add(CorePackage.Entity.Type.Scalar.Integer, CorePackage.Entity.Type.Scalar.Integer, CorePackage.Entity.Type.Scalar.Integer)); getAttrSum.LinkInstructionData(getAttrs, "x", xPy, CorePackage.Global.Operator.Left); getAttrSum.LinkInstructionData(getAttrs, "y", xPy, CorePackage.Global.Operator.Right); //x + y + z uint xPyPz = getAttrSum.addInstruction(new CorePackage.Execution.Operators.Add(CorePackage.Entity.Type.Scalar.Integer, CorePackage.Entity.Type.Scalar.Integer, CorePackage.Entity.Type.Scalar.Integer)); getAttrSum.LinkInstructionData(xPy, CorePackage.Global.Operator.Result, xPyPz, CorePackage.Global.Operator.Left); getAttrSum.LinkInstructionData(getAttrs, "z", xPyPz, CorePackage.Global.Operator.Right); getAttrSum.LinkInstructionData(xPyPz, CorePackage.Global.Operator.Result, setRes, "value"); getAttrSum.setEntryPoint(setRes); //System.IO.File.WriteAllText("toto.dot", getAttrSum.ToDotFile()); getAttrSum.Call(new Dictionary <string, dynamic> { { "this", tvar.Value } }); Console.WriteLine(getAttrSum.GetReturnValue("res")); Assert.IsTrue(getAttrSum.GetReturnValue("res") == 16); CorePackage.Entity.Function addOp = (CorePackage.Entity.Function)type.Declare(new CorePackage.Entity.Function(), "Add", CorePackage.Global.AccessMode.EXTERNAL); // Object Add(Object this, Objet RightOperand); type.SetFunctionAsMember("Add"); CorePackage.Entity.Variable rgt = new CorePackage.Entity.Variable(type); rgt.Name = CorePackage.Global.Operator.Right; addOp.Declare(rgt, CorePackage.Global.Operator.Right, CorePackage.Global.AccessMode.EXTERNAL); addOp.SetVariableAs(CorePackage.Global.Operator.Right, CorePackage.Entity.Function.VariableRole.PARAMETER); CorePackage.Entity.Variable rs = new CorePackage.Entity.Variable(type); rs.Name = CorePackage.Global.Operator.Result; addOp.Declare(rs, CorePackage.Global.Operator.Result, CorePackage.Global.AccessMode.EXTERNAL); addOp.SetVariableAs(CorePackage.Global.Operator.Result, CorePackage.Entity.Function.VariableRole.RETURN); type.OverloadOperator(CorePackage.Global.Operator.Name.ADD, "Add"); /* * * result.x = this.x + RightOperand.x; * result.y = this.y + RightOperand.y; * result.z = this.z + RightOperand.z; * */ uint getThis = addOp.addInstruction(new CorePackage.Execution.Getter(addOp.GetParameter("this"))); uint splitThis = addOp.addInstruction(new CorePackage.Execution.GetAttributes(type)); addOp.LinkInstructionData(getThis, "reference", splitThis, "this"); uint getROP = addOp.addInstruction(new CorePackage.Execution.Getter(addOp.GetParameter(CorePackage.Global.Operator.Right))); uint splitROP = addOp.addInstruction(new CorePackage.Execution.GetAttributes(type)); addOp.LinkInstructionData(getROP, "reference", splitROP, "this"); //this.x + RightOperand.x uint addX = addOp.addInstruction(new CorePackage.Execution.Operators.Add(integer, integer, integer)); addOp.LinkInstructionData(splitThis, "x", addX, CorePackage.Global.Operator.Left); addOp.LinkInstructionData(splitROP, "x", addX, CorePackage.Global.Operator.Right); //this.y + RightOperand.y uint addY = addOp.addInstruction(new CorePackage.Execution.Operators.Add(integer, integer, integer)); addOp.LinkInstructionData(splitThis, "y", addY, CorePackage.Global.Operator.Left); addOp.LinkInstructionData(splitROP, "y", addY, CorePackage.Global.Operator.Right); //this.z + RightOperand.z uint addZ = addOp.addInstruction(new CorePackage.Execution.Operators.Add(integer, integer, integer)); addOp.LinkInstructionData(splitThis, "z", addZ, CorePackage.Global.Operator.Left); addOp.LinkInstructionData(splitROP, "z", addZ, CorePackage.Global.Operator.Right); uint getRes = addOp.addInstruction(new CorePackage.Execution.Getter(addOp.GetReturn(CorePackage.Global.Operator.Result))); uint splitRes = addOp.addInstruction(new CorePackage.Execution.GetAttributes(type)); addOp.LinkInstructionData(getRes, "reference", splitRes, "this"); uint setResult = addOp.addInstruction(new CorePackage.Execution.SetAttribute(type)); addOp.LinkInstructionData(getRes, "reference", setResult, "this"); addOp.LinkInstructionData(addX, CorePackage.Global.Operator.Result, setResult, "x"); addOp.LinkInstructionData(addY, CorePackage.Global.Operator.Result, setResult, "y"); addOp.LinkInstructionData(addZ, CorePackage.Global.Operator.Result, setResult, "z"); addOp.setEntryPoint(setResult); var toadd = type.Instantiate(); toadd["x"] = 12; toadd["y"] = 20; toadd["z"] = -42; tvar.Value["x"] = 3; tvar.Value["y"] = 42; tvar.Value["z"] = -29; var addition = type.OperatorAdd(toadd, tvar.Value); Assert.IsTrue(addition["x"] == 15); Assert.IsTrue(addition["y"] == 62); Assert.IsTrue(addition["z"] == -71); }