//Form of Ax^2 + Bx + C
 //Takes array of double containing A,B,C 
 //Returns array of positive and negative result
 public static double[] QuadFormula(double[] abc)
 {
     NotFiniteNumberException e = new NotFiniteNumberException();
    
     try
     {
         double a = abc[0];
         double b = abc[1];
         double c = abc[2];
         double discriminant = ((b * b) - (4 * a * c));
         if (discriminant < 0.00)
         {
             throw  e; 
         }
         
         discriminant = (Math.Sqrt(discriminant));
         Console.WriteLine("discriminant: {0}",discriminant);
         double posAnswer = ((-b + discriminant) / (2 * a));
         double negAnswer = ((-b - discriminant) / (2 * a));
         double[] xArray= { posAnswer,negAnswer};
         return xArray;
     }
     catch (NotFiniteNumberException)
     {
         Console.WriteLine("Both answers will be imaginary numbers");
         double[] xArray = { Math.Sqrt(-1), Math.Sqrt(-1) };
         return xArray;
     }
 }
예제 #2
0
        public void CanCreatePoliciesForHandler()
        {
            ExceptionPolicyData exceptionPolicyData = new ExceptionPolicyData("policy");

            settings.ExceptionPolicies.Add(exceptionPolicyData);
            ExceptionTypeData exceptionTypeData = new ExceptionTypeData("type1", typeof(Exception), PostHandlingAction.ThrowNewException);

            exceptionPolicyData.ExceptionTypes.Add(exceptionTypeData);
            FaultContractExceptionHandlerData exceptionHandlerData = new FaultContractExceptionHandlerData("handler1",
                                                                                                           typeof(MockFaultContract).AssemblyQualifiedName);

            exceptionHandlerData.ExceptionMessage = "fault message";
            exceptionHandlerData.PropertyMappings.Add(new FaultContractExceptionHandlerMappingData("Message", "{Message}"));
            exceptionHandlerData.PropertyMappings.Add(new FaultContractExceptionHandlerMappingData("Data", "{Data}"));
            exceptionHandlerData.PropertyMappings.Add(new FaultContractExceptionHandlerMappingData("SomeNumber", "{OffendingNumber}"));
            exceptionTypeData.ExceptionHandlers.Add(exceptionHandlerData);
            container.AddExtension(new ExceptionHandlingBlockExtension());
            ExceptionPolicyImpl      policy            = container.Resolve <ExceptionPolicyImpl>("policy");
            NotFiniteNumberException originalException = new NotFiniteNumberException("MyException", 12341234123412);

            originalException.Data.Add("someKey", "someValue");
            try
            {
                policy.HandleException(originalException);
                Assert.Fail("a new exception should have been thrown");
            }
            catch (FaultContractWrapperException e)
            {
                MockFaultContract fault = (MockFaultContract)e.FaultContract;
                Assert.AreEqual(originalException.Message, fault.Message);
                Assert.AreEqual(originalException.Data.Count, fault.Data.Count);
                Assert.AreEqual(originalException.Data["someKey"], fault.Data["someKey"]);
                Assert.AreEqual(originalException.OffendingNumber, fault.SomeNumber);
            }
        }
        public void CreatesWcfExceptionShildingProgrammatically()
        {
            var excepionPolicies      = new List <ExceptionPolicyDefinition>();
            var excepionPolicyEntries = new List <ExceptionPolicyEntry>();
            var attributes            = new NameValueCollection {
                { "Message", "{Message}" }, { "Data", "{Data}" }, { "SomeNumber", "{OffendingNumber}" }
            };
            var exceptionHandler = new FaultContractExceptionHandler(typeof(MockFaultContract), "fault message", attributes);
            var exceptionPolicy  = new ExceptionPolicyEntry(typeof(NotFiniteNumberException), PostHandlingAction.ThrowNewException, new IExceptionHandler[] { exceptionHandler });

            excepionPolicyEntries.Add(exceptionPolicy);
            excepionPolicies.Add(new ExceptionPolicyDefinition("Wcf Shielding Policy", excepionPolicyEntries));

            var exceptionManager = new ExceptionManager(excepionPolicies);
            NotFiniteNumberException originalException = new NotFiniteNumberException("MyException", 555);

            originalException.Data.Add("someKey", "someValue");
            try
            {
                exceptionManager.HandleException(originalException, "Wcf Shielding Policy");
                Assert.Fail("a new exception should have been thrown");
            }
            catch (FaultContractWrapperException ex)
            {
                MockFaultContract fault = (MockFaultContract)ex.FaultContract;
                Assert.AreEqual(originalException.Message, fault.Message);
                Assert.AreEqual(originalException.Data.Count, fault.Data.Count);
                Assert.AreEqual(originalException.Data["someKey"], fault.Data["someKey"]);
                Assert.AreEqual(originalException.OffendingNumber, fault.SomeNumber);
            }
        }
예제 #4
0
    // Test the NotFiniteNumberException class.
    public void TestNotFiniteNumberException()
    {
        NotFiniteNumberException e;

        e = new NotFiniteNumberException();
        AssertEquals("NotFiniteNumberException (1)",
                     0.0, e.OffendingNumber);
        AssertNotNull("NotFiniteNumberException (2)", e.Message);
        ExceptionTester.CheckHResult
            ("NotFiniteNumberException (3)", e,
            unchecked ((int)0x80131528));

        e = new NotFiniteNumberException("msg");
        AssertEquals("NotFiniteNumberException (4)",
                     0.0, e.OffendingNumber);
        AssertEquals("NotFiniteNumberException (5)", "msg", e.Message);
        ExceptionTester.CheckHResult
            ("NotFiniteNumberException (6)", e,
            unchecked ((int)0x80131528));

        e = new NotFiniteNumberException("msg", 2.0);
        AssertEquals("NotFiniteNumberException (7)",
                     2.0, e.OffendingNumber);
        AssertEquals("NotFiniteNumberException (8)",
                     "msg", e.Message);
        ExceptionTester.CheckHResult
            ("NotFiniteNumberException (9)", e,
            unchecked ((int)0x80131528));
    }
예제 #5
0
        public void BuildsWcfExceptionShildingWithFluentConfiguration()
        {
            DictionaryConfigurationSource emptyConfigSource = new DictionaryConfigurationSource();

            ConfigurationSourceBuilder builder = new ConfigurationSourceBuilder();

            builder.ConfigureExceptionHandling()
            .GivenPolicyWithName("policy")
            .ForExceptionType(typeof(NotFiniteNumberException))
            .ShieldExceptionForWcf(typeof(MockFaultContract), "fault message")
            .MapProperty("Message", "{Message}")
            .MapProperty("Data", "{Data}")
            .MapProperty("SomeNumber", "{OffendingNumber}")
            .ThenThrowNewException();

            builder.UpdateConfigurationWithReplace(emptyConfigSource);

            ExceptionHandlingSettings settings = emptyConfigSource.GetSection(ExceptionHandlingSettings.SectionName) as ExceptionHandlingSettings;

            Assert.IsNotNull(settings);
            Assert.AreEqual(1, settings.ExceptionPolicies.Count);
            var policy = settings.ExceptionPolicies.Get("policy");

            Assert.IsNotNull(policy);
            Assert.AreEqual(1, policy.ExceptionTypes.Count);
            var configuredException = policy.ExceptionTypes.Get(0);

            Assert.AreEqual(typeof(NotFiniteNumberException), configuredException.Type);
            Assert.AreEqual(PostHandlingAction.ThrowNewException, configuredException.PostHandlingAction);
            Assert.AreEqual(1, configuredException.ExceptionHandlers.Count);

            var handler = configuredException.ExceptionHandlers.Get(0) as FaultContractExceptionHandlerData;

            Assert.IsNotNull(handler);
            Assert.AreEqual(typeof(MockFaultContract).AssemblyQualifiedName, handler.FaultContractType);
            Assert.AreEqual("fault message", handler.ExceptionMessage);
            Assert.AreEqual(3, handler.PropertyMappings.Count);
            Assert.IsNotNull(handler.PropertyMappings.SingleOrDefault(p => p.Name == "Message" && p.Source == "{Message}"));
            Assert.IsNotNull(handler.PropertyMappings.SingleOrDefault(p => p.Name == "Data" && p.Source == "{Data}"));
            Assert.IsNotNull(handler.PropertyMappings.SingleOrDefault(p => p.Name == "SomeNumber" && p.Source == "{OffendingNumber}"));

            var exceptionManager = settings.BuildExceptionManager();
            NotFiniteNumberException originalException = new NotFiniteNumberException("MyException", 555);

            originalException.Data.Add("someKey", "someValue");
            try
            {
                exceptionManager.HandleException(originalException, "policy");
                Assert.Fail("Should have thrown");
            }
            catch (FaultContractWrapperException ex)
            {
                MockFaultContract fault = (MockFaultContract)ex.FaultContract;
                Assert.AreEqual(originalException.Message, fault.Message);
                Assert.AreEqual(originalException.Data.Count, fault.Data.Count);
                Assert.AreEqual(originalException.Data["someKey"], fault.Data["someKey"]);
                Assert.AreEqual(originalException.OffendingNumber, fault.SomeNumber);
            }
        }
        public static void Ctor_String()
        {
            string message   = "Created NotFiniteNumberException";
            var    exception = new NotFiniteNumberException(message);

            Assert.Equal(message, exception.Message);
            Assert.Equal(COR_E_NOTFINITENUMBER, exception.HResult);
        }
        public static void Ctor_Double()
        {
            double offendingNumber = double.PositiveInfinity;
            var    exception       = new NotFiniteNumberException(offendingNumber);

            Assert.Equal(offendingNumber, exception.OffendingNumber);
            Assert.Equal(COR_E_NOTFINITENUMBER, exception.HResult);
        }
        public static void Ctor_Empty()
        {
            var exception = new NotFiniteNumberException();

            Assert.NotNull(exception);
            Assert.NotEmpty(exception.Message);
            Assert.Equal(COR_E_NOTFINITENUMBER, exception.HResult);
        }
        public static void Ctor_String_Double()
        {
            string message         = "Created NotFiniteNumberException";
            double offendingNumber = double.NegativeInfinity;
            var    exception       = new NotFiniteNumberException(message, offendingNumber);

            Assert.Equal(message, exception.Message);
            Assert.Equal(offendingNumber, exception.OffendingNumber);
            Assert.Equal(COR_E_NOTFINITENUMBER, exception.HResult);
        }
        public static void Ctor_String_Exception()
        {
            string message        = "Created NotFiniteNumberException";
            var    innerException = new Exception("Created inner exception");
            var    exception      = new NotFiniteNumberException(message, innerException);

            Assert.Equal(message, exception.Message);
            Assert.Equal(COR_E_NOTFINITENUMBER, exception.HResult);
            Assert.Same(innerException, exception.InnerException);
            Assert.Equal(innerException.HResult, exception.InnerException.HResult);
        }
        public void ResultsTest()
        {
            var r = Ok(4);

            Assert.AreEqual(10, r + 6);

            Result <int> resultInt = 5;

            Assert.AreEqual(Ok(5), resultInt);
            Assert.IsFalse(resultInt is IFailed);

            var ex = new NotFiniteNumberException();
            var e  = Fail <int>(ex);

            Assert.AreEqual(Fail <int>(ex), e);
            Assert.IsFalse(e.Success);

            Assert.AreEqual(4, r.ValueOrThrow());
            Assert.ThrowsException <NotFiniteNumberException>(() => e.ValueOrThrow());
            Assert.IsTrue(e is IFailed f && f.Error is NotFiniteNumberException);


            Assert.AreEqual(8, e.ValueOrDefault(8));
            Assert.AreEqual(ex.Message.Length, e.ValueOrElse(e => e.Message.Length));

            Assert.AreEqual(Ok(true), resultInt.Chain(i => Ok(i * 2).Chain(j => Ok(j > i))));
            Assert.AreEqual(e.Error, ((Fail <string>)e.Chain(_ => Ok($"Success!"))).Error);

            Assert.AreEqual(Ok(9),
                            from a in r
                            from b in resultInt
                            select a + b);

            Assert.AreEqual(e,
                            from a in r
                            from b in e
                            select a + b);

            Assert.AreEqual("Success: 5", resultInt.Handle(v => $"Success: {v}", e => $"Failed with: {e.Message}"));
            Assert.AreEqual("Failed with: " + ex.Message, e.Handle(v => $"Success: {v}", e => $"Failed with: {e.Message}"));

            Assert.AreEqual(Ok(5), r.Combine(resultInt));
            Assert.AreEqual(e, r.Combine(e));
            Assert.AreEqual(e, e.Combine(r));

            Assert.AreEqual(Ok(5), r & resultInt);
            Assert.AreEqual(e, r & e);
            Assert.AreEqual(e, e & r);

            Assert.IsTrue(Ok(5) == Ok(5));
            Assert.IsTrue(Ok(5) != Ok(6));
            Assert.IsTrue(Fail(ex) == Fail(ex));
            Assert.IsTrue(Fail(ex) != Fail(new ArgumentException()));
        }
