OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2015 The Crashpad Authors. 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 "util/misc/random_string.h" | |
16 | |
17 #include <set> | |
18 | |
19 #include "base/basictypes.h" | |
20 #include "gtest/gtest.h" | |
21 | |
22 namespace crashpad { | |
23 namespace test { | |
24 namespace { | |
25 | |
26 TEST(RandomString, RandomString) { | |
27 // Explicitly list the allowed characters, rather than relying on a range. | |
28 // This prevents the test from having any dependency on the character set, so | |
29 // that the implementation is free to assume all uppercase letters are | |
30 // contiguous as in ASCII. | |
31 const std::string allowed_characters("ABCDEFGHIJKLMNOPQRSTUVWXYZ"); | |
32 | |
33 size_t character_counts[26] = {}; | |
34 ASSERT_EQ(arraysize(character_counts), allowed_characters.size()); | |
35 | |
36 std::set<std::string> strings; | |
37 | |
38 for (size_t i = 0; i < 256; ++i) { | |
39 const std::string random_string = RandomString(); | |
40 EXPECT_EQ(16u, random_string.size()); | |
41 | |
42 // Make sure that the string is unique. It is possible, but extremely | |
43 // unlikely, for there to be collisions. | |
44 auto result = strings.insert(random_string); | |
45 EXPECT_TRUE(result.second) << random_string; | |
46 | |
47 for (char c : random_string) { | |
48 size_t character_index = allowed_characters.find(c); | |
49 | |
50 // Make sure that no unexpected characters appear. | |
51 EXPECT_NE(character_index, std::string::npos) << c; | |
52 | |
53 if (character_index != std::string::npos) { | |
54 ++character_counts[character_index]; | |
55 } | |
56 } | |
57 } | |
58 | |
59 // Make sure every character appears at least once. It is possible, but | |
60 // extremely unlikley, for a character to not appear at all. | |
scottmg
2015/11/16 18:24:32
unlikely
| |
61 for (size_t character_index = 0; | |
62 character_index < arraysize(character_counts); | |
63 ++character_index) { | |
64 EXPECT_GT(character_counts[character_index], 0u) | |
65 << allowed_characters[character_index]; | |
66 } | |
67 } | |
68 | |
69 } // namespace | |
70 } // namespace test | |
71 } // namespace crashpad | |
OLD | NEW |