Skip to content

CPP Tips and tricks

vivek kushwaha edited this page Aug 27, 2022 · 4 revisions

Table of Contents

C++ Tips

Pre-processors and macro

Assert definition

inline void _assert(const char* expression, const char* file, int line)
{
fprintf(stderr, "Assertion '%s' failed, file '%s' line '%d'.", expression, file, line);
abort();
}
 
#ifdef NDEBUG
#define assert(EXPRESSION) ((void)0)
#else
#define assert(EXPRESSION) ((EXPRESSION) ? (void)0 : _assert(#EXPRESSION, __FILE__, __LINE__))
#endif

Variadic Macros

#define eprintf(…) fprintf (stderr, __VA_ARGS__)

Code Coverage and Unit Testing

How to enable code coverage

To enable the coverage you must compile your all files (individual source file) with -fprofile-arcs and -ftest-coverage.

Description of data files

gcov uses two files for profiling. The names of these files are derived from the original object file by substituting the file suffix with either .gcno, or .gcda. The files contain coverage and profile data stored in a platform-independent format. The .gcno files are placed in the same directory as the object file. By default, the .gcda files are also stored in the same directory as the object file, but the GCC -fprofile-dir option may be used to store the .gcda files in a separate directory.

The .gcno notes file is generated when the source file is compiled with the GCC -ftest-coverage option. It contains information to reconstruct the basic block graphs and assign source line numbers to blocks.

The .gcda count data file is generated when a program containing object files built with the GCC -fprofile-arcs option is executed. A separate .gcda file is created for each object file compiled with this option. It contains arc transition counts, value profile counts, and some summary information.

The full details of the file format is specified in gcov-io.h, and functions provided in that header file should be used to access the coverage files.

More documentation on gcov

Unicode programming

Best resources for unicode

  1. https://betterexplained.com/articles/unicode/

Clone this wiki locally