source_gen_test.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #include "testing/base/source_gen.h"
  5. #include <gmock/gmock.h>
  6. #include <gtest/gtest.h>
  7. #include "common/set.h"
  8. #include "testing/base/global_exe_path.h"
  9. #include "toolchain/driver/driver.h"
  10. #include "toolchain/install/install_paths_test_helpers.h"
  11. namespace Carbon::Testing {
  12. namespace {
  13. using ::testing::AllOf;
  14. using ::testing::ContainerEq;
  15. using ::testing::Contains;
  16. using ::testing::Each;
  17. using ::testing::Eq;
  18. using ::testing::Ge;
  19. using ::testing::Gt;
  20. using ::testing::Le;
  21. using ::testing::MatchesRegex;
  22. using ::testing::SizeIs;
  23. // Tiny helper to sum the sizes of a range of ranges. Uses a template to avoid
  24. // hard coding any specific types for the two ranges.
  25. template <typename T>
  26. static auto SumSizes(const T& range) -> ssize_t {
  27. ssize_t sum = 0;
  28. for (const auto& inner_range : range) {
  29. sum += inner_range.size();
  30. }
  31. return sum;
  32. }
  33. TEST(SourceGenTest, Identifiers) {
  34. SourceGen gen;
  35. auto idents = gen.GetShuffledIdentifiers(1000);
  36. EXPECT_THAT(idents.size(), Eq(1000));
  37. for (llvm::StringRef ident : idents) {
  38. EXPECT_THAT(ident, MatchesRegex("[A-Za-z][A-Za-z0-9_]*"));
  39. }
  40. // We should have at least one identifier of each length [1, 64]. The exact
  41. // distribution is an implementation detail designed to vaguely match the
  42. // expected distribution in source code.
  43. for (int size : llvm::seq_inclusive(1, 64)) {
  44. EXPECT_THAT(idents, Contains(SizeIs(size)));
  45. }
  46. // Check that identifiers 4 characters or shorter are more common than longer
  47. // lengths. This is a very rough way of double checking that we got the
  48. // intended distribution.
  49. for (int short_size : llvm::seq_inclusive(1, 4)) {
  50. int short_count = llvm::count_if(idents, [&](auto ident) {
  51. return static_cast<int>(ident.size()) == short_size;
  52. });
  53. for (int long_size : llvm::seq_inclusive(5, 64)) {
  54. EXPECT_THAT(short_count, Gt(llvm::count_if(idents, [&](auto ident) {
  55. return static_cast<int>(ident.size()) == long_size;
  56. })));
  57. }
  58. }
  59. // Check that repeated calls are different in interesting ways, but have the
  60. // exact same total bytes.
  61. ssize_t idents_size_sum = SumSizes(idents);
  62. for ([[maybe_unused]] int _ : llvm::seq(10)) {
  63. auto idents2 = gen.GetShuffledIdentifiers(1000);
  64. EXPECT_THAT(idents2, SizeIs(1000));
  65. // Should be (at least) a different shuffle of identifiers.
  66. EXPECT_THAT(idents2, Not(ContainerEq(idents)));
  67. // But the sum of lengths should be identical.
  68. EXPECT_THAT(SumSizes(idents2), Eq(idents_size_sum));
  69. }
  70. // Check length constraints have the desired effect.
  71. idents =
  72. gen.GetShuffledIdentifiers(1000, /*min_length=*/10, /*max_length=*/20);
  73. EXPECT_THAT(idents, Each(SizeIs(AllOf(Ge(10), Le(20)))));
  74. }
  75. TEST(SourceGenTest, UniformIdentifiers) {
  76. SourceGen gen;
  77. // Check that uniform identifier length results in exact coverage of each
  78. // possible length for an easy case, both without and with a remainder.
  79. auto idents =
  80. gen.GetShuffledIdentifiers(100, /*min_length=*/10, /*max_length=*/19,
  81. /*uniform=*/true);
  82. EXPECT_THAT(idents, Contains(SizeIs(10)).Times(10));
  83. EXPECT_THAT(idents, Contains(SizeIs(11)).Times(10));
  84. EXPECT_THAT(idents, Contains(SizeIs(12)).Times(10));
  85. EXPECT_THAT(idents, Contains(SizeIs(13)).Times(10));
  86. EXPECT_THAT(idents, Contains(SizeIs(14)).Times(10));
  87. EXPECT_THAT(idents, Contains(SizeIs(15)).Times(10));
  88. EXPECT_THAT(idents, Contains(SizeIs(16)).Times(10));
  89. EXPECT_THAT(idents, Contains(SizeIs(17)).Times(10));
  90. EXPECT_THAT(idents, Contains(SizeIs(18)).Times(10));
  91. EXPECT_THAT(idents, Contains(SizeIs(19)).Times(10));
  92. idents = gen.GetShuffledIdentifiers(97, /*min_length=*/10, /*max_length=*/19,
  93. /*uniform=*/true);
  94. EXPECT_THAT(idents, Contains(SizeIs(10)).Times(10));
  95. EXPECT_THAT(idents, Contains(SizeIs(11)).Times(10));
  96. EXPECT_THAT(idents, Contains(SizeIs(12)).Times(10));
  97. EXPECT_THAT(idents, Contains(SizeIs(13)).Times(10));
  98. EXPECT_THAT(idents, Contains(SizeIs(14)).Times(10));
  99. EXPECT_THAT(idents, Contains(SizeIs(15)).Times(10));
  100. EXPECT_THAT(idents, Contains(SizeIs(16)).Times(10));
  101. EXPECT_THAT(idents, Contains(SizeIs(17)).Times(9));
  102. EXPECT_THAT(idents, Contains(SizeIs(18)).Times(9));
  103. EXPECT_THAT(idents, Contains(SizeIs(19)).Times(9));
  104. }
  105. // Largely covered by `Identifiers` and `UniformIdentifiers`, but need to check
  106. // for uniqueness specifically.
  107. TEST(SourceGenTest, UniqueIdentifiers) {
  108. SourceGen gen;
  109. auto unique = gen.GetShuffledUniqueIdentifiers(1000);
  110. EXPECT_THAT(unique.size(), Eq(1000));
  111. Set<llvm::StringRef> set;
  112. for (llvm::StringRef ident : unique) {
  113. EXPECT_THAT(ident, MatchesRegex("[A-Za-z][A-Za-z0-9_]*"));
  114. EXPECT_TRUE(set.Insert(ident).is_inserted())
  115. << "Colliding identifier: " << ident;
  116. }
  117. // Check single length specifically where uniqueness is the most challenging.
  118. set.Clear();
  119. unique = gen.GetShuffledUniqueIdentifiers(1000, /*min_length=*/4,
  120. /*max_length=*/4);
  121. for (llvm::StringRef ident : unique) {
  122. EXPECT_TRUE(set.Insert(ident).is_inserted())
  123. << "Colliding identifier: " << ident;
  124. }
  125. }
  126. // Check that the source code doesn't have compiler errors.
  127. auto TestCompile(llvm::StringRef source) -> bool {
  128. llvm::vfs::InMemoryFileSystem fs;
  129. InstallPaths installation(
  130. InstallPaths::MakeForBazelRunfiles(Testing::GetExePath()));
  131. Driver driver(fs, &installation, llvm::outs(), llvm::errs());
  132. AddPreludeFilesToVfs(installation, &fs);
  133. fs.addFile("test.carbon", /*ModificationTime=*/0,
  134. llvm::MemoryBuffer::getMemBuffer(source));
  135. return driver.RunCommand({"compile", "--phase=check", "test.carbon"}).success;
  136. }
  137. TEST(SourceGenTest, GenAPIFileDenseDeclsTest) {
  138. SourceGen gen;
  139. std::string source =
  140. gen.GenAPIFileDenseDecls(1000, SourceGen::DenseDeclParams{});
  141. // Should be within 1% of the requested line count.
  142. EXPECT_THAT(source, Contains('\n').Times(AllOf(Ge(950), Le(1050))));
  143. // Make sure we generated valid Carbon code.
  144. EXPECT_TRUE(TestCompile(source));
  145. }
  146. TEST(SourceGenTest, GenAPIFileDenseDeclsCppTest) {
  147. SourceGen gen(SourceGen::Language::Cpp);
  148. // Generate a 1000-line file which is enough to have a reasonably accurate
  149. // line count estimate and have a few classes.
  150. std::string source =
  151. gen.GenAPIFileDenseDecls(1000, SourceGen::DenseDeclParams{});
  152. // Should be within 10% of the requested line count.
  153. EXPECT_THAT(source, Contains('\n').Times(AllOf(Ge(900), Le(1100))));
  154. // TODO: When the driver supports compiling C++ code as easily as Carbon, we
  155. // should test that the generated C++ code is valid.
  156. }
  157. } // namespace
  158. } // namespace Carbon::Testing