function cgvObject = slvnvruncgvtest(model, dataFile, runOpts)
%SLVNVRUNCGVTEST - Invoke Code Generation Verification (CGV) API and execute model
%
%   CGVOBJECT = SLVNVRUNCGVTEST(MODEL, DATAFILE) Invokes the
%   Code Generation Verification (CGV) API methods and executes the MODEL 
%   using all test cases in DATAFILE. CGVOBJECT is a cgv.CGV object that 
%   slvnvruncgvtest creates during the execution of the model. 
%   slvnvruncgvtest sets the execution mode for CGVOBJECT to 'sim' by default. 
%
%   CGVOBJECT = SLVNVRUNCGVTEST(MODEL, DATAFILE, RUNOPTS) Invokes CGV API 
%   methods and executes the MODEL using test cases in DATAFILE. RUNOPTS 
%   defines the options for executing the test cases. The settings in 
%   RUNOPTS determine the configuration of CGVOBJECT.  
%
%   Input Arguments:
%     MODEL : Name or handle of the Simulink model to execute.    
%
%     DATAFILE : Name of the data file or a structure that contains the 
%     input data. Data can be generated either by analyzing the model using 
%     the Simulink Design Verifier software, or by using slvnvlogsignals
%     function. 
%
%     RUNOPTS : A structure whose fields specify the configuration of 
%     slvnvruncgvtest
%
%       testIdx            Test case index array to execute from DATAFILE.
%                          If testIdx is [], slvnvruncgvtest executes
%                          all test cases.
%                          Default: []
%
%       allowCopyModel     Specifies to create and configure the MODEL if 
%                          you have not configured it correctly for 
%                          executing test cases with the CGV API.
%
%                          If true and you have not configured MODEL 
%                          correctly to execute test cases with the CGV API, 
%                          slvnvruncgvtest copies the MODEL, corrects the 
%                          configuration, and executes the test cases on 
%                          the copied model.
%
%                          If false (the default), an error occurs if the 
%                          tests cannot execute with the CGV API.
%
%                          Note: If you have not configured the 
%                          top-level MODEL or any referenced models 
%                          correctly, slvnvruncgvtest does not copy the 
%                          model, even if allowCopyModel is true. An error occurs.                   
%
%       cgvCompType        Defines the software-in-the-loop (SIL) or 
%                          processor-in-the-loop (PIL) approach for CGV:
%                          'topmodel'(default) or 'modelblock'.
%
%       cgvConn            Specifies mode of execution for CGV:  
%                          'sim' (default) or 'sil' or 'pil'.
%
%   Output Arguments:
%     CGVOBJECT : cgv.CGV object that is created during the execution of MODEL. 
%                 Execution of each test case will be stored by a  
%                 Simulink.SimulationOutput object inside the CGVOBJECT,
%                 and has the following data:
%
%                 tout_slvnvruncgvtest    : Simulation time.
%
%                 xout_slvnvruncgvtest    : State data.
%
%                 yout_slvnvruncgvtest    : Output signal data.
%
%                 logsout_slvnvruncgvtest : Signal logging data for signals 
%                                           connected to outports and signals 
%                                           that are configured for logging on the model.
%
%   NOTES:
%     runOpts = slvnvruntestopts('cgv') returns a runOpts structure 
%     with the default values for each field.
%
%     Use the methods of cgv.CGV object to access the data contained in it. 
%     For example, to reach the data for test case index testIdx do:
%       cgvobject = slvnvruncgvtest(model,datafile);
%       outdata = cgvobject.getOutputData(testIdx);
%       tout_slvnvruncgvtest = outdata.find('tout_slvnvruncgvtest');
%       xout_slvnvruncgvtest = outdata.find('xout_slvnvruncgvtest');
%       yout_slvnvruncgvtest = outdata.find('yout_slvnvruncgvtest');
%       logsout_slvnvruncgvtest = outdata.find('logsout_slvnvruncgvtest');
%
%   See also SLVNVRUNTESTOPTS, SLVNVLOGSIGNALS, SLVNVRUNTEST, 
%   cgv.CGV, Simulink.SimulationOutput

%   Copyright 2010-2013 The MathWorks, Inc.

    if nargin<2 
        msg   = ['Invalid usage of slvnvruncgvtest. ', ...
             'At least two inputs are expected.'];
        error('Slvnv:RUNCGVTEST:InvalidNumInputs',msg);
    end        
    
    if nargin<3
        runOpts = slvnvruntestopts('cgv');   
    end
    
    errMsg = Sldv.RunTestCase.iscorrectRuntestOpts(runOpts,'cgv');
    if ~isempty(errMsg)
        error('Slvnv:RUNCGVTEST:InvalidInput',errMsg);    
    end
        
    try         
        runTestObj = Sldv.RunTestCaseCGV(mfilename);
        cgvObject = runTestObj.simTestCases(model, dataFile, runOpts);                    
    catch Mex
        throw(Mex);
    end                   
end
