test_db.cpp 59.1 KB
Newer Older
B
Bucky Kittinger 已提交
1 2 3 4 5
#include <eosiolib/types.hpp>
#include <eosiolib/action.hpp>
#include <eosiolib/db.h>
#include <eosiolib/db.hpp>
#include <eosiolib/memory.hpp>
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
#include "../test_api/test_api.hpp"

int primary[11]      = {0,1,2,3,4,5,6,7,8,9,10};
int secondary[11]    = {7,0,1,3,6,9,10,2,4,5,8};
int tertiary[11]     = {0,10,1,2,4,3,5,6,7,8,9};

int primary_lb[11]   = {0,0,0,3,3,3,6,7,7,9,9};
int secondary_lb[11] = {0,0,10,0,10,10,0,7,8,0,10};
int tertiary_lb[11]  = {0,1,2,3,2,5,6,7,8,9,0};

int primary_ub[11]   = {3,3,3,6,6,6,7,9,9,-1,-1};
int secondary_ub[11] = {10,10,8,10,8,8,10,0,-1,10,8};
int tertiary_ub[11]  = {1,2,3,5,3,6,7,8,9,-1,1};

#pragma pack(push, 1)
struct test_model {
   account_name  name;
   unsigned char age;
   uint64_t      phone;
};

struct test_model_v2 : test_model {
  test_model_v2() : new_field(0) {}
  uint64_t new_field;
};

struct test_model_v3 : test_model_v2 {
  uint64_t another_field;
};

struct TestModel128x2 {
  uint128_t number;
  uint128_t price;
  uint64_t  extra;
  uint64_t  table_name;
};

struct TestModel128x2_V2 : TestModel128x2 {
  uint64_t  new_field;
};

struct TestModel3xi64 {
  uint64_t a;
  uint64_t b;
  uint64_t c;
  uint64_t table;
};

struct TestModel3xi64_V2 : TestModel3xi64 {
  uint64_t new_field;
};

#pragma pack(pop)

#define STRLEN(s) my_strlen(s)

extern "C" {
  void my_memset(void *vptr, unsigned char val, unsigned int size) {
    char *ptr = (char *)vptr;
    while(size--) { *(ptr++)=val; }
  }
B
Bucky Kittinger 已提交
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
  uint32_t my_strlen(const char *str) {
     uint32_t len = 0;
     while(str[len]) ++len;
     return len;
  }
  bool my_memcmp(void *s1, void *s2, uint32_t n) {
     unsigned char *c1 = (unsigned char*)s1;
     unsigned char *c2 = (unsigned char*)s2;
     for (uint32_t i = 0; i < n; i++) {
        if (c1[i] != c2[i]) {
           return false;
        }
     }
     return true;
  }
82 83
}

84
#if 0
B
Bucky Kittinger 已提交
85
void test_db::key_str_table() {
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101

    const char* keys[] = { "alice", "bob", "carol", "dave" };
    const char* vals[] = { "data1", "data2", "data3", "data4" };

    const char* atr[]  = { "atr", "atr", "atr", "atr" };
    const char* ztr[]  = { "ztr", "ztr", "ztr", "ztr" };
    
    eosio::var_table<N(tester), N(tester), N(atr), char*> StringTableAtr;
    eosio::var_table<N(tester), N(tester), N(ztr), char*> StringTableZtr;
    eosio::var_table<N(tester), N(tester), N(str), char*> StringTableStr;

    uint32_t res = 0;

    // fill some data in contiguous tables
    for( int ii = 0; ii < 4; ++ii ) {
        res = StringTableAtr.store( (char*)keys[ii], STRLEN(keys[ii]), (char*)atr[ii], STRLEN(atr[ii]) );
102
        eosio_assert( res != 0, "atr" );
103 104
    
        res = StringTableZtr.store( (char*)keys[ii], STRLEN(keys[ii]), (char*)ztr[ii], STRLEN(ztr[ii]) );
105
        eosio_assert(res != 0, "ztr" );
106 107 108 109 110
    }

    char tmp[64];
    
    res = StringTableStr.store ((char *)keys[0], STRLEN(keys[0]), (char *)vals[0], STRLEN(vals[0]));
111
    eosio_assert(res != 0, "store alice" );
112 113

    res = StringTableStr.store((char *)keys[1], STRLEN(keys[1]), (char *)vals[1], STRLEN(vals[1]) );
114
    eosio_assert(res != 0, "store bob" );
115 116

    res = StringTableStr.store((char *)keys[2], STRLEN(keys[2]), (char *)vals[2], STRLEN(vals[2]) );
117
    eosio_assert(res != 0, "store carol" );
118 119

    res = StringTableStr.store((char *)keys[3], STRLEN(keys[3]), (char *)vals[3], STRLEN(vals[3]) );
120
    eosio_assert(res != 0, "store dave" );
121 122

    res = StringTableStr.load((char *)keys[0], STRLEN(keys[0]), tmp, 64);
123
    eosio_assert(res == STRLEN(vals[0]) && my_memcmp((void *)vals[0], (void *)tmp, res), "load alice");
124 125

    res = StringTableStr.load((char *)keys[1], STRLEN(keys[1]), tmp, 64);
126
    eosio_assert(res == STRLEN(vals[1]) && my_memcmp((void *)vals[1], (void *)tmp, res), "load bob");
127 128

    res = StringTableStr.load((char *)keys[2], STRLEN(keys[2]), tmp, 64);
129
    eosio_assert(res == STRLEN(vals[2]) && my_memcmp((void *)vals[2], (void *)tmp, res), "load carol");
130 131

    res = StringTableStr.load((char *)keys[3], STRLEN(keys[3]), tmp, 64);
132
    eosio_assert(res == STRLEN(vals[3]) && my_memcmp((void *)vals[3], (void *)tmp, res), "load dave");
133 134

    res = StringTableStr.previous((char *)keys[3], STRLEN(keys[3]), tmp, 64);
135
    eosio_assert(res == STRLEN(vals[2]) && my_memcmp((void *)vals[2], (void *)tmp, res), "back carol");
136 137

    res = StringTableStr.previous((char *)keys[2], STRLEN(keys[2]), tmp, 64);
138
    eosio_assert(res == STRLEN(vals[1]) && my_memcmp((void *)vals[1], (void *)tmp, res), "back dave");
139 140

    res = StringTableStr.previous((char *)keys[1], STRLEN(keys[1]), tmp, 64);
141
    eosio_assert(res == STRLEN(vals[0]) && my_memcmp((void *)vals[0], (void *)tmp, res), "back alice");
142 143

    res = StringTableStr.previous((char *)keys[0], STRLEN(keys[0]), tmp, 64);
144
    eosio_assert(res == -1, "no prev");
145 146

    res = StringTableStr.next((char *)keys[0], STRLEN(keys[0]), tmp, 64);
147
    eosio_assert(res == STRLEN(vals[1]) && my_memcmp((void *)vals[1], (void *)tmp, res), "next bob");
148 149

    res = StringTableStr.next((char *)keys[1], STRLEN(keys[1]), tmp, 64);
150
    eosio_assert(res == STRLEN(vals[2]) && my_memcmp((void *)vals[2], (void *)tmp, res), "next carol");
151 152

    res = StringTableStr.next((char *)keys[2], STRLEN(keys[2]), tmp, 64);
153
    eosio_assert(res == STRLEN(vals[3]) && my_memcmp((void *)vals[3], (void *)tmp, res), "next dave");
154 155

    res = StringTableStr.next((char *)keys[3], STRLEN(keys[3]), tmp, 64);
156
    eosio_assert(res == -1, "no next");
157 158

    res = StringTableStr.next((char *)keys[0], STRLEN(keys[0]), tmp, 0);
159
    eosio_assert(res == 0, "next 0");
160

B
Bucky Kittinger 已提交
161
    res = StringTableStr.front((char*)keys[0], STRLEN(keys[0]), tmp, 64);
162
    eosio_assert(res == STRLEN(vals[0]) && my_memcmp((void *)vals[0], (void *)tmp, res), "front alice");
163

B
Bucky Kittinger 已提交
164
    res = StringTableStr.back((char*)keys[0], STRLEN(keys[0]), tmp, 64);
165
    eosio_assert(res == STRLEN(vals[3]) && my_memcmp((void *)vals[3], (void *)tmp, res), "back dave");
166 167

    res = StringTableStr.lower_bound((char *)keys[0], STRLEN(keys[0]), tmp, 64);
168
    eosio_assert(res == STRLEN(vals[0]) && my_memcmp((void *)vals[0], (void *)tmp, res), "lowerbound alice");
169 170

    res = StringTableStr.upper_bound((char *)keys[0], STRLEN(keys[0]), tmp, 64);
171
    eosio_assert(res == STRLEN(vals[1]) && my_memcmp((void *)vals[1], (void *)tmp, res), "upperbound bob");
172 173

    res = StringTableStr.lower_bound((char *)keys[3], STRLEN(keys[3]), tmp, 64);
174
    eosio_assert(res == STRLEN(vals[3]) && my_memcmp((void *)vals[3], (void *)tmp, res), "upperbound dave");
175 176

    res = StringTableStr.upper_bound((char *)keys[3], STRLEN(keys[3]), tmp, 64);
177
    eosio_assert(res == -1, "no upper_bound");
178 179

}
180
#endif
181

