aboutsummaryrefslogtreecommitdiffstats
path: root/tests/test_format_api.c
blob: 51a6bc51177f922bf9fb65ee059d3430cfe3f503 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
/*
 * Asterisk -- An open source telephony toolkit.
 *
 * Copyright (C) 2010, Digium, Inc.
 *
 * David Vossel <dvossel@digium.com>
 *
 * See http://www.asterisk.org for more information about
 * the Asterisk project. Please do not directly contact
 * any of the maintainers of this project for assistance;
 * the project provides a web site, mailing lists and IRC
 * channels for your use.
 *
 * This program is free software, distributed under the terms of
 * the GNU General Public License Version 2. See the LICENSE file
 * at the top of the source tree.
 */

/*!
 * \file
 * \brief Tests for the ast_event API
 *
 * \author David Vossel <dvossel@digium.com>
 *
 * \ingroup tests
 *
 */

/*** MODULEINFO
	<depend>TEST_FRAMEWORK</depend>
 ***/

#include "asterisk.h"

ASTERISK_FILE_VERSION(__FILE__, "$Revision$")

#include "asterisk/module.h"
#include "asterisk/test.h"
#include "asterisk/format.h"
#include "asterisk/format_cap.h"
#include "asterisk/strings.h"

/*! These are the keys for accessing attributes */
enum test_attr_keys {
	TEST_ATTR_KEY_SAMP_RATE,
	TEST_ATTR_KEY_STRING,
};

/*! These are the values for the TEST_ATTR_KEY_SAMP_RATE key */
enum test_attr_vals_samp {
	TEST_ATTR_VAL_SAMP_8KHZ  = (1 << 0),
	TEST_ATTR_VAL_SAMP_12KHZ = (1 << 1),
	TEST_ATTR_VAL_SAMP_16KHZ = (1 << 2),
	TEST_ATTR_VAL_SAMP_32KHZ = (1 << 3),
	TEST_ATTR_VAL_SAMP_48KHZ = (1 << 4),
};

/*! This is the attribute structure used for our test interface. */
struct test_attr {
	enum test_attr_vals_samp samp_flags;
	char string[32];
};

static enum ast_format_cmp_res test_cmp(const struct ast_format_attr *fattr1, const struct ast_format_attr *fattr2)
{
	struct test_attr *attr1 = (struct test_attr *) fattr1;
	struct test_attr *attr2 = (struct test_attr *) fattr2;

	if ((attr1->samp_flags == attr2->samp_flags) &&
		!(strcmp(attr1->string, attr2->string))) {
		return AST_FORMAT_CMP_EQUAL;
	}
	if ((attr1->samp_flags != (attr1->samp_flags & attr2->samp_flags)) ||
		(!ast_strlen_zero(attr1->string) && strcmp(attr1->string, attr2->string))) {
		return AST_FORMAT_CMP_NOT_EQUAL;
	}
	return AST_FORMAT_CMP_SUBSET;
}

static int test_getjoint(const struct ast_format_attr *fattr1, const struct ast_format_attr *fattr2, struct ast_format_attr *result)
{
	struct test_attr *attr1 = (struct test_attr *) fattr1;
	struct test_attr *attr2 = (struct test_attr *) fattr2;
	struct test_attr *attr_res = (struct test_attr *) result;
	int joint = -1;

	attr_res->samp_flags = (attr1->samp_flags & attr2->samp_flags);

	if (attr_res->samp_flags) {
		joint = 0;
	}

	if (!strcmp(attr1->string, attr2->string)) {
		ast_copy_string(attr_res->string, attr1->string, sizeof(attr_res->string));
		joint = 0;
	}

	return joint;
}

static void test_set(struct ast_format_attr *fattr, va_list ap)
{
	enum test_attr_keys key;
	struct test_attr *attr = (struct test_attr *) fattr;
	char *string;

	for (key = va_arg(ap, int);
		key != AST_FORMAT_ATTR_END;
		key = va_arg(ap, int))
	{
		switch (key) {
		case TEST_ATTR_KEY_SAMP_RATE:
			attr->samp_flags = (va_arg(ap, int) | attr->samp_flags);
			break;
		case TEST_ATTR_KEY_STRING:
			string = va_arg(ap, char *);
			if (!ast_strlen_zero(string)) {
				ast_copy_string(attr->string, string, sizeof(attr->string));
			}
			break;
		default:
			ast_log(LOG_WARNING, "unknown attribute type %d\n", key);
		}
	}
}

