main.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. /*
  2. * Copyright (c) 2017-2018,2020 The Linux Foundation. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are
  6. * met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above
  10. * copyright notice, this list of conditions and the following
  11. * disclaimer in the documentation and/or other materials provided
  12. * with the distribution.
  13. * * Neither the name of The Linux Foundation nor the names of its
  14. * contributors may be used to endorse or promote products derived
  15. * from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
  18. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  19. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
  21. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  22. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  23. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  24. * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  25. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  26. * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
  27. * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #include <getopt.h>
  30. #include "Logger.h"
  31. #include "TestManager.h"
  32. #include "TestsUtils.h"
  33. #include <stdio.h>
  34. #include <iostream>
  35. #include <set>
  36. ///////////////////////////////////////////////////////////
  37. Logger g_Logger(LOG_ERROR);
  38. #define CHOOSER_MODE "--chooser"
  39. #define SHOW_TEST_FLAG "--show_tests"
  40. #define SHOW_SUIT_FLAG "--show_suites"
  41. #define RUN_TEST_FLAG "--test"
  42. #define RUN_SUIT_FLAG "--suite"
  43. string sFormat = "ip_accelerator <control_flag> <suit/name>, ..., <suit/name>\n"
  44. "contorl_flag = " RUN_TEST_FLAG " or " RUN_SUIT_FLAG "\n"
  45. "ip_accelerator " SHOW_TEST_FLAG "\n"
  46. "ip_accelerator " SHOW_SUIT_FLAG "\n"
  47. "or ip_accelerator --chooser "
  48. "for menu chooser interface\n";
  49. #define MAX_SUITES 15
  50. #undef strcasesame
  51. #define strcasesame(x, y) \
  52. (! strcasecmp((x), (y)))
  53. #undef legal_nat_mem_type
  54. #define legal_nat_mem_type(mt) \
  55. ( strcasesame(mt, "DDR") || \
  56. strcasesame(mt, "SRAM") || \
  57. strcasesame(mt, "HYBRID") )
  58. TestManager *testmanager = NULL;
  59. enum ipa_test_type{
  60. TEST = 1,
  61. SUITE,
  62. EXIT,
  63. MAX_TYPE
  64. };
  65. const char *ipa_hw_type_name[] = {
  66. "None",
  67. "1.0",
  68. "1.1",
  69. "2.0",
  70. "2.1",
  71. "2.5/2.6",
  72. "2.6L",
  73. "Unused",
  74. "Unused",
  75. "Unused",
  76. "3.0",
  77. "3.1",
  78. "3.5",
  79. "3.5.1",
  80. "4.0",
  81. "4.1",
  82. "4.2",
  83. "4.5",
  84. "4.9",
  85. "5.0",
  86. "MAX"
  87. };
  88. void BuildRegressionTestSuite()
  89. {
  90. TestBase *test;
  91. for (unsigned int i = 0; i < testmanager->m_testList.size(); i++) {
  92. test = testmanager->m_testList[i];
  93. if (test->m_runInRegression) {
  94. test->m_testSuiteName.push_back("Regression");
  95. }
  96. }
  97. }
  98. ///////////////////////////////////////////////////////////
  99. void showTests()
  100. {
  101. TestBase *test = testmanager->m_testList[0];
  102. for (unsigned i = 0; i < testmanager->m_testList.size(); i++) {
  103. test = testmanager->m_testList[i];
  104. string name = test->m_name, index = test->m_testSuiteName[0];
  105. printf("%d) %s (part of %s suite" ,i+1, name.c_str(), index.c_str());
  106. for (unsigned j = 0; j < test->m_testSuiteName.size(); ++j) {
  107. if ( test->m_testSuiteName[j] == index)
  108. continue;
  109. printf(", %s suite",
  110. test->m_testSuiteName[j].c_str());
  111. index = test->m_testSuiteName[j];
  112. }
  113. printf("), (%s <= HW Version <= %s)\n",
  114. ipa_hw_type_name[test->m_minIPAHwType],
  115. ipa_hw_type_name[test->m_maxIPAHwType]);
  116. }
  117. // Example:
  118. // 15) DmaModeMBIMggregationLoopTest (part of DmaMbim16Agg suite), (1.0 <= HW Version <= 2.1)
  119. }
  120. void showSuits()
  121. {
  122. TestBase *test;
  123. std::set<string> suiteSet;
  124. int suiteIndex = 1;
  125. test = testmanager->m_testList[0];
  126. for (unsigned i = 0; i < testmanager->m_testList.size(); i++) {
  127. test = testmanager->m_testList[i];
  128. for (unsigned j = 0; j < test->m_testSuiteName.size() ; j++)
  129. suiteSet.insert(test->m_testSuiteName[j]);
  130. }
  131. for (std::set<string>::iterator i = suiteSet.begin();
  132. i != suiteSet.end(); suiteIndex++) {
  133. printf("%d) %s\n", suiteIndex, (*i).c_str());
  134. ++i;
  135. }
  136. }
  137. void preparTests(int argc, char* argv[],
  138. vector<string>& list)
  139. {
  140. for (int i = 2; i < argc; i++)
  141. list.push_back(argv[i]);
  142. }
  143. const char* getSuite(int suite_num) {
  144. int count = 0, suiteIndex = 1;
  145. string result = "error";
  146. TestBase *test = testmanager->m_testList[0];
  147. set<string> suiteSet;
  148. if (testmanager->m_testList.size() < 1)
  149. return NULL;
  150. if (count == suite_num)
  151. return testmanager->m_testList[0]->m_testSuiteName[0].c_str();
  152. for (unsigned i = 0; i < testmanager->m_testList.size(); i++) {
  153. test = testmanager->m_testList[i];
  154. for (unsigned j = 0; j < test->m_testSuiteName.size() ; j++)
  155. suiteSet.insert(test->m_testSuiteName[j]);
  156. }
  157. for (std::set<string>::iterator i = suiteSet.begin();
  158. i != suiteSet.end(); suiteIndex++) {
  159. printf("%d) %s\n", suiteIndex, (*i).c_str());
  160. if (suiteIndex == suite_num)
  161. return (*i).c_str();
  162. ++i;
  163. }
  164. return NULL;
  165. }
  166. int chooserMode() {
  167. vector<string> testSuiteList;
  168. vector<string> testNameList;
  169. unsigned int test_num;
  170. int suite_num;
  171. int type;
  172. TestBase *test;
  173. const char* res;
  174. int result = 0;
  175. char input_str[4];
  176. printf("Welcome to the ip_accelerator\nChoose an option:\n");
  177. printf("1) Run tests\n2) Run suites\n3) Exit\nChoose an option: ");
  178. res = fgets(input_str, sizeof(input_str), stdin);
  179. type = atoi(input_str);
  180. switch((enum ipa_test_type)type) {
  181. case TEST:
  182. BuildRegressionTestSuite();
  183. showTests();
  184. printf("Choose which test you wish to run: \n");
  185. fflush(stdin);
  186. res = fgets(input_str, sizeof(input_str), stdin);
  187. test_num = atoi(input_str);
  188. if ( test_num > testmanager->m_testList.size()) {
  189. printf("Invalid test number. Try again\n");
  190. result = -1;
  191. break;
  192. }
  193. test = testmanager->m_testList[test_num-1];
  194. printf("Running Test %s\n",
  195. test->m_name.
  196. c_str());
  197. testNameList.push_back(test->m_name.c_str());
  198. BuildRegressionTestSuite();
  199. testmanager->Run(testSuiteList, testNameList);
  200. testNameList.clear();
  201. break;
  202. case SUITE:
  203. BuildRegressionTestSuite();
  204. showSuits();
  205. printf("Choose which suite you wish to run: \n");
  206. fflush(stdin);
  207. res = fgets(input_str, sizeof(input_str), stdin);
  208. suite_num = atoi(input_str);
  209. if (suite_num < 0 || suite_num > MAX_SUITES) {
  210. printf("Invalid test number. Try again\n");
  211. result = -1;
  212. break;
  213. }
  214. res = getSuite(suite_num);
  215. if (!res) {
  216. printf("Error in getSuite. Exiting\n");
  217. result = -1;
  218. break;
  219. }
  220. testSuiteList.push_back(res);
  221. testmanager->Run(testSuiteList, testNameList);
  222. testSuiteList.clear();
  223. break;
  224. default:
  225. printf("Exiting\n");
  226. break;
  227. }
  228. return result;
  229. }
  230. int scriptMode(int argc, char* argv[]) {
  231. vector<string> testSuiteList;
  232. vector<string> testNameList;
  233. string sControlFlag;
  234. if (argc < 2) {
  235. printf("please use correct format:\n%s", sFormat.c_str());
  236. return -1;
  237. }
  238. sControlFlag = argv[1];
  239. if (sControlFlag.find("--") == string::npos) {
  240. printf("please use correct format:\n%s", sFormat.c_str());
  241. return -1;
  242. }
  243. BuildRegressionTestSuite();
  244. if (sControlFlag.compare(SHOW_TEST_FLAG) == 0) {
  245. showTests();
  246. return 0;
  247. } else if (sControlFlag.compare(SHOW_SUIT_FLAG) == 0) {
  248. showSuits();
  249. return 0;
  250. }
  251. if (sControlFlag.compare(RUN_TEST_FLAG) == 0) {
  252. preparTests(argc, argv, testNameList);
  253. } else if (sControlFlag.compare(RUN_SUIT_FLAG) == 0) {
  254. preparTests(argc, argv, testSuiteList);
  255. } else {
  256. printf("please use correct format:\n%s", sFormat.c_str());
  257. return -1;
  258. }
  259. testmanager->Run(testSuiteList, testNameList);
  260. return 0;
  261. }
  262. int main(int argc, char* argv[])
  263. {
  264. string nat_mem_type = DFLT_NAT_MEM_TYPE;
  265. int c, result = 0, what = 0;
  266. int opt_idx = 0;
  267. struct option opts[] = {
  268. /* These options set a flag. */
  269. {"chooser", no_argument, &what, 1},
  270. {"show_tests", no_argument, &what, 2},
  271. {"show_suites", no_argument, &what, 3},
  272. {"test", no_argument, &what, 4},
  273. {"suite", no_argument, &what, 5},
  274. {"mem", required_argument, 0, 'm'},
  275. {0, 0, 0, 0}
  276. };
  277. if (argc <= 1) {
  278. printf("please use correct format:\n%s", sFormat.c_str());
  279. return -1;
  280. }
  281. while ( (c = getopt_long(argc, argv, "", opts, &opt_idx)) != -1 )
  282. {
  283. switch ( c )
  284. {
  285. case 0:
  286. break;
  287. case 'm':
  288. if ( legal_nat_mem_type(optarg) )
  289. {
  290. nat_mem_type = optarg;
  291. }
  292. else
  293. {
  294. fprintf(stderr, "Illegal: --mem %s\n", optarg);
  295. exit(1);
  296. }
  297. break;
  298. default:
  299. fprintf(stderr, "Illegal command line argument passed\n");
  300. printf("please use correct format:\n%s", sFormat.c_str());
  301. exit(1);
  302. }
  303. }
  304. if ( what == 0 ) {
  305. printf("please use correct format:\n%s", sFormat.c_str());
  306. return -1;
  307. }
  308. argc = 2;
  309. switch ( what )
  310. {
  311. case 1:
  312. argv[1] = (char*) CHOOSER_MODE;
  313. break;
  314. case 2:
  315. argv[1] = (char*) SHOW_TEST_FLAG;
  316. break;
  317. case 3:
  318. argv[1] = (char*) SHOW_SUIT_FLAG;
  319. break;
  320. case 4:
  321. argv[1] = (char*) RUN_TEST_FLAG;
  322. break;
  323. case 5:
  324. argv[1] = (char*) RUN_SUIT_FLAG;
  325. break;
  326. default:
  327. printf("please use correct format:\n%s", sFormat.c_str());
  328. exit(1);
  329. }
  330. testmanager = TestManager::GetInstance(nat_mem_type.c_str());
  331. string sControlFlag = argv[1];
  332. if (sControlFlag.compare(CHOOSER_MODE) == 0) {
  333. result = chooserMode();
  334. } else {
  335. result = scriptMode(argc, argv);
  336. }
  337. return result;
  338. }//main
  339. ////////////////////////////////////////////////////////////////////