diff options
| author | 3gg <3gg@shellblade.net> | 2025-12-27 12:03:39 -0800 |
|---|---|---|
| committer | 3gg <3gg@shellblade.net> | 2025-12-27 12:03:39 -0800 |
| commit | 5a079a2d114f96d4847d1ee305d5b7c16eeec50e (patch) | |
| tree | 8926ab44f168acf787d8e19608857b3af0f82758 /contrib/SDL-3.2.8/test/testiconv.c | |
Initial commit
Diffstat (limited to 'contrib/SDL-3.2.8/test/testiconv.c')
| -rw-r--r-- | contrib/SDL-3.2.8/test/testiconv.c | 147 |
1 files changed, 147 insertions, 0 deletions
diff --git a/contrib/SDL-3.2.8/test/testiconv.c b/contrib/SDL-3.2.8/test/testiconv.c new file mode 100644 index 0000000..189c02f --- /dev/null +++ b/contrib/SDL-3.2.8/test/testiconv.c | |||
| @@ -0,0 +1,147 @@ | |||
| 1 | /* | ||
| 2 | Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org> | ||
| 3 | |||
| 4 | This software is provided 'as-is', without any express or implied | ||
| 5 | warranty. In no event will the authors be held liable for any damages | ||
| 6 | arising from the use of this software. | ||
| 7 | |||
| 8 | Permission is granted to anyone to use this software for any purpose, | ||
| 9 | including commercial applications, and to alter it and redistribute it | ||
| 10 | freely. | ||
| 11 | */ | ||
| 12 | |||
| 13 | #include <SDL3/SDL.h> | ||
| 14 | #include <SDL3/SDL_main.h> | ||
| 15 | #include <SDL3/SDL_test.h> | ||
| 16 | #include "testutils.h" | ||
| 17 | |||
| 18 | static size_t | ||
| 19 | widelen(char *data) | ||
| 20 | { | ||
| 21 | size_t len = 0; | ||
| 22 | Uint32 *p = (Uint32 *)data; | ||
| 23 | while (*p++) { | ||
| 24 | ++len; | ||
| 25 | } | ||
| 26 | return len; | ||
| 27 | } | ||
| 28 | |||
| 29 | static char *get_next_line(Uint8 **fdataptr, size_t *fdatalen) | ||
| 30 | { | ||
| 31 | char *result = (char *) *fdataptr; | ||
| 32 | Uint8 *ptr = *fdataptr; | ||
| 33 | size_t len = *fdatalen; | ||
| 34 | |||
| 35 | if (len == 0) { | ||
| 36 | return NULL; | ||
| 37 | } | ||
| 38 | |||
| 39 | while (len > 0) { | ||
| 40 | if (*ptr == '\r') { | ||
| 41 | *ptr = '\0'; | ||
| 42 | } else if (*ptr == '\n') { | ||
| 43 | *ptr = '\0'; | ||
| 44 | ptr++; | ||
| 45 | len--; | ||
| 46 | break; | ||
| 47 | } | ||
| 48 | ptr++; | ||
| 49 | len--; | ||
| 50 | } | ||
| 51 | |||
| 52 | *fdataptr = ptr; | ||
| 53 | *fdatalen = len; | ||
| 54 | return result; | ||
| 55 | } | ||
| 56 | |||
| 57 | int main(int argc, char *argv[]) | ||
| 58 | { | ||
| 59 | const char *formats[] = { | ||
| 60 | "UTF8", | ||
| 61 | "UTF-8", | ||
| 62 | "UTF16BE", | ||
| 63 | "UTF-16BE", | ||
| 64 | "UTF16LE", | ||
| 65 | "UTF-16LE", | ||
| 66 | "UTF32BE", | ||
| 67 | "UTF-32BE", | ||
| 68 | "UTF32LE", | ||
| 69 | "UTF-32LE", | ||
| 70 | "UCS4", | ||
| 71 | "UCS-4", | ||
| 72 | }; | ||
| 73 | |||
| 74 | char *fname = NULL; | ||
| 75 | char *ucs4; | ||
| 76 | char *test[2]; | ||
| 77 | int i; | ||
| 78 | int errors = 0; | ||
| 79 | SDLTest_CommonState *state; | ||
| 80 | Uint8 *fdata = NULL; | ||
| 81 | Uint8 *fdataptr = NULL; | ||
| 82 | char *line = NULL; | ||
| 83 | size_t fdatalen = 0; | ||
| 84 | |||
| 85 | /* Initialize test framework */ | ||
| 86 | state = SDLTest_CommonCreateState(argv, 0); | ||
| 87 | if (!state) { | ||
| 88 | return 1; | ||
| 89 | } | ||
| 90 | |||
| 91 | /* Parse commandline */ | ||
| 92 | for (i = 1; i < argc;) { | ||
| 93 | int consumed; | ||
| 94 | |||
| 95 | consumed = SDLTest_CommonArg(state, i); | ||
| 96 | if (!consumed) { | ||
| 97 | if (!fname) { | ||
| 98 | fname = argv[i]; | ||
| 99 | consumed = 1; | ||
| 100 | } | ||
| 101 | } | ||
| 102 | if (consumed <= 0) { | ||
| 103 | static const char *options[] = { "[utf8.txt]", NULL }; | ||
| 104 | SDLTest_CommonLogUsage(state, argv[0], options); | ||
| 105 | return 1; | ||
| 106 | } | ||
| 107 | |||
| 108 | i += consumed; | ||
| 109 | } | ||
| 110 | |||
| 111 | fname = GetResourceFilename(fname, "utf8.txt"); | ||
| 112 | fdata = (Uint8 *) (fname ? SDL_LoadFile(fname, &fdatalen) : NULL); | ||
| 113 | if (!fdata) { | ||
| 114 | SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Unable to load %s", fname); | ||
| 115 | return 1; | ||
| 116 | } | ||
| 117 | |||
| 118 | fdataptr = fdata; | ||
| 119 | while ((line = get_next_line(&fdataptr, &fdatalen)) != NULL) { | ||
| 120 | /* Convert to UCS-4 */ | ||
| 121 | size_t len; | ||
| 122 | ucs4 = SDL_iconv_string("UCS-4", "UTF-8", line, SDL_strlen(line) + 1); | ||
| 123 | len = (widelen(ucs4) + 1) * 4; | ||
| 124 | |||
| 125 | for (i = 0; i < SDL_arraysize(formats); ++i) { | ||
| 126 | test[0] = SDL_iconv_string(formats[i], "UCS-4", ucs4, len); | ||
| 127 | test[1] = SDL_iconv_string("UCS-4", formats[i], test[0], len); | ||
| 128 | if (!test[1] || SDL_memcmp(test[1], ucs4, len) != 0) { | ||
| 129 | SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "FAIL: %s", formats[i]); | ||
| 130 | ++errors; | ||
| 131 | } | ||
| 132 | SDL_free(test[0]); | ||
| 133 | SDL_free(test[1]); | ||
| 134 | } | ||
| 135 | test[0] = SDL_iconv_string("UTF-8", "UCS-4", ucs4, len); | ||
| 136 | SDL_free(ucs4); | ||
| 137 | SDL_Log("%s", test[0]); | ||
| 138 | SDL_free(test[0]); | ||
| 139 | } | ||
| 140 | SDL_free(fdata); | ||
| 141 | SDL_free(fname); | ||
| 142 | |||
| 143 | SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Total errors: %d", errors); | ||
| 144 | SDL_Quit(); | ||
| 145 | SDLTest_CommonDestroyState(state); | ||
| 146 | return errors ? errors + 1 : 0; | ||
| 147 | } | ||
