private static string ToJsonString <G>(IValuable <G> input, int maxDepth = 30) where G : IGroupState <G>, new() { string jsonValue = null; if (input == null) { jsonValue = "null"; } else if (input is ICollectionValue <G> collectionValue) { jsonValue = maxDepth <= 0 ? "overflow" : collectionValue is DefaultCollectionValue <G> defaultCollectionValue && (defaultCollectionValue.IsMap || defaultCollectionValue == null) ? $"{{{string.Join(", ", collectionValue.GetEntries().Select(entry => $"{ToJsonString(entry.Key, 2)}: {ToJsonString(entry.Value.Value, maxDepth - 1)}"))}}}" : $"[{string.Join(", ", collectionValue.GetValues().Select(item => ToJsonString(item.Value, maxDepth - 1)))}]"; } else if (input is IActionValue <G> ) { jsonValue = "action pointer"; } else if (input is IGroupValue <G> groupValue) { jsonValue = $"{groupValue.State.Group.GroupName} pointer"; } else { IValue value = input as IValue; jsonValue = ToValueString(value?.GetData() ?? input?.ToString() ?? "undefined"); } return(jsonValue); }
public void DefaultDoubleValue_IsEqualTo(double value, object otherVal, bool throwsException) { DefaultValueProvider <G> valueProvider = new DefaultValueProvider <G>(); DefaultDoubleValue <G> sut = new DefaultDoubleValue <G>(value); IValue <G> newValue = null; Action action = () => { newValue = (IValue <G>)sut.IsEqualTo(otherVal.GetAsValue(), valueProvider); }; if (throwsException) { Assert.Throws <EngineRuntimeException>(action); } else { action(); } if (!throwsException) { switch (otherVal) { case double doubleVal: Assert.Equal(value == doubleVal, newValue.GetData()); break; case int intVal: Assert.Equal(value == intVal, newValue.GetData()); break; case long longVal: Assert.Equal(value == longVal, newValue.GetData()); break; default: Assert.False((bool)newValue.GetData()); break; } } }
public void DefaultIntValue_Remainder(int value, object otherVal, Type resultType, bool throwsException) { DefaultValueProvider <G> valueProvider = new DefaultValueProvider <G>(); DefaultIntValue <G> sut = new DefaultIntValue <G>(value); IValue <G> newValue = null; Action action = () => { newValue = (IValue <G>)sut.Remainder(otherVal.GetAsValue(), valueProvider); }; if (throwsException) { Assert.Throws <EngineRuntimeException>(action); } else { action(); } if (!throwsException) { switch (otherVal) { case double doubleVal: Assert.Equal(Convert.ChangeType(value % doubleVal, resultType), newValue.GetData()); break; case int intVal: Assert.Equal(Convert.ChangeType(value % intVal, resultType), newValue.GetData()); break; case long longVal: Assert.Equal(Convert.ChangeType(value % longVal, resultType), newValue.GetData()); break; } } }
private static void ExecuteInstructionRFactorial(IInstructionExecutor <GroupState> executor, ExecutionState <GroupState> executionState, object[] payload, StackList <ValuePointer <GroupState> > stackRegister, StackList <StackValuePointer <GroupState> > stackPointers) { IValue <GroupState> value = (IValue <GroupState>)stackRegister.Last().Value; int intVal = Convert.ToInt32(value.GetData()); long resultVal = 1; for (int i = intVal; i > 0; i--) { resultVal *= i; } stackRegister.SetLast(new ValuePointer <GroupState> { Value = executor.ValueProvider.GetReducedValue(resultVal) }); }
public void DefaultStringValue_IsNotEqualTo(object otherVal, bool throwsException) { string value = "hello"; DefaultValueProvider <G> valueProvider = new DefaultValueProvider <G>(); DefaultStringValue <G> sut = new DefaultStringValue <G>(value); IValue <G> newValue = null; Action action = () => { newValue = (IValue <G>)sut.IsNotEqualTo(valueProvider.GetAsValue(otherVal), valueProvider); }; if (throwsException) { Assert.Throws <EngineRuntimeException>(action); } else { action(); } if (!throwsException) { switch (otherVal) { case string strVal: Assert.Equal(value != strVal, newValue.GetData()); break; default: Assert.True((bool)newValue.GetData()); break; } } }
public void DefaultValueProvider_GetAsNullableString(string value) { DefaultValueProvider <G> sut = new DefaultValueProvider <G>(); IValue <G> result = sut.GetAsNullableString(value); Assert.Equal(value, result.GetData()); switch (value) { case null: Assert.IsAssignableFrom <INullValue <G> >(result); break; default: Assert.IsAssignableFrom <IValue <G, string> >(result); break; } }
public void DefaultStringValue_Minus(object otherVal, bool throwsException) { string value = "hello20true"; DefaultValueProvider <G> valueProvider = new DefaultValueProvider <G>(); DefaultStringValue <G> sut = new DefaultStringValue <G>(value); IValue <G> newValue = null; Action action = () => { newValue = (IValue <G>)sut.Minus(valueProvider.GetAsValue(otherVal), valueProvider); }; if (throwsException) { Assert.Throws <EngineRuntimeException>(action); } else { action(); } if (!throwsException) { switch (otherVal) { case null: Assert.Equal(sut, newValue); break; default: Assert.Equal(value.Replace(otherVal.ToString(), ""), newValue.GetData()); break; } } }
public static int ToInt(this IValue value) { return(Convert.ToInt32(value.GetData())); }
private static void ExecuteInstructionRPower(IInstructionExecutor <GroupState> executor, ExecutionState <GroupState> executionState, object[] payload, StackList <ValuePointer <GroupState> > stackRegister, StackList <StackValuePointer <GroupState> > stackPointers) { IValue <GroupState> secondValue = (IValue <GroupState>)stackRegister.TakeLast().Value; IValue <GroupState> firstValue = (IValue <GroupState>)stackRegister.Last().Value; IValuable <GroupState> result = executor.ValueProvider.GetReducedValue(Math.Pow(Convert.ToDouble(firstValue.GetData()), Convert.ToDouble(secondValue.GetData()))); stackRegister.SetLast(new ValuePointer <GroupState> { Value = result }); }