public void CanStructureToPrimitiveDouble() { double d = 3.1415926535897932384626433832795; var s = ScalarValue.Create(d); var dTest = kOS.Safe.Encapsulation.Structure.ToPrimitive(s); Assert.AreEqual(d, (double)dTest, 0d); }
public void CanImplicitCastToInt() { int i = 10; var s = ScalarValue.Create(i); int iTest = s; Assert.AreEqual(i, iTest); }
public void CanImplicitCastToDouble() { double d = 3.1415926535897932384626433832795; var s = ScalarValue.Create(d); double dTest = s; Assert.AreEqual(d, dTest); }
public void CanStructureToPrimitiveInt() { int i = 10; var s = ScalarValue.Create(i); var iTest = kOS.Safe.Encapsulation.Structure.ToPrimitive(s); Assert.AreEqual(i, (int)iTest); }
public void CanImplicitCastFromDouble() { double d = 3.1415926535897932384626433832795; var s = ScalarValue.Create(d); ScalarValue sTest = d; Assert.AreEqual(s, sTest); }
public void CanConvertInt() { int i = 10; var s = ScalarValue.Create(i); Assert.IsTrue(s.IsInt); Assert.IsFalse(s.IsDouble); Assert.AreEqual(i, (int)s); }
public override void Execute(SharedObjects shared) { string argument = PopValueAssert(shared).ToString(); AssertArgBottomAndConsume(shared); char result = argument.ToCharArray()[0]; ReturnValue = ScalarValue.Create((int)result); }
public void CanConvertDouble() { double d = 3.1415926535897932384626433832795; var s = ScalarValue.Create(d); Assert.IsFalse(s.IsInt); Assert.IsTrue(s.IsDouble); Assert.AreEqual(d, (double)s); }
public override ISuffixResult GetSuffix(string suffixName) { if (!IsResource(suffixName)) { return(base.GetSuffix(suffixName)); } var resourceAmount = GetResourceOfCurrentStage(suffixName); return(new SuffixResult(ScalarValue.Create(resourceAmount.HasValue ? resourceAmount.Value : 0.0))); }
public void CanAddMixedType() { double d1 = 10.5; int i2 = 5; var s1 = ScalarValue.Create(d1); var s2 = ScalarValue.Create(i2); double dResult = d1 + i2; ScalarValue sResult = s1 + s2; Assert.AreEqual(dResult, sResult, 0d); }
public void CanSubtract() { double d1 = 10.5; double d2 = 5.25; var s1 = ScalarValue.Create(d1); var s2 = ScalarValue.Create(d2); double dResult = d1 - d2; ScalarValue sResult = s1 - s2; Assert.AreEqual(dResult, sResult, 0d); }
public void CanDivide() { double d1 = 10.5; double d2 = 5.25; var s1 = ScalarValue.Create(d1); var s2 = ScalarValue.Create(d2); double dResult = d1 / d2; ScalarValue sResult = s1 / s2; Assert.AreEqual(dResult, sResult, 0d); }
public void CanPower() { double d1 = 10.5; double d2 = 5.25; var s1 = ScalarValue.Create(d1); var s2 = ScalarValue.Create(d2); double dResult = Math.Pow(d1, d2); ScalarValue sResult = s1 ^ s2; Assert.AreEqual(dResult, sResult, 0d); }
public void CanModulus() { double d1 = 10.5; double d2 = 5.25; var s1 = ScalarValue.Create(d1); var s2 = ScalarValue.Create(d2); double dResult = d1 % d2; ScalarValue sResult = s1 % s2; Assert.AreEqual(dResult, sResult, 0d); }
public void CanAddAsString() { double d1 = 10.5; string string1 = "foo"; var str1 = new StringValue(string1); var s1 = ScalarValue.Create(d1); var strResult = str1 + s1; string stringResult = string1 + d1.ToString(); Assert.AreEqual(stringResult, (string)strResult); }
public void CanCoerceAndExtendType() { var suffix = BuildBasicSetSuffix <ScalarIntValue>(); const int TEST_VALUE = 15; Assert.IsNotNull(suffix); suffix.Set(TEST_VALUE); var finalValue = suffix.Get().Value; Assert.AreEqual(ScalarValue.Create(TEST_VALUE), finalValue); }
public void CanCoerceAndTruncateType() { var suffix = BuildBasicSetSuffix <ScalarIntValue>(); const double TEST_VALUE = 15.1234d; const double TEST_VALUE_TRUNCATED = 15; Assert.IsNotNull(suffix); suffix.Set(TEST_VALUE); var finalValue = suffix.Get().Value; Assert.AreEqual(ScalarValue.Create(TEST_VALUE_TRUNCATED), finalValue); }
public override ISuffixResult GetSuffix(string suffixName) { string fixedName; if (!Utils.IsResource(suffixName, out fixedName)) { return(base.GetSuffix(suffixName)); } double resourceAmount = GetResourceOfCurrentStage(fixedName); return(new SuffixResult(ScalarValue.Create(resourceAmount))); }
public void CanAddIntegers() { var scalar1 = ScalarValue.Create(1); var scalar2 = ScalarValue.Create(1); var op = new OperandPair(scalar1, scalar2); var result = calc.Add(op); Assert.IsInstanceOf <ScalarValue>(result); var expected = scalar1 + scalar2; Assert.AreEqual(expected, result); }
public void CanCompareToScalar() { BooleanValue bv = new BooleanValue(true); ScalarValue sv = ScalarValue.Create(1); Assert.IsTrue(bv == sv); Assert.IsFalse(bv != sv); Assert.IsTrue(sv == bv); Assert.IsFalse(sv != bv); sv = ScalarValue.Create(0); Assert.IsTrue(bv != sv); Assert.IsFalse(bv == sv); Assert.IsTrue(sv != bv); Assert.IsFalse(sv == bv); sv = ScalarValue.Create(3.1415926535897932384626433832795); Assert.IsTrue(bv == sv); Assert.IsFalse(bv != sv); Assert.IsTrue(sv == bv); Assert.IsFalse(sv != bv); sv = ScalarValue.Create(0.0d); Assert.IsTrue(bv != sv); Assert.IsFalse(bv == sv); Assert.IsTrue(sv != bv); Assert.IsFalse(sv == bv); bv = new BooleanValue(false); sv = ScalarValue.Create(1); Assert.IsTrue(bv != sv); Assert.IsFalse(bv == sv); Assert.IsTrue(sv != bv); Assert.IsFalse(sv == bv); sv = ScalarValue.Create(0); Assert.IsTrue(bv == sv); Assert.IsFalse(bv != sv); Assert.IsTrue(sv == bv); Assert.IsFalse(sv != bv); sv = ScalarValue.Create(3.1415926535897932384626433832795); Assert.IsTrue(bv != sv); Assert.IsFalse(bv == sv); Assert.IsTrue(sv != bv); Assert.IsFalse(sv == bv); Assert.IsFalse(bv.Equals(sv)); sv = ScalarValue.Create(0.0d); Assert.IsTrue(bv == sv); Assert.IsFalse(bv != sv); Assert.IsTrue(sv == bv); Assert.IsFalse(sv != bv); Assert.IsFalse(bv.Equals(sv)); }
public void CanSimpleClamp() { const int MIN_VALUE = 0; const int MAX_VALUE = 1; const float SET_VALUE = 1.5f; float value = 0; var suffix = new ClampSetSuffix <ScalarValue>(() => value, i => value = i, MIN_VALUE, MAX_VALUE); suffix.Set(SET_VALUE); Assert.AreEqual(ScalarValue.Create(value), suffix.Get().Value); Assert.AreNotEqual(SET_VALUE, value); Assert.AreEqual(MAX_VALUE, value); }
public void CanSetSuffix() { const int TEST_VALUE = 12345; var structure = new StructureTest.TestStructure(); var strongBox = new StrongBox <ScalarIntValue>(TEST_VALUE); structure.TestAddInstanceSuffix("FOO", new SetSuffix <ScalarIntValue>(BuildBasicGetter(strongBox), BuildBasicSetter(strongBox))); structure.TestAddInstanceSuffix("BAR", new SetSuffix <ScalarIntValue>(BuildBasicGetter(strongBox), BuildBasicSetter(strongBox))); Assert.AreEqual(ScalarValue.Create(TEST_VALUE), structure.GetSuffix("FOO").Value); structure.SetSuffix("FOO", TEST_VALUE - 10); Assert.AreEqual(ScalarValue.Create(TEST_VALUE - 10), structure.GetSuffix("FOO").Value); structure.SetSuffix("FOO", TEST_VALUE / 20); Assert.AreEqual(ScalarValue.Create(TEST_VALUE / 20), structure.GetSuffix("FOO").Value); }
public override ISuffixResult GetSuffix(string suffixName, bool failOkay = false) { // Most suffixes are handled by the newer AddSuffix system, except for the // resource levels, which have to use this older technique as a fallback because // the AddSuffix system doesn't support this type of late-binding string matching: // Is this a resource? double dblValue; if (VesselUtils.TryGetResource(Vessel, suffixName, out dblValue)) { return(new SuffixResult(ScalarValue.Create(dblValue))); } return(base.GetSuffix(suffixName, failOkay)); }
public void CanComplexStepClamp() { const int MIN_VALUE = 0; const int MAX_VALUE = 1; const float SET_VALUE = 1.4f; const float EXPECTED_VALUE = MAX_VALUE; const float STEP_VALUE = 0.5f; float value = 0; var suffix = new ClampSetSuffix <ScalarValue>(() => value, i => value = i, MIN_VALUE, MAX_VALUE, STEP_VALUE); suffix.Set(SET_VALUE); Assert.AreEqual(ScalarValue.Create(value), suffix.Get().Value); Assert.AreNotEqual(SET_VALUE, value); Assert.AreEqual(EXPECTED_VALUE, value); }
public void CanGetDelegateValueType() { const int VALUE = 12345; var suffix = new NoArgsSuffix <Encapsulation.Structure>(() => ScalarValue.Create(VALUE)); var del = suffix.Get(); Assert.IsNotNull(del); cpu.PushStack(null); // dummy variable for ReverseStackArgs to pop cpu.PushStack(new KOSArgMarkerType()); del.Invoke(cpu); var value = del.Value; Assert.IsNotNull(value); Assert.IsInstanceOf <ScalarValue>(value); Assert.AreEqual(ScalarValue.Create(VALUE), value); }
public void CanNullCheck() { ScalarValue sv = null; Assert.IsTrue(sv == null); Assert.IsFalse(sv != null); sv = ScalarValue.Create(1); Assert.IsTrue(sv != null); Assert.IsFalse(sv == null); Assert.IsTrue(null != sv); Assert.IsFalse(null == sv); sv = ScalarValue.Create(0); Assert.IsTrue(sv != null); Assert.IsFalse(sv == null); Assert.IsTrue(null != sv); Assert.IsFalse(null == sv); sv = ScalarValue.Create(3.1415926535897932384626433832795); Assert.IsTrue(sv != null); Assert.IsFalse(sv == null); Assert.IsTrue(null != sv); Assert.IsFalse(null == sv); }
public void Invoke(ICpu cpu) { var args = new List <object>(); var paramArrayArgs = new List <Structure>(); // Will be true iff the lastmost parameter of the delegate is using the C# 'param' keyword and thus // expects the remainder of the arguments marshalled together into one array object. bool isParamArrayArg = false; CpuUtility.ReverseStackArgs(cpu, false); for (int i = 0; i < delInfo.Parameters.Length; ++i) { DelegateParameter paramInfo = delInfo.Parameters[i]; object arg = cpu.PopValueArgument(); Type argType = arg.GetType(); isParamArrayArg = i == delInfo.Parameters.Length - 1 && delInfo.Parameters[i].IsParams; if (arg != null && arg.GetType() == CpuUtility.ArgMarkerType) { if (isParamArrayArg) { break; // with param arguments, you want to consume everything to the arg bottom - it's normal. } else { throw new KOSArgumentMismatchException(delInfo.Parameters.Length, delInfo.Parameters.Length - (i + 1)); } } // Either the expected type of this one parameter, or if it's a 'param' array as the last arg, then // the expected type of that array's elements: Type paramType = (paramInfo.IsParams ? paramInfo.ParameterType.GetElementType() : paramInfo.ParameterType); // Parameter type-safe checking: bool inheritable = paramType.IsAssignableFrom(argType); if (!inheritable) { bool castError = false; // If it's not directly assignable to the expected type, maybe it's "castable" to it: try { arg = Convert.ChangeType(arg, Type.GetTypeCode(paramType)); } catch (InvalidCastException) { throw new KOSCastException(argType, paramType); } catch (FormatException) { castError = true; } if (castError) { throw new Exception(string.Format("Argument {0}({1}) to method {2} should be {3} instead of {4}.", (delInfo.Parameters.Length - i), arg, delInfo.Name, paramType.Name, argType)); } } if (isParamArrayArg) { paramArrayArgs.Add(Structure.FromPrimitiveWithAssert(arg)); --i; // keep hitting the last item in the param list again and again until a forced break because of arg bottom marker. } else { args.Add(Structure.FromPrimitiveWithAssert(arg)); } } if (isParamArrayArg) { // collect the param array args that were at the end into the one single // array item that will be sent to the method when invoked: args.Add(paramArrayArgs.ToArray()); } // Consume the bottom marker under the args, which had better be // immediately under the args we just popped, or the count was off. if (!isParamArrayArg) // A param array arg will have already consumed the arg bottom mark. { bool foundArgMarker = false; int numExtraArgs = 0; while (cpu.GetArgumentStackSize() > 0 && !foundArgMarker) { object marker = cpu.PopValueArgument(); if (marker != null && marker.GetType() == CpuUtility.ArgMarkerType) { foundArgMarker = true; } else { ++numExtraArgs; } } if (numExtraArgs > 0) { throw new KOSArgumentMismatchException(delInfo.Parameters.Length, delInfo.Parameters.Length + numExtraArgs); } } // Delegate.DynamicInvoke expects a null, rather than an array of zero length, when // there are no arguments to pass: object[] argArray = (args.Count > 0) ? args.ToArray() : null; object val = call(argArray); if (delInfo.ReturnType == typeof(void)) { value = ScalarValue.Create(0); } else { value = Structure.FromPrimitiveWithAssert(val); } }
public override object LessThanEqual(OperandPair pair) { return(ScalarValue.Create(pair.Left) <= ScalarValue.Create(pair.Right)); }
public override object NotEqual(OperandPair pair) { return(ScalarValue.Create(pair.Left) != ScalarValue.Create(pair.Right)); }
public override object Add(OperandPair pair) { return(ScalarValue.Create(pair.Left) + ScalarValue.Create(pair.Right)); }