* INTC Contribution to the O-RAN F Release for O-DU Low
[o-du/phy.git] / fhi_lib / test / common / xranlib_unit_test_main.cc
1 /*******************************************************************************
2  *
3  * <COPYRIGHT_TAG>
4  *
5  *******************************************************************************/
6 #include <rte_config.h>
7 #include <rte_eal.h>
8 #include <rte_debug.h>
9
10 #include "common.hpp"
11 #include "xran_lib_wrap.hpp"
12
13 static int parse_input_parameter(std::string executable, std::string option)
14 {
15     std::size_t delim_pos = option.find("=");
16     std::string param = option.substr(delim_pos + 1);
17
18     try
19     {
20         return std::stoi(param);
21     }
22     catch(std::logic_error &e)
23     {
24         std::cout << executable << ": invalid argument '"<< param << "' for '" << option  << "'" << std::endl;
25         std::cout << "Try '" << executable << " --usage' for more information." << std::endl;
26         exit(-1);
27     }
28 }
29
30 xranLibWraper *xranlib;
31
32 void
33 ut_version_print(void)
34 {
35     char            sysversion[100];
36     char           *compilation_date = (char *)__DATE__;
37     char           *compilation_time = (char *)__TIME__;
38     char            compiler[100];
39
40     //snprintf(sysversion, 99, "Version: %s", VERSIONX);
41
42 #if defined(__clang__)
43     snprintf(compiler, 99, "family clang: %s", __clang_version__);
44 #elif defined(__ICC) || defined(__INTEL_COMPILER)
45     snprintf(compiler, 99, "family icc: version %d", __INTEL_COMPILER);
46 #elif defined(__INTEL_LLVM_COMPILER)
47     snprintf(compiler, 99, "family icx: version %d", __INTEL_LLVM_COMPILER);
48 #elif defined(__GNUC__) || defined(__GNUG__)
49     snprintf(compiler, 99, "family gcc: version %d.%d.%d", __GNUC__, __GNUC_MINOR__,__GNUC_PATCHLEVEL__);
50 #endif
51
52     printf("\n\n");
53     printf("===========================================================================================================\n");
54     printf("UNITTESTS VERSION\n");
55     printf("===========================================================================================================\n");
56
57     //printf("%s\n", sysversion);
58     printf("build-date: %s\n", compilation_date);
59     printf("build-time: %s\n", compilation_time);
60     printf("build-with: %s\n", compiler);
61 }
62
63 int main(int argc, char** argv) {
64     int all_test_ret = 0;
65     ut_version_print();
66     /* Enable xml output by default */
67     ::testing::GTEST_FLAG(output) = "xml:test_results.xml";
68
69     ::testing::InitGoogleTest(&argc, argv);
70
71     const std::string executable = argv[0];
72
73     for(int index = 1; index < argc; index++) {
74
75         const std::string option = argv[index];
76
77         if (option.find("--nb_repetitions=") != std::string::npos)
78         {
79             BenchmarkParameters::repetition = parse_input_parameter(executable, option);
80         }
81         else if (option.find("--nb_loops=") != std::string::npos)
82         {
83             BenchmarkParameters::loop = parse_input_parameter(executable, option);
84         }
85         else if (option.find("--cpu_nb=") != std::string::npos)
86         {
87             BenchmarkParameters::cpu_id = (unsigned) parse_input_parameter(executable, option);
88
89             if (BenchmarkParameters::cpu_id == 0)
90                 std::cout << std::endl << "Warning: Core #0 is running the VM's OS. "
91                           << "Measurements won't be accurate. It shouldn't be used!"
92                           << std::endl << std::endl;
93         }
94         /* --usage used instead of --help to avoid conflict with gtest --help */
95         else if (!option.compare("--usage"))
96         {
97             std::cout << "Usage: " << executable << " [GTEST_OPTION]... [OPTION]..." << std::endl;
98             std::cout << "Runs unittests with given gtest options." << std::endl;
99             std::cout << std::endl;
100             std::cout << "Available options: " << std::endl;
101             std::cout << "--nb_repetitions=NUMBER Sets how many times results are measured" << std::endl;
102             std::cout << "--nb_loops=NUMBER Sets how many times function is called per repetition"
103                       << std::endl;
104             std::cout << "--cpu_nb=NUMBER Sets core number to run tests on" << std::endl;
105             std::cout << "--usage Prints this message" << std::endl;
106
107             return 0;
108         }
109         else
110         {
111             std::cout << executable << ": inavlid option " << option << std::endl;
112             std::cout << "Try '" << executable << " --usage' for more information." << std::endl;
113
114             return -1;
115         }
116     }
117
118     xranlib = new xranLibWraper;
119
120     if(xranlib->SetUp() < 0) {
121         return (-1);
122         }
123
124     all_test_ret = RUN_ALL_TESTS();
125
126     xranlib->TearDown();
127
128     if(xranlib != nullptr) {
129         delete xranlib;
130         xranlib = nullptr;
131         }
132
133     return all_test_ret;
134 }
135
136
137