encoding.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917
  1. /*
  2. * Copyright (C) 2017 Reece H. Dunn
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write see:
  16. * <http://www.gnu.org/licenses/>.
  17. */
  18. #include "config.h"
  19. #include <assert.h>
  20. #include <stdint.h>
  21. #include <stdlib.h>
  22. #include <stdio.h>
  23. #include <espeak-ng/espeak_ng.h>
  24. #include <espeak-ng/encoding.h>
  25. static void
  26. test_unbound_text_decoder()
  27. {
  28. printf("testing unbound text decoder\n");
  29. espeak_ng_TEXT_DECODER *decoder = create_text_decoder();
  30. assert(decoder != NULL);
  31. assert(text_decoder_eof(decoder) == 1);
  32. destroy_text_decoder(decoder);
  33. }
  34. static void
  35. test_unknown_encoding()
  36. {
  37. printf("testing unknown encodings\n");
  38. assert(espeak_ng_EncodingFromName(NULL) == ESPEAKNG_ENCODING_UNKNOWN);
  39. assert(espeak_ng_EncodingFromName("") == ESPEAKNG_ENCODING_UNKNOWN);
  40. assert(espeak_ng_EncodingFromName("abcxyz") == ESPEAKNG_ENCODING_UNKNOWN);
  41. assert(espeak_ng_EncodingFromName("US") == ESPEAKNG_ENCODING_UNKNOWN); // wrong case
  42. espeak_ng_TEXT_DECODER *decoder = create_text_decoder();
  43. assert(text_decoder_decode_string(decoder, "aG\x92\xA0\xDE", 5, ESPEAKNG_ENCODING_UNKNOWN) == ENS_UNKNOWN_TEXT_ENCODING);
  44. assert(text_decoder_eof(decoder) == 1);
  45. destroy_text_decoder(decoder);
  46. }
  47. static void
  48. test_us_ascii_encoding()
  49. {
  50. printf("testing US-ASCII encoding\n");
  51. assert(espeak_ng_EncodingFromName("US-ASCII") == ESPEAKNG_ENCODING_US_ASCII);
  52. assert(espeak_ng_EncodingFromName("iso-ir-6") == ESPEAKNG_ENCODING_US_ASCII);
  53. assert(espeak_ng_EncodingFromName("ANSI_X3.4-1968") == ESPEAKNG_ENCODING_US_ASCII);
  54. assert(espeak_ng_EncodingFromName("ANSI_X3.4-1986") == ESPEAKNG_ENCODING_US_ASCII);
  55. assert(espeak_ng_EncodingFromName("ISO_646.irv:1991") == ESPEAKNG_ENCODING_US_ASCII);
  56. assert(espeak_ng_EncodingFromName("ISO646-US") == ESPEAKNG_ENCODING_US_ASCII);
  57. assert(espeak_ng_EncodingFromName("us") == ESPEAKNG_ENCODING_US_ASCII);
  58. assert(espeak_ng_EncodingFromName("IBM367") == ESPEAKNG_ENCODING_US_ASCII);
  59. assert(espeak_ng_EncodingFromName("cp367") == ESPEAKNG_ENCODING_US_ASCII);
  60. assert(espeak_ng_EncodingFromName("csASCII") == ESPEAKNG_ENCODING_US_ASCII);
  61. espeak_ng_TEXT_DECODER *decoder = create_text_decoder();
  62. assert(text_decoder_decode_string(decoder, "aG\x92\xA0\xDE", 5, ESPEAKNG_ENCODING_US_ASCII) == ENS_OK);
  63. assert(text_decoder_eof(decoder) == 0);
  64. assert(text_decoder_getc(decoder) == 'a');
  65. assert(text_decoder_eof(decoder) == 0);
  66. assert(text_decoder_getc(decoder) == 'G');
  67. assert(text_decoder_eof(decoder) == 0);
  68. assert(text_decoder_getc(decoder) == 0xFFFD);
  69. assert(text_decoder_eof(decoder) == 0);
  70. assert(text_decoder_getc(decoder) == 0xFFFD);
  71. assert(text_decoder_eof(decoder) == 0);
  72. assert(text_decoder_getc(decoder) == 0xFFFD);
  73. assert(text_decoder_eof(decoder) == 1);
  74. destroy_text_decoder(decoder);
  75. }
  76. static void
  77. test_koi8_r_encoding()
  78. {
  79. printf("testing KOI8-R encoding\n");
  80. assert(espeak_ng_EncodingFromName("KOI8-R") == ESPEAKNG_ENCODING_KOI8_R);
  81. assert(espeak_ng_EncodingFromName("csKOI8R") == ESPEAKNG_ENCODING_KOI8_R);
  82. espeak_ng_TEXT_DECODER *decoder = create_text_decoder();
  83. assert(text_decoder_decode_string(decoder, "aG\x92\xA0\xDE", 5, ESPEAKNG_ENCODING_KOI8_R) == ENS_OK);
  84. assert(text_decoder_eof(decoder) == 0);
  85. assert(text_decoder_getc(decoder) == 'a');
  86. assert(text_decoder_eof(decoder) == 0);
  87. assert(text_decoder_getc(decoder) == 'G');
  88. assert(text_decoder_eof(decoder) == 0);
  89. assert(text_decoder_getc(decoder) == 0x92);
  90. assert(text_decoder_eof(decoder) == 0);
  91. assert(text_decoder_getc(decoder) == 0xA0);
  92. assert(text_decoder_eof(decoder) == 0);
  93. assert(text_decoder_getc(decoder) == 0x021a);
  94. assert(text_decoder_eof(decoder) == 1);
  95. destroy_text_decoder(decoder);
  96. }
  97. static void
  98. test_iscii_encoding()
  99. {
  100. printf("testing ISCII encoding\n");
  101. assert(espeak_ng_EncodingFromName("ISCII") == ESPEAKNG_ENCODING_ISCII);
  102. espeak_ng_TEXT_DECODER *decoder = create_text_decoder();
  103. assert(text_decoder_decode_string(decoder, "aG\x92\xA0\xE6", 5, ESPEAKNG_ENCODING_ISCII) == ENS_OK);
  104. assert(text_decoder_eof(decoder) == 0);
  105. assert(text_decoder_getc(decoder) == 'a');
  106. assert(text_decoder_eof(decoder) == 0);
  107. assert(text_decoder_getc(decoder) == 'G');
  108. assert(text_decoder_eof(decoder) == 0);
  109. assert(text_decoder_getc(decoder) == 0xfffd);
  110. assert(text_decoder_eof(decoder) == 0);
  111. assert(text_decoder_getc(decoder) == 0xfffd);
  112. assert(text_decoder_eof(decoder) == 0);
  113. assert(text_decoder_getc(decoder) == 0x094c);
  114. assert(text_decoder_eof(decoder) == 1);
  115. destroy_text_decoder(decoder);
  116. }
  117. static void
  118. test_iso_8859_1_encoding()
  119. {
  120. printf("testing ISO-8859-1 encoding\n");
  121. assert(espeak_ng_EncodingFromName("ISO-8859-1") == ESPEAKNG_ENCODING_ISO_8859_1);
  122. assert(espeak_ng_EncodingFromName("ISO_8859-1") == ESPEAKNG_ENCODING_ISO_8859_1);
  123. assert(espeak_ng_EncodingFromName("ISO_8859-1:1987") == ESPEAKNG_ENCODING_ISO_8859_1);
  124. assert(espeak_ng_EncodingFromName("iso-ir-100") == ESPEAKNG_ENCODING_ISO_8859_1);
  125. assert(espeak_ng_EncodingFromName("latin1") == ESPEAKNG_ENCODING_ISO_8859_1);
  126. assert(espeak_ng_EncodingFromName("l1") == ESPEAKNG_ENCODING_ISO_8859_1);
  127. assert(espeak_ng_EncodingFromName("IBM819") == ESPEAKNG_ENCODING_ISO_8859_1);
  128. assert(espeak_ng_EncodingFromName("cp819") == ESPEAKNG_ENCODING_ISO_8859_1);
  129. assert(espeak_ng_EncodingFromName("csISOLatin1") == ESPEAKNG_ENCODING_ISO_8859_1);
  130. espeak_ng_TEXT_DECODER *decoder = create_text_decoder();
  131. assert(text_decoder_decode_string(decoder, "aG\x92\xA0\xDE", 5, ESPEAKNG_ENCODING_ISO_8859_1) == ENS_OK);
  132. assert(text_decoder_eof(decoder) == 0);
  133. assert(text_decoder_getc(decoder) == 'a');
  134. assert(text_decoder_eof(decoder) == 0);
  135. assert(text_decoder_getc(decoder) == 'G');
  136. assert(text_decoder_eof(decoder) == 0);
  137. assert(text_decoder_getc(decoder) == 0x92);
  138. assert(text_decoder_eof(decoder) == 0);
  139. assert(text_decoder_getc(decoder) == 0xA0);
  140. assert(text_decoder_eof(decoder) == 0);
  141. assert(text_decoder_getc(decoder) == 0xDE);
  142. assert(text_decoder_eof(decoder) == 1);
  143. destroy_text_decoder(decoder);
  144. }
  145. static void
  146. test_iso_8859_2_encoding()
  147. {
  148. printf("testing ISO-8859-2 encoding\n");
  149. assert(espeak_ng_EncodingFromName("ISO-8859-2") == ESPEAKNG_ENCODING_ISO_8859_2);
  150. assert(espeak_ng_EncodingFromName("ISO_8859-2") == ESPEAKNG_ENCODING_ISO_8859_2);
  151. assert(espeak_ng_EncodingFromName("ISO_8859-2:1987") == ESPEAKNG_ENCODING_ISO_8859_2);
  152. assert(espeak_ng_EncodingFromName("iso-ir-101") == ESPEAKNG_ENCODING_ISO_8859_2);
  153. assert(espeak_ng_EncodingFromName("latin2") == ESPEAKNG_ENCODING_ISO_8859_2);
  154. assert(espeak_ng_EncodingFromName("l2") == ESPEAKNG_ENCODING_ISO_8859_2);
  155. assert(espeak_ng_EncodingFromName("csISOLatin2") == ESPEAKNG_ENCODING_ISO_8859_2);
  156. espeak_ng_TEXT_DECODER *decoder = create_text_decoder();
  157. assert(text_decoder_decode_string(decoder, "aG\x92\xA0\xDE", 5, ESPEAKNG_ENCODING_ISO_8859_2) == ENS_OK);
  158. assert(text_decoder_eof(decoder) == 0);
  159. assert(text_decoder_getc(decoder) == 'a');
  160. assert(text_decoder_eof(decoder) == 0);
  161. assert(text_decoder_getc(decoder) == 'G');
  162. assert(text_decoder_eof(decoder) == 0);
  163. assert(text_decoder_getc(decoder) == 0x92);
  164. assert(text_decoder_eof(decoder) == 0);
  165. assert(text_decoder_getc(decoder) == 0xA0);
  166. assert(text_decoder_eof(decoder) == 0);
  167. assert(text_decoder_getc(decoder) == 0x0162);
  168. assert(text_decoder_eof(decoder) == 1);
  169. destroy_text_decoder(decoder);
  170. }
  171. static void
  172. test_iso_8859_3_encoding()
  173. {
  174. printf("testing ISO-8859-3 encoding\n");
  175. assert(espeak_ng_EncodingFromName("ISO-8859-3") == ESPEAKNG_ENCODING_ISO_8859_3);
  176. assert(espeak_ng_EncodingFromName("ISO_8859-3") == ESPEAKNG_ENCODING_ISO_8859_3);
  177. assert(espeak_ng_EncodingFromName("ISO_8859-3:1988") == ESPEAKNG_ENCODING_ISO_8859_3);
  178. assert(espeak_ng_EncodingFromName("iso-ir-109") == ESPEAKNG_ENCODING_ISO_8859_3);
  179. assert(espeak_ng_EncodingFromName("latin3") == ESPEAKNG_ENCODING_ISO_8859_3);
  180. assert(espeak_ng_EncodingFromName("l3") == ESPEAKNG_ENCODING_ISO_8859_3);
  181. assert(espeak_ng_EncodingFromName("csISOLatin3") == ESPEAKNG_ENCODING_ISO_8859_3);
  182. espeak_ng_TEXT_DECODER *decoder = create_text_decoder();
  183. assert(text_decoder_decode_string(decoder, "aG\x92\xA0\xDE", 5, ESPEAKNG_ENCODING_ISO_8859_3) == ENS_OK);
  184. assert(text_decoder_eof(decoder) == 0);
  185. assert(text_decoder_getc(decoder) == 'a');
  186. assert(text_decoder_eof(decoder) == 0);
  187. assert(text_decoder_getc(decoder) == 'G');
  188. assert(text_decoder_eof(decoder) == 0);
  189. assert(text_decoder_getc(decoder) == 0x92);
  190. assert(text_decoder_eof(decoder) == 0);
  191. assert(text_decoder_getc(decoder) == 0xA0);
  192. assert(text_decoder_eof(decoder) == 0);
  193. assert(text_decoder_getc(decoder) == 0x015C);
  194. assert(text_decoder_eof(decoder) == 1);
  195. destroy_text_decoder(decoder);
  196. }
  197. static void
  198. test_iso_8859_4_encoding()
  199. {
  200. printf("testing ISO-8859-4 encoding\n");
  201. assert(espeak_ng_EncodingFromName("ISO-8859-4") == ESPEAKNG_ENCODING_ISO_8859_4);
  202. assert(espeak_ng_EncodingFromName("ISO_8859-4") == ESPEAKNG_ENCODING_ISO_8859_4);
  203. assert(espeak_ng_EncodingFromName("ISO_8859-4:1988") == ESPEAKNG_ENCODING_ISO_8859_4);
  204. assert(espeak_ng_EncodingFromName("iso-ir-110") == ESPEAKNG_ENCODING_ISO_8859_4);
  205. assert(espeak_ng_EncodingFromName("latin4") == ESPEAKNG_ENCODING_ISO_8859_4);
  206. assert(espeak_ng_EncodingFromName("l4") == ESPEAKNG_ENCODING_ISO_8859_4);
  207. assert(espeak_ng_EncodingFromName("csISOLatin4") == ESPEAKNG_ENCODING_ISO_8859_4);
  208. espeak_ng_TEXT_DECODER *decoder = create_text_decoder();
  209. assert(text_decoder_decode_string(decoder, "aG\x92\xA0\xDE", 5, ESPEAKNG_ENCODING_ISO_8859_4) == ENS_OK);
  210. assert(text_decoder_eof(decoder) == 0);
  211. assert(text_decoder_getc(decoder) == 'a');
  212. assert(text_decoder_eof(decoder) == 0);
  213. assert(text_decoder_getc(decoder) == 'G');
  214. assert(text_decoder_eof(decoder) == 0);
  215. assert(text_decoder_getc(decoder) == 0x92);
  216. assert(text_decoder_eof(decoder) == 0);
  217. assert(text_decoder_getc(decoder) == 0xA0);
  218. assert(text_decoder_eof(decoder) == 0);
  219. assert(text_decoder_getc(decoder) == 0x016A);
  220. assert(text_decoder_eof(decoder) == 1);
  221. destroy_text_decoder(decoder);
  222. }
  223. static void
  224. test_iso_8859_5_encoding()
  225. {
  226. printf("testing ISO-8859-5 encoding\n");
  227. assert(espeak_ng_EncodingFromName("ISO-8859-5") == ESPEAKNG_ENCODING_ISO_8859_5);
  228. assert(espeak_ng_EncodingFromName("ISO_8859-5") == ESPEAKNG_ENCODING_ISO_8859_5);
  229. assert(espeak_ng_EncodingFromName("ISO_8859-5:1988") == ESPEAKNG_ENCODING_ISO_8859_5);
  230. assert(espeak_ng_EncodingFromName("iso-ir-144") == ESPEAKNG_ENCODING_ISO_8859_5);
  231. assert(espeak_ng_EncodingFromName("cyrillic") == ESPEAKNG_ENCODING_ISO_8859_5);
  232. assert(espeak_ng_EncodingFromName("csISOLatinCyrillic") == ESPEAKNG_ENCODING_ISO_8859_5);
  233. espeak_ng_TEXT_DECODER *decoder = create_text_decoder();
  234. assert(text_decoder_decode_string(decoder, "aG\x92\xA0\xDE", 5, ESPEAKNG_ENCODING_ISO_8859_5) == ENS_OK);
  235. assert(text_decoder_eof(decoder) == 0);
  236. assert(text_decoder_getc(decoder) == 'a');
  237. assert(text_decoder_eof(decoder) == 0);
  238. assert(text_decoder_getc(decoder) == 'G');
  239. assert(text_decoder_eof(decoder) == 0);
  240. assert(text_decoder_getc(decoder) == 0x92);
  241. assert(text_decoder_eof(decoder) == 0);
  242. assert(text_decoder_getc(decoder) == 0xA0);
  243. assert(text_decoder_eof(decoder) == 0);
  244. assert(text_decoder_getc(decoder) == 0x043E);
  245. assert(text_decoder_eof(decoder) == 1);
  246. destroy_text_decoder(decoder);
  247. }
  248. static void
  249. test_iso_8859_6_encoding()
  250. {
  251. printf("testing ISO-8859-6 encoding\n");
  252. assert(espeak_ng_EncodingFromName("ISO-8859-6") == ESPEAKNG_ENCODING_ISO_8859_6);
  253. assert(espeak_ng_EncodingFromName("ISO_8859-6") == ESPEAKNG_ENCODING_ISO_8859_6);
  254. assert(espeak_ng_EncodingFromName("ISO_8859-6:1987") == ESPEAKNG_ENCODING_ISO_8859_6);
  255. assert(espeak_ng_EncodingFromName("iso-ir-127") == ESPEAKNG_ENCODING_ISO_8859_6);
  256. assert(espeak_ng_EncodingFromName("ECMA-114") == ESPEAKNG_ENCODING_ISO_8859_6);
  257. assert(espeak_ng_EncodingFromName("ASMO-708") == ESPEAKNG_ENCODING_ISO_8859_6);
  258. assert(espeak_ng_EncodingFromName("arabic") == ESPEAKNG_ENCODING_ISO_8859_6);
  259. assert(espeak_ng_EncodingFromName("csISOLatinArabic") == ESPEAKNG_ENCODING_ISO_8859_6);
  260. espeak_ng_TEXT_DECODER *decoder = create_text_decoder();
  261. assert(text_decoder_decode_string(decoder, "aG\x92\xA0\xDA", 5, ESPEAKNG_ENCODING_ISO_8859_6) == ENS_OK);
  262. assert(text_decoder_eof(decoder) == 0);
  263. assert(text_decoder_getc(decoder) == 'a');
  264. assert(text_decoder_eof(decoder) == 0);
  265. assert(text_decoder_getc(decoder) == 'G');
  266. assert(text_decoder_eof(decoder) == 0);
  267. assert(text_decoder_getc(decoder) == 0x92);
  268. assert(text_decoder_eof(decoder) == 0);
  269. assert(text_decoder_getc(decoder) == 0xA0);
  270. assert(text_decoder_eof(decoder) == 0);
  271. assert(text_decoder_getc(decoder) == 0x063A);
  272. assert(text_decoder_eof(decoder) == 1);
  273. destroy_text_decoder(decoder);
  274. }
  275. static void
  276. test_iso_8859_7_encoding()
  277. {
  278. printf("testing ISO-8859-7 encoding\n");
  279. assert(espeak_ng_EncodingFromName("ISO-8859-7") == ESPEAKNG_ENCODING_ISO_8859_7);
  280. assert(espeak_ng_EncodingFromName("ISO_8859-7") == ESPEAKNG_ENCODING_ISO_8859_7);
  281. assert(espeak_ng_EncodingFromName("ISO_8859-7:1987") == ESPEAKNG_ENCODING_ISO_8859_7);
  282. assert(espeak_ng_EncodingFromName("iso-ir-126") == ESPEAKNG_ENCODING_ISO_8859_7);
  283. assert(espeak_ng_EncodingFromName("ECMA-118") == ESPEAKNG_ENCODING_ISO_8859_7);
  284. assert(espeak_ng_EncodingFromName("ELOT_928") == ESPEAKNG_ENCODING_ISO_8859_7);
  285. assert(espeak_ng_EncodingFromName("greek") == ESPEAKNG_ENCODING_ISO_8859_7);
  286. assert(espeak_ng_EncodingFromName("greek8") == ESPEAKNG_ENCODING_ISO_8859_7);
  287. assert(espeak_ng_EncodingFromName("csISOLatinGreek") == ESPEAKNG_ENCODING_ISO_8859_7);
  288. espeak_ng_TEXT_DECODER *decoder = create_text_decoder();
  289. assert(text_decoder_decode_string(decoder, "aG\x92\xA0\xDE", 5, ESPEAKNG_ENCODING_ISO_8859_7) == ENS_OK);
  290. assert(text_decoder_eof(decoder) == 0);
  291. assert(text_decoder_getc(decoder) == 'a');
  292. assert(text_decoder_eof(decoder) == 0);
  293. assert(text_decoder_getc(decoder) == 'G');
  294. assert(text_decoder_eof(decoder) == 0);
  295. assert(text_decoder_getc(decoder) == 0x92);
  296. assert(text_decoder_eof(decoder) == 0);
  297. assert(text_decoder_getc(decoder) == 0xA0);
  298. assert(text_decoder_eof(decoder) == 0);
  299. assert(text_decoder_getc(decoder) == 0x03AE);
  300. assert(text_decoder_eof(decoder) == 1);
  301. destroy_text_decoder(decoder);
  302. }
  303. static void
  304. test_iso_8859_8_encoding()
  305. {
  306. printf("testing ISO-8859-8 encoding\n");
  307. assert(espeak_ng_EncodingFromName("ISO-8859-8") == ESPEAKNG_ENCODING_ISO_8859_8);
  308. assert(espeak_ng_EncodingFromName("ISO_8859-8") == ESPEAKNG_ENCODING_ISO_8859_8);
  309. assert(espeak_ng_EncodingFromName("ISO_8859-8:1988") == ESPEAKNG_ENCODING_ISO_8859_8);
  310. assert(espeak_ng_EncodingFromName("iso-ir-138") == ESPEAKNG_ENCODING_ISO_8859_8);
  311. assert(espeak_ng_EncodingFromName("hebrew") == ESPEAKNG_ENCODING_ISO_8859_8);
  312. assert(espeak_ng_EncodingFromName("csISOLatinHebrew") == ESPEAKNG_ENCODING_ISO_8859_8);
  313. espeak_ng_TEXT_DECODER *decoder = create_text_decoder();
  314. assert(text_decoder_decode_string(decoder, "aG\x92\xA0\xEE", 5, ESPEAKNG_ENCODING_ISO_8859_8) == ENS_OK);
  315. assert(text_decoder_eof(decoder) == 0);
  316. assert(text_decoder_getc(decoder) == 'a');
  317. assert(text_decoder_eof(decoder) == 0);
  318. assert(text_decoder_getc(decoder) == 'G');
  319. assert(text_decoder_eof(decoder) == 0);
  320. assert(text_decoder_getc(decoder) == 0x92);
  321. assert(text_decoder_eof(decoder) == 0);
  322. assert(text_decoder_getc(decoder) == 0xA0);
  323. assert(text_decoder_eof(decoder) == 0);
  324. assert(text_decoder_getc(decoder) == 0x05de);
  325. assert(text_decoder_eof(decoder) == 1);
  326. destroy_text_decoder(decoder);
  327. }
  328. static void
  329. test_iso_8859_9_encoding()
  330. {
  331. printf("testing ISO-8859-9 encoding\n");
  332. assert(espeak_ng_EncodingFromName("ISO-8859-9") == ESPEAKNG_ENCODING_ISO_8859_9);
  333. assert(espeak_ng_EncodingFromName("ISO_8859-9") == ESPEAKNG_ENCODING_ISO_8859_9);
  334. assert(espeak_ng_EncodingFromName("ISO_8859-9:1989") == ESPEAKNG_ENCODING_ISO_8859_9);
  335. assert(espeak_ng_EncodingFromName("iso-ir-148") == ESPEAKNG_ENCODING_ISO_8859_9);
  336. assert(espeak_ng_EncodingFromName("latin5") == ESPEAKNG_ENCODING_ISO_8859_9);
  337. assert(espeak_ng_EncodingFromName("l5") == ESPEAKNG_ENCODING_ISO_8859_9);
  338. assert(espeak_ng_EncodingFromName("csISOLatin5") == ESPEAKNG_ENCODING_ISO_8859_9);
  339. espeak_ng_TEXT_DECODER *decoder = create_text_decoder();
  340. assert(text_decoder_decode_string(decoder, "aG\x92\xA0\xDE", 5, ESPEAKNG_ENCODING_ISO_8859_9) == ENS_OK);
  341. assert(text_decoder_eof(decoder) == 0);
  342. assert(text_decoder_getc(decoder) == 'a');
  343. assert(text_decoder_eof(decoder) == 0);
  344. assert(text_decoder_getc(decoder) == 'G');
  345. assert(text_decoder_eof(decoder) == 0);
  346. assert(text_decoder_getc(decoder) == 0x92);
  347. assert(text_decoder_eof(decoder) == 0);
  348. assert(text_decoder_getc(decoder) == 0xA0);
  349. assert(text_decoder_eof(decoder) == 0);
  350. assert(text_decoder_getc(decoder) == 0x015e);
  351. assert(text_decoder_eof(decoder) == 1);
  352. destroy_text_decoder(decoder);
  353. }
  354. static void
  355. test_iso_8859_10_encoding()
  356. {
  357. printf("testing ISO-8859-10 encoding\n");
  358. assert(espeak_ng_EncodingFromName("ISO-8859-10") == ESPEAKNG_ENCODING_ISO_8859_10);
  359. assert(espeak_ng_EncodingFromName("ISO_8859-10") == ESPEAKNG_ENCODING_ISO_8859_10);
  360. assert(espeak_ng_EncodingFromName("ISO_8859-10:1992") == ESPEAKNG_ENCODING_ISO_8859_10);
  361. assert(espeak_ng_EncodingFromName("iso-ir-157") == ESPEAKNG_ENCODING_ISO_8859_10);
  362. assert(espeak_ng_EncodingFromName("latin6") == ESPEAKNG_ENCODING_ISO_8859_10);
  363. assert(espeak_ng_EncodingFromName("l6") == ESPEAKNG_ENCODING_ISO_8859_10);
  364. assert(espeak_ng_EncodingFromName("csISOLatin6") == ESPEAKNG_ENCODING_ISO_8859_10);
  365. espeak_ng_TEXT_DECODER *decoder = create_text_decoder();
  366. assert(text_decoder_decode_string(decoder, "aG\x92\xA0\xDE", 5, ESPEAKNG_ENCODING_ISO_8859_10) == ENS_OK);
  367. assert(text_decoder_eof(decoder) == 0);
  368. assert(text_decoder_getc(decoder) == 'a');
  369. assert(text_decoder_eof(decoder) == 0);
  370. assert(text_decoder_getc(decoder) == 'G');
  371. assert(text_decoder_eof(decoder) == 0);
  372. assert(text_decoder_getc(decoder) == 0x92);
  373. assert(text_decoder_eof(decoder) == 0);
  374. assert(text_decoder_getc(decoder) == 0xA0);
  375. assert(text_decoder_eof(decoder) == 0);
  376. assert(text_decoder_getc(decoder) == 0x00de);
  377. assert(text_decoder_eof(decoder) == 1);
  378. destroy_text_decoder(decoder);
  379. }
  380. static void
  381. test_iso_8859_11_encoding()
  382. {
  383. printf("testing ISO-8859-11 encoding\n");
  384. assert(espeak_ng_EncodingFromName("ISO-8859-11") == ESPEAKNG_ENCODING_ISO_8859_11);
  385. assert(espeak_ng_EncodingFromName("TIS-620") == ESPEAKNG_ENCODING_ISO_8859_11);
  386. assert(espeak_ng_EncodingFromName("csTIS620") == ESPEAKNG_ENCODING_ISO_8859_11);
  387. espeak_ng_TEXT_DECODER *decoder = create_text_decoder();
  388. assert(text_decoder_decode_string(decoder, "aG\x92\xA0\xEE", 5, ESPEAKNG_ENCODING_ISO_8859_11) == ENS_OK);
  389. assert(text_decoder_eof(decoder) == 0);
  390. assert(text_decoder_getc(decoder) == 'a');
  391. assert(text_decoder_eof(decoder) == 0);
  392. assert(text_decoder_getc(decoder) == 'G');
  393. assert(text_decoder_eof(decoder) == 0);
  394. assert(text_decoder_getc(decoder) == 0x92);
  395. assert(text_decoder_eof(decoder) == 0);
  396. assert(text_decoder_getc(decoder) == 0xA0);
  397. assert(text_decoder_eof(decoder) == 0);
  398. assert(text_decoder_getc(decoder) == 0x0e4e);
  399. assert(text_decoder_eof(decoder) == 1);
  400. destroy_text_decoder(decoder);
  401. }
  402. static void
  403. test_iso_8859_13_encoding()
  404. {
  405. printf("testing ISO-8859-13 encoding\n");
  406. assert(espeak_ng_EncodingFromName("ISO-8859-13") == ESPEAKNG_ENCODING_ISO_8859_13);
  407. assert(espeak_ng_EncodingFromName("csISO885913") == ESPEAKNG_ENCODING_ISO_8859_13);
  408. espeak_ng_TEXT_DECODER *decoder = create_text_decoder();
  409. assert(text_decoder_decode_string(decoder, "aG\x92\xA0\xEE", 5, ESPEAKNG_ENCODING_ISO_8859_13) == ENS_OK);
  410. assert(text_decoder_eof(decoder) == 0);
  411. assert(text_decoder_getc(decoder) == 'a');
  412. assert(text_decoder_eof(decoder) == 0);
  413. assert(text_decoder_getc(decoder) == 'G');
  414. assert(text_decoder_eof(decoder) == 0);
  415. assert(text_decoder_getc(decoder) == 0x92);
  416. assert(text_decoder_eof(decoder) == 0);
  417. assert(text_decoder_getc(decoder) == 0xA0);
  418. assert(text_decoder_eof(decoder) == 0);
  419. assert(text_decoder_getc(decoder) == 0x012b);
  420. assert(text_decoder_eof(decoder) == 1);
  421. destroy_text_decoder(decoder);
  422. }
  423. static void
  424. test_iso_8859_14_encoding()
  425. {
  426. printf("testing ISO-8859-14 encoding\n");
  427. assert(espeak_ng_EncodingFromName("ISO-8859-14") == ESPEAKNG_ENCODING_ISO_8859_14);
  428. assert(espeak_ng_EncodingFromName("ISO_8859-14") == ESPEAKNG_ENCODING_ISO_8859_14);
  429. assert(espeak_ng_EncodingFromName("ISO_8859-14:1998") == ESPEAKNG_ENCODING_ISO_8859_14);
  430. assert(espeak_ng_EncodingFromName("iso-ir-199") == ESPEAKNG_ENCODING_ISO_8859_14);
  431. assert(espeak_ng_EncodingFromName("iso-celtic") == ESPEAKNG_ENCODING_ISO_8859_14);
  432. assert(espeak_ng_EncodingFromName("latin8") == ESPEAKNG_ENCODING_ISO_8859_14);
  433. assert(espeak_ng_EncodingFromName("l8") == ESPEAKNG_ENCODING_ISO_8859_14);
  434. assert(espeak_ng_EncodingFromName("csISO885914") == ESPEAKNG_ENCODING_ISO_8859_14);
  435. espeak_ng_TEXT_DECODER *decoder = create_text_decoder();
  436. assert(text_decoder_decode_string(decoder, "aG\x92\xA0\xDE", 5, ESPEAKNG_ENCODING_ISO_8859_14) == ENS_OK);
  437. assert(text_decoder_eof(decoder) == 0);
  438. assert(text_decoder_getc(decoder) == 'a');
  439. assert(text_decoder_eof(decoder) == 0);
  440. assert(text_decoder_getc(decoder) == 'G');
  441. assert(text_decoder_eof(decoder) == 0);
  442. assert(text_decoder_getc(decoder) == 0x92);
  443. assert(text_decoder_eof(decoder) == 0);
  444. assert(text_decoder_getc(decoder) == 0xA0);
  445. assert(text_decoder_eof(decoder) == 0);
  446. assert(text_decoder_getc(decoder) == 0x0176);
  447. assert(text_decoder_eof(decoder) == 1);
  448. destroy_text_decoder(decoder);
  449. }
  450. static void
  451. test_iso_8859_15_encoding()
  452. {
  453. printf("testing ISO-8859-15 encoding\n");
  454. assert(espeak_ng_EncodingFromName("ISO-8859-15") == ESPEAKNG_ENCODING_ISO_8859_15);
  455. assert(espeak_ng_EncodingFromName("ISO_8859-15") == ESPEAKNG_ENCODING_ISO_8859_15);
  456. assert(espeak_ng_EncodingFromName("Latin-9") == ESPEAKNG_ENCODING_ISO_8859_15);
  457. assert(espeak_ng_EncodingFromName("csISO885915") == ESPEAKNG_ENCODING_ISO_8859_15);
  458. espeak_ng_TEXT_DECODER *decoder = create_text_decoder();
  459. assert(text_decoder_decode_string(decoder, "aG\x92\xA0\xBE", 5, ESPEAKNG_ENCODING_ISO_8859_15) == ENS_OK);
  460. assert(text_decoder_eof(decoder) == 0);
  461. assert(text_decoder_getc(decoder) == 'a');
  462. assert(text_decoder_eof(decoder) == 0);
  463. assert(text_decoder_getc(decoder) == 'G');
  464. assert(text_decoder_eof(decoder) == 0);
  465. assert(text_decoder_getc(decoder) == 0x92);
  466. assert(text_decoder_eof(decoder) == 0);
  467. assert(text_decoder_getc(decoder) == 0xA0);
  468. assert(text_decoder_eof(decoder) == 0);
  469. assert(text_decoder_getc(decoder) == 0x0178);
  470. assert(text_decoder_eof(decoder) == 1);
  471. destroy_text_decoder(decoder);
  472. }
  473. static void
  474. test_iso_8859_16_encoding()
  475. {
  476. printf("testing ISO-8859-16 encoding\n");
  477. assert(espeak_ng_EncodingFromName("ISO-8859-16") == ESPEAKNG_ENCODING_ISO_8859_16);
  478. assert(espeak_ng_EncodingFromName("ISO_8859-16") == ESPEAKNG_ENCODING_ISO_8859_16);
  479. assert(espeak_ng_EncodingFromName("ISO_8859-16:2001") == ESPEAKNG_ENCODING_ISO_8859_16);
  480. assert(espeak_ng_EncodingFromName("iso-ir-226") == ESPEAKNG_ENCODING_ISO_8859_16);
  481. assert(espeak_ng_EncodingFromName("latin10") == ESPEAKNG_ENCODING_ISO_8859_16);
  482. assert(espeak_ng_EncodingFromName("l10") == ESPEAKNG_ENCODING_ISO_8859_16);
  483. assert(espeak_ng_EncodingFromName("csISO885916") == ESPEAKNG_ENCODING_ISO_8859_16);
  484. espeak_ng_TEXT_DECODER *decoder = create_text_decoder();
  485. assert(text_decoder_decode_string(decoder, "aG\x92\xA0\xDE", 5, ESPEAKNG_ENCODING_ISO_8859_16) == ENS_OK);
  486. assert(text_decoder_eof(decoder) == 0);
  487. assert(text_decoder_getc(decoder) == 'a');
  488. assert(text_decoder_eof(decoder) == 0);
  489. assert(text_decoder_getc(decoder) == 'G');
  490. assert(text_decoder_eof(decoder) == 0);
  491. assert(text_decoder_getc(decoder) == 0x92);
  492. assert(text_decoder_eof(decoder) == 0);
  493. assert(text_decoder_getc(decoder) == 0xA0);
  494. assert(text_decoder_eof(decoder) == 0);
  495. assert(text_decoder_getc(decoder) == 0x021a);
  496. assert(text_decoder_eof(decoder) == 1);
  497. destroy_text_decoder(decoder);
  498. }
  499. static void
  500. test_utf_8_encoding()
  501. {
  502. printf("testing UTF-8 encoding\n");
  503. assert(espeak_ng_EncodingFromName("UTF-8") == ESPEAKNG_ENCODING_UTF_8);
  504. assert(espeak_ng_EncodingFromName("csUTF8") == ESPEAKNG_ENCODING_UTF_8);
  505. espeak_ng_TEXT_DECODER *decoder = create_text_decoder();
  506. // 1-byte UTF-8 sequences
  507. assert(text_decoder_decode_string(decoder, "\x0D\x1E\x20\x35\x42\x57\x65\x77", 8, ESPEAKNG_ENCODING_UTF_8) == ENS_OK);
  508. assert(text_decoder_eof(decoder) == 0);
  509. assert(text_decoder_getc(decoder) == 0x000D);
  510. assert(text_decoder_eof(decoder) == 0);
  511. assert(text_decoder_getc(decoder) == 0x001E);
  512. assert(text_decoder_eof(decoder) == 0);
  513. assert(text_decoder_getc(decoder) == 0x0020);
  514. assert(text_decoder_eof(decoder) == 0);
  515. assert(text_decoder_getc(decoder) == 0x0035);
  516. assert(text_decoder_eof(decoder) == 0);
  517. assert(text_decoder_getc(decoder) == 0x0042);
  518. assert(text_decoder_eof(decoder) == 0);
  519. assert(text_decoder_getc(decoder) == 0x0057);
  520. assert(text_decoder_eof(decoder) == 0);
  521. assert(text_decoder_getc(decoder) == 0x0065);
  522. assert(text_decoder_eof(decoder) == 0);
  523. assert(text_decoder_getc(decoder) == 0x0077);
  524. assert(text_decoder_eof(decoder) == 1);
  525. // UTF-8 tail bytes without an initial length indicator character
  526. assert(text_decoder_decode_string(decoder, "\x84\x92\xA8\xB5", 4, ESPEAKNG_ENCODING_UTF_8) == ENS_OK);
  527. assert(text_decoder_eof(decoder) == 0);
  528. assert(text_decoder_getc(decoder) == 0xFFFD);
  529. assert(text_decoder_eof(decoder) == 0);
  530. assert(text_decoder_getc(decoder) == 0xFFFD);
  531. assert(text_decoder_eof(decoder) == 0);
  532. assert(text_decoder_getc(decoder) == 0xFFFD);
  533. assert(text_decoder_eof(decoder) == 0);
  534. assert(text_decoder_getc(decoder) == 0xFFFD);
  535. assert(text_decoder_eof(decoder) == 1);
  536. // 2-byte UTF-8 sequences
  537. assert(text_decoder_decode_string(decoder, "\xC2\xA0\xD0\xB0\xC5\x65\xC2\xA0", 7, ESPEAKNG_ENCODING_UTF_8) == ENS_OK);
  538. assert(text_decoder_eof(decoder) == 0);
  539. assert(text_decoder_getc(decoder) == 0x00A0);
  540. assert(text_decoder_eof(decoder) == 0);
  541. assert(text_decoder_getc(decoder) == 0x0430);
  542. assert(text_decoder_eof(decoder) == 0);
  543. assert(text_decoder_getc(decoder) == 0xFFFD); // \x65 is not a continuation byte, so \xC5 is invalid
  544. assert(text_decoder_eof(decoder) == 0);
  545. assert(text_decoder_getc(decoder) == 0x0065);
  546. assert(text_decoder_eof(decoder) == 0);
  547. assert(text_decoder_getc(decoder) == 0xFFFD); // incomplete: \xA0 is past the end of the string
  548. assert(text_decoder_eof(decoder) == 1);
  549. // 3-byte UTF-8 sequences
  550. assert(text_decoder_decode_string(decoder, "\xE4\xBA\x8C\xE8\x42\xE2\x93\x44\xE4\xA0\x80", 9, ESPEAKNG_ENCODING_UTF_8) == ENS_OK);
  551. assert(text_decoder_eof(decoder) == 0);
  552. assert(text_decoder_getc(decoder) == 0x4E8C);
  553. assert(text_decoder_eof(decoder) == 0);
  554. assert(text_decoder_getc(decoder) == 0xFFFD); // \x42 is not a continuation byte, so \xE8 is invalid
  555. assert(text_decoder_eof(decoder) == 0);
  556. assert(text_decoder_getc(decoder) == 0x0042);
  557. assert(text_decoder_eof(decoder) == 0);
  558. assert(text_decoder_getc(decoder) == 0xFFFD); // \x44 is not a continuation byte, so \xE2\x93 is invalid
  559. assert(text_decoder_eof(decoder) == 0);
  560. assert(text_decoder_getc(decoder) == 0x0044);
  561. assert(text_decoder_eof(decoder) == 0);
  562. assert(text_decoder_getc(decoder) == 0xFFFD); // incomplete: \xA0 is past the end of the string
  563. assert(text_decoder_eof(decoder) == 1);
  564. // 4-byte UTF-8 sequences
  565. assert(text_decoder_decode_string(decoder, "\xF0\x90\x8C\x82\xF4\x8F\xBF\xBF\xF3\x61\xF3\xA5\x32\xF3\x87\xB2\x36\xF1\xA0\x80\x80", 18, ESPEAKNG_ENCODING_UTF_8) == ENS_OK);
  566. assert(text_decoder_eof(decoder) == 0);
  567. assert(text_decoder_getc(decoder) == 0x10302);
  568. assert(text_decoder_eof(decoder) == 0);
  569. assert(text_decoder_getc(decoder) == 0x10FFFF);
  570. assert(text_decoder_eof(decoder) == 0);
  571. assert(text_decoder_getc(decoder) == 0xFFFD); // \x61 is not a continuation byte, so \xF3 is invalid
  572. assert(text_decoder_eof(decoder) == 0);
  573. assert(text_decoder_getc(decoder) == 0x0061);
  574. assert(text_decoder_eof(decoder) == 0);
  575. assert(text_decoder_getc(decoder) == 0xFFFD); // \x32 is not a continuation byte, so \xF3\xA5 is invalid
  576. assert(text_decoder_eof(decoder) == 0);
  577. assert(text_decoder_getc(decoder) == 0x0032);
  578. assert(text_decoder_eof(decoder) == 0);
  579. assert(text_decoder_getc(decoder) == 0xFFFD); // \x36 is not a continuation byte, so \xF3\x87\xB2 is invalid
  580. assert(text_decoder_eof(decoder) == 0);
  581. assert(text_decoder_getc(decoder) == 0x0036);
  582. assert(text_decoder_eof(decoder) == 0);
  583. assert(text_decoder_getc(decoder) == 0xFFFD); // incomplete: \xA0 is past the end of the string
  584. assert(text_decoder_eof(decoder) == 1);
  585. // out of range (> 0x10FFFF)
  586. assert(text_decoder_decode_string(decoder, "\xF4\x90\x80\x80", 4, ESPEAKNG_ENCODING_UTF_8) == ENS_OK);
  587. assert(text_decoder_eof(decoder) == 0);
  588. assert(text_decoder_getc(decoder) == 0xFFFD);
  589. assert(text_decoder_eof(decoder) == 1);
  590. destroy_text_decoder(decoder);
  591. }
  592. static void
  593. test_iso_10646_ucs_2_encoding()
  594. {
  595. printf("testing ISO-10646-UCS-2 encoding\n");
  596. assert(espeak_ng_EncodingFromName("ISO-10646-UCS-2") == ESPEAKNG_ENCODING_ISO_10646_UCS_2);
  597. assert(espeak_ng_EncodingFromName("csUnicode") == ESPEAKNG_ENCODING_ISO_10646_UCS_2);
  598. espeak_ng_TEXT_DECODER *decoder = create_text_decoder();
  599. assert(text_decoder_decode_string(decoder, "a\00G\00\xA0\00\x22\x21\x23\x21", 9, ESPEAKNG_ENCODING_ISO_10646_UCS_2) == ENS_OK);
  600. assert(text_decoder_eof(decoder) == 0);
  601. assert(text_decoder_getc(decoder) == 'a');
  602. assert(text_decoder_eof(decoder) == 0);
  603. assert(text_decoder_getc(decoder) == 'G');
  604. assert(text_decoder_eof(decoder) == 0);
  605. assert(text_decoder_getc(decoder) == 0xA0);
  606. assert(text_decoder_eof(decoder) == 0);
  607. assert(text_decoder_getc(decoder) == 0x2122);
  608. assert(text_decoder_eof(decoder) == 0);
  609. assert(text_decoder_getc(decoder) == 0xFFFD);
  610. assert(text_decoder_eof(decoder) == 1);
  611. destroy_text_decoder(decoder);
  612. }
  613. static void
  614. test_char_decoder()
  615. {
  616. printf("testing char decoder\n");
  617. espeak_ng_TEXT_DECODER *decoder = create_text_decoder();
  618. // null string
  619. assert(text_decoder_decode_string(decoder, NULL, -1, ESPEAKNG_ENCODING_ISO_8859_1) == ENS_OK);
  620. assert(text_decoder_eof(decoder) == 1);
  621. assert(text_decoder_getc(decoder) == 0);
  622. assert(text_decoder_eof(decoder) == 1);
  623. // string length
  624. assert(text_decoder_decode_string(decoder, "aG", -1, ESPEAKNG_ENCODING_ISO_8859_1) == ENS_OK);
  625. assert(text_decoder_eof(decoder) == 0);
  626. assert(text_decoder_getc(decoder) == 'a');
  627. assert(text_decoder_eof(decoder) == 0);
  628. assert(text_decoder_getc(decoder) == 'G');
  629. assert(text_decoder_eof(decoder) == 0);
  630. assert(text_decoder_getc(decoder) == 0);
  631. assert(text_decoder_eof(decoder) == 1);
  632. destroy_text_decoder(decoder);
  633. }
  634. static void
  635. test_wchar_decoder()
  636. {
  637. printf("testing wchar_t decoder\n");
  638. espeak_ng_TEXT_DECODER *decoder = create_text_decoder();
  639. // null string
  640. assert(text_decoder_decode_wstring(decoder, NULL, -1) == ENS_OK);
  641. assert(text_decoder_eof(decoder) == 1);
  642. assert(text_decoder_getc(decoder) == 0);
  643. assert(text_decoder_eof(decoder) == 1);
  644. // wide-character string
  645. assert(text_decoder_decode_wstring(decoder, L"aG\xA0\x2045", 4) == ENS_OK);
  646. assert(text_decoder_eof(decoder) == 0);
  647. assert(text_decoder_getc(decoder) == 'a');
  648. assert(text_decoder_eof(decoder) == 0);
  649. assert(text_decoder_getc(decoder) == 'G');
  650. assert(text_decoder_eof(decoder) == 0);
  651. assert(text_decoder_getc(decoder) == 0xA0);
  652. assert(text_decoder_eof(decoder) == 0);
  653. assert(text_decoder_getc(decoder) == 0x2045);
  654. assert(text_decoder_eof(decoder) == 1);
  655. // string length
  656. assert(text_decoder_decode_wstring(decoder, L"aG\xA0\x2045", -1) == ENS_OK);
  657. assert(text_decoder_eof(decoder) == 0);
  658. assert(text_decoder_getc(decoder) == 'a');
  659. assert(text_decoder_eof(decoder) == 0);
  660. assert(text_decoder_getc(decoder) == 'G');
  661. assert(text_decoder_eof(decoder) == 0);
  662. assert(text_decoder_getc(decoder) == 0xA0);
  663. assert(text_decoder_eof(decoder) == 0);
  664. assert(text_decoder_getc(decoder) == 0x2045);
  665. assert(text_decoder_eof(decoder) == 0);
  666. assert(text_decoder_getc(decoder) == 0);
  667. assert(text_decoder_eof(decoder) == 1);
  668. destroy_text_decoder(decoder);
  669. }
  670. static void
  671. test_auto_decoder()
  672. {
  673. printf("testing auto decoder (UTF-8 + codepage-based fallback)\n");
  674. espeak_ng_TEXT_DECODER *decoder = create_text_decoder();
  675. // null string
  676. assert(text_decoder_decode_string_auto(decoder, NULL, -1, ESPEAKNG_ENCODING_ISO_8859_1) == ENS_OK);
  677. assert(text_decoder_eof(decoder) == 1);
  678. assert(text_decoder_getc(decoder) == 0);
  679. assert(text_decoder_eof(decoder) == 1);
  680. // UTF-8
  681. assert(text_decoder_decode_string_auto(decoder, "aG\xC2\xA0 ", 5, ESPEAKNG_ENCODING_ISO_8859_1) == ENS_OK);
  682. assert(text_decoder_eof(decoder) == 0);
  683. assert(text_decoder_getc(decoder) == 'a');
  684. assert(text_decoder_eof(decoder) == 0);
  685. assert(text_decoder_getc(decoder) == 'G');
  686. assert(text_decoder_eof(decoder) == 0);
  687. assert(text_decoder_getc(decoder) == 0xA0);
  688. assert(text_decoder_eof(decoder) == 0);
  689. assert(text_decoder_getc(decoder) == ' ');
  690. assert(text_decoder_eof(decoder) == 1);
  691. // ISO-8859-1
  692. assert(text_decoder_decode_string_auto(decoder, "aG\240f", 4, ESPEAKNG_ENCODING_ISO_8859_1) == ENS_OK);
  693. assert(text_decoder_eof(decoder) == 0);
  694. assert(text_decoder_getc(decoder) == 'a');
  695. assert(text_decoder_eof(decoder) == 0);
  696. assert(text_decoder_getc(decoder) == 'G');
  697. assert(text_decoder_eof(decoder) == 0);
  698. assert(text_decoder_getc(decoder) == 0xA0);
  699. assert(text_decoder_eof(decoder) == 0);
  700. assert(text_decoder_getc(decoder) == 'f');
  701. assert(text_decoder_eof(decoder) == 1);
  702. // string length
  703. assert(text_decoder_decode_string_auto(decoder, "aG", -1, ESPEAKNG_ENCODING_ISO_8859_1) == ENS_OK);
  704. assert(text_decoder_eof(decoder) == 0);
  705. assert(text_decoder_getc(decoder) == 'a');
  706. assert(text_decoder_eof(decoder) == 0);
  707. assert(text_decoder_getc(decoder) == 'G');
  708. assert(text_decoder_eof(decoder) == 0);
  709. assert(text_decoder_getc(decoder) == 0);
  710. assert(text_decoder_eof(decoder) == 1);
  711. destroy_text_decoder(decoder);
  712. }
  713. static void
  714. test_peekc()
  715. {
  716. printf("testing peekc\n");
  717. espeak_ng_TEXT_DECODER *decoder = create_text_decoder();
  718. assert(text_decoder_decode_string(decoder, "aGd", 3, ESPEAKNG_ENCODING_US_ASCII) == ENS_OK);
  719. assert(text_decoder_eof(decoder) == 0);
  720. assert(text_decoder_getc(decoder) == 'a');
  721. assert(text_decoder_eof(decoder) == 0);
  722. assert(text_decoder_peekc(decoder) == 'G');
  723. assert(text_decoder_eof(decoder) == 0);
  724. assert(text_decoder_getc(decoder) == 'G');
  725. assert(text_decoder_eof(decoder) == 0);
  726. assert(text_decoder_peekc(decoder) == 'd');
  727. assert(text_decoder_eof(decoder) == 0);
  728. assert(text_decoder_getc(decoder) == 'd');
  729. assert(text_decoder_eof(decoder) == 1);
  730. // Calling peekc past the end of the buffer.
  731. assert(text_decoder_peekc(decoder) == '\0');
  732. assert(text_decoder_eof(decoder) == 1);
  733. destroy_text_decoder(decoder);
  734. }
  735. int
  736. main(int argc, char **argv)
  737. {
  738. (void)argc; // unused parameter
  739. (void)argv; // unused parameter
  740. test_unbound_text_decoder();
  741. test_unknown_encoding();
  742. test_us_ascii_encoding();
  743. test_koi8_r_encoding();
  744. test_iscii_encoding();
  745. test_iso_8859_1_encoding();
  746. test_iso_8859_2_encoding();
  747. test_iso_8859_3_encoding();
  748. test_iso_8859_4_encoding();
  749. test_iso_8859_5_encoding();
  750. test_iso_8859_6_encoding();
  751. test_iso_8859_7_encoding();
  752. test_iso_8859_8_encoding();
  753. test_iso_8859_9_encoding();
  754. test_iso_8859_10_encoding();
  755. test_iso_8859_11_encoding();
  756. // ISO-8859-12 is not a valid encoding.
  757. test_iso_8859_13_encoding();
  758. test_iso_8859_14_encoding();
  759. test_iso_8859_15_encoding();
  760. test_iso_8859_16_encoding();
  761. test_utf_8_encoding();
  762. test_iso_10646_ucs_2_encoding();
  763. test_char_decoder();
  764. test_wchar_decoder();
  765. test_auto_decoder();
  766. test_peekc();
  767. return EXIT_SUCCESS;
  768. }