Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

better exception handling in rb test

  • Loading branch information...
commit 816407cecebab79e728a041123204c212bd17b0a 1 parent 02d1661
@matthiaskramm authored
Showing with 94 additions and 20 deletions.
  1. +65 −7 tests/language_rb.c
  2. +29 −13 tests/test_codegen.c
View
72 tests/language_rb.c
@@ -29,16 +29,55 @@ static void rb_report_error()
}
}
-static bool define_function_rb(language_interpreter_t*li, const char*script)
+typedef struct _ruby_dfunc {
+ language_interpreter_t*li;
+ const char*script;
+ bool fail;
+} ruby_dfunc_t;
+
+static VALUE define_function_internal(VALUE _dfunc)
{
+ fprintf(stderr, "define_function_internal\n");fflush(stderr);
+
+ ruby_dfunc_t*dfunc = (ruby_dfunc_t*)_dfunc;
+ language_interpreter_t*li = dfunc->li;
rb_internal_t*rb = (rb_internal_t*)li->internal;
- VALUE ret = rb_eval_string(script);
- rb_report_error();
- return true;
+
+ VALUE ret = rb_eval_string(dfunc->script);
+
+ return Qtrue;
}
-static int call_function_rb(language_interpreter_t*li, row_t*row)
+static VALUE define_function_exception(VALUE _dfunc)
{
+ fprintf(stderr, "define_function_exception\n");fflush(stderr);
+ ruby_dfunc_t*dfunc = (ruby_dfunc_t*)_dfunc;
+ dfunc->fail = true;
+ return Qfalse;
+}
+
+static bool define_function_rb(language_interpreter_t*li, const char*script)
+{
+ ruby_dfunc_t dfunc;
+ dfunc.li = li;
+ dfunc.script = script;
+ dfunc.fail = false;
+ VALUE ret = rb_rescue(define_function_internal, (VALUE)&dfunc, define_function_exception, (VALUE)&dfunc);
+ return !dfunc.fail;
+}
+
+typedef struct _ruby_fcall {
+ language_interpreter_t*li;
+ row_t*row;
+ bool fail;
+} ruby_fcall_t;
+
+static VALUE call_function_internal(VALUE _fcall)
+{
+ ruby_fcall_t*fcall = (ruby_fcall_t*)_fcall;
+ language_interpreter_t*li = fcall->li;
+ row_t*row = fcall->row;
+
rb_internal_t*rb = (rb_internal_t*)li->internal;
volatile VALUE array = rb_ary_new2(row->num_inputs);
@@ -50,10 +89,29 @@ static int call_function_rb(language_interpreter_t*li, row_t*row)
volatile ID fname = rb_intern("predict");
volatile VALUE object = rb_eval_string("Object");
- VALUE ret = rb_funcall(object, fname, 1, array);
+ return rb_funcall(object, fname, 1, array);
+}
+
+static VALUE call_function_exception(VALUE _fcall)
+{
+ ruby_fcall_t*fcall = (ruby_fcall_t*)_fcall;
+ fcall->fail = true;
+}
+
+static int call_function_rb(language_interpreter_t*li, row_t*row)
+{
+ ruby_fcall_t fcall;
+ fcall.li = li;
+ fcall.row = row;
+ fcall.fail = false;
+
+ VALUE ret = rb_rescue(call_function_internal, (VALUE)&fcall, call_function_exception, (VALUE)&fcall);
+
+ if(fcall.fail) {
+ return -1;
+ }
int result = NUM2INT(ret);
- rb_report_error();
return result;
}
View
42 tests/test_codegen.c
@@ -33,27 +33,43 @@ void test_language(language_interpreter_t*lang)
model_t*model = model_train_specific_model(data1, model_name);
bool fail_generate = model == NULL;
bool fail_predict = false;
+ bool fail_define_function = false;
+ bool fail_call_function = false;
+ int count_total = 0;
+ int count_good = 0;
if(model) {
char*code = model_generate_code(model, lang->name);
- lang->define_function(lang, code);
-
- example_t*e;
- for(e = tdata1->first_example; e; e = e->next) {
- row_t*r = example_to_row(e, 0);
- variable_t v = model_predict(model, r);
- int c = lang->call_function(lang, r);
- if(v.category != c) {
- fail_predict = true;
- break;
+ bool success = lang->define_function(lang, code);
+ fail_define_function = !success;
+ if(success) {
+ example_t*e;
+ for(e = tdata1->first_example; e; e = e->next) {
+ row_t*r = example_to_row(e, 0);
+ variable_t v = model_predict(model, r);
+ int c = lang->call_function(lang, r);
+ if(c < 0) {
+ fail_call_function = true;
+ break;
+ }
+ if(v.category != c) {
+ fail_predict = true;
+ } else {
+ count_good++;
+ }
+ count_total++;
}
}
}
if(fail_generate) {
- printf("[ ] %s %s\n", lang->name, model_name);
+ printf("[ ] %s %s\n", lang->name, model_name);
+ } else if(fail_define_function) {
+ printf("[EFUNC] %s %s\n", lang->name, model_name);
+ } else if(fail_call_function) {
+ printf("[ECALL] %s %s\n", lang->name, model_name);
} else if(fail_predict) {
- printf("[EE] %s %s\n", lang->name, model_name);
+ printf("[%02x/%02x] %s %s\n", count_good, count_total, lang->name, model_name);
} else {
- printf("[OK] %s %s\n", lang->name, model_name);
+ printf("[ OK! ] %s %s\n", lang->name, model_name);
}
}
lang->destroy(lang);
Please sign in to comment.
Something went wrong with that request. Please try again.