Advertisement
Guest User

Untitled

a guest
Feb 27th, 2016
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.05 KB | None | 0 0
  1. // required includes
  2. #include <cstdio>  // printf and friends
  3. #include <cstdlib> // malloc, free, qsort
  4. #include <cstring> // strlen, strcpy, strtok
  5. #include <new>     // placement new
  6.  
  7. struct String
  8. {
  9.     char* m_str;
  10.  
  11.     //__attribute__((noinline))
  12.     void copy(const String& other) {
  13.         if(m_str)
  14.             free(m_str);
  15.         m_str = 0;
  16.  
  17.  
  18.         if(other.m_str) {
  19.             m_str = static_cast<char*>(malloc(strlen(other.m_str) + 1));
  20.             strcpy(m_str, other.m_str);
  21.         }
  22.     }
  23.  
  24.     //__attribute__((noinline))
  25.     String(const char* in = 0)
  26.             : m_str(0) {
  27.         if(in == 0)
  28.             return;
  29.  
  30.         m_str = static_cast<char*>(malloc(strlen(in) + 1));
  31.         strcpy(m_str, in);
  32.     }
  33.  
  34.     //__attribute__((noinline))
  35.     String(const String& other)
  36.             : m_str(0) {
  37.         copy(other);
  38.     }
  39.  
  40.     ~String() {
  41.         if(m_str)
  42.             free(m_str);
  43.     }
  44.  
  45.     String& operator=(const String& other) {
  46.         if(this != &other)
  47.             copy(other);
  48.         return *this;
  49.     }
  50. };
  51.  
  52. struct FunctionData
  53. {
  54.     String m_suite;
  55.     String m_name;
  56.  
  57.     const char* m_file;
  58.  
  59.     FunctionData(const char* suite, const char* name, const char* file)
  60.             : m_suite(suite)
  61.             , m_name(name)
  62.             , m_file(file) {}
  63.  
  64.     FunctionData(const FunctionData& other)
  65.             : m_suite(other.m_suite)
  66.             , m_name(other.m_name)
  67.             , m_file(other.m_file) {}
  68. };
  69.  
  70. const char*& getCurrentTestSuite() {
  71.     static const char* data = 0;
  72.     return data;
  73. }
  74.  
  75. int setTestSuiteName(const char* name) {
  76.     getCurrentTestSuite() = name;
  77.     return 0;
  78. }
  79.  
  80. int regTest(const char* file, const char* name) {
  81.     FunctionData temp(FunctionData(String(getCurrentTestSuite()).m_str, name, file));
  82.  
  83.     printf("hello! %s\n", temp.m_name);
  84.  
  85.     return 0;
  86. }
  87.  
  88. __attribute__((unused)) static int a1 = setTestSuiteName("current testsuite");
  89. __attribute__((unused)) static int a2 = regTest("a.cpp", "zzz");
  90.  
  91. int main(int, char**) { return 0; }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement