O-RAN E Maintenance Release contribution for ODULOW
[o-du/phy.git] / fhi_lib / test / common / xranlib_unit_test_main.cc
1 /*******************************************************************************
2  *
3  * Copyright (c) 2020 Intel.
4  * 
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  * 
9  * http://www.apache.org/licenses/LICENSE-2.0
10  * 
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  * 
17  *
18  *******************************************************************************/
19 #include <rte_config.h>
20 #include <rte_eal.h>
21 #include <rte_debug.h>
22
23 #include "common.hpp"
24 #include "xran_lib_wrap.hpp"
25
26 static int parse_input_parameter(std::string executable, std::string option)
27 {
28     std::size_t delim_pos = option.find("=");
29     std::string param = option.substr(delim_pos + 1);
30
31     try
32     {
33         return std::stoi(param);
34     }
35     catch(std::logic_error &e)
36     {
37         std::cout << executable << ": invalid argument '"<< param << "' for '" << option  << "'" << std::endl;
38         std::cout << "Try '" << executable << " --usage' for more information." << std::endl;
39         exit(-1);
40     }
41 }
42
43 xranLibWraper *xranlib;
44
45 int main(int argc, char** argv) {
46     int all_test_ret = 0;
47
48     /* Enable xml output by default */
49     ::testing::GTEST_FLAG(output) = "xml:test_results.xml";
50
51     ::testing::InitGoogleTest(&argc, argv);
52
53     const std::string executable = argv[0];
54
55     for(int index = 1; index < argc; index++) {
56
57         const std::string option = argv[index];
58
59         if (option.find("--nb_repetitions=") != std::string::npos)
60         {
61             BenchmarkParameters::repetition = parse_input_parameter(executable, option);
62         }
63         else if (option.find("--nb_loops=") != std::string::npos)
64         {
65             BenchmarkParameters::loop = parse_input_parameter(executable, option);
66         }
67         else if (option.find("--cpu_nb=") != std::string::npos)
68         {
69             BenchmarkParameters::cpu_id = (unsigned) parse_input_parameter(executable, option);
70
71             if (BenchmarkParameters::cpu_id == 0)
72                 std::cout << std::endl << "Warning: Core #0 is running the VM's OS. "
73                           << "Measurements won't be accurate. It shouldn't be used!"
74                           << std::endl << std::endl;
75         }
76         /* --usage used instead of --help to avoid conflict with gtest --help */
77         else if (!option.compare("--usage"))
78         {
79             std::cout << "Usage: " << executable << " [GTEST_OPTION]... [OPTION]..." << std::endl;
80             std::cout << "Runs unittests with given gtest options." << std::endl;
81             std::cout << std::endl;
82             std::cout << "Available options: " << std::endl;
83             std::cout << "--nb_repetitions=NUMBER Sets how many times results are measured" << std::endl;
84             std::cout << "--nb_loops=NUMBER Sets how many times function is called per repetition"
85                       << std::endl;
86             std::cout << "--cpu_nb=NUMBER Sets core number to run tests on" << std::endl;
87             std::cout << "--usage Prints this message" << std::endl;
88
89             return 0;
90         }
91         else
92         {
93             std::cout << executable << ": inavlid option " << option << std::endl;
94             std::cout << "Try '" << executable << " --usage' for more information." << std::endl;
95
96             return -1;
97         }
98     }
99
100     xranlib = new xranLibWraper;
101
102     if(xranlib->SetUp() < 0) {
103         return (-1);
104         }
105
106     all_test_ret = RUN_ALL_TESTS();
107
108     xranlib->TearDown();
109
110     if(xranlib != nullptr) {
111         delete xranlib;
112         xranlib == nullptr;
113         }
114
115     return all_test_ret;
116 }
117
118
119