A method and device for generating C program test cases based on Csmith random generator

By obtaining the preset probability information table and safety check method, C program test cases are generated, which solves the problems of low efficiency and strong dependence of the Csmith generator, realizes efficient and customized test case generation, and improves test efficiency and coverage.

CN115687154BActive Publication Date: 2025-09-09JIANGNAN INST OF COMPUTING TECH
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202211487167.6
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2022-11-25
Publication Date
2025-09-09
Estimated Expiration
2042-11-25

AI Technical Summary

Technical Problem

The existing Csmith random generator may fail to terminate when generating C program test cases. It is inefficient and highly dependent on the probability information table input by the user, making it difficult to efficiently detect errors in the generated test cases.

Method used

By obtaining the preset probability information table, the generator is initialized and generates a test case program including the main function, variable types and other functions, and uses filters to perform security checks and verifications. After the filter check is completed, the generator organizes and outputs the test case program and calculates the checksum of non-pointer global variables.

Benefits of technology

It realizes the automatic generation of a large number of test cases, improves the efficiency and pertinence of test case generation, can effectively expose more problems, and saves labor costs.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN115687154B_ABST
    Figure CN115687154B_ABST
Patent Text Reader

Abstract

The present invention provides a method and device for generating C program test cases based on a Csmith random generator, belonging to the technical field of C program testing. The method includes the following steps: S1: obtaining a preset probability information table and obtaining preset parameters through the preset probability information table; S2: initializing a generator according to the preset parameters; S3: generating a test case program including a main function, variable types, and other functions based on the initialization information, and outputting auxiliary information of the test case program when generating the main function; S4: a filter performing a security check on the generated program fragments; S5: after the filter has checked all program fragments, the generator organizes and outputs the generated test case program, calculates the checksum of non-pointer global variables based on the generated test case program, and prints the checksum. The present invention automatically generates a large number of test cases, can efficiently expose more problems, and can perform customized operations based on input, saving labor costs.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of C program testing technology, and in particular to a method and device for generating C program test cases based on a Csmith random generator. Background Art

[0002] Compiler testing using hand-crafted high-level language test cases often fails to uncover hidden bugs. To this end, the industry has been dedicated to research on random test case generation. The basic idea behind random test case generation is to randomly generate a value within the search space of each input variable in the program, and then combine these values ​​to form a test case. This method is simple and time-efficient, but for more complex programs, test cases generated using random methods struggle to meet coverage requirements. Currently, Csmith uses complex heuristics to avoid generating C programs with undefined behavior or dependencies on unspecified behavior. However, Csmith has two drawbacks: First, due to its random nature and the efficiency of its generation, it can fail to terminate, resulting in prolonged periods of inability to generate test cases. Second, Csmith relies on a user-entered probability information table, which may not be sufficient to efficiently generate test cases that detect bugs. Summary of the Invention

[0003] In order to solve the above problems, an embodiment of the present application provides a method and device for generating C program test cases based on the Csmith random generator.

[0004] In a first aspect, the present application provides a method for generating C program test cases based on the Csmith random generator, comprising the following steps:

[0005] S1: Obtain a preset probability information table, and obtain preset parameters through the preset probability information table;

[0006] S2: The generator is initialized according to the preset parameters;

[0007] S3: The generator generates a test case program including a main function, variable types, and other functions based on the initialization information, and outputs auxiliary information of the test case program when generating the main function;

[0008] S4: The filter performs security checks on the generated program fragments;

[0009] S5: After the filter has checked all program fragments, the generator organizes and outputs the generated test case program, calculates the checksum of non-pointer global variables based on the generated test case program, and prints the checksum.

[0010] Preferably, step S1 specifically includes:

[0011] S11: Obtain all attribute values ​​of the probability information table;

[0012] S12: Marking attribute values ​​that can efficiently generate test cases based on historical data;

[0013] S13: performing cluster analysis on the marked attribute values ​​to obtain a preset probability information table;

