/
tests.c.txt
executable file
·1003 lines (811 loc) · 26 KB
/
tests.c.txt
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* $Id: tests.c.txt 1267 2012-07-23 19:10:28Z jiri $
*
* ***** BEGIN BSD LICENSE BLOCK *****
*
* Copyright (c) 2009-2010, Jiri Hnidek
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* ***** END BSD LICENSE BLOCK *****
*
* Authors: Jiri Hnidek <jiri.hnidek@tul.cz>
*
*/
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include "verse.h"
#include "v_in_queue.h"
#include "v_out_queue.h"
#include "v_sys_commands.h"
#include "v_fake_commands.h"
#include "v_node_commands.h"
#include "v_common.h"
#include "v_history.h"
#include "v_list.h"
int test_ack_nak_history(void)
{
int i;
struct Ack_Nak_Cmd ack_nak;
struct AckNakHistory hist;
v_ack_nak_history_init(&hist);
ack_nak.id = CMD_ACK_ID;
ack_nak.pay_id = 0;
printf("Added: ACK CMD, 0\n");
v_ack_nak_history_add_cmd(&hist, &ack_nak);
v_ack_nak_history_print(&hist);
for(i=1; i<10; i++) {
if((rand() / (RAND_MAX + 1.0)) > 0.5) {
ack_nak.id = CMD_ACK_ID;
printf("Added: ACK CMD, %d\n", i);
} else {
ack_nak.id = CMD_NAK_ID;
printf("Added: NAK CMD, %d\n", i);
}
ack_nak.pay_id = i;
v_ack_nak_history_add_cmd(&hist, &ack_nak);
v_ack_nak_history_print(&hist);
}
ack_nak.id = CMD_ACK_ID;
ack_nak.pay_id = 10;
printf("Added: ACK CMD, 10\n");
v_ack_nak_history_add_cmd(&hist, &ack_nak);
v_ack_nak_history_print(&hist);
printf("\n\n");
for(i=0; i<11; i+=2) {
printf("Removed: ANK_ID: %d\n", i);
v_ack_nak_history_remove_cmds(&hist, i);
v_ack_nak_history_print(&hist);
}
return EXIT_SUCCESS;
}
#if 0
void *add_fake_cmds_thread(void *arg)
{
struct VNetQueueList *queue_list = (struct VNetQueueList*)arg;
struct Connect_Accept_Cmd data;
int i;
printf("Add items\n\n");
v_queue_print(queue_list->queues[FAKE_CMD_CONNECT_ACCEPT]);
for(i=0; i<15; i++) {
data.avatar_id = i;
data.user_id = i;
v_queue_push_item(queue_list->queues[FAKE_CMD_CONNECT_ACCEPT], &data);
printf("ADDED: avatar_id: %d, user_id: %d\n\n", data.avatar_id, data.user_id);
v_queue_print(queue_list->queues[FAKE_CMD_CONNECT_ACCEPT]);
sleep(1);
}
pthread_exit(NULL);
return NULL;
}
void *remove_fake_cmds_thread(void *arg)
{
struct VNetQueueList *queue_list = (struct VNetQueueList*)arg;
struct Connect_Accept_Cmd data;
int i;
printf("Remove items\n\n");
sleep(5);
for(i=0; i<10; i++) {
if(v_queue_pop_item(queue_list->queues[FAKE_CMD_CONNECT_ACCEPT], &data)!=NULL) {
printf("REMOVED: avatar_id: %d, user_id: %d\n\n", data.avatar_id, data.user_id);
}
v_queue_print(queue_list->queues[FAKE_CMD_CONNECT_ACCEPT]);
sleep(2);
}
pthread_exit(NULL);
return NULL;
}
int test_queues_fake(void)
{
struct VNetQueueList queue_list;
pthread_t add_thread, rem_thread;
void *thread_result;
v_out_queue_init(&queue_list);
if( (pthread_create(&add_thread, NULL, add_fake_cmds_thread, (void*)&queue_list)) != 0 ) {
perror("Thread (A) creation failed.\n");
return EXIT_FAILURE;
}
if( (pthread_create(&rem_thread, NULL, remove_fake_cmds_thread, (void*)&queue_list)) != 0 ) {
perror("Thread (B) creation failed.\n");
return EXIT_FAILURE;
}
if( (pthread_join(add_thread, &thread_result)) != 0 ) {
perror("Thread (A) join failed.\n");
return EXIT_FAILURE;
}
if( (pthread_join(rem_thread, &thread_result)) != 0 ) {
perror("Thread (B) join failed.\n");
return EXIT_FAILURE;
}
v_out_queue_destroy(&queue_list);
return EXIT_SUCCESS;
}
void *add_node_cmds_thread(void *arg)
{
struct VNetQueueList *queue_list = (struct VNetQueueList*)arg;
struct Node_Destroy_Cmd node_cmd;
int i;
printf("Add node cmds\n\n");
v_queue_print(queue_list->queues[FAKE_CMD_CONNECT_ACCEPT]);
for(i=0; i<128; i++) {
node_cmd.id = CMD_NODE_DESTROY;
node_cmd.addr.node_id = i/2;
v_queue_push_item(queue_list->queues[CMD_NODE_DESTROY], &node_cmd);
printf("ADDED: id: %d, node_id: %d\n\n",
node_cmd.id, node_cmd.addr.node_id);
v_queue_print(queue_list->queues[CMD_NODE_DESTROY]);
usleep(10000);
}
pthread_exit(NULL);
return NULL;
}
void *remove_node_cmds_thread(void *arg)
{
struct VNetQueueList *queue_list = (struct VNetQueueList*)arg;
struct Node_Destroy_Cmd node_cmd;
int i;
printf("Remove node cmds\n\n");
sleep(2);
for(i=0; i<64; i++) {
if(v_queue_pop_item(queue_list->queues[CMD_NODE_DESTROY], &node_cmd)!=NULL) {
printf("REMOVED: id: %d, node_id: %d\n\n",
node_cmd.id, node_cmd.addr.node_id);
}
v_queue_print(queue_list->queues[CMD_NODE_DESTROY]);
usleep(10000);
}
pthread_exit(NULL);
return NULL;
}
int test_queues_node(void)
{
struct VNetQueueList queue_list;
pthread_t add_thread, rem_thread;
void *thread_result;
v_out_queue_init(&queue_list);
if( (pthread_create(&add_thread, NULL, add_node_cmds_thread, (void*)&queue_list)) != 0 ) {
perror("Thread (A) creation failed.\n");
return EXIT_FAILURE;
}
if( (pthread_create(&rem_thread, NULL, remove_node_cmds_thread, (void*)&queue_list)) != 0 ) {
perror("Thread (B) creation failed.\n");
return EXIT_FAILURE;
}
if( (pthread_join(add_thread, &thread_result)) != 0 ) {
perror("Thread (A) join failed.\n");
return EXIT_FAILURE;
}
if( (pthread_join(rem_thread, &thread_result)) != 0 ) {
perror("Thread (B) join failed.\n");
return EXIT_FAILURE;
}
v_out_queue_destroy(&queue_list);
return EXIT_SUCCESS;
}
#endif
/* Example item structure, that could be added to the linked list. It has to
* contain prev and next pointer. It should contain its unique id. */
typedef struct ExampleItem {
struct ExampleItem *prev, *next;
uint32 id;
} ExampleItem;
int test_list(void)
{
struct VArrayBase ab;
struct ExampleItem *item;
int i;
uint32 index, ret;
v_array_init(&ab);
for(i=0; i<10; i++) {
item = (struct ExampleItem*)malloc(sizeof(struct ExampleItem));
ret = v_array_add_item(&ab, item, &index);
if(ret == 1) {
item->id = index;
printf("Added item, id: %d\n", index);
} else {
printf("Failed to add item\n");
}
}
for(i=11; i>=0; i--) {
item = v_array_find_item(&ab, i);
if(item != NULL) {
ret = v_array_remove_item(&ab, item->id);
if(ret == 1) {
printf("Item, id: %d removed\n", i);
} else {
printf("Failed to remove item, id %d\n", i);
}
} else {
printf("Unable to remove item, id: %d\n", i);
}
}
v_array_free(&ab);
return EXIT_SUCCESS;
}
/**
* Simple address for testing collisions
*/
typedef struct Simple_Cmd_Addr {
uint16 id;
} Simple_Cmd_Addr;
/**
* Simple structure for testing hashed linked list
*/
typedef struct Simple_Cmd {
uint8 id;
struct Simple_Cmd_Addr addr;
} Simple_Cmd;
/**
* \brief This function does basic test for adding simple structure
*/
int test_hash(void)
{
struct VHashArrayBase *hash_array;
struct VBucket *vbucket;
struct Simple_Cmd cmd;
uint16 cmd_id;
uint8 id;
int ret;
hash_array = (struct VHashArrayBase *)malloc(sizeof(struct VHashArrayBase));
v_hash_array_init(hash_array,
HASH_MOD_256 | HASH_COPY_BUCKET,
(char*)&(cmd.addr) - (char*)&(cmd),
sizeof(struct Simple_Cmd_Addr));
printf("sizeof cmd: %ld\n", (long int)sizeof(struct Simple_Cmd));
printf("key offset: %d, key size: %d\n", hash_array->key_offset, hash_array->key_size);
/* Adding first command */
cmd.id = id = 0;
cmd.addr.id = cmd_id = 5;
printf("Adding item: %d ...\n", cmd_id);
vbucket = v_hash_array_add_item(hash_array, &cmd, sizeof(cmd));
if(vbucket != NULL) printf("Item added\n");
else printf("Item not added\n");
/* Adding second command with collision */
cmd.id = id = 1;
cmd.addr.id = cmd_id = 256 + 5;
printf("Adding cmd: %d ...\n", cmd_id);
vbucket = v_hash_array_add_item(hash_array, &cmd, sizeof(cmd));
if(vbucket != NULL) printf("Item added\n");
else printf("Item not added\n");
/* Adding third command with collision */
cmd.id = id = 2;
cmd.addr.id = cmd_id = 2*256 + 5;
printf("Adding cmd: %d ...\n", cmd_id);
vbucket = v_hash_array_add_item(hash_array, &cmd, sizeof(cmd));
if(vbucket != NULL) printf("Item added\n");
else printf("Item not added\n");
if(0) {
/* Finding first command */
cmd.id = id = 0;
cmd.addr.id = cmd_id = 5;
printf("Finding item: %d ...\n", cmd_id);
vbucket = v_hash_array_find_item(hash_array, (void*)&cmd);
if(vbucket != NULL) {
printf("Found: %d\n", ((struct Simple_Cmd*)(vbucket->data))->addr.id);
} else {
printf("Item not found\n");
}
/* Finding second command */
cmd.id = id = 1;
cmd.addr.id = cmd_id = 256 + 5;
printf("Finding item: %d ...\n", cmd_id);
vbucket = v_hash_array_find_item(hash_array, (void*)&cmd);
if(vbucket != NULL) {
printf("Found: %d\n", ((struct Simple_Cmd*)(vbucket->data))->addr.id);
} else {
printf("Item not found\n");
}
/* Finding third command */
cmd.id = id = 2;
cmd.addr.id = cmd_id = 2*256 + 5;
printf("Finding item: %d ...\n", cmd_id);
vbucket = v_hash_array_find_item(hash_array, (void*)&cmd);
if(vbucket != NULL) {
printf("Found: %d\n", ((struct Simple_Cmd*)(vbucket->data))->addr.id);
} else {
printf("Item not found\n");
}
}
if(1) {
/* Removing first item */
cmd.id = id = 0;
cmd.addr.id = cmd_id = 5;
printf("Removing item: %d ...\n", cmd_id);
ret = v_hash_array_remove_item(hash_array, &cmd);
if(ret == 1) printf("Item removed\n");
else printf("Item not removed\n");
}
if(1) {
/* Removing second item */
cmd.id = id = 1;
cmd.addr.id = cmd_id = 256 + 5;
printf("Removing item: %d ...\n", cmd_id);
ret = v_hash_array_remove_item(hash_array, &cmd);
if(ret == 1) printf("Item removed\n");
else printf("Item not removed\n");
}
if(1) {
/* Removing last item */
cmd.id = id = 2;
cmd.addr.id = cmd_id = 2*256 + 5;
printf("Removing item: %d ...\n", cmd_id);
ret = v_hash_array_remove_item(hash_array, &cmd);
if(ret == 1) printf("Item removed\n");
else printf("Item not removed\n");
}
/* Adding 4th command with collision */
cmd.id = id = 3;
cmd.addr.id = cmd_id = 3*256 + 5;
printf("Adding cmd: %d ...\n", cmd_id);
vbucket = v_hash_array_add_item(hash_array, &cmd, sizeof(cmd));
if(vbucket != NULL) printf("Item added\n");
else printf("Item not added\n");
v_hash_array_destroy(hash_array);
free(hash_array);
return EXIT_SUCCESS;
}
typedef struct Example_Cmd_Addr {
uint32 node_id;
uint16 taggroup_id;
uint16 tag_id;
} Example_Cmd_Addr;
typedef struct Example_Cmd {
uint8 id;
struct Example_Cmd_Addr addr;
} Example_Cmd;
int test_hash_256(void)
{
struct VHashArrayBase *hash_array;
struct VBucket *vbucket;
struct Example_Cmd cmd;
uint32 node_id, init_node_id;
uint16 taggroup_id;
uint16 tag_id, init_tag_id;
uint16 count, collisions;
int i, ret;
hash_array = (struct VHashArrayBase *)malloc(sizeof(struct VHashArrayBase));
v_hash_array_init(hash_array,
HASH_MOD_256 | HASH_COPY_BUCKET,
(char*)&(cmd.addr) - (char*)&(cmd),
sizeof(struct Example_Cmd_Addr));
printf("sizeof cmd: %ld\n", (long int)sizeof(struct Example_Cmd));
printf("key offset: %d, key size: %d\n", hash_array->key_offset, hash_array->key_size);
init_node_id = rand();
init_tag_id = rand();
/* Adding items */
for(node_id=init_node_id, taggroup_id=init_tag_id, tag_id=init_tag_id;
node_id < init_node_id+1000;
node_id+=1, taggroup_id+=3, tag_id+=6)
{
cmd.addr.node_id = node_id;
cmd.addr.taggroup_id = taggroup_id;
cmd.addr.tag_id = tag_id;
printf("Adding cmd: %d, %d, %d ...\n",
node_id, taggroup_id, tag_id);
vbucket = v_hash_array_add_item(hash_array, &cmd, sizeof(struct Example_Cmd));
if(vbucket != NULL) {
printf("Added cmd: %d, %d, %d to the bucket: %p\n",
node_id, taggroup_id, tag_id, (void*)vbucket);
} else {
printf("Adding cmd: %d, %d, %d FAILED.\n",
node_id, taggroup_id, tag_id);
}
}
count = v_hash_array_count_items(hash_array);
collisions = 0;
for(i=0; i<hash_array->length; i++) {
if(hash_array->buckets[i].next != NULL) {
struct VBucketP *vbucket_p = hash_array->buckets[i].next;
while(vbucket_p != NULL) {
collisions++;
vbucket_p = vbucket_p->next;
}
}
}
printf("\n\n%d collisions in %d items\n", collisions, count);
/* Finding items */
for(node_id=init_node_id, taggroup_id=init_tag_id, tag_id=init_tag_id;
node_id < init_node_id+1000;
node_id+=1, taggroup_id+=3, tag_id+=6)
{
cmd.addr.node_id = node_id;
cmd.addr.taggroup_id = taggroup_id;
cmd.addr.tag_id = tag_id;
printf("Trying to find cmd: %d, %d, %d ...\n",
node_id, taggroup_id, tag_id);
/*printf("\t");
for(m=0; m < sizeof(struct Example_Cmd_Addr); m++) {
printf("%2X ", *(((uint8*)&cmd.addr)+m));
}
printf("\n");*/
vbucket = v_hash_array_find_item(hash_array, (void*)&cmd);
if(vbucket != NULL) {
struct Example_Cmd *fcmd;
printf("Cmd: %d, %d, %d found: %p\n",
node_id, taggroup_id, tag_id, (void*)vbucket);
fcmd = (struct Example_Cmd *)vbucket->data;
printf("\tFound Cmd: %d, %d, %d\n",
fcmd->addr.node_id, fcmd->addr.taggroup_id, fcmd->addr.tag_id);
} else {
printf("Cmd: %d, %d, %d NOT FOUND.\n",
node_id, taggroup_id, tag_id);
}
}
/* Removing items */
for(node_id=init_node_id, taggroup_id=init_tag_id, tag_id=init_tag_id;
node_id < init_node_id+50;
node_id+=1, taggroup_id+=3, tag_id+=6)
{
cmd.addr.node_id = node_id;
cmd.addr.taggroup_id = taggroup_id;
cmd.addr.tag_id = tag_id;
printf("Trying to remove cmd: %d, %d, %d ...\n",
node_id, taggroup_id, tag_id);
ret = v_hash_array_remove_item(hash_array, &cmd);
if(ret==1) {
printf("Cmd: %d, %d, %d removed\n",
node_id, taggroup_id, tag_id);
} else {
printf("Cmd: %d, %d, %d NOT REMOVED.\n",
node_id, taggroup_id, tag_id);
}
}
v_hash_array_destroy(hash_array);
free(hash_array);
return EXIT_SUCCESS;
}
int test_hash_65536(void)
{
struct VHashArrayBase *hash_array;
struct VBucket *vbucket;
struct Example_Cmd cmd;
uint32 node_id, init_node_id;
uint16 taggroup_id;
uint16 tag_id, init_tag_id;
uint16 count, collisions;
int i, ret;
hash_array = (struct VHashArrayBase *)malloc(sizeof(struct VHashArrayBase));
v_hash_array_init(hash_array,
HASH_MOD_65536 | HASH_COPY_BUCKET,
(char*)&(cmd.addr) - (char*)&(cmd),
sizeof(struct Example_Cmd_Addr));
printf("sizeof cmd: %ld\n", (long int)sizeof(struct Example_Cmd));
printf("offset: %d, size: %d\n", hash_array->key_offset, hash_array->key_size);
init_node_id = rand();
init_tag_id = rand();
/* Adding items */
for(node_id=init_node_id, taggroup_id=init_tag_id, tag_id=init_tag_id;
node_id < init_node_id+100000;
node_id+=1011, taggroup_id+=317, tag_id+=632)
{
cmd.addr.node_id = node_id;
cmd.addr.taggroup_id = taggroup_id;
cmd.addr.tag_id = tag_id;
printf("Adding cmd: %d, %d, %d ...\n",
node_id, taggroup_id, tag_id);
vbucket = v_hash_array_add_item(hash_array, &cmd, sizeof(struct Example_Cmd));
if(vbucket != NULL) {
printf("Added cmd: %d, %d, %d to the bucket: %p\n",
node_id, taggroup_id, tag_id, (void*)vbucket);
} else {
printf("Adding cmd: %d, %d, %d FAILED.\n",
node_id, taggroup_id, tag_id);
}
}
count = v_hash_array_count_items(hash_array);
collisions = 0;
for(i=0; i<hash_array->length; i++) {
if(hash_array->buckets[i].next != NULL) {
struct VBucketP *vbucket_p = hash_array->buckets[i].next;
while(vbucket_p != NULL) {
collisions++;
vbucket_p = vbucket_p->next;
}
}
}
printf("\n\n%d collisions in %d items\n", collisions, count);
/* Finding items */
for(node_id=init_node_id, taggroup_id=init_tag_id, tag_id=init_tag_id;
node_id < init_node_id+100000;
node_id+=1011, taggroup_id+=317, tag_id+=632)
{
cmd.addr.node_id = node_id;
cmd.addr.taggroup_id = taggroup_id;
cmd.addr.tag_id = tag_id;
printf("Trying to find cmd: %d, %d, %d ...\n",
node_id, taggroup_id, tag_id);
/*printf("\t");
for(m=0; m < sizeof(struct Example_Cmd_Addr); m++) {
printf("%2X ", *(((uint8*)&cmd.addr)+m));
}
printf("\n");*/
vbucket = v_hash_array_find_item(hash_array, (void*)&cmd);
if(vbucket != NULL) {
struct Example_Cmd *fcmd;
printf("Cmd: %d, %d, %d found: %p\n",
node_id, taggroup_id, tag_id, (void*)vbucket);
fcmd = (struct Example_Cmd *)vbucket->data;
printf("\tFound Cmd: %d, %d, %d\n",
fcmd->addr.node_id, fcmd->addr.taggroup_id, fcmd->addr.tag_id);
} else {
printf("Cmd: %d, %d, %d NOT FOUND.\n",
node_id, taggroup_id, tag_id);
}
}
/* Removing items */
for(node_id=init_node_id, taggroup_id=init_tag_id, tag_id=init_tag_id;
node_id < init_node_id+50000;
node_id+=1011, taggroup_id+=317, tag_id+=632)
{
cmd.addr.node_id = node_id;
cmd.addr.taggroup_id = taggroup_id;
cmd.addr.tag_id = tag_id;
printf("Trying to remove cmd: %d, %d, %d ...\n",
node_id, taggroup_id, tag_id);
ret = v_hash_array_remove_item(hash_array, &cmd);
if(ret==1) {
printf("Cmd: %d, %d, %d removed\n",
node_id, taggroup_id, tag_id);
} else {
printf("Cmd: %d, %d, %d NOT REMOVED.\n",
node_id, taggroup_id, tag_id);
}
}
v_hash_array_destroy(hash_array);
free(hash_array);
return EXIT_SUCCESS;
}
int test_history(void)
{
struct VPacket_History history;
struct VSent_Packet *vpacket;
uint32 packet_id = 0;
struct Node_Create_Cmd node_create;
struct Node_Destroy_Cmd node_destroy;
struct Node_Subscribe_Cmd node_subscribe;
/* struct Node_Unsubscribe_Cmd node_unsubscribe;*/
v_packet_history_init(&history);
packet_id = 0;
vpacket = v_packet_history_add_packet(&history, packet_id);
printf("Added packet: %d\n\n", packet_id);
node_subscribe.id = CMD_NODE_SUBSCRIBE;
node_subscribe.addr.node_id = 0;
v_packet_history_add_cmd(&history,
vpacket,
(struct Generic_Cmd*)&node_subscribe);
node_create.id = CMD_NODE_CREATE;
node_create.addr.user_id = 1234;
node_create.addr.parent_id = 65536;
node_create.node_id = VRS_RESERVED_NODE_ID;
v_packet_history_add_cmd(&history,
vpacket,
(struct Generic_Cmd*)&node_create);
v_print_packet_history(&history);
printf("\n");
packet_id = 1;
vpacket = v_packet_history_add_packet(&history, packet_id);
printf("Added packet: %d\n\n", packet_id);
node_subscribe.id = CMD_NODE_SUBSCRIBE;
node_subscribe.addr.node_id = 0;
v_packet_history_add_cmd(&history,
vpacket,
(struct Generic_Cmd*)&node_subscribe);
node_create.id = CMD_NODE_CREATE;
node_create.addr.user_id = 1234;
node_create.addr.parent_id = 65536;
node_create.node_id = VRS_RESERVED_NODE_ID;
v_packet_history_add_cmd(&history,
vpacket,
(struct Generic_Cmd*)&node_create);
node_destroy.id = CMD_NODE_DESTROY;
node_destroy.addr.node_id = 100;
v_packet_history_add_cmd(&history,
vpacket,
(struct Generic_Cmd*)&node_destroy);
node_destroy.id = CMD_NODE_DESTROY;
node_destroy.addr.node_id = 100+65535; /* Test of collision */
v_packet_history_add_cmd(&history,
vpacket,
(struct Generic_Cmd*)&node_destroy);
v_print_packet_history(&history);
printf("\n");
packet_id = 2;
vpacket = v_packet_history_add_packet(&history, packet_id);
printf("Added packet: %d\n\n", packet_id);
node_subscribe.id = CMD_NODE_SUBSCRIBE;
node_subscribe.addr.node_id = 1;
v_packet_history_add_cmd(&history,
vpacket,
(struct Generic_Cmd*)&node_subscribe);
v_print_packet_history(&history);
printf("\n");
packet_id = 0;
v_packet_history_rem_packet(&history, packet_id);
printf("Removed packet: %d\n\n", packet_id);
v_print_packet_history(&history);
printf("\n");
packet_id = 1;
v_packet_history_rem_packet(&history, packet_id);
printf("Removed packet: %d\n\n", packet_id);
v_print_packet_history(&history);
printf("\n");
packet_id = 2;
v_packet_history_rem_packet(&history, packet_id);
printf("Removed packet: %d\n\n", packet_id);
v_print_packet_history(&history);
printf("\n");
return EXIT_SUCCESS;
}
int test_out_queue(void)
{
struct VOutQueue queue_list;
struct Generic_Cmd *cmd;
struct Connect_Accept_Cmd connect_accept;
struct Node_Create_Cmd node_create;
struct Node_Destroy_Cmd node_destroy;
uint8 prio, max, min;
uint16 count, len;
int8 share;
v_out_queue_init(&queue_list);
connect_accept.id = FAKE_CMD_CONNECT_ACCEPT;
connect_accept.user_id = 1001;
connect_accept.avatar_id = 67000;
node_create.id = CMD_NODE_CREATE;
node_create.addr.user_id = 100;
node_create.addr.parent_id = 2;
node_create.node_id = 1000;
node_destroy.id = CMD_NODE_DESTROY;
node_destroy.addr.node_id = 1001;
printf("Pushing commands to queues\n\n");
v_cmd_print(VRS_PRINT_DEBUG_MSG, (struct Generic_Cmd*)&connect_accept);
v_cmd_print(VRS_PRINT_DEBUG_MSG, (struct Generic_Cmd*)&node_create);
v_cmd_print(VRS_PRINT_DEBUG_MSG, (struct Generic_Cmd*)&node_destroy);
v_out_queue_push_tail(&queue_list, 128, (struct Generic_Cmd*)&connect_accept);
v_out_queue_push_tail(&queue_list, 134, (struct Generic_Cmd*)&node_create);
v_out_queue_push_tail(&queue_list, 120, (struct Generic_Cmd*)&node_destroy);
v_out_queue_push_tail(&queue_list, 120, (struct Generic_Cmd*)&node_destroy);
printf("cmd count: %d\n", v_out_queue_get_count(&queue_list));
printf("prio sum high: %6.3f\n", v_out_queue_get_prio_sum_high(&queue_list));
printf("prio sum low: %6.3f\n", v_out_queue_get_prio_sum_low(&queue_list));
max = queue_list.max_prio;
min = queue_list.min_prio;
printf("Priority queues, max: %d, min: %d\n\n", max, min);
printf("Removing commands from queues with high priority\n\n");
for(prio=max; prio>=VRS_DEFAULT_PRIORITY; prio--) {
printf("Priority queue: %d\n", prio);
while( v_out_queue_get_count_prio(&queue_list, prio) > 0) {
cmd = v_out_queue_pop(&queue_list, prio, &count, &share, &len);
v_cmd_print(VRS_PRINT_DEBUG_MSG, cmd);
printf("cmd count: %d\n", v_out_queue_get_count(&queue_list));
printf("prio sum: %6.3f\n", v_out_queue_get_prio_sum_high(&queue_list));
free(cmd);
}
}
printf("\nRemoving commands from queues with low priority\n\n");
for(prio=VRS_DEFAULT_PRIORITY-1; prio>=min; prio--) {
printf("Priority queue: %d\n", prio);
while( v_out_queue_get_count_prio(&queue_list, prio) > 0) {
cmd = v_out_queue_pop(&queue_list, prio, &count, &share, &len);
v_cmd_print(VRS_PRINT_DEBUG_MSG, cmd);
printf("cmd count: %d\n", v_out_queue_get_count(&queue_list));
printf("prio sum: %6.3f\n", v_out_queue_get_prio_sum_low(&queue_list));
free(cmd);
}
}
printf("\nPriority queues, max: %d, min: %d\n\n",
queue_list.max_prio,
queue_list.min_prio);
v_out_queue_destroy(&queue_list);
return EXIT_SUCCESS;
}
int test_in_queue(void)
{
struct VInQueue in_queue;
struct Generic_Cmd *cmd;
struct Connect_Accept_Cmd connect_accept;
struct Node_Create_Cmd node_create;
struct Node_Destroy_Cmd node_destroy;
v_in_queue_init(&in_queue);
connect_accept.id = FAKE_CMD_CONNECT_ACCEPT;
connect_accept.user_id = 1001;
connect_accept.avatar_id = 67000;
node_create.id = CMD_NODE_CREATE;
node_create.addr.user_id = 100;
node_create.addr.parent_id = 2;
node_create.node_id = 1000;
node_destroy.id = CMD_NODE_DESTROY;
node_destroy.addr.node_id = 1001;
printf("Pushing commands to queue\n");
v_cmd_print(VRS_PRINT_DEBUG_MSG, (struct Generic_Cmd*)&connect_accept);
v_cmd_print(VRS_PRINT_DEBUG_MSG, (struct Generic_Cmd*)&node_create);
v_cmd_print(VRS_PRINT_DEBUG_MSG, (struct Generic_Cmd*)&node_destroy);
v_in_queue_push(&in_queue, (struct Generic_Cmd*)&connect_accept);
v_in_queue_push(&in_queue, (struct Generic_Cmd*)&node_create);
v_in_queue_push(&in_queue, (struct Generic_Cmd*)&node_destroy);
v_in_queue_push(&in_queue, (struct Generic_Cmd*)&node_destroy);
printf("Cmd count: %d\n\n", v_in_queue_cmd_count(&in_queue));
printf("Poping commands from queue\n");
while( v_in_queue_cmd_count(&in_queue) > 0) {
cmd = v_in_queue_pop(&in_queue);
v_cmd_print(VRS_PRINT_DEBUG_MSG, cmd);
}
v_in_queue_destroy(&in_queue);
return EXIT_SUCCESS;
}
void print_help(char *prog)
{
printf("Bad syntax:\n");
printf("%s [--test-ack-nak|\n", prog);
printf(" --test-queue-fake|\n");
printf(" --test-queue-node|\n");
printf(" --test-list|\n");
printf(" --test-hash|\n");
printf(" --test-hash-256|\n");
printf(" --test-hash-65536|\n");
printf(" --test-history|\n");
printf(" --test-out-queue|\n");
printf(" --test-in-queue]\n");
}
int main(int argc, char *argv[])
{
int ret = EXIT_FAILURE;
#ifdef __APPLE__
sranddev();
#else
srand(rand());
#endif
if(argc != 2) {
print_help(argv[0]);
ret = EXIT_FAILURE;
} else {
v_init_print_log(VRS_PRINT_DEBUG_MSG, stdout);
if( strcmp(argv[1], "--test-ack-nak") == 0 ) {
ret = test_ack_nak_history();
} /*else if( strcmp(argv[1], "--test-queue-fake") == 0 ) {
ret = test_queues_fake();
} else if( strcmp(argv[1], "--test-queue-node") == 0 ) {
ret = test_queues_node();
} */else if( strcmp(argv[1], "--test-list") == 0 ) {
ret = test_list();
} else if( strcmp(argv[1], "--test-hash") == 0) {
ret = test_hash();
} else if( strcmp(argv[1], "--test-hash-256") == 0) {
ret = test_hash_256();
} else if( strcmp(argv[1], "--test-hash-65536") == 0) {
ret = test_hash_65536();
} else if( strcmp(argv[1], "--test-history") == 0) {
ret = test_history();
} else if( strcmp(argv[1], "--test-out-queue") == 0) {
ret = test_out_queue();
} else if( strcmp(argv[1], "--test-in-queue") == 0) {
ret = test_in_queue();
} else {
print_help(argv[0]);
ret = EXIT_FAILURE;
}
}