Example #1
0
File: add.c Project: wgtdkp/wgtcc
int main() {
  test_char();
  test_uchar();
  test_short();
  test_ushort();
  test_int();
  test_uint();
  test_long();
  test_ulong();
  test_float();
  test_double();
  return 0;
}
Example #2
0
static void test_parcom(const char *testcode)
{
	int found=0;
	struct parser_s *parser;

	puts("Testing parser combinators");

	if (!testcode) testcode="ALL";

	if (!strcmp(testcode, "keyword") || !strcmp(testcode, "ALL")) {
		found=1;
		puts(" keyword (int)");
		parser=keyword("int");
		test_ulong("int i;\\n", 3, do_parse(parser, "int i;\n", 0));
		test_ulong("int", 3, do_parse(parser, "int", 0));
		test_ulong("integer", 0, do_parse(parser, "integer", 0));
	}
	
	if (!strcmp(testcode, "pattern-1") || !strcmp(testcode, "ALL")) {
		found=1;
		puts(" pattern ([:ident:][:idnum:]*)");
		parser=pattern("[:ident:][:idnum:]*");
		test_ulong("a", 1, do_parse(parser, "a", 0));
		test_ulong("aa", 2, do_parse(parser, "aa", 0));
		test_ulong("1aa", 0, do_parse(parser, "1aa", 0));
		test_ulong("a1aa", 4, do_parse(parser, "a1aa", 0));
	}
	
	if (!strcmp(testcode, "pattern-2") || !strcmp(testcode, "ALL")) {
		found=1;
		puts(" pattern ( *)");
		parser=pattern(" *");
		test_ulong(" ", 1, do_parse(parser, " ", 0));
		test_ulong("  ", 2, do_parse(parser, "  ", 0));
		test_ulong("   ", 3, do_parse(parser, "   ", 0));
		test_ulong(" .  ", 1, do_parse(parser, " .  ", 0));
	}
	
	if (!strcmp(testcode, "sequence") || !strcmp(testcode, "ALL")) {
		found=1;
		puts(" sequence (([:ident:][:idnum:]*), ( *), ([:digit:]))");
		parser=sequence(pattern("[:ident:][:idnum:]*"), 
			pattern(" *"), 
			pattern("[:digit:]"), 
			NULL);
		test_ulong("a 3", 3, do_parse(parser,"a 3", 0));
		test_ulong("a  3", 4, do_parse(parser,"a  3", 0));
		test_ulong("a1  3", 5, do_parse(parser,"a1  3", 0));
	}

	if (!strcmp(testcode, "Choice") || !strcmp(testcode, "ALL")) {
		found=1;
		puts(" choice (([:ident:][:idnum:]*), ([:digit:]*))");
		parser=choice(pattern("[:ident:][:idnum:]*"),
			pattern("[:digit:]*"),
			NULL);
		test_ulong("a1", 2, do_parse(parser, "a1", 0));
		test_ulong("99", 2, do_parse(parser, "99", 0));
		test_ulong("##", 0, do_parse(parser, "##", 0));
	}
	
	if (!found) fprintf(stderr, "Unknown test code %s\n", testcode);

}
Example #3
0
static ERL_NIF_TERM type_test(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{
    int i;
    int sint;
    unsigned uint;
    long slong;
    unsigned long ulong;
    ErlNifSInt64 sint64;
    ErlNifUInt64 uint64;
    double d;
    ERL_NIF_TERM atom, ref1, ref2;

    sint = INT_MIN;
    do {
	if (!test_int(env,sint)) {
	    goto error;
	}
	sint += ~sint / 3 + 1;
    } while (sint < 0);
    sint = INT_MAX;
    do {
	if (!test_int(env,sint)) {
	    goto error;
	}
	sint -= sint / 3 + 1;
    } while (sint >= 0);

    slong = LONG_MIN;
    do {
	if (!test_long(env,slong)) {
	    goto error;
	}
	slong += ~slong / 3 + 1;
    } while (slong < 0);
    slong = LONG_MAX;
    do {
	if (!test_long(env,slong)) {
	    goto error;
	}
	slong -= slong / 3 + 1;
    } while (slong >= 0);

    sint64 = ((ErlNifSInt64)1 << 63); /* INT64_MIN */
    do {
	if (!test_int64(env,sint64)) {
	    goto error;
	}
	sint64 += ~sint64 / 3 + 1;
    } while (sint64 < 0);
    sint64 = ((ErlNifUInt64)1 << 63) - 1; /* INT64_MAX */
    do {
	if (!test_int64(env,sint64)) {
	    goto error;
	}
	sint64 -= sint64 / 3 + 1;
    } while (sint64 >= 0);

    uint = UINT_MAX;
    for (;;) {
	if (!test_uint(env,uint)) {
	    goto error;
	}
	if (uint == 0) break;
	uint -= uint / 3 + 1;
    }
    ulong = ULONG_MAX;
    for (;;) {
	if (!test_ulong(env,ulong)) {
	    goto error;
	}
	if (ulong == 0) break;
	ulong -= ulong / 3 + 1;
    }    
    uint64 = (ErlNifUInt64)-1; /* UINT64_MAX */
    for (;;) {
	if (!test_uint64(env,uint64)) {
	    goto error;
	}
	if (uint64 == 0) break;
	uint64 -= uint64 / 3 + 1;
    }    

    if (MAX_SMALL < INT_MAX) { /* 32-bit */
	for (i=-10 ; i <= 10; i++) {
	    if (!test_int(env,MAX_SMALL+i)) {
		goto error;
	    }
	}
	for (i=-10 ; i <= 10; i++) {
	    if (!test_int(env,MIN_SMALL+i)) {
		goto error;
	    }
	}
	for (i=-10 ; i <= 10; i++) {
	    if (!test_uint(env,MAX_SMALL+i)) {
		goto error;
	    }
	}
    }
    assert((MAX_SMALL < INT_MAX) == (MIN_SMALL > INT_MIN));

    for (i=-10 ; i < 10; i++) {
	if (!test_long(env,MAX_SMALL+i) || !test_ulong(env,MAX_SMALL+i) ||
	    !test_long(env,MIN_SMALL+i) ||
	    !test_int64(env,MAX_SMALL+i) || !test_uint64(env,MAX_SMALL+i) ||
	    !test_int64(env,MIN_SMALL+i)) {
	    goto error;
	}
	if (MAX_SMALL < INT_MAX) {
	    if (!test_int(env,MAX_SMALL+i) || !test_uint(env,MAX_SMALL+i) ||
		!test_int(env,MIN_SMALL+i)) {
		goto error;
	    }
	}
    }
    for (d=3.141592e-100 ; d < 1e100 ; d *= 9.97) {
	if (!test_double(env,d) || !test_double(env,-d)) {
	    goto error;
	}	
    }

    if (!enif_make_existing_atom(env,"nif_SUITE", &atom, ERL_NIF_LATIN1)
	|| !enif_is_identical(atom,enif_make_atom(env,"nif_SUITE"))) {
	fprintf(stderr, "nif_SUITE not an atom?\r\n");
	goto error;
    }
    for (i=2; i; i--) {
	if (enif_make_existing_atom(env,"nif_SUITE_pink_unicorn", &atom, ERL_NIF_LATIN1)) {
	    fprintf(stderr, "pink unicorn exist?\r\n");
	    goto error;
	}
    }
    ref1 = enif_make_ref(env);
    ref2 = enif_make_ref(env);
    if (!enif_is_ref(env,ref1) || !enif_is_ref(env,ref2) 
	|| enif_is_identical(ref1,ref2) || enif_compare(ref1,ref2)==0) {
	fprintf(stderr, "strange refs?\r\n");
	goto error;
    }
    return enif_make_atom(env,"ok");

error:
    return enif_make_atom(env,"error");
}