tests: Move all test programs to a subdirectory
[libav.git] / libavutil / tests / opt.c
1 /*
2 * This file is part of Libav.
3 *
4 * Libav is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * Libav is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with Libav; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19 #include <limits.h>
20 #include <stdio.h>
21
22 #include "libavutil/common.h"
23 #include "libavutil/error.h"
24 #include "libavutil/log.h"
25 #include "libavutil/mem.h"
26 #include "libavutil/rational.h"
27 #include "libavutil/opt.h"
28
29 typedef struct TestContext {
30 const AVClass *class;
31 int num;
32 int toggle;
33 char *string;
34 int flags;
35 AVRational rational;
36 } TestContext;
37
38 #define OFFSET(x) offsetof(TestContext, x)
39
40 #define TEST_FLAG_COOL 01
41 #define TEST_FLAG_LAME 02
42 #define TEST_FLAG_MU 04
43
44 static const AVOption test_options[] = {
45 { "num", "set num", OFFSET(num), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 100 },
46 { "toggle", "set toggle", OFFSET(toggle), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1 },
47 { "rational", "set rational", OFFSET(rational), AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, 10 },
48 { "string", "set string", OFFSET(string), AV_OPT_TYPE_STRING, { 0 }, CHAR_MIN, CHAR_MAX },
49 { "flags", "set flags", OFFSET(flags), AV_OPT_TYPE_FLAGS, { .i64 = 0 }, 0, INT_MAX, 0, "flags"},
50 { "cool", "set cool flag ", 0, AV_OPT_TYPE_CONST, { .i64 = TEST_FLAG_COOL }, INT_MIN, INT_MAX, 0, "flags"},
51 { "lame", "set lame flag ", 0, AV_OPT_TYPE_CONST, { .i64 = TEST_FLAG_LAME }, INT_MIN, INT_MAX, 0, "flags"},
52 { "mu", "set mu flag ", 0, AV_OPT_TYPE_CONST, { .i64 = TEST_FLAG_MU }, INT_MIN, INT_MAX, 0, "flags"},
53 { NULL },
54 };
55
56 static const char *test_get_name(void *ctx)
57 {
58 return "test";
59 }
60
61 static const AVClass test_class = {
62 "TestContext",
63 test_get_name,
64 test_options
65 };
66
67 int main(void)
68 {
69 int i;
70 TestContext test_ctx = { .class = &test_class };
71 static const char *options[] = {
72 "",
73 ":",
74 "=",
75 "foo=:",
76 ":=foo",
77 "=foo",
78 "foo=",
79 "foo",
80 "foo=val",
81 "foo==val",
82 "toggle=:",
83 "string=:",
84 "toggle=1 : foo",
85 "toggle=100",
86 "toggle==1",
87 "flags=+mu-lame : num=42: toggle=0",
88 "num=42 : string=blahblah",
89 "rational=0 : rational=1/2 : rational=1/-1",
90 "rational=-1/0",
91 };
92
93 printf("\nTesting av_set_options_string()\n");
94
95 av_opt_set_defaults(&test_ctx);
96 test_ctx.string = av_strdup("default");
97 if (!test_ctx.string)
98 return AVERROR(ENOMEM);
99
100 av_log_set_level(AV_LOG_DEBUG);
101
102 for (i = 0; i < FF_ARRAY_ELEMS(options); i++) {
103 av_log(&test_ctx, AV_LOG_DEBUG, "Setting options string '%s'\n", options[i]);
104 if (av_set_options_string(&test_ctx, options[i], "=", ":") < 0)
105 av_log(&test_ctx, AV_LOG_ERROR, "Error setting options string: '%s'\n", options[i]);
106 printf("\n");
107 }
108
109 return 0;
110 }