Add unit tests for selection index mapping.

Add unit tests for parts of indexutil.* that are mainly used by the
selection position calculation engine.

Add required functionality to toputils.* and update typedefs.c to
support more flexible freeing of required data structures.

Part of #651, related to #1221.

Change-Id: Ibc68ad71a4834b991820014969be46152426a9f5