Exemplo n.º 1
0
VALUE
throw_exc(VALUE self, int argc, VALUE *argv)
{
	/* argv[0] == VALUE string descrip; argv[1] == optional
	   exception class being thrown (default `Exception') */
	int i, len;
	VALUE buf, thr, ret, exc_type;
	VALUE exc;

	thr = cur_thr;
	exc = THREAD(thr)->excobj;

	while (!TEST(exc) && TEST(thr))
	{
		thr = THREAD(thr)->up;
		if (TEST(thr))
			exc = THREAD(thr)->excobj;
		else
			exc = Qnil;
	}

	if (!TEST(exc))
	{
		if ( (argc > 0) && argv[0] && (TYPE(argv[0]) == T_STRING) )
			fail(str2cstr(argv[0]));
		else
			fail("unhandled exception");
	}

	if ((argc > 0) && (argv[0]) && TEST(argv[0]))
		EXCEPTION(exc)->strerror = argv[0];
	else
		EXCEPTION(exc)->strerror = string_new("(details of exception unspecified)");

	if (argc > 1)
		exc_type = argv[1];
	else
		exc_type = cException;

	if (!TEST(exc) || !kind_of_p(exc, exc_type))
		fail("uncaught exception");

	if (TEST(EXCEPTION(exc)->rescue_thr))
	{
		thr = cur_thr;
		cur_thr = EXCEPTION(exc)->rescue_thr;
		eval_stack( THREAD(EXCEPTION(exc)->rescue_thr)->stack );
		ret = Qbreak;
		cur_thr = thr;
	}
	else
	{
		ret = THREAD(cur_thr)->last;
		cur_thr = EXCEPTION(exc)->thr;
	}

	return ret;
}
void test_string_delete() {
  const char *test_type = "string", *test_content = "delete";
  bool ok = 1;

  String s1 = cstr2str("0123456789");
  sdeleter(&s1, 4, 7);
  char *t = str2cstr(s1);

  if (strcmp(t, "0123789") != 0) {
    ok = 0;
  }

  if (!ok) {
    printf("%s test fails on %s.\n", test_type, test_content);
  }

  sdestro(&s1);
  free(t);
}
void test_string_insert() {
  const char *test_type = "string", *test_content = "insert";
  bool ok = 1;
  String s = sinit();

  for (int i = 0; i < 100; i++) {
    sinsertc(&s, 0, i);
  }

  for (int i = 0; i < 100; i++) {
    if (snth(s, i)+1 != 100-i) {
      ok = 0;
      break;
    }
  }

  String n = sinit();
  for (int i = 0; i < 10; i++) {
    sinsertc(&n, 0, i+'0');
  }

  String t = cstr2str("9876543210");
  sinserts(&n, &t, 5);

  char *tt = str2cstr(n);
  if (strcmp("98765987654321043210", tt) != 0) {
    ok = 0;
  }

  if (!ok) {
    printf("%s test fails on %s.\n", test_type, test_content);
  }

  free(tt);
  sdestro(&s);
  sdestro(&t);
  sdestro(&n);
}