]>
Commit | Line | Data |
---|---|---|
9ff46db9 | 1 | |
3fd1520c | 2 | #include "clar_libgit2.h" |
9ff46db9 MS |
3 | |
4 | #include "odb.h" | |
5 | ||
6 | void test_object_raw_convert__succeed_on_oid_to_string_conversion(void) | |
7 | { | |
8 | const char *exp = "16a0123456789abcdef4b775213c23a8bd74f5e0"; | |
9 | git_oid in; | |
10 | char out[GIT_OID_HEXSZ + 1]; | |
11 | char *str; | |
12 | int i; | |
13 | ||
14 | cl_git_pass(git_oid_fromstr(&in, exp)); | |
15 | ||
16 | /* NULL buffer pointer, returns static empty string */ | |
5621d809 | 17 | str = git_oid_tostr(NULL, sizeof(out), &in); |
9ff46db9 MS |
18 | cl_assert(str && *str == '\0' && str != out); |
19 | ||
20 | /* zero buffer size, returns static empty string */ | |
5621d809 | 21 | str = git_oid_tostr(out, 0, &in); |
9ff46db9 MS |
22 | cl_assert(str && *str == '\0' && str != out); |
23 | ||
8fe713cc | 24 | /* NULL oid pointer, sets existing buffer to empty string */ |
5621d809 | 25 | str = git_oid_tostr(out, sizeof(out), NULL); |
8fe713cc | 26 | cl_assert(str && *str == '\0' && str == out); |
9ff46db9 MS |
27 | |
28 | /* n == 1, returns out as an empty string */ | |
5621d809 | 29 | str = git_oid_tostr(out, 1, &in); |
9ff46db9 MS |
30 | cl_assert(str && *str == '\0' && str == out); |
31 | ||
32 | for (i = 1; i < GIT_OID_HEXSZ; i++) { | |
33 | out[i+1] = 'Z'; | |
5621d809 | 34 | str = git_oid_tostr(out, i+1, &in); |
9ff46db9 MS |
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'); | |
e579e0f7 | 41 | /* i == n-1 characters of string */ |
9ff46db9 MS |
42 | cl_git_pass(strncmp(exp, out, i)); |
43 | } | |
44 | ||
45 | /* returns out as hex formatted c-string */ | |
5621d809 | 46 | str = git_oid_tostr(out, sizeof(out), &in); |
9ff46db9 | 47 | cl_assert(str && str == out && *(str+GIT_OID_HEXSZ) == '\0'); |
946a6dc4 | 48 | cl_assert_equal_s(exp, out); |
9ff46db9 MS |
49 | } |
50 | ||
51 | void test_object_raw_convert__succeed_on_oid_to_string_conversion_big(void) | |
52 | { | |
53 | const char *exp = "16a0123456789abcdef4b775213c23a8bd74f5e0"; | |
54 | git_oid in; | |
55 | char big[GIT_OID_HEXSZ + 1 + 3]; /* note + 4 => big buffer */ | |
56 | char *str; | |
57 | ||
58 | cl_git_pass(git_oid_fromstr(&in, exp)); | |
59 | ||
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 */ | |
65 | ||
66 | /* returns big as hex formatted c-string */ | |
5621d809 | 67 | str = git_oid_tostr(big, sizeof(big), &in); |
9ff46db9 | 68 | cl_assert(str && str == big && *(str+GIT_OID_HEXSZ) == '\0'); |
946a6dc4 | 69 | cl_assert_equal_s(exp, big); |
9ff46db9 MS |
70 | |
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'); | |
75 | } | |
660d59ca RB |
76 | |
77 | static void check_partial_oid( | |
78 | char *buffer, size_t count, const git_oid *oid, const char *expected) | |
79 | { | |
80 | git_oid_nfmt(buffer, count, oid); | |
81 | buffer[count] = '\0'; | |
82 | cl_assert_equal_s(expected, buffer); | |
83 | } | |
84 | ||
85 | void test_object_raw_convert__convert_oid_partially(void) | |
86 | { | |
87 | const char *exp = "16a0123456789abcdef4b775213c23a8bd74f5e0"; | |
88 | git_oid in; | |
89 | char big[GIT_OID_HEXSZ + 1 + 3]; /* note + 4 => big buffer */ | |
660d59ca RB |
90 | |
91 | cl_git_pass(git_oid_fromstr(&in, exp)); | |
92 | ||
93 | git_oid_nfmt(big, sizeof(big), &in); | |
94 | cl_assert_equal_s(exp, big); | |
95 | ||
96 | git_oid_nfmt(big, GIT_OID_HEXSZ + 1, &in); | |
97 | cl_assert_equal_s(exp, big); | |
98 | ||
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"); | |
104 | ||
105 | check_partial_oid(big, GIT_OID_HEXSZ, &in, exp); | |
106 | check_partial_oid( | |
107 | big, GIT_OID_HEXSZ - 1, &in, "16a0123456789abcdef4b775213c23a8bd74f5e"); | |
108 | check_partial_oid( | |
109 | big, GIT_OID_HEXSZ - 2, &in, "16a0123456789abcdef4b775213c23a8bd74f5"); | |
110 | check_partial_oid( | |
111 | big, GIT_OID_HEXSZ - 3, &in, "16a0123456789abcdef4b775213c23a8bd74f"); | |
112 | } |