/*! uLaw does not actually have any attributes associated with it.
 * This is just for the purpose of testing. We are guaranteed there
 * will never exist a interface for uLaw already. */
static struct ast_format_attr_interface test_interface = {
	.id = AST_FORMAT_TESTLAW,
	.format_attr_cmp = test_cmp,
	.format_attr_get_joint = test_getjoint,
	.format_attr_set = test_set
};

/*!
 * \internal
 */
AST_TEST_DEFINE(format_test1)
{
	struct ast_format format1 = { 0, };
	struct ast_format format2 = { 0, };
	struct ast_format joint = { 0, };

	switch (cmd) {
	case TEST_INIT:
		info->name = "ast_format_test1";
		info->category = "/main/format/";
		info->summary = "Test ast_format with attributes.";
		info->description =
			"This test exercises the Ast Format API by creating and registering "
			"a custom ast_format_attr_interface and performing various function "
			"calls on ast_formats using the interface. ";
		return AST_TEST_NOT_RUN;
	case TEST_EXECUTE:
		break;
	}

	if (ast_format_attr_reg_interface(&test_interface)) {
		ast_test_status_update(test, "test_interface failed to register.\n");
		return AST_TEST_FAIL;
	}

	/* set a format with a single attribute. */
	ast_format_set(&format1, AST_FORMAT_TESTLAW, 1,
		TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_8KHZ,
		AST_FORMAT_ATTR_END);
	if (ast_format_isset(&format1, TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_8KHZ, AST_FORMAT_ATTR_END)) {
		ast_test_status_update(test, "format1 did not set number attribute correctly.\n");
		return AST_TEST_FAIL;
	}
	if (!ast_format_isset(&format1, TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_12KHZ, AST_FORMAT_ATTR_END)) {
		ast_test_status_update(test, "format1 did not determine isset on number correctly. \n");
		return AST_TEST_FAIL;
	}

	/* append the string attribute to a format with previous attributes already set */
	ast_format_append(&format1,
		TEST_ATTR_KEY_STRING,"String",
		AST_FORMAT_ATTR_END);
	if (ast_format_isset(&format1, TEST_ATTR_KEY_STRING, "String", AST_FORMAT_ATTR_END)) {
		ast_test_status_update(test, "format1 did not set string attribute correctly.\n");
		return AST_TEST_FAIL;
	}
	if (!ast_format_isset(&format1, TEST_ATTR_KEY_STRING, "Not a string", AST_FORMAT_ATTR_END)) {
		ast_test_status_update(test, "format1 did not determine isset on string correctly. \n");
		return AST_TEST_FAIL;
	}

	/* set format2 with both STRING and NUMBER at the same time */
	ast_format_set(&format2, AST_FORMAT_TESTLAW, 1,
		TEST_ATTR_KEY_STRING, "MOOOoo",
		TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_8KHZ,
		TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_16KHZ,
		AST_FORMAT_ATTR_END);
	/* perform isset with multiple key value pairs. */

	if (ast_format_isset(&format2,
			TEST_ATTR_KEY_STRING, "MOOOoo",
			TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_16KHZ,
			AST_FORMAT_ATTR_END)) {

		ast_test_status_update(test, "format2 did not set attributes correctly.\n");
		return AST_TEST_FAIL;
	}
	if (!ast_format_isset(&format2,
			TEST_ATTR_KEY_STRING, "WRONG",
			TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_8KHZ,
			TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_16KHZ,
			AST_FORMAT_ATTR_END)) {

		ast_test_status_update(test, "format2 did not deterine isset correctly.\n");
		return AST_TEST_FAIL;
	}

	/* get joint attributes between format1 and format2. */
	if (ast_format_joint(&format1, &format2, &joint)) {
		ast_test_status_update(test, "failed to get joint attributes.\n");
		return AST_TEST_FAIL;
	}
	if (ast_format_isset(&joint, TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_8KHZ, AST_FORMAT_ATTR_END)) {
		ast_test_status_update(test, "joint attribute was not what we expected.\n");
		return AST_TEST_FAIL;
	}

	/* exercise compare functions */
	if (ast_format_cmp(&format1, &format2) != AST_FORMAT_CMP_NOT_EQUAL) {
		ast_test_status_update(test, "cmp 1 failed.\n");
		return AST_TEST_FAIL;
	}
	if (ast_format_cmp(&format1, &format1) != AST_FORMAT_CMP_EQUAL) {
		ast_test_status_update(test, "cmp 2 failed.\n");
		return AST_TEST_FAIL;
	}
	if (ast_format_cmp(&joint, &format1) != AST_FORMAT_CMP_SUBSET) {
		ast_test_status_update(test, "cmp 3 failed.\n");
		return AST_TEST_FAIL;
	}

	/* unregister interface */
	if (ast_format_attr_unreg_interface(&test_interface)) {
		ast_test_status_update(test, "test_interface failed to unregister.\n");
		return AST_TEST_FAIL;
	}

	return AST_TEST_PASS;
}

