Automatic testing of CPAL script -- file series functions

preface

  in the process of using the test report, we may use to write our test results to the specified file. Compared with a test report, we may only care about some parameters. For example, in the results of stress testing, we may be more concerned about the results and key information of the final test case, but we don't really care about some of the information. At this time, we format the information we want to focus on into the text, which will also facilitate us to quickly get the results we want.

Function introduction

fileClose

usage method

Function function

  close the specified file. If an error occurs, the return value is 0, otherwise it is 1.

fileGetBinaryBlock

usage method

Function function

  this function reads characters from the specified file in binary format. The source file must be opened in binary format. This function returns the number of characters read.

fileGetString

usage method

Function function

  this function reads the string in the specified file into the buffer buff. Read characters until the end of the line is reached or the number of characters read is equal to buffsize -1. End of line marked as
  1. Through a single line feed
   2. Carriage return and line feed (DOS file)
  the end of a line in the buffer is represented by a newline character. See also fileGetStringSZ. If the end of a line is encountered, the returned string contains a newline character. Otherwise, the next call to fileGetString will start reading on the last line, starting with the first character in the last call that is not suitable for the buffer. If an error occurs, the return value is 0, otherwise it is 1.

fileGetStringSZ

usage method

Function function

  this function reads a string from the specified file. Read characters until the end of the line is reached or the number of characters read is equal to buffsize -1. End of line marked as
  1. Through a single line feed
   2. Carriage return and line feed (DOS file)
  line breaks will not be included in the buffer. See also fileGetString. If an error occurs, the return value is 0, otherwise it is 1.

filePutString

usage method

Function function

  writes the string to the specified file. If an error occurs, the return value is 0, otherwise it is 1.

fileRewind

usage method

Function function

  this function resets the position pointer to the beginning of the file. If an error occurs, the return value is 0, otherwise it is 1.

fileWriteBinaryBlock

usage method

Function function

  this function writes bytes of buffsize size in the specified file. The return value is the number of bytes written this time.

getOfflineFileName

usage method

Function function

  returns the full path of a file of the currently used offline source file. If format 1 is used, the first file is returned; otherwise, the index of the returned file is passed as a parameter. There are three conditions for the return value:
  0 - No error
  1 - Buffer too small
  2 - Other errors

Example

long isActive;
char buffer[256];
long numFiles;
long i;

write("Offline source files:");
numFiles = getNumOfflineFiles();
for (i = 0; i < numFiles; ++i)
{
  getOfflineFileName(i, buffer, 256);
  write("%s", buffer);
}

getNumOfflineFiles

usage method

Function function

  returns the number of configured offline source files. If an error occurs, the return value is - 1, otherwise it is the number of configured offline source files.

Example

long numFiles;

numFiles = getNumOfflineFiles();
write("Number of configured offline source files: %d", numFiles);

getAbsFilePath

usage method

Function function

  get the absolute path of the file. As a parameter, the currently configured relative path definition file should be used. When successful, this function returns the length of the full pathname, otherwise - 1. This function is not available in a distributed environment.

Example

on key 'x'
{
   char absPath[256];
   getAbsFilePath("Nodes\\Test.can", absPath, 256);
   write ("absPath: %s ", absPath);
}

getProfileArray / getProfileFloat / getProfileInt / getProfileString

usage method




Function function

  reads the value of the given variable from the specified part of the specified file.
   getProfileArray -- search for variable entries in the file under the section section. Entries are interpreted as a list of values separated by commas, tabs, spaces, semicolons, or slashes. The 0x prefix represents a hexadecimal value.
  getProfileFloat -- search for variable entries in the file filename under the section section. If its value is a number, this number is returned as the result of the function. If the file or entry is not found, or the entry does not contain a valid number, the default value def is returned as the result of the function.
   getProfileInt -- search for variable entries in the file filename under the section section. If its value is a number, this number is returned as the result of the function. If the file or entry is not found, or the entry does not contain a valid number, the default value def is returned as the result of the function.
   getProfileString -- search for variable entries in the file filename under the section section. Its contents (values) are written to the buffer buff. Its length must be passed correctly in buffsize. If the file or entry is not found, the default value def is copied to the buffer. If the read string length is greater than the buffer, the string is cut to the buffer length.

getUserFilePath

usage method

