| 1 |
// Unit test for CPUInfo. |
| 2 |
|
| 3 |
#include "cpuinfo.h" |
| 4 |
#include "fred.h" |
| 5 |
|
| 6 |
#include <iostream> |
| 7 |
|
| 8 |
const int NUMTHREADS = 256; |
| 9 |
|
| 10 |
/// A counter array. We use this to check that CPUInfo is evenly |
| 11 |
/// distributing thread ids. |
| 12 |
int counter[NUMTHREADS]; |
| 13 |
|
| 14 |
using namespace HL; |
| 15 |
using namespace std; |
| 16 |
|
| 17 |
void * fn (void *) { |
| 18 |
counter[CPUInfo::getThreadId() % NUMTHREADS]++; |
| 19 |
return NULL; |
| 20 |
} |
| 21 |
|
| 22 |
int |
| 23 |
main() |
| 24 |
{ |
| 25 |
// Clear the counter array. |
| 26 |
for (int i = 0; i < NUMTHREADS; i++) { |
| 27 |
counter[i] = 0; |
| 28 |
} |
| 29 |
|
| 30 |
Fred t[NUMTHREADS]; |
| 31 |
for (int i = 0; i < NUMTHREADS; i++) { |
| 32 |
t[i].create (fn, NULL); |
| 33 |
} |
| 34 |
for (int i = 0; i < NUMTHREADS; i++) { |
| 35 |
t[i].join(); |
| 36 |
} |
| 37 |
|
| 38 |
// Now check the counter array. |
| 39 |
int maxCount = 0; |
| 40 |
for (int i = 0; i < NUMTHREADS; i++) { |
| 41 |
if (counter[i] > maxCount) { |
| 42 |
maxCount = counter[i]; |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
cout << "Maximum entries (should be near 1): " << maxCount << endl; |
| 47 |
return 0; |
| 48 |
} |