Esempio n. 1
0
        protected virtual void CheckArbitrage()
        {
            PrintImportant("Testing arbitrage...");
            int[] actualExchangeCycle = null;
            bool  actualArbitrageFound;

            for (int i = 0; i < testSize; i++)
            {
#if DEBUG
                PrintMessage("Testing vertex {0}", i);
#endif
                Exception exception;
                actualArbitrageFound = GetResult(() =>
                {
                    return(graph.findArbitrage(i, out actualExchangeCycle));
                }, out exception);
                if (exception != null)
                {
                    throw exception;
                }
                try
                {
                    CompareResult(expectedArbitrages[i], actualArbitrageFound, "Arbitrage");
                    CheckArbitrageCycle(expectedArbitrageCycles[i], actualExchangeCycle);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
        }
Esempio n. 2
0
        private static int performTestPart3(int testId, int currencyCount, int currency, ExchangePair [] exchanges)
        {
            int[]         cycle;
            CurrencyGraph cg      = new CurrencyGraph(currencyCount, exchanges);
            bool          result  = cg.findArbitrage(currency, out cycle);
            bool          success = result && cycle != null;
            string        reason  = "";

            if (success)
            {
                if (cycle.Length == 0 || cycle [0] != cycle [cycle.Length - 1])
                {
                    success = false;
                }
                else
                {
                    for (int i = 1; i < cycle.Length; ++i)
                    {
                        if (!isValidExchange(cycle [i - 1], cycle [i], exchanges))
                        {
                            success = false;
                            reason  = string.Format(" (details: invalid cycle)");
                            break;
                        }
                    }

                    if (success)
                    {
                        double v = 1.0;

                        for (int i = 1; i < cycle.Length; ++i)
                        {
                            v *= findExchangePrice(cycle [i - 1], cycle [i], exchanges);
                        }

                        if (v <= 1.0)
                        {
                            success = false;
                            reason  = string.Format(" (details: {0} <= 1.0)", v);
                        }
                    }
                }
            }

            Console.WriteLine("Test #{0}: {1} {2}", testId, success ? "SUCCESS" : "FAILURE", reason);
            return(success ? 1 : 0);
        }
Esempio n. 3
0
        private static int performTestPart1(int testId, bool expectedResult, int currencyCount, int currency, ExchangePair [] exchanges)
        {
            int[]         cycle;
            CurrencyGraph cg     = new CurrencyGraph(currencyCount, exchanges);
            bool          result = cg.findArbitrage(currency, out cycle);

            bool success = result == expectedResult;

            Console.Write("Test #{0}: {1}", testId, success ? "SUCCESS" : "FAILURE");

            if (!success)
            {
                Console.Write(" (details: test result: {0}; expected result: {1})", result, expectedResult);
            }

            Console.WriteLine("");
            return(success ? 1 : 0);
        }