/*!
 * \internal
 */
AST_TEST_DEFINE(format_test2)
{
	struct ast_format format = { 0, };

	switch (cmd) {
	case TEST_INIT:
		info->name = "ast_format_test2";
		info->category = "/main/format/";
		info->summary = "Test ast_format unique id and category system";
		info->description =
			"This test exercises the Ast Format unique id and category "
			"system by creating formats of various types and verifying "
			"their category matches what we expect.";
		return AST_TEST_NOT_RUN;
	case TEST_EXECUTE:
		break;
	}

	ast_format_set(&format, AST_FORMAT_ULAW, 0);
	if (AST_FORMAT_GET_TYPE(format.id) != AST_FORMAT_TYPE_AUDIO) {
		ast_test_status_update(test, "audio type failed\n");
		return AST_TEST_FAIL;
	}

	ast_format_set(&format, AST_FORMAT_H264, 0);
	if (AST_FORMAT_GET_TYPE(format.id) != AST_FORMAT_TYPE_VIDEO) {
		ast_test_status_update(test, "video type failed\n");
		return AST_TEST_FAIL;
	}

	ast_format_set(&format, AST_FORMAT_JPEG, 0);
	if (AST_FORMAT_GET_TYPE(format.id) != AST_FORMAT_TYPE_IMAGE) {
		ast_test_status_update(test, "image type failed\n");
		return AST_TEST_FAIL;
	}

	ast_format_set(&format, AST_FORMAT_T140, 0);
	if (AST_FORMAT_GET_TYPE(format.id) != AST_FORMAT_TYPE_TEXT) {
		ast_test_status_update(test, "text type failed\n");
		return AST_TEST_FAIL;
	}

	return AST_TEST_PASS;
}

