classdef test_expcodes < matlab.unittest.TestCase
  
  properties
    expcode_obj;
    SCDexps;
    shot = 65668; % a shot with data, for actualize/sim purposes
  end
  
  properties(ClassSetupParameter)
    expcode = { '1010' }; % ,'1','1005','1007', '1006',list of expcodes to test 
  end
   
  methods(TestClassSetup)
    function setup_environment(testCase)
      testCase.addTeardown(@cd,pwd);
      testCase.addTeardown(@path,path);
      
      testCase.addTeardown(@() SCDclass_expcode.close_all(0))
      
      basePath = fullfile(fileparts(mfilename('fullpath')),'..');
      run(fullfile(basePath,'rtccode_paths'));
      
      % get SCD experimental code object container
      testCase.SCDexps = SCDconf_createexpcodes;
      
      SCDconf_setSIMconf; % set ConfigurationSettings for Simulation
    end
  
    function setup_expcode(testCase,expcode)
      % get this expcode object from expcode object container
      fprintf('\n=== Testing expcode %s ===\n',expcode);
      testCase.expcode_obj = getbymaincode(testCase.SCDexps,str2double(expcode));
    end
    
  end
  
  methods(Test,TestTags={'expcodes'})
    
    function test_expcode_printinfo(testCase)
      testCase.expcode_obj.printinfo
    end
    
    function test_expcode_printparameters(testCase)
      testCase.expcode_obj.printparameters
    end
    
    function test_expcode_printwavegens(testCase)
      testCase.expcode_obj.printwavegens;
    end
    
    function test_expcode_printtasks(testCase)
      testCase.expcode_obj.printtasks;
    end
    
    function test_expcode_printinits(testCase)
      testCase.expcode_obj.printinits
    end
    
    function test_expcode_setup(testCase)    
      fprintf('\n=== Testing setup for expcode %d: %s === \n',...
      testCase.expcode_obj.maincode,...
      testCase.expcode_obj.name);
      testCase.expcode_obj.setup; % run setup this exp code
    end

    function test_callinits(testCase)
        fprintf('\n=== Testing callinits for expcode %d: %s === \n',...
        testCase.expcode_obj.maincode,...
        testCase.expcode_obj.name);
        testCase.expcode_obj.callinits;
    end
    
    function test_actualize(testCase)
      testCase.expcode_obj.actualize(testCase.shot);
    end
    
    function test_expcode_compile_nodes(testCase)
      % compile each node separately first
      testCase.assumeFalse(testCase.expcode_obj.maincode==1006,'Skipping compile tests for 1006 which requires library')

      for inode = 1:numel(testCase.expcode_obj.nodes)
        node = testCase.expcode_obj.nodes(inode);
        if node.active
          fprintf('\n === Testing Simulink compilation for node %02d of expcode %d: %s === \n',...
            inode,testCase.expcode_obj.maincode,testCase.expcode_obj.name);
          testCase.expcode_obj.compile(inode); % compile single node
        else
          fprintf('skipping compilation for node %d since not active\n',inode)
        end
      end
    end
    
    function test_expcode_sim(testCase)
      testCase.assumeFalse(testCase.expcode_obj.maincode==1006,'Skipping compile tests for 1006 which requires library')

      fprintf('\n === Testing Simulink simulation of tcv.slx for expcode %d: === \n',testCase.expcode_obj.maincode)
      testCase.expcode_obj.sim; % simulate whole tcv.slx with this expcode
    end
    
    
    function test_build(testCase)
      testCase.assumeFalse(testCase.expcode_obj.maincode==1006,'Skipping build tests for 1006 which requires library')
      testCase.expcode_obj.build;
    end
  end
end
