-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extend the test coverage a bit. * cc/test-oidmap: t0016: add 'remove' subcommand test test-oidmap: remove 'add' subcommand test-hashmap: remove 'hash' command oidmap: use sha1hash() instead of static hash() function t: add t0016-oidmap.sh t/helper: add test-oidmap.c
- Loading branch information
Showing
8 changed files
with
221 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 1,112 @@ | ||
#include "test-tool.h" | ||
#include "cache.h" | ||
#include "oidmap.h" | ||
#include "strbuf.h" | ||
|
||
/* key is an oid and value is a name (could be a refname for example) */ | ||
struct test_entry { | ||
struct oidmap_entry entry; | ||
char name[FLEX_ARRAY]; | ||
}; | ||
|
||
#define DELIM " \t\r\n" | ||
|
||
/* | ||
* Read stdin line by line and print result of commands to stdout: | ||
* | ||
* hash oidkey -> sha1hash(oidkey) | ||
* put oidkey namevalue -> NULL / old namevalue | ||
* get oidkey -> NULL / namevalue | ||
* remove oidkey -> NULL / old namevalue | ||
* iterate -> oidkey1 namevalue1\noidkey2 namevalue2\n... | ||
* | ||
*/ | ||
int cmd__oidmap(int argc, const char **argv) | ||
{ | ||
struct strbuf line = STRBUF_INIT; | ||
struct oidmap map = OIDMAP_INIT; | ||
|
||
setup_git_directory(); | ||
|
||
/* init oidmap */ | ||
oidmap_init(&map, 0); | ||
|
||
/* process commands from stdin */ | ||
while (strbuf_getline(&line, stdin) != EOF) { | ||
char *cmd, *p1 = NULL, *p2 = NULL; | ||
struct test_entry *entry; | ||
struct object_id oid; | ||
|
||
/* break line into command and up to two parameters */ | ||
cmd = strtok(line.buf, DELIM); | ||
/* ignore empty lines */ | ||
if (!cmd || *cmd == '#') | ||
continue; | ||
|
||
p1 = strtok(NULL, DELIM); | ||
if (p1) | ||
p2 = strtok(NULL, DELIM); | ||
|
||
if (!strcmp("put", cmd) && p1 && p2) { | ||
|
||
if (get_oid(p1, &oid)) { | ||
printf("Unknown oid: %s\n", p1); | ||
continue; | ||
} | ||
|
||
/* create entry with oid_key = p1, name_value = p2 */ | ||
FLEX_ALLOC_STR(entry, name, p2); | ||
oidcpy(&entry->entry.oid, &oid); | ||
|
||
/* add / replace entry */ | ||
entry = oidmap_put(&map, entry); | ||
|
||
/* print and free replaced entry, if any */ | ||
puts(entry ? entry->name : "NULL"); | ||
free(entry); | ||
|
||
} else if (!strcmp("get", cmd) && p1) { | ||
|
||
if (get_oid(p1, &oid)) { | ||
printf("Unknown oid: %s\n", p1); | ||
continue; | ||
} | ||
|
||
/* lookup entry in oidmap */ | ||
entry = oidmap_get(&map, &oid); | ||
|
||
/* print result */ | ||
puts(entry ? entry->name : "NULL"); | ||
|
||
} else if (!strcmp("remove", cmd) && p1) { | ||
|
||
if (get_oid(p1, &oid)) { | ||
printf("Unknown oid: %s\n", p1); | ||
continue; | ||
} | ||
|
||
/* remove entry from oidmap */ | ||
entry = oidmap_remove(&map, &oid); | ||
|
||
/* print result and free entry*/ | ||
puts(entry ? entry->name : "NULL"); | ||
free(entry); | ||
|
||
} else if (!strcmp("iterate", cmd)) { | ||
|
||
struct oidmap_iter iter; | ||
oidmap_iter_init(&map, &iter); | ||
while ((entry = oidmap_iter_next(&iter))) | ||
printf("%s %s\n", oid_to_hex(&entry->entry.oid), entry->name); | ||
|
||
} else { | ||
|
||
printf("Unknown command %s\n", cmd); | ||
|
||
} | ||
} | ||
|
||
strbuf_release(&line); | ||
oidmap_free(&map, 1); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 1,102 @@ | ||
#!/bin/sh | ||
|
||
test_description='test oidmap' | ||
. ./test-lib.sh | ||
|
||
# This purposefully is very similar to t0011-hashmap.sh | ||
|
||
test_oidmap () { | ||
echo "$1" | test-tool oidmap $3 >actual && | ||
echo "$2" >expect && | ||
test_cmp expect actual | ||
} | ||
|
||
|
||
test_expect_success 'setup' ' | ||
test_commit one && | ||
test_commit two && | ||
test_commit three && | ||
test_commit four | ||
' | ||
|
||
test_expect_success 'put' ' | ||
test_oidmap "put one 1 | ||
put two 2 | ||
put invalidOid 4 | ||
put three 3" "NULL | ||
NULL | ||
Unknown oid: invalidOid | ||
NULL" | ||
' | ||
|
||
test_expect_success 'replace' ' | ||
test_oidmap "put one 1 | ||
put two 2 | ||
put three 3 | ||
put invalidOid 4 | ||
put two deux | ||
put one un" "NULL | ||
NULL | ||
NULL | ||
Unknown oid: invalidOid | ||
2 | ||
1" | ||
' | ||
|
||
test_expect_success 'get' ' | ||
test_oidmap "put one 1 | ||
put two 2 | ||
put three 3 | ||
get two | ||
get four | ||
get invalidOid | ||
get one" "NULL | ||
NULL | ||
NULL | ||
2 | ||
NULL | ||
Unknown oid: invalidOid | ||
1" | ||
' | ||
|
||
test_expect_success 'remove' ' | ||
test_oidmap "put one 1 | ||
put two 2 | ||
put three 3 | ||
remove one | ||
remove two | ||
remove invalidOid | ||
remove four" "NULL | ||
NULL | ||
NULL | ||
1 | ||
2 | ||
Unknown oid: invalidOid | ||
NULL" | ||
' | ||
|
||
test_expect_success 'iterate' ' | ||
test_oidmap "put one 1 | ||
put two 2 | ||
put three 3 | ||
iterate" "NULL | ||
NULL | ||
NULL | ||
$(git rev-parse two) 2 | ||
$(git rev-parse one) 1 | ||
$(git rev-parse three) 3" | ||
' | ||
|
||
test_done |