Function function

  get the absolute path of the user file.
   1. If it is executed in a distributed environment, if the user file is predefined, the absolute user file path (including file name) on the remote device (such as VN8900) is returned. If the file is not predefined, an error code is returned.
   2. If it is a stand-alone environment, if the user file is predefined, return the registered absolute file path (including file name) of the user file. If the file is not predefined, the function returns the same result as getAbsFilePath (converting the path relative to the configuration directory to an absolute path).

Example

on preStart
{
   char absPath[256];
   VgetUserFilePath("MyCAPLDll.INI", absPath, 256);
   MyCAPLDllFunction(absPath);
}

Open

usage method

Function function

  this function opens a file named filename.
   if access = 0, open the file for write access;
   if access = 1, open the file for read access.
   if mode = 0, the file is opened in text mode;
   if mode = 1, the file opens in binary mode.
  the file name must be passed to this function. The absolute file name is determined by the search process. First, a search is performed to determine whether the given file is located in the directory of the database. If the required file is not found, the active configuration directory is used.

openFileRead

usage method

Function function

  this function opens a file named filename for read access.
   if mode=0, the file is opened in text mode;
  if mode=1, the file is opened in binary mode

openFileWrite

usage method

Function function

  this function opens a file named filename for write access.
   if mode=0, writing can be executed in text mode;
  if mode=1, writing can be performed in binary mode. Existing files will be overwritten.
  if mode=2, append data at the end of the file used for text mode.
  if mode=3, append data at the end of the file in binary mode.
  before calling this function, the write path must be set through the function SetWritePath. Otherwise, the configuration directory will be used. The relative file name must be passed to the function.

RegisterUserFile

usage method

Function function

  dynamically register user files.
  this function can be used
   if the file names are unknown before running (for example, if they are read from an XML file, or if they contain counters as part of the file name)
  avoid the continuous implementation of the file list in the Options dialog box of CANoe.

setFilePath

usage method

Function function

  this function sets the read-write path of the directory. The path can be absolute or relative to the current active configuration.

Example

//set directory for reading
setFilePath("C:\\Windows\\TEMP", 0);
//set directory for writing
setFilePath("D:\\TEMP", 1);
//set directory for writing and reading
setFilePath("C:\\TEMP", 2);

on key 'i'
{
int defaultPara1;
int returnParaInt;
int counter;
double defaultPara2;
double returnParaFloat;
char buffer [256];
//define symbolic values for read/write access mode
dword FILE_PATH_R   = 0;
dword FILE_PATH_W   = 1;
dword FILE_PATH_RW  = 2;
defaultPara1 = -1;
defaultPara2 = -1;
//set absolute file path for writing
setFilePath("C:\\TEMP" , FILE_PATH_W);
// Write different values into the section "Parameter" of the INI file "Test.INI"
// If no file path is specified, the path from previous setFilePath command is used
writeProfileString ("Parameter","String","TestString","Test.INI");
writeProfileFloat ("Parameter","Float", 1.7845,"Test.INI");
writeProfileInt ("Parameter", "Integer", 8, "Test.INI");
// Read different values from the Section "Parameter" of the INI file "C:\\temp\\Test.INI"
// And display the values in the Write Window
//if an absolute path is to be used, this can be coded in the parameter <filename> directly
returnParaInt = getProfileInt("Parameter","Integer",defaultPara1,"C:\\TEMP\\Test.INI");
returnParaFloat = getProFileFloat("Parameter","Float",defaultPara2,"C:\\TEMP\\Test.INI");
getProFileString("Parameter","String","Default String", buffer, elcount(buffer), "C:\\TEMP\\Test.INI");
write("Integer: %d", returnParaInt);
write("Float: %f", returnParaFloat);
write("String: %s", buffer);
}

setWritePath

usage method

Function function

  this function sets the write path for the functions openFileWritewriteProfileString, writeprofileint and writeprofilefloat. The path can be absolute or relative to the current active configuration. This feature is not available in a distributed environment. After the drive letter and between folders, you must enter two "\", such as setWritePath("E:\testconfiguration\exercise").

Example

SetWritePath("C:\\TEMP")– after the drive letter (e.g. "C:") you have to enter two backslashes "\\"!

writeProfileFloat

usage method

