Util.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. #ifndef UTIL_H_
  19. #define UTIL_H_
  20. #include <map>
  21. #include <vector>
  22. #include <string>
  23. #include "zookeeper_log.h"
  24. // number of elements in array
  25. #define COUNTOF(array) sizeof(array)/sizeof(array[0])
  26. #define DECLARE_WRAPPER(ret,sym,sig) \
  27. extern "C" ret __real_##sym sig; \
  28. extern "C" ret __wrap_##sym sig
  29. #define CALL_REAL(sym,params) \
  30. __real_##sym params
  31. // must include "src/zookeeper_log.h" to be able to use this macro
  32. #define TEST_TRACE(x...) \
  33. log_message(LOGSTREAM, ZOO_LOG_LEVEL_DEBUG,__LINE__,__func__,x)
  34. extern const std::string EMPTY_STRING;
  35. // *****************************************************************************
  36. // A bit of wizardry to get to the bare type from a reference or a pointer
  37. // to the type
  38. template <class T>
  39. struct TypeOp {
  40. typedef T BareT;
  41. typedef T ArgT;
  42. };
  43. // partial specialization for reference types
  44. template <class T>
  45. struct TypeOp<T&>{
  46. typedef T& ArgT;
  47. typedef typename TypeOp<T>::BareT BareT;
  48. };
  49. // partial specialization for pointers
  50. template <class T>
  51. struct TypeOp<T*>{
  52. typedef T* ArgT;
  53. typedef typename TypeOp<T>::BareT BareT;
  54. };
  55. // *****************************************************************************
  56. // Container utilities
  57. template <class K, class V>
  58. void putValue(std::map<K,V>& map,const K& k, const V& v){
  59. typedef std::map<K,V> Map;
  60. typename Map::const_iterator it=map.find(k);
  61. if(it==map.end())
  62. map.insert(typename Map::value_type(k,v));
  63. else
  64. map[k]=v;
  65. }
  66. template <class K, class V>
  67. bool getValue(const std::map<K,V>& map,const K& k,V& v){
  68. typedef std::map<K,V> Map;
  69. typename Map::const_iterator it=map.find(k);
  70. if(it==map.end())
  71. return false;
  72. v=it->second;
  73. return true;
  74. }
  75. // *****************************************************************************
  76. // misc utils
  77. // millisecond sleep
  78. void millisleep(int ms);
  79. FILE *openlogfile(const char* name);
  80. // evaluate given predicate until it returns true or the timeout
  81. // (in millis) has expired
  82. template<class Predicate>
  83. int ensureCondition(const Predicate& p,int timeout){
  84. int elapsed=0;
  85. while(!p() && elapsed<timeout){
  86. millisleep(2);
  87. elapsed+=2;
  88. }
  89. return elapsed;
  90. };
  91. // *****************************************************************************
  92. // test global configuration data
  93. class TestConfig{
  94. typedef std::vector<std::string> CmdLineOptList;
  95. public:
  96. typedef CmdLineOptList::const_iterator const_iterator;
  97. TestConfig(){}
  98. ~TestConfig(){}
  99. void addConfigFromCmdLine(int argc, char* argv[]){
  100. if(argc>=2)
  101. testName_=argv[1];
  102. for(int i=2; i<argc;++i)
  103. cmdOpts_.push_back(argv[i]);
  104. }
  105. const_iterator getExtraOptBegin() const {return cmdOpts_.begin();}
  106. const_iterator getExtraOptEnd() const {return cmdOpts_.end();}
  107. size_t getExtraOptCount() const {
  108. return cmdOpts_.size();
  109. }
  110. const std::string& getTestName() const {
  111. return testName_=="all"?EMPTY_STRING:testName_;
  112. }
  113. private:
  114. CmdLineOptList cmdOpts_;
  115. std::string testName_;
  116. };
  117. extern TestConfig globalTestConfig;
  118. #endif /*UTIL_H_*/