static int container_test1_helper(struct ast_format_cap *cap1, struct ast_format_cap *cap2, struct ast_test *test)
{

	int res = AST_TEST_PASS;
	struct ast_format_cap *cap_joint = NULL;
	struct ast_format tmpformat;

	if (ast_format_attr_reg_interface(&test_interface)) {
		ast_test_status_update(test, "test_interface failed to register.\n");
		ast_format_cap_destroy(cap1);
		ast_format_cap_destroy(cap2);
		return AST_TEST_FAIL;
	}

	ast_format_cap_add(cap1, ast_format_set(&tmpformat, AST_FORMAT_GSM, 0));
	ast_format_cap_add(cap1, ast_format_set(&tmpformat, AST_FORMAT_ULAW, 0));
	ast_format_cap_add(cap1, ast_format_set(&tmpformat, AST_FORMAT_G722, 0));
	ast_format_cap_add(cap1, ast_format_set(&tmpformat, AST_FORMAT_ALAW, 0));
	ast_format_cap_add(cap1, ast_format_set(&tmpformat, AST_FORMAT_H264, 0));
	ast_format_cap_add(cap1, ast_format_set(&tmpformat, AST_FORMAT_H263, 0));
	ast_format_cap_add(cap1, ast_format_set(&tmpformat, AST_FORMAT_T140, 0));
	ast_format_cap_add(cap1, ast_format_set(&tmpformat, AST_FORMAT_JPEG, 0));
	ast_format_cap_add(cap1, ast_format_set(&tmpformat, AST_FORMAT_TESTLAW, 1,
			TEST_ATTR_KEY_STRING, "testing caps hooray",
			TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_8KHZ,
			TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_16KHZ,
			TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_32KHZ,
			AST_FORMAT_ATTR_END));

	/* Test is compatible */
	if (!ast_format_cap_iscompatible(cap1, ast_format_set(&tmpformat, AST_FORMAT_ALAW, 0)) ||
		!ast_format_cap_iscompatible(cap1, ast_format_set(&tmpformat, AST_FORMAT_ULAW, 0)) ||
		!ast_format_cap_iscompatible(cap1, ast_format_set(&tmpformat, AST_FORMAT_GSM, 0)) ||
		!ast_format_cap_iscompatible(cap1, ast_format_set(&tmpformat, AST_FORMAT_H264, 0)) ||
		!ast_format_cap_iscompatible(cap1, ast_format_set(&tmpformat, AST_FORMAT_JPEG, 0)) ||
		!ast_format_cap_iscompatible(cap1, ast_format_set(&tmpformat, AST_FORMAT_T140, 0))) {
		ast_test_status_update(test, "ast cap1 failed to properly detect compatibility test 1.\n");
		res = AST_TEST_FAIL;
		goto test3_cleanup;
	}

	/* Test things that are not compatible */
	if (ast_format_cap_iscompatible(cap1, ast_format_set(&tmpformat, AST_FORMAT_SPEEX, 0)) ||
		ast_format_cap_iscompatible(cap1, ast_format_set(&tmpformat, AST_FORMAT_SPEEX16, 0)) ||
		ast_format_cap_iscompatible(cap1, ast_format_set(&tmpformat, AST_FORMAT_H261, 0))) {
		ast_test_status_update(test, "ast cap1 failed to properly detect compatibility test 2.\n");
		res = AST_TEST_FAIL;
		goto test3_cleanup;
	}

	/* Test compatiblity with format with attributes. */
	if (!ast_format_cap_iscompatible(cap1, ast_format_set(&tmpformat, AST_FORMAT_TESTLAW, 1,
			TEST_ATTR_KEY_STRING, "testing caps hooray",
			TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_8KHZ,
			TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_16KHZ,
			AST_FORMAT_ATTR_END))) {

		ast_test_status_update(test, "ast cap1 failed to properly detect compatibility test 3.\n");
		res = AST_TEST_FAIL;
		goto test3_cleanup;
	}
	if (!ast_format_cap_iscompatible(cap1, ast_format_set(&tmpformat, AST_FORMAT_TESTLAW, 1,
			TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_8KHZ,
			AST_FORMAT_ATTR_END))) {

		ast_test_status_update(test, "ast cap1 failed to properly detect compatibility test 4.\n");
		res = AST_TEST_FAIL;
		goto test3_cleanup;
	}
	if (ast_format_cap_iscompatible(cap1, ast_format_set(&tmpformat, AST_FORMAT_TESTLAW, 1,
			TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_8KHZ,
			TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_48KHZ, /* 48khz was not compatible, so this should fail iscompatible check */
			AST_FORMAT_ATTR_END))) {

		ast_test_status_update(test, "ast cap1 failed to properly detect compatibility test 5.\n");
		res = AST_TEST_FAIL;
		goto test3_cleanup;
	}

	/* Lets start testing the functions that compare ast_format_cap objects.
	 * Genreate the cap2 object to contain some similar formats as cap1
	 * and some different formats as well. */
	ast_format_cap_add(cap2, ast_format_set(&tmpformat, AST_FORMAT_GSM, 0));
	ast_format_cap_add(cap2, ast_format_set(&tmpformat, AST_FORMAT_ULAW, 0));
	ast_format_cap_add(cap2, ast_format_set(&tmpformat, AST_FORMAT_SIREN7, 0));
	ast_format_cap_add(cap2, ast_format_set(&tmpformat, AST_FORMAT_H261, 0));
	ast_format_cap_add(cap2, ast_format_set(&tmpformat, AST_FORMAT_T140, 0));
	ast_format_cap_add(cap2, ast_format_set(&tmpformat, AST_FORMAT_TESTLAW, 1,
			TEST_ATTR_KEY_STRING, "testing caps hooray",
			TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_8KHZ,
			TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_12KHZ,
			TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_16KHZ,
			TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_32KHZ,
			TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_48KHZ,
			AST_FORMAT_ATTR_END));


	/* find joint formats between cap1 and cap2 */
	cap_joint = ast_format_cap_joint(cap1, cap2);

	if (!cap_joint) {
		ast_test_status_update(test, "failed to create joint capabilities correctly.\n");
		res = AST_TEST_FAIL;
		goto test3_cleanup;
	}
	/* determine if cap_joint is what we think it should be */
	if (!ast_format_cap_iscompatible(cap_joint, ast_format_set(&tmpformat, AST_FORMAT_GSM, 0)) ||
		!ast_format_cap_iscompatible(cap_joint, ast_format_set(&tmpformat, AST_FORMAT_ULAW, 0)) ||
		!ast_format_cap_iscompatible(cap_joint, ast_format_set(&tmpformat, AST_FORMAT_T140, 0)) ||
		!ast_format_cap_iscompatible(cap_joint, ast_format_set(&tmpformat, AST_FORMAT_TESTLAW, 1,
			TEST_ATTR_KEY_STRING, "testing caps hooray",
			TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_8KHZ,
			TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_16KHZ,
			AST_FORMAT_ATTR_END))) {

		ast_test_status_update(test, "ast cap_joint failed to properly detect compatibility test 1.\n");
		res = AST_TEST_FAIL;
		goto test3_cleanup;
	}
	/* make sure joint cap does not have formats that should not be there */
	if (ast_format_cap_iscompatible(cap_joint, ast_format_set(&tmpformat, AST_FORMAT_SIREN7, 0)) ||
		ast_format_cap_iscompatible(cap_joint, ast_format_set(&tmpformat, AST_FORMAT_TESTLAW, 1,
			TEST_ATTR_KEY_STRING, "testing caps hooray",
			TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_8KHZ,
			TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_16KHZ,
			TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_48KHZ,
			AST_FORMAT_ATTR_END))) {

		ast_test_status_update(test, "ast cap_joint failed to properly detect compatibility test 1.\n");
		res = AST_TEST_FAIL;
		goto test3_cleanup;
	}

	/* Lets test removing a capability */
	if (ast_format_cap_remove(cap_joint, ast_format_set(&tmpformat, AST_FORMAT_T140, 0))) {
		ast_test_status_update(test, "ast_format_cap_remove failed. \n");
		res = AST_TEST_FAIL;
		goto test3_cleanup;
	}
	/* Lets make sure what we just removed does not still exist */
	if (ast_format_cap_iscompatible(cap_joint, &tmpformat)) {
		ast_test_status_update(test, "ast_format_cap_remove failed 2. \n");
		res = AST_TEST_FAIL;
		goto test3_cleanup;
	}
	/* Lets test removing a capability by id.*/
	if (ast_format_cap_remove_byid(cap_joint, AST_FORMAT_GSM)) {
		ast_test_status_update(test, "ast_format_cap_remove failed 3. \n");
		res = AST_TEST_FAIL;
		goto test3_cleanup;
	}
	/* Lets make sure what we just removed does not still exist */
	if (ast_format_cap_iscompatible(cap_joint, ast_format_set(&tmpformat, AST_FORMAT_GSM, 0))) {
		ast_test_status_update(test, "ast_format_cap_remove failed 4. \n");
		res = AST_TEST_FAIL;
		goto test3_cleanup;
	}

	/* lets test getting joint formats by type */
	ast_format_cap_destroy(cap_joint);
	if (!(cap_joint = ast_format_cap_get_type(cap1, AST_FORMAT_TYPE_VIDEO))) {
		ast_test_status_update(test, "ast_format_cap_get_type failed.\n");
		res = AST_TEST_FAIL;
		goto test3_cleanup;
	}
	/* lets make sure our joint capability structure has what we expect */
	if (!ast_format_cap_iscompatible(cap_joint, ast_format_set(&tmpformat, AST_FORMAT_H264, 0)) ||
		!ast_format_cap_iscompatible(cap_joint, ast_format_set(&tmpformat, AST_FORMAT_H263, 0))) {
		ast_test_status_update(test, "get_type failed 2.\n");
		res = AST_TEST_FAIL;
		goto test3_cleanup;
	}
	/* now make sure joint does not have anything but video */
	if (ast_format_cap_iscompatible(cap_joint, ast_format_set(&tmpformat, AST_FORMAT_ALAW, 0)) ||
		ast_format_cap_iscompatible(cap_joint, ast_format_set(&tmpformat, AST_FORMAT_ULAW, 0)) ||
		ast_format_cap_iscompatible(cap_joint, ast_format_set(&tmpformat, AST_FORMAT_GSM, 0)) ||
		ast_format_cap_iscompatible(cap_joint, ast_format_set(&tmpformat, AST_FORMAT_JPEG, 0)) ||
		ast_format_cap_iscompatible(cap_joint, ast_format_set(&tmpformat, AST_FORMAT_T140, 0))) {
		ast_test_status_update(test, "get_type failed 3.\n");
		res = AST_TEST_FAIL;
		goto test3_cleanup;
	}

	/* now lets remove everythign from cap_joint */
	ast_format_cap_remove_all(cap_joint);
	if (!ast_format_cap_is_empty(cap_joint)) {
		ast_test_status_update(test, "failed to remove all\n");
		res = AST_TEST_FAIL;
		goto test3_cleanup;
	}

	/* now lets add all by type */
	ast_format_cap_add_all_by_type(cap_joint, AST_FORMAT_TYPE_AUDIO);
	if (ast_format_cap_is_empty(cap_joint)) {
			ast_test_status_update(test, "failed to add all by type AUDIO\n");
			res = AST_TEST_FAIL;
	}
	ast_format_cap_iter_start(cap_joint);
	while (!(ast_format_cap_iter_next(cap_joint, &tmpformat))) {
		if (AST_FORMAT_GET_TYPE(tmpformat.id) != AST_FORMAT_TYPE_AUDIO) {
			ast_test_status_update(test, "failed to add all by type AUDIO\n");
			res = AST_TEST_FAIL;
			ast_format_cap_iter_end(cap_joint);
			goto test3_cleanup;
		}
	}
	ast_format_cap_iter_end(cap_joint);

	/* test append */
	ast_format_cap_append(cap_joint, cap1);
	ast_format_cap_iter_start(cap1);
	while (!(ast_format_cap_iter_next(cap1, &tmpformat))) {
		if (!ast_format_cap_iscompatible(cap_joint, &tmpformat)) {
			ast_test_status_update(test, "failed to append format capabilities.\n");
			res = AST_TEST_FAIL;
			ast_format_cap_iter_end(cap1);
			goto test3_cleanup;
		}
	}
	ast_format_cap_iter_end(cap1);

	/* test copy */
	cap1 = ast_format_cap_destroy(cap1);
	cap1 = ast_format_cap_dup(cap_joint);
	if (!ast_format_cap_identical(cap_joint, cap1)) {
			ast_test_status_update(test, "failed to copy capabilities\n");
			res = AST_TEST_FAIL;
			goto test3_cleanup;
	}

	/* test remove by type */
	ast_format_cap_remove_bytype(cap_joint, AST_FORMAT_TYPE_AUDIO);
	if (ast_format_cap_has_type(cap_joint, AST_FORMAT_TYPE_AUDIO)) {
		ast_test_status_update(test, "failed to remove all by type audio\n");
		res = AST_TEST_FAIL;
		goto test3_cleanup;
	}
	if (!ast_format_cap_has_type(cap_joint, AST_FORMAT_TYPE_TEXT)) { /* it should still have text */
		ast_test_status_update(test, "failed to remove all by type audio\n");
		res = AST_TEST_FAIL;
		goto test3_cleanup;
	}
	ast_format_cap_iter_start(cap_joint);
	while (!(ast_format_cap_iter_next(cap_joint, &tmpformat))) {
		if (AST_FORMAT_GET_TYPE(tmpformat.id) == AST_FORMAT_TYPE_AUDIO) {
			ast_test_status_update(test, "failed to remove all by type audio\n");
			res = AST_TEST_FAIL;
			ast_format_cap_iter_end(cap_joint);
			goto test3_cleanup;
		}
	}
	ast_format_cap_iter_end(cap_joint);

	/* test add all */
	ast_format_cap_remove_all(cap_joint);
	ast_format_cap_add_all(cap_joint);
	{
		int video = 0, audio = 0, text = 0, image = 0;
		ast_format_cap_iter_start(cap_joint);
		while (!(ast_format_cap_iter_next(cap_joint, &tmpformat))) {
			switch (AST_FORMAT_GET_TYPE(tmpformat.id)) {
			case AST_FORMAT_TYPE_AUDIO:
				audio++;
				break;
			case AST_FORMAT_TYPE_VIDEO:
				video++;
				break;
			case AST_FORMAT_TYPE_TEXT:
				text++;
				break;
			case AST_FORMAT_TYPE_IMAGE:
				image++;
				break;
			}
		}
		ast_format_cap_iter_end(cap_joint);
		if (!video || !audio || !text || !image) {
			ast_test_status_update(test, "failed to add all\n");
			res = AST_TEST_FAIL;
			ast_format_cap_iter_end(cap_joint);
			goto test3_cleanup;
		}
	}

	/* test copy2 */
	ast_format_cap_copy(cap2, cap_joint);
	if (!ast_format_cap_identical(cap2, cap_joint)) {
		ast_test_status_update(test, "ast_format_cap_copy failed\n");
		res = AST_TEST_FAIL;
		goto test3_cleanup;
	}

test3_cleanup:
	ast_format_cap_destroy(cap1);
	ast_format_cap_destroy(cap2);
	ast_format_cap_destroy(cap_joint);

	/* unregister interface */
	if (ast_format_attr_unreg_interface(&test_interface)) {
		ast_test_status_update(test, "test_interface failed to unregister.\n");
		res = AST_TEST_FAIL;
	}

	return res;
}