[0014] S14: Determine whether the input user parameter is received. If it is determined that the user parameter is not received, use the attribute value in the preset probability information table as the preset parameter. If it is determined that the user parameter is received, compare the user parameter with the preset probability information table.

[0015] S15: If the user parameters include all the attribute values ​​in the preset probability information table, the user parameters are used as preset parameters; if the user parameters do not include all the attribute values ​​in the preset probability information table, an input parameter error is output.

[0016] Preferably, the variable types in step S3 include simple data types, undefined floating-point data, structures, union declarations, global variables, and forward declarations.

[0017] Preferably, generating other functions in step S3 specifically includes: randomly selecting the return value type, collecting pointing information and union information, generating a parameter list of the function, generating a function body, and collecting three types of information when generating the function body: the impact value of the current environment, the current context environment, and the extended call list. If other functions are called in the function body, function parameters are defined for the called function and a function body is generated.

[0018] Preferably, in step S4, the filter performs a security check on the generated program fragment, specifically including: obtaining the program fragment to be checked; the filter judges the legality of the currently generated program fragment, if it is judged that the program fragment will cause a permanent true loop or the loop data corresponding to the program fragment exceeds the data threshold, the filter returns a program error, the generator abandons the program fragment and rolls back to the sequence point corresponding to the last checked program fragment, if it is judged that there is no problem with the program fragment, then continue to obtain the subsequent program fragment to be checked.

[0019] Preferably, step S5 specifically includes: the generator outputs the structure, joint declaration, global variables, forward declaration, other functions, and summary information in sequence; when the top-level function is completely generated, the generator outputs a main function; the main function calls the top-level generated function, calculates the checksum of non-pointer global variables, and prints the checksum.

[0020] Preferably, if an unimplemented function is found in the program generated by the filter check, the generator generates a function body for the function.

[0021] In a second aspect, the embodiment of the present application provides a device for generating C program test cases based on the Csmith random generator, comprising:

[0022] A parameter acquisition module is used to obtain a preset probability information table and obtain preset parameters through the preset probability information table;

[0023] A generator is used to initialize according to preset parameters, generate a test case program including a main function, variable types, and other functions based on the initialization information, and output auxiliary information of the test case program when generating the main function;

[0024] Filter, performs security checks on the generated program fragments;

[0025] The generator is used to organize and output the generated test case program after the filter has checked all program fragments, calculate the checksum of non-pointer global variables based on the generated test case program, and print the checksum.

[0026] In a third aspect, an embodiment of the present application provides an electronic device comprising a memory, a processor, and a computer program stored in the memory and runnable on the processor, wherein when the processor executes the computer program, the steps of the method provided in the first aspect or any possible implementation of the first aspect are implemented.

[0027] In a fourth aspect, an embodiment of the present application provides a computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, implements the steps of the method provided in the first aspect or any possible implementation of the first aspect.

[0028] The beneficial effects of the present invention are: 1) a large number of test cases can be automatically generated, which can efficiently expose more problems and can be customized according to input, saving labor costs; 2) preset parameters are obtained through a preset probability information table, and customized test cases are implemented according to the probability information table, which can improve the possibility of rapid targeted testing during the testing process; 3) the filter performs security checks on the generated program fragments, the filter enforces context sensitivity, and performs generation control and security checks, which greatly improves the efficiency of test case generation. BRIEF DESCRIPTION OF THE DRAWINGS

[0029] In order to more clearly illustrate the technical solutions in the embodiments of the present application, the following briefly introduces the drawings required for use in the embodiments. Obviously, the drawings described below are only some embodiments of the present application. For ordinary technicians in this field, other drawings can be obtained based on these drawings without creative work.

[0030] Figure 1 A flowchart of a method for generating C program test cases based on the Csmith random generator provided in an embodiment of the present application;

[0031] Figure 2 A schematic diagram of the structure of a device for generating C program test cases based on the Csmith random generator provided in an embodiment of the present application;

