Skip to content

Commit 38d905b

Browse files
rscharfegitster
authored andcommitted
sha1-array: add test-sha1-array and basic tests
Helped-by: Jeff King <[email protected]> Helped-by: Eric Sunshine <[email protected]> Signed-off-by: Rene Scharfe <[email protected]> Acked-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent d31f3ad commit 38d905b

File tree

4 files changed

+110
-0
lines changed

4 files changed

+110
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@
195195
/test-revision-walking
196196
/test-run-command
197197
/test-sha1
198+
/test-sha1-array
198199
/test-sigchain
199200
/test-string-list
200201
/test-subprocess

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -572,6 +572,7 @@ TEST_PROGRAMS_NEED_X += test-revision-walking
572572
TEST_PROGRAMS_NEED_X += test-run-command
573573
TEST_PROGRAMS_NEED_X += test-scrap-cache-tree
574574
TEST_PROGRAMS_NEED_X += test-sha1
575+
TEST_PROGRAMS_NEED_X += test-sha1-array
575576
TEST_PROGRAMS_NEED_X += test-sigchain
576577
TEST_PROGRAMS_NEED_X += test-string-list
577578
TEST_PROGRAMS_NEED_X += test-subprocess

t/t0064-sha1-array.sh

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#!/bin/sh
2+
3+
test_description='basic tests for the SHA1 array implementation'
4+
. ./test-lib.sh
5+
6+
echo20 () {
7+
prefix="${1:+$1 }"
8+
shift
9+
while test $# -gt 0
10+
do
11+
echo "$prefix$1$1$1$1$1$1$1$1$1$1$1$1$1$1$1$1$1$1$1$1"
12+
shift
13+
done
14+
}
15+
16+
test_expect_success 'ordered enumeration' '
17+
echo20 "" 44 55 88 aa >expect &&
18+
{
19+
echo20 append 88 44 aa 55 &&
20+
echo for_each_unique
21+
} | test-sha1-array >actual &&
22+
test_cmp expect actual
23+
'
24+
25+
test_expect_success 'ordered enumeration with duplicate suppression' '
26+
echo20 "" 44 55 88 aa >expect &&
27+
{
28+
echo20 append 88 44 aa 55 &&
29+
echo20 append 88 44 aa 55 &&
30+
echo for_each_unique
31+
} | test-sha1-array >actual &&
32+
test_cmp expect actual
33+
'
34+
35+
test_expect_success 'lookup' '
36+
{
37+
echo20 append 88 44 aa 55 &&
38+
echo20 lookup 55
39+
} | test-sha1-array >actual &&
40+
n=$(cat actual) &&
41+
test "$n" -eq 1
42+
'
43+
44+
test_expect_success 'lookup non-existing entry' '
45+
{
46+
echo20 append 88 44 aa 55 &&
47+
echo20 lookup 33
48+
} | test-sha1-array >actual &&
49+
n=$(cat actual) &&
50+
test "$n" -lt 0
51+
'
52+
53+
test_expect_success 'lookup with duplicates' '
54+
{
55+
echo20 append 88 44 aa 55 &&
56+
echo20 append 88 44 aa 55 &&
57+
echo20 lookup 55
58+
} | test-sha1-array >actual &&
59+
n=$(cat actual) &&
60+
test "$n" -ge 2 &&
61+
test "$n" -le 3
62+
'
63+
64+
test_expect_success 'lookup non-existing entry with duplicates' '
65+
{
66+
echo20 append 88 44 aa 55 &&
67+
echo20 append 88 44 aa 55 &&
68+
echo20 lookup 66
69+
} | test-sha1-array >actual &&
70+
n=$(cat actual) &&
71+
test "$n" -lt 0
72+
'
73+
74+
test_done

test-sha1-array.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#include "cache.h"
2+
#include "sha1-array.h"
3+
4+
static void print_sha1(const unsigned char sha1[20], void *data)
5+
{
6+
puts(sha1_to_hex(sha1));
7+
}
8+
9+
int main(int argc, char **argv)
10+
{
11+
struct sha1_array array = SHA1_ARRAY_INIT;
12+
struct strbuf line = STRBUF_INIT;
13+
14+
while (strbuf_getline(&line, stdin, '\n') != EOF) {
15+
const char *arg;
16+
unsigned char sha1[20];
17+
18+
if (skip_prefix(line.buf, "append ", &arg)) {
19+
if (get_sha1_hex(arg, sha1))
20+
die("not a hexadecimal SHA1: %s", arg);
21+
sha1_array_append(&array, sha1);
22+
} else if (skip_prefix(line.buf, "lookup ", &arg)) {
23+
if (get_sha1_hex(arg, sha1))
24+
die("not a hexadecimal SHA1: %s", arg);
25+
printf("%d\n", sha1_array_lookup(&array, sha1));
26+
} else if (!strcmp(line.buf, "clear"))
27+
sha1_array_clear(&array);
28+
else if (!strcmp(line.buf, "for_each_unique"))
29+
sha1_array_for_each_unique(&array, print_sha1, NULL);
30+
else
31+
die("unknown command: %s", line.buf);
32+
}
33+
return 0;
34+
}

0 commit comments

Comments
 (0)