Changed commit time sorting to be descending (from newest to oldest).
[libgit2.git] / tests / t0403-lists.c
blobff4fd6b1cde132c6cc6278ad7873d7047c897d2f
1 #include "test_lib.h"
2 #include "test_helpers.h"
3 #include "commit.h"
4 #include <git/odb.h>
5 #include <git/commit.h>
7 BEGIN_TEST(list_timesort_test)
9 git_commit_list list;
10 git_commit_node *n;
11 int i, t;
12 time_t previous_time;
14 #define TEST_SORTED() \
15 previous_time = INT_MAX;\
16 for (n = list.head; n != NULL; n = n->next)\
18 must_be_true(n->commit->commit_time <= previous_time);\
19 previous_time = n->commit->commit_time;\
22 memset(&list, 0x0, sizeof(git_commit_list));
23 srand(time(NULL));
25 for (t = 0; t < 20; ++t)
27 const int test_size = rand() % 500 + 500;
29 // Purely random sorting test
30 for (i = 0; i < test_size; ++i)
32 git_commit *c = git__malloc(sizeof(git_commit));
33 c->commit_time = (time_t)rand();
35 git_commit_list_push_back(&list, c);
38 git_commit_list_timesort(&list);
39 TEST_SORTED();
40 git_commit_list_clear(&list, 1);
43 // Try to sort list with all dates equal.
44 for (i = 0; i < 200; ++i)
46 git_commit *c = git__malloc(sizeof(git_commit));
47 c->commit_time = 0;
49 git_commit_list_push_back(&list, c);
52 git_commit_list_timesort(&list);
53 TEST_SORTED();
54 git_commit_list_clear(&list, 1);
56 // Try to sort empty list
57 git_commit_list_timesort(&list);
58 TEST_SORTED();
60 END_TEST