[0032] Figure 3 A schematic diagram of the structure of an electronic device provided in an embodiment of the present application. DETAILED DESCRIPTION

[0033] The technical solutions in the embodiments of the present application will be described clearly and completely below in conjunction with the drawings in the embodiments of the present application.

[0034] In the following introduction, the terms "first" and "second" are used for descriptive purposes only and should not be understood as indicating or implying relative importance. The following introduction provides multiple embodiments of the present application. Different embodiments can be replaced or combined, so the present application can also be considered to include all possible combinations of the same and / or different embodiments described. Therefore, if one embodiment includes features A, B, and C, and another embodiment includes features B and D, then the present application should also be considered to include embodiments containing one or more of all other possible combinations of A, B, C, and D, even though the embodiment may not be clearly described in the following text.

[0035] The following description provides examples and does not limit the scope, applicability, or examples set forth in the claims. Changes may be made to the function and arrangement of the elements described without departing from the scope of the present application. Various examples may appropriately omit, replace, or add various processes or components. For example, the described method may be performed in an order different from the order described, and various steps may be added, omitted, or combined. In addition, features described in some examples may be combined in other examples.

[0036] See also Figure 1 . Figure 1 This is a flow chart of a method for generating C program test cases based on the Csmith random generator provided in an embodiment of the present application. In this embodiment of the present application, the method includes the following steps:

[0037] S1: Obtain a preset probability information table, and obtain preset parameters through the preset probability information table;

[0038] S2: The generator is initialized according to the preset parameters;

[0039] S3: The generator generates a test case program including a main function, variable types, and other functions based on the initialization information, and outputs auxiliary information of the test case program when generating the main function;

[0040] S4: The filter performs security checks on the generated program fragments;

[0041] S5: After the filter has checked all program fragments, the generator organizes and outputs the generated test case program, calculates the checksum of non-pointer global variables based on the generated test case program, and prints the checksum.

[0042] In the embodiments of this application, context-sensitivity refers to considering the context of function calls during interprocedural analysis. A subroutine or function may be called by multiple procedures. When different procedures call it, the actual parameters passed to it or the global variables at the time may differ. This is called context, and context-sensitivity accounts for these differences. Filter functions within filters enforce context sensitivity. During the generation process, certain security checks are performed, and code snippets are created only when all security checks pass. This supports random instruction or instruction sequence testing and random parallel testing. The production process in this application first creates a main function and a set of structure type declarations, each containing a random number of member variables of randomly determined types. Using the main function as a starting point, the remainder of the test case code is generated based on a subset of C syntax. Based on the current generation state, a permitted generation is selected using a probability table and a filter function. Based on Csmith, restrictions are implemented to control processes such as entering a never-true loop and loop variables with very large data values ​​to ensure controllable generation. The present invention inputs a probability information table and user input parameters, and generates test cases through a test case random generator, wherein the randomly generated test cases will eventually output a checksum, and the invention finally adopts a random differential testing method for testing. Differential testing means that if a program has multiple implementations of the same specification, all implementations should be able to obtain the same results under the same legal input. The overall solution is divided into a two-stage process. The first stage is the generation of alternative probability information tables. Based on a large amount of historical data annotation information obtained by random generation and verification, a probability information table that can generate effective test cases (i.e., can verify errors) can be obtained. Cluster analysis can be performed to obtain a set of probability information tables with better effects as alternatives. The second stage is to generate test cases using a test case generator. The present invention can automatically generate a large number of test cases, can efficiently expose more problems, and can perform customized operations based on the input, saving labor costs.

[0043] In one embodiment, step S1 specifically includes:

[0044] S11: Obtain all attribute values ​​of the probability information table;

[0045] S12: Marking attribute values ​​that can efficiently generate test cases based on historical data;

[0046] S13: performing cluster analysis on the marked attribute values ​​to obtain a preset probability information table;

[0047] S14: Determine whether the input user parameter is received. If it is determined that the user parameter is not received, use the attribute value in the preset probability information table as the preset parameter. If it is determined that the user parameter is received, compare the user parameter with the preset probability information table.

[0048] S15: If the user parameters include all the attribute values ​​in the preset probability information table, the user parameters are used as preset parameters; if the user parameters do not include all the attribute values ​​in the preset probability information table, an input parameter error is output.

[0049] In an embodiment of the present application, cluster analysis is performed using the labels of a large number of random probability information tables to obtain a set of probability information with good results, from which a probability information table can be selected for the user to choose. The attributes of the probability information table can form a multidimensional space. Historical data labels the attribute values ​​of the information table that are most effective in generating test cases. Based on these labels, several relatively good attribute sets can be clustered. The optimal attribute set is selected from these several attribute sets as the preset probability information table. If the user enters user parameters, the user probability includes multiple attribute values, and the entered user parameters are compared with the preset probability information table. If the comparison is successful, the user parameters are used as the preset parameters. If the comparison is unsuccessful, the parameters need to be re-entered. If the user does not enter user parameters, the attribute values ​​in the generated preset probability information table are used by default. First, the parameters are set. The user's input parameters are received and the default parameters are set. If there is a conflict between the user's parameters, the conflict resolution function displays the reason for the conflict so that the user can modify the parameter settings. The parameters in the generator include the parameters for the entire generator and the parameters for the production attributes. The parameters for the entire generator can be set in either depth-first or breadth-first mode. Cluster analysis only uses the parameters for the production attributes. The parameters of the overall generator do not need to be entered by the user, and these parameters are default values.

[0050] In one embodiment, the variable types in step S3 include simple data types, undefined floating-point data, structures, union declarations, global variables, and forward declarations.

[0051] In an embodiment of the present application, a generator for generating a program is randomly selected and initialized, and different test generation strategies are initialized according to the user's selection. Initialization is adjusted according to the input parameters, and initialization is to initialize all parameters according to the input parameters. If the user does not input, the overall generator parameters are default, and the parameters of the generated attributes are the attribute values ​​of the probability information table generated by clustering. The user can use -help to query the configurable parameters. If the user sets the parameters by input, the parameter values ​​can be adjusted accordingly. Generate variable types, and generate various types according to the above parameter settings. The types here include 11 simple data types, undefined floating-point data, as well as composite types, structures, joint declarations, global variables, and forward declarations.

[0052] In one embodiment, generating other functions in step S3 specifically includes: randomly selecting a return value type, collecting pointing information and union information, generating a parameter list of the function, generating a function body, and collecting three types of information when generating the function body: the impact value of the current environment, the current context environment, and the extended call list. In the function body, if other functions are called, function parameters are defined for the called function and a function body is generated.

[0053] In one embodiment, the filter performs a security check on the generated program fragment in step S4, specifically including: obtaining the program fragment to be checked; the filter judges the legality of the currently generated program fragment, if it is judged that the program fragment will cause a permanent true loop or the loop data corresponding to the program fragment exceeds the data threshold, the filter returns a program error, the generator abandons the program fragment and rolls back to the sequence point corresponding to the last checked program fragment, if it is judged that there is no problem with the program fragment, then continue to obtain the subsequent program fragment to be checked.

[0054] In this embodiment, a function is generated, its return value type is randomly selected, and pointer and union information are collected to generate the function's parameter list and body. When generating the function body, three types of information are collected: the impact value of the current environment (such as read and write information), the current context, and the extended call list. The function body may call other functions, for which function parameters must be defined and their bodies randomly generated. During this process, the filter verifies the legitimacy of the currently generated program fragment and determines whether it enters a potentially unterminated perpetual true loop or contains very large data. If the filter return value indicates a program error, the current fragment is discarded and rolled back to the previous sequence point.

[0055] In one embodiment, step S5 specifically includes: the generator outputs structures, joint declarations, global variables, forward declarations, other functions, and summary information in sequence; when the top-level function is completely generated, the generator outputs a main function; the main function calls the top-level generated function, calculates the checksum of non-pointer global variables, and prints the checksum.