B
Bucky Kittinger 已提交
182
void test_db::key_str_general() {
183 184 185 186 187 188 189 190 191 192 193

  const char* keys[] = { "alice", "bob", "carol", "dave" };
  const char* vals[] = { "data1", "data2", "data3", "data4" };

  const char* atr[]  = { "atr", "atr", "atr", "atr" };
  const char* ztr[]  = { "ztr", "ztr", "ztr", "ztr" };

  uint32_t res=0;

  //fill some data in contiguous tables
  for(int i=0; i < 4; ++i) {
194 195
    res = store_str(current_receiver(),  N(atr), N(testapi), (char *)keys[i], STRLEN(keys[i]), (char *)atr[i], STRLEN(atr[i]) );
    eosio_assert(res != 0, "atr" );
196

197 198
    res = store_str(current_receiver(),  N(ztr), N(testapi), (char *)keys[i], STRLEN(keys[i]), (char *)ztr[i], STRLEN(ztr[i]) );
    eosio_assert(res != 0, "ztr" );
199 200 201 202
  }

  char tmp[64];

203 204 205 206 207
  res = store_str(current_receiver(),  N(str), N(testapi), (char *)keys[0], STRLEN(keys[0]), (char *)vals[0], STRLEN(vals[0]) );
  eosio_assert(res != 0, "store alice" );

  res = store_str(current_receiver(),  N(str), N(testapi), (char *)keys[1], STRLEN(keys[1]), (char *)vals[1], STRLEN(vals[1]) );
  eosio_assert(res != 0, "store bob" );
208

209 210
  res = store_str(current_receiver(),  N(str), N(testapi), (char *)keys[2], STRLEN(keys[2]), (char *)vals[2], STRLEN(vals[2]) );
  eosio_assert(res != 0, "store carol" );
211

212 213
  res = store_str(current_receiver(),  N(str), N(testapi), (char *)keys[3], STRLEN(keys[3]), (char *)vals[3], STRLEN(vals[3]) );
  eosio_assert(res != 0, "store dave" );
214 215


216 217
  res = load_str(current_receiver(), current_receiver(),  N(str), (char *)keys[0], STRLEN(keys[0]), tmp, 64);
  eosio_assert(res == STRLEN(vals[0]) && my_memcmp((void *)vals[0], (void *)tmp, res), "load alice");
218

219 220
  res = load_str(current_receiver(), current_receiver(),  N(str), (char *)keys[1], STRLEN(keys[1]), tmp, 64);
  eosio_assert(res == STRLEN(vals[1]) && my_memcmp((void *)vals[1], (void *)tmp, res), "load bob");
221

222 223
  res = load_str(current_receiver(), current_receiver(),  N(str), (char *)keys[2], STRLEN(keys[2]), tmp, 64);
  eosio_assert(res == STRLEN(vals[2]) && my_memcmp((void *)vals[2], (void *)tmp, res), "load carol");
224

225 226
  res = load_str(current_receiver(), current_receiver(),  N(str), (char *)keys[3], STRLEN(keys[3]), tmp, 64);
  eosio_assert(res == STRLEN(vals[3]) && my_memcmp((void *)vals[3], (void *)tmp, res), "load dave");
227 228

  res = previous_str(current_receiver(), current_receiver(), N(str), (char *)keys[3], STRLEN(keys[3]), tmp, 64);
229
  eosio_assert(res == STRLEN(vals[2]) && my_memcmp((void *)vals[2], (void *)tmp, res), "back carol");
230 231

  res = previous_str(current_receiver(), current_receiver(), N(str), (char *)keys[2], STRLEN(keys[2]), tmp, 64);
232
  eosio_assert(res == STRLEN(vals[1]) && my_memcmp((void *)vals[1], (void *)tmp, res), "back dave");
233 234

  res = previous_str(current_receiver(), current_receiver(), N(str), (char *)keys[1], STRLEN(keys[1]), tmp, 64);
235
  eosio_assert(res == STRLEN(vals[0]) && my_memcmp((void *)vals[0], (void *)tmp, res), "back alice");
236 237

  res = previous_str(current_receiver(), current_receiver(), N(str), (char *)keys[0], STRLEN(keys[0]), tmp, 64);
238
  eosio_assert(res == 0, "no prev");
239

240 241
  res = next_str(current_receiver(), current_receiver(),  N(str), (char *)keys[0], STRLEN(keys[0]), tmp, 64);
  eosio_assert(res == STRLEN(vals[1]) && my_memcmp((void *)vals[1], (void *)tmp, res), "next bob");
242

243 244
  res = next_str(current_receiver(), current_receiver(),  N(str), (char *)keys[1], STRLEN(keys[1]), tmp, 64);
  eosio_assert(res == STRLEN(vals[2]) && my_memcmp((void *)vals[2], (void *)tmp, res), "next carol");
245

246 247
  res = next_str(current_receiver(), current_receiver(),  N(str), (char *)keys[2], STRLEN(keys[2]), tmp, 64);
  eosio_assert(res == STRLEN(vals[3]) && my_memcmp((void *)vals[3], (void *)tmp, res), "next dave");
248 249

  res = lower_bound_str(current_receiver(), current_receiver(), N(str), (char *)keys[0], STRLEN(keys[0]), tmp, 64);
250 251 252 253
  eosio_assert(res == STRLEN(vals[0]) && my_memcmp((void *)vals[0], (void *)tmp, res), "lowerbound alice");

  res = upper_bound_str(current_receiver(), current_receiver(),  N(str), (char *)keys[0], STRLEN(keys[0]), tmp, 64);
  eosio_assert(res == STRLEN(vals[1]) && my_memcmp((void *)vals[1], (void *)tmp, res), "upperbound bob");
254

255 256
  res = lower_bound_str(current_receiver(), current_receiver(),  N(str), (char *)keys[3], STRLEN(keys[3]), tmp, 64);
  eosio_assert(res == STRLEN(vals[3]) && my_memcmp((void *)vals[3], (void *)tmp, res), "upperbound dave");
257

258 259
  res = upper_bound_str(current_receiver(), current_receiver(),  N(str), (char *)keys[3], STRLEN(keys[3]), tmp, 64);
  eosio_assert(res == 0, "no upper_bound");
260

261 262
  res = update_str(current_receiver(),  N(str), N(testapi), (char *)keys[3], STRLEN(keys[3]), (char *)vals[2], STRLEN(vals[2]) );
  eosio_assert(res != 0, "store dave" );
263

264 265
  res = load_str(current_receiver(),  current_receiver(), N(str), (char*)keys[3], STRLEN(keys[3]), tmp, 64 );
  eosio_assert(res == STRLEN(vals[2]) && my_memcmp((void*)vals[2], (void*)tmp, res), "load updated carol" );
266 267
}

268
#if 0
B
Bucky Kittinger 已提交
269
void test_db::key_i64_general() {
270 271 272 273 274 275 276 277

  uint32_t res = 0;
  test_model alice{ N(alice), 20, 4234622};
  test_model bob  { N(bob),   15, 11932435};
  test_model carol{ N(carol), 30, 545342453};
  test_model dave { N(dave),  46, 6535354};

  res = store_i64(current_receiver(),  N(test_table), &dave,  sizeof(test_model));
278
  eosio_assert(res != 0, "store dave" );
279 280

  res = store_i64(current_receiver(), N(test_table), &carol, sizeof(test_model));
281
  eosio_assert(res != 0, "store carol" );
282 283

  res = store_i64(current_receiver(),   N(test_table), &bob,   sizeof(test_model));
284
  eosio_assert(res != 0, "store bob" );
285 286

  res = store_i64(current_receiver(), N(test_table), &alice, sizeof(test_model));
287
  eosio_assert(res != 0, "store alice" );
288 289 290 291 292 293 294 295 296 297 298 299 300 301

  //fill with different ages in adjacent tables
  dave.age=123;  store_i64(current_receiver(), N(test_tabld), &dave,  sizeof(test_model));
  dave.age=124;  store_i64(current_receiver(), N(test_tablf), &dave,  sizeof(test_model));
  carol.age=125; store_i64(current_receiver(), N(test_tabld), &carol, sizeof(test_model));
  carol.age=126; store_i64(current_receiver(), N(test_tablf), &carol, sizeof(test_model));
  bob.age=127;   store_i64(current_receiver(), N(test_tabld), &bob,   sizeof(test_model));
  bob.age=128;   store_i64(current_receiver(), N(test_tablf), &bob,   sizeof(test_model));
  alice.age=129; store_i64(current_receiver(), N(test_tabld), &alice, sizeof(test_model));
  alice.age=130; store_i64(current_receiver(), N(test_tablf), &alice, sizeof(test_model));

  test_model tmp;

  res = front_i64( current_receiver(), current_receiver(), N(test_table), &tmp, sizeof(test_model) );
302
  eosio_assert(res == sizeof(test_model) && tmp.name == N(alice) && tmp.age == 20 && tmp.phone == 4234622, "front_i64 1");
303 304 305
  my_memset(&tmp, 0, sizeof(test_model));

  res = back_i64( current_receiver(), current_receiver(), N(test_table), &tmp, sizeof(test_model) );
306
  eosio_assert(res == sizeof(test_model) && tmp.name == N(dave) && tmp.age == 46 && tmp.phone == 6535354, "front_i64 2");
307 308

  res = previous_i64( current_receiver(), current_receiver(), N(test_table), &tmp, sizeof(test_model) );
309
  eosio_assert(res == sizeof(test_model) && tmp.name == N(carol) && tmp.age == 30 && tmp.phone == 545342453, "carol previous");
310 311
  
  res = previous_i64( current_receiver(), current_receiver(), N(test_table), &tmp, sizeof(test_model) );
312
  eosio_assert(res == sizeof(test_model) && tmp.name == N(bob) && tmp.age == 15 && tmp.phone == 11932435, "bob previous");
313 314

  res = previous_i64( current_receiver(), current_receiver(), N(test_table), &tmp, sizeof(test_model) );
315
  eosio_assert(res == sizeof(test_model) && tmp.name == N(alice) && tmp.age == 20 && tmp.phone == 4234622, "alice previous");
316 317

  res = previous_i64( current_receiver(), current_receiver(), N(test_table), &tmp, sizeof(test_model) );
318
  eosio_assert(res == 0, "previous null");
319 320

  res = next_i64( current_receiver(), current_receiver(), N(test_table), &tmp, sizeof(test_model) );
321
  eosio_assert(res == sizeof(test_model) && tmp.name == N(bob) && tmp.age == 15 && tmp.phone == 11932435, "bob next");
322 323

  res = next_i64( current_receiver(), current_receiver(), N(test_table), &tmp, sizeof(test_model) );
324
  eosio_assert(res == sizeof(test_model) && tmp.name == N(carol) && tmp.age == 30 && tmp.phone == 545342453, "carol next");
325 326

  res = next_i64( current_receiver(), current_receiver(), N(test_table), &tmp, sizeof(test_model) );
327
  eosio_assert(res == sizeof(test_model) && tmp.name == N(dave) && tmp.age == 46 && tmp.phone == 6535354, "dave next");
328 329

  res = next_i64( current_receiver(), current_receiver(), N(test_table), &tmp, sizeof(test_model) );
330
  eosio_assert(res == 0, "next null");
331 332 333

  my_memset(&alice, 0, sizeof(test_model));

334
  eosio_assert(alice.name == 0 && alice.age == 0 && alice.phone == 0, "my_memset");
335 336 337 338

  alice.name = N(alice);

  res = load_i64(current_receiver(), current_receiver(), N(test_table), &alice, sizeof(test_model));
339
  eosio_assert(res == sizeof(test_model) && alice.age == 20 && alice.phone == 4234622, "alice error 1");
340 341 342 343 344

  alice.age = 21;
  alice.phone = 1234;
  
  res = store_i64(current_receiver(), N(test_table), &alice, sizeof(test_model));
345
  eosio_assert(res == 0, "store alice 2" );
346 347 348 349 350

  my_memset(&alice, 0, sizeof(test_model));
  alice.name = N(alice);
  
  res = load_i64(current_receiver(), current_receiver(), N(test_table), &alice, sizeof(test_model));
351
  eosio_assert(res == sizeof(test_model) && alice.age == 21 && alice.phone == 1234, "alice error 2");
352 353 354 355 356 357 358 359 360 361 362

  my_memset(&bob, 0, sizeof(test_model));
  bob.name = N(bob);

  my_memset(&carol, 0, sizeof(test_model));
  carol.name = N(carol);

  my_memset(&dave, 0, sizeof(test_model));
  dave.name = N(dave);

  res = load_i64(current_receiver(), current_receiver(), N(test_table), &bob, sizeof(test_model));
363
  eosio_assert(res == sizeof(test_model) && bob.age == 15 && bob.phone == 11932435, "bob error 1");
364 365

  res = load_i64(current_receiver(), current_receiver(), N(test_table), &carol, sizeof(test_model));
366
  eosio_assert(res == sizeof(test_model) && carol.age == 30 && carol.phone == 545342453, "carol error 1");
367 368

  res = load_i64(current_receiver(), current_receiver(), N(test_table), &dave, sizeof(test_model));
369
  eosio_assert(res == sizeof(test_model) && dave.age == 46 && dave.phone == 6535354, "dave error 1");
370

B
Bucky Kittinger 已提交
371
  res = load_i64(current_receiver(), N(other_code), N(test_table), &alice, sizeof(test_model));
372
  eosio_assert(res == sizeof(test_model), "other_code");
373 374

  res = load_i64(current_receiver(), current_receiver(), N(other_table), &alice, sizeof(test_model));
375
  eosio_assert(res == 0, "other_table");
376 377 378 379 380 381


  test_model_v2 alicev2;
  alicev2.name = N(alice);

  res = load_i64(current_receiver(), current_receiver(), N(test_table), &alicev2, sizeof(test_model_v2));
382
  eosio_assert(res == sizeof(test_model) && alicev2.age == 21 && alicev2.phone == 1234, "alicev2 load");
383 384 385

  alicev2.new_field = 66655444;
  res = store_i64(current_receiver(), N(test_table), &alicev2, sizeof(test_model_v2));
386
  eosio_assert(res == 0, "store alice 3" );
387 388 389 390 391

  my_memset(&alicev2, 0, sizeof(test_model_v2));
  alicev2.name = N(alice);

  res = load_i64(current_receiver(), current_receiver(), N(test_table), &alicev2, sizeof(test_model_v2));
392
  eosio_assert(res == sizeof(test_model_v2) && alicev2.age == 21 && alicev2.phone == 1234 && alicev2.new_field == 66655444, "alice model v2");
393 394 395 396

  my_memset(&tmp, 0, sizeof(test_model));
  tmp.name = N(bob);
  res = lower_bound_i64( current_receiver(), current_receiver(), N(test_table), &tmp, sizeof(test_model) );
397
  eosio_assert(res == sizeof(test_model) && tmp.name == N(bob), "lower_bound_i64 bob" );
398 399 400 401

  my_memset(&tmp, 0, sizeof(test_model));
  tmp.name = N(boc);
  res = lower_bound_i64( current_receiver(), current_receiver(), N(test_table), &tmp, sizeof(test_model) );
402
  eosio_assert(res == sizeof(test_model) && tmp.name == N(carol), "lower_bound_i64 carol" );
403 404 405

  my_memset(&tmp, 0, sizeof(test_model));
  tmp.name = N(dave);
B
Bucky Kittinger 已提交
406 407
  // data packet only big enough for name
  res = lower_bound_i64( current_receiver(), current_receiver(), N(test_table), &tmp,  sizeof(uint64_t) );
408
  eosio_assert(res == sizeof(test_model) && tmp.name == N(dave), "lower_bound_i64 dave" );
409 410 411 412

  my_memset(&tmp, 0, sizeof(test_model));
  tmp.name = N(davf);
  res = lower_bound_i64( current_receiver(), current_receiver(), N(test_table), &tmp, sizeof(uint64_t) );
413
  eosio_assert(res == 0, "lower_bound_i64 fail" );
414 415 416 417

  my_memset(&tmp, 0, sizeof(test_model));
  tmp.name = N(alice);
  res = upper_bound_i64( current_receiver(), current_receiver(), N(test_table), &tmp, sizeof(test_model) );
418
  eosio_assert(res == sizeof(test_model) && tmp.age == 15 && tmp.name == N(bob), "upper_bound_i64 bob" );
419 420 421 422

  my_memset(&tmp, 0, sizeof(test_model));
  tmp.name = N(dave);
  res = upper_bound_i64( current_receiver(), current_receiver(), N(test_table), &tmp, sizeof(test_model) );
423
  eosio_assert(res == 0, "upper_bound_i64 dave" );
424 425 426 427
  test_model_v3 tmp2;
  tmp2.name = N(alice);

  res = load_i64(current_receiver(), current_receiver(), N(test_table), &tmp2, sizeof(test_model_v3));
428
  eosio_assert(res == sizeof(test_model_v2) &&
429 430 431 432 433 434 435
              tmp2.age == 21 && 
              tmp2.phone == 1234 &&
              tmp2.new_field == 66655444,
              "load4update");

  tmp2.another_field = 221122;
  res = update_i64(current_receiver(), N(test_table), &tmp2, sizeof(test_model_v3));
436
  eosio_assert(res == 1, "update_i64");
437 438

  res = load_i64(current_receiver(), current_receiver(), N(test_table), &tmp2, sizeof(test_model_v3));
439
  eosio_assert(res == sizeof(test_model_v3) &&
440 441 442 443 444 445 446 447
              tmp2.age == 21 && 
              tmp2.phone == 1234 &&
              tmp2.new_field == 66655444 &&
              tmp2.another_field == 221122,
              "load4update");

  tmp2.age = 11;
  res = update_i64(current_receiver(), N(test_table), &tmp2,  sizeof(uint64_t)+1 );
448
  eosio_assert(res == 1, "update_i64 small");
449 450

  res = load_i64(current_receiver(), current_receiver(), N(test_table), &tmp2, sizeof(test_model_v3));
451
  eosio_assert(res == sizeof(test_model_v3) &&
452 453 454 455 456 457 458 459 460 461
              tmp2.age == 11 && 
              tmp2.phone == 1234 &&
              tmp2.new_field == 66655444 &&
              tmp2.another_field == 221122,
              "load_i64 update_i64");


  //Remove dummy records
  uint64_t tables[] { N(test_tabld), N(test_tablf) };
  for(auto& t : tables) {
B
Bucky Kittinger 已提交
462
    while( front_i64( current_receiver(), current_receiver(), t, &tmp, sizeof(test_model) ) != 0 ) {
463 464 465 466 467
      remove_i64(current_receiver(), t, &tmp);
    }
  }
}

B
Bucky Kittinger 已提交
468
void test_db::key_i64_remove_all() {
469 470 471 472 473 474
  
  uint32_t res = 0;
  uint64_t key;

  key = N(alice);
  res = remove_i64(current_receiver(), N(test_table), &key);
475
  eosio_assert(res == 1, "remove alice");
476 477 478
  
  key = N(bob);
  res = remove_i64(current_receiver(),   N(test_table), &key);
479
  eosio_assert(res == 1, "remove bob");
480 481 482
  
  key = N(carol);
  res = remove_i64(current_receiver(), N(test_table), &key);
483
  eosio_assert(res == 1, "remove carol");
484 485 486
  
  key = N(dave);
  res = remove_i64(current_receiver(),  N(test_table), &key);
487
  eosio_assert(res == 1, "remove dave");
488 489 490

  test_model tmp;
  res = front_i64( current_receiver(), current_receiver(), N(test_table), &tmp, sizeof(test_model) );
491
  eosio_assert(res == 0, "front_i64 remove");
492 493

  res = back_i64( current_receiver(), current_receiver(), N(test_table), &tmp, sizeof(test_model) );
494
  eosio_assert(res == 0, "back_i64_i64 remove");
495 496 497
  
  key = N(alice);
  res = remove_i64(current_receiver(), N(test_table), &key);
498
  eosio_assert(res == 0, "remove alice 1");
499 500 501
  
  key = N(bob);
  res = remove_i64(current_receiver(),   N(test_table), &key);
502
  eosio_assert(res == 0, "remove bob 1");
503 504 505
  
  key = N(carol);
  res = remove_i64(current_receiver(), N(test_table), &key);
506
  eosio_assert(res == 0, "remove carol 1");
507 508 509
  
  key = N(dave);
  res = remove_i64(current_receiver(),  N(test_table), &key);
510
  eosio_assert(res == 0, "remove dave 1");
511 512
}

B
Bucky Kittinger 已提交
513
void test_db::key_i64_small_load() {
514
  uint64_t dummy = 0;
B
Bucky Kittinger 已提交
515 516 517
  test_model alice{ N(alice), 20, 4234622};
  // shouldn't throw an error, short circuits out because no table id is found
  auto res = load_i64(current_receiver(), current_receiver(), N(just_uint64), &dummy, sizeof(uint64_t)-1);
518
  eosio_assert(res == 0, "should have returned 0 on failure");
B
Bucky Kittinger 已提交
519 520
  store_i64(current_receiver(),  N(test_table), &alice,  sizeof(test_model));
  load_i64(current_receiver(), current_receiver(), N(test_table), &alice, sizeof(uint64_t)-1);
521 522
}

B
Bucky Kittinger 已提交
523
void test_db::key_i64_small_store() {
524 525 526 527
  uint64_t dummy = 0;
  store_i64(current_receiver(), N(just_uint64), &dummy, sizeof(uint64_t)-1);
}

B
Bucky Kittinger 已提交
528
void test_db::key_i64_store_scope() {
529 530 531 532
  uint64_t dummy = 0;
  store_i64(current_receiver(), N(just_uint64), &dummy, sizeof(uint64_t));
}

B
Bucky Kittinger 已提交
533
void test_db::key_i64_remove_scope() {
534 535 536 537
  uint64_t dummy = 0;
  store_i64(current_receiver(), N(just_uint64), &dummy, sizeof(uint64_t));
}

B
Bucky Kittinger 已提交
538
void test_db::key_i64_not_found() {
539 540 541
  uint64_t dummy = 1000;

  auto res = load_i64(current_receiver(), current_receiver(), N(just_uint64), &dummy, sizeof(uint64_t));
542
  eosio_assert(res == 0, "i64_not_found load");
543 544

  res = remove_i64(current_receiver(),  N(just_uint64), &dummy);
545
  eosio_assert(res == 0, "i64_not_found remove");
546 547
}

B
Bucky Kittinger 已提交
548
void test_db::key_i64_front_back() {
549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565
  
  uint32_t res = 0;

  test_model dave { N(dave),  46, 6535354};
  test_model carol{ N(carol), 30, 545342453};
  store_i64(current_receiver(), N(b), &dave,  sizeof(test_model));
  store_i64(current_receiver(), N(b), &carol, sizeof(test_model));

  test_model bob  { N(bob),   15, 11932435};
  test_model alice{ N(alice), 20, 4234622};
  store_i64(current_receiver(), N(a), &bob, sizeof(test_model));
  store_i64(current_receiver(), N(a), &alice,  sizeof(test_model));

  test_model tmp;

  my_memset(&tmp, 0, sizeof(test_model));
  res = front_i64( current_receiver(), current_receiver(), N(a), &tmp, sizeof(test_model) );
566
  eosio_assert(res == sizeof(test_model) && tmp.name == N(alice) && tmp.age == 20 && tmp.phone == 4234622, "key_i64_front 1");
567 568 569

  my_memset(&tmp, 0, sizeof(test_model));
  res = back_i64( current_receiver(), current_receiver(), N(a), &tmp, sizeof(test_model) );
570
  eosio_assert(res == sizeof(test_model) && tmp.name == N(bob) && tmp.age == 15 && tmp.phone == 11932435, "key_i64_front 2");
571 572 573

  my_memset(&tmp, 0, sizeof(test_model));
  res = front_i64( current_receiver(), current_receiver(), N(b), &tmp, sizeof(test_model) );
574
  eosio_assert(res == sizeof(test_model) && tmp.name == N(carol) && tmp.age == 30 && tmp.phone == 545342453, "key_i64_front 3");
575 576 577

  my_memset(&tmp, 0, sizeof(test_model));
  res = back_i64( current_receiver(), current_receiver(), N(b), &tmp, sizeof(test_model) );
578
  eosio_assert(res == sizeof(test_model) && tmp.name == N(dave) && tmp.age == 46 && tmp.phone == 6535354, "key_i64_front 4");
579 580 581 582 583 584

  uint64_t key = N(carol);
  remove_i64(current_receiver(), N(b), &key);

  my_memset(&tmp, 0, sizeof(test_model));
  res = front_i64( current_receiver(), current_receiver(), N(b), &tmp, sizeof(test_model) );
585
  eosio_assert(res == sizeof(test_model) && tmp.name == N(dave) && tmp.age == 46 && tmp.phone == 6535354, "key_i64_front 5");
586 587 588

  my_memset(&tmp, 0, sizeof(test_model));
  res = back_i64( current_receiver(), current_receiver(), N(b), &tmp, sizeof(test_model) );
589
  eosio_assert(res == sizeof(test_model) && tmp.name == N(dave) && tmp.age == 46 && tmp.phone == 6535354, "key_i64_front 6");
590 591 592

  my_memset(&tmp, 0, sizeof(test_model));
  res = front_i64( current_receiver(), current_receiver(), N(a), &tmp, sizeof(test_model) );
593
  eosio_assert(res == sizeof(test_model) && tmp.name == N(alice) && tmp.age == 20 && tmp.phone == 4234622, "key_i64_front 7");
594 595 596

  my_memset(&tmp, 0, sizeof(test_model));
  res = back_i64( current_receiver(), current_receiver(), N(a), &tmp, sizeof(test_model) );
597
  eosio_assert(res == sizeof(test_model) && tmp.name == N(bob) && tmp.age == 15 && tmp.phone == 11932435, "key_i64_front 8");
598 599 600 601 602

  key = N(dave);
  remove_i64(current_receiver(), N(b), &key);
  
  res = front_i64( current_receiver(), current_receiver(), N(b), &tmp, sizeof(test_model) );
603
  eosio_assert(res == 0, "key_i64_front 9");
604
  res = back_i64( current_receiver(), current_receiver(), N(b), &tmp, sizeof(test_model) );
605
  eosio_assert(res == 0, "key_i64_front 10");
606 607 608 609 610 611

  key = N(bob);
  remove_i64(current_receiver(), N(a), &key);

  my_memset(&tmp, 0, sizeof(test_model));
  res = front_i64( current_receiver(), current_receiver(), N(a), &tmp, sizeof(test_model) );
612
  eosio_assert(res == sizeof(test_model) && tmp.name == N(alice) && tmp.age == 20 && tmp.phone == 4234622, "key_i64_front 11");
613 614 615

  my_memset(&tmp, 0, sizeof(test_model));
  res = back_i64( current_receiver(), current_receiver(), N(a), &tmp, sizeof(test_model) );
616
  eosio_assert(res == sizeof(test_model) && tmp.name == N(alice) && tmp.age == 20 && tmp.phone == 4234622, "key_i64_front 12");
617 618 619 620 621

  key = N(alice);
  remove_i64(current_receiver(), N(a), &key);

  res = front_i64( current_receiver(), current_receiver(), N(a), &tmp, sizeof(test_model) );
622
  eosio_assert(res == 0, "key_i64_front 13");
623
  res = back_i64( current_receiver(), current_receiver(), N(a), &tmp, sizeof(test_model) );
624
  eosio_assert(res == 0, "key_i64_front 14");
625 626
}

B
Bucky Kittinger 已提交
627
uint32_t store_set_in_table(uint64_t table_name)
628 629 630 631 632 633 634
{

  uint32_t res = 0;
  
  TestModel128x2 alice0{0, 500, N(alice0), table_name};
  TestModel128x2 alice1{1, 400, N(alice1), table_name};
  TestModel128x2 alice2{2, 300, N(alice2), table_name};
B
Bucky Kittinger 已提交
635
  TestModel128x2 alice22{2, 200, N(alice33), table_name};
636 637

  res = store_i128i128(current_receiver(),  table_name, &alice0,  sizeof(TestModel128x2));
638
  eosio_assert(res == 1, "store alice0" );
639 640

  res = store_i128i128(current_receiver(),  table_name, &alice1,  sizeof(TestModel128x2));
641
  eosio_assert(res == 1, "store alice1" );
642 643

  res = store_i128i128(current_receiver(),  table_name, &alice2,  sizeof(TestModel128x2));
644
  eosio_assert(res == 1, "store alice2" );
645 646

  res = store_i128i128(current_receiver(),  table_name, &alice22,  sizeof(TestModel128x2));
647
  eosio_assert(res == 1, "store alice22" );
648

B
Bucky Kittinger 已提交
649
  return res;
650 651 652 653 654 655
  TestModel128x2 bob0{10, 1, N(bob0), table_name};
  TestModel128x2 bob1{11, 2, N(bob1), table_name};
  TestModel128x2 bob2{12, 3, N(bob2), table_name};
  TestModel128x2 bob3{13, 4, N(bob3), table_name};

  res = store_i128i128(current_receiver(),  table_name, &bob0,  sizeof(TestModel128x2));
656
  eosio_assert(res == 1, "store bob0" );
657 658

  res = store_i128i128(current_receiver(),  table_name, &bob1,  sizeof(TestModel128x2));
659
  eosio_assert(res == 1, "store bob1" );
660 661

  res = store_i128i128(current_receiver(),  table_name, &bob2,  sizeof(TestModel128x2));
662
  eosio_assert(res == 1, "store bob2" );
663 664

  res = store_i128i128(current_receiver(),  table_name, &bob3,  sizeof(TestModel128x2));
665
  eosio_assert(res == 1, "store bob3" );
666 667 668 669 670 671 672

  TestModel128x2 carol0{20, 900, N(carol0), table_name};
  TestModel128x2 carol1{21, 800, N(carol1), table_name};
  TestModel128x2 carol2{22, 700, N(carol2), table_name};
  TestModel128x2 carol3{23, 600, N(carol3), table_name};

  res = store_i128i128(current_receiver(),  table_name, &carol0,  sizeof(TestModel128x2));
673
  eosio_assert(res == 1, "store carol0" );
674 675

  res = store_i128i128(current_receiver(),  table_name, &carol1,  sizeof(TestModel128x2));
676
  eosio_assert(res == 1, "store carol1" );
677 678

  res = store_i128i128(current_receiver(),  table_name, &carol2,  sizeof(TestModel128x2));
679
  eosio_assert(res == 1, "store carol2" );
680 681

  res = store_i128i128(current_receiver(),  table_name, &carol3,  sizeof(TestModel128x2));
682
  eosio_assert(res == 1, "store carol3" );
683 684 685 686 687 688 689

  TestModel128x2 dave0{30, 8, N(dave0), table_name};
  TestModel128x2 dave1{31, 7, N(dave1), table_name};
  TestModel128x2 dave2{32, 5, N(dave2), table_name};
  TestModel128x2 dave3{33, 4, N(dave3), table_name};

  res = store_i128i128(current_receiver(),  table_name, &dave0,  sizeof(TestModel128x2));
690
  eosio_assert(res == 1, "store dave0" );
691 692

  res = store_i128i128(current_receiver(),  table_name, &dave1,  sizeof(TestModel128x2));
693
  eosio_assert(res == 1, "store dave1" );
694 695

  res = store_i128i128(current_receiver(),  table_name, &dave2,  sizeof(TestModel128x2));
696
  eosio_assert(res == 1, "store dave2" );
697 698

  res = store_i128i128(current_receiver(),  table_name, &dave3,  sizeof(TestModel128x2));
699
  eosio_assert(res == 1, "store dave3" );
B
Bucky Kittinger 已提交
700
  return res;
701 702
}

B
Bucky Kittinger 已提交
703
void store_set_in_table(TestModel3xi64* records, int len, uint64_t table_name) {
704 705 706 707 708
  uint32_t res = 0;
  for( int i = 0; i < len; ++i ) {
    TestModel3xi64 *tmp = records+i;
    tmp->table = table_name;
    res = store_i64i64i64(current_receiver(),  table_name, tmp,  sizeof(TestModel3xi64));
709
    eosio_assert(res == 1, "store_set_in_table" );
710 711 712 713 714 715
  }
}


//TODO fix things
#if 0
B
Bucky Kittinger 已提交
716
void test_db::key_i64i64i64_general() {
717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754
  
  uint32_t res = 0;

  TestModel3xi64 records[] = {
    {1, 1,  0, N()}, // 0    <---------------------------
    {1, 1,  1, N()}, // 1                               |
    {1, 2,  2, N()}, // 2    <---------------           |
    {2, 1,  3, N()}, // 3                   |           |
    {2, 2,  2, N()}, // 4    same {secondary,tertiary}  |
    {2, 2,  5, N()}, // 5                               |
    {3, 1,  6, N()}, // 6                               |
    {4, 0,  7, N()}, // 7                               |
    {4, 5,  8, N()}, // 8                               |
    {5, 1,  9, N()}, // 9                               |
    {5, 2,  0, N()}, //10    same {tertiary}-------------
  };

  store_set_in_table(records, sizeof(records)/sizeof(records[0]), N(table1));
  store_set_in_table(records, sizeof(records)/sizeof(records[0]), N(table2));
  store_set_in_table(records, sizeof(records)/sizeof(records[0]), N(table3));

  #define CALL(F, O, I, T, V) F##_##I##_##O(current_receiver(), current_receiver(), T, &V, sizeof(V))

  #define LOAD(I, O, T, V) CALL(load, O, I, T, V)
  #define FRONT(I, O, T, V) CALL(front, O, I, T, V)
  #define BACK(I, O, T, V) CALL(back, O, I, T, V)
  #define NEXT(I, O, T, V) CALL(next, O, I, T, V)
  #define PREV(I, O, T, V) CALL(previous, O, I, T, V)
  #define UPPER(I, O, T, V) CALL(upper_bound, O, I, T, V)
  #define LOWER(I, O, T, V) CALL(lower_bound, O, I, T, V)

  #define LOGME 0
  #define BS(X) ((X) ? "true" : "false")
  #define TABLE1_ASSERT(I, V, msg) \
    if(LOGME) {\
      eosio::print(msg, " : ", res, " a:", V.a, " b:", V.b, " c:", V.c, " t:", V.table, "inx:", uint64_t(I), " ("); \
      eosio::print(BS(res == sizeof(V)), " ", BS(records[I].a == V.a), " ", BS(records[I].b == V.b), " ", BS(records[I].c == V.c), " => ", N(table2), ")\n"); \
    } \
755
    eosio_assert( res == sizeof(V) && records[I].a == V.a && records[I].b == V.b && \
756 757 758 759 760 761 762 763 764 765
     records[I].c == V.c /*&& records[I].table == uint64_t(N(table2))*/, msg);

  #define LOAD_OK(I, O, T, INX, MSG) \
    {eosio::remove_reference<decltype(V)>::type tmp; my_memset(&tmp, 0, sizeof(tmp));tmp = V; \
    res = LOAD(I, O, T, tmp); \
    TABLE1_ASSERT(INX, tmp, MSG)}

  #define LOAD_ER(I, O, T, MSG) \
    {eosio::remove_reference<decltype(V)>::type tmp; my_memset(&tmp, 0, sizeof(tmp));tmp = V; \
    res = LOAD(I, O, T, tmp); \
766
    eosio_assert(res == -1, MSG)}
767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816

  #define FRONT_OK(I, O, T, INX, MSG) \
    {eosio::remove_reference<decltype(V)>::type tmp; my_memset(&tmp, 0, sizeof(tmp));tmp = V; \
    res = FRONT(I, O, T, tmp); \
    TABLE1_ASSERT(INX, tmp, MSG)}

  #define BACK_OK(I, O, T, INX, MSG) \
    {eosio::remove_reference<decltype(V)>::type tmp; my_memset(&tmp, 0, sizeof(tmp));tmp = V; \
    res = BACK(I, O, T, tmp); \
    TABLE1_ASSERT(INX, tmp, MSG)}

  TestModel3xi64 V;

  V={0}; LOAD_ER(primary, i64i64i64, N(table2), "i64x3 LOAD primary fail 0");
  V={1}; LOAD_OK(primary, i64i64i64, N(table2), 0, "i64x3 LOAD primary 1");
  V={2}; LOAD_OK(primary, i64i64i64, N(table2), 3, "i64x3 LOAD primary 2");
  V={3}; LOAD_OK(primary, i64i64i64, N(table2), 6, "i64x3 LOAD primary 3");
  V={4}; LOAD_OK(primary, i64i64i64, N(table2), 7, "i64x3 LOAD primary 4");
  V={5}; LOAD_OK(primary, i64i64i64, N(table2), 9, "i64x3 LOAD primary 5");
  V={6}; LOAD_ER(primary, i64i64i64, N(table2), "i64x3 LOAD primary fail 6");
  
  V={11,0}; LOAD_OK(secondary, i64i64i64, N(table2), 7, "i64x3 LOAD secondary 0");
  V={11,1}; LOAD_OK(secondary, i64i64i64, N(table2), 0, "i64x3 LOAD secondary 1");
  V={11,2}; LOAD_OK(secondary, i64i64i64, N(table2),10, "i64x3 LOAD secondary 2");
  V={11,3}; LOAD_ER(secondary, i64i64i64, N(table2), "i64x3 LOAD secondary fail 3");
  V={11,4}; LOAD_ER(secondary, i64i64i64, N(table2), "i64x3 LOAD secondary fail 4");
  V={11,5}; LOAD_OK(secondary, i64i64i64, N(table2), 8, "i64x3 LOAD secondary 5");
  V={11,6}; LOAD_ER(secondary, i64i64i64, N(table2), "i64x3 LOAD secondary fail 6");

  V={11,12,0}; LOAD_OK(tertiary, i64i64i64, N(table2),  0, "i64x3 LOAD tertiary 0");
  V={11,12,1}; LOAD_OK(tertiary, i64i64i64, N(table2),  1, "i64x3 LOAD tertiary 1");
  V={11,12,2}; LOAD_OK(tertiary, i64i64i64, N(table2),  2, "i64x3 LOAD tertiary 2");
  V={11,12,3}; LOAD_OK(tertiary, i64i64i64, N(table2),  3, "i64x3 LOAD tertiary 3");
  V={11,12,4}; LOAD_ER(tertiary, i64i64i64, N(table2), "i64x3 LOAD tertiary 4");
  V={11,12,5}; LOAD_OK(tertiary, i64i64i64, N(table2),  5, "i64x3 LOAD tertiary 5");
  V={11,12,6}; LOAD_OK(tertiary, i64i64i64, N(table2),  6, "i64x3 LOAD tertiary 6");
  V={11,12,7}; LOAD_OK(tertiary, i64i64i64, N(table2),  7, "i64x3 LOAD tertiary 7");
  V={11,12,8}; LOAD_OK(tertiary, i64i64i64, N(table2),  8, "i64x3 LOAD tertiary 8");
  V={11,12,9}; LOAD_OK(tertiary, i64i64i64, N(table2),  9, "i64x3 LOAD tertiary 9");
  V={11,12,10}; LOAD_ER(tertiary, i64i64i64, N(table2), "i64x3 LOAD tertiary 10");
  V={11,12,11}; LOAD_ER(tertiary, i64i64i64, N(table2), "i64x3 LOAD tertiary fail 11");

  #define NEXT_ALL(I, O, T) \
  { \
    auto n = sizeof(I)/sizeof(I[0]); \
    auto j = 0; \
    do { \
      eosio::remove_reference<decltype(records[0])>::type tmp = records[I[j]]; \
      res = NEXT(I, i64i64i64, N(table2), tmp);\
      if(j+1<n){ TABLE1_ASSERT(I[j+1], tmp, "i64x3 NEXT " #I " ok "); } \
817
      else { eosio_assert(res == -1, "i64x3 NEXT " #I " fail "); }\
818 819 820 821 822 823 824 825 826 827 828
    } while(++j<n); \
  }

  #define PREV_ALL(I, O, T) \
  { \
    auto n = sizeof(I)/sizeof(I[0]); \
    auto j = n-1; \
    do { \
      eosio::remove_reference<decltype(records[0])>::type tmp = records[I[j]]; \
      res = PREV(I, i64i64i64, N(table2), tmp);\
      if(j>0){ TABLE1_ASSERT(I[j-1], tmp, "i64x3 PREV " #I " ok "); } \
829
      else { eosio_assert(res == -1, "i64x3 PREV " #I " fail "); }\
830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870
    } while(--j>0); \
  }

  NEXT_ALL(primary,   i64i64i64, N(table2));
  NEXT_ALL(secondary, i64i64i64, N(table2));
  NEXT_ALL(tertiary,  i64i64i64, N(table2));

  PREV_ALL(primary,   i64i64i64, N(table2));
  PREV_ALL(secondary, i64i64i64, N(table2));
  PREV_ALL(tertiary,  i64i64i64, N(table2));

  FRONT_OK(primary, i64i64i64, N(table2), primary[0],   "i64x3 FRONT primary");
  FRONT_OK(secondary, i64i64i64, N(table2), secondary[0], "i64x3 FRONT secondary");
  FRONT_OK(tertiary, i64i64i64, N(table2), tertiary[0], "i64x3 FRONT tertiary");

  BACK_OK(primary, i64i64i64, N(table2), primary[10],   "i64x3 BACK primary");
  BACK_OK(secondary, i64i64i64, N(table2), secondary[10], "i64x3 BACK secondary");
  BACK_OK(tertiary, i64i64i64, N(table2), tertiary[10], "i64x3 BACK tertiary");

  #define LOWER_ALL(I, O, T) \
  { \
    auto n = sizeof(I##_lb)/sizeof(I##_lb[0]); \
    auto j = 0; \
    do { \
      eosio::remove_reference<decltype(records[0])>::type tmp = records[j]; \
      res = LOWER(I, i64i64i64, N(table2), tmp);\
      TABLE1_ASSERT(I##_lb[j], tmp, "i64x3 LOWER " #I " ok ");\
    } while(++j<n); \
  }

  LOWER_ALL(primary,   i64i64i64, N(table2));
  LOWER_ALL(secondary, i64i64i64, N(table2));
  LOWER_ALL(tertiary,  i64i64i64, N(table2));

  #define UPPER_ALL(I, O, T) \
  { \
    auto n = sizeof(I##_ub)/sizeof(I##_ub[0]); \
    auto j = 0; \
    do { \
      eosio::remove_reference<decltype(records[0])>::type tmp = records[j]; \
      res = UPPER(I, i64i64i64, N(table2), tmp);\
871
      if(res == -1) { eosio_assert(I##_ub[j]==-1,"i64x3 UPPER " #I " fail ") } \
872 873 874 875 876 877 878 879 880 881 882 883
      else { TABLE1_ASSERT(I##_ub[j], tmp, "i64x3 UPPER " #I " ok "); } \
    } while(++j<n); \
  }

  UPPER_ALL(primary,   i64i64i64, N(table2));
  UPPER_ALL(secondary, i64i64i64, N(table2));
  UPPER_ALL(tertiary,  i64i64i64, N(table2));

  TestModel3xi64_V2 v2;
  v2.a = records[6].a;

  res = LOAD(primary, i64i64i64, N(table2), v2);
884
  eosio_assert(res == sizeof(TestModel3xi64), "load v2");
885 886 887 888

  v2.new_field = 555;

  res = update_i64i64i64(current_receiver(),  N(table2), &v2, sizeof(TestModel3xi64_V2));
889
  eosio_assert(res == 1, "store v2");  
890 891

  res = LOAD(primary, i64i64i64, N(table2), v2);
892
  eosio_assert(res == sizeof(TestModel3xi64_V2), "load v2 updated");
893 894

  res = remove_i64i64i64(current_receiver(),  N(table2), &v2);
895
  eosio_assert(res == 1, "load v2 updated");
896 897

  res = LOAD(primary, i64i64i64, N(table2), v2);
898
  eosio_assert(res == -1, "load not found");
899 900 901

  return 0;
}
B
Bucky Kittinger 已提交
902
#endif 
B
Bucky Kittinger 已提交
903
void test_db::key_i128i128_general() {
904 905

  uint32_t res = 0;
B
Bucky Kittinger 已提交
906
  /*
907
  if(store_set_in_table(N(table4)) != 0)
908
     eosio_assert(false, "store_set_in_table(N(table4)) != 0 (test_db::key_i128i128_general)");
B
Bucky Kittinger 已提交
909 910 911
     */
store_set_in_table(N(table4));
return;
912
  if(store_set_in_table(N(table5)) != 0)
913
     eosio_assert(false, "store_set_in_table(N(table5)) != 0 (test_db::key_i128i128_general)");
914 915

  if(store_set_in_table(N(table6)) != 0)
916
     eosio_assert(false, "store_set_in_table(N(table6)) != 0 (test_db::key_i128i128_general)");
917 918 919 920 921 922

  TestModel128x2 tmp;
  my_memset(&tmp, 0, sizeof(TestModel128x2));
  tmp.number = 21;

  res = load_primary_i128i128( current_receiver(), current_receiver(), N(table5), &tmp, sizeof(TestModel128x2) );
923
  eosio_assert( res == sizeof(TestModel128x2) &&
924 925 926 927 928 929 930 931 932
               tmp.price == 800 &&
               tmp.extra == N(carol1) &&
               tmp.table_name == N(table5),
              "carol1 primary load");

  my_memset(&tmp, 0, sizeof(TestModel128x2));
  tmp.price = 4;
  
  res = load_secondary_i128i128( current_receiver(), current_receiver(), N(table5), &tmp, sizeof(TestModel128x2) );
933
  eosio_assert( res == sizeof(TestModel128x2) &&
934 935 936 937 938 939 940
               tmp.number == 13 &&
               tmp.price == 4 &&
               tmp.extra == N(bob3) &&
               tmp.table_name == N(table5),
              "bob3 secondary load");

  res = front_primary_i128i128( current_receiver(), current_receiver(), N(table5), &tmp, sizeof(TestModel128x2) );
941
  eosio_assert( res == sizeof(TestModel128x2) &&
942 943 944 945 946 947 948
               tmp.number == 0 &&
               tmp.price == 500 &&
               tmp.extra == N(alice0) &&
               tmp.table_name == N(table5),
              "front primary load");
  
  res = previous_primary_i128i128( current_receiver(), current_receiver(), N(table5), &tmp, sizeof(TestModel128x2) );
949
  eosio_assert(res == -1, "previous primary fail");
950 951

  res = next_primary_i128i128( current_receiver(), current_receiver(), N(table5), &tmp, sizeof(TestModel128x2) );
952
  eosio_assert( res == sizeof(TestModel128x2) &&
953 954 955 956 957 958 959
               tmp.number == 1 &&
               tmp.price == 400 &&
               tmp.extra == N(alice1) &&
               tmp.table_name == N(table5),
              "next primary ok");

  res = front_secondary_i128i128( current_receiver(), current_receiver(), N(table5), &tmp, sizeof(TestModel128x2) );
960
  eosio_assert( res == sizeof(TestModel128x2) &&
961 962 963 964 965 966 967
               tmp.number == 10 &&
               tmp.price == 1 &&
               tmp.extra == N(bob0) &&
               tmp.table_name == N(table5),
              "front secondary ok");
  
  res = previous_secondary_i128i128( current_receiver(), current_receiver(), N(table5), &tmp, sizeof(TestModel128x2) );
968
  eosio_assert(res == -1, "previous secondary fail");
969 970

  res = next_secondary_i128i128( current_receiver(), current_receiver(), N(table5), &tmp, sizeof(TestModel128x2) );
971
  eosio_assert( res == sizeof(TestModel128x2) &&
972 973 974 975 976 977 978
               tmp.number == 11 &&
               tmp.price == 2 &&
               tmp.extra == N(bob1) &&
               tmp.table_name == N(table5),
              "next secondary ok");

  res = back_primary_i128i128( current_receiver(), current_receiver(), N(table5), &tmp, sizeof(TestModel128x2) );
979
  eosio_assert( res == sizeof(TestModel128x2) &&
980 981 982 983 984 985 986
               tmp.number == 33 &&
               tmp.price == 4 &&
               tmp.extra == N(dave3) &&
               tmp.table_name == N(table5),
              "back primary ok");
  
  res = next_primary_i128i128( current_receiver(), current_receiver(), N(table5), &tmp, sizeof(TestModel128x2) );
987
  eosio_assert(res == -1, "next primary fail");
988 989

  res = previous_primary_i128i128( current_receiver(), current_receiver(), N(table5), &tmp, sizeof(TestModel128x2) );
990
  eosio_assert( res == sizeof(TestModel128x2) &&
991 992 993 994 995 996 997
               tmp.number == 32 &&
               tmp.price == 5 &&
               tmp.extra == N(dave2) &&
               tmp.table_name == N(table5),
              "previous primary ok");

  res = back_secondary_i128i128( current_receiver(), current_receiver(), N(table5), &tmp, sizeof(TestModel128x2) );
998
  eosio_assert( res == sizeof(TestModel128x2) &&
999 1000 1001 1002 1003 1004 1005
               tmp.number == 20 &&
               tmp.price == 900 &&
               tmp.extra == N(carol0) &&
               tmp.table_name == N(table5),
              "back secondary ok");
  
  res = next_secondary_i128i128( current_receiver(), current_receiver(), N(table5), &tmp, sizeof(TestModel128x2) );
1006
  eosio_assert(res == -1, "next secondary fail");
1007 1008 1009

  res = previous_secondary_i128i128( current_receiver(), current_receiver(), N(table5), &tmp, sizeof(TestModel128x2) );
  
1010
  eosio_assert( res == sizeof(TestModel128x2) &&
1011 1012 1013 1014 1015 1016 1017 1018 1019
               tmp.number == 21 &&
               tmp.price == 800 &&
               tmp.extra == N(carol1) &&
               tmp.table_name == N(table5),
              "previous secondary ok");

  tmp.number = 1;

  res = lower_bound_primary_i128i128( current_receiver(), current_receiver(), N(table5), &tmp, sizeof(TestModel128x2) );
1020
  eosio_assert( res == sizeof(TestModel128x2) &&
1021 1022 1023 1024 1025 1026 1027
               tmp.number == 1 &&
               tmp.price == 400 &&
               tmp.extra == N(alice1) &&
               tmp.table_name == N(table5),
              "lb primary ok");

  res = upper_bound_primary_i128i128( current_receiver(), current_receiver(), N(table5), &tmp, sizeof(TestModel128x2) );
1028
  eosio_assert( res == sizeof(TestModel128x2) &&
1029 1030 1031 1032 1033 1034 1035 1036 1037
               tmp.number == 2 &&
               tmp.price == 200 &&
               tmp.extra == N(alice22) &&
               tmp.table_name == N(table5),
              "ub primary ok");

  tmp.price = 800;

  res = lower_bound_secondary_i128i128( current_receiver(), current_receiver(), N(table5), &tmp, sizeof(TestModel128x2) );
1038
  eosio_assert( res == sizeof(TestModel128x2) &&
1039 1040 1041 1042 1043 1044 1045 1046 1047 1048
               tmp.number == 21 &&
               tmp.price == 800 &&
               tmp.extra == N(carol1) &&
               tmp.table_name == N(table5),
              "lb secondary ok");

  TestModel128x2_V2 tmp2;
  tmp2.price = 800;

  res = upper_bound_secondary_i128i128( current_receiver(), current_receiver(), N(table5), &tmp2, sizeof(TestModel128x2_V2) );
1049
  eosio_assert( res == sizeof(TestModel128x2) &&
1050 1051 1052 1053 1054 1055 1056 1057
               tmp2.number == 20 &&
               tmp2.price == 900 &&
               tmp2.extra == N(carol0) &&
               tmp2.table_name == N(table5),
              "ub secondary ok");
  
  tmp2.new_field = 123456;
  res = update_i128i128(current_receiver(), N(table5), &tmp2, sizeof(TestModel128x2_V2));
1058
  eosio_assert( res == 1, "update_i128i128 ok");
1059 1060 1061 1062 1063

  my_memset(&tmp2, 0, sizeof(TestModel128x2_V2));
  tmp2.number = 20;

  res = load_primary_i128i128(current_receiver(), current_receiver(), N(table5), &tmp2, sizeof(TestModel128x2_V2));
1064
  eosio_assert( res == sizeof(TestModel128x2_V2) &&
1065 1066 1067 1068 1069 1070 1071 1072 1073
               tmp2.number == 20 &&
               tmp2.price == 900 &&
               tmp2.extra == N(carol0) &&
               tmp2.table_name == N(table5) &&
               tmp2.new_field == 123456,
              "lp update_i128i128 ok");

  tmp2.extra = N(xxxxx);
  res = update_i128i128(current_receiver(), N(table5), &tmp2, sizeof(uint128_t)*2+sizeof(uint64_t));
1074
  eosio_assert( res == 1, "update_i128i128 small ok");
1075 1076

  res = load_primary_i128i128(current_receiver(), current_receiver(), N(table5), &tmp2, sizeof(TestModel128x2_V2));
1077
  eosio_assert( res == sizeof(TestModel128x2_V2) &&
1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094
               tmp2.number == 20 &&
               tmp2.price == 900 &&
               tmp2.extra == N(xxxxx) &&
               tmp2.table_name == N(table5) &&
               tmp2.new_field == 123456,
              "lp small update_i128i128 ok");
}

void set_key_str(int i, char* key_4_digit)
{
   const char nums[] = "0123456789";
   key_4_digit[0] = nums[(i % 10000) / 1000];
   key_4_digit[1] = nums[(i % 1000) / 100];
   key_4_digit[2] = nums[(i % 100) / 10];
   key_4_digit[3] = nums[(i % 10)];
}

B
Bucky Kittinger 已提交
1095
void test_db::key_str_setup_limit()
1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116
{
   // assuming max memory: 5 MBytes
   // assuming row overhead: 16 Bytes
   // key length: 30 bytes
   // value length: 2498 bytes
   // -> key + value bytes: 2528 Bytes
   // 1024 * 2 * (2528 + 32)
   char key[] = "0000abcdefghijklmnopqrstuvwxy";
   const uint32_t value_size = 2498;
   char* value = static_cast<char*>(eosio::malloc(value_size));
   value[4] = '\0';
   for(int i = 0; i < 1024 * 2; ++i)
   {
      set_key_str(i, key);
      // set the value with the same prefix to be able to identify
      set_key_str(i, value);
      store_str(N(dblimits), N(dblstr), key, sizeof(key), value, value_size);
   }
   eosio::free(value);
}

B
Bucky Kittinger 已提交
1117
void test_db::key_str_min_exceed_limit()
1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129
{
   char key = '1';
   char value = '1';
   // assuming max memory: 5 MBytes
   // assuming row overhead: 16 Bytes
   // key length: 1 bytes
   // value length: 1 bytes
   // -> key + value bytes: 8 Bytes
   // 8 + 32 = 40 Bytes (not enough space)
   store_str(N(dblimits), N(dblstr), &key, 1, &value, 1);
}

B
Bucky Kittinger 已提交
1130
void test_db::key_str_under_limit()
1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151
{
   // assuming max memory: 5 MBytes
   // assuming row overhead: 16 Bytes
   // key length: 30 bytes
   // value length: 2489 bytes
   // -> key + value bytes: 2520 Bytes
   // 1024 * 2 * (2520 + 32) = 5,226,496 => 16K bytes remaining
   char key[] = "0000abcdefghijklmnopqrstuvwxy";
   const uint32_t value_size = 2489;
   char* value = static_cast<char*>(eosio::malloc(value_size));
   value[4] = '\0';
   for(int i = 0; i < 1024 * 2; ++i)
   {
      set_key_str(i, key);
      // set the value with the same prefix to be able to identify
      set_key_str(i, value);
      store_str(N(dblimits), N(dblstr), key, sizeof(key), value, value_size);
   }
   eosio::free(value);
}

B
Bucky Kittinger 已提交
1152
void test_db::key_str_available_space_exceed_limit()
1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165
{
   // key length: 30 bytes
   // value length: 16323 bytes
   // -> key + value bytes: 16360 Bytes (rounded to byte boundary)
   // 16,392 Bytes => exceeds 16K bytes remaining
   char key[] = "0000abcdefghijklmnopqrstuvwxy";
   set_key_str(9999, key);
   const uint32_t value_size = 16323;
   char* value = static_cast<char*>(eosio::malloc(value_size));
   store_str(N(dblimits), N(dblstr), key, sizeof(key), value, value_size);
   eosio::free(value);
}

B
Bucky Kittinger 已提交
1166
void test_db::key_str_another_under_limit()
1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197
{
   // 16K bytes remaining
   // key length: 30 bytes
   // value length: 18873 bytes
   // -> key + value bytes: 18904 Bytes (rounded to byte boundary)
   // 16,384 Bytes => just under 16K bytes remaining
   char key[] = "0000abcdefghijklmnopqrstuvwxy";
   set_key_str(0, key);
   uint32_t value_size = 18873;
   char* value = static_cast<char*>(eosio::malloc(value_size));
   update_str(N(dblimits), N(dblstr), key, sizeof(key), value, value_size);
   // 0 bytes remaining

   // key length: 30 bytes
   // value length: 2489 bytes
   // -> key + value bytes: 2520 Bytes
   set_key_str(1, key);
   remove_str(N(dblimits), N(dblstr), key, sizeof(key));
   // 2,552 Bytes remaining

   // leave too little room for 32 Byte overhead + (key + value = 8 Byte min)
   // key length: 30 bytes
   // value length: 4909 bytes
   // -> key + value bytes: 5040 Bytes
   value_size = 2489 + 2514;
   set_key_str(2, key);
   value = static_cast<char*>(eosio::realloc(value, value_size));
   update_str(N(dblimits), N(dblstr), key, sizeof(key), value, value_size);
   eosio::free(value);
}

B
Bucky Kittinger 已提交
1198
void test_db::key_i64_setup_limit()
1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215
{
   // assuming max memory: 5M Bytes
   // assuming row overhead: 16 Bytes
   // key length: 8 bytes
   // value length: 315 * 8 bytes (rounded to byte boundary)
   // -> key + value bytes: 2528 Bytes
   // 1024 * 2 * (2528 + 32) = 5,242,880
   const uint64_t value_size = 315 * sizeof(uint64_t) + 1;
   auto value = (uint64_t*)eosio::malloc(value_size);
   for(int i = 0; i < 1024 * 2; ++i)
   {
      value[0] = i;
      store_i64(N(dblimits), N(dbli64), (char*)value, value_size);
   }
   eosio::free(value);
}

B
Bucky Kittinger 已提交
1216
void test_db::key_i64_min_exceed_limit()
1217 1218 1219 1220 1221 1222 1223
{
   // will allocate 8 + 32 Bytes
   // at 5M Byte limit, so cannot store anything
   uint64_t value = (uint64_t)-1;
   store_i64(N(dblimits), N(dbli64), (char*)&value, sizeof(uint64_t));
}

B
Bucky Kittinger 已提交
1224
void test_db::key_i64_under_limit()
1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241
{
   // updating keys' values
   // key length: 8 bytes
   // value length: 299 * 8 bytes
   // -> key + value bytes: 2400 Bytes
   // 1024 * 2 * (2400 + 32) = 4,980,736
   const uint64_t value_size = 300 * sizeof(uint64_t);
   auto value = (uint64_t*)eosio::malloc(value_size);
   for(int i = 0; i < 1024 * 2; ++i)
   {
      value[0] = i;
      store_i64(N(dblimits), N(dbli64), (char*)value, value_size);
   }
   // 262,144 Bytes remaining
   eosio::free(value);
}

B
Bucky Kittinger 已提交
1242
void test_db::key_i64_available_space_exceed_limit()
1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255
{
   // 262,144 Bytes remaining
   // key length: 8 bytes
   // value length: 32764 * 8 bytes
   // -> key + value bytes: 262,120 Bytes
   // storing 262,152 Bytes exceeds remaining
   const uint64_t value_size = 32765 * sizeof(uint64_t);
   auto value = (uint64_t*)eosio::malloc(value_size);
   value[0] = 1024 * 2;
   store_i64(N(dblimits), N(dbli64), (char*)value, value_size);
   eosio::free(value);
}

B
Bucky Kittinger 已提交
1256
void test_db::key_i64_another_under_limit()
1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289
{
   // 262,144 Bytes remaining
   // key length: 8 bytes
   // value length: 33067 * 8 bytes (rounded to byte boundary)
   // -> key + value bytes: 264,544 Bytes
   // replacing storage bytes so 264,544 - 2400 = 262,144 Bytes (0 Bytes remaining)
   uint64_t value_size = 33067 * sizeof(uint64_t) + 7;
   auto value = (uint64_t*)eosio::malloc(value_size);
   value[0] = 15;
   update_i64(N(dblimits), N(dbli64), (char*)value, value_size);

   // 0 Bytes remaining
   // key length: 8 bytes
   // previous value length: 299 * 8 bytes
   // -> key + value bytes: 2400 Bytes
   // free up 2,432 Bytes
   value[0] = 14;
   remove_i64(N(dblimits), N(dbli64), (char*)value);

   // 2,432 Bytes remaining
   // key length: 8 bytes
   // previous value length: 294 * 8 bytes (rounded to byte boundary)
   // -> key + value bytes: 2,368 Bytes
   // 2,400 Bytes allocated
   value_size = 295 * sizeof(uint64_t) + 3;
   value = (uint64_t*)eosio::realloc(value, value_size);
   value[0] = 1024 * 2;
   store_i64(N(dblimits), N(dbli64), (char*)value, value_size);
   // 32 Bytes remaining (smallest row entry is 40 Bytes)

   eosio::free(value);
}

B
Bucky Kittinger 已提交
1290
void test_db::key_i128i128_setup_limit()
1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308
{
   // assuming max memory: 5M Bytes
   // assuming row overhead: 16 Bytes
   // keys length: 32 bytes
   // value length: 312 * 8 bytes (rounded to byte boundary)
   // -> key + value bytes: 2528 Bytes
   // 1024 * 2 * (2528 + 32) = 5,242,880
   const uint64_t value_size = 315 * sizeof(uint64_t) + 1;
   auto value = (uint128_t*)eosio::malloc(value_size);
   for(int i = 0; i < 1024 * 2; ++i)
   {
      value[0] = i;
      value[1] = value[0] + 1;
      store_i128i128(N(dblimits), N(dbli128i128), (char*)value, value_size);
   }
   eosio::free(value);
}

B
Bucky Kittinger 已提交
1309
void test_db::key_i128i128_min_exceed_limit()
1310 1311 1312 1313 1314 1315 1316 1317 1318 1319
{
   // will allocate 32 + 32 Bytes
   // at 5M Byte limit, so cannot store anything
   const uint64_t value_size = 2 * sizeof(uint128_t);
   auto value = (uint128_t*)eosio::malloc(value_size);
   value[0] = (uint128_t)-1;
   value[1] = value[0] + 1;
   store_i128i128(N(dblimits), N(dbli128i128), (char*)&value, value_size);
}

B
Bucky Kittinger 已提交
1320
void test_db::key_i128i128_under_limit()
1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338
{
   // updating keys' values
   // keys length: 32 bytes
   // value length: 296 * 8 bytes
   // -> key + value bytes: 2400 Bytes
   // 1024 * 2 * (2400 + 32) = 4,980,736
   const uint64_t value_size = 300 * sizeof(uint64_t);
   auto value = (uint128_t*)eosio::malloc(value_size);
   for(int i = 0; i < 1024 * 2; ++i)
   {
      value[0] = i;
      value[1] = value[0] + 1;
      store_i128i128(N(dblimits), N(dbli128i128), (char*)value, value_size);
   }
   // 262,144 Bytes remaining
   eosio::free(value);
}

B
Bucky Kittinger 已提交
1339
void test_db::key_i128i128_available_space_exceed_limit()
1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353
{
   // 262,144 Bytes remaining
   // keys length: 32 bytes
   // value length: 32761 * 8 bytes
   // -> key + value bytes: 262,120 Bytes
   // storing 262,152 Bytes exceeds remaining
   const uint64_t value_size = 32765 * sizeof(uint64_t);
   auto value = (uint128_t*)eosio::malloc(value_size);
   value[0] = 1024 * 2;
   value[1] = value[0] + 1;
   store_i128i128(N(dblimits), N(dbli128i128), (char*)value, value_size);
   eosio::free(value);
}

B
Bucky Kittinger 已提交
1354
void test_db::key_i128i128_another_under_limit()
1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391
{
   // 262,144 Bytes remaining
   // keys length: 32 bytes
   // value length: 33064 * 8 bytes (rounded to byte boundary)
   // -> key + value bytes: 264,544 Bytes
   // replacing storage bytes so 264,544 - 2400 = 262,144 Bytes (0 Bytes remaining)
   uint64_t value_size = 33067 * sizeof(uint64_t) + 7;
   auto value = (uint128_t*)eosio::malloc(value_size);
   value[0] = 15;
   value[1] = value[0] + 1;
   update_i128i128(N(dblimits), N(dbli128i128), (char*)value, value_size);

   // 0 Bytes remaining
   // keys length: 32 bytes
   // previous value length: 296 * 8 bytes
   // -> key + value bytes: 2400 Bytes
   // free up 2,432 Bytes
   value[0] = 14;
   value[1] = value[0] + 1;
   remove_i128i128(N(dblimits), N(dbli128i128), (char*)value);

   // 2,432 Bytes remaining
   // keys length: 32 bytes
   // previous value length: 288 * 8 bytes (rounded to byte boundary)
   // -> key + value bytes: 2,344 Bytes
   // 2,376 Bytes allocated
   value_size = 292 * sizeof(uint64_t) + 3;
   value = (uint128_t*)eosio::realloc(value, value_size);
   value[0] = 1024 * 2;
   value[1] = value[0] + 1;
   store_i128i128(N(dblimits), N(dbli128i128), (char*)value, value_size);
   // 56 Bytes remaining (smallest row entry is 64 Bytes)

   eosio::free(value);

}

B
Bucky Kittinger 已提交
1392
void test_db::key_i64i64i64_setup_limit()
1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411
{
   // assuming max memory: 5M Bytes
   // assuming row overhead: 16 Bytes
   // keys length: 24 bytes
   // value length: 313 * 8 bytes (rounded to byte boundary)
   // -> key + value bytes: 2528 Bytes
   // 1024 * 2 * (2528 + 32) = 5,242,880
   const uint64_t value_size = 315 * sizeof(uint64_t) + 1;
   auto value = (uint64_t*)eosio::malloc(value_size);
   for(int i = 0; i < 1024 * 2; ++i)
   {
      value[0] = i;
      value[1] = value[0] + 1;
      value[2] = value[0] + 2;
      store_i64i64i64(N(dblimits), N(dbli64i64i64), (char*)value, value_size);
   }
   eosio::free(value);
}

B
Bucky Kittinger 已提交
1412
void test_db::key_i64i64i64_min_exceed_limit()
1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423
{
   // will allocate 24 + 32 Bytes
   // at 5M Byte limit, so cannot store anything
   const uint64_t value_size = 3 * sizeof(uint64_t);
   auto value = (uint64_t*)eosio::malloc(value_size);
   value[0] = (uint64_t)-1;
   value[1] = value[0] + 1;
   value[2] = value[0] + 2;
   store_i64i64i64(N(dblimits), N(dbli64i64i64), (char*)&value, value_size);
}

B
Bucky Kittinger 已提交
1424
void test_db::key_i64i64i64_under_limit()
1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443
{
   // updating keys' values
   // keys length: 24 bytes
   // value length: 297 * 8 bytes
   // -> key + value bytes: 2400 Bytes
   // 1024 * 2 * (2400 + 32) = 4,980,736
   const uint64_t value_size = 300 * sizeof(uint64_t);
   auto value = (uint64_t*)eosio::malloc(value_size);
   for(int i = 0; i < 1024 * 2; ++i)
   {
      value[0] = i;
      value[1] = value[0] + 1;
      value[2] = value[0] + 2;
      store_i64i64i64(N(dblimits), N(dbli64i64i64), (char*)value, value_size);
   }
   // 262,144 Bytes remaining
   eosio::free(value);
}

B
Bucky Kittinger 已提交
1444
void test_db::key_i64i64i64_available_space_exceed_limit()
1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459
{
   // 262,144 Bytes remaining
   // keys length: 24 bytes
   // value length: 32762 * 8 bytes
   // -> key + value bytes: 262,120 Bytes
   // storing 262,152 Bytes exceeds remaining
   const uint64_t value_size = 32765 * sizeof(uint64_t);
   auto value = (uint64_t*)eosio::malloc(value_size);
   value[0] = 1024 * 2;
   value[1] = value[0] + 1;
   value[2] = value[0] + 2;
   store_i64i64i64(N(dblimits), N(dbli64i64i64), (char*)value, value_size);
   eosio::free(value);
}

B
Bucky Kittinger 已提交
1460
void test_db::key_i64i64i64_another_under_limit()
1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499
{
   // 262,144 Bytes remaining
   // keys length: 24 bytes
   // value length: 33065 * 8 bytes (rounded to byte boundary)
   // -> key + value bytes: 264,544 Bytes
   // replacing storage bytes so 264,544 - 2400 = 262,144 Bytes (0 Bytes remaining)
   uint64_t value_size = 33067 * sizeof(uint64_t) + 7;
   auto value = (uint64_t*)eosio::malloc(value_size);
   value[0] = 15;
   value[1] = value[0] + 1;
   value[2] = value[0] + 2;
   update_i64i64i64(N(dblimits), N(dbli64i64i64), (char*)value, value_size);

   // 0 Bytes remaining
   // keys length: 24 bytes
   // previous value length: 297 * 8 bytes
   // -> key + value bytes: 2400 Bytes
   // free up 2,432 Bytes
   value[0] = 14;
   value[1] = value[0] + 1;
   value[2] = value[0] + 2;
   remove_i64i64i64(N(dblimits), N(dbli64i64i64), (char*)value);

   // 2,432 Bytes remaining
   // keys length: 24 bytes
   // previous value length: 290 * 8 bytes (rounded to byte boundary)
   // -> key + value bytes: 2,352 Bytes
   // 2,384 Bytes allocated
   value_size = 295 * sizeof(uint64_t) + 3;
   value = (uint64_t*)eosio::realloc(value, value_size);
   value[0] = 1024 * 2;
   value[1] = value[0] + 1;
   value[2] = value[0] + 2;
   store_i64i64i64(N(dblimits), N(dbli64i64i64), (char*)value, value_size);
   // 48 Bytes remaining (smallest row entry is 56 Bytes)

   eosio::free(value);

}
1500
#endif