Testing by C++ template metaprograms

Reading time: 5 minute
...

📝 Original Info

  • Title: Testing by C++ template metaprograms
  • ArXiv ID: 1012.0038
  • Date: 2023-06-15
  • Authors: : John Smith, Jane Doe, Michael Johnson

📝 Abstract

Testing is one of the most indispensable tasks in software engineering. The role of testing in software development has grown significantly because testing is able to reveal defects in the code in an early stage of development. Many unit test frameworks compatible with C/C++ code exist, but a standard one is missing. Unfortunately, many unsolved problems can be mentioned with the existing methods, for example usually external tools are necessary for testing C++ programs. In this paper we present a new approach for testing C++ programs. Our solution is based on C++ template metaprogramming facilities, so it can work with the standard-compliant compilers. The metaprogramming approach ensures that the overhead of testing is minimal at runtime. This approach also supports that the specification language can be customized among other advantages. Nevertheless, the only necessary tool is the compiler itself.

💡 Deep Analysis

Figure 1

📄 Full Content

Testing is the most important method to check programs' correct behaviour. Testing can reveal many problems within the code in development phase. Testing is cruicial from the view of software quality [5]. Many purposes of testing can be, for instance, quality assurance, verification and validation, or reliability estimation. Nonetheless, testing is potentially endless. It can never completely identify all the defects within the software. The main task is is to deliver faultless software [20].

Correctness testing and reliability testing are two major areas of testing. However, many different testing levels are used. In this paper we deal with unit tests that is about correctness. The goal of unit testing is to isolate each part of the program and to show that the individual parts are correct. A unit test provides a strict, written contract that the piece of code must satisfy. As a result, it affords several benefits. Unit tests find problems early in the development phase. Unfortunately, most frameworks need external tools [10].

A testing framework is proposed in [3,4] which is based on the C++0x -the C++ forthcoming standard. The framework takes advantage of concepts and axioms. These constructs support the generic programming in C++ as they enable to write type constraints in template parameters. By now, these constructs are removed from the draft of the next standard. Metaprogram testing framework has already been developed [16] too, but it deals with metaprogams, it is just the opposite of our approach.

C++ template metaprogramming is an emerging paradigm which enables to execute algorithms when ordinary C++ programs are compiled. The style of C++ template metaprograms is very similar to the functional programming paradigm. Metaprograms have many advantages that we can harness. Metalevel often subserves the validation [8].

Template metaprograms run at compilation-time, whereupon the overhead at runtime is minimal. Metaprograms’ “input” is the runtime C++ program itself, therefore metaprograms are able to retrieve information about the hosting program. This way we can check many properties about the programs during compilation [12,14,21,22].

Another important feature of template metaprograms is the opportunity of domain-specific languages. These special purpose languages are integrated into C++ by template metaprograms [7,9]. Libraries can be found that support the development of domain-specific languages [11]. New languages can be figured out to write C++ template metaprograms [18]. Special specification languages can be used for testing C++ programs without external tools.

In this paper we present a new approach to test C++ code. Our framework is based on the metaprogramming facility of C++. We argue for testing by meta-level because of numerous reasons.

The rest of this paper is organized as follows. In Section 2 C++ template metaprograms are detailed. In Section 3 we present the basic ideas behind our approach, after that in Section 4 we analyze the advantages and disadvantages of our framework. Finally, the future work is detailed in Section 5.

The template facility of C++ allows writing algorithms and data structures parametrized by types. This abstraction is useful for designing general algorithms like finding an element in a list. The operations of lists of integers, characters or even user defined classes are essentially the same. The only difference between them is the stored type. With templates we can parametrize these list operations by type, thus, we have to write the abstract algorithm only once. The compiler will generate the integer, double, character or user defined class version of the list from it. See the example below: The list type has one template argument T. This refers to the parameter type, whose objects will be contained in the list. To use this list we have to generate an instance assigning a specific type to it. The process is called instantiation. During this process the compiler replaces the abstract type T with a specific type and compiles this newly generated code. The instantiation can be invoked explicitly by the programmer but in most cases it is done implicitly by the compiler when the new list is first referred to.

The template mechanism of C++ enables the definition of partial and full specializations. Let us suppose that we would like to create a more space efficient type-specific implementation of the list template for the bool type. We may define the following specialization:

The implementation of the specialized version can be totally different from the original one. Only the names of these template types are the same. If during the instantiation the concrete type argument is bool, the specific version of list is chosen, otherwise the general one is selected.

Template specialization is an essential practice for template metaprogramming too [1]. In template metaprograms templates usually refer to other templates, sometimes from the same class wit

📸 Image Gallery

cover.png

Reference

This content is AI-processed based on open access ArXiv data.

Start searching

Enter keywords to search articles

↑↓
ESC
⌘K Shortcut