/*!
 * \internal
 */
AST_TEST_DEFINE(container_test1_nolock)
{
	struct ast_format_cap *cap1;
	struct ast_format_cap *cap2;

	switch (cmd) {
	case TEST_INIT:
		info->name = "container_test_1_no_locking";
		info->category = "/main/format/";
		info->summary = "Test ast_format and ast_format_cap structures, no locking";
		info->description =
			"This test exercises the Ast Format Capability API by creating "
			"capability structures and performing various API calls on them.";
		return AST_TEST_NOT_RUN;
	case TEST_EXECUTE:
		break;
	}

	cap1 = ast_format_cap_alloc_nolock();
	cap2 = ast_format_cap_alloc_nolock();

	if (!cap1 || !cap2) {
		ast_test_status_update(test, "cap alloc failed.\n");
		return AST_TEST_FAIL;
	}
	return container_test1_helper(cap1, cap2, test);
}


/*!
 * \internal
 */
AST_TEST_DEFINE(container_test1_withlock)
{
	struct ast_format_cap *cap1;
	struct ast_format_cap *cap2;

	switch (cmd) {
	case TEST_INIT:
		info->name = "container_test1_with_locking";
		info->category = "/main/format/";
		info->summary = "Test ast_format and ast_format_cap structures, with locking";
		info->description =
			"This test exercises the Ast Format Capability API by creating "
			"capability structures and performing various API calls on them.";
		return AST_TEST_NOT_RUN;
	case TEST_EXECUTE:
		break;
	}

	cap1 = ast_format_cap_alloc();
	cap2 = ast_format_cap_alloc();

	if (!cap1 || !cap2) {
		ast_test_status_update(test, "cap alloc failed.\n");
		return AST_TEST_FAIL;
	}
	return container_test1_helper(cap1, cap2, test);
}

