Example #1
0
        private static int performTestPart2(int testId, double [] expectedBestPrices, int currencyCount, int currency, ExchangePair [] exchanges)
        {
            double[]      bestPrices;
            CurrencyGraph cg     = new CurrencyGraph(currencyCount, exchanges);
            bool          result = cg.findBestPrice(currency, out bestPrices);

            bool success = result && bestPrices != null && bestPrices.Length == expectedBestPrices.Length;

            if (success)
            {
                for (int i = 0; i < bestPrices.Length; ++i)
                {
                    if (Math.Abs(bestPrices [i] - expectedBestPrices [i]) > 10e-8)
                    {
                        success = false;
                        break;
                    }
                }
            }

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

            //if (!success)
            //	Console.Write (" (details: test result: [{0}]; expected result: [{1}])", string.Join(", ", bestPrices), string.Join(", ", expectedBestPrices));

            Console.WriteLine("");
            return(success ? 1 : 0);
        }
Example #2
0
        private void ConstructCurrencyGraph()
        {
            PrintImportant("Constructing graph...");
            Exception ex;

            graph = GetResult(() =>
            {
                return(new CurrencyGraph(testSize, exchanges));
            }, out ex);
            if (ex != null)
            {
                PrintMessage("Unexpected exception: {0}", ex.Message);
            }
        }
Example #3
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);
        }
Example #4
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);
        }
Example #5
0
 private double[,] GetWeights(CurrencyGraph graph)
 {
     return(typeof(CurrencyGraph)
            .GetField("weights", BindingFlags.NonPublic | BindingFlags.Instance)
            .GetValue(graph) as double[, ]);
 }