sample8_unittest.cc 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. // Copyright 2008 Google Inc.
  2. // All Rights Reserved.
  3. //
  4. // Redistribution and use in source and binary forms, with or without
  5. // modification, are permitted provided that the following conditions are
  6. // met:
  7. //
  8. // * Redistributions of source code must retain the above copyright
  9. // notice, this list of conditions and the following disclaimer.
  10. // * Redistributions in binary form must reproduce the above
  11. // copyright notice, this list of conditions and the following disclaimer
  12. // in the documentation and/or other materials provided with the
  13. // distribution.
  14. // * Neither the name of Google Inc. nor the names of its
  15. // contributors may be used to endorse or promote products derived from
  16. // this software without specific prior written permission.
  17. //
  18. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. //
  30. // Author: vladl@google.com (Vlad Losev)
  31. // This sample shows how to test code relying on some global flag variables.
  32. // Combine() helps with generating all possible combinations of such flags,
  33. // and each test is given one combination as a parameter.
  34. // Use class definitions to test from this header.
  35. #include "prime_tables.h"
  36. #include "gtest/gtest.h"
  37. #if GTEST_HAS_COMBINE
  38. // Suppose we want to introduce a new, improved implementation of PrimeTable
  39. // which combines speed of PrecalcPrimeTable and versatility of
  40. // OnTheFlyPrimeTable (see prime_tables.h). Inside it instantiates both
  41. // PrecalcPrimeTable and OnTheFlyPrimeTable and uses the one that is more
  42. // appropriate under the circumstances. But in low memory conditions, it can be
  43. // told to instantiate without PrecalcPrimeTable instance at all and use only
  44. // OnTheFlyPrimeTable.
  45. class HybridPrimeTable : public PrimeTable {
  46. public:
  47. HybridPrimeTable(bool force_on_the_fly, int max_precalculated)
  48. : on_the_fly_impl_(new OnTheFlyPrimeTable),
  49. precalc_impl_(force_on_the_fly ? NULL :
  50. new PreCalculatedPrimeTable(max_precalculated)),
  51. max_precalculated_(max_precalculated) {}
  52. virtual ~HybridPrimeTable() {
  53. delete on_the_fly_impl_;
  54. delete precalc_impl_;
  55. }
  56. virtual bool IsPrime(int n) const {
  57. if (precalc_impl_ != NULL && n < max_precalculated_)
  58. return precalc_impl_->IsPrime(n);
  59. else
  60. return on_the_fly_impl_->IsPrime(n);
  61. }
  62. virtual int GetNextPrime(int p) const {
  63. int next_prime = -1;
  64. if (precalc_impl_ != NULL && p < max_precalculated_)
  65. next_prime = precalc_impl_->GetNextPrime(p);
  66. return next_prime != -1 ? next_prime : on_the_fly_impl_->GetNextPrime(p);
  67. }
  68. private:
  69. OnTheFlyPrimeTable* on_the_fly_impl_;
  70. PreCalculatedPrimeTable* precalc_impl_;
  71. int max_precalculated_;
  72. };
  73. using ::testing::TestWithParam;
  74. using ::testing::Bool;
  75. using ::testing::Values;
  76. using ::testing::Combine;
  77. // To test all code paths for HybridPrimeTable we must test it with numbers
  78. // both within and outside PreCalculatedPrimeTable's capacity and also with
  79. // PreCalculatedPrimeTable disabled. We do this by defining fixture which will
  80. // accept different combinations of parameters for instantiating a
  81. // HybridPrimeTable instance.
  82. class PrimeTableTest : public TestWithParam< ::testing::tuple<bool, int> > {
  83. protected:
  84. virtual void SetUp() {
  85. // This can be written as
  86. //
  87. // bool force_on_the_fly;
  88. // int max_precalculated;
  89. // tie(force_on_the_fly, max_precalculated) = GetParam();
  90. //
  91. // once the Google C++ Style Guide allows use of ::std::tr1::tie.
  92. //
  93. bool force_on_the_fly = ::testing::get<0>(GetParam());
  94. int max_precalculated = ::testing::get<1>(GetParam());
  95. table_ = new HybridPrimeTable(force_on_the_fly, max_precalculated);
  96. }
  97. virtual void TearDown() {
  98. delete table_;
  99. table_ = NULL;
  100. }
  101. HybridPrimeTable* table_;
  102. };
  103. TEST_P(PrimeTableTest, ReturnsFalseForNonPrimes) {
  104. // Inside the test body, you can refer to the test parameter by GetParam().
  105. // In this case, the test parameter is a PrimeTable interface pointer which
  106. // we can use directly.
  107. // Please note that you can also save it in the fixture's SetUp() method
  108. // or constructor and use saved copy in the tests.
  109. EXPECT_FALSE(table_->IsPrime(-5));
  110. EXPECT_FALSE(table_->IsPrime(0));
  111. EXPECT_FALSE(table_->IsPrime(1));
  112. EXPECT_FALSE(table_->IsPrime(4));
  113. EXPECT_FALSE(table_->IsPrime(6));
  114. EXPECT_FALSE(table_->IsPrime(100));
  115. }
  116. TEST_P(PrimeTableTest, ReturnsTrueForPrimes) {
  117. EXPECT_TRUE(table_->IsPrime(2));
  118. EXPECT_TRUE(table_->IsPrime(3));
  119. EXPECT_TRUE(table_->IsPrime(5));
  120. EXPECT_TRUE(table_->IsPrime(7));
  121. EXPECT_TRUE(table_->IsPrime(11));
  122. EXPECT_TRUE(table_->IsPrime(131));
  123. }
  124. TEST_P(PrimeTableTest, CanGetNextPrime) {
  125. EXPECT_EQ(2, table_->GetNextPrime(0));
  126. EXPECT_EQ(3, table_->GetNextPrime(2));
  127. EXPECT_EQ(5, table_->GetNextPrime(3));
  128. EXPECT_EQ(7, table_->GetNextPrime(5));
  129. EXPECT_EQ(11, table_->GetNextPrime(7));
  130. EXPECT_EQ(131, table_->GetNextPrime(128));
  131. }
  132. // In order to run value-parameterized tests, you need to instantiate them,
  133. // or bind them to a list of values which will be used as test parameters.
  134. // You can instantiate them in a different translation module, or even
  135. // instantiate them several times.
  136. //
  137. // Here, we instantiate our tests with a list of parameters. We must combine
  138. // all variations of the boolean flag suppressing PrecalcPrimeTable and some
  139. // meaningful values for tests. We choose a small value (1), and a value that
  140. // will put some of the tested numbers beyond the capability of the
  141. // PrecalcPrimeTable instance and some inside it (10). Combine will produce all
  142. // possible combinations.
  143. INSTANTIATE_TEST_CASE_P(MeaningfulTestParameters,
  144. PrimeTableTest,
  145. Combine(Bool(), Values(1, 10)));
  146. #else
  147. // Google Test may not support Combine() with some compilers. If we
  148. // use conditional compilation to compile out all code referring to
  149. // the gtest_main library, MSVC linker will not link that library at
  150. // all and consequently complain about missing entry point defined in
  151. // that library (fatal error LNK1561: entry point must be
  152. // defined). This dummy test keeps gtest_main linked in.
  153. TEST(DummyTest, CombineIsNotSupportedOnThisPlatform) {}
  154. #endif // GTEST_HAS_COMBINE