static int container_test2_no_locking_helper(struct ast_format_cap *cap, struct ast_test *test)
{
	int num = 0;
	struct ast_format tmpformat = { 0, };

	ast_format_cap_add(cap, ast_format_set(&tmpformat, AST_FORMAT_GSM, 0));
	ast_format_cap_add(cap, ast_format_set(&tmpformat, AST_FORMAT_ULAW, 0));
	ast_format_cap_add(cap, ast_format_set(&tmpformat, AST_FORMAT_G722, 0));

	ast_format_cap_iter_start(cap);
	while (!ast_format_cap_iter_next(cap, &tmpformat)) {
		num++;
	}
	ast_format_cap_iter_end(cap);

	ast_format_cap_iter_start(cap);
	while (!ast_format_cap_iter_next(cap, &tmpformat)) {
		num++;
	}
	ast_format_cap_iter_end(cap);

	ast_format_cap_destroy(cap);
	ast_test_status_update(test, "%d items iterated over\n", num);
	return (num == 6) ? AST_TEST_PASS : AST_TEST_FAIL;

}

/*!
 * \internal
 */
AST_TEST_DEFINE(container_test2_no_locking)
{
	struct ast_format_cap *cap;

	switch (cmd) {
	case TEST_INIT:
		info->name = "container_test2_no_locking";
		info->category = "/main/format/";
		info->summary = "Test ast_format_cap iterator, no locking";
		info->description =
			"This test exercises the Ast Capability API iterators.";
		return AST_TEST_NOT_RUN;
	case TEST_EXECUTE:
		break;
	}

	cap = ast_format_cap_alloc_nolock();
	if (!cap) {
		ast_test_status_update(test, "alloc failed\n");
		return AST_TEST_FAIL;
	}
	return container_test2_no_locking_helper(cap, test);
}

