@@ -1,38 +1,68 @@
|
|
|
|
1 |
#include <stdio.h>
|
2 |
#include <stdlib.h>
|
3 |
#include <time.h>
|
4 |
|
5 |
#include "array.h"
|
6 |
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
void print_array(const char* name, array_t* array)
|
11 |
-
{
|
12 |
-
printf("%s =", name);
|
13 |
-
for ( size_t index = 0; index < array_get_count(array); ++index )
|
14 |
-
printf(" %zu", (size_t)array_get_element(array, index));
|
15 |
-
putchar('\n');
|
16 |
-
fflush(stdout);
|
17 |
-
}
|
18 |
|
19 |
int main()
|
20 |
{
|
21 |
-
|
22 |
-
|
23 |
|
24 |
-
|
25 |
-
for ( size_t index = 0, count = MIN_CNT + rand() % MAX_CNT; index < count; ++index )
|
26 |
-
{
|
27 |
-
array_append( array1, (void*)(10 + (size_t)rand() % 90) );
|
28 |
-
array_append( array2, (void*)(100 + (size_t)rand() % 900) );
|
29 |
-
}
|
30 |
|
31 |
print_array("array1", array1);
|
32 |
print_array("array2", array2);
|
33 |
|
34 |
array_destroy(array1);
|
35 |
array_destroy(array2);
|
36 |
|
37 |
return 0;
|
38 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#include <pthread.h>
|
2 |
#include <stdio.h>
|
3 |
#include <stdlib.h>
|
4 |
#include <time.h>
|
5 |
|
6 |
#include "array.h"
|
7 |
|
8 |
+
void print_array(const char* name, array_t* array);
|
9 |
+
void test_arrays(array_t* array1, array_t* array2);
|
10 |
+
void* test_array(void* data);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
int main()
|
13 |
{
|
14 |
+
array_t* array1 = array_create(100);
|
15 |
+
array_t* array2 = array_create(100);
|
16 |
|
17 |
+
test_arrays(array1, array2);
|
|
|
|
|
|
|
|
|
|
|
18 |
|
19 |
print_array("array1", array1);
|
20 |
print_array("array2", array2);
|
21 |
|
22 |
array_destroy(array1);
|
23 |
array_destroy(array2);
|
24 |
|
25 |
return 0;
|
26 |
}
|
27 |
+
|
28 |
+
void print_array(const char* name, array_t* array)
|
29 |
+
{
|
30 |
+
printf("%s: %zu elements\n", name, array_get_count(array));
|
31 |
+
fflush(stdout);
|
32 |
+
}
|
33 |
+
|
34 |
+
void test_arrays(array_t* array1, array_t* array2)
|
35 |
+
{
|
36 |
+
srand( (unsigned)((unsigned)time(NULL) + (unsigned)clock()) );
|
37 |
+
|
38 |
+
size_t thread_count = 10 + rand() % 20;
|
39 |
+
pthread_t* threads = (pthread_t*) malloc( thread_count * sizeof(pthread_t) );
|
40 |
+
|
41 |
+
for ( size_t current = 0; current < thread_count; ++current )
|
42 |
+
pthread_create( threads + current, NULL, test_array, current % 2 ? array2 : array1 );
|
43 |
+
|
44 |
+
for ( size_t current = 0; current < thread_count; ++current )
|
45 |
+
pthread_join( threads[current], NULL );
|
46 |
+
|
47 |
+
free(threads);
|
48 |
+
}
|
49 |
+
|
50 |
+
void* test_array(void* data)
|
51 |
+
{
|
52 |
+
array_t* array = (array_t*)data;
|
53 |
+
|
54 |
+
for ( size_t index = 0, count = 1000 + rand() % 10000; index < count; ++index )
|
55 |
+
{
|
56 |
+
size_t num = rand() % 100;
|
57 |
+
if ( num >= 10 )
|
58 |
+
array_append( array, (void*)(num) );
|
59 |
+
else
|
60 |
+
{
|
61 |
+
num += rand() % 90;
|
62 |
+
if ( array_find_first(array, (void*)num, 0) != array_not_found )
|
63 |
+
array_remove_first(array, (void*)num, 0);
|
64 |
+
}
|
65 |
+
}
|
66 |
+
|
67 |
+
return NULL;
|
68 |
+
}
|