summaryrefslogtreecommitdiffstats
path: root/amarok/src/atomicstring_unittest.cpp
blob: 73f23f13d8499fb56b4ec15b2de10f01d3501441 (plain)
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
// Ovidiu Gheorghioiu <ovy@alum.mit.edu>, (C) 2006
// License: GNU General Public License V2
//
// Stress-test the AtomicString class for thread safety. Run on SMP for maximum exposure.
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>

#include "atomicstring.h"

void *
Worker(void *num) {
    srand( reinterpret_cast<int>( num ) );
    TQString base = "str";
    // create 5 strings, destroy them, copy them around
    const int kNumStrings = 5;
    AtomicString *atStrings[kNumStrings * 2];
    for( int i = 0; i < kNumStrings * 2; i++ ) atStrings[i] = NULL;
    const int kIterations = 100000;
    for( int i = 0; i < kIterations; i++ ) {
	int k = rand() % (kNumStrings * 2);
	if( atStrings[k] == NULL ) {
	    // the upper half are sometimes copies of the corresponding
	    // lower half strings
	    if( k >= kNumStrings && atStrings[k % kNumStrings] != NULL ) {
		atStrings[k] = new AtomicString( *atStrings[k % kNumStrings] );
	    } else {
		atStrings[k] = new AtomicString( base + TQString::number( k ) );
	    }
	} else {
	    // check the string; could be either upper or lower
	    TQString str = atStrings[k]->string();
	    if( str != base + TQString::number( k )
		&& str != base + TQString::number( k % kNumStrings ) ) {
		tqFatal( "unexpected atStrings[%d]: %s", k, str.ascii() );
	    }
	    delete atStrings[k];
	    atStrings[k] = NULL;
	}
    }

    return NULL;
}

int main() {
    const int kWorkers = 2;

    pthread_t workers[kWorkers];

    for( int i = 0; i < kWorkers; i++ ) {
	if( pthread_create(& workers[i], NULL,
			   & Worker,
			   reinterpret_cast<void *>(i))
	    != 0)
	    tqFatal( "Could not create thread %d", i );
    }

    for( int i = 0; i < kWorkers; i++ ) {
	void *thread_return;
	pthread_join( workers[i], &thread_return );
    }
}