public void ComposeOneFunctionTest() { Executeable<int> linearfunction = (Func<int, int>)((int x) => 2 * x + 10); Composition<int> comp = new Composition<int>(linearfunction); Assert.AreEqual(10, comp.Execute(0)); Assert.AreEqual(14, comp.Execute(2)); Assert.AreEqual(20, comp.Execute(5)); }
static void Main(string[] args) { IExecute<int> identity = new SingleFunction<int>(x => x); IExecute<int> linearfunction = new SingleFunction<int>(x => 3 * x - 2); IExecute<int> quadraticfunction = new SingleFunction<int>(x => 2 * x * x - 5); IExecute<int> cubicfunction = new SingleFunction<int>(x => x * x * x + x * x + x + 1); var functionList = new[] { identity, linearfunction, quadraticfunction, cubicfunction }; var comp = new Composition<int>(functionList); Console.WriteLine(comp.Execute(5)); Console.ReadKey(); }
public void AddingFunctionToCompositionTest() { Executeable<int> identity = (Func<int, int>)((int x) => x); Composition<int> comp = new Composition<int>(identity); Assert.AreEqual(0, (int)comp.Execute(0)); Assert.AreEqual(2, (int)comp.Execute(2)); Executeable<int> linearfunction = (Func<int, int>)((int x) => x + 2); comp.Add(linearfunction); Assert.AreEqual(2, (int)comp.Execute(0)); Assert.AreEqual(4, (int)comp.Execute(2)); Executeable<int> cubicfunction = (Func<int, int>)((int x) => x * x * x); comp.Add(cubicfunction); Assert.AreEqual(8, (int)comp.Execute(0)); Assert.AreEqual(64, (int)comp.Execute(2)); }
public void AddingFunctionToComposition() { IExecute<int> identity = new SingleFunction<int>(x => x); var comp = new Composition<int>(identity); Assert.AreEqual(0, comp.Execute(0)); Assert.AreEqual(2, comp.Execute(2)); IExecute<int> linearFunction = new SingleFunction<int>(x => x + 2); comp.Add(linearFunction); Assert.AreEqual(2, comp.Execute(0)); Assert.AreEqual(4, comp.Execute(2)); IExecute<int> cubicfunction = new SingleFunction<int>(x => x * x * x); comp.Add(cubicfunction); Assert.AreEqual(8, comp.Execute(0)); Assert.AreEqual(64, comp.Execute(2)); }
public void ComposeTwoFunctionsTest() { Executeable<int> linearfunction = (Func<int, int>)((int x) => 2 * x + 5); Executeable<int> quadraticfunction = (Func<int, int>)((int x) => x* x); Composition<int> comp = new Composition<int>(linearfunction, quadraticfunction); Assert.AreEqual(25, (int)comp.Execute(0)); Assert.AreEqual(81, (int)comp.Execute(2)); Assert.AreEqual(225, (int)comp.Execute(5)); comp = new Composition<int>(quadraticfunction, linearfunction); Assert.AreEqual(5, (int)comp.Execute(0)); Assert.AreEqual(13, (int)comp.Execute(2)); Assert.AreEqual(55, (int)comp.Execute(5)); comp = new Composition<int>(linearfunction, linearfunction); Assert.AreEqual(15, (int)comp.Execute(0)); Assert.AreEqual(23, (int)comp.Execute(2)); Assert.AreEqual(35, (int)comp.Execute(5)); comp = new Composition<int>(quadraticfunction, quadraticfunction); Assert.AreEqual(0, (int)comp.Execute(0)); Assert.AreEqual(16, (int)comp.Execute(2)); Assert.AreEqual(625, (int)comp.Execute(5)); }
public void ComposeThreeFunctionsTest() { Executeable<int> identity = (Func<int, int>)((int x) => x); Executeable<int> linearfunction = (Func<int, int>)((int x) => x + 2); Executeable<int> cubicfunction = (Func<int, int>)((int x) => x * x * x - 1); List<Executeable<int>> functions = new List<Executeable<int>>(); functions.Add(identity); functions.Add(linearfunction); functions.Add(cubicfunction); Composition<int> compose = new Composition<int>(functions.ToArray()); Assert.AreEqual(7, (int)compose.Execute(0)); Assert.AreEqual(63, (int)compose.Execute(2)); Assert.AreEqual(26, (int)compose.Execute(1)); }
public void CompositionOfCompositionsTest() { Executeable<int> linearfunction = (Func<int, int>)((int x) => x + 2); Executeable<int> quadraticfunction = (Func<int, int>)((int x) => 2 * x * x); Composition<int> comp1 = new Composition<int>(linearfunction, quadraticfunction); Composition<int> comp2 = new Composition<int>(quadraticfunction, linearfunction); Composition<int> compOfComps = new Composition<int>(comp1, comp2); Assert.AreEqual(130, (int)compOfComps.Execute(0)); Assert.AreEqual(650, (int)compOfComps.Execute(1)); Assert.AreEqual(2050, (int)compOfComps.Execute(2)); }
public void ComposeOneFunction() { IExecute<int> linearfunction = new SingleFunction<int>(x => 2 * x + 10); var comp = new Composition<int>(linearfunction); Assert.AreEqual(10, comp.Execute(0)); Assert.AreEqual(14, comp.Execute(2)); Assert.AreEqual(20, comp.Execute(5)); }
public void CompositionOfCompositions() { IExecute<int> linearfunction = new SingleFunction<int>(x => x + 2); IExecute<int> quadraticfunction = new SingleFunction<int>(x => 2 * x * x); var comp1 = new Composition<int>(linearfunction, quadraticfunction); var comp2 = new Composition<int>(quadraticfunction, linearfunction); var compOfComps = new Composition<int>(comp1, comp2); Assert.AreEqual(130, compOfComps.Execute(0)); Assert.AreEqual(650, compOfComps.Execute(1)); Assert.AreEqual(2050, compOfComps.Execute(2)); }
public void ComposeTwoFunctions() { IExecute<int> linearfunction = new SingleFunction<int>(x => 2 * x + 5); IExecute<int> quadraticfunction = new SingleFunction<int>(x => x * x); var comp = new Composition<int>(linearfunction, quadraticfunction); Assert.AreEqual(25, comp.Execute(0)); Assert.AreEqual(81, comp.Execute(2)); Assert.AreEqual(225, comp.Execute(5)); comp = new Composition<int>(quadraticfunction, linearfunction); Assert.AreEqual(5, comp.Execute(0)); Assert.AreEqual(13, comp.Execute(2)); Assert.AreEqual(55, comp.Execute(5)); comp = new Composition<int>(linearfunction, linearfunction); Assert.AreEqual(15, comp.Execute(0)); Assert.AreEqual(23, comp.Execute(2)); Assert.AreEqual(35, comp.Execute(5)); comp = new Composition<int>(quadraticfunction, quadraticfunction); Assert.AreEqual(0, comp.Execute(0)); Assert.AreEqual(16, comp.Execute(2)); Assert.AreEqual(625, comp.Execute(5)); }
public void ComposeThreeFunctions() { IExecute<int> identity = new SingleFunction<int>(x => x); IExecute<int> linearFunction = new SingleFunction<int>(x => x + 2); IExecute<int> cubicfunction = new SingleFunction<int>(x => x * x * x - 1); var functionList = new[] { identity, linearFunction, cubicfunction }; var comp = new Composition<int>(functionList); Assert.AreEqual(7, comp.Execute(0)); Assert.AreEqual(63, comp.Execute(2)); Assert.AreEqual(26, comp.Execute(1)); }