summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorEdward Thomson <ethomson@edwardthomson.com>2023-05-08 10:07:11 +0100
committerEdward Thomson <ethomson@edwardthomson.com>2023-05-09 17:14:08 +0100
commit0a7e32b2326c02a91f9560dfd209e56ea9fb9d49 (patch)
tree1040ae92dd88cdacce82395200ecf4f966883310 /tests
parent04cddffea9d00d5788b4f41a7dce3356089228ab (diff)
downloadlibgit2-0a7e32b2326c02a91f9560dfd209e56ea9fb9d49.tar.gz
oid: use an oid array instead of shallowarray
Users should provide us an array of object ids; we don't need a separate type. And especially, we should not be mutating user-providing values. Instead, use `git_oid *` in the shallow code.
Diffstat (limited to 'tests')
-rw-r--r--tests/libgit2/core/oidarray.c98
-rw-r--r--tests/libgit2/online/shallow.c35
-rw-r--r--tests/libgit2/transports/smart/shallowarray.c52
3 files changed, 117 insertions, 68 deletions
diff --git a/tests/libgit2/core/oidarray.c b/tests/libgit2/core/oidarray.c
new file mode 100644
index 000000000..4a9e47c70
--- /dev/null
+++ b/tests/libgit2/core/oidarray.c
@@ -0,0 +1,98 @@
+#include "clar_libgit2.h"
+
+#include "git2/oid.h"
+#include "git2/transport.h"
+
+#include "common.h"
+#include "transports/smart.h"
+#include "oid.h"
+#include "oidarray.h"
+
+#include <assert.h>
+
+#define oid_0 "c070ad8c08840c8116da865b2d65593a6bb9cd2a"
+#define oid_1 "0966a434eb1a025db6b71485ab63a3bfbea520b6"
+#define oid_2 "83834a7afdaa1a1260568567f6ad90020389f664"
+#define oid_3 "746fb4c91a7b6190bc4761adf7410afc4b59812c"
+
+void test_core_oidarray__add_and_remove_oid_from_shallowarray(void)
+{
+ git_oid oid_0_obj, oid_1_obj, oid_2_obj, oid_3_obj;
+ git_array_oid_t array = GIT_ARRAY_INIT;
+
+ git_oid__fromstr(&oid_0_obj, oid_0, GIT_OID_SHA1);
+ git_oid__fromstr(&oid_1_obj, oid_1, GIT_OID_SHA1);
+ git_oid__fromstr(&oid_2_obj, oid_2, GIT_OID_SHA1);
+ git_oid__fromstr(&oid_3_obj, oid_3, GIT_OID_SHA1);
+
+ /* add some initial ids */
+ git_oidarray__add(&array, &oid_0_obj);
+ git_oidarray__add(&array, &oid_1_obj);
+ git_oidarray__add(&array, &oid_2_obj);
+
+ cl_assert_equal_i(3, array.size);
+ cl_assert_equal_s("c070ad8c08840c8116da865b2d65593a6bb9cd2a", git_oid_tostr_s(&array.ptr[0]));
+ cl_assert_equal_s("0966a434eb1a025db6b71485ab63a3bfbea520b6", git_oid_tostr_s(&array.ptr[1]));
+ cl_assert_equal_s("83834a7afdaa1a1260568567f6ad90020389f664", git_oid_tostr_s(&array.ptr[2]));
+
+ /* don't duplicate existing ids */
+ git_oidarray__add(&array, &oid_1_obj);
+
+ cl_assert_equal_i(3, array.size);
+ cl_assert_equal_s("c070ad8c08840c8116da865b2d65593a6bb9cd2a", git_oid_tostr_s(&array.ptr[0]));
+ cl_assert_equal_s("0966a434eb1a025db6b71485ab63a3bfbea520b6", git_oid_tostr_s(&array.ptr[1]));
+ cl_assert_equal_s("83834a7afdaa1a1260568567f6ad90020389f664", git_oid_tostr_s(&array.ptr[2]));
+
+ /* remove the last id */
+ cl_assert_equal_i(1, git_oidarray__remove(&array, &oid_2_obj));
+
+ cl_assert_equal_i(2, array.size);
+ cl_assert_equal_s("c070ad8c08840c8116da865b2d65593a6bb9cd2a", git_oid_tostr_s(&array.ptr[0]));
+ cl_assert_equal_s("0966a434eb1a025db6b71485ab63a3bfbea520b6", git_oid_tostr_s(&array.ptr[1]));
+
+ /* add another id */
+ git_oidarray__add(&array, &oid_3_obj);
+
+ cl_assert_equal_i(3, array.size);
+ cl_assert_equal_s("c070ad8c08840c8116da865b2d65593a6bb9cd2a", git_oid_tostr_s(&array.ptr[0]));
+ cl_assert_equal_s("0966a434eb1a025db6b71485ab63a3bfbea520b6", git_oid_tostr_s(&array.ptr[1]));
+ cl_assert_equal_s("746fb4c91a7b6190bc4761adf7410afc4b59812c", git_oid_tostr_s(&array.ptr[2]));
+
+ /* remove the first id */
+ cl_assert_equal_i(1, git_oidarray__remove(&array, &oid_0_obj));
+
+ cl_assert_equal_i(2, array.size);
+ cl_assert_equal_s("0966a434eb1a025db6b71485ab63a3bfbea520b6", git_oid_tostr_s(&array.ptr[0]));
+ cl_assert_equal_s("746fb4c91a7b6190bc4761adf7410afc4b59812c", git_oid_tostr_s(&array.ptr[1]));
+
+ /* removing a nonexistent oid does nothing */
+ cl_assert_equal_i(0, git_oidarray__remove(&array, &oid_2_obj));
+
+ /* add another id */
+ git_oidarray__add(&array, &oid_0_obj);
+
+ cl_assert_equal_i(3, array.size);
+ cl_assert_equal_s("0966a434eb1a025db6b71485ab63a3bfbea520b6", git_oid_tostr_s(&array.ptr[0]));
+ cl_assert_equal_s("746fb4c91a7b6190bc4761adf7410afc4b59812c", git_oid_tostr_s(&array.ptr[1]));
+ cl_assert_equal_s("c070ad8c08840c8116da865b2d65593a6bb9cd2a", git_oid_tostr_s(&array.ptr[2]));
+
+ /* remove another id */
+ cl_assert_equal_i(1, git_oidarray__remove(&array, &oid_3_obj));
+
+ cl_assert_equal_i(2, array.size);
+ cl_assert_equal_s("0966a434eb1a025db6b71485ab63a3bfbea520b6", git_oid_tostr_s(&array.ptr[0]));
+ cl_assert_equal_s("c070ad8c08840c8116da865b2d65593a6bb9cd2a", git_oid_tostr_s(&array.ptr[1]));
+
+ /* remove another id */
+ cl_assert_equal_i(1, git_oidarray__remove(&array, &oid_1_obj));
+
+ cl_assert_equal_i(1, array.size);
+ cl_assert_equal_s("c070ad8c08840c8116da865b2d65593a6bb9cd2a", git_oid_tostr_s(&array.ptr[0]));
+
+ /* remove the final id */
+ cl_assert_equal_i(1, git_oidarray__remove(&array, &oid_0_obj));
+
+ cl_assert_equal_i(0, array.size);
+
+ git_array_clear(array);
+}
diff --git a/tests/libgit2/online/shallow.c b/tests/libgit2/online/shallow.c
index a889a68cd..12ef7748b 100644
--- a/tests/libgit2/online/shallow.c
+++ b/tests/libgit2/online/shallow.c
@@ -16,7 +16,8 @@ void test_online_shallow__clone_depth_zero(void)
git_str path = GIT_STR_INIT;
git_repository *repo;
git_clone_options clone_opts = GIT_CLONE_OPTIONS_INIT;
- git_array_oid_t roots = GIT_ARRAY_INIT;
+ git_oid *roots;
+ size_t roots_len;
clone_opts.fetch_opts.depth = 0;
clone_opts.remote_cb = remote_single_branch;
@@ -29,10 +30,10 @@ void test_online_shallow__clone_depth_zero(void)
cl_assert_equal_b(false, git_repository_is_shallow(repo));
/* full clones do not have shallow roots. */
- cl_git_pass(git_repository__shallow_roots(&roots, repo));
- cl_assert_equal_i(0, roots.size);
+ cl_git_pass(git_repository__shallow_roots(&roots, &roots_len, repo));
+ cl_assert_equal_i(0, roots_len);
- git_array_clear(roots);
+ git__free(roots);
git_str_dispose(&path);
git_repository_free(repo);
}
@@ -44,7 +45,8 @@ void test_online_shallow__clone_depth_one(void)
git_revwalk *walk;
git_clone_options clone_opts = GIT_CLONE_OPTIONS_INIT;
git_oid oid;
- git_array_oid_t roots = GIT_ARRAY_INIT;
+ git_oid *roots;
+ size_t roots_len;
size_t num_commits = 0;
int error = 0;
@@ -57,9 +59,9 @@ void test_online_shallow__clone_depth_one(void)
cl_assert_equal_b(true, git_repository_is_shallow(repo));
- cl_git_pass(git_repository__shallow_roots(&roots, repo));
- cl_assert_equal_i(1, roots.size);
- cl_assert_equal_s("49322bb17d3acc9146f98c97d078513228bbf3c0", git_oid_tostr_s(&roots.ptr[0]));
+ cl_git_pass(git_repository__shallow_roots(&roots, &roots_len, repo));
+ cl_assert_equal_i(1, roots_len);
+ cl_assert_equal_s("49322bb17d3acc9146f98c97d078513228bbf3c0", git_oid_tostr_s(&roots[0]));
git_revwalk_new(&walk, repo);
@@ -72,7 +74,7 @@ void test_online_shallow__clone_depth_one(void)
cl_assert_equal_i(num_commits, 1);
cl_assert_equal_i(error, GIT_ITEROVER);
- git_array_clear(roots);
+ git__free(roots);
git_str_dispose(&path);
git_revwalk_free(walk);
git_repository_free(repo);
@@ -85,7 +87,8 @@ void test_online_shallow__clone_depth_five(void)
git_revwalk *walk;
git_clone_options clone_opts = GIT_CLONE_OPTIONS_INIT;
git_oid oid;
- git_array_oid_t roots = GIT_ARRAY_INIT;
+ git_oid *roots;
+ size_t roots_len;
size_t num_commits = 0;
int error = 0;
@@ -98,11 +101,11 @@ void test_online_shallow__clone_depth_five(void)
cl_assert_equal_b(true, git_repository_is_shallow(repo));
- cl_git_pass(git_repository__shallow_roots(&roots, repo));
- cl_assert_equal_i(3, roots.size);
- cl_assert_equal_s("c070ad8c08840c8116da865b2d65593a6bb9cd2a", git_oid_tostr_s(&roots.ptr[0]));
- cl_assert_equal_s("0966a434eb1a025db6b71485ab63a3bfbea520b6", git_oid_tostr_s(&roots.ptr[1]));
- cl_assert_equal_s("83834a7afdaa1a1260568567f6ad90020389f664", git_oid_tostr_s(&roots.ptr[2]));
+ cl_git_pass(git_repository__shallow_roots(&roots, &roots_len, repo));
+ cl_assert_equal_i(3, roots_len);
+ cl_assert_equal_s("c070ad8c08840c8116da865b2d65593a6bb9cd2a", git_oid_tostr_s(&roots[0]));
+ cl_assert_equal_s("0966a434eb1a025db6b71485ab63a3bfbea520b6", git_oid_tostr_s(&roots[1]));
+ cl_assert_equal_s("83834a7afdaa1a1260568567f6ad90020389f664", git_oid_tostr_s(&roots[2]));
git_revwalk_new(&walk, repo);
@@ -115,7 +118,7 @@ void test_online_shallow__clone_depth_five(void)
cl_assert_equal_i(num_commits, 13);
cl_assert_equal_i(error, GIT_ITEROVER);
- git_array_clear(roots);
+ git__free(roots);
git_str_dispose(&path);
git_revwalk_free(walk);
git_repository_free(repo);
diff --git a/tests/libgit2/transports/smart/shallowarray.c b/tests/libgit2/transports/smart/shallowarray.c
deleted file mode 100644
index 34511c5f6..000000000
--- a/tests/libgit2/transports/smart/shallowarray.c
+++ /dev/null
@@ -1,52 +0,0 @@
-#include "clar_libgit2.h"
-
-#include "git2/oid.h"
-#include "git2/transport.h"
-
-#include "common.h"
-#include "transports/smart.h"
-#include "oid.h"
-
-#include <assert.h>
-
-#define oid_0 "c070ad8c08840c8116da865b2d65593a6bb9cd2a"
-#define oid_1 "0966a434eb1a025db6b71485ab63a3bfbea520b6"
-#define oid_2 "83834a7afdaa1a1260568567f6ad90020389f664"
-
-void test_transports_smart_shallowarray__add_and_remove_oid_from_shallowarray(void)
-{
- git_oid oid_0_obj, oid_1_obj, oid_2_obj;
- git_shallowarray *shallow_roots = git__malloc(sizeof(git_shallowarray));
- git_array_init(shallow_roots->array);
-
- git_oid__fromstr(&oid_0_obj, oid_0, GIT_OID_SHA1);
- git_oid__fromstr(&oid_1_obj, oid_1, GIT_OID_SHA1);
- git_oid__fromstr(&oid_2_obj, oid_2, GIT_OID_SHA1);
-
- git_shallowarray_add(shallow_roots, &oid_0_obj);
- git_shallowarray_add(shallow_roots, &oid_1_obj);
- git_shallowarray_add(shallow_roots, &oid_2_obj);
-
- cl_assert_equal_i(3, shallow_roots->array.size);
- cl_assert_equal_s("c070ad8c08840c8116da865b2d65593a6bb9cd2a", git_oid_tostr_s(&shallow_roots->array.ptr[0]));
- cl_assert_equal_s("0966a434eb1a025db6b71485ab63a3bfbea520b6", git_oid_tostr_s(&shallow_roots->array.ptr[1]));
- cl_assert_equal_s("83834a7afdaa1a1260568567f6ad90020389f664", git_oid_tostr_s(&shallow_roots->array.ptr[2]));
-
- git_shallowarray_remove(shallow_roots, &oid_2_obj);
-
- cl_assert_equal_i(2, shallow_roots->array.size);
- cl_assert_equal_s("c070ad8c08840c8116da865b2d65593a6bb9cd2a", git_oid_tostr_s(&shallow_roots->array.ptr[0]));
- cl_assert_equal_s("0966a434eb1a025db6b71485ab63a3bfbea520b6", git_oid_tostr_s(&shallow_roots->array.ptr[1]));
-
- git_shallowarray_remove(shallow_roots, &oid_1_obj);
-
- cl_assert_equal_i(1, shallow_roots->array.size);
- cl_assert_equal_s("c070ad8c08840c8116da865b2d65593a6bb9cd2a", git_oid_tostr_s(&shallow_roots->array.ptr[0]));
-
- git_shallowarray_remove(shallow_roots, &oid_0_obj);
-
- cl_assert_equal_i(0, shallow_roots->array.size);
-
- git_array_clear(shallow_roots->array);
- git__free(shallow_roots);
-}