예제 #1
0
        public void Test_ConfigurationJISONJS()
        {
            var ngfmPrototype = new NGFMPrototype();

            CheckIfFileExists(ngfmPrototype.grammar_ast_js, "grammar-ast.js");
            CheckIfFileExists(ngfmPrototype.underscore_js, "underscore.js");

            ngfmPrototype.Dispose();
        }
예제 #2
0
        public void API_ExecuteFM()
        {
            var testCases = new Dictionary <string, Dictionary <string, string> >();

            #region Get Testing Configurations
            using (StreamReader sr = new StreamReader("TestCases_FMExecution.txt"))
            {
                string str;
                Dictionary <string, string> testContent = null;

                while ((str = sr.ReadLine()) != null)
                {
                    if (string.IsNullOrEmpty(str) || string.IsNullOrEmpty(str) || str.StartsWith("//"))//Commenting
                    {
                        continue;
                    }
                    else if (str.StartsWith("[Test"))
                    {
                        testContent = new Dictionary <string, string>();
                        testCases.Add(str, testContent);
                    }
                    else if (null != testContent)
                    {
                        string[] pair = str.Replace(" ", string.Empty)
                                        .Split(new char[] { ':' }, 2);

                        testContent.Add(pair[0], pair[1]);
                    }
                }
            }
            #endregion

            #region === Construct NGFMPrototype ===

            NGFMPrototype ngfmPrototype = new NGFMPrototype();

            #endregion

            foreach (var kv in testCases)
            {
                #region Get Expected Results

                var lossesPerSubPeril = ReadGULossFromFile(kv.Value["DR"]);

                PartitionData pData = RMS.Prototype.NGFM.Utilities.DeserializePartitionData(kv.Value["RITE"]);

                if (kv.Value.ContainsKey("CDL"))
                {
                    string cdl = ReadCDL(kv.Value["CDL"]);
                    foreach (var ce in pData.Exposures)
                    {
                        ce.Contract.CDLString = cdl;
                    }
                }

                string[] res            = kv.Value["RESULT"].Split(new char[] { ';', ',', ' ' });
                var      expectedResult = new Dictionary <long, double>();
                for (int i = 0; i < res.Length - 1; i += 2)
                {
                    long id = long.Parse(res[i]);
                    if (!expectedResult.ContainsKey(id))
                    {
                        expectedResult.Add(id, double.Parse(res[i + 1]));
                    }
                }

                #endregion

                #region === ProcessEvent() ===

                ngfmPrototype.Prepare(pData);

                // old API ----------------
                //var payOutDict = HDFM.ProcessEvent(EventOccurrenceDRs);
                //-------------------------

                // new new API ----------------
                int eventID = 0;
                ngfmPrototype.ProcessEvent_OLDNEWNEWAPI(eventID, lossesPerSubPeril);
                var payOutDict = ngfmPrototype.GetResultPositions(eventID, expectedResult.Keys.ToArray())
                                 .ToDictionary(p => p.Key, p => p.Value.PayOut);
                //-------------------------

                #endregion

                #region Comparison

                foreach (long id in expectedResult.Keys)
                {
                    double actualResult = (payOutDict.ContainsKey(id)) ? payOutDict[id] : -1;
                    Assert.AreEqual(expectedResult[id], actualResult, 0.01, "Test \"{0}\" (ID={1}) is failed.", kv.Key, id);
                    //string r = expectedResult[id];
                    //int n = r.Length - 1 - r.IndexOf('.');

                    //double payOut = (payOutDict.ContainsKey(id)) ? payOutDict[id] : -1;

                    //double d = Math.Round(payOut, n);
                    //string actualResult = d.ToString();
                    //Assert.AreEqual(expectedResult[id], actualResult, "Test \"{0}\" (ID={1}) is failed.", kv.Key, id);
                }

                #endregion
            }

            ngfmPrototype.Dispose();
        }
예제 #3
0
        public void Test_ParseCDLUsingJISONJS()
        {
            string fileWithTestCases = "TestCases_ParseCDLtoJSON.txt";
            string testName          = "";

            var ngfmPrototype = new NGFMPrototype();

            using (StreamReader sr = new StreamReader(fileWithTestCases))
            {
                string str, actualOut, expectedOut;
                var    testIn  = new StringBuilder();
                var    testOut = new StringBuilder();

                bool flagIn  = false;
                bool flagOut = false;

                while ((str = sr.ReadLine()) != null)
                {
                    if (str.StartsWith("[Test"))
                    {
                        if (testName != "")
                        {
                            actualOut   = ngfmPrototype.ParseCDLUsingJISONJS_GetIR(testIn.ToString());
                            expectedOut = testOut.ToString().Trim();
                            Assert.AreEqual(expectedOut, actualOut, "Test \"{0}\" is failed.", testName);
                            testIn.Clear();
                            testOut.Clear();
                            flagIn = flagOut = false;
                        }
                        testName = str;
                    }
                    else if (str.StartsWith("[CDL]"))
                    {
                        flagIn  = true;
                        flagOut = false;
                    }
                    else if (str.StartsWith("[JSON]"))
                    {
                        flagIn  = false;
                        flagOut = true;
                    }
                    else if (flagIn)
                    {
                        if (testIn.Length > 0)
                        {
                            testIn.Append("\r\n");
                        }
                        testIn.Append(str);
                    }
                    else if (flagOut)
                    {
                        if (testOut.Length > 0)
                        {
                            testOut.Append("\r\n");
                        }
                        testOut.Append(str);
                    }
                }

                if (flagOut)
                {
                    actualOut   = ngfmPrototype.ParseCDLUsingJISONJS_GetIR(testIn.ToString());
                    expectedOut = testOut.ToString().Trim();
                    Assert.AreEqual(expectedOut, actualOut, "Test \"{0}\" is failed.", testName);
                }
            }

            ngfmPrototype.Dispose();
        }