2014-10-09 03:16:19 -04:00
|
|
|
#include <ruby.h>
|
|
|
|
#include <ruby/st.h>
|
|
|
|
|
|
|
|
static st_data_t expect_size = 32;
|
|
|
|
struct checker {
|
|
|
|
st_table *tbl;
|
|
|
|
st_index_t nr;
|
|
|
|
VALUE test;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
|
|
|
force_unpack_check(struct checker *c, st_data_t key, st_data_t val)
|
|
|
|
{
|
|
|
|
if (c->nr == 0) {
|
|
|
|
st_data_t i;
|
|
|
|
|
2016-11-06 19:45:00 -05:00
|
|
|
if (c->tbl->bins != NULL) rb_bug("should be packed\n");
|
2014-10-09 03:16:19 -04:00
|
|
|
|
|
|
|
/* force unpacking during iteration: */
|
|
|
|
for (i = 1; i < expect_size; i++)
|
|
|
|
st_add_direct(c->tbl, i, i);
|
|
|
|
|
2016-11-06 19:45:00 -05:00
|
|
|
if (c->tbl->bins == NULL) rb_bug("should be unpacked\n");
|
2014-10-09 03:16:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (key != c->nr) {
|
2014-11-03 00:36:18 -05:00
|
|
|
rb_bug("unexpected key: %"PRIuVALUE" (expected %"PRIuVALUE")\n", (VALUE)key, (VALUE)c->nr);
|
2014-10-09 03:16:19 -04:00
|
|
|
}
|
|
|
|
if (val != c->nr) {
|
2014-11-03 00:36:18 -05:00
|
|
|
rb_bug("unexpected val: %"PRIuVALUE" (expected %"PRIuVALUE")\n", (VALUE)val, (VALUE)c->nr);
|
2014-10-09 03:16:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
c->nr++;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
unp_fec_i(st_data_t key, st_data_t val, st_data_t args, int error)
|
|
|
|
{
|
|
|
|
struct checker *c = (struct checker *)args;
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
if (c->test == ID2SYM(rb_intern("delete2")))
|
|
|
|
return ST_STOP;
|
|
|
|
|
|
|
|
rb_bug("unexpected error");
|
|
|
|
}
|
|
|
|
|
|
|
|
force_unpack_check(c, key, val);
|
|
|
|
|
|
|
|
if (c->test == ID2SYM(rb_intern("check"))) {
|
|
|
|
return ST_CHECK;
|
|
|
|
}
|
|
|
|
if (c->test == ID2SYM(rb_intern("delete1"))) {
|
|
|
|
if (c->nr == 1) return ST_DELETE;
|
|
|
|
return ST_CHECK;
|
|
|
|
}
|
|
|
|
if (c->test == ID2SYM(rb_intern("delete2"))) {
|
|
|
|
if (c->nr == 1) {
|
|
|
|
st_data_t k = 0;
|
|
|
|
st_data_t v;
|
2022-07-21 12:23:58 -04:00
|
|
|
|
2014-10-09 03:16:19 -04:00
|
|
|
if (!st_delete(c->tbl, &k, &v)) {
|
|
|
|
rb_bug("failed to delete\n");
|
|
|
|
}
|
|
|
|
if (v != 0) {
|
2014-11-03 00:36:18 -05:00
|
|
|
rb_bug("unexpected value deleted: %"PRIuVALUE" (expected 0)", (VALUE)v);
|
2014-10-09 03:16:19 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return ST_CHECK;
|
|
|
|
}
|
|
|
|
|
|
|
|
rb_raise(rb_eArgError, "unexpected arg: %+"PRIsVALUE, c->test);
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
unp_fec(VALUE self, VALUE test)
|
|
|
|
{
|
|
|
|
st_table *tbl = st_init_numtable();
|
|
|
|
struct checker c;
|
|
|
|
|
|
|
|
c.tbl = tbl;
|
|
|
|
c.nr = 0;
|
|
|
|
c.test = test;
|
|
|
|
|
|
|
|
st_add_direct(tbl, 0, 0);
|
|
|
|
|
2016-11-06 19:45:00 -05:00
|
|
|
if (tbl->bins != NULL) rb_bug("should still be packed\n");
|
2014-10-09 03:16:19 -04:00
|
|
|
|
|
|
|
st_foreach_check(tbl, unp_fec_i, (st_data_t)&c, -1);
|
|
|
|
|
|
|
|
if (c.test == ID2SYM(rb_intern("delete2"))) {
|
|
|
|
if (c.nr != 1) {
|
2014-11-03 00:36:18 -05:00
|
|
|
rb_bug("mismatched iteration: %"PRIuVALUE" (expected 1)\n", (VALUE)c.nr);
|
2014-10-09 03:16:19 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (c.nr != expect_size) {
|
2014-11-03 00:36:18 -05:00
|
|
|
rb_bug("mismatched iteration: %"PRIuVALUE" (expected %"PRIuVALUE")\n",
|
|
|
|
(VALUE)c.nr, (VALUE)expect_size);
|
2014-10-09 03:16:19 -04:00
|
|
|
}
|
|
|
|
|
2016-11-06 19:45:00 -05:00
|
|
|
if (tbl->bins == NULL) rb_bug("should be unpacked\n");
|
2014-10-09 03:16:19 -04:00
|
|
|
|
|
|
|
st_free_table(tbl);
|
|
|
|
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2019-08-26 03:06:40 -04:00
|
|
|
unp_fe_i(st_data_t key, st_data_t val, st_data_t args)
|
2014-10-09 03:16:19 -04:00
|
|
|
{
|
|
|
|
struct checker *c = (struct checker *)args;
|
|
|
|
|
|
|
|
force_unpack_check(c, key, val);
|
|
|
|
if (c->test == ID2SYM(rb_intern("unpacked"))) {
|
|
|
|
return ST_CONTINUE;
|
|
|
|
}
|
|
|
|
else if (c->test == ID2SYM(rb_intern("unpack_delete"))) {
|
|
|
|
if (c->nr == 1) {
|
|
|
|
st_data_t k = 0;
|
|
|
|
st_data_t v;
|
2022-07-21 12:23:58 -04:00
|
|
|
|
2014-10-09 03:16:19 -04:00
|
|
|
if (!st_delete(c->tbl, &k, &v)) {
|
|
|
|
rb_bug("failed to delete\n");
|
|
|
|
}
|
|
|
|
if (v != 0) {
|
2014-11-03 00:36:18 -05:00
|
|
|
rb_bug("unexpected value deleted: %"PRIuVALUE" (expected 0)", (VALUE)v);
|
2014-10-09 03:16:19 -04:00
|
|
|
}
|
|
|
|
return ST_CONTINUE;
|
|
|
|
}
|
|
|
|
rb_bug("should never get here\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
rb_raise(rb_eArgError, "unexpected arg: %+"PRIsVALUE, c->test);
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
unp_fe(VALUE self, VALUE test)
|
|
|
|
{
|
|
|
|
st_table *tbl = st_init_numtable();
|
|
|
|
struct checker c;
|
|
|
|
|
|
|
|
c.tbl = tbl;
|
|
|
|
c.nr = 0;
|
|
|
|
c.test = test;
|
|
|
|
|
|
|
|
st_add_direct(tbl, 0, 0);
|
|
|
|
|
2016-11-06 19:45:00 -05:00
|
|
|
if (tbl->bins != NULL) rb_bug("should still be packed\n");
|
2014-10-09 03:16:19 -04:00
|
|
|
|
|
|
|
st_foreach(tbl, unp_fe_i, (st_data_t)&c);
|
|
|
|
|
|
|
|
if (c.test == ID2SYM(rb_intern("unpack_delete"))) {
|
|
|
|
if (c.nr != 1) {
|
2014-11-03 00:36:18 -05:00
|
|
|
rb_bug("mismatched iteration: %"PRIuVALUE" (expected 1)\n", (VALUE)c.nr);
|
2014-10-09 03:16:19 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (c.nr != expect_size) {
|
2014-11-03 00:36:18 -05:00
|
|
|
rb_bug("mismatched iteration: %"PRIuVALUE" (expected %"PRIuVALUE"o)\n",
|
|
|
|
(VALUE)c.nr, (VALUE)expect_size);
|
2014-10-09 03:16:19 -04:00
|
|
|
}
|
|
|
|
|
2016-11-06 19:45:00 -05:00
|
|
|
if (tbl->bins == NULL) rb_bug("should be unpacked\n");
|
2014-10-09 03:16:19 -04:00
|
|
|
|
|
|
|
st_free_table(tbl);
|
|
|
|
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Init_foreach(void)
|
|
|
|
{
|
|
|
|
VALUE bug = rb_define_module("Bug");
|
|
|
|
rb_define_singleton_method(bug, "unp_st_foreach_check", unp_fec, 1);
|
|
|
|
rb_define_singleton_method(bug, "unp_st_foreach", unp_fe, 1);
|
|
|
|
}
|