|
| 1 | +// SPDX-License-Identifier: BSD-3-Clause |
| 2 | +/* Copyright 2020, Intel Corporation */ |
| 3 | + |
| 4 | +#include "../common/transaction_helpers.hpp" |
| 5 | +#include "../common/unittest.hpp" |
| 6 | + |
| 7 | +#include <libpmemobj++/container/string.hpp> |
| 8 | +#include <libpmemobj++/make_persistent.hpp> |
| 9 | +#include <libpmemobj++/pool.hpp> |
| 10 | +#include <libpmemobj++/slice.hpp> |
| 11 | +#include <libpmemobj++/transaction.hpp> |
| 12 | + |
| 13 | +#include <algorithm> |
| 14 | + |
| 15 | +namespace nvobj = pmem::obj; |
| 16 | + |
| 17 | +using S = pmem::obj::string; |
| 18 | + |
| 19 | +struct root { |
| 20 | + nvobj::persistent_ptr<S> s; |
| 21 | +}; |
| 22 | + |
| 23 | +void |
| 24 | +test_out_of_range_exception(pmem::obj::pool<root> &pop, bool use_sso_only) |
| 25 | +{ |
| 26 | + auto r = pop.root(); |
| 27 | + |
| 28 | + size_t bonus_size = use_sso_only ? 0 : 20; |
| 29 | + |
| 30 | + try { |
| 31 | + nvobj::transaction::run(pop, [&] { |
| 32 | + r->s = nvobj::make_persistent<S>(10U + bonus_size, 'a'); |
| 33 | + }); |
| 34 | + } catch (std::exception &e) { |
| 35 | + UT_FATALexc(e); |
| 36 | + } |
| 37 | + |
| 38 | + S &str = *(r->s); |
| 39 | + const S &const_str = *(r->s); |
| 40 | + |
| 41 | + try { |
| 42 | + nvobj::transaction::run(pop, [&] { |
| 43 | + auto slice = str.range( |
| 44 | + 0, 10 + bonus_size); /* should not throw */ |
| 45 | + (void)slice; |
| 46 | + }); |
| 47 | + } catch (std::exception &e) { |
| 48 | + UT_FATALexc(e); |
| 49 | + } |
| 50 | + |
| 51 | + try { |
| 52 | + nvobj::transaction::run(pop, [&] { |
| 53 | + auto slice = str.crange( |
| 54 | + 0, 10 + bonus_size); /* should not throw */ |
| 55 | + (void)slice; |
| 56 | + }); |
| 57 | + } catch (std::exception &e) { |
| 58 | + UT_FATALexc(e); |
| 59 | + } |
| 60 | + |
| 61 | + try { |
| 62 | + nvobj::transaction::run(pop, [&] { |
| 63 | + auto slice = const_str.range( |
| 64 | + 0, 10 + bonus_size); /* should not throw */ |
| 65 | + (void)slice; |
| 66 | + }); |
| 67 | + } catch (std::exception &e) { |
| 68 | + UT_FATALexc(e); |
| 69 | + } |
| 70 | + |
| 71 | + try { |
| 72 | + nvobj::transaction::run(pop, [&] { |
| 73 | + auto slice = str.range(0, 10 + bonus_size, |
| 74 | + 3); /* should not throw */ |
| 75 | + (void)slice; |
| 76 | + }); |
| 77 | + } catch (std::exception &e) { |
| 78 | + UT_FATALexc(e); |
| 79 | + } |
| 80 | + |
| 81 | + bool exception_thrown = false; |
| 82 | + try { |
| 83 | + nvobj::transaction::run(pop, [&] { |
| 84 | + auto slice = str.range( |
| 85 | + 0, 11 + bonus_size); /* should throw */ |
| 86 | + (void)slice; |
| 87 | + }); |
| 88 | + UT_ASSERT(0); |
| 89 | + } catch (std::out_of_range &) { |
| 90 | + exception_thrown = true; |
| 91 | + } catch (std::exception &e) { |
| 92 | + UT_FATALexc(e); |
| 93 | + } |
| 94 | + UT_ASSERT(exception_thrown); |
| 95 | + |
| 96 | + exception_thrown = false; |
| 97 | + try { |
| 98 | + nvobj::transaction::run(pop, [&] { |
| 99 | + auto slice = str.range(0, 11 + bonus_size, |
| 100 | + 3); /* should throw */ |
| 101 | + (void)slice; |
| 102 | + }); |
| 103 | + UT_ASSERT(0); |
| 104 | + } catch (std::out_of_range &) { |
| 105 | + exception_thrown = true; |
| 106 | + } catch (std::exception &e) { |
| 107 | + UT_FATALexc(e); |
| 108 | + } |
| 109 | + UT_ASSERT(exception_thrown); |
| 110 | + |
| 111 | + exception_thrown = false; |
| 112 | + try { |
| 113 | + nvobj::transaction::run(pop, [&] { |
| 114 | + auto slice = const_str.range( |
| 115 | + 0, 11 + bonus_size); /* should throw */ |
| 116 | + (void)slice; |
| 117 | + }); |
| 118 | + UT_ASSERT(0); |
| 119 | + } catch (std::out_of_range &) { |
| 120 | + exception_thrown = true; |
| 121 | + } catch (std::exception &e) { |
| 122 | + UT_FATALexc(e); |
| 123 | + } |
| 124 | + UT_ASSERT(exception_thrown); |
| 125 | + |
| 126 | + exception_thrown = false; |
| 127 | + try { |
| 128 | + nvobj::transaction::run(pop, [&] { |
| 129 | + auto slice = str.crange( |
| 130 | + 0, 11 + bonus_size); /* should throw */ |
| 131 | + (void)slice; |
| 132 | + }); |
| 133 | + UT_ASSERT(0); |
| 134 | + } catch (std::out_of_range &) { |
| 135 | + exception_thrown = true; |
| 136 | + } catch (std::exception &e) { |
| 137 | + UT_FATALexc(e); |
| 138 | + } |
| 139 | + UT_ASSERT(exception_thrown); |
| 140 | + |
| 141 | + nvobj::transaction::run(pop, |
| 142 | + [&] { nvobj::delete_persistent<S>(r->s); }); |
| 143 | +} |
| 144 | + |
| 145 | +void |
| 146 | +test_returned_values(pmem::obj::pool<root> &pop, bool use_sso_only) |
| 147 | +{ |
| 148 | + auto r = pop.root(); |
| 149 | + |
| 150 | + size_t bonus_size = use_sso_only ? 0 : 20; |
| 151 | + |
| 152 | + try { |
| 153 | + nvobj::transaction::run(pop, [&] { |
| 154 | + r->s = nvobj::make_persistent<S>(10U + bonus_size, 'a'); |
| 155 | + }); |
| 156 | + } catch (std::exception &e) { |
| 157 | + UT_FATALexc(e); |
| 158 | + } |
| 159 | + |
| 160 | + S &str = *(r->s); |
| 161 | + const S &const_str = *(r->s); |
| 162 | + |
| 163 | + assert_tx_abort(pop, [&] { |
| 164 | + nvobj::slice<S::pointer> slice = str.range(0, 3); |
| 165 | + UT_ASSERTeq(&str.front(), slice.begin()); |
| 166 | + UT_ASSERTeq(&str.front() + 3, slice.end()); |
| 167 | + |
| 168 | + size_t cnt = 0; |
| 169 | + for (auto &c : slice) { |
| 170 | + UT_ASSERTeq(c, 'a'); |
| 171 | + c = 'b'; |
| 172 | + UT_ASSERTeq(str[cnt++], 'b'); |
| 173 | + UT_ASSERTeq(std::count(slice.begin(), slice.end(), 'b'), |
| 174 | + static_cast<long>(cnt)); |
| 175 | + UT_ASSERTeq(str.size(), 10U + bonus_size); |
| 176 | + } |
| 177 | + }); |
| 178 | + UT_ASSERTeq(str.find('b'), str.npos); |
| 179 | + |
| 180 | + assert_tx_abort(pop, [&] { |
| 181 | + nvobj::slice<S::range_snapshotting_iterator> slice = |
| 182 | + str.range(2, 8 + bonus_size, 1); |
| 183 | + UT_ASSERTeq(&str.front() + 2, &*slice.begin()); |
| 184 | + UT_ASSERTeq(&str.front() + 10 + bonus_size, &*slice.end()); |
| 185 | + |
| 186 | + size_t cnt = 2; |
| 187 | + for (auto &c : slice) { |
| 188 | + UT_ASSERTeq(c, 'a'); |
| 189 | + c = 'b'; |
| 190 | + UT_ASSERTeq(str[cnt++], 'b'); |
| 191 | + UT_ASSERTeq(std::count(slice.begin(), slice.end(), 'b'), |
| 192 | + static_cast<long>(cnt - 2)); |
| 193 | + UT_ASSERTeq(str.size(), 10U + bonus_size); |
| 194 | + } |
| 195 | + }); |
| 196 | + UT_ASSERTeq(str.find('b'), str.npos); |
| 197 | + |
| 198 | + assert_tx_abort(pop, [&] { |
| 199 | + nvobj::slice<S::range_snapshotting_iterator> slice = |
| 200 | + str.range(0, 10 + bonus_size, 11 + bonus_size); |
| 201 | + UT_ASSERTeq(&str.front(), &*slice.begin()); |
| 202 | + UT_ASSERTeq(&str.front() + 10 + bonus_size, &*slice.end()); |
| 203 | + |
| 204 | + size_t cnt = 0; |
| 205 | + for (auto &c : slice) { |
| 206 | + UT_ASSERTeq(c, 'a'); |
| 207 | + c = 'b'; |
| 208 | + UT_ASSERTeq(str[cnt++], 'b'); |
| 209 | + UT_ASSERTeq(std::count(slice.begin(), slice.end(), 'b'), |
| 210 | + static_cast<long>(cnt)); |
| 211 | + UT_ASSERTeq(str.size(), 10U + bonus_size); |
| 212 | + } |
| 213 | + }); |
| 214 | + UT_ASSERTeq(str.find('b'), str.npos); |
| 215 | + |
| 216 | + nvobj::transaction::run(pop, [&] { |
| 217 | + nvobj::slice<S::pointer> slice = str.range(0, 10 + bonus_size); |
| 218 | + for (auto &c : slice) { |
| 219 | + c = 'b'; |
| 220 | + } |
| 221 | + }); |
| 222 | + UT_ASSERTeq(str.size(), 10U + bonus_size); |
| 223 | + UT_ASSERTeq(std::count(str.begin(), str.end(), 'b'), |
| 224 | + static_cast<long>(10 + bonus_size)); |
| 225 | + |
| 226 | + nvobj::slice<S::const_iterator> slice1 = const_str.range(0, 3); |
| 227 | + UT_ASSERTeq(&const_str.front(), slice1.begin()); |
| 228 | + UT_ASSERTeq(&const_str.front() + 3, slice1.end()); |
| 229 | + |
| 230 | + nvobj::slice<S::const_iterator> slice2 = str.crange(0, 3); |
| 231 | + UT_ASSERTeq(&str.front(), slice2.begin()); |
| 232 | + UT_ASSERTeq(&str.front() + 3, slice2.end()); |
| 233 | + |
| 234 | + nvobj::transaction::run(pop, |
| 235 | + [&] { nvobj::delete_persistent<S>(r->s); }); |
| 236 | +} |
| 237 | + |
| 238 | +static void |
| 239 | +test(int argc, char *argv[]) |
| 240 | +{ |
| 241 | + if (argc < 2) { |
| 242 | + UT_FATAL("usage: %s file-name", argv[0]); |
| 243 | + } |
| 244 | + |
| 245 | + auto path = argv[1]; |
| 246 | + auto pop = nvobj::pool<root>::create( |
| 247 | + path, "StringTest", PMEMOBJ_MIN_POOL, S_IWUSR | S_IRUSR); |
| 248 | + |
| 249 | + test_out_of_range_exception(pop, true); |
| 250 | + test_out_of_range_exception(pop, false); |
| 251 | + test_returned_values(pop, true); |
| 252 | + test_returned_values(pop, false); |
| 253 | + |
| 254 | + pop.close(); |
| 255 | +} |
| 256 | + |
| 257 | +int |
| 258 | +main(int argc, char *argv[]) |
| 259 | +{ |
| 260 | + return run_test([&] { test(argc, argv); }); |
| 261 | +} |
0 commit comments