Util.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * Copyright 2008, Yahoo! 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. #ifndef UTIL_H_
  17. #define UTIL_H_
  18. #include <map>
  19. #include <vector>
  20. #include <string>
  21. #include "src/zk_log.h"
  22. // number of elements in array
  23. #define COUNTOF(array) sizeof(array)/sizeof(array[0])
  24. #define DECLARE_WRAPPER(ret,sym,sig) \
  25. extern "C" ret __real_##sym sig; \
  26. extern "C" ret __wrap_##sym sig
  27. #define CALL_REAL(sym,params) \
  28. __real_##sym params
  29. // must include "src/zk_log.h" to be able to use this macro
  30. #define TEST_TRACE(x) \
  31. log_message(LOG_LEVEL_DEBUG,__LINE__,__func__,format_log_message x)
  32. extern const std::string EMPTY_STRING;
  33. // *****************************************************************************
  34. // A bit of wizardry to get to the bare type from a reference or a pointer
  35. // to the type
  36. template <class T>
  37. struct TypeOp {
  38. typedef T BareT;
  39. typedef T ArgT;
  40. };
  41. // partial specialization for reference types
  42. template <class T>
  43. struct TypeOp<T&>{
  44. typedef T& ArgT;
  45. typedef typename TypeOp<T>::BareT BareT;
  46. };
  47. // partial specialization for pointers
  48. template <class T>
  49. struct TypeOp<T*>{
  50. typedef T* ArgT;
  51. typedef typename TypeOp<T>::BareT BareT;
  52. };
  53. // *****************************************************************************
  54. // Container utilities
  55. template <class K, class V>
  56. void putValue(std::map<K,V>& map,const K& k, const V& v){
  57. typedef std::map<K,V> Map;
  58. typename Map::const_iterator it=map.find(k);
  59. if(it==map.end())
  60. map.insert(typename Map::value_type(k,v));
  61. else
  62. map[k]=v;
  63. }
  64. template <class K, class V>
  65. bool getValue(const std::map<K,V>& map,const K& k,V& v){
  66. typedef std::map<K,V> Map;
  67. typename Map::const_iterator it=map.find(k);
  68. if(it==map.end())
  69. return false;
  70. v=it->second;
  71. return true;
  72. }
  73. // *****************************************************************************
  74. // misc utils
  75. // millisecond sleep
  76. void millisleep(int ms);
  77. // evaluate given predicate until it returns true or the timeout
  78. // (in millis) has expired
  79. template<class Predicate>
  80. int ensureCondition(const Predicate& p,int timeout){
  81. int elapsed=0;
  82. while(!p() && elapsed<timeout){
  83. millisleep(2);
  84. elapsed+=2;
  85. }
  86. return elapsed;
  87. };
  88. // *****************************************************************************
  89. // test global configuration data
  90. class TestConfig{
  91. typedef std::vector<std::string> CmdLineOptList;
  92. public:
  93. typedef CmdLineOptList::const_iterator const_iterator;
  94. TestConfig(){}
  95. ~TestConfig(){}
  96. void addConfigFromCmdLine(int argc, char* argv[]){
  97. if(argc>=2)
  98. testName_=argv[1];
  99. for(int i=2; i<argc;++i)
  100. cmdOpts_.push_back(argv[i]);
  101. }
  102. const_iterator getExtraOptBegin() const {return cmdOpts_.begin();}
  103. const_iterator getExtraOptEnd() const {return cmdOpts_.end();}
  104. size_t getExtraOptCount() const {
  105. return cmdOpts_.size();
  106. }
  107. const std::string& getTestName() const {
  108. return testName_=="all"?EMPTY_STRING:testName_;
  109. }
  110. private:
  111. CmdLineOptList cmdOpts_;
  112. std::string testName_;
  113. };
  114. extern TestConfig globalTestConfig;
  115. #endif /*UTIL_H_*/