예제 #1
0
        /// <summary>
        /// generate single test case
        /// </summary>
        /// <param name="arrProv">test data provider (ILCell, specific type)</param>
        /// <param name="definition">case definition</param>
        /// <returns>localPostfixedTestCaseName</returns>
        public string AutoCreateTestCase(ILTestArrayProvider arrProv, ILTestCaseDefinition definition)
        {
            StringBuilder ocs = new StringBuilder("clear output*;");
            string        matInputFileName        = INPUT_PARAMETER_PREFIX + ".mat";
            string        matOutputFileName       = OUTPUT_PARAMETER_PREFIX + ".mat";
            string        resultOverridesFileName = OVERRIDES_PARAMETER_PREFIX + ".mat";
            ILCell        inputVars = arrProv.GenerateTestArrays();
            ILMatFile     matFile   = new ILMatFile();
            List <string> inTag     = new List <string>();
            List <string> outTag    = new List <string>();
            string        outTagJoined;
            string        inTagJoined;

            ocs.Append("\n" + String.Format("parameter = load('{0}');", matInputFileName));
            for (int i = 0; i < inputVars.Dimensions[0]; i++)
            {
                inTag.Clear(); outTag.Clear();
                // handle input parameters
                for (int c = 0; c < definition.InputParameterCount; c++)
                {
                    string ident = "_" + i.ToString() + "_" + c.ToString();
                    matFile[INPUT_PARAMETER_PREFIX + ident] = inputVars.GetValue(i, c);
                    inTag.Add("parameter." + INPUT_PARAMETER_PREFIX + ident);
                }
                // handle output parameters
                for (int c = 0; c < definition.OutputParameterCount; c++)
                {
                    string ident = "_" + i.ToString() + "_" + c.ToString();
                    outTag.Add(OUTPUT_PARAMETER_PREFIX + ident);
                }
                //outTagJoined = String.Join(",", outTag.ToArray());
                //inTagJoined = String.Join(",", inTag.ToArray());
                List <object> allTags = new List <object>();
                foreach (string s in inTag)
                {
                    allTags.Add(s);
                }
                foreach (string s in outTag)
                {
                    allTags.Add(s);
                }

                string octaveFuncCall = String.Format(definition.OctaveFunctionDef, allTags.ToArray());
                ocs.Append(String.Format("\ntry {0} catch  {1} = 'Exception'; end",
                                         octaveFuncCall, outTag[0]));
            }
            // the name for the case directory will be postfixed with the arrayprovider types ('double',...)
            string localPostfixedTestCaseName = definition.TestCaseName + "_" + arrProv.GetIdentifierPostfix();
            string TestCaseOutputPath         = Path.Combine(m_baseDirectory, RelativeTestCasePath);

            TestCaseOutputPath = Path.Combine(TestCaseOutputPath, localPostfixedTestCaseName)
                                 + Path.DirectorySeparatorChar;
            if (Directory.Exists(TestCaseOutputPath))
            {
                Directory.Delete(TestCaseOutputPath, true);
            }
            Directory.CreateDirectory(TestCaseOutputPath);
            // save matfile
            using (Stream outStream = new FileStream(TestCaseOutputPath + matInputFileName, FileMode.Create)) {
                matFile.Write(outStream);
            }
            // finish + save creation script
            ocs.Append(String.Format("\n\nsave ('{0}','{1}*','-v6');", matOutputFileName, OUTPUT_PARAMETER_PREFIX));
            File.WriteAllText(TestCaseOutputPath + OCTAVE_CREATION_SCRIPT_NAME, ocs.ToString());
            #region create test class file
            inTag.Clear(); outTag.Clear();
            List <string> inTagR      = new List <string>();
            StringBuilder testRunFunc = new StringBuilder();
            // handles input parameter
            for (int i = 0; i < definition.InputParameterCount; i++)
            {
                testRunFunc.Append(String.Format(@"{1} {2}{0} = ({1})m_inputMatfile[""{2}_""+id.ToString()+""_{0}""];
                {2}{0}.Detach();"
                                                 , i.ToString(), arrProv.GetCSharpTypeDefinition(), INPUT_PARAMETER_PREFIX));
                inTag.Add(INPUT_PARAMETER_PREFIX + i.ToString());
                inTagR.Add(INPUT_PARAMETER_PREFIX + i.ToString() + ".R");
            }
            // handles output parameter
            StringBuilder errorCheckString = new StringBuilder();
            for (int i = 0; i < definition.OutputParameterCount; i++)
            {
                testRunFunc.Append(Environment.NewLine + "\t\t\t\t"
                                   + String.Format(@"ILBaseArray parameter{0} = null;", i));
                outTag.Add("parameter" + i.ToString());
                // error checking
                errorCheckString.Append(String.Format(@"
                if (!(parameter{0}.IsEmpty & m_outputMatfile[""output_""+id.ToString()+""_{0}""].IsEmpty) &&
                     (!TestHelper.TestEqualSloppy(parameter{0},m_outputMatfile[""{1}_""+id.ToString()+""_{0}""]))) {{
                    Error(""result{0} does not match expected value! parameterset: #""+id.ToString()+"",input #0: "" + input0.Dimensions.ToString());
                    return ProcessParameterErrorInfo(id, parameter{0}, {2}, {3}, {0}, m_inputMatfile, m_outputMatfile,{4});
                }}", i.ToString(), OUTPUT_PARAMETER_PREFIX, definition.InputParameterCount, definition.OutputParameterCount, escape(definition.TargetFunctionDef)));
            }
            outTagJoined = String.Join(",", outTag.ToArray());
            inTagJoined  = String.Join(",", inTag.ToArray());
            string inTagRJoined = String.Join(",", inTagR.ToArray());
            inTag.AddRange(outTag);
            inTagR.AddRange(outTag);
            string targetFunction = String.Format(definition.TargetFunctionDef, inTag.ToArray());
            // create dense test
            testRunFunc.Append(Environment.NewLine + "\t\t\t\t" + targetFunction);
            testRunFunc.Append(errorCheckString.ToString());
            // create reference test
            targetFunction = String.Format(definition.TargetFunctionDef, inTagR.ToArray());
            testRunFunc.Append(Environment.NewLine + "\t\t\t\t" + targetFunction);
            testRunFunc.Append(errorCheckString.ToString());
            // finish
            string runtimeRelPath2matFiles = Path.Combine(m_baseDirectoryRelative2EXE, RelativeTestCasePath) + Path.DirectorySeparatorChar
                                             + localPostfixedTestCaseName
                                             + Path.DirectorySeparatorChar;
            string testClass = String.Format(HEADER, localPostfixedTestCaseName,          // 0
                                             runtimeRelPath2matFiles + matInputFileName,
                                             runtimeRelPath2matFiles + matOutputFileName, // 1,2
                                             inputVars.Dimensions[0].ToString(),
                                             testRunFunc.ToString(),
                                             arrProv.GetCSharpTypeDefinition(),
                                             runtimeRelPath2matFiles + resultOverridesFileName);
            File.WriteAllText(TestCaseOutputPath + localPostfixedTestCaseName + ".cs", testClass);
            #endregion
            if (DeleteResultsOnRecreate)
            {
                if (File.Exists(TestCaseOutputPath + matOutputFileName))
                {
                    File.Delete(TestCaseOutputPath + matOutputFileName);
                }
            }
            #region write ResultOverrides
            if (definition.ResultOverrides != null)
            {
                ILMatFile overridesMatFile   = new ILMatFile();
                bool      mustWriteOverrides = false;
                foreach (ILResultOverride overr in definition.ResultOverrides)
                {
                    if (overr.TestArrayProvider == arrProv)
                    {
                        // foreach override find matching parameters in current provider
                        for (int o = 0; o < overr.Inputs.Dimensions[0]; o++)
                        {
                            for (int i = 0; i < inputVars.Dimensions[0]; i++)
                            {
                                if (ILMath.isequalwithequalnans(
                                        overr.Inputs[o, null], inputVars[i, null]))
                                {
                                    // matching override found
                                    System.Diagnostics.Debug.Assert(overr.Outputs.Dimensions[1] == inputVars.Dimensions[1]);
                                    for (int c = 0; c < inputVars.Dimensions[1]; c++)
                                    {
                                        string ident = "_" + i.ToString() + "_" + c.ToString();
                                        overridesMatFile[OUTPUT_PARAMETER_PREFIX + ident] = overr.Outputs[o, c];
                                    }
                                    mustWriteOverrides = true;
                                }
                            }
                        }
                    }
                }
                if (mustWriteOverrides)
                {
                    using (FileStream overridesStream = new FileStream(TestCaseOutputPath + OVERRIDES_PARAMETER_PREFIX + ".mat", FileMode.Create)) {
                        overridesMatFile.Write(overridesStream);
                    }
                }
                else if (File.Exists(TestCaseOutputPath + OVERRIDES_PARAMETER_PREFIX + ".mat"))
                {
                    File.Delete(TestCaseOutputPath + OVERRIDES_PARAMETER_PREFIX + ".mat");
                }
            }
            #endregion

            return(localPostfixedTestCaseName);
        }