main.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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 17
  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. "5.1",
  87. "MAX"
  88. };
  89. void BuildRegressionTestSuite()
  90. {
  91. TestBase *test;
  92. for (unsigned int i = 0; i < testmanager->m_testList.size(); i++) {
  93. test = testmanager->m_testList[i];
  94. if (test->m_runInRegression) {
  95. test->m_testSuiteName.push_back("Regression");
  96. }
  97. }
  98. }
  99. ///////////////////////////////////////////////////////////
  100. void showTests()
  101. {
  102. TestBase *test = testmanager->m_testList[0];
  103. for (unsigned i = 0; i < testmanager->m_testList.size(); i++) {
  104. test = testmanager->m_testList[i];
  105. string name = test->m_name, index = test->m_testSuiteName[0];
  106. printf("%d) %s (part of %s suite" ,i+1, name.c_str(), index.c_str());
  107. for (unsigned j = 0; j < test->m_testSuiteName.size(); ++j) {
  108. if ( test->m_testSuiteName[j] == index)
  109. continue;
  110. printf(", %s suite",
  111. test->m_testSuiteName[j].c_str());
  112. index = test->m_testSuiteName[j];
  113. }
  114. printf("), (%s <= HW Version <= %s)\n",
  115. ipa_hw_type_name[test->m_minIPAHwType],
  116. ipa_hw_type_name[test->m_maxIPAHwType]);
  117. }
  118. // Example:
  119. // 15) DmaModeMBIMggregationLoopTest (part of DmaMbim16Agg suite), (1.0 <= HW Version <= 2.1)
  120. }
  121. void showSuits()
  122. {
  123. TestBase *test;
  124. std::set<string> suiteSet;
  125. int suiteIndex = 1;
  126. test = testmanager->m_testList[0];
  127. for (unsigned i = 0; i < testmanager->m_testList.size(); i++) {
  128. test = testmanager->m_testList[i];
  129. for (unsigned j = 0; j < test->m_testSuiteName.size() ; j++)
  130. suiteSet.insert(test->m_testSuiteName[j]);
  131. }
  132. for (std::set<string>::iterator i = suiteSet.begin();
  133. i != suiteSet.end(); suiteIndex++) {
  134. printf("%d) %s\n", suiteIndex, (*i).c_str());
  135. ++i;
  136. }
  137. }
  138. void preparTests(int argc, char* argv[],
  139. vector<string>& list)
  140. {
  141. for (int i = 2; i < argc; i++)
  142. list.push_back(argv[i]);
  143. }
  144. const char* getSuite(int suite_num) {
  145. int count = 0, suiteIndex = 1;
  146. string result = "error";
  147. TestBase *test = testmanager->m_testList[0];
  148. set<string> suiteSet;
  149. if (testmanager->m_testList.size() < 1)
  150. return NULL;
  151. if (count == suite_num)
  152. return testmanager->m_testList[0]->m_testSuiteName[0].c_str();
  153. for (unsigned i = 0; i < testmanager->m_testList.size(); i++) {
  154. test = testmanager->m_testList[i];
  155. for (unsigned j = 0; j < test->m_testSuiteName.size() ; j++)
  156. suiteSet.insert(test->m_testSuiteName[j]);
  157. }
  158. for (std::set<string>::iterator i = suiteSet.begin();
  159. i != suiteSet.end(); suiteIndex++) {
  160. printf("%d) %s\n", suiteIndex, (*i).c_str());
  161. if (suiteIndex == suite_num)
  162. return (*i).c_str();
  163. ++i;
  164. }
  165. return NULL;
  166. }
  167. int chooserMode() {
  168. vector<string> testSuiteList;
  169. vector<string> testNameList;
  170. unsigned int test_num;
  171. int suite_num;
  172. int type;
  173. TestBase *test;
  174. const char* res;
  175. int result = 0;
  176. char input_str[4];
  177. char* temp;
  178. printf("Welcome to the ip_accelerator\nChoose an option:\n");
  179. printf("1) Run tests\n2) Run suites\n3) Exit\nChoose an option: ");
  180. temp = fgets(input_str, sizeof(input_str), stdin);
  181. if (!temp) {
  182. printf("Error: fgets returned nullptr !!");
  183. return -1;
  184. }
  185. type = atoi(input_str);
  186. switch((enum ipa_test_type)type) {
  187. case TEST:
  188. BuildRegressionTestSuite();
  189. showTests();
  190. printf("Choose which test you wish to run: \n");
  191. fflush(stdin);
  192. temp = fgets(input_str, sizeof(input_str), stdin);
  193. if (!temp) {
  194. printf("Error: fgets returned nullptr !!");
  195. return -1;
  196. }
  197. test_num = atoi(input_str);
  198. if ( test_num > testmanager->m_testList.size()) {
  199. printf("Invalid test number. Try again\n");
  200. result = -1;
  201. break;
  202. }
  203. test = testmanager->m_testList[test_num-1];
  204. printf("Running Test %s\n",
  205. test->m_name.
  206. c_str());
  207. testNameList.push_back(test->m_name.c_str());
  208. BuildRegressionTestSuite();
  209. testmanager->Run(testSuiteList, testNameList);
  210. testNameList.clear();
  211. break;
  212. case SUITE:
  213. BuildRegressionTestSuite();
  214. showSuits();
  215. printf("Choose which suite you wish to run: \n");
  216. fflush(stdin);
  217. temp = fgets(input_str, sizeof(input_str), stdin);
  218. if (!temp) {
  219. printf("Error: fgets returned nullptr !!");
  220. return -1;
  221. }
  222. suite_num = atoi(input_str);
  223. if (suite_num < 0 || suite_num > MAX_SUITES) {
  224. printf("Invalid test number. Try again\n");
  225. result = -1;
  226. break;
  227. }
  228. res = getSuite(suite_num);
  229. if (!res) {
  230. printf("Error in getSuite. Exiting\n");
  231. result = -1;
  232. break;
  233. }
  234. testSuiteList.push_back(res);
  235. testmanager->Run(testSuiteList, testNameList);
  236. testSuiteList.clear();
  237. break;
  238. default:
  239. printf("Exiting\n");
  240. break;
  241. }
  242. return result;
  243. }
  244. int scriptMode(int argc, char* argv[]) {
  245. vector<string> testSuiteList;
  246. vector<string> testNameList;
  247. string sControlFlag;
  248. if (argc < 2) {
  249. printf("please use correct format:\n%s", sFormat.c_str());
  250. return -1;
  251. }
  252. sControlFlag = argv[1];
  253. if (sControlFlag.find("--") == string::npos) {
  254. printf("please use correct format:\n%s", sFormat.c_str());
  255. return -1;
  256. }
  257. BuildRegressionTestSuite();
  258. if (sControlFlag.compare(SHOW_TEST_FLAG) == 0) {
  259. showTests();
  260. return 0;
  261. } else if (sControlFlag.compare(SHOW_SUIT_FLAG) == 0) {
  262. showSuits();
  263. return 0;
  264. }
  265. if (sControlFlag.compare(RUN_TEST_FLAG) == 0) {
  266. preparTests(argc, argv, testNameList);
  267. } else if (sControlFlag.compare(RUN_SUIT_FLAG) == 0) {
  268. preparTests(argc, argv, testSuiteList);
  269. } else {
  270. printf("please use correct format:\n%s", sFormat.c_str());
  271. return -1;
  272. }
  273. testmanager->Run(testSuiteList, testNameList);
  274. return 0;
  275. }
  276. int main(int argc, char* argv[])
  277. {
  278. string nat_mem_type = DFLT_NAT_MEM_TYPE;
  279. int c, result = 0, what = 0;
  280. int opt_idx = 0;
  281. struct option opts[] = {
  282. /* These options set a flag. */
  283. {"chooser", no_argument, &what, 1},
  284. {"show_tests", no_argument, &what, 2},
  285. {"show_suites", no_argument, &what, 3},
  286. {"test", no_argument, &what, 4},
  287. {"suite", no_argument, &what, 5},
  288. {"mem", required_argument, 0, 'm'},
  289. {0, 0, 0, 0}
  290. };
  291. if (argc <= 1) {
  292. printf("please use correct format:\n%s", sFormat.c_str());
  293. return -1;
  294. }
  295. while ( (c = getopt_long(argc, argv, "", opts, &opt_idx)) != -1 )
  296. {
  297. switch ( c )
  298. {
  299. case 0:
  300. break;
  301. case 'm':
  302. if ( legal_nat_mem_type(optarg) )
  303. {
  304. nat_mem_type = optarg;
  305. }
  306. else
  307. {
  308. fprintf(stderr, "Illegal: --mem %s\n", optarg);
  309. exit(1);
  310. }
  311. break;
  312. default:
  313. fprintf(stderr, "Illegal command line argument passed\n");
  314. printf("please use correct format:\n%s", sFormat.c_str());
  315. exit(1);
  316. }
  317. }
  318. if ( what == 0 ) {
  319. printf("please use correct format:\n%s", sFormat.c_str());
  320. return -1;
  321. }
  322. argc = 2;
  323. switch ( what )
  324. {
  325. case 1:
  326. argv[1] = (char*) CHOOSER_MODE;
  327. break;
  328. case 2:
  329. argv[1] = (char*) SHOW_TEST_FLAG;
  330. break;
  331. case 3:
  332. argv[1] = (char*) SHOW_SUIT_FLAG;
  333. break;
  334. case 4:
  335. argv[1] = (char*) RUN_TEST_FLAG;
  336. break;
  337. case 5:
  338. argv[1] = (char*) RUN_SUIT_FLAG;
  339. break;
  340. default:
  341. printf("please use correct format:\n%s", sFormat.c_str());
  342. exit(1);
  343. }
  344. testmanager = TestManager::GetInstance(nat_mem_type.c_str());
  345. string sControlFlag = argv[1];
  346. if (sControlFlag.compare(CHOOSER_MODE) == 0) {
  347. result = chooserMode();
  348. } else {
  349. result = scriptMode(argc, argv);
  350. }
  351. return result;
  352. }//main
  353. ////////////////////////////////////////////////////////////////////