Fix range serialization

closes #146 and copies tests from: apache/incubator-spark#49