Function function

  open the file name, search the section and write the variable entry with the value. If the entry already exists, the old value will be overwritten. Before calling this function, the write path must be set through the function setWritePath. Otherwise, the configuration directory will be used. The relative file name must be passed to the function.

writeProfileInt

usage method

Function function

  open the file name, search the section and write the variable entry with the value. If the entry already exists, the old value will be overwritten. Before calling this function, the write path must be set through the function setWritePath. Otherwise, the configuration directory will be used. The relative file name must be passed to the function.

writeProfileString

usage method

Function function

  open the file name, search the section and write the variable entry with the value. If the entry already exists, the old value will be overwritten. The result of the function is the number of characters written or 0 when an error occurs. Before calling this function, the write path must be set through the function setWritePath. Otherwise, the configuration directory will be used. The relative file name must be passed to the function.

File environment

Single host environment

  the file search process is used to determine the absolute file path. There are different processes for writing and reading files.
  file search order for read access:
   1. If the CAPL file function is used in CANoe simulation or test settings, please find the file name in the predefined user file list (configuration | options | extension | user file) and adopt the path specified there.
   2. If the file name is not found in the predefined user file list and the CAPL function setFilePath has been called before, the absolute path is determined based on the directory specified by setFilePath.
   3. If setFilePath is not called before opening the file for read access, search the file in the directory of the first database.
   4. If the file cannot be found there, the directory of other databases will be used.
   5. If the file is still not found, the directory of the configuration file will be used.
  file search order for write access:
   1. If the CAPL file function is used in CANoe simulation or test settings, please find the file name in the predefined user file list (configuration | options | extension | user file) and adopt the path specified there.
   2. If the file name is not found in the predefined user file list, and the CAPL function setFilePath or setWritePath has been called before, the absolute path is determined based on the directory specified by setFilePath/setWritePath.
   3. If setFilePath/setWritePath is not called before opening the file for write access, the configuration directory will be used as the basis for determining the absolute path.

Distributed simulation environment

  in the distributed simulation environment (distributed mode or VN8900), all files to be read in the CAPL program need to be predefined in the CANoe simulation or test settings under the configuration | options | extension | user file. At the beginning of the measurement, they are copied to a special synchronization directory on the remote device. The CAPL file function called by the kernel at remote runtime implicitly resolves the given file name to a file path based on this synchronization directory. Files for write access only can also be predefined as described above. If they have not been predefined, they will be implicitly registered as user files for the current measurement. All files created or changed on the remote device are transferred back to the user PC at the end of the measurement. CAPL functions that require a file name also accept the path in front of the file name. If the file name is already registered, the path component is ignored. In the case of implicitly registering a user file, the file path is interpreted as the file path on the user PC to which the file is copied at the end of the measurement. The CAPL functions setFilePath, setWritePath, and getAbsFilePath are not available in a distributed environment.

Example

  this part is not an example of a function, but the implementation of the whole module and the use of some functions.

fileGetBinaryBlock

variables
{
  msTimer writeTimer;
  long glbPeriod = 250;

  dword glbHandle = 0;
  struct Data
  {
    long value;
  }
  glbData;
}

on Start
{
  byte buffer[4];
  long ret;

  //
  // Opens the file in binary mode for read access.
  //
  // To determine the absolute path, the search procedure will be used.
  // The file must be located in the directory of the databases or the
  // configuration directory.
  //
  glbHandle = OpenFileRead ("Data.Bin",1);

  if ( glbHandle!=0 )
  {
    //
    // got to end of file ...
    //
    while ( fileGetBinaryBlock(buffer,elcount(buffer),glbHandle)!=0 ) {};
    //
    // Get the last parameters
    // (saved on disk after the end of the last measurement)
    //
    memcpy(glbData, buffer);
    write ("Last value %d.",glbData.value);
    fileClose (glbHandle);
  }
  else
  {
    write ("File 'Data.Bin' was not opened for read access.");
  }
  //
  // Open the file in binary mode for write access.
  //
  // The write path was not set using the function setWritePath(), so
  // the configuration directory will be used instead. This is the
  // default behavior.
  //
  glbHandle = OpenFileWrite ("Data.Bin",3);

  if ( glbHandle!=0 )
  {
    setTimer (writeTimer,glbPeriod);
  }
  else
  {
    write ("File 'Data.Bin' was not opened for write access.");
  }
}

