Fix warning in util.h (signed vs unsigned comparison)
[libgit2.git] / tests / t0403-lists.c
blobdd1a5b5b3b7a1e3abd956a0808675001ee06347e
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) {\
17 must_be_true(n->commit->commit_time <= previous_time);\
18 previous_time = n->commit->commit_time;\
21 memset(&list, 0x0, sizeof(git_commit_list));
22 srand((unsigned int)time(NULL));
24 for (t = 0; t < 20; ++t) {
25 const int test_size = rand() % 500 + 500;
27 /* Purely random sorting test */
28 for (i = 0; i < test_size; ++i) {
29 git_commit *c = git__malloc(sizeof(git_commit));
30 c->commit_time = (time_t)rand();
32 git_commit_list_push_back(&list, c);
35 git_commit_list_timesort(&list);
36 TEST_SORTED();
37 git_commit_list_clear(&list, 1);
40 /* Try to sort list with all dates equal. */
41 for (i = 0; i < 200; ++i) {
42 git_commit *c = git__malloc(sizeof(git_commit));
43 c->commit_time = 0;
45 git_commit_list_push_back(&list, c);
48 git_commit_list_timesort(&list);
49 TEST_SORTED();
50 git_commit_list_clear(&list, 1);
52 /* Try to sort empty list */
53 git_commit_list_timesort(&list);
54 TEST_SORTED();
56 END_TEST