| 1 |
#include <ruby.h> |
| 2 |
#include <ruby/st.h> |
| 3 |
|
| 4 |
static st_data_t expect_size = 32; |
| 5 |
struct checker { |
| 6 |
st_table *tbl; |
| 7 |
st_index_t nr; |
| 8 |
VALUE test; |
| 9 |
}; |
| 10 |
|
| 11 |
static void |
| 12 |
force_unpack_check(struct checker *c, st_data_t key, st_data_t val) |
| 13 |
{ |
| 14 |
if (c->nr == 0) { |
| 15 |
st_data_t i; |
| 16 |
|
| 17 |
if (c->tbl->bins != NULL) rb_bug("should be packed"); |
| 18 |
|
| 19 |
/* force unpacking during iteration: */ |
| 20 |
for (i = 1; i < expect_size; i++) |
| 21 |
st_add_direct(c->tbl, i, i); |
| 22 |
|
| 23 |
if (c->tbl->bins == NULL) rb_bug("should be unpacked"); |
| 24 |
} |
| 25 |
|
| 26 |
if (key != c->nr) { |
| 27 |
rb_bug("unexpected key: %"PRIuVALUE" (expected %"PRIuVALUE")", (VALUE)key, (VALUE)c->nr); |
| 28 |
} |
| 29 |
if (val != c->nr) { |
| 30 |
rb_bug("unexpected val: %"PRIuVALUE" (expected %"PRIuVALUE")", (VALUE)val, (VALUE)c->nr); |
| 31 |
} |
| 32 |
|
| 33 |
c->nr++; |
| 34 |
} |
| 35 |
|
| 36 |
static int |
| 37 |
unp_fec_i(st_data_t key, st_data_t val, st_data_t args, int error) |
| 38 |
{ |
| 39 |
struct checker *c = (struct checker *)args; |
| 40 |
|
| 41 |
if (error) { |
| 42 |
if (c->test == ID2SYM(rb_intern("delete2"))) |
| 43 |
return ST_STOP; |
| 44 |
|
| 45 |
rb_bug("unexpected error"); |
| 46 |
} |
| 47 |
|
| 48 |
force_unpack_check(c, key, val); |
| 49 |
|
| 50 |
if (c->test == ID2SYM(rb_intern("check"))) { |
| 51 |
return ST_CHECK; |
| 52 |
} |
| 53 |
if (c->test == ID2SYM(rb_intern("delete1"))) { |
| 54 |
if (c->nr == 1) return ST_DELETE; |
| 55 |
return ST_CHECK; |
| 56 |
} |
| 57 |
if (c->test == ID2SYM(rb_intern("delete2"))) { |
| 58 |
if (c->nr == 1) { |
| 59 |
st_data_t k = 0; |
| 60 |
st_data_t v; |
| 61 |
|
| 62 |
if (!st_delete(c->tbl, &k, &v)) { |
| 63 |
rb_bug("failed to delete"); |
| 64 |
} |
| 65 |
if (v != 0) { |
| 66 |
rb_bug("unexpected value deleted: %"PRIuVALUE" (expected 0)", (VALUE)v); |
| 67 |
} |
| 68 |
} |
| 69 |
return ST_CHECK; |
| 70 |
} |
| 71 |
|
| 72 |
rb_raise(rb_eArgError, "unexpected arg: %+"PRIsVALUE, c->test); |
| 73 |
} |
| 74 |
|
| 75 |
static VALUE |
| 76 |
unp_fec(VALUE self, VALUE test) |
| 77 |
{ |
| 78 |
st_table *tbl = st_init_numtable(); |
| 79 |
struct checker c; |
| 80 |
|
| 81 |
c.tbl = tbl; |
| 82 |
c.nr = 0; |
| 83 |
c.test = test; |
| 84 |
|
| 85 |
st_add_direct(tbl, 0, 0); |
| 86 |
|
| 87 |
if (tbl->bins != NULL) rb_bug("should still be packed"); |
| 88 |
|
| 89 |
st_foreach_check(tbl, unp_fec_i, (st_data_t)&c, -1); |
| 90 |
|
| 91 |
if (c.test == ID2SYM(rb_intern("delete2"))) { |
| 92 |
if (c.nr != 1) { |
| 93 |
rb_bug("mismatched iteration: %"PRIuVALUE" (expected 1)", (VALUE)c.nr); |
| 94 |
} |
| 95 |
} |
| 96 |
else if (c.nr != expect_size) { |
| 97 |
rb_bug("mismatched iteration: %"PRIuVALUE" (expected %"PRIuVALUE")", |
| 98 |
(VALUE)c.nr, (VALUE)expect_size); |
| 99 |
} |
| 100 |
|
| 101 |
if (tbl->bins == NULL) rb_bug("should be unpacked"); |
| 102 |
|
| 103 |
st_free_table(tbl); |
| 104 |
|
| 105 |
return Qnil; |
| 106 |
} |
| 107 |
|
| 108 |
static int |
| 109 |
unp_fe_i(st_data_t key, st_data_t val, st_data_t args) |
| 110 |
{ |
| 111 |
struct checker *c = (struct checker *)args; |
| 112 |
|
| 113 |
force_unpack_check(c, key, val); |
| 114 |
if (c->test == ID2SYM(rb_intern("unpacked"))) { |
| 115 |
return ST_CONTINUE; |
| 116 |
} |
| 117 |
else if (c->test == ID2SYM(rb_intern("unpack_delete"))) { |
| 118 |
if (c->nr == 1) { |
| 119 |
st_data_t k = 0; |
| 120 |
st_data_t v; |
| 121 |
|
| 122 |
if (!st_delete(c->tbl, &k, &v)) { |
| 123 |
rb_bug("failed to delete"); |
| 124 |
} |
| 125 |
if (v != 0) { |
| 126 |
rb_bug("unexpected value deleted: %"PRIuVALUE" (expected 0)", (VALUE)v); |
| 127 |
} |
| 128 |
return ST_CONTINUE; |
| 129 |
} |
| 130 |
rb_bug("should never get here"); |
| 131 |
} |
| 132 |
|
| 133 |
rb_raise(rb_eArgError, "unexpected arg: %+"PRIsVALUE, c->test); |
| 134 |
} |
| 135 |
|
| 136 |
static VALUE |
| 137 |
unp_fe(VALUE self, VALUE test) |
| 138 |
{ |
| 139 |
st_table *tbl = st_init_numtable(); |
| 140 |
struct checker c; |
| 141 |
|
| 142 |
c.tbl = tbl; |
| 143 |
c.nr = 0; |
| 144 |
c.test = test; |
| 145 |
|
| 146 |
st_add_direct(tbl, 0, 0); |
| 147 |
|
| 148 |
if (tbl->bins != NULL) rb_bug("should still be packed"); |
| 149 |
|
| 150 |
st_foreach(tbl, unp_fe_i, (st_data_t)&c); |
| 151 |
|
| 152 |
if (c.test == ID2SYM(rb_intern("unpack_delete"))) { |
| 153 |
if (c.nr != 1) { |
| 154 |
rb_bug("mismatched iteration: %"PRIuVALUE" (expected 1)", (VALUE)c.nr); |
| 155 |
} |
| 156 |
} |
| 157 |
else if (c.nr != expect_size) { |
| 158 |
rb_bug("mismatched iteration: %"PRIuVALUE" (expected %"PRIuVALUE"o)", |
| 159 |
(VALUE)c.nr, (VALUE)expect_size); |
| 160 |
} |
| 161 |
|
| 162 |
if (tbl->bins == NULL) rb_bug("should be unpacked"); |
| 163 |
|
| 164 |
st_free_table(tbl); |
| 165 |
|
| 166 |
return Qnil; |
| 167 |
} |
| 168 |
|
| 169 |
void |
| 170 |
Init_foreach(void) |
| 171 |
{ |
| 172 |
VALUE bug = rb_define_module("Bug"); |
| 173 |
rb_define_singleton_method(bug, "unp_st_foreach_check", unp_fec, 1); |
| 174 |
rb_define_singleton_method(bug, "unp_st_foreach", unp_fe, 1); |
| 175 |
} |