static void test_stringmap(void) { mget_stringmap_t *m; char key[128], value[128], *val; int run, it; size_t valuesize; // the initial size of 16 forces the internal reshashing function to be called twice m = mget_stringmap_create(16); for (run = 0; run < 2; run++) { if (run) { mget_stringmap_clear(m); mget_stringmap_sethashfunc(m, hash_txt); } for (it = 0; it < 26; it++) { sprintf(key, "http://www.example.com/subdir/%d.html", it); valuesize = sprintf(value, "%d.html", it); if (mget_stringmap_put(m, key, value, valuesize + 1)) { failed++; info_printf("stringmap_put(%s) returns unexpected old value\n", key); } else ok++; } if ((it = mget_stringmap_size(m)) != 26) { failed++; info_printf("stringmap_size() returned %d (expected %d)\n", it, 26); } else ok++; // now, look up every single entry for (it = 0; it < 26; it++) { sprintf(key, "http://www.example.com/subdir/%d.html", it); sprintf(value, "%d.html", it); if (!(val = mget_stringmap_get(m, key))) { failed++; info_printf("stringmap_get(%s) didn't find entry\n", key); } else if (strcmp(val, value)) { failed++; info_printf("stringmap_get(%s) found '%s' (expected '%s')\n", key, val, value); } else ok++; } mget_stringmap_clear(m); if ((it = mget_stringmap_size(m)) != 0) { failed++; info_printf("stringmap_size() returned %d (expected 0)\n", it); } else ok++; for (it = 0; it < 26; it++) { sprintf(key, "http://www.example.com/subdir/%d.html", it); valuesize = sprintf(value, "%d.html", it); if (mget_stringmap_put(m, key, value, valuesize + 1)) { failed++; info_printf("stringmap_put(%s) returns unexpected old value\n", key); } else ok++; } if ((it = mget_stringmap_size(m)) != 26) { failed++; info_printf("stringmap_size() returned %d (expected %d)\n", it, 26); } else ok++; // now, remove every single entry for (it = 0; it < 26; it++) { sprintf(key, "http://www.example.com/subdir/%d.html", it); sprintf(value, "%d.html", it); mget_stringmap_remove(m, key); } if ((it = mget_stringmap_size(m)) != 0) { failed++; info_printf("stringmap_size() returned %d (expected 0)\n", it); } else ok++; for (it = 0; it < 26; it++) { sprintf(key, "http://www.example.com/subdir/%d.html", it); valuesize = sprintf(value, "%d.html", it); if (mget_stringmap_put(m, key, value, valuesize + 1)) { failed++; info_printf("stringmap_put(%s) returns unexpected old value\n", key); } else ok++; } if ((it = mget_stringmap_size(m)) != 26) { failed++; info_printf("stringmap_size() returned %d (expected %d)\n", it, 26); } else ok++; } // testing alloc/free in stringmap/hashmap mget_stringmap_clear(m); mget_stringmap_put(m, "thekey", NULL, 0) ? failed++ : ok++; mget_stringmap_put(m, "thekey", NULL, 0) ? ok++ : failed++; mget_stringmap_put(m, "thekey", "thevalue", 9) ? ok++ : failed++; mget_stringmap_put(m, "thekey", "thevalue", 9) ? ok++ : failed++; mget_stringmap_put(m, "thekey", NULL, 0) ? ok++ : failed++; // testing key/value identity alloc/free in stringmap/hashmap mget_stringmap_clear(m); mget_stringmap_put(m, "thekey", NULL, 0) ? failed++ : ok++; mget_stringmap_put(m, "thekey", NULL, 0) ? ok++ : failed++; mget_stringmap_put(m, "thekey", "thevalue", 9) ? ok++ : failed++; mget_stringmap_put(m, "thekey", NULL, 0) ? ok++ : failed++; mget_stringmap_free(&m); mget_http_challenge_t challenge; mget_http_parse_challenge("Basic realm=\"test realm\"", &challenge); mget_http_free_challenge(&challenge); mget_vector_t *challenges; challenges = mget_vector_create(2, 2, NULL); mget_vector_set_destructor(challenges, (void(*)(void *))mget_http_free_challenge); mget_http_parse_challenge("Basic realm=\"test realm\"", &challenge); mget_vector_add(challenges, &challenge, sizeof(challenge)); mget_http_free_challenges(&challenges); char *response_text = strdup( "HTTP/1.1 401 Authorization Required\r\n"\ "Date: Sun, 23 Dec 2012 21:03:45 GMT\r\n"\ "Server: Apache/2.2.22 (Debian)\r\n"\ "WWW-Authenticate: Digest realm=\"therealm\", nonce=\"Ip6MaovRBAA=c4af733c51270698260f5d357724c2cbce20fa3d\", algorithm=MD5, domain=\"/prot_digest_md5\", qop=\"auth\"\r\n"\ "Vary: Accept-Encoding\r\n"\ "Content-Length: 476\r\n"\ "Keep-Alive: timeout=5, max=99\r\n"\ "Connection: Keep-Alive\r\n"\ "Content-Type: text/html; charset=iso-8859-1\r\n\r\n"); mget_iri_t *iri = mget_iri_parse("http://localhost/prot_digest_md5/", NULL); mget_http_request_t *req = mget_http_create_request(iri, "GET"); mget_http_response_t *resp = mget_http_parse_response_header(response_text); mget_http_add_credentials(req, mget_vector_get(resp->challenges, 0), "tim", "123"); // for (it=0;it<vec_size(req->lines);it++) { // info_printf("%s\n", (char *)vec_get(req->lines, it)); // } mget_http_free_response(&resp); mget_http_free_request(&req); mget_iri_free(&iri); xfree(response_text); // Authorization: Digest username="******", realm="therealm", nonce="Ip6MaovRBAA=c4af733c51270698260f5d357724c2cbce20fa3d", uri="/prot_digest_md5/", response="a99e2012d507a73dd46eb044d3f4641c", qop=auth, nc=00000001, cnonce="3d20faa1" }
int main(int argc, const char *const *argv) { int fd, it, unique = 0, duple = 0; char *buf, *word, *end; size_t length; struct stat st; mget_stringmap_t *map = mget_stringmap_create(1024); for (it = 1; it < argc; it++) { if ((fd = open(argv[it], O_RDONLY)) == -1) { fprintf(stderr, "Failed to read open %s\n", argv[it]); continue; } if (fstat(fd, &st)) { fprintf(stderr, "Failed to stat %s\n", argv[it]); close(fd); continue; } length = st.st_size; if (!(buf = mmap(NULL, length, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0))) { fprintf(stderr, "Failed to mmap %s (%d)\n", argv[it], errno); close(fd); continue; } buf[length] = 0; for (word = buf; *word; word = end) { while (*word && !isalnum(*word)) word++; for (end = word; *end && isalnum(*end);) end++; if (word != end) { char c = *end; *end = 0; /* if (stringmap_get(map, word)) { duple++; } else { stringmap_put_ident_noalloc(map, strndup(word, end - word)); unique++; } */ if (mget_stringmap_put(map, word, NULL, 0)) duple++; else unique++; *end = c; } } munmap(buf, length); close(fd); } printf("read %u words, %u uniques, %u doubles\n", unique + duple, unique, duple); // const void *keys = stringmap_get_keys(map); mget_stringmap_browse(map, (int(*)(void *, const char *, void *))_print_word, NULL); mget_stringmap_free(&map); return 0; }