Skip to content

Commit 40e2d8d

Browse files
committed
Merge branch 'rs/sha1-array-test'
* rs/sha1-array-test: sha1-lookup: handle duplicates in sha1_pos() sha1-array: add test-sha1-array and basic tests
2 parents 11cb313 + 0eb0fb8 commit 40e2d8d

File tree

5 files changed

+130
-2
lines changed

5 files changed

+130
-2
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@
199199
/test-revision-walking
200200
/test-run-command
201201
/test-sha1
202+
/test-sha1-array
202203
/test-sigchain
203204
/test-string-list
204205
/test-subprocess

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -568,6 +568,7 @@ TEST_PROGRAMS_NEED_X += test-revision-walking
568568
TEST_PROGRAMS_NEED_X += test-run-command
569569
TEST_PROGRAMS_NEED_X += test-scrap-cache-tree
570570
TEST_PROGRAMS_NEED_X += test-sha1
571+
TEST_PROGRAMS_NEED_X += test-sha1-array
571572
TEST_PROGRAMS_NEED_X += test-sigchain
572573
TEST_PROGRAMS_NEED_X += test-string-list
573574
TEST_PROGRAMS_NEED_X += test-subprocess

sha1-lookup.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,6 @@ int sha1_pos(const unsigned char *sha1, void *table, size_t nr,
8484
die("BUG: assertion failed in binary search");
8585
}
8686
}
87-
if (18 <= ofs)
88-
die("cannot happen -- lo and hi are identical");
8987
}
9088

9189
do {

t/t0064-sha1-array.sh

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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_expect_success 'lookup with almost duplicate values' '
75+
{
76+
echo "append 5555555555555555555555555555555555555555" &&
77+
echo "append 555555555555555555555555555555555555555f" &&
78+
echo20 lookup 55
79+
} | test-sha1-array >actual &&
80+
n=$(cat actual) &&
81+
test "$n" -eq 0
82+
'
83+
84+
test_expect_success 'lookup with single duplicate value' '
85+
{
86+
echo20 append 55 55 &&
87+
echo20 lookup 55
88+
} | test-sha1-array >actual &&
89+
n=$(cat actual) &&
90+
test "$n" -ge 0 &&
91+
test "$n" -le 1
92+
'
93+
94+
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)