1 // Copyright 2012 Google Inc. All Rights Reserved.
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 #include "build_log.h"
23 int random(int low, int high) {
24 return int(low + (rand() / double(RAND_MAX)) * (high - low) + 0.5);
27 void RandomCommand(char** s) {
28 int len = random(5, 100);
30 for (int i = 0; i < len; ++i)
31 (*s)[i] = (char)random(32, 127);
35 const int N = 20 * 1000 * 1000;
37 // Leak these, else 10% of the runtime is spent destroying strings.
38 char** commands = new char*[N];
39 pair<uint64_t, int>* hashes = new pair<uint64_t, int>[N];
41 srand((int)time(NULL));
43 for (int i = 0; i < N; ++i) {
44 RandomCommand(&commands[i]);
45 hashes[i] = make_pair(BuildLog::LogEntry::HashCommand(commands[i]), i);
48 sort(hashes, hashes + N);
50 int collision_count = 0;
51 for (int i = 1; i < N; ++i) {
52 if (hashes[i - 1].first == hashes[i].first) {
53 if (strcmp(commands[hashes[i - 1].second],
54 commands[hashes[i].second]) != 0) {
55 printf("collision!\n string 1: '%s'\n string 2: '%s'\n",
56 commands[hashes[i - 1].second],
57 commands[hashes[i].second]);
62 printf("\n\n%d collisions after %d runs\n", collision_count, N);