|
| 1 | +/** |
| 2 | + * Copyright (c) 2026, Timothy Stack |
| 3 | + * |
| 4 | + * All rights reserved. |
| 5 | + * |
| 6 | + * Redistribution and use in source and binary forms, with or without |
| 7 | + * modification, are permitted provided that the following conditions are met: |
| 8 | + * |
| 9 | + * * Redistributions of source code must retain the above copyright notice, this |
| 10 | + * list of conditions and the following disclaimer. |
| 11 | + * * Redistributions in binary form must reproduce the above copyright notice, |
| 12 | + * this list of conditions and the following disclaimer in the documentation |
| 13 | + * and/or other materials provided with the distribution. |
| 14 | + * * Neither the name of Timothy Stack nor the names of its contributors |
| 15 | + * may be used to endorse or promote products derived from this software |
| 16 | + * without specific prior written permission. |
| 17 | + * |
| 18 | + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ''AS IS'' AND ANY |
| 19 | + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 20 | + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 21 | + * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY |
| 22 | + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 23 | + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 24 | + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 25 | + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 26 | + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 27 | + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | + */ |
| 29 | + |
| 30 | +#include "sparse_cursor_container.hh" |
| 31 | + |
| 32 | +#include "doctest/doctest.h" |
| 33 | + |
| 34 | +TEST_CASE("sparse_cursor_container-basic") |
| 35 | +{ |
| 36 | + constexpr size_t NCOLS = 3; |
| 37 | + constexpr size_t NROWS = 500; |
| 38 | + |
| 39 | + lnav::cell_container cells; |
| 40 | + lnav::sparse_cursor_container<16> idx; |
| 41 | + idx.set_columns(NCOLS); |
| 42 | + |
| 43 | + for (size_t r = 0; r < NROWS; ++r) { |
| 44 | + idx.push_row(cells.end_cursor()); |
| 45 | + cells.push_int_cell(static_cast<int64_t>(r * 100 + 0)); |
| 46 | + cells.push_int_cell(static_cast<int64_t>(r * 100 + 1)); |
| 47 | + cells.push_int_cell(static_cast<int64_t>(r * 100 + 2)); |
| 48 | + } |
| 49 | + |
| 50 | + CHECK(idx.size() == NROWS); |
| 51 | + // One block cursor per 16 rows, rounded up. |
| 52 | + CHECK(idx.block_count() == (NROWS + 15) / 16); |
| 53 | + |
| 54 | + // Spot-check every 37th row to cover aligned/unaligned positions. |
| 55 | + for (size_t r = 0; r < NROWS; r += 37) { |
| 56 | + auto c = idx.at(r); |
| 57 | + CHECK(c.get_type() == lnav::cell_type::CT_INTEGER); |
| 58 | + CHECK(c.get_int() == static_cast<int64_t>(r * 100 + 0)); |
| 59 | + |
| 60 | + auto c1 = c.next(); |
| 61 | + REQUIRE(c1.has_value()); |
| 62 | + CHECK(c1->get_int() == static_cast<int64_t>(r * 100 + 1)); |
| 63 | + |
| 64 | + auto c2 = c1->next(); |
| 65 | + REQUIRE(c2.has_value()); |
| 66 | + CHECK(c2->get_int() == static_cast<int64_t>(r * 100 + 2)); |
| 67 | + } |
| 68 | + |
| 69 | + // First and last. |
| 70 | + CHECK(idx.front().get_int() == 0); |
| 71 | + CHECK(idx.back().get_int() |
| 72 | + == static_cast<int64_t>((NROWS - 1) * 100 + 0)); |
| 73 | +} |
| 74 | + |
| 75 | +TEST_CASE("sparse_cursor_container-empty") |
| 76 | +{ |
| 77 | + lnav::sparse_cursor_container<> idx; |
| 78 | + CHECK(idx.empty()); |
| 79 | + CHECK(idx.size() == 0); |
| 80 | + CHECK(idx.block_count() == 0); |
| 81 | +} |
| 82 | + |
| 83 | +TEST_CASE("sparse_cursor_container-single-row") |
| 84 | +{ |
| 85 | + lnav::cell_container cells; |
| 86 | + lnav::sparse_cursor_container<16> idx; |
| 87 | + idx.set_columns(2); |
| 88 | + |
| 89 | + idx.push_row(cells.end_cursor()); |
| 90 | + cells.push_text_cell(string_fragment::from_const("hello")); |
| 91 | + cells.push_int_cell(42); |
| 92 | + |
| 93 | + CHECK(idx.size() == 1); |
| 94 | + CHECK(idx.block_count() == 1); |
| 95 | + |
| 96 | + auto c = idx.at(0); |
| 97 | + CHECK(c.get_type() == lnav::cell_type::CT_TEXT); |
| 98 | + CHECK(c.get_text() == "hello"); |
| 99 | +} |
| 100 | + |
| 101 | +TEST_CASE("sparse_cursor_container-spans-chunk-boundaries") |
| 102 | +{ |
| 103 | + // A `cell_container` chunk is ~32 KiB; each int cell is 9 bytes, |
| 104 | + // so two cells per row is 18 bytes/row. Pushing 10000 rows forces |
| 105 | + // several new-chunk allocations, which is the case where a block- |
| 106 | + // start cursor captured by `end_cursor()` ends up pinned to a |
| 107 | + // chunk whose capacity was then exhausted — the cursor's |
| 108 | + // `c_offset` equals the old chunk's `cc_size`, and a subsequent |
| 109 | + // `next()` call reads past valid data unless `at()` syncs first. |
| 110 | + constexpr size_t NCOLS = 2; |
| 111 | + constexpr size_t NROWS = 10000; |
| 112 | + |
| 113 | + lnav::cell_container cells; |
| 114 | + lnav::sparse_cursor_container<64> idx; |
| 115 | + idx.set_columns(NCOLS); |
| 116 | + |
| 117 | + for (size_t r = 0; r < NROWS; ++r) { |
| 118 | + idx.push_row(cells.end_cursor()); |
| 119 | + cells.push_int_cell(static_cast<int64_t>(r)); |
| 120 | + cells.push_int_cell(static_cast<int64_t>(r + 1000000)); |
| 121 | + } |
| 122 | + |
| 123 | + CHECK(idx.size() == NROWS); |
| 124 | + // Every row's first and second cell should read back its seeded |
| 125 | + // value, no matter where in the chunk chain it sits. |
| 126 | + for (size_t r = 0; r < NROWS; ++r) { |
| 127 | + auto c0 = idx.at(r); |
| 128 | + CHECK_MESSAGE(c0.get_type() == lnav::cell_type::CT_INTEGER, |
| 129 | + "row ", r); |
| 130 | + CHECK_MESSAGE(c0.get_int() == static_cast<int64_t>(r), |
| 131 | + "row ", r); |
| 132 | + |
| 133 | + auto c1 = c0.next(); |
| 134 | + REQUIRE_MESSAGE(c1.has_value(), "row ", r); |
| 135 | + CHECK_MESSAGE(c1->get_int() == static_cast<int64_t>(r + 1000000), |
| 136 | + "row ", r); |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +TEST_CASE("sparse_cursor_container-clear") |
| 141 | +{ |
| 142 | + lnav::cell_container cells; |
| 143 | + lnav::sparse_cursor_container<16> idx; |
| 144 | + idx.set_columns(1); |
| 145 | + |
| 146 | + for (int i = 0; i < 50; ++i) { |
| 147 | + idx.push_row(cells.end_cursor()); |
| 148 | + cells.push_int_cell(i); |
| 149 | + } |
| 150 | + CHECK(idx.size() == 50); |
| 151 | + |
| 152 | + idx.clear(); |
| 153 | + cells.reset(); |
| 154 | + |
| 155 | + CHECK(idx.empty()); |
| 156 | + CHECK(idx.block_count() == 0); |
| 157 | + |
| 158 | + // Still usable after clear. |
| 159 | + idx.push_row(cells.end_cursor()); |
| 160 | + cells.push_int_cell(999); |
| 161 | + CHECK(idx.size() == 1); |
| 162 | + CHECK(idx.at(0).get_int() == 999); |
| 163 | +} |
0 commit comments