Write C unit tests in Python

How to write a unit test for C language test using Python unit test. The motivation is to reduce the number of compilations as much as possible. This method eliminates the need for compilation when writing tests (although I don't know if it's speedy or if the return value is complex).

First, the C language code is as follows.

//main.c
#include<stdio.h>
int add(int a, int b);
struct Pair {
  int l;
  int r;
};
int
main(void) {
  printf("%d + %d = %d\n", 3, 4, add(3, 4));
  return (0);
}
int add(int a, int b) {
  return a + b;
}
struct Pair  make_pair(int l, int r) {
  struct Pair p;
  p.l = l;
  p.r = r;
  return p;
}

Then the python code to call from gdb looks like this:

#debug.py
import unittest

class Test(unittest.TestCase):
  def assertEqual_(self, a, expr):
    self.assertEqual(a, gdb.parse_and_eval(expr))
  def test_aaaa(self):
    self.assertEqual_(42, 'add(30, 12)')
  def test_bbb(self):
    self.assertEqual_(0, 'add(0, 0)')
  def test_ccc(self):
    p = gdb.parse_and_eval("make_pair(3, 4)")
    self.assertEqual("{l = 3, r = 4}", str(p))


gdb.execute('break main')
gdb.execute('run')
unittest.main()

To test C code with this python code:

$ gcc -g3 main.c
$ gdb -q -x ./debug.py a.out

The standard output looks like this: Now you can add and modify tests written in C in Python without recompiling.

Reading symbols from a.out...done.
Breakpoint 1 at 0x804841c: file main.c, line 9.

Breakpoint 1, main () at main.c:9
9	  printf("%d + %d = %d\n", 3, 4, add(3, 4));
...
----------------------------------------------------------------------
Ran 3 tests in 0.003s

OK

As I wrote at the beginning, if the return value of the function is complicated, I think that it is possible to handle the structure better. In C language, it may be realistic to prepare a function that converts the contents of an object into a character string.

About version

Recommended Posts

Write C unit tests in Python
Python unit tests
Write O_SYNC file in C and Python
Write Python in MySQL
Next Python in C
C API in Python 3
Extend python in C ++ (Boost.NumPy)
Write Pandoc filters in Python
Write beta distribution in Python
Binary search in Python / C ++
Write Spider tests in Scrapy
Write tests in Python to profile and check coverage
Write a binary search in Python
Write a table-driven test in C
Write JSON Schema in Python DSL
Write an HTTP / 2 server in Python
Write AWS Lambda function in Python
ABC166 in Python A ~ C problem
Write selenium test code in python
Solve ABC036 A ~ C in Python
Write a pie chart in Python
Write a vim plugin in Python
Write a depth-first search in Python
How to wrap C in Python
Write tests in GO language + gin
Code tests around time in Python
Write documentation in Sphinx with Python Livereload
Solve ABC175 A, B, C in Python
Pharmaceutical company researchers summarized Python unit tests
Write the test in a python docstring
Algorithm in Python (ABC 146 C Binary Search
Implement FIR filters in Python and C
Write a short property definition in Python
Write a Caesar cipher program in Python
Read and write JSON files in Python
Write a simple greedy algorithm in Python
Write a simple Vim Plugin in Python 3
Generate C language from S-expressions in Python
How to write Ruby to_s in Python
Run Python in C ++ on Visual Studio 2017
Quadtree in Python --2
Python in optimization
CURL in python
Metaprogramming in Python
Python 3.3 in Anaconda
Geocoding in python
SendKeys in Python
Meta-analysis in Python
Unittest in python
Epoch in Python
Discord in Python
Sudoku in Python
DCI in Python
quicksort in python
N-Gram in Python
Programming in python
Plink in Python
Constant in python
python, openFrameworks (c ++)
Lifegame in Python.
FizzBuzz in Python