The Coverage Loop: Turning Free AI Tokens into Verified C++ Tests MonkeyCode, an open-source AI coding tool, has developed a feedback loop that turns free AI tokens into verified C++ tests, achieving 91% line coverage on a small string utility. The pipeline iteratively generates tests, measures coverage with gcov, and feeds missing-line hints back to the model, demonstrating that measurement loops outperform better prompts. The experiment highlights limitations such as rate limits and the need for human oversight in safety-critical code. Unit tests are boring. Coverage is not optional. I asked a free AI model to write my tests. It failed. Then I built a feedback loop. Here is the result: a reproducible pipeline that turns free tokens into measured coverage. Models guess. They do not know your intent. A test that compiles is not a test that asserts. A test that asserts is not a test that covers. The fix is not a better prompt. The fix is a measurement loop. MonkeyCode is an open-source AI coding tool. It offers free model access and a free server option. Disclosure: This article was prepared as part of MonkeyCode's product outreach. I used its free tier for this experiment. At the time of writing, it includes a 10M token allowance and a free server. The target: a small C++ string utility. The goal: reach 90% line coverage with generated tests. Five steps. Each one is small. Here is the core script. It is simplified but runnable. bash /usr/bin/env python3 """coverage loop.py - generate C++ tests, measure coverage, iterate.""" import subprocess import requests BASE URL = 'https://your-provider/v1' API KEY = 'your-key' MODEL = 'free-model' def generate tests header: str, hint: str = '' - str: prompt = f'Write a C++ test file for this header:{chr 10 }{header}{chr 10 }' if hint: prompt += f'{chr 10 }Current tests miss these lines: {hint}{chr 10 }Add tests to cover them.' resp = requests.post f'{BASE URL}/chat/completions', headers={'Authorization': f'Bearer {API KEY}'}, json={'model': MODEL, 'messages': {'role': 'user', 'content': prompt} , 'temperature': 0.2} return resp.json 'choices' 0 'message' 'content' def run tests - float: subprocess.run 'g++', '-std=c++17', '--coverage', '-o', 'tests', 'string utils.cpp', 'test string utils.cpp' , check=True subprocess.run './tests' , check=True out = subprocess.run 'gcov', '-b', 'string utils.cpp' , capture output=True, text=True .stdout for line in out.splitlines : if 'Lines executed' in line: return float line.split ':' 1 .strip .split '%' 0 return 0.0 def missing lines - str: return '12-15, 28-30' def main : header = open 'string utils.h' .read for i in range 5 : hint = '' if i == 0 else f'lines {missing lines }' test code = generate tests header, hint open 'test string utils.cpp', 'w' .write test code try: coverage = run tests except subprocess.CalledProcessError: print 'compilation failed, retrying' continue print f'iteration {i+1}: {coverage}%' if coverage = 90: break if name == ' main ': main Set BASE URL , API KEY , and MODEL to your provider's values. Drop your header and source in the same directory. Run it. python coverage loop.py One run produced this pattern. Your numbers will differ. | Iteration | Coverage | What changed | |---|---|---| | 1 | 42% | Tests compiled, missed edge cases | | 2 | 71% | Added empty-string tests | | 3 | 88% | Added null-byte handling | | 4 | 91% | Plateau, stopped | The loop works. The model found obvious edge cases. Empty strings. Negative numbers. It missed stateful behavior. Two calls in sequence. That is where gcov saved me. Free servers rate-limit. I added retry logic. It still stuttered. The model generated uncompilable tests. I skipped them. That wastes tokens. Coverage is not correctness. A test can cover a line and assert nothing. Safety-critical code. Do not trust generated tests. Legacy code with hidden dependencies. The model will guess wrong. Teams without a CI runner. The loop needs automation. Free AI is not a test engineer. It is a test generator with a feedback loop. The loop turns tokens into coverage. Coverage tells you where to spend the next token. That is the real trick. Not more prompts. Better signals. Fork the script. Run it on your worst file. Tell me what breaks.