diff options
author | eregon <eregon@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2017-09-20 20:18:52 +0000 |
---|---|---|
committer | eregon <eregon@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2017-09-20 20:18:52 +0000 |
commit | 1d15d5f08032acf1b7bceacbb450d617ff6e0931 (patch) | |
tree | a3785a79899302bc149e4a6e72f624ac27dc1f10 /spec/ruby/optional/capi/ext/st_spec.c | |
parent | 75bfc6440d595bf339007f4fb280fd4d743e89c1 (diff) |
Move spec/rubyspec to spec/ruby for consistency
* Other ruby implementations use the spec/ruby directory.
[Misc #13792] [ruby-core:82287]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@59979 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'spec/ruby/optional/capi/ext/st_spec.c')
-rw-r--r-- | spec/ruby/optional/capi/ext/st_spec.c | 93 |
1 files changed, 93 insertions, 0 deletions
diff --git a/spec/ruby/optional/capi/ext/st_spec.c b/spec/ruby/optional/capi/ext/st_spec.c new file mode 100644 index 0000000000..4e59698d77 --- /dev/null +++ b/spec/ruby/optional/capi/ext/st_spec.c @@ -0,0 +1,93 @@ +#include "ruby.h" +#include "rubyspec.h" + +#include <string.h> +#include <stdarg.h> + +#ifdef HAVE_RB_ST +#include <ruby/st.h> +#endif + +#ifdef __cplusplus +extern "C" { +#endif + +#ifdef HAVE_RB_ST + +#if SIZEOF_LONG == SIZEOF_VOIDP +# define ST2NUM(x) ULONG2NUM(x) +#else +# define ST2NUM(x) ULL2NUM(x) +#endif + +VALUE st_spec_st_init_numtable(VALUE self) { + st_table *tbl = st_init_numtable(); + st_index_t entries = tbl->num_entries; + st_free_table(tbl); + return ST2NUM(entries); +} + +VALUE st_spec_st_init_numtable_with_size(VALUE self) { + st_table *tbl = st_init_numtable_with_size(128); + st_index_t entries = tbl->num_entries; + st_free_table(tbl); + return ST2NUM(entries); +} + +VALUE st_spec_st_insert(VALUE self) { + st_index_t entries; + st_table *tbl = st_init_numtable_with_size(128); + st_insert(tbl, 1, 1); + entries = tbl->num_entries; + st_free_table(tbl); + return ST2NUM(entries); +} + +static int sum(st_data_t key, st_data_t value, st_data_t arg) { + *(int*)arg += (int)value; + return ST_CONTINUE; +} + +VALUE st_spec_st_foreach(VALUE self) { + int total = 0; + st_table *tbl = st_init_numtable_with_size(128); + st_insert(tbl, 1, 3); + st_insert(tbl, 2, 4); + st_foreach(tbl, sum, (st_data_t)&total); + st_free_table(tbl); + return INT2FIX(total); +} + +VALUE st_spec_st_lookup(VALUE self) { + st_data_t result = (st_data_t)0; + st_table *tbl = st_init_numtable_with_size(128); + st_insert(tbl, 7, 42); + st_insert(tbl, 2, 4); + st_lookup(tbl, (st_data_t)7, &result); + st_free_table(tbl); +#if SIZEOF_LONG == SIZEOF_VOIDP + return ULONG2NUM(result); +#else + return ULL2NUM(result); +#endif +} + +#endif + +void Init_st_spec(void) { + VALUE cls; + cls = rb_define_class("CApiStSpecs", rb_cObject); + +#ifdef HAVE_RB_ST + rb_define_method(cls, "st_init_numtable", st_spec_st_init_numtable, 0); + rb_define_method(cls, "st_init_numtable_with_size", st_spec_st_init_numtable_with_size, 0); + rb_define_method(cls, "st_insert", st_spec_st_insert, 0); + rb_define_method(cls, "st_foreach", st_spec_st_foreach, 0); + rb_define_method(cls, "st_lookup", st_spec_st_lookup, 0); +#endif + +} + +#ifdef __cplusplus +} +#endif |