-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.c
330 lines (276 loc) · 6.55 KB
/
main.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
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
//
// main.c
// mylib
//
// Created by bikang on 17/10/25.
// Copyright (c) 2017年 bikang. All rights reserved.
//
#include <stdio.h>
#include <string.h>
#include "kheader.h"
#include "klog.h"
#include "kdaemon.h"
#include "kmempool.h"
#include "avector.h"
#include "klist.h"
#include "kstack.h"
#include "kqueue.h"
#include "kprime.h"
#include "ksort.h"
#include "kstring.h"
struct data{
int a;
};
int match_data ( void *d1, void *d2){
struct data* dd1 = (struct data*)d1;
struct data* dd2 = (struct data*)d2;
if(dd1->a > dd2->a){
return 1;
}else if(dd1->a == dd1->a){
return 0;
}else{
return -1;
}
}
void printData(struct data *d){
if(d != NULL){
printf("a=%d\n",d->a);
}
}
//( void *, void * )
int compareData(void *a, void *b){
struct data *a1 = (struct data *)a;
struct data *b1 = (struct data *)b;
if (a1->a < b1->a) {
return 1;
}
return 0;
}
//测试使用
void test_base();//简单测试
void test_log2();//日志测试
void test_mempool();//内存池测试
void put_mempool();//测试内存池
void test_avector();//测试动态数组
void test_linklist();//测试链表
void test_stack();//测试stack
void test_queue();//测试队列
void test_primer();//测试素数
void test_sort();//测试排序
void test_string();//测试string操作
void printLink2(LinkList* link){
int i;
LinkNode *node = link->head;
puts("#######start print####");
for (i=0; i<link->size; i++) {
if (node->value != NULL) {
//printf("%p\n",node);
printf("%p,%d\n",node,((struct data*)(node->value))->a);
}
node = node->next;
}
puts("#######end print####");
}
int main(int argc, const char * argv[]) {
//test_base();
//test_log();
//test_daemon();
//test_log2();
//test_mempool();
//test_avector();
//test_linklist();
//test_stack();
//test_queue();
//test_primer();
//test_sort();
test_string();
return 0;
}
//测试string的基本操作
void test_string(){
char *astr = "hi c base";
kstr *s1;
s1 = kstr_create(astr);
printf("str_le=%d,str=%s",s1->len,s1->str);
kstr_free(&s1);
}
//测试排序
void test_sort(){
//排序struct data
struct data d1;
d1.a = 8;
struct data d2;
d2.a = 11;
struct data d3;
d3.a = 5;
struct data d4;
d4.a = 1;
struct data arr[4] = {d1,d2,d3,d4};
//排序前
for (int i= 0; i<4; i++) {
printData(arr+i);
}
//插入排序
k_insert_sort(arr,4,sizeof(struct data),compareData);
puts("");
//排序后
for (int i= 0; i<4; i++) {
printData(arr+i);
}
}
//测试素数
void test_primer(){
int n = 123131;
if(is_primer(n)){
printf("%d is primer\n",n);
}
printf("next primer %d\n",next_primer(n));
}
//测试队列
void test_queue(){
KQUEUE queue = queue_new();
//放入队列
struct data d1;
d1.a = 11;
queue_enter(queue, &d1);
struct data d2;
d2.a = 12;
queue_enter(queue, &d2);
struct data d3;
d3.a = 13;
queue_enter(queue, &d3);
//弹出数据
LinkNode *node = queue_denter(queue);
printData(node->value);
//获取队列头部数据
printData(queue_peek_header(queue)->value);
//获取队列尾部数据
printData(queue_peek_tail(queue)->value);
//获取栈的所有数据
printf("size=%d\n",stack_size(queue));
queue_free(&queue, NULL);
}
//测试stack
void test_stack(){
//初始化
KSTACK stack = stack_new();
//放入数据
struct data d1;
d1.a = 11;
stack_push(stack, &d1);
struct data d2;
d2.a = 12;
stack_push(stack, &d2);
struct data d3;
d3.a = 13;
stack_push(stack, &d3);
//弹出数据
LinkNode *node = stack_pop(stack);
printData(node->value);
//获取栈顶数据
printData(stack_peek(stack)->value);
//获取栈的所有数据
printf("size=%d\n",stack_size(stack));
//释放所有数据
stack_free(&stack, NULL);
}
//测试链表
void test_linklist(){
puts("start linklist");
LinkList* link = link_new();
struct data d1;
d1.a = 11;
link_add_last(link, &d1);
struct data d2;
d2.a = 12;
link_add_first(link, &d2);
printLink(link);
//insert
struct data d3;
d3.a = 13;
link_insert(link, 1, &d3);
printLink(link);
//remove
LinkNode *node = link_remove(link, 1);
link_node_free(node,NULL);
//insert
struct data d4;
d4.a = 14;
link_insert(link, 1, &d4);
printLink(link);
//insert
struct data d5;
d5.a = 15;
link_insert(link, 1, &d5);
printLink(link);
printLink2(link);
//remove first
node = link_remove_first(link);
link_node_free(node,NULL);
//remove last
node = link_remove_last(link);
link_node_free(node,NULL);
//get node
node = link_get(link, 0);
printf("node=%d\n",((struct data*)(node->value))->a);
node = link_get(link, 1);
printf("node=%d\n",((struct data*)(node->value))->a);
//set node data
struct data d6;
d6.a = 33;
link_set(link, 0, &d6);
printLink2(link);
//search and find
struct data d7;
d7.a = 99;
link_set_obj(link,&d6,&d7,match_data);
link_insert(link, 1, &d6);
printLink2(link);
link_remove_obj(link, &d6, match_data);
printLink2(link);
}
void test_avector(){
printf("start test vector!\n");
Avector *avl = NewAvector();
Avector(avl, 1);
char *str = "abc";
Avector_Append(avl, str);
char *str2 = "eee";
Avector_Append(avl, str2);
printf("size=%d,cap=%d\n",avl->_size,avl->_cap);
printf("avl[0]=%s\n",Avector_Get(avl,0));
printf("avl[1]=%s\n",Avector_Get(avl,0));
Av_Free(avl);
Av_Destroy(avl);
}
void test_mempool(){
kmempool*km = kmempool_init(96, 10);
put_mempool(km);
char *ptr = kmempool_malloc(km, 30);
char *qtr;
printf("\n\n%d\n",sizeof(kblock*));
qtr = ptr+sizeof(kblock*);
strcpy(qtr, "abc");
qtr[3] = '\0';
printf("ptr=%s\n",qtr);
kmempool_free(km, ptr);
puts("after free\n");
put_mempool(km);
kmempool_destry(km);
}
void put_mempool(kmempool*km){
kblock *pos;
pos = km->free_head;
while (pos != NULL) {
printf("%p\n",pos);
pos = pos->next;
}
}
void test_log2(){
logInit("/Users/kang/Documents/CProject/php-5.6.6/ext/new_ext/t.log", PUT_SFILE);
klogTrace("%s","abc");
}
void test_base(){
int i = 1;
printf("a start = %d",i);
}