예제 #12
0
        public void CanInjectInvalidPropertyValueIntoCustomPropertyFaultContract()
        {
            NameValueCollection attributes = new NameValueCollection();

            attributes.Add("Invalid", "{OffendingNumber}");
            FaultContractExceptionHandler instance  = new FaultContractExceptionHandler(typeof(MockFaultContract), attributes);
            NotFiniteNumberException      exception = new NotFiniteNumberException("MyException", 1231254);
            FaultContractWrapperException result    = (FaultContractWrapperException)instance.HandleException(exception, Guid.NewGuid());

            Assert.AreEqual((double)0, ((MockFaultContract)result.FaultContract).SomeNumber);
        }
        public static void Ctor_String_Double_Exception()
        {
            string message         = "Created NotFiniteNumberException";
            double offendingNumber = double.NaN;
            var    innerException  = new Exception("Created inner exception");
            var    exception       = new NotFiniteNumberException(message, offendingNumber, innerException);

            Assert.Equal(offendingNumber, exception.OffendingNumber);
            Assert.Equal(message, exception.Message);
            Assert.Equal(innerException, exception.InnerException);
            Assert.Equal(COR_E_NOTFINITENUMBER, exception.HResult);
        }
예제 #14
0
        public void can_create_a_failure_case_with_a_specific_exception()
        {
            var msg   = "Example";
            var error = new NotFiniteNumberException(msg);

            var result = Result <string> .Failure(error); // note, we can't infer the type of a failure

            Assert.IsFalse(result.IsSuccess);
            Assert.IsTrue(result.IsFailure);
            Assert.IsNull(result.ResultData);
            Assert.AreEqual(result.FailureCause, error);
            Assert.AreEqual(msg, result.FailureCause.Message);
        }
