test/testiconv.c
author Andreas Schiffler <aschiffler@ferzkopp.net>
Mon, 03 Jun 2013 19:24:18 -0700
changeset 7268 b399d0441810
parent 5535 96594ac5fd1a
child 7436 10163e41e563
permissions -rw-r--r--
Add missing VS2012 test projects; update VS2010 and VS2012 solutions; update keybord suite for VS compiler warnings
     1 /*
     2   Copyright (C) 1997-2011 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 <stdio.h>
    14 
    15 #include "SDL.h"
    16 
    17 static size_t
    18 widelen(char *data)
    19 {
    20     size_t len = 0;
    21     Uint32 *p = (Uint32 *) data;
    22     while (*p++) {
    23         ++len;
    24     }
    25     return len;
    26 }
    27 
    28 int
    29 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 buffer[BUFSIZ];
    46     char *ucs4;
    47     char *test[2];
    48     int i, index = 0;
    49     FILE *file;
    50     int errors = 0;
    51 
    52     if (!argv[1]) {
    53         argv[1] = "utf8.txt";
    54     }
    55     file = fopen(argv[1], "rb");
    56     if (!file) {
    57         fprintf(stderr, "Unable to open %s\n", argv[1]);
    58         return (1);
    59     }
    60 
    61     while (fgets(buffer, sizeof(buffer), file)) {
    62         /* Convert to UCS-4 */
    63         size_t len;
    64         ucs4 =
    65             SDL_iconv_string("UCS-4", "UTF-8", buffer,
    66                              SDL_strlen(buffer) + 1);
    67         len = (widelen(ucs4) + 1) * 4;
    68         for (i = 0; i < SDL_arraysize(formats); ++i) {
    69             test[0] = SDL_iconv_string(formats[i], "UCS-4", ucs4, len);
    70             test[1] = SDL_iconv_string("UCS-4", formats[i], test[0], len);
    71             if (!test[1] || SDL_memcmp(test[1], ucs4, len) != 0) {
    72                 fprintf(stderr, "FAIL: %s\n", formats[i]);
    73                 ++errors;
    74             }
    75             if (test[0]) {
    76                 SDL_free(test[0]);
    77             }
    78             if (test[1]) {
    79                 SDL_free(test[1]);
    80             }
    81         }
    82         test[0] = SDL_iconv_string("UTF-8", "UCS-4", ucs4, len);
    83         SDL_free(ucs4);
    84         fputs(test[0], stdout);
    85         SDL_free(test[0]);
    86     }
    87     return (errors ? errors + 1 : 0);
    88 }