Aussie AI
Function Call Counting
-
Book Excerpt from "Generative AI in C++"
-
by David Spuler, Ph.D.
Function Call Counting
The idea of once-only code assertions can be generalized to a count. For example, if you want to ensure a function isn't called too many times, use this code:
yassert_N_times(1000);
Here's the macro, similar to yassert_once
, but with a parameter:
#define yassert_N_times(ntimes) do { \ static int s_count = 0; \ ++s_count; \ if (s_count > (ntimes)) { \ aussie_yassert_fail( \ "Code executed more than " #ntimes " times", \ __FILE__, __LINE__); \ } \ } while(0)
This checks for too many invocations of the code block.
Checking for “too few” is a little trickier,
and would need a static
smart counter object with a destructor.
• Next: • Up: Table of Contents |
The new AI programming book by Aussie AI co-founders:
Get your copy from Amazon: Generative AI in C++ |