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
|
//
//********************************************************************
// Copyright (C) 2002, International Business Machines
// Corporation and others. All Rights Reserved.
//********************************************************************
//
// File stringtest.cpp
//
#include "threadtest.h"
#include "unicode/unistr.h"
#include "stdio.h"
class StringThreadTest: public AbstractThreadTest {
public:
StringThreadTest();
virtual ~StringThreadTest();
virtual void check();
virtual void runOnce();
void makeStringCopies(int recursionCount);
private:
UnicodeString *fCleanStrings;
UnicodeString *fSourceStrings;
};
StringThreadTest::StringThreadTest() {
// cleanStrings and sourceStrings are separately initialized to the same values.
// cleanStrings are never touched after in any remotely unsafe way.
// sourceStrings are copied during the test, which will run their buffer's reference
// counts all over the place.
fCleanStrings = new UnicodeString[5];
fSourceStrings = new UnicodeString[5];
fCleanStrings[0] = "When sorrows come, they come not single spies, but in batallions.";
fSourceStrings[0] = "When sorrows come, they come not single spies, but in batallions.";
fCleanStrings[1] = "Away, you scullion! You rampallion! You fustilarion! I'll tickle your catastrophe!";
fSourceStrings[1] = "Away, you scullion! You rampallion! You fustilarion! I'll tickle your catastrophe!";
fCleanStrings[2] = "hot";
fSourceStrings[2] = "hot";
fCleanStrings[3] = "";
fSourceStrings[3] = "";
fCleanStrings[4] = "Tomorrow, and tomorrow, and tomorrow,\n"
"Creeps in this petty pace from day to day\n"
"To the last syllable of recorded time;\n"
"And all our yesterdays have lighted fools \n"
"The way to dusty death. Out, out brief candle!\n"
"Life's but a walking shadow, a poor player\n"
"That struts and frets his hour upon the stage\n"
"And then is heard no more. It is a tale\n"
"Told by and idiot, full of sound and fury,\n"
"Signifying nothing.\n";
fSourceStrings[4] = "Tomorrow, and tomorrow, and tomorrow,\n"
"Creeps in this petty pace from day to day\n"
"To the last syllable of recorded time;\n"
"And all our yesterdays have lighted fools \n"
"The way to dusty death. Out, out brief candle!\n"
"Life's but a walking shadow, a poor player\n"
"That struts and frets his hour upon the stage\n"
"And then is heard no more. It is a tale\n"
"Told by and idiot, full of sound and fury,\n"
"Signifying nothing.\n";
};
StringThreadTest::~StringThreadTest() {
delete [] fCleanStrings;
delete [] fSourceStrings;
}
void StringThreadTest::runOnce() {
makeStringCopies(25);
}
void StringThreadTest::makeStringCopies(int recursionCount) {
UnicodeString firstGeneration[5];
UnicodeString secondGeneration[5];
UnicodeString thirdGeneration[5];
UnicodeString fourthGeneration[5];
// Make four generations of copies of the source strings, in slightly variant ways.
//
int i;
for (i=0; i<5; i++) {
firstGeneration[i] = fSourceStrings[i];
secondGeneration[i] = firstGeneration[i];
thirdGeneration[i] = UnicodeString(secondGeneration[i]);
// fourthGeneration[i] = UnicodeString("Lay on, MacDuff, And damn'd be him that first cries, \"Hold, enough!\"");
fourthGeneration[i] = UnicodeString();
fourthGeneration[i] = thirdGeneration[i];
}
// Recurse to make even more copies of the strings/
//
if (recursionCount > 0) {
makeStringCopies(recursionCount-1);
}
// Verify that all four generations are equal.
for (i=0; i<5; i++) {
if (firstGeneration[i] != fSourceStrings[i] ||
firstGeneration[i] != secondGeneration[i] ||
firstGeneration[i] != thirdGeneration[i] ||
firstGeneration[i] != fourthGeneration[i])
{
fprintf(stderr, "Error, strings don't compare equal.\n");
}
}
};
void StringThreadTest::check() {
//
// Check that the reference counts on the buffers for all of the source strings
// are one. The ref counts will have run way up while the test threads
// make numerous copies of the strings, but at the top of the loop all
// of the copies should be gone.
//
int i;
for (i=0; i<5; i++) {
if (fSourceStrings[i].fFlags & UnicodeString::kRefCounted) {
const UChar *buf = fSourceStrings[i].getBuffer();
uint32_t refCount = fSourceStrings[i].refCount();
if (refCount != 1) {
fprintf(stderr, "\nFailure. SourceString Ref Count was %d, should be 1.\n", refCount);
}
}
}
};
//
// Factory functoin for StringThreadTest.
// C function lets ThreadTest create StringTests without needing StringThreadTest header.
//
AbstractThreadTest *createStringTest() {
return new StringThreadTest();
};
|