
#include <test.hpp>

TEST_CASE("read repeated packed double field") {

    // Run these tests twice, the second time we basically move the data
    // one byte down in the buffer. It doesn't matter how the data or buffer
    // is aligned before that, in at least one of these cases the doubles will
    // not be aligned properly. So we test that even in that case the doubles
    // will be extracted properly.

    for (std::string::size_type n = 0; n < 2; ++n) {

        std::string abuffer;
        abuffer.reserve(1000);
        abuffer.append(n, '\0');

        SECTION("empty") {
            abuffer.append(load_data("repeated_packed_double/data-empty"));
            protozero::pbf_reader item(abuffer.data() + n, abuffer.size() - n);

            REQUIRE(!item.next());
        }

        SECTION("one") {
            abuffer.append(load_data("repeated_packed_double/data-one"));
            protozero::pbf_reader item(abuffer.data() + n, abuffer.size() - n);

            REQUIRE(item.next());
            auto it_pair = item.get_packed_double();
            REQUIRE(!item.next());

            REQUIRE(*it_pair.first == 17.34);
            REQUIRE(++it_pair.first == it_pair.second);
        }

        SECTION("many") {
            abuffer.append(load_data("repeated_packed_double/data-many"));
            protozero::pbf_reader item(abuffer.data() + n, abuffer.size() - n);

            REQUIRE(item.next());
            auto it_pair = item.get_packed_double();
            REQUIRE(!item.next());

            auto it = it_pair.first;
            REQUIRE(*it++ == 17.34);
            REQUIRE(*it++ ==   0.0);
            REQUIRE(*it++ ==   1.0);
            REQUIRE(*it++ == std::numeric_limits<double>::min());
            REQUIRE(*it++ == std::numeric_limits<double>::max());
            REQUIRE(it == it_pair.second);
        }

        SECTION("end_of_buffer") {
            abuffer.append(load_data("repeated_packed_double/data-many"));

            for (std::string::size_type i = 1; i < abuffer.size() - n; ++i) {
                protozero::pbf_reader item(abuffer.data() + n, i);
                REQUIRE(item.next());
                REQUIRE_THROWS_AS(item.get_packed_double(), protozero::end_of_buffer_exception);
            }
        }

    }

}

TEST_CASE("write repeated packed double field") {

    std::string buffer;
    protozero::pbf_writer pw(buffer);

    SECTION("empty") {
        double data[] = { 17.34 };
        pw.add_packed_double(1, std::begin(data), std::begin(data) /* !!!! */);

        REQUIRE(buffer == load_data("repeated_packed_double/data-empty"));
    }

    SECTION("one") {
        double data[] = { 17.34 };
        pw.add_packed_double(1, std::begin(data), std::end(data));

        REQUIRE(buffer == load_data("repeated_packed_double/data-one"));
    }

    SECTION("many") {
        double data[] = { 17.34, 0.0, 1.0, std::numeric_limits<double>::min(), std::numeric_limits<double>::max() };
        pw.add_packed_double(1, std::begin(data), std::end(data));

        REQUIRE(buffer == load_data("repeated_packed_double/data-many"));
    }

}

