]>
git.proxmox.com Git - libgit2.git/blob - tests/object/raw/short.c
e8d2cf5a5bfbb524cfd32276934eca88d1c3983f
2 #include "clar_libgit2.h"
7 void test_object_raw_short__oid_shortener_no_duplicates(void)
12 os
= git_oid_shorten_new(0);
13 cl_assert(os
!= NULL
);
15 git_oid_shorten_add(os
, "22596363b3de40b06f981fb85d82312e8c0ed511");
16 git_oid_shorten_add(os
, "ce08fe4884650f067bd5703b6a59a8b3b3c99a09");
17 git_oid_shorten_add(os
, "16a0123456789abcdef4b775213c23a8bd74f5e0");
18 min_len
= git_oid_shorten_add(os
, "ce08fe4884650f067bd5703b6a59a8b3b3c99a09");
20 cl_assert(min_len
== GIT_OID_HEXSZ
+ 1);
22 git_oid_shorten_free(os
);
25 static int insert_sequential_oids(
26 char ***out
, git_oid_shorten
*os
, int n
, int fail
)
31 char **oids
= git__calloc(n
, sizeof(char *));
32 cl_assert(oids
!= NULL
);
34 for (i
= 0; i
< n
; ++i
) {
35 p_snprintf(numbuf
, sizeof(numbuf
), "%u", (unsigned int)i
);
36 git_hash_buf(oid
.id
, numbuf
, strlen(numbuf
), GIT_HASH_ALGORITHM_SHA1
);
38 oids
[i
] = git__malloc(GIT_OID_HEXSZ
+ 1);
40 git_oid_nfmt(oids
[i
], GIT_OID_HEXSZ
+ 1, &oid
);
42 min_len
= git_oid_shorten_add(os
, oids
[i
]);
44 /* After "fail", we expect git_oid_shorten_add to fail */
45 if (fail
>= 0 && i
>= fail
)
46 cl_assert(min_len
< 0);
48 cl_assert(min_len
>= 0);
56 static void free_oids(int n
, char **oids
)
60 for (i
= 0; i
< n
; ++i
) {
66 void test_object_raw_short__oid_shortener_stresstest_git_oid_shorten(void)
72 int min_len
= 0, found_collision
;
75 os
= git_oid_shorten_new(0);
76 cl_assert(os
!= NULL
);
79 * Insert in the shortener 1000 unique SHA1 ids
81 min_len
= insert_sequential_oids(&oids
, os
, MAX_OIDS
, MAX_OIDS
);
82 cl_assert(min_len
> 0);
85 * Compare the first `min_char - 1` characters of each
86 * SHA1 OID. If the minimizer worked, we should find at
90 for (i
= 0; i
< MAX_OIDS
; ++i
) {
91 for (j
= i
+ 1; j
< MAX_OIDS
; ++j
) {
92 if (memcmp(oids
[i
], oids
[j
], min_len
- 1) == 0)
96 cl_assert_equal_b(true, found_collision
);
99 * Compare the first `min_char` characters of each
100 * SHA1 OID. If the minimizer worked, every single preffix
104 for (i
= 0; i
< MAX_OIDS
; ++i
) {
105 for (j
= i
+ 1; j
< MAX_OIDS
; ++j
) {
106 if (memcmp(oids
[i
], oids
[j
], min_len
) == 0)
110 cl_assert_equal_b(false, found_collision
);
113 free_oids(MAX_OIDS
, oids
);
114 git_oid_shorten_free(os
);
119 void test_object_raw_short__oid_shortener_too_much_oids(void)
121 /* The magic number of oids at which an oid_shortener will fail.
122 * This was experimentally established. */
123 #define MAX_OIDS 24556
128 os
= git_oid_shorten_new(0);
129 cl_assert(os
!= NULL
);
131 cl_assert(insert_sequential_oids(&oids
, os
, MAX_OIDS
, MAX_OIDS
- 1) < 0);
133 free_oids(MAX_OIDS
, oids
);
134 git_oid_shorten_free(os
);