1/*
2 * This file is part of FFmpeg.
3 *
4 * FFmpeg 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 * FFmpeg 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 FFmpeg; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19#include <stdio.h>
20
21#include "libavutil/common.h"
22#include "libavutil/mem.h"
23#include "libavutil/avstring.h"
24
25int main(void)
26{
27    int i;
28    char *fullpath, *ptr;
29    static const char * const strings[] = {
30        "''",
31        "",
32        ":",
33        "\\",
34        "'",
35        "    ''    :",
36        "    ''  ''  :",
37        "foo   '' :",
38        "'foo'",
39        "foo     ",
40        "  '  foo  '  ",
41        "foo\\",
42        "foo':  blah:blah",
43        "foo\\:  blah:blah",
44        "foo\'",
45        "'foo :  '  :blahblah",
46        "\\ :blah",
47        "     foo",
48        "      foo       ",
49        "      foo     \\ ",
50        "foo ':blah",
51        " foo   bar    :   blahblah",
52        "\\f\\o\\o",
53        "'foo : \\ \\  '   : blahblah",
54        "'\\fo\\o:': blahblah",
55        "\\'fo\\o\\:':  foo  '  :blahblah"
56    };
57    const char *haystack = "Education consists mainly in what we have unlearned.";
58    const char * const needle[] = {"learned.", "unlearned.", "Unlearned"};
59
60    printf("Testing av_get_token()\n");
61    for (i = 0; i < FF_ARRAY_ELEMS(strings); i++) {
62        const char *p = strings[i];
63        char *q;
64        printf("|%s|", p);
65        q = av_get_token(&p, ":");
66        printf(" -> |%s|", q);
67        printf(" + |%s|\n", p);
68        av_free(q);
69    }
70
71    printf("Testing av_append_path_component()\n");
72    #define TEST_APPEND_PATH_COMPONENT(path, component, expected) \
73        fullpath = av_append_path_component((path), (component)); \
74        printf("%s = %s\n", fullpath ? fullpath : "(null)", expected); \
75        av_free(fullpath);
76    TEST_APPEND_PATH_COMPONENT(NULL, NULL, "(null)")
77    TEST_APPEND_PATH_COMPONENT("path", NULL, "path");
78    TEST_APPEND_PATH_COMPONENT(NULL, "comp", "comp");
79    TEST_APPEND_PATH_COMPONENT("path", "comp", "path/comp");
80    TEST_APPEND_PATH_COMPONENT("path/", "comp", "path/comp");
81    TEST_APPEND_PATH_COMPONENT("path", "/comp", "path/comp");
82    TEST_APPEND_PATH_COMPONENT("path/", "/comp", "path/comp");
83    TEST_APPEND_PATH_COMPONENT("path/path2/", "/comp/comp2", "path/path2/comp/comp2");
84
85    /*Testing av_strnstr()*/
86    #define TEST_STRNSTR(haystack, needle, hay_length, expected) \
87        ptr = av_strnstr(haystack, needle, hay_length); \
88        if (ptr != expected){ \
89            printf("expected: %p, received %p\n", expected, ptr); \
90        }
91    TEST_STRNSTR(haystack, needle [0], strlen(haystack), haystack+44);
92    TEST_STRNSTR(haystack, needle [1], strlen(haystack), haystack+42);
93    TEST_STRNSTR(haystack, needle [2], strlen(haystack), NULL       );
94    TEST_STRNSTR(haystack, strings[1], strlen(haystack), haystack   );
95
96    /*Testing av_strireplace()*/
97    #define TEST_STRIREPLACE(haystack, needle, expected) \
98        ptr = av_strireplace(haystack, needle, "instead"); \
99        if (ptr == NULL) { \
100            printf("error, received null pointer!\n"); \
101        } else { \
102            if (strcmp(ptr, expected) != 0) \
103                printf( "expected: %s, received: %s\n", expected, ptr); \
104            av_free(ptr); \
105        }
106
107    TEST_STRIREPLACE(haystack, needle [0], "Education consists mainly in what we have uninstead");
108    TEST_STRIREPLACE(haystack, needle [1], "Education consists mainly in what we have instead");
109    TEST_STRIREPLACE(haystack, needle [2], "Education consists mainly in what we have instead.");
110    TEST_STRIREPLACE(haystack, needle [1], "Education consists mainly in what we have instead");
111
112#if FF_API_D2STR
113FF_DISABLE_DEPRECATION_WARNINGS
114    /*Testing av_d2str()*/
115    #define TEST_D2STR(value, expected) \
116        if((ptr = av_d2str(value)) == NULL){ \
117            printf("error, received null pointer!\n"); \
118        } else { \
119            if(strcmp(ptr, expected) != 0) \
120                printf( "expected: %s, received: %s\n", expected, ptr); \
121            av_free(ptr); \
122        }
123    TEST_D2STR(0         ,  "0.000000");
124    TEST_D2STR(-1.2333234, "-1.233323");
125    TEST_D2STR(-1.2333237, "-1.233324");
126FF_ENABLE_DEPRECATION_WARNINGS
127#endif
128    return 0;
129}
130