Boundary condition fix

If the size was exactly BUFFER_SIZE, then we were susceptible of
suffering a buffer overrun. This commit fixes that and introduces
unit tests to make sure this never happens.