git.y1.nz

okra

C99 URI parser library.
download: http://git.y1.nz/archives/okra.tar.gz
README | Files | Log | Refs

test/merge.c

      1 #include <stdio.h> /* printf */
      2 #include <stdlib.h> /* atoi */
      3 
      4 #include "okra.h"
      5 
      6 int print = 0;
      7 
      8 #define CASE(rel_str, condition)  { \
      9 	print = 0; \
     10 	\
     11 	if (target_index == 0 || target_index == test_index) { \
     12 		ref = okra_parse(rel_str); \
     13 		merged = okra_merge(base, ref); \
     14 		result = okra_recompose(merged); \
     15 		if (condition) { \
     16 			printf("\033[1;32m" " %d. pass" "\033[0m", test_index); \
     17 			tests_passed++; \
     18 		} else { \
     19 			printf("\033[1;31m" " %d. fail" "\033[0m", test_index); \
     20 			print = 1; \
     21 			condition; \
     22 			printf("\n" "%10s\t\t%s" "\n", base_str, rel_str); \
     23 		} \
     24 	} \
     25 	\
     26 	okra_free(merged); \
     27 	okra_free(ref); \
     28 	\
     29 	test_index++; \
     30 }
     31 
     32 int
     33 null(const void *arg)
     34 {
     35 	if (print) printf("[%lx] vs expected [(null)]", (unsigned long int) arg);
     36 	return arg == NULL;
     37 }
     38 
     39 int
     40 eq(const char *a, const char *b)
     41 {
     42 	if (print) printf("[%s] vs expected [%s] ", a, b);
     43 
     44 	if (a == NULL || b == NULL) return 0;
     45 
     46 	int i = 0;
     47 	for (;;) {
     48 		if (a[i] != b[i]) return 0;
     49 		if (a[i] == '\0') return 1;
     50 		i++;
     51 	}
     52 }
     53 
     54 int
     55 main(int argc, const char *argv[])
     56 {
     57 	int test_index = 1;
     58 	int tests_passed = 0;
     59 	OkraUri *ref, *uri, *merged;
     60 	char *result;
     61 	int target_index = argc == 1 ? 0 : atoi(argv[1]);
     62 
     63 	const char *base_str = "http://a/b/c/d;p?q";
     64 	OkraUri *base = okra_parse(base_str);
     65 
     66 	printf("\033[1m" "Testing relative URI resolution..." "\033[0m" "\n");
     67 	
     68 	/* "Normal Examples" from section 5.4.1 */
     69 	CASE("g:h", eq(result, "g:h"));
     70 	CASE("g", eq(result, "http://a/b/c/g"));
     71 	CASE("./g", eq(result, "http://a/b/c/g"));
     72 	CASE("g/", eq(result, "http://a/b/c/g/"));
     73 	CASE("/g", eq(result, "http://a/g"));
     74 	CASE("//g", eq(result, "http://g"));
     75 	CASE("?y", eq(result, "http://a/b/c/d;p?y"));
     76 	CASE("g?y", eq(result, "http://a/b/c/g?y"));
     77 	CASE("#s", eq(result, "http://a/b/c/d;p?q#s"));
     78 	CASE("g#s", eq(result, "http://a/b/c/g#s"));
     79 	CASE("g?y#s", eq(result, "http://a/b/c/g?y#s"));
     80 	CASE(";x", eq(result, "http://a/b/c/;x"));
     81 	CASE("g;x", eq(result, "http://a/b/c/g;x"));
     82 	CASE("g;x?y#s", eq(result, "http://a/b/c/g;x?y#s"));
     83 	CASE("", eq(result, "http://a/b/c/d;p?q"));
     84 	CASE(".", eq(result, "http://a/b/c/"));
     85 	CASE("./", eq(result, "http://a/b/c/"));
     86 	CASE("..", eq(result, "http://a/b/"));
     87 	CASE("../", eq(result, "http://a/b/"));
     88 	CASE("../g", eq(result, "http://a/b/g"));
     89 	CASE("../..", eq(result, "http://a/"));
     90 	CASE("../../", eq(result, "http://a/"));
     91 	CASE("../../g", eq(result, "http://a/g"));
     92 
     93 	/* "Abnormal Examples" from section 5.4.2 */
     94 	CASE("../../../g", eq(result, "http://a/g"));
     95 	CASE("../../../../g", eq(result, "http://a/g"));
     96 	CASE("/./g", eq(result, "http://a/g"));
     97 	CASE("/../g", eq(result, "http://a/g"));
     98 	CASE("g.", eq(result, "http://a/b/c/g."));
     99 	CASE(".g", eq(result, "http://a/b/c/.g"));
    100 	CASE("g..", eq(result, "http://a/b/c/g.."));
    101 	CASE("..g", eq(result, "http://a/b/c/..g"));
    102 	CASE("./../g", eq(result, "http://a/b/g"));
    103 	CASE("./g/.", eq(result, "http://a/b/c/g/"));
    104 	CASE("g/./h", eq(result, "http://a/b/c/g/h"));
    105 	CASE("g/../h", eq(result, "http://a/b/c/h"));
    106 	CASE("g;x=1/./y", eq(result, "http://a/b/c/g;x=1/y"));
    107 	CASE("g;x=1/../y", eq(result, "http://a/b/c/y"));
    108 	CASE("g?y/./x", eq(result, "http://a/b/c/g?y/./x"));
    109 	CASE("g?y/../x", eq(result, "http://a/b/c/g?y/../x"));
    110 	CASE("g#s/./x", eq(result, "http://a/b/c/g#s/./x"));
    111 	CASE("g#s/../x", eq(result, "http://a/b/c/g#s/../x"));
    112 	CASE("http:g", eq(result, "http:g"));
    113 	printf(
    114 		"\n" "\033[1m" "Passed %d/%d" "\033[0m" "\n",
    115 		tests_passed,
    116 		test_index-1
    117 	);
    118 
    119 	free(base);
    120 }

This webpage is intended to be an accessible preview of this repository. To get a fuller picture, clone it and use the git CLI.