// For every length we want to test, vary and change alignment
// of allocated memory, fill it with some values, calculate
// expected result and then run function and compare what we got.
// These tests contributed by Intel Corporation.
// TODO: make these tests more intention-revealing and less random.
template<class Character>
struct StringTestState {
StringTestState(size_t MAX_LEN) : MAX_LEN(MAX_LEN) {
int max_alignment = 64;
// TODO: fix the tests to not sometimes use twice their specified "MAX_LEN".
glob_ptr = reinterpret_cast<Character*>(valloc(2 * sizeof(Character) * MAX_LEN + max_alignment));
glob_ptr1 = reinterpret_cast<Character*>(valloc(2 * sizeof(Character) * MAX_LEN + max_alignment));
glob_ptr2 = reinterpret_cast<Character*>(valloc(2 * sizeof(Character) * MAX_LEN + max_alignment));
InitLenArray();
srandom(1234);
}
~StringTestState() {
free(glob_ptr);
free(glob_ptr1);
free(glob_ptr2);
}
void NewIteration() {
int alignments[] = { 24, 32, 16, 48, 1, 2, 3, 0, 5, 11 };
int usable_alignments = 10;
int align1 = alignments[random() % (usable_alignments - 1)];
int align2 = alignments[random() % (usable_alignments - 1)];
ptr = glob_ptr + align1;
ptr1 = glob_ptr1 + align1;
ptr2 = glob_ptr2 + align2;
}
const size_t MAX_LEN;
Character *ptr, *ptr1, *ptr2;
size_t n;
int len[ITER + 1];
private:
Character *glob_ptr, *glob_ptr1, *glob_ptr2;
// Calculate input lengths and fill state.len with them.
// Test small lengths with more density than big ones. Manually push
// smallest (0) and biggest (MAX_LEN) lengths. Avoid repeats.
// Return number of lengths to test.
void InitLenArray() {
n = 0;
len[n++] = 0;
for (size_t i = 1; i < ITER; ++i) {
int l = (int) exp(log((double) MAX_LEN) * i / ITER);
if (l != len[n - 1]) {
len[n++] = l;
}
}
len[n++] = MAX_LEN;
}
};
转载于:https://my.oschina.net/rinehart/blog/155603