[0056] In this embodiment of the present application, the generated program is organized and output, first outputting structures, union declarations, global variables, and forward declarations, then outputting generated functions, and finally outputting summary information about the generated program, such as program depth and number. Ultimately, when the top-level functions are fully generated, the C language test case generator outputs a main function. This main function calls the top-level random generation function, calculates the checksum of non-pointer global variables, and prints the checksum.

[0057] In one embodiment, if an unimplemented function is found in the program generated by the filter check, the generator generates a function body for the function.

[0058] In this embodiment of the present application, the generated program is checked for any unimplemented functions. If so, a function body is generated for each function. When the test case generator creates a call to a new function (one whose body does not yet exist), generation of the current function is paused until the new function is completed. An unimplemented function is one that does not implement any functionality and initially consists of only a left parenthesis. Generating the function body then generates the right parenthesis, completing the generation of the function.

[0059] The following will be combined with the Figure 2 , the device for generating C program test cases based on Csmith random generator provided by the embodiment of the present application is introduced in detail. Figure 2 The device shown in the figure generates C program test cases based on the Csmith random generator, which is used to execute the application Figure 1 For the convenience of explanation, only the part related to the embodiment of the present application is shown. For the specific technical details not disclosed, please refer to the present application. Figure 1 The embodiment shown.

[0060] See Figure 2 , Figure 2 This is a schematic diagram of the structure of a device for generating C program test cases based on a Csmith random generator provided by an embodiment of the present application. Figure 2 As shown, the device includes

[0061] Parameter acquisition module 201, used to obtain a preset probability information table, and obtain preset parameters through the preset probability information table;

[0062] Generator 202, configured to initialize according to preset parameters, generate a test case program including a main function, variable types, and other functions based on the initialization information, and output auxiliary information of the test case program when generating the main function;

[0063] Filter 203, performs security checks on the generated program fragments;

[0064] The generator 202 is used to organize and output the generated test case program after the filter has checked all program fragments, calculate the checksum of non-pointer global variables based on the generated test case program, and print the checksum.

[0065] In the embodiment of the present application, variable types include simple data types, undefined floating-point data, structures, union declarations, global variables, and forward declarations;

[0066] Generating other functions includes: randomly selecting the return value type, collecting pointer information and union information, generating the function parameter list, generating the function body, collecting the impact value of the current environment, the current context environment, and the extended call list when generating the function body, and defining the function parameters for the called function when calling other functions in the function body and generating the function body;

[0067] The filter performs a security check on the generated program fragments, specifically including: obtaining the program fragment to be checked; the filter judges the legality of the currently generated program fragment. If it is judged that the program fragment will cause a perpetual true loop or the loop data corresponding to the program fragment exceeds the data threshold, the filter returns a program error, the generator discards the program fragment and rolls back to the previous sequence point. If it is judged that there is no problem with the program fragment, the subsequent program fragment to be checked will continue to be obtained;

[0068] If an unimplemented function is found in the program generated by the filter check, the generator generates a function body for the function;

[0069] The generator outputs structures, union declarations, global variables, forward declarations, other functions, and summary information in sequence. When the top-level function is completely generated, the generator outputs a main function. The main function calls the top-level generated function, calculates the checksum of non-pointer global variables, and prints the checksum.

[0070] In one embodiment, the parameter acquisition module 201 may include:

[0071] A probability information table acquisition unit is used to obtain all attribute values ​​of the probability information table, mark attribute values ​​that can efficiently generate test cases based on historical data, and perform cluster analysis on the marked attribute values ​​to obtain a preset probability information table;

[0072] The parameter judgment unit is used to judge whether the input user parameters are received. If it is judged that the user parameters are not received, the attribute values ​​in the preset probability information table are used as the preset parameters. If it is judged that the user parameters are received, the user parameters are compared with the preset probability information table. If the user parameters include all the attribute values ​​in the preset probability information table, the user parameters are used as the preset parameters. If the user parameters do not include all the attribute values ​​in the preset probability information table, an input parameter error is output.

