sha1-array: add test-sha1-array and basic tests

Helped-by: Jeff King <peff@peff.net> Helped-by: Eric Sunshine <sunshine@sunshineco.com> Signed-off-by: Rene Scharfe <l.s.r@web.de> Acked-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

René Scharfe committed Oct 1, 2014 at 17:00 UTC 38d905bf585999367a24fa27645ece6fe996d6f0
4 files changed +110
.gitignore
+1
@@ -195,6 +195,7 @@
195 /test-revision-walking
196 /test-run-command
197 /test-sha1
198 +/test-sha1-array
199 /test-sigchain
200 /test-string-list
201 /test-subprocess
Makefile
+1
@@ -572,6 +572,7 @@ TEST_PROGRAMS_NEED_X += test-revision-walking
572 TEST_PROGRAMS_NEED_X += test-run-command
573 TEST_PROGRAMS_NEED_X += test-scrap-cache-tree
574 TEST_PROGRAMS_NEED_X += test-sha1
575 +TEST_PROGRAMS_NEED_X += test-sha1-array
576 TEST_PROGRAMS_NEED_X += test-sigchain
577 TEST_PROGRAMS_NEED_X += test-string-list
578 TEST_PROGRAMS_NEED_X += test-subprocess
t/t0064-sha1-array.sh new
+74
@@ -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 new
+34
@@ -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 +}