aboutsummaryrefslogtreecommitdiffstats
path: root/t/unit-tests/t-reftable-basics.c
diff options
context:
space:
mode:
authorChandra Pratap <chandrapratap3519@gmail.com>2024-05-29 22:29:27 +0530
committerJunio C Hamano <gitster@pobox.com>2024-05-30 07:30:10 -0700
commitb34116a30ce5ae19e303725d6e1fd0f558d36dce (patch)
tree6de3ee1094caaabd8786dc7a8b454818bc41813e /t/unit-tests/t-reftable-basics.c
parent786a3e4b8d754d2b14b1208b98eeb0a554ef19a8 (diff)
downloadgit-b34116a30ce5ae19e303725d6e1fd0f558d36dce.tar.gz
t: move reftable/basics_test.c to the unit testing framework
reftable/basics_test.c exercise the functions defined in reftable/basics.{c, h}. Migrate reftable/basics_test.c to the unit testing framework. Migration involves refactoring the tests to use the unit testing framework instead of reftable's test framework. Mentored-by: Patrick Steinhardt <ps@pks.im> Mentored-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Chandra Pratap <chandrapratap3519@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 't/unit-tests/t-reftable-basics.c')
-rw-r--r--t/unit-tests/t-reftable-basics.c102
1 files changed, 102 insertions, 0 deletions
diff --git a/t/unit-tests/t-reftable-basics.c b/t/unit-tests/t-reftable-basics.c
new file mode 100644
index 0000000000..99e6c89120
--- /dev/null
+++ b/t/unit-tests/t-reftable-basics.c
@@ -0,0 +1,102 @@
+/*
+Copyright 2020 Google LLC
+
+Use of this source code is governed by a BSD-style
+license that can be found in the LICENSE file or at
+https://developers.google.com/open-source/licenses/bsd
+*/
+
+#include "test-lib.h"
+#include "reftable/basics.h"
+
+struct integer_needle_lesseq_args {
+ int needle;
+ int *haystack;
+};
+
+static int integer_needle_lesseq(size_t i, void *_args)
+{
+ struct integer_needle_lesseq_args *args = _args;
+ return args->needle <= args->haystack[i];
+}
+
+static void test_binsearch(void)
+{
+ int haystack[] = { 2, 4, 6, 8, 10 };
+ struct {
+ int needle;
+ size_t expected_idx;
+ } testcases[] = {
+ {-9000, 0},
+ {-1, 0},
+ {0, 0},
+ {2, 0},
+ {3, 1},
+ {4, 1},
+ {7, 3},
+ {9, 4},
+ {10, 4},
+ {11, 5},
+ {9000, 5},
+ };
+
+ for (size_t i = 0; i < ARRAY_SIZE(testcases); i++) {
+ struct integer_needle_lesseq_args args = {
+ .haystack = haystack,
+ .needle = testcases[i].needle,
+ };
+ size_t idx;
+
+ idx = binsearch(ARRAY_SIZE(haystack), &integer_needle_lesseq, &args);
+ check_int(idx, ==, testcases[i].expected_idx);
+ }
+}
+
+static void test_names_length(void)
+{
+ char *a[] = { "a", "b", NULL };
+ check_int(names_length(a), ==, 2);
+}
+
+static void test_parse_names_normal(void)
+{
+ char in[] = "a\nb\n";
+ char **out = NULL;
+ parse_names(in, strlen(in), &out);
+ check_str(out[0], "a");
+ check_str(out[1], "b");
+ check(!out[2]);
+ free_names(out);
+}
+
+static void test_parse_names_drop_empty(void)
+{
+ char in[] = "a\n\n";
+ char **out = NULL;
+ parse_names(in, strlen(in), &out);
+ check_str(out[0], "a");
+ check(!out[1]);
+ free_names(out);
+}
+
+static void test_common_prefix(void)
+{
+ struct strbuf s1 = STRBUF_INIT;
+ struct strbuf s2 = STRBUF_INIT;
+ strbuf_addstr(&s1, "abcdef");
+ strbuf_addstr(&s2, "abc");
+ check_int(common_prefix_size(&s1, &s2), ==, 3);
+ strbuf_release(&s1);
+ strbuf_release(&s2);
+}
+
+int cmd_main(int argc, const char *argv[])
+{
+ TEST(test_common_prefix(), "common_prefix_size works");
+ TEST(test_parse_names_normal(), "parse_names works for basic input");
+ TEST(test_parse_names_drop_empty(), "parse_names drops empty string");
+ TEST(test_binsearch(), "binary search with binsearch works");
+ TEST(test_names_length(), "names_length retuns size of a NULL-terminated string array");
+
+ return test_done();
+}