예제 #15
0
        public void CanInjectAttributesIntoFaultContract()
        {
            NameValueCollection attributes = new NameValueCollection();

            attributes.Add("Message", "{Message}");
            attributes.Add("Data", "{Data}");
            attributes.Add("SomeNumber", "{OffendingNumber}");

            FaultContractExceptionHandler instance  = new FaultContractExceptionHandler(typeof(MockFaultContract), attributes);
            NotFiniteNumberException      exception = new NotFiniteNumberException("MyException", 12341234123412);

            exception.Data.Add("someKey", "someValue");
            FaultContractWrapperException result = (FaultContractWrapperException)instance.HandleException(exception, Guid.NewGuid());

            MockFaultContract fault = (MockFaultContract)result.FaultContract;

            Assert.AreEqual(exception.Message, fault.Message);
            Assert.AreEqual(exception.Data.Count, fault.Data.Count);
            Assert.AreEqual(exception.Data["someKey"], fault.Data["someKey"]);
            Assert.AreEqual(exception.OffendingNumber, fault.SomeNumber);
        }
	// Test the NotFiniteNumberException class.
	public void TestNotFiniteNumberException()
			{
				NotFiniteNumberException e;

				e = new NotFiniteNumberException();
				AssertEquals("NotFiniteNumberException (1)",
							 0.0, e.OffendingNumber);
				AssertNotNull("NotFiniteNumberException (2)", e.Message);
				ExceptionTester.CheckHResult
						("NotFiniteNumberException (3)", e,
						 unchecked((int)0x80131528));

				e = new NotFiniteNumberException("msg");
				AssertEquals("NotFiniteNumberException (4)",
							 0.0, e.OffendingNumber);
				AssertEquals("NotFiniteNumberException (5)", "msg", e.Message);
				ExceptionTester.CheckHResult
						("NotFiniteNumberException (6)", e,
						 unchecked((int)0x80131528));

				e = new NotFiniteNumberException("msg", 2.0);
				AssertEquals("NotFiniteNumberException (7)",
							 2.0, e.OffendingNumber);
				AssertEquals("NotFiniteNumberException (8)",
							 "msg", e.Message);
				ExceptionTester.CheckHResult
						("NotFiniteNumberException (9)", e,
						 unchecked((int)0x80131528));
			}