[0073] Those skilled in the art will clearly understand that the technical solutions of the embodiments of the present application can be implemented with the help of software and / or hardware. "Unit," "module," and "section" in this specification refer to software and / or hardware that can independently perform or cooperate with other components to perform specific functions, where the hardware can be, for example, a Field-Programmable Gate Array (FPGA) or an Integrated Circuit (IC).

[0074] Each processing unit and / or module in the embodiments of the present application may be implemented by an analog circuit that implements the functions described in the embodiments of the present application, or may be implemented by software that executes the functions described in the embodiments of the present application.

[0075] See also Figure 3 , which shows a schematic diagram of the structure of an electronic device involved in an embodiment of the present application, the electronic device can be used to implement Figure 1 The method in the embodiment shown. Figure 3 As shown, the electronic device 300 may include: at least one central processor 301 , at least one network interface 304 , a user interface 303 , a memory 305 , and at least one communication bus 302 .

[0076] The communication bus 302 is used to implement the connection and communication between these components.

[0077] The user interface 303 may include a display screen (Display) and a camera (Camera). Optionally, the user interface 303 may also include a standard wired interface and a wireless interface.

[0078] The network interface 304 may optionally include a standard wired interface or a wireless interface (such as a WI-FI interface).

[0079] The central processing unit 301 may include one or more processing cores. The central processing unit 301 utilizes various interfaces and circuits to connect various components within the electronic device 300. It executes instructions, programs, code sets, or instruction sets stored in the memory 305 and accesses data stored in the memory 305 to perform various functions and process data for the terminal 300. Optionally, the central processing unit 301 may be implemented in hardware using at least one of a digital signal processing (DSP), a field-programmable gate array (FPGA), and a programmable logic array (PLA). The central processing unit 301 may integrate one or a combination of a central processing unit (CPU), a graphics processing unit (GPU), and a modem. The CPU primarily processes the operating system, user interface, and application programs; the GPU is responsible for rendering and drawing content displayed on the display; and the modem handles wireless communications. It is understood that the modem may also be implemented as a separate chip, rather than integrated into the central processing unit 301.

[0080] Among them, the memory 305 may include a random access memory (RAM) or a read-only memory (Read-Only Memory). Optionally, the memory 305 includes a non-transitory computer-readable storage medium. The memory 305 can be used to store instructions, programs, codes, code sets or instruction sets. The memory 305 may include a program storage area and a data storage area, wherein the program storage area may store instructions for implementing an operating system, instructions for at least one function (such as a touch function, a sound playback function, an image playback function, etc.), instructions for implementing the above-mentioned various method embodiments, etc.; the data storage area may store data involved in the above-mentioned various method embodiments, etc. The memory 305 may also be optionally at least one storage device located away from the aforementioned central processor 301. As Figure 3 As shown, the memory 305 as a computer storage medium may include an operating system, a network communication module, a user interface module, and program instructions.

[0081] exist Figure 3In the electronic device 300 shown, the user interface 303 is mainly used to provide an input interface for the user and obtain user input data; and the central processing unit 301 can be used to call the application stored in the memory 305 that generates C program test cases based on the Csmith random generator, and specifically perform the following operations:

[0082] S1: Obtain a preset probability information table, and obtain preset parameters through the preset probability information table;

[0083] S2: The generator is initialized according to the preset parameters;

[0084] S3: The generator generates a test case program including a main function, variable types, and other functions based on the initialization information, and outputs auxiliary information of the test case program when generating the main function;

[0085] S4: The filter performs security checks on the generated program fragments;

[0086] S5: After the filter has checked all program fragments, the generator organizes and outputs the generated test case program, calculates the checksum of non-pointer global variables based on the generated test case program, and prints the checksum.

