Verified Commit 56947cea authored by Alberto Miranda's avatar Alberto Miranda ♨️
Browse files

Add tests for chnk_lpad()

parent e0383aea
Loading
Loading
Loading
Loading
+83 −0
Original line number Diff line number Diff line
@@ -201,3 +201,86 @@ SCENARIO(" offsets can be right-aligned to block size boundaries ",
        }
    }
}

SCENARIO(" overrun distance can be computed correctly ",
         "[utils][numeric][chnk_lpad]") {

    GIVEN(" a block size ") {

        const std::size_t block_size = GENERATE(filter(
                [](uint64_t bs) { return is_power_of_2(bs); }, range(0, 100000)));

        WHEN(" offset is smaller than block_size ") {

            AND_WHEN(" offset equals 0 ") {

                const uint64_t offset = 0;

                CAPTURE(offset, block_size);

                THEN(" the computed overrun distance equals 0 ") {
                    const uint64_t overrun = chnk_lpad(offset, block_size);
                    const uint64_t expected_overrun = 0;
                    REQUIRE(overrun == expected_overrun);
                }
            }

            AND_WHEN(" 0 < offset < block_size ") {

                const uint64_t offset = GENERATE_COPY(take(
                        test_reps, random(std::size_t{0}, block_size - 1)));

                CAPTURE(offset, block_size);

                THEN(" the computed overrun distance equals offset ") {
                    const uint64_t overrun = chnk_lpad(offset, block_size);
                    const uint64_t expected_overrun = offset;
                    REQUIRE(overrun == expected_overrun);
                }
            }

            AND_WHEN(" offset equals block_size - 1 ") {

                const uint64_t offset = block_size - 1;

                CAPTURE(offset, block_size);

                THEN(" the computed overrun distance equals block_size - 1 ") {
                    const uint64_t overrun = chnk_lpad(offset, block_size);
                    const uint64_t expected_overrun = block_size - 1;
                    REQUIRE(overrun == expected_overrun);
                }
            }
        }

        WHEN(" offset equals block_size ") {

            const uint64_t offset = block_size;

            CAPTURE(offset, block_size);

            THEN(" the computed overrun distance equals 0 ") {
                const uint64_t overrun = chnk_lpad(offset, block_size);
                const uint64_t expected_overrun = 0;
                REQUIRE(overrun == expected_overrun);
            }
        }

        WHEN(" offset is larger than block_size ") {

            const uint64_t offset = GENERATE_COPY(
                    take(test_reps, random(block_size, block_size * 31)));

            CAPTURE(offset, block_size);

            THEN(" the computed overrun distance equals the difference between "
                 "offset and its closest block's left boundary ") {
                const uint64_t overrun = chnk_lpad(offset, block_size);
                const uint64_t expected_overrun =
                        offset -
                        static_cast<uint64_t>(offset / block_size) * block_size;
                REQUIRE(overrun == expected_overrun);
            }
        }
    }
}