Checks that for given settings of the string generator: it generates strings that are non-decreasing in length. strings of the same length are sorted in alphabet order. it doesn't generate the same string twice. it generates the right number of strings. If all of these hold, the StringGenerator is behaving. Assumes that the alphabet is sorted, so that the generated strings can just be compared le
| 32 | // Assumes that the alphabet is sorted, so that the generated |
| 33 | // strings can just be compared lexicographically. |
| 34 | static void RunTest(int len, const std::string& alphabet, bool donull) { |
| 35 | StringGenerator g(len, Explode(alphabet)); |
| 36 | |
| 37 | int n = 0; |
| 38 | int last_l = -1; |
| 39 | std::string last_s; |
| 40 | |
| 41 | if (donull) { |
| 42 | g.GenerateNULL(); |
| 43 | EXPECT_TRUE(g.HasNext()); |
| 44 | StringPiece sp = g.Next(); |
| 45 | EXPECT_EQ(sp.data(), static_cast<const char*>(NULL)); |
| 46 | EXPECT_EQ(sp.size(), 0); |
| 47 | } |
| 48 | |
| 49 | while (g.HasNext()) { |
| 50 | std::string s = std::string(g.Next()); |
| 51 | n++; |
| 52 | |
| 53 | // Check that all characters in s appear in alphabet. |
| 54 | for (const char *p = s.c_str(); *p != '\0'; ) { |
| 55 | Rune r; |
| 56 | p += chartorune(&r, p); |
| 57 | EXPECT_TRUE(utfrune(alphabet.c_str(), r) != NULL); |
| 58 | } |
| 59 | |
| 60 | // Check that string is properly ordered w.r.t. previous string. |
| 61 | int l = utflen(s.c_str()); |
| 62 | EXPECT_LE(l, len); |
| 63 | if (last_l < l) { |
| 64 | last_l = l; |
| 65 | } else { |
| 66 | EXPECT_EQ(last_l, l); |
| 67 | EXPECT_LT(last_s, s); |
| 68 | } |
| 69 | last_s = s; |
| 70 | } |
| 71 | |
| 72 | // Check total string count. |
| 73 | int64_t m = 0; |
| 74 | int alpha = utflen(alphabet.c_str()); |
| 75 | if (alpha == 0) // Degenerate case. |
| 76 | len = 0; |
| 77 | for (int i = 0; i <= len; i++) |
| 78 | m += IntegerPower(alpha, i); |
| 79 | EXPECT_EQ(n, m); |
| 80 | } |
| 81 | |
| 82 | TEST(StringGenerator, NoLength) { |
| 83 | RunTest(0, "abc", false); |
no test coverage detected