source_gen_test.cpp 6.9 KB

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