• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) Huawei Technologies Co., Ltd. 2018-2020. All rights reserved.
3  * Description: CPPTest框架中ext入口
4  */
5 
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <strings.h>
9 #include <string>
10 #include "gtest/hwext/gtest-ext.h"
11 
12 
13 namespace testing {
14   namespace ext {
15     #define GTEST_EXT_DEBUG 0
16 
instance()17     TestDefManager* TestDefManager::instance()
18     {
19         static TestDefManager* instance_ = NULL;
20         if (instance_ == NULL) {
21             instance_ = new TestDefManager();
22         }
23         return instance_;
24     }
25 
cinstance()26     const TestDefManager* TestDefManager::cinstance()
27     {
28         return instance();
29     }
30 
31     // c'tor, all members immutable
TestDefInfo(const char * tcn,const char * n,int fs,TestDefType tdf)32     TestDefInfo::TestDefInfo(const char* tcn, const char* n, int fs, TestDefType tdf) :\
33         test_case_name(tcn), name(n), flags(fs), def_type(tdf) {};
34 
regist(const char * test_case_name,const char * test_name,int test_flags,TestDefType tdf)35     bool TestDefManager::regist(const char* test_case_name, const char* test_name, int test_flags, TestDefType tdf)
36     {
37         TestDefManager::testDefInfos.push_back(new TestDefInfo(test_case_name, test_name, test_flags, tdf));
38         return true;
39     }
40 
queryFlagsFor(const TestInfo * test,int def_value) const41     int TestDefManager::queryFlagsFor(const TestInfo* test, int def_value) const
42     {
43         const TestDefInfo* def = findDefFor(test);
44         return def == NULL ? def_value : def->flags;
45     }
46 
findDefFor(const TestInfo * test) const47     const TestDefInfo* TestDefManager::findDefFor(const TestInfo* test) const
48     {
49         // search by matching test definition information
50         NamingMatchType case_name_mt = AEqualsB;
51         NamingMatchType test_name_mt = AEqualsB;
52 
53         for (unsigned int i = 0; i < testDefInfos.size(); i++)
54         {
55             const TestDefInfo* info = testDefInfos.at(i);
56             switch (info->def_type) {
57                 case Plain:
58                 case Fixtured:
59                     case_name_mt = AEqualsB;
60                     test_name_mt = AEqualsB;
61                     break;
62                 case Typed:
63                     case_name_mt = AStartsWithB;
64                     test_name_mt = AEqualsB;
65                     break;
66                 case PatternTyped:
67                     case_name_mt = AContainsB;
68                     test_name_mt = AEqualsB;
69                     break;
70                 case Parameterized:
71                     case_name_mt = AEndsWithB;
72                     test_name_mt = AStartsWithB;
73                     break;
74                 default:
75                     break;
76             }
77 
78             const bool matched = matchNaming(test->test_case_name(), info->test_case_name, case_name_mt)
79               && matchNaming(test->name(), info->name, test_name_mt);
80             if (matched) {
81                 return info;
82             }
83         }
84 
85         #if GTEST_EXT_DEBUG
86         printf("cannot find test definition for: %s.%s\n", test->test_case_name(), test->name());
87         #endif
88         return NULL;
89     }
90 
matchNaming(const char * const a,const char * const b,NamingMatchType mt) const91     bool TestDefManager::matchNaming(const char * const a, const char * const b, NamingMatchType mt) const
92     {
93         const char sep = TestDefInfo::kNamingSepchar;
94         const int len_a = strlen(a);
95         const int len_b = strlen(b);
96         int i;
97         switch (mt) {
98         case AEqualsB:
99             // a=b
100             return strcmp(a, b) == 0;
101         case AStartsWithB:
102             // a=b/xxx
103             return strstr(a, b) == a && a[len_b] == sep;
104         case AContainsB:
105             // a=xxx/b/yyy
106             for (i = 1; i < len_a - len_b; i++) {
107                 if (a[i - 1] == sep&&a[i + len_b] == sep&&strstr(a + i, b) == a + i) {
108                     return true;
109                 }
110             }
111             return false;
112         case AEndsWithB:
113             // a=xxx/b
114             return len_a > len_b&&a[len_a - len_b - 1] == sep&&strcmp(a + len_a - len_b, b) == 0;
115         default:
116             fprintf(stderr, "Illegal NamingMatchType: %d", mt);
117             return false;
118         }
119     }
120 
getLevel(const std::string testcasename,const std::string testname) const121     int TestDefManager::getLevel(const std::string testcasename, const std::string testname) const
122     {
123         NamingMatchType case_name_mt = AEqualsB;
124         NamingMatchType test_name_mt = AEqualsB;
125         int levelShift = 24;
126         int levelList[5] = {1, 2, 4, 8, 16};
127         enum indexEnum {I0 = 0, I1, I2, I3, I4};
128 
129         for (unsigned int i = 0; i < testDefInfos.size(); i++) {
130             const TestDefInfo* info = testDefInfos.at(i);
131             switch (info->def_type) {
132                 case Plain:
133                 case Fixtured:
134                     case_name_mt = AEqualsB;
135                     test_name_mt = AEqualsB;
136                     break;
137                 case Typed:
138                     case_name_mt = AStartsWithB;
139                     test_name_mt = AEqualsB;
140                     break;
141                 case PatternTyped:
142                     case_name_mt = AContainsB;
143                     test_name_mt = AEqualsB;
144                     break;
145                 case Parameterized:
146                     case_name_mt = AEndsWithB;
147                     test_name_mt = AStartsWithB;
148                     break;
149                 default:
150                     break;
151             }
152 
153             const bool matched = matchNaming(testcasename.c_str(), info->test_case_name, case_name_mt)
154               && matchNaming(testname.c_str(), info->name, test_name_mt);
155             if (matched) {
156                 int level = (info->flags >> levelShift);
157                 if (level == levelList[I0]) return I0;
158                 if (level == levelList[I1]) return I1;
159                 if (level == levelList[I2]) return I2;
160                 if (level == levelList[I3]) return I3;
161                 if (level == levelList[I4]) return I4;
162             }
163         }
164         return -1;
165     }
166 
getTestFlags(const std::string testcasename,const std::string testname) const167     int* TestDefManager::getTestFlags (const std::string testcasename, const std::string testname) const
168     {
169         NamingMatchType case_name_mt = AEqualsB;
170         NamingMatchType test_name_mt = AEqualsB;
171         static int flagList[3] = {-1, -1, -1};
172         int posType = 8;
173         int posSize = 4;
174         int indexType = 0;
175         int indexSize = 1;
176         int indexRank = 2;
177 
178         for (unsigned int i = 0; i < testDefInfos.size(); i++) {
179             const TestDefInfo* info = testDefInfos.at(i);
180             switch (info->def_type) {
181                 case Plain:
182                 case Fixtured:
183                     case_name_mt = AEqualsB;
184                     test_name_mt = AEqualsB;
185                     break;
186                 case Typed:
187                     case_name_mt = AStartsWithB;
188                     test_name_mt = AEqualsB;
189                     break;
190                 case PatternTyped:
191                     case_name_mt = AContainsB;
192                     test_name_mt = AEqualsB;
193                     break;
194                 case Parameterized:
195                     case_name_mt = AEndsWithB;
196                     test_name_mt = AStartsWithB;
197                     break;
198                 default:
199                     break;
200             }
201 
202             const bool matched = matchNaming(testcasename.c_str(), info->test_case_name, case_name_mt)
203               && matchNaming(testname.c_str(), info->name, test_name_mt);
204             if (matched) {
205                 // get the three flag
206                 int type = (info->flags >> posType);
207                 int size = (info->flags >> posSize);
208                 int rank =  info->flags;
209 
210                 // add three flag value in the flag list
211                 flagList[indexType] = type;
212                 flagList[indexSize] = size;
213                 flagList[indexRank] = rank;
214             }
215         }
216         return flagList;
217     }
218 
219   } //namespace ext
220 } //namespace testing
221