• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2012 Google Inc. All Rights Reserved.
2 //
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
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
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.
14 
15 #include "build_log.h"
16 
17 #include <algorithm>
18 using namespace std;
19 
20 #include <stdlib.h>
21 #include <time.h>
22 
random(int low,int high)23 int random(int low, int high) {
24   return int(low + (rand() / double(RAND_MAX)) * (high - low) + 0.5);
25 }
26 
RandomCommand(char ** s)27 void RandomCommand(char** s) {
28   int len = random(5, 100);
29   *s = new char[len];
30   for (int i = 0; i < len; ++i)
31     (*s)[i] = (char)random(32, 127);
32 }
33 
main()34 int main() {
35   const int N = 20 * 1000 * 1000;
36 
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];
40 
41   srand((int)time(NULL));
42 
43   for (int i = 0; i < N; ++i) {
44     RandomCommand(&commands[i]);
45     hashes[i] = make_pair(BuildLog::LogEntry::HashCommand(commands[i]), i);
46   }
47 
48   sort(hashes, hashes + N);
49 
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]);
58         collision_count++;
59       }
60     }
61   }
62   printf("\n\n%d collisions after %d runs\n", collision_count, N);
63 }
64