[GO] Write a table-driven test in C

Introduction

I noticed that the table-driven test recommended by Go can be written in C as well.

Go table-driven test

Table-driven test is recommended for Go testing. The pair of data input and expected value given to the test target is treated as one test case, and the list of expected value and measured value is shown in the table.

For example, to write a test for the gcd () method in a table-driven manner:

gcd_test.go


func TestGcd(t *testing.T) {
	cases := []struct {
		a, b int
		z    int
	}{
		{12, 8, 4},
		{3, 7, 1},
	}

	for _, c := range cases {
		if gcd(c.a, c.b) != c.z {
			t.Errorf("expected gcd(%v,%v) == %v\n", c.a, c.b, c.z)
		}
	}

}

Go can write struct definitions and variable initializations at the same time. Test case initialization is easy and type-safe with the struct literal syntax{}. The code is very easy to read as you can focus on the test data only.

C table-driven test

I also wrote a test of gcd () in C in table-driven. Even in C, structure declaration and variable initialization can be performed at the same time. And initialization can be type-safe with struct literal syntax{}.

gcd_test.c


#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))

int test_gcd() {
	struct {
		int a, b;
		int z;
	} cases[] = {
		{12, 8, 4},
		{3, 7, 1},
	};

	for (int i = 0; i < ARRAY_SIZE(cases); ++i) {
		if (gcd(cases[i].a, cases[i].b) != cases[i].z) {
			printf("expected gcd(%d,%d) == %d\n", cases[i].a, cases[i].b, cases[i].z);
		}
	}
}

Compared to Go, there was no big difference in the amount of description and readability, and I was able to write a table-driven test in C language.

in conclusion

Go is often referred to as a simple C, but it's also a good idea to import Go culture into C.

Recommended Posts

Write a table-driven test in C
Write the test in a python docstring
I want to write in Python! (2) Let's write a test
Write a binary search in Python
Multi-instance module test in C language
ABC166 in Python A ~ C problem
Write A * (A-star) algorithm in Python
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
To write a test in Go, first design the interface
Solve ABC037 A ~ C in Python
Write C unit tests in Python
Solve ABC175 A, B, C in Python
How to use Google Test in C
Write a short property definition in Python
Write O_SYNC file in C and Python
Write a Caesar cipher program in Python
Write a simple greedy algorithm in Python
Write a simple Vim Plugin in Python 3
Create a Vim + Python test environment in 1 minute
Set up a test SMTP server in Python.
Set up a UDP server in C language
Call a Python script from Embedded Python in C ++ / C ++
I tried adding a Python3 module in C
Write Kikagaku-style algorithm theory in Go Primality test
How to write a named tuple document in 2020
Feel free to write a test with nose (in the case of + gevent)
Write Pulumi in Go
Handle signals in C
Try to make a Python module in C language
Write a super simple molecular dynamics program in python
Write DCGAN in Keras
Write decorator in class
I tried using google test and CMake in C
Access MongoDB in C
Try embedding Python in a C ++ program with pybind11
Write Python in MySQL
Next Python in C
dict in dict Makes a dict a dict
Write a log-scale histogram on the x-axis in python
Write code to Unit Test a Python web app
Solve Atcoder ABC176 (A, B, C, E) in Python
C API in Python 3
It's hard to write a very simple algorithm in php
Write a co-author network in a specific field using arxiv information
How to write a test for processing that uses BigQuery
Extend python in C ++ (Boost.NumPy)
Machine language embedding in C language
Take a screenshot in Python
Write Pandoc filters in Python
Heapsort made in C language
Create a function in Python
Create a dictionary in Python
Write standard input in code
Write beta distribution in Python
Algorithm in Python (primality test)
Write a kernel density function
Write python in Rstudio (reticulate)