testiconv.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. Copyright (C) 1997-2023 Sam Lantinga <[email protected]>
  3. This software is provided 'as-is', without any express or implied
  4. warranty. In no event will the authors be held liable for any damages
  5. arising from the use of this software.
  6. Permission is granted to anyone to use this software for any purpose,
  7. including commercial applications, and to alter it and redistribute it
  8. freely.
  9. */
  10. /* quiet windows compiler warnings */
  11. #if defined(_MSC_VER) && !defined(_CRT_SECURE_NO_WARNINGS)
  12. #define _CRT_SECURE_NO_WARNINGS
  13. #endif
  14. #include <stdio.h>
  15. #include <SDL3/SDL.h>
  16. #include <SDL3/SDL_main.h>
  17. #include <SDL3/SDL_test.h>
  18. #include "testutils.h"
  19. static size_t
  20. widelen(char *data)
  21. {
  22. size_t len = 0;
  23. Uint32 *p = (Uint32 *)data;
  24. while (*p++) {
  25. ++len;
  26. }
  27. return len;
  28. }
  29. int main(int argc, char *argv[])
  30. {
  31. const char *formats[] = {
  32. "UTF8",
  33. "UTF-8",
  34. "UTF16BE",
  35. "UTF-16BE",
  36. "UTF16LE",
  37. "UTF-16LE",
  38. "UTF32BE",
  39. "UTF-32BE",
  40. "UTF32LE",
  41. "UTF-32LE",
  42. "UCS4",
  43. "UCS-4",
  44. };
  45. char *fname = NULL;
  46. char buffer[BUFSIZ];
  47. char *ucs4;
  48. char *test[2];
  49. int i;
  50. FILE *file;
  51. int errors = 0;
  52. SDLTest_CommonState *state;
  53. /* Initialize test framework */
  54. state = SDLTest_CommonCreateState(argv, 0);
  55. if (state == NULL) {
  56. return 1;
  57. }
  58. /* Enable standard application logging */
  59. SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
  60. /* Parse commandline */
  61. for (i = 1; i < argc;) {
  62. int consumed;
  63. consumed = SDLTest_CommonArg(state, i);
  64. if (!consumed) {
  65. if (!fname) {
  66. fname = argv[i];
  67. consumed = 1;
  68. }
  69. }
  70. if (consumed <= 0) {
  71. static const char *options[] = { "[utf8.txt]", NULL };
  72. SDLTest_CommonLogUsage(state, argv[0], options);
  73. return 1;
  74. }
  75. i += consumed;
  76. }
  77. fname = GetResourceFilename(fname, "utf8.txt");
  78. file = fopen(fname, "rb");
  79. if (file == NULL) {
  80. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Unable to open %s\n", fname);
  81. return 1;
  82. }
  83. SDL_free(fname);
  84. while (fgets(buffer, sizeof(buffer), file)) {
  85. /* Convert to UCS-4 */
  86. size_t len;
  87. ucs4 =
  88. SDL_iconv_string("UCS-4", "UTF-8", buffer,
  89. SDL_strlen(buffer) + 1);
  90. len = (widelen(ucs4) + 1) * 4;
  91. for (i = 0; i < SDL_arraysize(formats); ++i) {
  92. test[0] = SDL_iconv_string(formats[i], "UCS-4", ucs4, len);
  93. test[1] = SDL_iconv_string("UCS-4", formats[i], test[0], len);
  94. if (!test[1] || SDL_memcmp(test[1], ucs4, len) != 0) {
  95. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "FAIL: %s\n", formats[i]);
  96. ++errors;
  97. }
  98. SDL_free(test[0]);
  99. SDL_free(test[1]);
  100. }
  101. test[0] = SDL_iconv_string("UTF-8", "UCS-4", ucs4, len);
  102. SDL_free(ucs4);
  103. (void)fputs(test[0], stdout);
  104. SDL_free(test[0]);
  105. }
  106. (void)fclose(file);
  107. SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Total errors: %d\n", errors);
  108. SDLTest_CommonDestroyState(state);
  109. return errors ? errors + 1 : 0;
  110. }