From 089c2d931b6b68dd82527430da51881d23bbd9a6 Mon Sep 17 00:00:00 2001 From: Vicent Marti Date: Sun, 23 May 2010 04:39:33 +0200 Subject: [PATCH] Add unit tests for list sorting. Signed-off-by: Vicent Marti Signed-off-by: Andreas Ericsson --- tests/t0403-lists.c | 60 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 tests/t0403-lists.c diff --git a/tests/t0403-lists.c b/tests/t0403-lists.c new file mode 100644 index 000000000..1093c8ff4 --- /dev/null +++ b/tests/t0403-lists.c @@ -0,0 +1,60 @@ +#include "test_lib.h" +#include "test_helpers.h" +#include "commit.h" +#include +#include + +BEGIN_TEST(list_sort_test) + + git_commit_list list; + git_commit_node *n; + int i, t; + time_t previous_time; + +#define TEST_SORTED() \ + previous_time = 0;\ + for (n = list.head; n != NULL; n = n->next)\ + {\ + must_be_true(n->commit->commit_time >= previous_time);\ + previous_time = n->commit->commit_time;\ + } + + memset(&list, 0x0, sizeof(git_commit_list)); + srand(time(NULL)); + + for (t = 0; t < 20; ++t) + { + const int test_size = rand() % 500 + 500; + + // Purely random sorting test + for (i = 0; i < test_size; ++i) + { + git_commit *c = git__malloc(sizeof(git_commit)); + c->commit_time = (time_t)rand(); + + git_commit_list_append(&list, c); + } + + git_commit_list_sort(&list); + TEST_SORTED(); + git_commit_list_clear(&list, 1); + } + + // Try to sort list with all dates equal. + for (i = 0; i < 200; ++i) + { + git_commit *c = git__malloc(sizeof(git_commit)); + c->commit_time = 0; + + git_commit_list_append(&list, c); + } + + git_commit_list_sort(&list); + TEST_SORTED(); + git_commit_list_clear(&list, 1); + + // Try to sort empty list + git_commit_list_sort(&list); + TEST_SORTED(); + +END_TEST