[0087] The present application also provides a computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, implements the steps of the above method. The computer-readable storage medium may include, but is not limited to, any type of disk, including a floppy disk, an optical disk, a DVD, a CD-ROM, a microdrive, a magneto-optical disk, a ROM, a RAM, an EPROM, an EEPROM, a DRAM, a VRAM, a flash memory device, a magnetic card or an optical card, a nanosystem (including a molecular memory IC), or any type of medium or device suitable for storing instructions and / or data.

[0088] It should be noted that for the aforementioned method embodiments, for the sake of simplicity, they are all expressed as a series of action combinations, but those skilled in the art should be aware that this application is not limited by the order of the actions described, because according to this application, certain steps can be performed in other orders or simultaneously. Secondly, those skilled in the art should also be aware that the embodiments described in the specification are all preferred embodiments, and the actions and modules involved are not necessarily required by this application.

[0089] In the above embodiments, the description of each embodiment has its own focus. For parts that are not described in detail in a certain embodiment, reference can be made to the relevant descriptions of other embodiments.

[0090] In the several embodiments provided in this application, it should be understood that the disclosed devices can be implemented in other ways. For example, the device embodiments described above are merely schematic. For example, the division of the units is merely a logical function division. In actual implementation, there may be other division methods, such as multiple units or components can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the mutual coupling or direct coupling or communication connection shown or discussed can be through some service interface, and the indirect coupling or communication connection of the device or unit can be electrical or other forms.

[0091] The units described as separate components may or may not be physically separate, and the components shown as units may or may not be physical units, that is, they may be located in one place or distributed across multiple network units. Some or all of these units may be selected to achieve the purpose of this embodiment according to actual needs.

[0092] In addition, the functional units in the various embodiments of the present application may be integrated into a single processing unit, or each unit may exist physically separately, or two or more units may be integrated into a single unit. The aforementioned integrated units may be implemented in the form of hardware or software functional units.

[0093] If the integrated unit is implemented in the form of a software functional unit and sold or used as an independent product, it can be stored in a computer-readable memory. Based on this understanding, the technical solution of the present application, or the part that contributes to the prior art, or all or part of the technical solution can be embodied in the form of a software product. The computer software product is stored in a memory and includes a number of instructions for enabling a computer device (which can be a personal computer, server, or network device, etc.) to execute all or part of the steps of the method described in each embodiment of the present application. The aforementioned memory includes: U disk, read-only memory (ROM), random access memory (RAM), mobile hard disk, magnetic disk, or optical disk, etc., various media that can store program code.

[0094] Those skilled in the art will appreciate that all or part of the steps in the various methods of the above embodiments may be completed by instructing related hardware through a program. The program may be stored in a computer-readable memory, which may include a flash drive, a read-only memory (ROM), a random access memory (RAM), a magnetic disk, or an optical disk.

[0095] The above is only an exemplary embodiment of the present disclosure and cannot be used to limit the scope of the present disclosure. That is, any equivalent changes and modifications made according to the teachings of the present disclosure are still within the scope of the present disclosure. After considering the specification and practicing the disclosure herein, those skilled in the art will easily think of the implementation scheme of the present disclosure. This application is intended to cover any variation, use or adaptation of the present disclosure, which follows the general principles of the present disclosure and includes common knowledge or customary technical means in the art that are not recorded in the present disclosure. The description and examples are to be regarded as exemplary only, and the scope and spirit of the present disclosure are defined by the claims.

Claims

1. A method for generating C program test cases based on Csmith random generator, characterized in that: The steps include: S1: Obtain a preset probability information table, and obtain preset parameters through the preset probability information table; S2: The generator is initialized according to the preset parameters; S3: The generator generates a test case program including a main function, variable types, and other functions based on the initialization information, and outputs auxiliary information of the test case program when generating the main function; S4: The filter performs security checks on the generated program fragments; S5: After the filter has checked all program fragments, the generator organizes and outputs the generated test case program, calculates the checksum of non-pointer global variables based on the generated test case program, and prints the checksum; Step S1 specifically includes: S11: Obtain all attribute values ​​of the probability information table; S12: Marking attribute values ​​that can efficiently generate test cases based on historical data; S13: performing cluster analysis on the marked attribute values ​​to obtain a preset probability information table; S14: Determine whether the input user parameter is received. If it is determined that the user parameter is not received, use the attribute value in the preset probability information table as the preset parameter. If it is determined that the user parameter is received, compare the user parameter with the preset probability information table. S15: If the user parameters include all the attribute values ​​in the preset probability information table, the user parameters are used as preset parameters; if the user parameters do not include all the attribute values ​​in the preset probability information table, an input parameter error is output.