on timer writeTimer
{
  struct Data randomValue;
  byte buffer [4];

  if ( glbHandle!=0 )
  {
    randomValue.value = random (32767);
    memcpy(buffer, randomValue);
    fileWriteBinaryBlock (buffer, elcount(buffer),glbHandle);
    write("Last value written: %d", randomValue.value);
    setTimer (writeTimer,glbPeriod);
  }
  else
  {
    write ("Error, invalid file handle.");
  }
}

on StopMeasurement
{
  fileClose (glbHandle);
}

fileGetString

variables
{
  msTimer writeTimer;
  long glbPeriod = 250;

  dword glbHandle = 0;
  long glbValue;
}

on Start
{
  char buffer[64];
  long ret;

  //
  // Opens the file in ASCII mode for read access.
  //
  // To determine the absolute path, the search procedure will be used.
  // The file must be located in the directory of the databases or the
  // configuration directory.
  //
  glbHandle = OpenFileRead ("Data.Txt",0);

  if ( glbHandle!=0 )
  {
    //
    // got to end of file ...
    //
    while ( fileGetString(buffer,elcount(buffer),glbHandle)!=0 ) {};
    //
    // Get the last parameters
    // (saved on disk after the end of the last measurement)
    //
    glbValue = atol (buffer);
    write ("Last value %d.",glbValue);
    fileClose (glbHandle);
  }
  else
  {
    write ("File 'Data.Txt' was not opened for read access.");
  }
  //
  // Open the file in ASCII mode for write access.
  //
  // The write path was not set using the function setWritePath(), so
  // the configuration directory will be used instead. This is the
  // default behavior.
  //
  glbHandle = OpenFileWrite ("Data.Txt",2);

  if ( glbHandle!=0 )
  {
    setTimer (writeTimer,glbPeriod);
  }
  else
  {
    write ("File 'Data.Txt' was not opened for write access.");
  }
}

on timer writeTimer
{
  long randomValue;
  char buffer [64];

  if ( glbHandle!=0 )
  {
    randomValue = random (32767);
    snprintf (buffer,elcount(buffer)," %d \n",randomValue);
    filePutString (buffer, elcount(buffer),glbHandle);
    setTimer (writeTimer,glbPeriod);
  }
  else
  {
    write ("Error, invalid file handle.");
  }
}

on StopMeasurement
{
  fileClose (glbHandle);
}

fileGetStringSZ

variables
{
  msTimer writeTimer;
  long glbPeriod = 250;

  dword glbHandle = 0;
  long glbValue;
}

on Start
{
  char buffer[64];
  long ret;

  //
  // Opens the file in ASCII mode for read access.
  //
  // To determine the absolute path, the search procedure will be used.
  // The file must be located in the directory of the databases or the
  // configuration directory.
  //
  glbHandle = OpenFileRead ("Data.Txt",0);

  if ( glbHandle!=0 )
  {
    //
    // got to end of file ...
    //
    while ( fileGetStringSZ(buffer,elcount(buffer),glbHandle)!=0 ) {};
    //
    // Get the last parameters
    // (saved on disk after the end of the last measurement)
    //
    glbValue = atol (buffer);
    write ("Last value %d.",glbValue);
    fileClose (glbHandle);
  }
  else
  {
    write ("File 'Data.Txt' was not opened for read access.");
  }
  //
  // Open the file in ASCII mode for write access.
  //
  // The write path was not set using the function setWritePath(), so
  // the configuration directory will be used instead. This is the
  // default behavior.
  //
  glbHandle = OpenFileWrite ("Data.Txt",2);

  if ( glbHandle!=0 )
  {
    setTimer (writeTimer,glbPeriod);
  }
  else
  {
    write ("File 'Data.Txt' was not opened for write access.");
  }
}

on timer writeTimer
{
  long randomValue;
  char buffer [64];

  if ( glbHandle!=0 )
  {
    randomValue = random (32767);
    snprintf (buffer,elcount(buffer)," %d \n",randomValue);
    filePutString (buffer, elcount(buffer),glbHandle);
    setTimer (writeTimer,glbPeriod);
  }
  else
  {
    write ("Error, invalid file handle.");
  }
}

on StopMeasurement
{
  fileClose (glbHandle);
}

Tags: software testing CAPL

Posted on Wed, 24 Nov 2021 08:58:19 -0500 by maverick3d