| 1 |
/* |
| 2 |
text: Test text transformations in libmpg123 (conversion to UTF-8). |
| 3 |
|
| 4 |
copyright 2009-2023 by the mpg123 project - free software under the terms of the LGPL 2.1 |
| 5 |
see COPYING and AUTHORS files in distribution or http://mpg123.org |
| 6 |
initially written by Thomas Orgis |
| 7 |
|
| 8 |
arguments: decoder testfile.mpeg |
| 9 |
*/ |
| 10 |
|
| 11 |
#include "config.h" |
| 12 |
#include <mpg123.h> |
| 13 |
#include "../compat/compat.h" |
| 14 |
|
| 15 |
#include "testtext.h" |
| 16 |
|
| 17 |
int string_good(mpg123_string *sb) |
| 18 |
{ |
| 19 |
/* Let's accept additional null bytes... */ |
| 20 |
if(sb->fill >= sizeof(utf8) && memcmp(utf8, sb->p, sizeof(utf8)) == 0 |
| 21 |
&& (sb->fill <= sizeof(utf8) || sb->p[sizeof(utf8)] == 0) ) |
| 22 |
return 1; |
| 23 |
else |
| 24 |
return 0; |
| 25 |
} |
| 26 |
|
| 27 |
int chomp_check(const char *in, const char *out) |
| 28 |
{ |
| 29 |
int ret = 1; |
| 30 |
mpg123_string work; |
| 31 |
mpg123_init_string(&work); |
| 32 |
if( mpg123_set_string(&work, in) && mpg123_chomp_string(&work) |
| 33 |
&& mpg123_strlen(&work, 0) == strlen(out) |
| 34 |
&& !strcmp(work.p, out) ) |
| 35 |
{ |
| 36 |
ret = 0; |
| 37 |
} |
| 38 |
mpg123_free_string(&work); |
| 39 |
return ret; |
| 40 |
} |
| 41 |
|
| 42 |
int check_string(const char* name, enum mpg123_text_encoding enc, const unsigned char* source, size_t source_size) |
| 43 |
{ |
| 44 |
int ret = 0; |
| 45 |
mpg123_string sb; |
| 46 |
mpg123_init_string(&sb); |
| 47 |
printf("Conversion of %s: ", name); |
| 48 |
if( mpg123_store_utf8(&sb, enc, source, source_size) |
| 49 |
&& string_good(&sb) ) |
| 50 |
{ |
| 51 |
printf("PASS\n"); |
| 52 |
ret = 0; |
| 53 |
} |
| 54 |
else |
| 55 |
{ |
| 56 |
printf("FAIL (%zu vs. %zu)\n", sb.fill, sizeof(utf8)); |
| 57 |
ret = 1; |
| 58 |
} |
| 59 |
|
| 60 |
mpg123_free_string(&sb); |
| 61 |
return ret; |
| 62 |
} |
| 63 |
|
| 64 |
/* We test: latin1, cp1252, utf8 and all of utf16be/le with and without BOM. |
| 65 |
Everything should succeed -- except utf16le without BOM. */ |
| 66 |
int main() |
| 67 |
{ |
| 68 |
int ret = 0; |
| 69 |
mpg123_string trans_utf16le; |
| 70 |
|
| 71 |
mpg123_init_string(&trans_utf16le); |
| 72 |
|
| 73 |
/* First, all conversions that should work. */ |
| 74 |
ret += check_string("latin1", mpg123_text_latin1, latin1, sizeof(latin1)); |
| 75 |
ret += check_string("cp1252", mpg123_text_cp1252, cp1252, sizeof(cp1252)); |
| 76 |
ret += check_string("utf8", mpg123_text_utf8, utf8, sizeof(utf8)); |
| 77 |
ret += check_string("utf16bom_be", mpg123_text_utf16bom, utf16bom_be, sizeof(utf16bom_be)); |
| 78 |
ret += check_string("utf16bom_le", mpg123_text_utf16, utf16bom_le, sizeof(utf16bom_le)); |
| 79 |
ret += check_string("utf16be", mpg123_text_utf16be, utf16be, sizeof(utf16be)); |
| 80 |
|
| 81 |
/* Now test the non-supported string. */ |
| 82 |
printf("Let's see what happens with a non-BOM little endian UTF-16 string: "); |
| 83 |
mpg123_store_utf8(&trans_utf16le, mpg123_text_utf16, utf16le, sizeof(utf16le)); |
| 84 |
if(string_good(&trans_utf16le)) |
| 85 |
{ |
| 86 |
++ret; |
| 87 |
printf("FAIL\n"); |
| 88 |
} |
| 89 |
else printf("PASS\n"); |
| 90 |
|
| 91 |
mpg123_free_string(&trans_utf16le); |
| 92 |
|
| 93 |
printf("Now some basic usage of chomp and strlen.\n"); |
| 94 |
ret += chomp_check( |
| 95 |
"This is the longest text evaah \n\n\r" |
| 96 |
, "This is the longest text evaah " |
| 97 |
); |
| 98 |
ret += chomp_check( |
| 99 |
"Foo." |
| 100 |
, "Foo." |
| 101 |
); |
| 102 |
|
| 103 |
printf("\n%s\n", ret == 0 ? "PASS" : "FAIL"); |
| 104 |
|
| 105 |
return ret; |
| 106 |
} |