/*!
 * \internal
 */
AST_TEST_DEFINE(container_test2_with_locking)
{
	struct ast_format_cap *cap;

	switch (cmd) {
	case TEST_INIT:
		info->name = "container_test2_with_locking";
		info->category = "/main/format/";
		info->summary = "Test ast_format_cap iterator, with locking";
		info->description =
			"This test exercises the Ast Capability API iterators.";
		return AST_TEST_NOT_RUN;
	case TEST_EXECUTE:
		break;
	}

	cap = ast_format_cap_alloc();
	if (!cap) {
		ast_test_status_update(test, "alloc failed\n");
		return AST_TEST_FAIL;
	}
	return container_test2_no_locking_helper(cap, test);
}


static int container_test3_helper(int nolocking, struct ast_test *test)
{
	int x;
	int res = AST_TEST_PASS;
	struct ast_format_cap *cap1;
	struct ast_format_cap *cap2;
	struct ast_format_cap *joint;

	for (x = 0; x < 2000; x++) {
		if (nolocking) {
			cap1 = ast_format_cap_alloc_nolock();
			cap2 = ast_format_cap_alloc_nolock();
			joint = ast_format_cap_alloc_nolock();
		} else {
			cap1 = ast_format_cap_alloc();
			cap2 = ast_format_cap_alloc();
			joint = ast_format_cap_alloc();
		}
		if (!cap1 || !cap2 || !joint) {
			ast_test_status_update(test, "cap alloc fail\n");
			return AST_TEST_FAIL;
		}
		ast_format_cap_add_all(cap1);
		ast_format_cap_add_all_by_type(cap2, AST_FORMAT_TYPE_AUDIO);
		ast_format_cap_joint_copy(cap1, cap2, joint);
		if (!(ast_format_cap_identical(cap2, joint))) {
			ast_test_status_update(test, "failed identical test\n");
			res = AST_TEST_FAIL;
			cap1 = ast_format_cap_destroy(cap1);
			cap2 = ast_format_cap_destroy(cap2);
			joint = ast_format_cap_destroy(joint);
			break;
		}
		cap1 = ast_format_cap_destroy(cap1);
		cap2 = ast_format_cap_destroy(cap2);
		joint = ast_format_cap_destroy(joint);
	}
	return res;
}

