1 /*
2 * Copyright 2015 Rockchip Electronics Co. LTD
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 #define MODULE_TAG "mpp_trie_test"
18
19 #include <string.h>
20
21 #include "mpp_log.h"
22 #include "mpp_mem.h"
23 #include "mpp_time.h"
24 #include "mpp_common.h"
25
26 #include "mpp_trie.h"
27
28 typedef void *(*TestProc)(void *, RK_S64 time);
29
30 typedef struct TestAction_t {
31 const char *name;
32 void *ctx;
33 TestProc proc;
34 } TestAction;
35
36 typedef struct TestCase_t {
37 const char *name;
38 MPP_RET ret;
39 } TestCase;
40
print_opt(void * ctx,RK_S64 time)41 void *print_opt(void *ctx, RK_S64 time)
42 {
43 RK_U8 **str = (RK_U8 **)ctx;
44
45 if (str && *str)
46 mpp_log("get option %-16s time %lld us\n", *str, time);
47
48 return NULL;
49 }
50
51 TestAction test_info[] = {
52 { "rc:mode", &test_info[0], print_opt},
53 { "rc:bps_target", &test_info[1], print_opt},
54 { "rc:bps_max", &test_info[2], print_opt},
55 { "rc:bps_min", &test_info[3], print_opt},
56 /* test valid info end in the middle */
57 { "rc:bps", &test_info[4], print_opt},
58 };
59
60 TestCase test_case[] = {
61 { "rc:mode", MPP_OK, },
62 { "rc:bps_target", MPP_OK, },
63 { "rc:bps_max", MPP_OK, },
64 { "rc:bps", MPP_OK, },
65 { "this is an error string", MPP_NOK, },
66 { "", MPP_NOK, },
67 };
68
main()69 int main()
70 {
71 MppTrie trie = NULL;
72 RK_S32 i;
73 RK_S64 end = 0;
74 RK_S64 start = 0;
75 RK_S32 info_cnt = MPP_ARRAY_ELEMS(test_info);
76 RK_S32 ret = MPP_OK;
77
78 mpp_log("mpp_trie_test start\n");
79
80 mpp_trie_init(&trie, "test_trie");
81
82 start = mpp_time();
83 for (i = 0; i < info_cnt; i++)
84 mpp_trie_add_info(trie, test_info[i].name, &test_info[i], sizeof(test_info[i]));
85 end = mpp_time();
86 mpp_log("add act time %lld us\n", end - start);
87
88 ret = mpp_trie_add_info(trie, NULL, NULL, 0);
89 if (ret) {
90 mpp_loge("mpp_trie_add_info last failed\n");
91 goto DONE;
92 }
93
94 for (i = 0; i < (RK_S32)MPP_ARRAY_ELEMS(test_case); i++) {
95 MppTrieInfo *info = NULL;
96 TestAction *act = NULL;
97 const char *name = test_case[i].name;
98
99 start = mpp_time();
100 info = mpp_trie_get_info(trie, name);
101 end = mpp_time();
102
103 if (info)
104 act = (TestAction *)mpp_trie_info_ctx(info);
105
106 if (act && act->proc)
107 act->proc(act->ctx, end - start);
108 else
109 mpp_loge("search %s failed time %lld us\n", name, end - start);
110
111 ret |= ((info && !test_case[i].ret) ||
112 (!info && test_case[i].ret)) ? MPP_OK : MPP_NOK;
113 }
114
115 mpp_trie_deinit(trie);
116
117 DONE:
118 mpp_log("mpp_trie_test ret %s\n", ret ? "failed" : "success");
119
120 return ret;
121 }
122