]>
Commit | Line | Data |
---|---|---|
917f60c5 RB |
1 | #include "clar_libgit2.h" |
2 | #include "oidmap.h" | |
3 | ||
22a2d3d5 | 4 | static struct { |
917f60c5 RB |
5 | git_oid oid; |
6 | size_t extra; | |
22a2d3d5 | 7 | } test_oids[0x0FFF]; |
917f60c5 | 8 | |
22a2d3d5 | 9 | static git_oidmap *g_map; |
917f60c5 | 10 | |
22a2d3d5 | 11 | void test_core_oidmap__initialize(void) |
917f60c5 | 12 | { |
917f60c5 | 13 | uint32_t i, j; |
22a2d3d5 UG |
14 | for (i = 0; i < ARRAY_SIZE(test_oids); ++i) { |
15 | uint32_t segment = i / 8; | |
16 | int modi = i - (segment * 8); | |
17 | ||
18 | test_oids[i].extra = i; | |
917f60c5 | 19 | |
917f60c5 | 20 | for (j = 0; j < GIT_OID_RAWSZ / 4; ++j) { |
22a2d3d5 UG |
21 | test_oids[i].oid.id[j * 4 ] = (unsigned char)modi; |
22 | test_oids[i].oid.id[j * 4 + 1] = (unsigned char)(modi >> 8); | |
23 | test_oids[i].oid.id[j * 4 + 2] = (unsigned char)(modi >> 16); | |
24 | test_oids[i].oid.id[j * 4 + 3] = (unsigned char)(modi >> 24); | |
917f60c5 | 25 | } |
917f60c5 | 26 | |
22a2d3d5 UG |
27 | test_oids[i].oid.id[ 8] = (unsigned char)i; |
28 | test_oids[i].oid.id[ 9] = (unsigned char)(i >> 8); | |
29 | test_oids[i].oid.id[10] = (unsigned char)(i >> 16); | |
30 | test_oids[i].oid.id[11] = (unsigned char)(i >> 24); | |
917f60c5 RB |
31 | } |
32 | ||
22a2d3d5 UG |
33 | cl_git_pass(git_oidmap_new(&g_map)); |
34 | } | |
917f60c5 | 35 | |
22a2d3d5 UG |
36 | void test_core_oidmap__cleanup(void) |
37 | { | |
38 | git_oidmap_free(g_map); | |
39 | } | |
917f60c5 | 40 | |
22a2d3d5 UG |
41 | void test_core_oidmap__basic(void) |
42 | { | |
43 | size_t i; | |
917f60c5 | 44 | |
22a2d3d5 UG |
45 | for (i = 0; i < ARRAY_SIZE(test_oids); ++i) { |
46 | cl_assert(!git_oidmap_exists(g_map, &test_oids[i].oid)); | |
47 | cl_git_pass(git_oidmap_set(g_map, &test_oids[i].oid, &test_oids[i])); | |
917f60c5 RB |
48 | } |
49 | ||
22a2d3d5 UG |
50 | for (i = 0; i < ARRAY_SIZE(test_oids); ++i) { |
51 | cl_assert(git_oidmap_exists(g_map, &test_oids[i].oid)); | |
52 | cl_assert_equal_p(git_oidmap_get(g_map, &test_oids[i].oid), &test_oids[i]); | |
53 | } | |
917f60c5 RB |
54 | } |
55 | ||
56 | void test_core_oidmap__hash_collision(void) | |
57 | { | |
22a2d3d5 | 58 | size_t i; |
917f60c5 | 59 | |
22a2d3d5 UG |
60 | for (i = 0; i < ARRAY_SIZE(test_oids); ++i) { |
61 | cl_assert(!git_oidmap_exists(g_map, &test_oids[i].oid)); | |
62 | cl_git_pass(git_oidmap_set(g_map, &test_oids[i].oid, &test_oids[i])); | |
63 | } | |
917f60c5 | 64 | |
22a2d3d5 UG |
65 | for (i = 0; i < ARRAY_SIZE(test_oids); ++i) { |
66 | cl_assert(git_oidmap_exists(g_map, &test_oids[i].oid)); | |
67 | cl_assert_equal_p(git_oidmap_get(g_map, &test_oids[i].oid), &test_oids[i]); | |
917f60c5 | 68 | } |
22a2d3d5 | 69 | } |
917f60c5 | 70 | |
22a2d3d5 UG |
71 | void test_core_oidmap__get_succeeds_with_existing_keys(void) |
72 | { | |
73 | size_t i; | |
917f60c5 | 74 | |
22a2d3d5 UG |
75 | for (i = 0; i < ARRAY_SIZE(test_oids); ++i) |
76 | cl_git_pass(git_oidmap_set(g_map, &test_oids[i].oid, &test_oids[i])); | |
917f60c5 | 77 | |
22a2d3d5 UG |
78 | for (i = 0; i < ARRAY_SIZE(test_oids); ++i) |
79 | cl_assert_equal_p(git_oidmap_get(g_map, &test_oids[i].oid), &test_oids[i]); | |
80 | } | |
917f60c5 | 81 | |
22a2d3d5 UG |
82 | void test_core_oidmap__get_fails_with_nonexisting_key(void) |
83 | { | |
84 | size_t i; | |
917f60c5 | 85 | |
22a2d3d5 UG |
86 | /* Do _not_ add last OID to verify that we cannot look it up */ |
87 | for (i = 0; i < ARRAY_SIZE(test_oids) - 1; ++i) | |
88 | cl_git_pass(git_oidmap_set(g_map, &test_oids[i].oid, &test_oids[i])); | |
89 | ||
90 | cl_assert_equal_p(git_oidmap_get(g_map, &test_oids[ARRAY_SIZE(test_oids) - 1].oid), NULL); | |
91 | } | |
917f60c5 | 92 | |
22a2d3d5 UG |
93 | void test_core_oidmap__setting_oid_persists(void) |
94 | { | |
95 | git_oid oids[] = { | |
96 | {{ 0x01 }}, | |
97 | {{ 0x02 }}, | |
98 | {{ 0x03 }} | |
99 | }; | |
100 | ||
101 | cl_git_pass(git_oidmap_set(g_map, &oids[0], "one")); | |
102 | cl_git_pass(git_oidmap_set(g_map, &oids[1], "two")); | |
103 | cl_git_pass(git_oidmap_set(g_map, &oids[2], "three")); | |
104 | ||
105 | cl_assert_equal_s(git_oidmap_get(g_map, &oids[0]), "one"); | |
106 | cl_assert_equal_s(git_oidmap_get(g_map, &oids[1]), "two"); | |
107 | cl_assert_equal_s(git_oidmap_get(g_map, &oids[2]), "three"); | |
108 | } | |
917f60c5 | 109 | |
22a2d3d5 UG |
110 | void test_core_oidmap__setting_existing_key_updates(void) |
111 | { | |
112 | git_oid oids[] = { | |
113 | {{ 0x01 }}, | |
114 | {{ 0x02 }}, | |
115 | {{ 0x03 }} | |
116 | }; | |
917f60c5 | 117 | |
22a2d3d5 UG |
118 | cl_git_pass(git_oidmap_set(g_map, &oids[0], "one")); |
119 | cl_git_pass(git_oidmap_set(g_map, &oids[1], "two")); | |
120 | cl_git_pass(git_oidmap_set(g_map, &oids[2], "three")); | |
121 | cl_assert_equal_i(git_oidmap_size(g_map), 3); | |
917f60c5 | 122 | |
22a2d3d5 UG |
123 | cl_git_pass(git_oidmap_set(g_map, &oids[1], "other")); |
124 | cl_assert_equal_i(git_oidmap_size(g_map), 3); | |
917f60c5 | 125 | |
22a2d3d5 | 126 | cl_assert_equal_s(git_oidmap_get(g_map, &oids[1]), "other"); |
917f60c5 | 127 | } |