FBVectorTestUtil.h 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. * Copyright 2012-present Facebook, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. //
  17. // Author: andrei.alexandrescu@fb.com
  18. #include <boost/random/mersenne_twister.hpp>
  19. #include <folly/Benchmark.h>
  20. #include <folly/FBString.h>
  21. #include <folly/Random.h>
  22. #include <folly/portability/GTest.h>
  23. namespace folly {
  24. namespace test {
  25. namespace detail {
  26. auto static const seed = randomNumberSeed();
  27. typedef boost::random::mt19937 RandomT;
  28. static RandomT rng(seed);
  29. template <class Integral1, class Integral2>
  30. Integral2 random(Integral1 low, Integral2 up) {
  31. boost::uniform_int<> range(low, up);
  32. return range(rng);
  33. }
  34. template <class String>
  35. void randomString(String* toFill, unsigned int maxSize = 1000) {
  36. assert(toFill);
  37. toFill->resize(random(0, maxSize));
  38. for (auto& c : *toFill) {
  39. c = random('a', 'z');
  40. }
  41. }
  42. template <class String, class Integral>
  43. void Num2String(String& str, Integral /* n */) {
  44. str.resize(10, '\0');
  45. sprintf(&str[0], "%ul", 10);
  46. str.resize(strlen(str.c_str()));
  47. }
  48. std::list<char> RandomList(unsigned int maxSize) {
  49. std::list<char> lst(random(0u, maxSize));
  50. std::list<char>::iterator i = lst.begin();
  51. for (; i != lst.end(); ++i) {
  52. *i = random('a', 'z');
  53. }
  54. return lst;
  55. }
  56. template <class T>
  57. T randomObject();
  58. template <>
  59. int randomObject<int>() {
  60. return random(0, 1024);
  61. }
  62. template <>
  63. std::string randomObject<std::string>() {
  64. std::string result;
  65. randomString(&result);
  66. return result;
  67. }
  68. template <>
  69. folly::fbstring randomObject<folly::fbstring>() {
  70. folly::fbstring result;
  71. randomString(&result);
  72. return result;
  73. }
  74. #define CONCAT(A, B) CONCAT_HELPER(A, B)
  75. #define CONCAT_HELPER(A, B) A##B
  76. #define BENCHFUN(F) CONCAT(CONCAT(BM_, F), CONCAT(_, VECTOR))
  77. #define TESTFUN(F) TEST(fbvector, CONCAT(F, VECTOR))
  78. } // namespace detail
  79. } // namespace test
  80. } // namespace folly