fix($mdUtil): fix bugs in iterator's `next()`/`previous()` methods and add tests for relooping

The first commit adds tests (mainly for "relooping" functionality), some of which fail because of iterator implementation bugs.
The second commit fixes those bugs (and slightly refactors `next()`/`previous()` for DRY-ness).

Related to ff79c91