forked from istng/SO-TP1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-ejs.cpp
145 lines (104 loc) · 3.87 KB
/
test-ejs.cpp
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
#include <iostream>
#include "ConcurrentHashMap.hpp"
#include <unistd.h>
using namespace std;
void *add(void* h){
ConcurrentHashMap* ch = (ConcurrentHashMap*) h ;
usleep(rand() % 10000);
ch->addAndInc("gola");
return NULL;
}
void *add1(void* h){
ConcurrentHashMap* ch = (ConcurrentHashMap*) h ;
//cout << ch->member("hola2");
usleep(rand() % 10000);
ch->addAndInc("hola");
return NULL;
}
void *add2(void* h){
ConcurrentHashMap* ch = (ConcurrentHashMap*) h ;
//cout << ch->member("hola3");
usleep(rand() % 10000);
ch->addAndInc("lola");
return NULL;
}
void *add3(void* h){
ConcurrentHashMap* ch = (ConcurrentHashMap*) h ;
//c//out << ch->member("hola4");
usleep(rand() % 10000);
ch->addAndInc("coca");
return NULL;
}
int main(void) {
cout << "--------------- Chequeando que anden las funciones de ConcurrentHashMap-----------" << endl;
ConcurrentHashMap h;
ConcurrentHashMap h2;
pthread_t threads[200]; int tid;
// Creamos 200 threads, 100 insertaran valores en h y 100 en h2
for (tid = 0; tid < 50; tid++) {
pthread_create(&threads[tid],NULL,add,&h);
pthread_create(&threads[50+tid],NULL,add1,&h);
pthread_create(&threads[100+tid],NULL,add2,&h2);
pthread_create(&threads[150+tid],NULL,add3,&h2);
}
for (tid = 0; tid < 200; tid++) {
pthread_join(threads[tid],NULL);
}
// En h veremos que hay 50 gola's y 50 hola's y en h2 y ordenados alfabeticamente
h.print_tabla();
h2.print_tabla();
cout << "gola y hola estan en h: " << h.member("gola") << ", " << h.member("hola") << endl;
cout << "coca y lola estan en h2: " << h2.member("coca") << ", " << h2.member("lola") << endl;
h.addAndInc("gola");
h2.addAndInc("coca");
cout << h.maximum(4).first << " " << h.maximum(4).second << endl;
cout << h2.maximum(3).first << " " << h2.maximum(2).second << endl;
cout << "--------------- Creando nueva tabla para testear count_words no concurrente-----------" << endl;
ConcurrentHashMap h_nc = count_words("file1");
ConcurrentHashMap h_nc2 = count_words("file2");
h_nc.print_tabla();
h_nc2.print_tabla();
cout << "--------------- Creando nueva tabla para testear Count_words(list<string>)-----------" << endl;
ConcurrentHashMap palabras;
list<string> l;
l.push_back("file1");
l.push_back("file2");
l.push_back("file3");
l.push_back("file4");
palabras = count_words(l);
int i;
for (i = 0; i < 26; i++) {
for (auto it = palabras.tabla[i]->CrearIt(); it.HaySiguiente(); it.Avanzar()) {
auto t = it.Siguiente();
cout << t.first << " " << t.second << endl;
}
}
pair<string,unsigned int> max = palabras.maximum(26);
cout << "<" << max.first << " , " << max.second << ">" << endl;
cout << "-----------------Creando nueva tabla para testear Count_words(n, list<string>)-----------" << endl;
ConcurrentHashMap words;
list<string> l2;
l2.push_back("file1");
l2.push_back("file2");
l2.push_back("file3");
l2.push_back("file4");
unsigned int k = 2;
words = count_words(k,l2);
for (i = 0; i < 26; i++) {
for (auto it = words.tabla[i]->CrearIt(); it.HaySiguiente(); it.Avanzar()) {
auto t = it.Siguiente();
cout << t.first << " " << t.second << endl;
}
}
cout << "--- Reutilizamos tabla anterior para chequear la función maximum(p_archivos, p_maximos, list_archs)-----" << endl;
pair<string , unsigned int > m = maximum(2,2,l2);
cout << "<" << m.first << " " << m.second << ">" << endl;
ConcurrentHashMap h_basic = count_words(l2);
pair<string, unsigned int > m_basic = h_basic.maximum(3);
cout << "<" << m_basic.first << " " << m_basic.second << ">" << endl;
pair<string, unsigned int > m_basic2 = maximum2(3,3,l2);
pair<string, unsigned int > m_basic3 = maximum3(4,4,l2);
cout << "<" << m_basic2.first << " " << m_basic2.second << ">" << endl;
cout << "<" << m_basic3.first << " " << m_basic3.second << ">" << endl;
return 0;
}