#include "build_log.h"
#include <algorithm>
#include <stdlib.h>
#include <time.h>
using namespace std;
int random(int low, int high) {
return int(low + (rand() / double(RAND_MAX)) * (high - low) + 0.5);
}
void RandomCommand(char** s) {
int len = random(5, 100);
*s = new char[len+1];
for (int i = 0; i < len; ++i)
(*s)[i] = (char)random(32, 127);
(*s)[len] = '\0';
}
int main() {
const int N = 20 * 1000 * 1000;
char** commands = new char*[N];
pair<uint64_t, int>* hashes = new pair<uint64_t, int>[N];
srand((int)time(NULL));
for (int i = 0; i < N; ++i) {
RandomCommand(&commands[i]);
hashes[i] = make_pair(BuildLog::LogEntry::HashCommand(commands[i]), i);
}
sort(hashes, hashes + N);
int collision_count = 0;
for (int i = 1; i < N; ++i) {
if (hashes[i - 1].first == hashes[i].first) {
if (strcmp(commands[hashes[i - 1].second],
commands[hashes[i].second]) != 0) {
printf("collision!\n string 1: '%s'\n string 2: '%s'\n",
commands[hashes[i - 1].second],
commands[hashes[i].second]);
collision_count++;
}
}
}
printf("\n\n%d collisions after %d runs\n", collision_count, N);
}