forked from swoole/swoole-src
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathu2.c
96 lines (82 loc) · 1.62 KB
/
u2.c
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
#include "swoole.h"
#include "tests.h"
typedef struct _test_node
{
struct _test_node *next, *prev;
void *data;
int exectime;
} test_node;
static struct
{
test_node *root;
} timer;
void list_add(int exectime);
void list_dump();
swUnitTest(list_test1)
{
int i = 0;
for (i = 0; i < 10000; i++)
{
list_add(swoole_system_random(10000, 99999));
}
// list_add(1200);
// list_add(1800);
// list_add(800);
// list_add(900);
// list_add(900);
// list_add(200);
// list_add(2000);
// list_add(700);
list_dump();
}
void list_dump()
{
test_node *tmp = timer.root;
printf("root=%d\n", tmp->exectime);
while (tmp->next)
{
tmp = tmp->next;
printf("node=%d\n", tmp->exectime);
}
}
void list_add(int exectime)
{
test_node *node = malloc(sizeof(test_node));
bzero(node, sizeof(test_node));
node->data = NULL;
node->exectime = exectime;
if (timer.root == NULL)
{
timer.root = node;
return;
}
test_node *tmp = timer.root;
while (1)
{
if (tmp->exectime >= node->exectime)
{
node->prev = tmp->prev;
node->next = tmp;
if (node->prev)
{
node->prev->next = node;
}
tmp->prev = node;
if (tmp == timer.root)
{
timer.root = node;
}
break;
}
else if (tmp->next)
{
tmp = tmp->next;
}
else
{
tmp->next = node;
node->prev = tmp;
break;
}
}
}