Advertisement
Gistrec

Code coverage macro

Oct 21st, 2019
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.02 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. struct CoverageNode
  4. {
  5.     CoverageNode(const int in_line)
  6.         : line(in_line)
  7.     {
  8.         if (first)
  9.         {
  10.             next = first;
  11.         }
  12.         first = this;
  13.     }
  14.  
  15.     int line;
  16.     bool covered;
  17.     CoverageNode *next;
  18.  
  19.     static CoverageNode *first;
  20. };
  21.  
  22. CoverageNode *CoverageNode::first;
  23.  
  24. template<class Function>
  25. void for_each_coverage(const Function &function)
  26. {
  27.     for (const auto *node = CoverageNode::first; node; node = node->next)
  28.     {
  29.         function(*node);
  30.     }
  31. }
  32.  
  33. template<int line>
  34. struct LineCoverage
  35. {
  36.     static CoverageNode node;
  37. };
  38.  
  39. template<int line>
  40. CoverageNode LineCoverage<line>::node = CoverageNode(line);
  41.  
  42. #define BRANCH() LineCoverage<__LINE__>::node.covered = true
  43.  
  44. void foo(const bool b)
  45. {
  46.     if (b)
  47.         BRANCH();
  48.     else
  49.         BRANCH();
  50. }
  51.  
  52. int main()
  53. {
  54.     foo(false);
  55.  
  56.     for_each_coverage([] (const CoverageNode &node) {
  57.         std::cout << node.line << " " << node.covered << std::endl;
  58.     });
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement