SDB_API ut64 sdb_array_pop_num(Sdb *s, const char *key, ut32 *cas) { ut64 ret; char *a = sdb_array_pop (s, key, cas); if (!a) { if (cas) *cas = UT32_MAX; // invalid return UT64_MAX; } if (cas) *cas = 0; ret = sdb_atoi (a); free (a); return ret; }
bool test_sdb_array_push_pop(void) { Sdb *db = sdb_new (NULL, NULL, false); char *p; sdb_array_push (db, "foo", "foo", 0); sdb_array_push (db, "foo", "bar", 0); sdb_array_push (db, "foo", "cow", 0); mu_assert_streq (sdb_const_get (db, "foo", 0), "cow,bar,foo", "Not all items found"); p = sdb_array_pop (db, "foo", NULL); mu_assert_streq (p, "cow", "cow was not at the top"); p = sdb_array_pop (db, "foo", NULL); mu_assert_streq (p, "bar", "bar was not at the top"); p = sdb_array_pop (db, "foo", NULL); mu_assert_streq (p, "foo", "foo was not at the top"); p = sdb_array_pop (db, "foo", NULL); mu_assert_eq ((int)(size_t)p, (int)(size_t)NULL, "there shouldn't be any element in the array"); sdb_free (db); mu_end; }
int main(int argc, char **argv) { int i, N; if (argc>1) N = atoi (argv[1]); else N = 1000; Sdb *s = sdb_new (NULL, NULL, 0); for (i=0;i<N;i++) sdb_array_push (s, "key", "foo", 0); #if 1 if (N != sdb_alen (sdb_const_get (s, "key", 0))) { printf ("FAIL IN VERY PUSH\n"); return 1; } eprintf ("--%d\n", (int)strlen (sdb_const_get (s, "key", 0))); #endif for (i=0;i<N;i++) free (sdb_array_pop (s, "key", 0)); sdb_free (s); return 0; }