[r] optimize tests and codes