]>
git.proxmox.com Git - libgit2.git/blob - tests/object/raw/convert.c
88b1380a481383a95fc32a408841aacdd8cfef02
2 #include "clar_libgit2.h"
6 void test_object_raw_convert__succeed_on_oid_to_string_conversion(void)
8 const char *exp
= "16a0123456789abcdef4b775213c23a8bd74f5e0";
10 char out
[GIT_OID_HEXSZ
+ 1];
14 cl_git_pass(git_oid_fromstr(&in
, exp
));
16 /* NULL buffer pointer, returns static empty string */
17 str
= git_oid_tostr(NULL
, sizeof(out
), &in
);
18 cl_assert(str
&& *str
== '\0' && str
!= out
);
20 /* zero buffer size, returns static empty string */
21 str
= git_oid_tostr(out
, 0, &in
);
22 cl_assert(str
&& *str
== '\0' && str
!= out
);
24 /* NULL oid pointer, sets existing buffer to empty string */
25 str
= git_oid_tostr(out
, sizeof(out
), NULL
);
26 cl_assert(str
&& *str
== '\0' && str
== out
);
28 /* n == 1, returns out as an empty string */
29 str
= git_oid_tostr(out
, 1, &in
);
30 cl_assert(str
&& *str
== '\0' && str
== out
);
32 for (i
= 1; i
< GIT_OID_HEXSZ
; i
++) {
34 str
= git_oid_tostr(out
, i
+1, &in
);
35 /* returns out containing c-string */
36 cl_assert(str
&& str
== out
);
37 /* must be '\0' terminated */
38 cl_assert(*(str
+i
) == '\0');
39 /* must not touch bytes past end of string */
40 cl_assert(*(str
+(i
+1)) == 'Z');
41 /* i == n-1 charaters of string */
42 cl_git_pass(strncmp(exp
, out
, i
));
45 /* returns out as hex formatted c-string */
46 str
= git_oid_tostr(out
, sizeof(out
), &in
);
47 cl_assert(str
&& str
== out
&& *(str
+GIT_OID_HEXSZ
) == '\0');
48 cl_assert_equal_s(exp
, out
);
51 void test_object_raw_convert__succeed_on_oid_to_string_conversion_big(void)
53 const char *exp
= "16a0123456789abcdef4b775213c23a8bd74f5e0";
55 char big
[GIT_OID_HEXSZ
+ 1 + 3]; /* note + 4 => big buffer */
58 cl_git_pass(git_oid_fromstr(&in
, exp
));
60 /* place some tail material */
61 big
[GIT_OID_HEXSZ
+0] = 'W'; /* should be '\0' afterwards */
62 big
[GIT_OID_HEXSZ
+1] = 'X'; /* should remain untouched */
63 big
[GIT_OID_HEXSZ
+2] = 'Y'; /* ditto */
64 big
[GIT_OID_HEXSZ
+3] = 'Z'; /* ditto */
66 /* returns big as hex formatted c-string */
67 str
= git_oid_tostr(big
, sizeof(big
), &in
);
68 cl_assert(str
&& str
== big
&& *(str
+GIT_OID_HEXSZ
) == '\0');
69 cl_assert_equal_s(exp
, big
);
71 /* check tail material is untouched */
72 cl_assert(str
&& str
== big
&& *(str
+GIT_OID_HEXSZ
+1) == 'X');
73 cl_assert(str
&& str
== big
&& *(str
+GIT_OID_HEXSZ
+2) == 'Y');
74 cl_assert(str
&& str
== big
&& *(str
+GIT_OID_HEXSZ
+3) == 'Z');
77 static void check_partial_oid(
78 char *buffer
, size_t count
, const git_oid
*oid
, const char *expected
)
80 git_oid_nfmt(buffer
, count
, oid
);
82 cl_assert_equal_s(expected
, buffer
);
85 void test_object_raw_convert__convert_oid_partially(void)
87 const char *exp
= "16a0123456789abcdef4b775213c23a8bd74f5e0";
89 char big
[GIT_OID_HEXSZ
+ 1 + 3]; /* note + 4 => big buffer */
91 cl_git_pass(git_oid_fromstr(&in
, exp
));
93 git_oid_nfmt(big
, sizeof(big
), &in
);
94 cl_assert_equal_s(exp
, big
);
96 git_oid_nfmt(big
, GIT_OID_HEXSZ
+ 1, &in
);
97 cl_assert_equal_s(exp
, big
);
99 check_partial_oid(big
, 1, &in
, "1");
100 check_partial_oid(big
, 2, &in
, "16");
101 check_partial_oid(big
, 3, &in
, "16a");
102 check_partial_oid(big
, 4, &in
, "16a0");
103 check_partial_oid(big
, 5, &in
, "16a01");
105 check_partial_oid(big
, GIT_OID_HEXSZ
, &in
, exp
);
107 big
, GIT_OID_HEXSZ
- 1, &in
, "16a0123456789abcdef4b775213c23a8bd74f5e");
109 big
, GIT_OID_HEXSZ
- 2, &in
, "16a0123456789abcdef4b775213c23a8bd74f5");
111 big
, GIT_OID_HEXSZ
- 3, &in
, "16a0123456789abcdef4b775213c23a8bd74f");