public void AutoGeneratesValuesCorrectly(string key, UInt16 expectedValue) { var operationDictionary = new OperationDictionary(); var actual = operationDictionary[key].Calculate(); Assert.Equal(expectedValue, actual); }
private static OperationDictionary BuildOperations(IEnumerable <KeyValuePair <string, string> > operations) { var ops = new OperationDictionary(); foreach (var operation in operations) { var operationInputs = operation.Value.Split(' '); switch (operationInputs.Count()) { case 1: UInt16 parsedInput; if (UInt16.TryParse(operationInputs[0], out parsedInput)) { ops[operation.Key] = new Operation(parsedInput); } else { ops[operation.Key] = new Operation(() => ops[operationInputs[0]], Operation.Load); } break; case 2: if (operationInputs[0] == "NOT") { ops[operation.Key] = new Operation(() => ops[operationInputs[1]], Operation.Not); } break; case 3: switch (operationInputs[1]) { case "LSHIFT": ops[operation.Key] = new Operation(() => ops[operationInputs[0]], () => ops[operationInputs[2]], Operation.LeftShift); break; case "RSHIFT": ops[operation.Key] = new Operation(() => ops[operationInputs[0]], () => ops[operationInputs[2]], Operation.RightShift); break; case "AND": ops[operation.Key] = new Operation(() => ops[operationInputs[0]], () => ops[operationInputs[2]], Operation.And); break; case "OR": ops[operation.Key] = new Operation(() => ops[operationInputs[0]], () => ops[operationInputs[2]], Operation.Or); break; default: throw new InvalidOperationException(); } break; default: throw new InvalidOperationException(); } } return(ops); }
//holds operands and alwasys both //however if exactly unary function - meaningfull just right //the left will be empty public Operation(string name, FormulaObject _left, FormulaObject _right) { if (!OperationDictionary.ContainsKey(name)) { throw new Exception("Unacceptable formula : " + name); } index = OperationDictionary[name]; left = _left; right = _right; wasActivated = false; }