blob: 78dfd7fbf7c586375f2798204d04b85241da6a37 [file] [log] [blame]
xjb04a4022021-11-25 15:01:52 +08001#include "AudioParamParserPriv.h"
2
3typedef APP_STATUS(*TEST_FUN)(AppHandle *appHandle);
4
5typedef struct
6{
7 AppHandle *appHandle;
8 int times;
9 TEST_FUN fun;
10} ThreadParam;
11
12EXPORT APP_STATUS unitTest(AppHandle *appHandle)
13{
14 APP_STATUS res = APP_NO_ERROR;
15 APP_STATUS finalRes = APP_NO_ERROR;
16
17 printf("===APP internal unit test===\n");
18#if 0
19 res = testAppHandleInitUninit();
20 if (res == APP_ERROR)
21 {
22 finalRes = APP_ERROR;
23 }
24 printf("testAppHandleInitUninit: %s\n", res ? "pass" : "fail");
25#endif
26 res = testReadWriteParam(appHandle);
27 if (res == APP_ERROR)
28 {
29 finalRes = APP_ERROR;
30 }
31 printf("testReadWriteParam: %s\n", res ? "pass" : "fail");
32
33 res = testMemoryLeak(appHandle);
34 if (res == APP_ERROR)
35 {
36 finalRes = APP_ERROR;
37 }
38 printf("testMemoryLeak: %s\n", res ? "pass" : "fail");
39
40 res = testAudioTypeLock(appHandle);
41 if (res == APP_ERROR)
42 {
43 finalRes = APP_ERROR;
44 }
45 printf("testAudioTypeLock: %s\n", res ? "pass" : "fail");
46 printf("=============================\n");
47
48 return finalRes;
49}
50
51
52EXPORT void *commonThreadLoop(void *arg)
53{
54 ThreadParam threadParam = *(ThreadParam *)arg;
55 int i = 0 ;
56 for (i = 0; i < threadParam.times; i++)
57 {
58 (*threadParam.fun)(threadParam.appHandle);
59 INFO_LOG("2nd thread round = %d\n", i);
60 }
61 return NULL;
62}
63
64EXPORT void testDebugLevel()
65{
66 appSetDebugLevel(ERR_LEVEL);
67 ERR_LOG("error - pass\n");
68 WARN_LOG("warn - ok\n");
69 INFO_LOG("info - ok\n");
70 DEBUG_LOG("debug - ok\n");
71
72 appSetDebugLevel(WARN_LEVEL);
73 ERR_LOG("error - fail\n");
74 WARN_LOG("warn - pass\n");
75 INFO_LOG("info - ok\n");
76 DEBUG_LOG("debug - ok\n");
77
78 appSetDebugLevel(INFO_LEVEL);
79 ERR_LOG("error - fail\n");
80 WARN_LOG("warn - fail\n");
81 INFO_LOG("info - pass\n");
82 DEBUG_LOG("debug - ok\n");
83
84 appSetDebugLevel(DEBUG_LEVEL);
85 ERR_LOG("error - fail\n");
86 WARN_LOG("warn - fail\n");
87 INFO_LOG("info - fail\n");
88 DEBUG_LOG("debug - pass\n");
89}
90
91EXPORT void testHashParamTree()
92{
93 ParamTree *item;
94 ParamTree *ParamTreeHash = NULL, *tmp = NULL; /* Used for hash */
95 const char *key;
96
97 item = (ParamTree *)malloc(sizeof(ParamTree));
98 item->categoryPath = "NB,Normal,,";
99 item->paramId = 1;
100 HASH_ADD_KEYPTR(hh, ParamTreeHash, item->categoryPath, strlen(item->categoryPath), item);
101
102 item = (ParamTree *)malloc(sizeof(ParamTree));
103 item->categoryPath = "WB,Normal,,";
104 item->paramId = 7;
105 HASH_ADD_KEYPTR(hh, ParamTreeHash, item->categoryPath, strlen(item->categoryPath), item);
106
107 item = (ParamTree *)malloc(sizeof(ParamTree));
108 item->categoryPath = "NB,Normal,0,GSM,";
109 item->paramId = 1;
110 HASH_ADD_KEYPTR(hh, ParamTreeHash, item->categoryPath, strlen(item->categoryPath), item);
111
112 item = (ParamTree *)malloc(sizeof(ParamTree));
113 item->categoryPath = "NB,HAC,0,GSM";
114 item->paramId = 0;
115 HASH_ADD_KEYPTR(hh, ParamTreeHash, item->categoryPath, strlen(item->categoryPath), item);
116
117 /* Find string */
118 key = "WB,,,";
119 HASH_FIND_STR(ParamTreeHash, key, item);
120 if (item) { printf("[%s] id is %d\n", key, item->paramId); }
121
122 /* Free hash table content */
123 HASH_ITER(hh, ParamTreeHash, item, tmp)
124 {
125 HASH_DEL(ParamTreeHash, item);
126 free(item);
127 }
128}
129
130EXPORT void testHashParamUnit()
131{
132 ParamUnit *item;
133 ParamUnit *ParamUnitHash = NULL, *tmp = NULL; /* Used for hash */
134 int key;
135
136 item = (ParamUnit *)malloc(sizeof(ParamUnit));
137 item->paramId = 0;
138 item->paramHash = (Param *) 0x1;
139 HASH_ADD_INT(ParamUnitHash, paramId, item);
140
141 item = (ParamUnit *)malloc(sizeof(ParamUnit));
142 item->paramId = 1;
143 item->paramHash = (Param *)0x2;
144 HASH_ADD_INT(ParamUnitHash, paramId, item);
145 item = (ParamUnit *)malloc(sizeof(ParamUnit));
146
147 item->paramId = 7;
148 item->paramHash = (Param *)0x3;
149 HASH_ADD_INT(ParamUnitHash, paramId, item);
150
151 /* Find string */
152 key = 0;
153 HASH_FIND_INT(ParamUnitHash, &key, item);
154 if (item) { INFO_LOG("[%d] Param is %p\n", key, item->paramHash); }
155
156 key = 1;
157 HASH_FIND_INT(ParamUnitHash, &key, item);
158 if (item) { INFO_LOG("[%d] Param is %p\n", key, item->paramHash); }
159
160 key = 7;
161 HASH_FIND_INT(ParamUnitHash, &key, item);
162 if (item) { INFO_LOG("[%d] Param is %p\n", key, item->paramHash); }
163
164 /* Free hash table content */
165 HASH_ITER(hh, ParamUnitHash, item, tmp)
166 {
167 HASH_DEL(ParamUnitHash, item);
168 free(item);
169 }
170}
171
172EXPORT void testHashParam()
173{
174 Param *item;
175 Param *paramHash = NULL, *tmp = NULL; /* Used for hash */
176 const char *key;
177
178 item = (Param *)malloc(sizeof(Param));
179 memset(item, 0, sizeof(Param));
180 item->name = "speech_mode_para";
181 item->data = "0x0011,0x2233,0x4455";
182 HASH_ADD_KEYPTR(hh, paramHash, item->name, strlen(item->name), item);
183
184 item = (Param *)malloc(sizeof(Param));
185 memset(item, 0, sizeof(Param));
186 item->name = "uint_param";
187 item->data = "4294967295";
188 HASH_ADD_KEYPTR(hh, paramHash, item->name, strlen(item->name), item);
189
190 item = (Param *)malloc(sizeof(Param));
191 memset(item, 0, sizeof(Param));
192 item->name = "float_param";
193 item->data = "0.1234567";
194 HASH_ADD_KEYPTR(hh, paramHash, item->name, strlen(item->name), item);
195
196 /* Find string */
197 key = "speech_mode_para";
198 HASH_FIND_STR(paramHash, key, item);
199 if (item) { INFO_LOG("[%s] value is %s\n", key, (char *)item->data); }
200
201 key = "uint_param";
202 HASH_FIND_STR(paramHash, key, item);
203 if (item) { INFO_LOG("[%s] value is %s\n", key, (char *)item->data); }
204
205 key = "float_param";
206 HASH_FIND_STR(paramHash, key, item);
207 if (item) { INFO_LOG("[%s] value is %s\n", key, (char *)item->data); }
208
209 /* Free hash table content */
210 HASH_ITER(hh, paramHash, item, tmp)
211 {
212 HASH_DEL(paramHash, item);
213 free(item);
214 }
215}
216
217void testCb(AppHandle *appHandle, const char *audioTypeName)
218{
219 printf("XML file changed. (cus folder = %s, audioType = %s)\n", appHandle->xmlCusDir, audioTypeName);
220}
221
222EXPORT void notifyCbTest(AppHandle *appHandle)
223{
224 NotifyCb *cb;
225
226 appHandleRegXmlChangedCb(appHandle, testCb);
227
228 LL_FOREACH(appHandle->noficyCbList, cb)
229 {
230 (*cb->cb)(appHandle, "OK");
231 }
232
233 appHandleUnregXmlChangedCb(appHandle, testCb);
234
235 LL_FOREACH(appHandle->noficyCbList, cb)
236 {
237 (*cb->cb)(appHandle, "FAIL");
238 }
239}
240
241EXPORT void inotifyTest(const char *path)
242{
243
244#ifndef WIN32
245#define INOTIFY_BUF_SIZE 512
246 /* inotify test */
247 int wd;
248 size_t len;
249 char buf[INOTIFY_BUF_SIZE];
250 char *ptr;
251 const struct inotify_event *event;
252
253 int fd = inotify_init();
254 if (fd < 0)
255 {
256 printf("inotify_init failed !!!");
257 exit(1);
258 }
259
260 printf("inotify path = %s\n", path);
261 wd = inotify_add_watch(fd, path, IN_CLOSE_WRITE);
262 if (wd < 0)
263 {
264 printf("inotify_add_watch failed !!!");
265 exit(1);
266 }
267
268 while (1)
269 {
270 len = read(fd, buf, sizeof(buf));
271 if (len < 0)
272 {
273 perror("read");
274 exit(EXIT_FAILURE);
275 }
276
277 /* Loop over all events in the buffer */
278 for (ptr = buf; ptr < buf + len; ptr += sizeof(struct inotify_event) + event->len)
279 {
280 event = (const struct inotify_event *) ptr;
281
282 /* Print event type */
283 if (event->mask & IN_OPEN)
284 {
285 printf("IN_OPEN: ");
286 }
287 if (event->mask & IN_CLOSE_NOWRITE)
288 {
289 printf("IN_CLOSE_NOWRITE: ");
290 }
291 if (event->mask & IN_CLOSE_WRITE)
292 {
293 printf("IN_CLOSE_WRITE: ");
294 }
295 if (event->mask & IN_ACCESS)
296 {
297 printf("IN_ACCESS: ");
298 }
299
300 /* Print the name of the file */
301
302 if (event->len)
303 {
304 printf("%s", event->name);
305 }
306
307 /* Print type of filesystem object */
308
309 if (event->mask & IN_ISDIR)
310 {
311 printf(" [directory]\n");
312 }
313 else
314 {
315 printf(" [file]\n");
316 }
317 }
318 }
319 inotify_rm_watch(fd, IN_CLOSE_NOWRITE);
320#endif
321}
322
323/***********************************
324 * Test Steps:
325 * 1. Create thread to read/write param
326 * 2. Check the lock is work well
327 * Crash/deadlock checking
328 **********************************/
329EXPORT APP_STATUS testAudioTypeLockFun(AppHandle *appHandle)
330{
331 /* Read param */
332 char *audioTypeName = "Speech";
333 char *categoryPath = "Band,NB,Profile,4_pole_Headset,VolIndex,3";
334 char *paramName = "speech_mode_para";
335 unsigned short shortArray1[] = {0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7};
336 unsigned short shortArray2[] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77};
337 int arraySize = 7;
338 AudioType *audioType;
339 ParamUnit *paramUnit;
340 ParamInfo *paramInfo;
341 Param *param;
342
343 appHandle = appHandleGetInstance();
344
345 /* Query AudioType */
346 audioType = appHandleGetAudioTypeByName(appHandle, audioTypeName);
347 if (!audioType)
348 {
349 ERR_LOG("audioType is NULL\n");
350 return APP_ERROR;
351 }
352
353 /* Read Lock */
354 audioTypeReadLock(audioType, __FUNCTION__);
355
356 /* Query the ParamUnit */
357 paramUnit = audioTypeGetParamUnit(audioType, categoryPath);
358 if (!paramUnit)
359 {
360 ERR_LOG("paramUnit is NULL\n");
361 return APP_ERROR;
362 }
363
364 /* Query the param value */
365 param = paramUnitGetParamByName(paramUnit, paramName);
366 if (!param)
367 {
368 ERR_LOG("Error: Cannot query param value!\n");
369 return APP_ERROR;
370 }
371
372 /* Read unlock */
373 audioTypeUnlock(audioType);
374
375 utilUsleep(1); // delay time make cpu scheduling to other thread
376
377 /* Write param */
378 paramInfo = audioTypeGetParamInfoByName(audioType, paramName);
379 if (audioTypeSetParamData(audioType, categoryPath, paramInfo, (void *)shortArray1, arraySize) == APP_ERROR)
380 {
381 ERR_LOG("Cannot update the param data!!\n");
382 return APP_ERROR;
383 }
384
385 utilUsleep(1); // delay time make cpu scheduling to other thread
386
387 if (audioTypeSetParamData(audioType, categoryPath, paramInfo, (void *)shortArray2, arraySize) == APP_ERROR)
388 {
389 ERR_LOG("Cannot update the param data!!\n");
390 return APP_ERROR;
391 }
392
393 utilUsleep(1); // delay time make cpu scheduling to other thread
394
395 /* Save XML */
396 audioTypeSaveAudioParamXml(audioType, XML_CUS_FOLDER_ON_DEVICE, 1);
397
398 utilUsleep(1); // delay time make cpu scheduling to other thread
399
400 /* Reload XML */
401 appHandleReloadAudioType(appHandle, audioType->name);
402
403 return APP_NO_ERROR;
404}
405
406EXPORT APP_STATUS testAudioTypeLock(AppHandle *appHandle)
407{
408#ifndef WIN32
409 int i;
410 pthread_t appThread;
411 void *status;
412 ThreadParam threadParam;
413
414 threadParam.times = 50;
415 threadParam.appHandle = appHandle;
416 threadParam.fun = testAudioTypeLockFun;
417
418 if (pthread_create(&appThread, NULL, commonThreadLoop, &threadParam))
419 {
420 ERR_LOG("Create app thread fail!\n");
421 return APP_ERROR;
422 }
423
424 for (i = 0; i < threadParam.times; i++)
425 {
426 (*threadParam.fun)(appHandle);
427 INFO_LOG("Main thread test round = %d\n", i);
428 }
429
430 /* Waiting 2nd thread join */
431 pthread_join(appThread, &status);
432#else
433 INFO_LOG("Not test this UT on windows\n");
434#endif
435 return APP_NO_ERROR;
436}
437
438EXPORT APP_STATUS testAppHandleInitUninit()
439{
440 int times = 10;
441 int i;
442 for (i = 0; i < times; i++)
443 {
444 AppHandle testAppHandle;
445 appHandleInit(&testAppHandle);
446#ifdef WIN32
447 appHandleParseXml(&testAppHandle, XML_FOLDER_ON_TUNING_TOOL, XML_CUS_FOLDER_ON_TUNING_TOOL);
448#else
449 appHandleParseXml(&testAppHandle, XML_FOLDER_ON_DEVICE, XML_CUS_FOLDER_ON_DEVICE);
450#endif
451 appHandleUninit(&testAppHandle);
452 }
453 return APP_NO_ERROR;
454}
455
456/***********************************
457 * Test Steps:
458 * 1. Reload audio type xml 100 times
459 * Memory leak / crash checking
460 **********************************/
461EXPORT APP_STATUS testMemoryLeak(AppHandle *appHandle)
462{
463 int i = 0;
464 for (i = 0; i < 100; i++)
465 {
466 /* stress test query / release / create */
467 AudioType *audioType = appHandleGetAudioTypeByName(appHandle, "Speech");
468 audioType->allowReload = 1;
469 if (appHandleReloadAudioType(appHandle, "Speech") == APP_ERROR)
470 {
471 return APP_ERROR;
472 }
473 }
474
475 printf("Checking memory status and press enter key to continue\n");
476 getchar();
477 return APP_NO_ERROR;
478}
479
480/***********************************
481 * Test Steps:
482 * 1. Read param array
483 * 2. Update param array one item with 32767
484 * 3. Repeat array size times
485 * 4. Check the result
486 **********************************/
487APP_STATUS testReadWriteParam(AppHandle *appHandle)
488{
489 size_t i, j;
490 ParamUnit *paramUnit;
491 ParamInfo *paramInfo;
492 Param *param;
493 unsigned short *shortArray;
494 size_t arraySize = 1; // The size will update by real array size latter
495
496 const char *audioTypeName = "Speech";
497 const char *paraName = "speech_mode_para";
498 const char *categoryPath = "Band,NB,Profile,Normal,VolIndex,2,Network,GSM";
499
500 AudioType *audioType = appHandleGetAudioTypeByName(appHandle, audioTypeName);
501
502 /* Test steps */
503 for (j = 0; j < arraySize; j++)
504 {
505 paramUnit = audioTypeGetParamUnit(audioType, categoryPath);
506 if (!paramUnit)
507 {
508 ERR_LOG("Cannot find paramUnit\n");
509 return APP_ERROR;
510 }
511
512 param = paramUnitGetParamByName(paramUnit, paraName);
513 if (!param)
514 {
515 ERR_LOG("Cannot query param value!\n");
516 return APP_ERROR;
517 }
518
519 shortArray = (unsigned short *)param->data;
520 arraySize = param->arraySize;
521 /*for(i = 0; i < param->arraySize; i++)
522 {
523 printf("[%d]0x%x\n", i, ((unsigned short*)param->data)[i])
524 }*/
525
526 shortArray[j] = 32767;
527
528 /* You should cache follow object in somewhere without query again */
529 paramInfo = audioTypeGetParamInfoByName(audioType, paraName);
530
531 /* The sph_in_fir param is short array type */
532 if (audioTypeSetParamData(audioType, categoryPath, paramInfo, (void *)shortArray, param->arraySize) == APP_ERROR)
533 {
534 return APP_ERROR;
535 }
536 }
537
538 /* Result check */
539 paramUnit = audioTypeGetParamUnit(audioType, categoryPath);
540 if (!paramUnit)
541 {
542 ERR_LOG("Cannot find paramUnit\n");
543 return APP_ERROR;
544 }
545
546 param = paramUnitGetParamByName(paramUnit, paraName);
547 if (!param)
548 {
549 ERR_LOG("Cannot query param value!\n");
550 return APP_ERROR;
551 }
552
553 shortArray = (unsigned short *)param->data;
554 for (i = 0; i < param->arraySize; i++)
555 {
556 if (shortArray[i] != 32767)
557 {
558 ERR_LOG("Verify short array[%lu] = %d != 32767\n", i, shortArray[i]);
559 return APP_ERROR;
560 }
561 }
562
563 return APP_NO_ERROR;
564}