int main(void) {
	int i, r, test_cases, credit, num_items, *prices = NULL;
	char *stream_buf = NULL;
	size_t size;

	stream = open_memstream(&stream_buf, &size);

	r = get_int_from_stdin(&test_cases);
	if (r < 0) {
		fprintf(stderr, "Failed to determine number of test cases\n");
		return EXIT_FAILURE;
	}

	for (i = 0; i < test_cases; ++i) { 
		free(prices);
		prices = NULL;

		r = load_test_case(&credit, &num_items, &prices);
		if (r < 0) {
			free(stream_buf);
			fprintf(stderr, "Failed to parse test cases from stdin\n");
			return EXIT_FAILURE;
		}

		solve(i + 1, credit, num_items, prices);
	}

	free(prices);

	printf("%s", stream_buf);
	free(stream_buf);
	
	return 0;
}
Beispiel #2
0
char *test_parser_thrashing()
{
    glob_t test_files;
    int i = 0;
    int nparsed = 0;
    int tests_run = 0;
    int execs_run = 0;
    int unfinished = 0;
    int errors = 0;

    int rc = glob("tests/client_suite/*", 0, NULL, &test_files);
    mu_assert(rc == 0, "Failed to glob file sin tests/client_suite/*");

    for(i = 0; i < test_files.gl_pathc; i++) {
        debug("TESTING: %s", test_files.gl_pathv[i]);
        bstring data = load_test_case(test_files.gl_pathv[i]);
        mu_assert(data != NULL, "Failed to load test case.");

        tests_run++;

        httpclient_parser p = setup_parser();
        p.data = &p;

        nparsed = httpclient_parser_execute(&p, bdata(data), blength(data), 0);
        execs_run++;

        if(!httpclient_parser_finish(&p)) {
            unfinished++;
        }

        if(httpclient_parser_has_error(&p)) {
            errors++;
            debug("TEST %s results: delta %d, has_error: %d, is_finished: %d",
                    test_files.gl_pathv[i],
                    nparsed, httpclient_parser_has_error(&p), 
                    httpclient_parser_is_finished(&p));
        } else if(p.chunked) {
            int start = p.body_start;

            do {
                httpclient_parser_init(&p);
                
                nparsed = httpclient_parser_execute(&p, bdata(data), blength(data), start);
                mu_assert(p.body_start > start, "Didn't go past start.");
                mu_assert(p.chunked, "Should still be chunked.");
                start = p.body_start + p.content_len + 1;

                debug("CHUNK: length: %d, done: %d, start: %d",
                        p.content_len, p.chunks_done, start);
            } while(!p.chunks_done && p.content_len > 0 && !httpclient_parser_has_error(&p));

            mu_assert(p.chunks_done, "Should have chunks_done set.");
            mu_assert(p.content_len == 0, "Should have 0 content_len too.");
        }
    }

    debug("HTTP PARSING: tests_run: %d, execs_run: %d, unfinished: %d, errors: %d",
            tests_run, execs_run, unfinished, errors);

    return NULL;
}