/*!
 * \internal
 */
AST_TEST_DEFINE(container_test3_nolock)
{
	switch (cmd) {
	case TEST_INIT:
		info->name = "container_test3_no_locking";
		info->category = "/main/format/";
		info->summary = "Load Test ast_format_cap no locking.";
		info->description =
			"This test exercises the Ast Capability API and its iterators for the purpose "
			"of measuring performance.";
		return AST_TEST_NOT_RUN;
	case TEST_EXECUTE:
		break;
	}

	return container_test3_helper(1, test);
}

/*!
 * \internal
 */
AST_TEST_DEFINE(container_test3_withlock)
{
	switch (cmd) {
	case TEST_INIT:
		info->name = "container_test3_with_locking";
		info->category = "/main/format/";
		info->summary = "Load Test ast_format_cap with locking.";
		info->description =
			"This test exercises the Ast Capability API and its iterators for the purpose "
			"of measuring performance.";
		return AST_TEST_NOT_RUN;
	case TEST_EXECUTE:
		break;
	}

	return container_test3_helper(0, test);
}

static int unload_module(void)
{
	AST_TEST_UNREGISTER(format_test1);
	AST_TEST_UNREGISTER(format_test2);
	AST_TEST_UNREGISTER(container_test1_nolock);
	AST_TEST_UNREGISTER(container_test1_withlock);
	AST_TEST_UNREGISTER(container_test2_no_locking);
	AST_TEST_UNREGISTER(container_test2_with_locking);
	AST_TEST_UNREGISTER(container_test3_nolock);
	AST_TEST_UNREGISTER(container_test3_withlock);

	return 0;
}

static int load_module(void)
{
	AST_TEST_REGISTER(format_test1);
	AST_TEST_REGISTER(format_test2);
	AST_TEST_REGISTER(container_test1_nolock);
	AST_TEST_REGISTER(container_test1_withlock);
	AST_TEST_REGISTER(container_test2_no_locking);
	AST_TEST_REGISTER(container_test2_with_locking);
	AST_TEST_REGISTER(container_test3_nolock);
	AST_TEST_REGISTER(container_test3_withlock);

	return AST_MODULE_LOAD_SUCCESS;
}

AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "ast_format API Tests");