Create a basic test suite for the library and test oid functions This is a horribly simple test suite that makes it fairly easy to put together some basic function level unit tests on the library. Its patterned somewhat after the test suite in git.git, but also after the "Check" test library. Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
diff --git a/.gitignore b/.gitignore
index f5e3dd7..a6a1a6f 100644
--- a/.gitignore
+++ b/.gitignore
@@ -2,3 +2,4 @@
/include/git/config.h
*.o
*.a
+*.exe
diff --git a/Makefile b/Makefile
index 51fcd3c..8c7af5e 100644
--- a/Makefile
+++ b/Makefile
@@ -16,11 +16,19 @@ OBJS += src/os/$(OS).o
HDRS += include/git/config.h
HDRS += include/git/os/$(OS).h
+GIT_LIB = libgit2.a
-all:: libgit2.a
+TEST_OBJ = $(patsubst %.c,%.o,\
+ $(wildcard tests/t[0-9][0-9][0-9][0-9]-*.c))
+TEST_EXE = $(patsubst %.o,%.exe,$(TEST_OBJ))
+TEST_RUN = $(patsubst %.exe,%.run,$(TEST_EXE))
+
+all:: $(GIT_LIB)
clean:
- rm -f libgit2.a src/*.o
+ rm -f $(GIT_LIB)
+ rm -f src/*.o
+ rm -f tests/*.o tests/*.exe
rm -f include/git/config.h
rm -rf apidocs
@@ -28,6 +36,8 @@ apidocs:
$(DOXYGEN) api.doxygen
cp CONVENTIONS apidocs/
+test: $(TEST_RUN)
+
.c.o:
$(CC) $(BASIC_CFLAGS) $(CFLAGS) -c $< -o $@
@@ -36,10 +46,28 @@ include/git/config.h: include/git/config.h.in
mv $@+ $@
$(OBJS): $(HDRS)
-libgit2.a: $(OBJS)
- rm -f libgit2.a
- $(AR) cr libgit2.a $(OBJS)
+$(GIT_LIB): $(OBJS)
+ rm -f $(LIB)
+ $(AR) cr $(GIT_LIB) $(OBJS)
+
+T_HDR = tests/test_lib.h
+T_LIB = tests/test_lib.o
+T_MAIN_C = tests/test_main.c
+T_MAIN_O = tests/test_main.o
+
+$(T_LIB): tests/test_lib.h $(HDRS)
+$(TEST_EXE): $(T_LIB) $(T_HDR) $(T_MAIN_C) $(HDRS) $(GIT_LIB)
+
+tests/%.exe: tests/%.o
+ grep BEGIN_TEST $(patsubst %.o,%.c,$<) >tests/test_contents
+ $(CC) $(CFLAGS) -Iinclude -c $(T_MAIN_C) -o $(T_MAIN_O)
+ $(CC) -o $@ $(T_MAIN_O) $< $(T_LIB) -L. -lgit2
+ rm -f $(T_MAIN_O) tests/test_contents
+
+$(TEST_RUN): $(TEST_EXE)
+ $<
.PHONY: all
.PHONY: clean
+.PHONY: test $(TEST_RUN)
.PHONY: apidocs
diff --git a/include/git/common.h b/include/git/common.h
index 45bb348..9882255 100644
--- a/include/git/common.h
+++ b/include/git/common.h
@@ -43,6 +43,20 @@
# define GIT_EXTERN(type) type
#endif
+/** Declare a function never returns to the caller. */
+#ifdef __GNUC__
+# define GIT_NORETURN __attribute__((__noreturn__))
+#else
+# define GIT_NORETURN /* empty */
+#endif
+
+/** Declare a function's takes printf style arguments. */
+#ifdef __GNUC__
+# define GIT_FORMAT_PRINTF(a,b) __attribute__((format (printf, a, b)))
+#else
+# define GIT_FORMAT_PRINTF(a,b) /* empty */
+#endif
+
/**
* @file git/common.h
* @brief Git common platform definitions
diff --git a/tests/t0000-oid.c b/tests/t0000-oid.c
new file mode 100644
index 0000000..b0a3077
--- /dev/null
+++ b/tests/t0000-oid.c
@@ -0,0 +1,46 @@
+#include "test_lib.h"
+#include <git/oid.h>
+
+BEGIN_TEST(empty_string)
+ git_oid out;
+ must_fail(git_oid_mkstr(&out, ""));
+END_TEST
+
+BEGIN_TEST(invalid_string_moo)
+ git_oid out;
+ must_fail(git_oid_mkstr(&out, "moo"));
+END_TEST
+
+BEGIN_TEST(invalid_string_16a67770b7d8d72317c4b775213c23a8bd74f5ez)
+ git_oid out;
+ must_fail(git_oid_mkstr(&out, "16a67770b7d8d72317c4b775213c23a8bd74f5ez"));
+END_TEST
+
+BEGIN_TEST(valid_string_16a67770b7d8d72317c4b775213c23a8bd74f5e0)
+ git_oid out;
+ unsigned char exp[] = {
+ 0x16, 0xa6, 0x77, 0x70, 0xb7,
+ 0xd8, 0xd7, 0x23, 0x17, 0xc4,
+ 0xb7, 0x75, 0x21, 0x3c, 0x23,
+ 0xa8, 0xbd, 0x74, 0xf5, 0xe0,
+ };
+
+ must_pass(git_oid_mkstr(&out, "16a67770b7d8d72317c4b775213c23a8bd74f5e0"));
+ must_pass(memcmp(out.id, exp, sizeof(out.id)));
+
+ must_pass(git_oid_mkstr(&out, "16A67770B7D8D72317C4b775213C23A8BD74F5E0"));
+ must_pass(memcmp(out.id, exp, sizeof(out.id)));
+END_TEST
+
+BEGIN_TEST(valid_raw)
+ git_oid out;
+ unsigned char exp[] = {
+ 0x16, 0xa6, 0x77, 0x70, 0xb7,
+ 0xd8, 0xd7, 0x23, 0x17, 0xc4,
+ 0xb7, 0x75, 0x21, 0x3c, 0x23,
+ 0xa8, 0xbd, 0x74, 0xf5, 0xe0,
+ };
+
+ git_oid_mkraw(&out, exp);
+ must_pass(memcmp(out.id, exp, sizeof(out.id)));
+END_TEST
diff --git a/tests/test_lib.c b/tests/test_lib.c
new file mode 100644
index 0000000..06a1c29
--- /dev/null
+++ b/tests/test_lib.c
@@ -0,0 +1,99 @@
+/*
+ * This file is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License, version 2,
+ * as published by the Free Software Foundation.
+ *
+ * In addition to the permissions in the GNU General Public License,
+ * the authors give you unlimited permission to link the compiled
+ * version of this file into combinations with other programs,
+ * and to distribute those combinations without any restriction
+ * coming from the use of this file. (The General Public License
+ * restrictions do apply in other respects; for example, they cover
+ * modification of the file, and distribution when not linked into
+ * a combined executable.)
+ *
+ * This file is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; see the file COPYING. If not, write to
+ * the Free Software Foundation, 51 Franklin Street, Fifth Floor,
+ * Boston, MA 02110-1301, USA.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdarg.h>
+#include <string.h>
+#include "test_lib.h"
+
+struct test_info
+{
+ struct test_info *next;
+ const char *test_name;
+ const char *file_name;
+ int line_no;
+};
+
+static int first_test = 1;
+static struct test_info *current_test;
+
+static void show_test_result(const char *status)
+{
+ fprintf(stderr, "* %-6s %5d: %s\n",
+ status,
+ current_test->line_no,
+ current_test->test_name);
+}
+
+void test_die(const char *fmt, ...)
+{
+ va_list p;
+
+ if (current_test)
+ show_test_result("FAILED");
+
+ va_start(p, fmt);
+ vfprintf(stderr, fmt, p);
+ va_end(p);
+ fputc('\n', stderr);
+ fflush(stderr);
+ exit(128);
+}
+
+void test_begin(
+ const char *test_name,
+ const char *file_name,
+ int line_no)
+{
+ struct test_info *i = malloc(sizeof(*i));
+ if (!i)
+ test_die("cannot malloc memory");
+
+ i->test_name = test_name;
+ i->file_name = file_name;
+ i->line_no = line_no;
+ current_test = i;
+
+ if (first_test) {
+ const char *name = strrchr(i->file_name, '/');
+ if (name)
+ name = name + 1;
+ else
+ name = i->file_name;
+ fprintf(stderr, "*** %s ***\n", name);
+ first_test = 0;
+ }
+}
+
+void test_end(void)
+{
+ if (!current_test)
+ test_die("BEGIN_TEST() not used before END_TEST");
+
+ show_test_result("ok");
+ free(current_test);
+ current_test = NULL;
+}
diff --git a/tests/test_lib.h b/tests/test_lib.h
new file mode 100644
index 0000000..7e7c393
--- /dev/null
+++ b/tests/test_lib.h
@@ -0,0 +1,85 @@
+/*
+ * This file is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License, version 2,
+ * as published by the Free Software Foundation.
+ *
+ * In addition to the permissions in the GNU General Public License,
+ * the authors give you unlimited permission to link the compiled
+ * version of this file into combinations with other programs,
+ * and to distribute those combinations without any restriction
+ * coming from the use of this file. (The General Public License
+ * restrictions do apply in other respects; for example, they cover
+ * modification of the file, and distribution when not linked into
+ * a combined executable.)
+ *
+ * This file is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; see the file COPYING. If not, write to
+ * the Free Software Foundation, 51 Franklin Street, Fifth Floor,
+ * Boston, MA 02110-1301, USA.
+ */
+
+#include <stdio.h>
+#include <git/common.h>
+
+/**
+ * Declares a new test block starting, with the specified name.
+ * @param name C symbol to assign to this test's function.
+ */
+#define BEGIN_TEST(name) \
+ void testfunc__##name (void) \
+ { \
+ test_begin(#name, __FILE__, __LINE__); \
+ {
+
+/** Denote the end of a test. */
+#define END_TEST \
+ } \
+ test_end(); \
+ }
+
+/* These are internal functions for BEGIN_TEST, END_TEST. */
+extern void test_begin(const char *, const char *, int);
+extern void test_end(void);
+
+/**
+ * Abort the current test suite.
+ *
+ * This function terminates the current test suite
+ * and does not return to the caller.
+ *
+ * @param fmt printf style format string.
+ */
+extern void test_die(const char *fmt, ...)
+ GIT_NORETURN
+ GIT_FORMAT_PRINTF(1, 2);
+
+/**
+ * Evaluate a library function which must return success.
+ *
+ * The definition of "success" is the classical 0 return value.
+ * This macro makes the test suite fail if the expression evaluates
+ * to a non-zero result. It is suitable for testing most API
+ * functions in the library.
+ *
+ * @param expr the expression to evaluate, and test the result of.
+ */
+#define must_pass(expr) \
+ if (expr) test_die("line %d: %s", __LINE__, #expr)
+
+/**
+ * Evaluate a library function which must return an error.
+ *
+ * The definition of "failure" is the classical non-0 return value.
+ * This macro makes the test suite fail if the expression evaluates
+ * to 0 (aka success). It is suitable for testing most API
+ * functions in the library.
+ *
+ * @param expr the expression to evaluate, and test the result of.
+ */
+#define must_fail(expr) \
+ if (!(expr)) test_die("line %d: %s", __LINE__, #expr)
diff --git a/tests/test_main.c b/tests/test_main.c
new file mode 100644
index 0000000..a870abd
--- /dev/null
+++ b/tests/test_main.c
@@ -0,0 +1,39 @@
+/*
+ * This file is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License, version 2,
+ * as published by the Free Software Foundation.
+ *
+ * In addition to the permissions in the GNU General Public License,
+ * the authors give you unlimited permission to link the compiled
+ * version of this file into combinations with other programs,
+ * and to distribute those combinations without any restriction
+ * coming from the use of this file. (The General Public License
+ * restrictions do apply in other respects; for example, they cover
+ * modification of the file, and distribution when not linked into
+ * a combined executable.)
+ *
+ * This file is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; see the file COPYING. If not, write to
+ * the Free Software Foundation, 51 Franklin Street, Fifth Floor,
+ * Boston, MA 02110-1301, USA.
+ */
+
+#include "test_lib.h"
+
+#undef BEGIN_TEST
+#define BEGIN_TEST(name) extern void testfunc__##name(void);
+#include "test_contents"
+
+int main(int argc, char **argv)
+{
+#undef BEGIN_TEST
+#define BEGIN_TEST(name) testfunc__##name();
+#include "test_contents"
+
+ return 0;
+}