git subrepo clone https://github.com/libretro/libretro-common.git deps/libretro-common
[pcsx_rearmed.git] / deps / libretro-common / samples / file / config_file / config_file_test.c
1 /* Copyright  (C) 2010-2020 The RetroArch team
2  *
3  * ---------------------------------------------------------------------------------------
4  * The following license statement only applies to this file (config_file_test.c).
5  * ---------------------------------------------------------------------------------------
6  *
7  * Permission is hereby granted, free of charge,
8  * to any person obtaining a copy of this software and associated documentation files (the "Software"),
9  * to deal in the Software without restriction, including without limitation the rights to
10  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
11  * and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
16  * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
19  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21  */
22
23 #include <stdlib.h>
24 #include <string.h>
25 #include <stdio.h>
26 #include <ctype.h>
27 #include <errno.h>
28
29 #include <file/config_file.h>
30
31 static void test_config_file_parse_contains(
32       const char *cfgtext,
33       const char *key, const char *val)
34 {
35    char *cfgtext_copy = strdup(cfgtext);
36    config_file_t *cfg = config_file_new_from_string(cfgtext_copy, NULL);
37    char          *out = NULL;
38    bool            ok = false;
39
40    free(cfgtext_copy);
41
42    if (!cfg)
43       abort();
44
45    ok = config_get_string(cfg, key, &out);
46    if (ok != (bool)val)
47       abort();
48    if (!val)
49       return;
50
51    if (!out)
52       out = strdup("");
53    if (strcmp(out, val) != 0)
54    {
55       printf("[FAILED] Key [%s] Doesn't contain val [%s]\n", key, val);
56       abort();
57    }
58    printf("[SUCCESS] Key [%s] contains val [%s]\n", key, val);
59    free(out);
60 }
61
62 int main(void)
63 {
64    test_config_file_parse_contains("foo = \"bar\"\n",   "foo", "bar");
65    test_config_file_parse_contains("foo = \"bar\"",     "foo", "bar");
66    test_config_file_parse_contains("foo = \"bar\"\r\n", "foo", "bar");
67    test_config_file_parse_contains("foo = \"bar\"",     "foo", "bar");
68
69    test_config_file_parse_contains("foo = \"\"\n",   "foo", "");
70    test_config_file_parse_contains("foo = \"\"",     "foo", "");
71    test_config_file_parse_contains("foo = \"\"\r\n", "foo", "");
72    test_config_file_parse_contains("foo = \"\"",     "foo", "");
73
74    test_config_file_parse_contains("foo = \"\"\n",   "bar", NULL);
75    test_config_file_parse_contains("foo = \"\"",     "bar", NULL);
76    test_config_file_parse_contains("foo = \"\"\r\n", "bar", NULL);
77    test_config_file_parse_contains("foo = \"\"",     "bar", NULL);
78 }