Main Content

crltest.ContainsPattern.ContainsSubstring Class

Namespace: crltest.ContainsPattern

Constraint to check if C/C++ file contains a specific pattern

Since R2024b

Description

The crltest.ContainsPattern class provides a constraint to test if C/C++ file contains a specific pattern.

The constraint filters out both line comments (starting with //) and block comments (enclosed in /* ... */) from the C/C++ file before searching for the pattern.

The pattern search is case-sensitive.

The constraint is designed to use with files containing C or C++ code only.

Creation

Description

c = crltest.ContainsPattern(pattern) creates a constraint to test if a file contains a specified pattern. The constraint is satisfied by a string scalar or character vector that contains pattern.

example

c = crltest.ContainsPattern(pattern,'WithCount',count) an optional name-value pair specifying the expected number of times the pattern should occur within the file.

example

Input Arguments

expand all

Expected pattern, specified as a nonempty string scalar or character vector.

Name-Value Arguments

Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Example: c = crltest.ContainsPattern(pattern,WithCount=count)

Example: c = crltest.ContainsPattern(pattern,"WithCount",count)

Number of times pattern must occur, specified as a positive integer scalar.

You can specify this name-value argument to count only nonoverlapping occurrences of a pattern. For example, this tests fails.

Data Types: double | single | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

Examples

collapse all

Test for using the crltest.ContainsPattern constraint to find patterns in C code for multiple occurrence.

The following MATLAB code is a example.h file.

//example.h
#include <stdio.h>
 
// Definition of foo1 function
void foo1(int a) {
    printf("foo1 function: %d\n", a);
}
 
// Definition of foo2 function
void foo2(int a) {
    printf("foo2 function: %d\n", a);
}

The following MATLAB code is a example.c file.

#include "example.h"
 
int main() {
    int num1 = 10; // First int literal
    int num2 = 20; // Second int literal
 
    foo1(num1); // Calling foo1 function with num1
    foo2(num2); // Calling foo2 function with num2
 
    return 0;
}

Run the following MATLAB code to verify if example.c file contains the pattern exactly 3 times.

classdef sampleTest1 < matlab.unittest.TestCase
	methods (Test)
         function checkForCodeReplacement(testCase)
 
             cfile = 'example.c';
             pattern = 'int';
             constraint = crltest.ContainsPattern(pattern, 'WithCount', 3);
 
             % Verify the generated code contains the expected pattern
             testCase.verifyThat(cfile,constraint);
         end
     end
end

Test to search a function call in C file using the crltest.ContainsPattern constraint. This constrain will search for exactly one occurrence as withcount is default value.

In order to search a function call in the generated code, i.e., foo1() exist in the *.c/*.cpp file then '(' can be appended to the pattern.

Run the following MATLAB code.

classdef sampleTest2 < matlab.unittest.TestCase
	methods (Test)
         function checkForFunctionCall(testCase)
			cfile = 'example.c';
			pattern = 'foo1(';
			constraint = crltest.ContainsPattern(pattern);
			testCase.verifyThat(cfile,constraint);
		end
    end
end

Test to find multiple patterns in a single file.

Run the following MATLAB code..

classdef sampleTest3 < matlab.unittest.TestCase
	methods (Test)
         function checkForFunctionCall(testCase)
			file = 'example.c';
			pattern1 = 'foo1(';
			pattern2 = 'foo2(';
			constraint1 = crltest.ContainsPattern(pattern1);
			constraint2 = crltest.ContainsPattern(pattern2);
			testCase.verifyThat(cfile,constraint1);
			testCase.verifyThat(cfile,constraint2);
		end
    end
end

Version History

Introduced in R2024b