예제 #17
0
 private static void Ex1(NotFiniteNumberException number)
 {
     throw new NotImplementedException();
 }
예제 #18
0
        static void Main(string[] args)
        {
            var number = new NotFiniteNumberException();

            Ex1(number);
        }
        private int MapExceptionToNumber(Exception e)
        {
            Type type = e.GetType();

            if (type == typeof(IndexOutOfRangeException))
            {
                return(9);
            }
            if (type == typeof(RankException))
            {
                return(9);
            }
            if (type == typeof(DivideByZeroException))
            {
                return(11);
            }
            if (type == typeof(OverflowException))
            {
                return(6);
            }
            if (type == typeof(NotFiniteNumberException))
            {
                NotFiniteNumberException exception = (NotFiniteNumberException)e;
                if (exception.OffendingNumber == 0.0)
                {
                    return(11);
                }
                return(6);
            }
            if (type == typeof(NullReferenceException))
            {
                return(0x5b);
            }
            if (e is AccessViolationException)
            {
                return(-2147467261);
            }
            if (type == typeof(InvalidCastException))
            {
                return(13);
            }
            if (type == typeof(NotSupportedException))
            {
                return(13);
            }
            if (type == typeof(COMException))
            {
                COMException exception2 = (COMException)e;
                return(Utils.MapHRESULT(exception2.ErrorCode));
            }
            if (type == typeof(SEHException))
            {
                return(0x63);
            }
            if (type == typeof(DllNotFoundException))
            {
                return(0x35);
            }
            if (type == typeof(EntryPointNotFoundException))
            {
                return(0x1c5);
            }
            if (type == typeof(TypeLoadException))
            {
                return(0x1ad);
            }
            if (type == typeof(OutOfMemoryException))
            {
                return(7);
            }
            if (type == typeof(FormatException))
            {
                return(13);
            }
            if (type == typeof(DirectoryNotFoundException))
            {
                return(0x4c);
            }
            if (type == typeof(IOException))
            {
                return(0x39);
            }
            if (type == typeof(FileNotFoundException))
            {
                return(0x35);
            }
            if (e is MissingMemberException)
            {
                return(0x1b6);
            }
            if (e is InvalidOleVariantTypeException)
            {
                return(0x1ca);
            }
            return(5);
        }