mardi 26 janvier 2016

Unit testing bidirectional converters

While writing unit tests for a bidirectional converter, I was wondering whether it would be enough to cover only one direction with a unit test. Assume a converter that converts both from a -> b and from b -> a:

class Converter<A, B> {
    B convertToB(A a) { /* ... */ }
    A convertToA(B b) { /* ... */ }
}

Or more formally:

f(a) = b
f(b) = a

Standard unit tests would have to test both conversion directions. It's however very easy to write tests like f(f(a)) == a. Let's assume, f(a) == b is covered by a unit test. Which of these tests would be necessary and sufficient to cover f(b) == a?

(1) f(f(a)) == a
(2) f(f(b)) == b
(3) f(f(a)) == a && f(f(b)) == b

or do we have to test

(4) f(b) == a

Aucun commentaire:

Enregistrer un commentaire