Bidirectional Transducer Testing Devices

When writing unit tests for a bidirectional converter, I was wondering if it was enough to cover only one direction with the unit test. Suppose a converter that converts both from a -> band 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 check both directions of conversion. However, it is very easy to write type tests f(f(a)) == a. Suppose that it is f(a) == bcovered by the unit test symbol. Which of these tests will 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 we need to check

(4) f(b) == a
+4
source share
3 answers

:

f(a) == b, :

  • f(f(a)) == a f(b) == a .

  • f(f(b)) == b ! , , f(b) == a. f(f(b)) == b, , f(b) == b!

+2

, convertToB(null);
assertEquals(bObject, convertToB(convertToA(bObject)));, , .

, .

f(a) = b
f(b) = a
f(null) = ?
f(f(b)) = b
f(f(a)) = a

( , A B) (, foo, , foo, null,...).
.

+1

Yes, you need to also check f (a) == b.

Converter implementation example:

Int convertToB(Int a) { return -a }
Int convertToA(Int b) { return -a }

tests f (f (a)) == a will pass. Although this obviously does not work correctly;)

0
source

Source: https://habr.com/ru/post/1625962/


All Articles