2. A method for generating C program test cases based on Csmith random generator according to claim 1, characterized in that: The variable types in step S3 include simple data types, undefined floating-point data, structures, union declarations, global variables, and forward declarations.

3. A method for generating C program test cases based on Csmith random generator according to claim 1, characterized in that: Generating other functions in step S3 specifically includes: randomly selecting the return value type, collecting pointing information and union information, generating the function parameter list, generating the function body, collecting three types of information when generating the function body: the impact value of the current environment, the current context environment, and the extended call list. In the function body, if other functions are called, the function parameters are defined for the called function and the function body is generated.

4. A method for generating C program test cases based on Csmith random generator as claimed in claim 3, characterized in that: In step S4, the filter performs a security check on the generated program fragment, specifically including: obtaining the program fragment to be checked; the filter judges the legality of the currently generated program fragment. If it is judged that the program fragment will cause a permanent true loop or the loop data corresponding to the program fragment exceeds the data threshold, the filter returns a program error, the generator abandons the program fragment and rolls back to the sequence point corresponding to the last checked program fragment. If it is judged that there is no problem with the program fragment, the subsequent program fragment to be checked will continue to be obtained.

5. A method for generating C program test cases based on Csmith random generator according to claim 1, characterized in that: Step S5 specifically includes: the generator outputs structures, joint declarations, global variables, forward declarations, other functions, and summary information in sequence; when the top-level function is completely generated, the generator outputs a main function; the main function calls the top-level generated function, calculates the checksum of non-pointer global variables, and prints the checksum.

6. A method for generating C program test cases based on the Csmith random generator according to claim 4, characterized in that: If an unimplemented function is found in the program generated by the filter check, the generator generates a function body for the function.

7. A device for generating C program test cases based on the Csmith random generator, characterized by: include A parameter acquisition module is used to obtain a preset probability information table and obtain preset parameters through the preset probability information table; A generator is used to initialize according to preset parameters, generate a test case program including a main function, variable types, and other functions based on the initialization information, and output auxiliary information of the test case program when generating the main function; Filter, performs security checks on the generated program fragments; A generator, used to organize and output the generated test case program after the filter has checked all program fragments, calculate the checksum of non-pointer global variables based on the generated test case program, and print the checksum; The parameter acquisition module includes: obtaining all attribute values ​​of the probability information table; marking the attribute values ​​that can efficiently generate test cases according to historical data; performing cluster analysis on the marked attribute values ​​to obtain a preset probability information table; judging whether the input user parameters are received, if it is judged that the user parameters are not received, the attribute values ​​in the preset probability information table are used as preset parameters, if it is judged that the user parameters are received, the user parameters are compared with the preset probability information table; if the user parameters include all the attribute values ​​in the preset probability information table, the user parameters are used as the preset parameters, if the user parameters do not include all the attribute values ​​in the preset probability information table, the input parameter error is output.

8. An electronic device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein: When the processor executes the computer program, the steps of the method according to any one of claims 1 to 5 are implemented.

9. A computer-readable storage medium having a computer program stored thereon, characterized in that: When the computer program is executed by a processor, the steps of the method according to any one of claims 1 to 5 are implemented.

Citation Information

Patent Citations

  • Frame-based automatic generating method for Web service test data

    CN102710460A

  • Lightweight parallel test case generation method for heterogeneous many-core processor

    CN114218063A