xref: /aosp_15_r20/external/libavc/tests/TestArgs.h (revision 495ae853bb871d1e5a258cb02c2cc13cde8ddb9a)
1 /*
2  * Copyright (C) 2021 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef __AVC_ENCODER_TEST_ENVIRONMENT_H__
18 #define __AVC_ENCODER_TEST_ENVIRONMENT_H__
19 
20 #include <gtest/gtest.h>
21 #include <getopt.h>
22 
23 using namespace std;
24 
25 class TestArgs : public::testing::Environment {
26   public:
TestArgs()27     TestArgs() : res("/data/local/tmp/AvcEncTestRes/") {}
28 
29     // Parses the command line arguments
30     int initFromOptions(int argc, char **argv);
31 
setRes(const char * _res)32     void setRes(const char *_res) { res = _res; }
33 
getRes()34     const string getRes() const { return res; }
35 
36   private:
37     string res;
38 };
39 
initFromOptions(int argc,char ** argv)40 int TestArgs::initFromOptions(int argc, char **argv) {
41     static struct option options[] = {{"path", required_argument, 0, 'P'}, {0, 0, 0, 0}};
42 
43     while (true) {
44         int index = 0;
45         int c = getopt_long(argc, argv, "P:", options, &index);
46         if (c == -1) {
47             break;
48         }
49 
50         switch (c) {
51             case 'P': {
52                 setRes(optarg);
53                 break;
54             }
55             default:
56                 break;
57         }
58     }
59 
60     if (optind < argc) {
61         fprintf(stderr,
62                 "unrecognized option: %s\n\n"
63                 "usage: %s <gtest options> <test options>\n\n"
64                 "test options are:\n\n"
65                 "-P, --path: Resource files directory location\n",
66                 argv[optind ?: 1], argv[0]);
67         return 2;
68     }
69     return 0;
70 }
71 
72 #endif  // __AVC_ENCODER_TEST_ENVIRONMENT_H__
73