Add test cases for #4063.

I'm unsure whether they are correct or not (see my comment on #4063) but certainly it works.