static const int MAX_LEN = 10000000;//max size, if we cannot find
deplication within MAX_LEN, the random function is good.
//return where it begin duplicate
int TestRandomFun(int (*f)()){
static const int INIT_LEN = 100000;
static const int SUBARR_LEN = 10; //find 10-len substr first then
keep comparing,
static const int DUP_THRESHHOD = 1000; //if it keep equalling to
1000, we say then random function produce duplications.
vector v(INIT_LEN);
int c = 0;
while (v.size() < MAX_LEN){
for (int i = 0; i < INIT_LEN; ++i){
v.push_back((*f)());
}
vector::iterator search_start;
if (0 == c){
search_start = v.begin() + SUBARR_LEN;
}else{
search_start = v.begin() + INIT_LEN * c -
SUBARR_LEN;
}
++c;
vector::iterator it = search(search_start, v.end(),
v.begin(), v.begin() + SUBARR_LEN);
while (it != v.end()){
vector::iterator jt = v.begin() + SUBARR_LEN;
vector::iterator kt = it + SUBARR_LEN;
if (v.end() - kt < DUP_THRESHHOD){
for (int i = 0; i < DUP_THRESHHOD; ++i){
v.push_back((*f)());
}
}
int count = 0;
while (jt != it && kt != v.end()){
if (*jt != *kt){
break;
}
++jt;
++kt;
if (++count >= DUP_THRESHHOD){
return (it - v.begin());
}
}
if (jt == it){
return (it - v.begin());
}
it = search(it + 1, v.end(), v.begin(), v.begin() +
SUBARR_LEN);
}
}
return 0;
}