okra | C99 URI parser library. |
| download: http://git.y1.nz/archives/okra.tar.gz | |
| README | Files | Log | Refs |
test/base.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(uri_str, condition) { \
9 print = 0; \
10 \
11 uri = okra_parse(uri_str); \
12 if (condition) { \
13 printf("\033[1;32m" " %d. pass" "\033[0m", test_index); \
14 tests_passed++; \
15 } else { \
16 print = 1; \
17 printf("\n"); \
18 printf("\033[1;31m" " %d. fail" "\033[0m" "\n", test_index); \
19 printf("%s" "\n", uri_str); \
20 condition; \
21 printf("\n"); \
22 } \
23 \
24 okra_free(uri); \
25 \
26 test_index++; \
27 }
28
29 int
30 null(const void *arg)
31 {
32 if (print) printf("[%lx] vs expected [(null)]", (unsigned long int) arg);
33 return arg == NULL;
34 }
35
36 int
37 eq(const char *a, const char *b)
38 {
39 if (print) printf("[%s] vs expected [%s] ", a, b);
40
41 if (a == NULL || b == NULL) return 0;
42
43 int i = 0;
44 for (;;) {
45 if (a[i] != b[i]) return 0;
46 if (a[i] == '\0') return 1;
47 i++;
48 }
49 }
50
51 int
52 main(int argc, const char *argv[])
53 {
54 int test_index = 1;
55 int tests_passed = 0;
56 const char *str;
57 OkraUri *uri;
58 int target_index = argc == 1 ? 0 : atoi(argv[1]);
59
60 printf("\033[1m" "Testing parser..." "\033[0m" "\n");
61
62 str = "http://google.com";
63 CASE(str, eq(uri->scheme, "http"));
64 CASE(str, eq(uri->authority, "google.com"));
65 CASE(str, eq(uri->host, "google.com"));
66
67 str = "mailto:user@mail.com";
68 CASE(str, eq(uri->scheme, "mailto"));
69 CASE(str, eq(uri->path, "user@mail.com"));
70 CASE(str, null(uri->authority));
71
72 str = "foo://example.com:8042/over/there?name=ferret#nose";
73 CASE(str, eq(uri->scheme, "foo"));
74 CASE(str, eq(uri->path, "/over/there"));
75 CASE(str, eq(uri->authority, "example.com:8042"));
76 CASE(str, eq(uri->host, "example.com"));
77 CASE(str, eq(uri->port, "8042"));
78 CASE(str, eq(uri->query, "name=ferret"));
79 CASE(str, eq(uri->fragment, "nose"));
80
81 str = "urn:example:animal:ferret:nose";
82 CASE(str, eq(uri->scheme, "urn"));
83 CASE(str, eq(uri->path, "example:animal:ferret:nose"));
84
85 str = "example://a/b/c/%7Bfoo%7D";
86 CASE(str, eq(uri->scheme, "example"));
87 CASE(str, eq(uri->path, "/b/c/%7Bfoo%7D"));
88
89 str = "foo:this/is/the/path#nose/\?\?/./.a.";
90 CASE(str, eq(uri->scheme, "foo"));
91 CASE(str, eq(uri->path, "this/is/the/path"));
92 CASE(str, eq(uri->fragment, "nose/\?\?/./.a."));
93
94 printf(
95 "\n" "\033[1m" "Passed %d/%d" "\033[0m" "\n",
96 tests_passed,
97 test_index-1
98 );
99 }
This webpage is intended to be an accessible preview of this repository. To get a fuller picture, clone it and use the git CLI.