test-conversion.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205
  1. /* Copyright (C) 1999,2000,2001,2003,2004, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
  2. *
  3. * This library is free software; you can redistribute it and/or
  4. * modify it under the terms of the GNU Lesser General Public License
  5. * as published by the Free Software Foundation; either version 3 of
  6. * the License, or (at your option) any later version.
  7. *
  8. * This library is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * Lesser General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Lesser General Public
  14. * License along with this library; if not, write to the Free Software
  15. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  16. * 02110-1301 USA
  17. */
  18. #if HAVE_CONFIG_H
  19. # include <config.h>
  20. #endif
  21. #include <libguile.h>
  22. #include <stdlib.h>
  23. #include <stdio.h>
  24. #include <string.h>
  25. #ifdef HAVE_INTTYPES_H
  26. # include <inttypes.h>
  27. #endif
  28. #ifndef PRIiMAX
  29. # if (defined SIZEOF_LONG_LONG) && (SIZEOF_LONG_LONG >= 8)
  30. # define PRIiMAX "lli"
  31. # define PRIuMAX "llu"
  32. # else
  33. # define PRIiMAX "li"
  34. # define PRIuMAX "lu"
  35. # endif
  36. #endif
  37. static void
  38. test_1 (const char *str, scm_t_intmax min, scm_t_intmax max,
  39. int result)
  40. {
  41. int r = scm_is_signed_integer (scm_c_eval_string (str), min, max);
  42. if (r != result)
  43. {
  44. fprintf (stderr, "fail: scm_is_signed_integer (%s, "
  45. "%" PRIiMAX ", %" PRIiMAX ") == %d\n",
  46. str, min, max, result);
  47. exit (EXIT_FAILURE);
  48. }
  49. }
  50. static void
  51. test_is_signed_integer ()
  52. {
  53. test_1 ("'foo",
  54. SCM_T_INTMAX_MIN, SCM_T_INTMAX_MAX,
  55. 0);
  56. test_1 ("3.0",
  57. SCM_T_INTMAX_MIN, SCM_T_INTMAX_MAX,
  58. 0);
  59. test_1 ("(inexact->exact 3.0)",
  60. SCM_T_INTMAX_MIN, SCM_T_INTMAX_MAX,
  61. 1);
  62. test_1 ("3.5",
  63. SCM_T_INTMAX_MIN, SCM_T_INTMAX_MAX,
  64. 0);
  65. test_1 ("most-positive-fixnum",
  66. SCM_T_INTMAX_MIN, SCM_T_INTMAX_MAX,
  67. 1);
  68. test_1 ("(+ most-positive-fixnum 1)",
  69. SCM_T_INTMAX_MIN, SCM_T_INTMAX_MAX,
  70. 1);
  71. test_1 ("most-negative-fixnum",
  72. SCM_T_INTMAX_MIN, SCM_T_INTMAX_MAX,
  73. 1);
  74. test_1 ("(- most-negative-fixnum 1)",
  75. SCM_T_INTMAX_MIN, SCM_T_INTMAX_MAX,
  76. 1);
  77. if (sizeof (scm_t_intmax) == 8)
  78. {
  79. test_1 ("(- (expt 2 63) 1)",
  80. SCM_T_INTMAX_MIN, SCM_T_INTMAX_MAX,
  81. 1);
  82. test_1 ("(expt 2 63)",
  83. SCM_T_INTMAX_MIN, SCM_T_INTMAX_MAX,
  84. 0);
  85. test_1 ("(- (expt 2 63))",
  86. SCM_T_INTMAX_MIN, SCM_T_INTMAX_MAX,
  87. 1);
  88. test_1 ("(- (- (expt 2 63)) 1)",
  89. SCM_T_INTMAX_MIN, SCM_T_INTMAX_MAX,
  90. 0);
  91. }
  92. else if (sizeof (scm_t_intmax) == 4)
  93. {
  94. test_1 ("(- (expt 2 31) 1)",
  95. SCM_T_INTMAX_MIN, SCM_T_INTMAX_MAX,
  96. 1);
  97. test_1 ("(expt 2 31)",
  98. SCM_T_INTMAX_MIN, SCM_T_INTMAX_MAX,
  99. 0);
  100. test_1 ("(- (expt 2 31))",
  101. SCM_T_INTMAX_MIN, SCM_T_INTMAX_MAX,
  102. 1);
  103. test_1 ("(- (- (expt 2 31)) 1)",
  104. SCM_T_INTMAX_MIN, SCM_T_INTMAX_MAX,
  105. 0);
  106. }
  107. else
  108. fprintf (stderr, "NOTE: skipped some tests.\n");
  109. /* bignum with range that fits into fixnum. */
  110. test_1 ("(+ most-positive-fixnum 1)",
  111. -32768, 32767,
  112. 0);
  113. /* bignum with range that doesn't fit into fixnum, but probably
  114. fits into long. */
  115. test_1 ("(+ most-positive-fixnum 1)",
  116. SCM_MOST_NEGATIVE_FIXNUM-1, SCM_MOST_POSITIVE_FIXNUM+1,
  117. 1);
  118. }
  119. static void
  120. test_2 (const char *str, scm_t_uintmax min, scm_t_uintmax max,
  121. int result)
  122. {
  123. int r = scm_is_unsigned_integer (scm_c_eval_string (str), min, max);
  124. if (r != result)
  125. {
  126. fprintf (stderr, "fail: scm_is_unsigned_integer (%s, "
  127. "%" PRIuMAX ", %" PRIuMAX ") == %d\n",
  128. str, min, max, result);
  129. exit (EXIT_FAILURE);
  130. }
  131. }
  132. static void
  133. test_is_unsigned_integer ()
  134. {
  135. test_2 ("'foo",
  136. 0, SCM_T_UINTMAX_MAX,
  137. 0);
  138. test_2 ("3.0",
  139. 0, SCM_T_UINTMAX_MAX,
  140. 0);
  141. test_2 ("(inexact->exact 3.0)",
  142. 0, SCM_T_UINTMAX_MAX,
  143. 1);
  144. test_2 ("3.5",
  145. 0, SCM_T_UINTMAX_MAX,
  146. 0);
  147. test_2 ("most-positive-fixnum",
  148. 0, SCM_T_UINTMAX_MAX,
  149. 1);
  150. test_2 ("(+ most-positive-fixnum 1)",
  151. 0, SCM_T_UINTMAX_MAX,
  152. 1);
  153. test_2 ("most-negative-fixnum",
  154. 0, SCM_T_UINTMAX_MAX,
  155. 0);
  156. test_2 ("(- most-negative-fixnum 1)",
  157. 0, SCM_T_UINTMAX_MAX,
  158. 0);
  159. if (sizeof (scm_t_intmax) == 8)
  160. {
  161. test_2 ("(- (expt 2 64) 1)",
  162. 0, SCM_T_UINTMAX_MAX,
  163. 1);
  164. test_2 ("(expt 2 64)",
  165. 0, SCM_T_UINTMAX_MAX,
  166. 0);
  167. }
  168. else if (sizeof (scm_t_intmax) == 4)
  169. {
  170. test_2 ("(- (expt 2 32) 1)",
  171. 0, SCM_T_UINTMAX_MAX,
  172. 1);
  173. test_2 ("(expt 2 32)",
  174. 0, SCM_T_UINTMAX_MAX,
  175. 0);
  176. }
  177. else
  178. fprintf (stderr, "NOTE: skipped some tests.\n");
  179. /* bignum with range that fits into fixnum. */
  180. test_2 ("(+ most-positive-fixnum 1)",
  181. 0, 32767,
  182. 0);
  183. /* bignum with range that doesn't fit into fixnum, but probably
  184. fits into long. */
  185. test_2 ("(+ most-positive-fixnum 1)",
  186. 0, SCM_MOST_POSITIVE_FIXNUM+1,
  187. 1);
  188. }
  189. typedef struct {
  190. SCM val;
  191. scm_t_intmax min, max;
  192. scm_t_intmax result;
  193. } to_signed_data;
  194. static SCM
  195. out_of_range_handler (void *data, SCM key, SCM args)
  196. {
  197. return scm_equal_p (key, scm_from_locale_symbol ("out-of-range"));
  198. }
  199. static SCM
  200. wrong_type_handler (void *data, SCM key, SCM args)
  201. {
  202. return scm_equal_p (key, scm_from_locale_symbol ("wrong-type-arg"));
  203. }
  204. static SCM
  205. misc_error_handler (void *data, SCM key, SCM args)
  206. {
  207. return scm_equal_p (key, scm_from_locale_symbol ("misc-error"));
  208. }
  209. static SCM
  210. any_handler (void *data, SCM key, SCM args)
  211. {
  212. return SCM_BOOL_T;
  213. }
  214. static SCM
  215. to_signed_integer_body (void *data)
  216. {
  217. to_signed_data *d = (to_signed_data *)data;
  218. d->result = scm_to_signed_integer (d->val, d->min, d->max);
  219. return SCM_BOOL_F;
  220. }
  221. static void
  222. test_3 (const char *str, scm_t_intmax min, scm_t_intmax max,
  223. scm_t_intmax result, int range_error, int type_error)
  224. {
  225. to_signed_data data;
  226. data.val = scm_c_eval_string (str);
  227. data.min = min;
  228. data.max = max;
  229. if (range_error)
  230. {
  231. if (scm_is_false (scm_internal_catch (SCM_BOOL_T,
  232. to_signed_integer_body, &data,
  233. out_of_range_handler, NULL)))
  234. {
  235. fprintf (stderr,
  236. "fail: scm_to_signed_int (%s, "
  237. "%" PRIiMAX ", %" PRIiMAX ") -> out of range\n",
  238. str, min, max);
  239. exit (EXIT_FAILURE);
  240. }
  241. }
  242. else if (type_error)
  243. {
  244. if (scm_is_false (scm_internal_catch (SCM_BOOL_T,
  245. to_signed_integer_body, &data,
  246. wrong_type_handler, NULL)))
  247. {
  248. fprintf (stderr,
  249. "fail: scm_to_signed_int (%s, "
  250. "%" PRIiMAX", %" PRIiMAX ") -> wrong type\n",
  251. str, min, max);
  252. exit (EXIT_FAILURE);
  253. }
  254. }
  255. else
  256. {
  257. if (scm_is_true (scm_internal_catch (SCM_BOOL_T,
  258. to_signed_integer_body, &data,
  259. any_handler, NULL))
  260. || data.result != result)
  261. {
  262. fprintf (stderr,
  263. "fail: scm_to_signed_int (%s, "
  264. "%" PRIiMAX ", %" PRIiMAX ") = %" PRIiMAX "\n",
  265. str, min, max, result);
  266. exit (EXIT_FAILURE);
  267. }
  268. }
  269. }
  270. static void
  271. test_to_signed_integer ()
  272. {
  273. test_3 ("'foo",
  274. SCM_T_INTMAX_MIN, SCM_T_INTMAX_MAX,
  275. 0, 0, 1);
  276. test_3 ("3.5",
  277. SCM_T_INTMAX_MIN, SCM_T_INTMAX_MAX,
  278. 0, 0, 1);
  279. test_3 ("12",
  280. SCM_T_INTMAX_MIN, SCM_T_INTMAX_MAX,
  281. 12, 0, 0);
  282. test_3 ("1000",
  283. -999, 999,
  284. 0, 1, 0);
  285. test_3 ("-1000",
  286. -999, 999,
  287. 0, 1, 0);
  288. test_3 ("most-positive-fixnum",
  289. SCM_T_INTMAX_MIN, SCM_T_INTMAX_MAX,
  290. SCM_MOST_POSITIVE_FIXNUM, 0, 0);
  291. test_3 ("most-negative-fixnum",
  292. SCM_T_INTMAX_MIN, SCM_T_INTMAX_MAX,
  293. SCM_MOST_NEGATIVE_FIXNUM, 0, 0);
  294. test_3 ("(+ most-positive-fixnum 1)",
  295. SCM_T_INTMAX_MIN, SCM_T_INTMAX_MAX,
  296. SCM_MOST_POSITIVE_FIXNUM+1, 0, 0);
  297. test_3 ("(- most-negative-fixnum 1)",
  298. SCM_T_INTMAX_MIN, SCM_T_INTMAX_MAX,
  299. SCM_MOST_NEGATIVE_FIXNUM-1, 0, 0);
  300. if (sizeof (scm_t_intmax) == 8)
  301. {
  302. test_3 ("(- (expt 2 63) 1)",
  303. SCM_T_INTMAX_MIN, SCM_T_INTMAX_MAX,
  304. SCM_T_INTMAX_MAX, 0, 0);
  305. test_3 ("(+ (- (expt 2 63)) 1)",
  306. SCM_T_INTMAX_MIN, SCM_T_INTMAX_MAX,
  307. SCM_T_INTMAX_MIN+1, 0, 0);
  308. test_3 ("(- (expt 2 63))",
  309. SCM_T_INTMAX_MIN, SCM_T_INTMAX_MAX,
  310. SCM_T_INTMAX_MIN, 0, 0);
  311. test_3 ("(expt 2 63)",
  312. SCM_T_INTMAX_MIN, SCM_T_INTMAX_MAX,
  313. 0, 1, 0);
  314. test_3 ("(- (- (expt 2 63)) 1)",
  315. SCM_T_INTMAX_MIN, SCM_T_INTMAX_MAX,
  316. 0, 1, 0);
  317. }
  318. else if (sizeof (scm_t_intmax) == 4)
  319. {
  320. test_3 ("(- (expt 2 31) 1)",
  321. SCM_T_INTMAX_MIN, SCM_T_INTMAX_MAX,
  322. SCM_T_INTMAX_MAX, 0, 0);
  323. test_3 ("(+ (- (expt 2 31)) 1)",
  324. SCM_T_INTMAX_MIN, SCM_T_INTMAX_MAX,
  325. SCM_T_INTMAX_MIN+1, 0, 0);
  326. test_3 ("(- (expt 2 31))",
  327. SCM_T_INTMAX_MIN, SCM_T_INTMAX_MAX,
  328. SCM_T_INTMAX_MIN, 0, 0);
  329. test_3 ("(expt 2 31)",
  330. SCM_T_INTMAX_MIN, SCM_T_INTMAX_MAX,
  331. 0, 1, 0);
  332. test_3 ("(- (- (expt 2 31)) 1)",
  333. SCM_T_INTMAX_MIN, SCM_T_INTMAX_MAX,
  334. 0, 1, 0);
  335. }
  336. else
  337. fprintf (stderr, "NOTE: skipped some tests.\n");
  338. }
  339. typedef struct {
  340. SCM val;
  341. scm_t_uintmax min, max;
  342. scm_t_uintmax result;
  343. } to_unsigned_data;
  344. static SCM
  345. to_unsigned_integer_body (void *data)
  346. {
  347. to_unsigned_data *d = (to_unsigned_data *)data;
  348. d->result = scm_to_unsigned_integer (d->val, d->min, d->max);
  349. return SCM_BOOL_F;
  350. }
  351. static void
  352. test_4 (const char *str, scm_t_uintmax min, scm_t_uintmax max,
  353. scm_t_uintmax result, int range_error, int type_error)
  354. {
  355. to_unsigned_data data;
  356. data.val = scm_c_eval_string (str);
  357. data.min = min;
  358. data.max = max;
  359. if (range_error)
  360. {
  361. if (scm_is_false (scm_internal_catch (SCM_BOOL_T,
  362. to_unsigned_integer_body, &data,
  363. out_of_range_handler, NULL)))
  364. {
  365. fprintf (stderr,
  366. "fail: scm_to_unsigned_int (%s, "
  367. "%" PRIuMAX ", %" PRIuMAX ") -> out of range\n",
  368. str, min, max);
  369. exit (EXIT_FAILURE);
  370. }
  371. }
  372. else if (type_error)
  373. {
  374. if (scm_is_false (scm_internal_catch (SCM_BOOL_T,
  375. to_unsigned_integer_body, &data,
  376. wrong_type_handler, NULL)))
  377. {
  378. fprintf (stderr,
  379. "fail: scm_to_unsigned_int (%s, "
  380. "%" PRIuMAX ", %" PRIuMAX ") -> wrong type\n",
  381. str, min, max);
  382. exit (EXIT_FAILURE);
  383. }
  384. }
  385. else
  386. {
  387. if (scm_is_true (scm_internal_catch (SCM_BOOL_T,
  388. to_unsigned_integer_body, &data,
  389. any_handler, NULL))
  390. || data.result != result)
  391. {
  392. fprintf (stderr,
  393. "fail: scm_to_unsigned_int (%s, "
  394. "%" PRIuMAX ", %" PRIuMAX ") == %" PRIuMAX "\n",
  395. str, min, max, result);
  396. exit (EXIT_FAILURE);
  397. }
  398. }
  399. }
  400. static void
  401. test_to_unsigned_integer ()
  402. {
  403. test_4 ("'foo",
  404. 0, SCM_T_UINTMAX_MAX,
  405. 0, 0, 1);
  406. test_4 ("3.5",
  407. 0, SCM_T_UINTMAX_MAX,
  408. 0, 0, 1);
  409. test_4 ("12",
  410. 0, SCM_T_UINTMAX_MAX,
  411. 12, 0, 0);
  412. test_4 ("1000",
  413. 0, 999,
  414. 0, 1, 0);
  415. test_4 ("most-positive-fixnum",
  416. 0, SCM_T_UINTMAX_MAX,
  417. SCM_MOST_POSITIVE_FIXNUM, 0, 0);
  418. test_4 ("(+ most-positive-fixnum 1)",
  419. 0, SCM_T_UINTMAX_MAX,
  420. SCM_MOST_POSITIVE_FIXNUM+1, 0, 0);
  421. if (sizeof (scm_t_intmax) == 8)
  422. {
  423. test_4 ("(- (expt 2 64) 1)",
  424. 0, SCM_T_UINTMAX_MAX,
  425. SCM_T_UINTMAX_MAX, 0, 0);
  426. test_4 ("(expt 2 64)",
  427. 0, SCM_T_UINTMAX_MAX,
  428. 0, 1, 0);
  429. }
  430. else if (sizeof (scm_t_intmax) == 4)
  431. {
  432. test_4 ("(- (expt 2 32) 1)",
  433. 0, SCM_T_UINTMAX_MAX,
  434. SCM_T_UINTMAX_MAX, 0, 0);
  435. test_4 ("(expt 2 32)",
  436. 0, SCM_T_UINTMAX_MAX,
  437. 0, 1, 0);
  438. }
  439. else
  440. fprintf (stderr, "NOTE: skipped some tests.\n");
  441. }
  442. static void
  443. test_5 (scm_t_intmax val, const char *result)
  444. {
  445. SCM res = scm_c_eval_string (result);
  446. if (scm_is_false (scm_equal_p (scm_from_signed_integer (val), res)))
  447. {
  448. fprintf (stderr, "fail: scm_from_signed_integer (%" PRIiMAX ") == %s\n",
  449. val, result);
  450. exit (EXIT_FAILURE);
  451. }
  452. }
  453. static void
  454. test_from_signed_integer ()
  455. {
  456. test_5 (12, "12");
  457. if (sizeof (scm_t_intmax) == 8)
  458. {
  459. test_5 (SCM_T_INTMAX_MAX, "(- (expt 2 63) 1)");
  460. test_5 (SCM_T_INTMAX_MIN, "(- (expt 2 63))");
  461. }
  462. else if (sizeof (scm_t_intmax) == 4)
  463. {
  464. test_5 (SCM_T_INTMAX_MAX, "(- (expt 2 31) 1)");
  465. test_5 (SCM_T_INTMAX_MIN, "(- (expt 2 31))");
  466. }
  467. test_5 (SCM_MOST_POSITIVE_FIXNUM, "most-positive-fixnum");
  468. test_5 (SCM_MOST_NEGATIVE_FIXNUM, "most-negative-fixnum");
  469. test_5 (SCM_MOST_POSITIVE_FIXNUM+1, "(+ most-positive-fixnum 1)");
  470. test_5 (SCM_MOST_NEGATIVE_FIXNUM-1, "(- most-negative-fixnum 1)");
  471. }
  472. static void
  473. test_6 (scm_t_uintmax val, const char *result)
  474. {
  475. SCM res = scm_c_eval_string (result);
  476. if (scm_is_false (scm_equal_p (scm_from_unsigned_integer (val), res)))
  477. {
  478. fprintf (stderr, "fail: scm_from_unsigned_integer (%"
  479. PRIuMAX ") == %s\n",
  480. val, result);
  481. exit (EXIT_FAILURE);
  482. }
  483. }
  484. static void
  485. test_from_unsigned_integer ()
  486. {
  487. test_6 (12, "12");
  488. if (sizeof (scm_t_intmax) == 8)
  489. {
  490. test_6 (SCM_T_UINTMAX_MAX, "(- (expt 2 64) 1)");
  491. }
  492. else if (sizeof (scm_t_intmax) == 4)
  493. {
  494. test_6 (SCM_T_UINTMAX_MAX, "(- (expt 2 32) 1)");
  495. }
  496. test_6 (SCM_MOST_POSITIVE_FIXNUM, "most-positive-fixnum");
  497. test_6 (SCM_MOST_POSITIVE_FIXNUM+1, "(+ most-positive-fixnum 1)");
  498. }
  499. static void
  500. test_7s (SCM n, scm_t_intmax c_n, const char *result, const char *func)
  501. {
  502. SCM r = scm_c_eval_string (result);
  503. if (scm_is_false (scm_equal_p (n, r)))
  504. {
  505. fprintf (stderr, "fail: %s (%" PRIiMAX ") == %s\n", func, c_n, result);
  506. exit (EXIT_FAILURE);
  507. }
  508. }
  509. #define TEST_7S(func,arg,res) test_7s (func(arg), arg, res, #func)
  510. static void
  511. test_7u (SCM n, scm_t_uintmax c_n, const char *result, const char *func)
  512. {
  513. SCM r = scm_c_eval_string (result);
  514. if (scm_is_false (scm_equal_p (n, r)))
  515. {
  516. fprintf (stderr, "fail: %s (%" PRIuMAX ") == %s\n", func, c_n, result);
  517. exit (EXIT_FAILURE);
  518. }
  519. }
  520. #define TEST_7U(func,arg,res) test_7u (func(arg), arg, res, #func)
  521. typedef struct {
  522. SCM val;
  523. scm_t_intmax (*func) (SCM);
  524. scm_t_intmax result;
  525. } to_signed_func_data;
  526. static SCM
  527. to_signed_func_body (void *data)
  528. {
  529. to_signed_func_data *d = (to_signed_func_data *)data;
  530. d->result = d->func (d->val);
  531. return SCM_BOOL_F;
  532. }
  533. static void
  534. test_8s (const char *str, scm_t_intmax (*func) (SCM), const char *func_name,
  535. scm_t_intmax result, int range_error, int type_error)
  536. {
  537. to_signed_func_data data;
  538. data.val = scm_c_eval_string (str);
  539. data.func = func;
  540. if (range_error)
  541. {
  542. if (scm_is_false (scm_internal_catch (SCM_BOOL_T,
  543. to_signed_func_body, &data,
  544. out_of_range_handler, NULL)))
  545. {
  546. fprintf (stderr,
  547. "fail: %s (%s) -> out of range\n", func_name, str);
  548. exit (EXIT_FAILURE);
  549. }
  550. }
  551. else if (type_error)
  552. {
  553. if (scm_is_false (scm_internal_catch (SCM_BOOL_T,
  554. to_signed_func_body, &data,
  555. wrong_type_handler, NULL)))
  556. {
  557. fprintf (stderr,
  558. "fail: %s (%s) -> wrong type\n", func_name, str);
  559. exit (EXIT_FAILURE);
  560. }
  561. }
  562. else
  563. {
  564. if (scm_is_true (scm_internal_catch (SCM_BOOL_T,
  565. to_signed_func_body, &data,
  566. any_handler, NULL))
  567. || data.result != result)
  568. {
  569. fprintf (stderr,
  570. "fail: %s (%s) = %" PRIiMAX "\n", func_name, str, result);
  571. exit (EXIT_FAILURE);
  572. }
  573. }
  574. }
  575. typedef struct {
  576. SCM val;
  577. scm_t_uintmax (*func) (SCM);
  578. scm_t_uintmax result;
  579. } to_unsigned_func_data;
  580. static SCM
  581. to_unsigned_func_body (void *data)
  582. {
  583. to_unsigned_func_data *d = (to_unsigned_func_data *)data;
  584. d->result = d->func (d->val);
  585. return SCM_BOOL_F;
  586. }
  587. static void
  588. test_8u (const char *str, scm_t_uintmax (*func) (SCM), const char *func_name,
  589. scm_t_uintmax result, int range_error, int type_error)
  590. {
  591. to_unsigned_func_data data;
  592. data.val = scm_c_eval_string (str);
  593. data.func = func;
  594. if (range_error)
  595. {
  596. if (scm_is_false (scm_internal_catch (SCM_BOOL_T,
  597. to_unsigned_func_body, &data,
  598. out_of_range_handler, NULL)))
  599. {
  600. fprintf (stderr,
  601. "fail: %s (%s) -> out of range\n", func_name, str);
  602. exit (EXIT_FAILURE);
  603. }
  604. }
  605. else if (type_error)
  606. {
  607. if (scm_is_false (scm_internal_catch (SCM_BOOL_T,
  608. to_unsigned_func_body, &data,
  609. wrong_type_handler, NULL)))
  610. {
  611. fprintf (stderr,
  612. "fail: %s (%s) -> wrong type\n", func_name, str);
  613. exit (EXIT_FAILURE);
  614. }
  615. }
  616. else
  617. {
  618. if (scm_is_true (scm_internal_catch (SCM_BOOL_T,
  619. to_unsigned_func_body, &data,
  620. any_handler, NULL))
  621. || data.result != result)
  622. {
  623. fprintf (stderr,
  624. "fail: %s (%s) = %" PRIiMAX "\n", func_name, str, result);
  625. exit (EXIT_FAILURE);
  626. }
  627. }
  628. }
  629. /* We can't rely on the scm_to functions being proper functions but we
  630. want to pass them to test_8s and test_8u, so we wrap'em. Also, we
  631. need to give them a common return type.
  632. */
  633. #define DEFSTST(f) static scm_t_intmax tst_##f (SCM x) { return f(x); }
  634. #define DEFUTST(f) static scm_t_uintmax tst_##f (SCM x) { return f(x); }
  635. DEFSTST (scm_to_schar)
  636. DEFUTST (scm_to_uchar)
  637. DEFSTST (scm_to_char)
  638. DEFSTST (scm_to_short)
  639. DEFUTST (scm_to_ushort)
  640. DEFSTST (scm_to_int)
  641. DEFUTST (scm_to_uint)
  642. DEFSTST (scm_to_long)
  643. DEFUTST (scm_to_ulong)
  644. #if SCM_SIZEOF_LONG_LONG != 0
  645. DEFSTST (scm_to_long_long)
  646. DEFUTST (scm_to_ulong_long)
  647. #endif
  648. DEFSTST (scm_to_ssize_t)
  649. DEFUTST (scm_to_size_t)
  650. DEFSTST (scm_to_int8)
  651. DEFUTST (scm_to_uint8)
  652. DEFSTST (scm_to_int16)
  653. DEFUTST (scm_to_uint16)
  654. DEFSTST (scm_to_int32)
  655. DEFUTST (scm_to_uint32)
  656. DEFSTST (scm_to_int64)
  657. DEFUTST (scm_to_uint64)
  658. #define TEST_8S(v,f,r,re,te) test_8s (v, tst_##f, #f, r, re, te)
  659. #define TEST_8U(v,f,r,re,te) test_8u (v, tst_##f, #f, r, re, te)
  660. static void
  661. test_int_sizes ()
  662. {
  663. TEST_7U (scm_from_uchar, 91, "91");
  664. TEST_7S (scm_from_schar, 91, "91");
  665. TEST_7S (scm_from_char, 91, "91");
  666. TEST_7S (scm_from_short, -911, "-911");
  667. TEST_7U (scm_from_ushort, 911, "911");
  668. TEST_7S (scm_from_int, 911, "911");
  669. TEST_7U (scm_from_uint, 911, "911");
  670. TEST_7S (scm_from_long, 911, "911");
  671. TEST_7U (scm_from_ulong, 911, "911");
  672. #if SCM_SIZEOF_LONG_LONG != 0
  673. TEST_7S (scm_from_long_long, 911, "911");
  674. TEST_7U (scm_from_ulong_long, 911, "911");
  675. #endif
  676. TEST_7U (scm_from_size_t, 911, "911");
  677. TEST_7S (scm_from_ssize_t, 911, "911");
  678. TEST_7S (scm_from_int8, -128, "-128");
  679. TEST_7S (scm_from_int8, 127, "127");
  680. TEST_7S (scm_from_int8, 128, "-128");
  681. TEST_7U (scm_from_uint8, 255, "255");
  682. TEST_7S (scm_from_int16, -32768, "-32768");
  683. TEST_7S (scm_from_int16, 32767, "32767");
  684. TEST_7S (scm_from_int16, 32768, "-32768");
  685. TEST_7U (scm_from_uint16, 65535, "65535");
  686. TEST_7S (scm_from_int32, SCM_T_INT32_MIN, "-2147483648");
  687. TEST_7S (scm_from_int32, SCM_T_INT32_MAX, "2147483647");
  688. TEST_7S (scm_from_int32, SCM_T_INT32_MAX+1LL, "-2147483648");
  689. TEST_7U (scm_from_uint32, SCM_T_UINT32_MAX, "4294967295");
  690. TEST_7S (scm_from_int64, SCM_T_INT64_MIN, "-9223372036854775808");
  691. TEST_7S (scm_from_int64, SCM_T_INT64_MAX, "9223372036854775807");
  692. TEST_7U (scm_from_uint64, SCM_T_UINT64_MAX, "18446744073709551615");
  693. TEST_8S ("91", scm_to_schar, 91, 0, 0);
  694. TEST_8U ("91", scm_to_uchar, 91, 0, 0);
  695. TEST_8S ("91", scm_to_char, 91, 0, 0);
  696. TEST_8S ("-911", scm_to_short, -911, 0, 0);
  697. TEST_8U ("911", scm_to_ushort, 911, 0, 0);
  698. TEST_8S ("-911", scm_to_int, -911, 0, 0);
  699. TEST_8U ("911", scm_to_uint, 911, 0, 0);
  700. TEST_8S ("-911", scm_to_long, -911, 0, 0);
  701. TEST_8U ("911", scm_to_ulong, 911, 0, 0);
  702. #if SCM_SIZEOF_LONG_LONG != 0
  703. TEST_8S ("-911", scm_to_long_long, -911, 0, 0);
  704. TEST_8U ("911", scm_to_ulong_long, 911, 0, 0);
  705. #endif
  706. TEST_8U ("911", scm_to_size_t, 911, 0, 0);
  707. TEST_8S ("911", scm_to_ssize_t, 911, 0, 0);
  708. TEST_8S ("-128", scm_to_int8, SCM_T_INT8_MIN, 0, 0);
  709. TEST_8S ("127", scm_to_int8, SCM_T_INT8_MAX, 0, 0);
  710. TEST_8S ("128", scm_to_int8, 0, 1, 0);
  711. TEST_8S ("#f", scm_to_int8, 0, 0, 1);
  712. TEST_8U ("255", scm_to_uint8, SCM_T_UINT8_MAX, 0, 0);
  713. TEST_8U ("256", scm_to_uint8, 0, 1, 0);
  714. TEST_8U ("-1", scm_to_uint8, 0, 1, 0);
  715. TEST_8U ("#f", scm_to_uint8, 0, 0, 1);
  716. TEST_8S ("-32768", scm_to_int16, SCM_T_INT16_MIN, 0, 0);
  717. TEST_8S ("32767", scm_to_int16, SCM_T_INT16_MAX, 0, 0);
  718. TEST_8S ("32768", scm_to_int16, 0, 1, 0);
  719. TEST_8S ("#f", scm_to_int16, 0, 0, 1);
  720. TEST_8U ("65535", scm_to_uint16, SCM_T_UINT16_MAX, 0, 0);
  721. TEST_8U ("65536", scm_to_uint16, 0, 1, 0);
  722. TEST_8U ("-1", scm_to_uint16, 0, 1, 0);
  723. TEST_8U ("#f", scm_to_uint16, 0, 0, 1);
  724. TEST_8S ("-2147483648", scm_to_int32, SCM_T_INT32_MIN, 0, 0);
  725. TEST_8S ("2147483647", scm_to_int32, SCM_T_INT32_MAX, 0, 0);
  726. TEST_8S ("2147483648", scm_to_int32, 0, 1, 0);
  727. TEST_8S ("#f", scm_to_int32, 0, 0, 1);
  728. TEST_8U ("4294967295", scm_to_uint32, SCM_T_UINT32_MAX, 0, 0);
  729. TEST_8U ("4294967296", scm_to_uint32, 0, 1, 0);
  730. TEST_8U ("-1", scm_to_uint32, 0, 1, 0);
  731. TEST_8U ("#f", scm_to_uint32, 0, 0, 1);
  732. TEST_8S ("-9223372036854775808", scm_to_int64, SCM_T_INT64_MIN, 0, 0);
  733. TEST_8S ("9223372036854775807", scm_to_int64, SCM_T_INT64_MAX, 0, 0);
  734. TEST_8S ("9223372036854775808", scm_to_int64, 0, 1, 0);
  735. TEST_8S ("#f", scm_to_int64, 0, 0, 1);
  736. TEST_8U ("18446744073709551615", scm_to_uint64, SCM_T_UINT64_MAX, 0, 0);
  737. TEST_8U ("18446744073709551616", scm_to_uint64, 0, 1, 0);
  738. TEST_8U ("-1", scm_to_uint64, 0, 1, 0);
  739. TEST_8U ("#f", scm_to_uint64, 0, 0, 1);
  740. }
  741. static void
  742. test_9 (double val, const char *result)
  743. {
  744. SCM res = scm_c_eval_string (result);
  745. if (scm_is_false (scm_eqv_p (res, scm_from_double (val))))
  746. {
  747. fprintf (stderr, "fail: scm_from_double (%g) == %s\n", val, result);
  748. exit (EXIT_FAILURE);
  749. }
  750. }
  751. /* The `infinity' and `not-a-number' values. */
  752. static double guile_Inf, guile_NaN;
  753. /* Initialize GUILE_INF and GUILE_NAN. Taken from `guile_ieee_init ()' in
  754. `libguile/numbers.c'. */
  755. static void
  756. ieee_init (void)
  757. {
  758. #ifdef INFINITY
  759. /* C99 INFINITY, when available.
  760. FIXME: The standard allows for INFINITY to be something that overflows
  761. at compile time. We ought to have a configure test to check for that
  762. before trying to use it. (But in practice we believe this is not a
  763. problem on any system guile is likely to target.) */
  764. guile_Inf = INFINITY;
  765. #elif defined HAVE_DINFINITY
  766. /* OSF */
  767. extern unsigned int DINFINITY[2];
  768. guile_Inf = (*((double *) (DINFINITY)));
  769. #else
  770. double tmp = 1e+10;
  771. guile_Inf = tmp;
  772. for (;;)
  773. {
  774. guile_Inf *= 1e+10;
  775. if (guile_Inf == tmp)
  776. break;
  777. tmp = guile_Inf;
  778. }
  779. #endif
  780. #ifdef NAN
  781. /* C99 NAN, when available */
  782. guile_NaN = NAN;
  783. #elif defined HAVE_DQNAN
  784. {
  785. /* OSF */
  786. extern unsigned int DQNAN[2];
  787. guile_NaN = (*((double *)(DQNAN)));
  788. }
  789. #else
  790. guile_NaN = guile_Inf / guile_Inf;
  791. #endif
  792. }
  793. static void
  794. test_from_double ()
  795. {
  796. test_9 (12, "12.0");
  797. test_9 (0.25, "0.25");
  798. test_9 (0.1, "0.1");
  799. test_9 (guile_Inf, "+inf.0");
  800. test_9 (-guile_Inf, "-inf.0");
  801. test_9 (guile_NaN, "+nan.0");
  802. }
  803. typedef struct {
  804. SCM val;
  805. double result;
  806. } to_double_data;
  807. static SCM
  808. to_double_body (void *data)
  809. {
  810. to_double_data *d = (to_double_data *)data;
  811. d->result = scm_to_double (d->val);
  812. return SCM_BOOL_F;
  813. }
  814. static void
  815. test_10 (const char *val, double result, int type_error)
  816. {
  817. to_double_data data;
  818. data.val = scm_c_eval_string (val);
  819. if (type_error)
  820. {
  821. if (scm_is_false (scm_internal_catch (SCM_BOOL_T,
  822. to_double_body, &data,
  823. wrong_type_handler, NULL)))
  824. {
  825. fprintf (stderr,
  826. "fail: scm_double (%s) -> wrong type\n", val);
  827. exit (EXIT_FAILURE);
  828. }
  829. }
  830. else
  831. {
  832. if (scm_is_true (scm_internal_catch (SCM_BOOL_T,
  833. to_double_body, &data,
  834. any_handler, NULL))
  835. || data.result != result)
  836. {
  837. fprintf (stderr,
  838. "fail: scm_to_double (%s) = %g\n", val, result);
  839. exit (EXIT_FAILURE);
  840. }
  841. }
  842. }
  843. static void
  844. test_to_double ()
  845. {
  846. test_10 ("#f", 0.0, 1);
  847. test_10 ("12", 12.0, 0);
  848. test_10 ("0.25", 0.25, 0);
  849. test_10 ("1/4", 0.25, 0);
  850. test_10 ("+inf.0", guile_Inf, 0);
  851. test_10 ("-inf.0",-guile_Inf, 0);
  852. test_10 ("+1i", 0.0, 1);
  853. }
  854. typedef struct {
  855. SCM val;
  856. char *result;
  857. } to_locale_string_data;
  858. static SCM
  859. to_locale_string_body (void *data)
  860. {
  861. to_locale_string_data *d = (to_locale_string_data *)data;
  862. d->result = scm_to_locale_string (d->val);
  863. return SCM_BOOL_F;
  864. }
  865. static void
  866. test_11 (const char *str, const char *result, int misc_error, int type_error)
  867. {
  868. to_locale_string_data data;
  869. data.val = scm_c_eval_string (str);
  870. data.result = NULL;
  871. if (misc_error)
  872. {
  873. if (scm_is_false (scm_internal_catch (SCM_BOOL_T,
  874. to_locale_string_body, &data,
  875. misc_error_handler, NULL)))
  876. {
  877. fprintf (stderr,
  878. "fail: scm_to_locale_string (%s) -> misc error\n", str);
  879. exit (EXIT_FAILURE);
  880. }
  881. }
  882. else if (type_error)
  883. {
  884. if (scm_is_false (scm_internal_catch (SCM_BOOL_T,
  885. to_locale_string_body, &data,
  886. wrong_type_handler, NULL)))
  887. {
  888. fprintf (stderr,
  889. "fail: scm_to_locale_string (%s) -> wrong type\n", str);
  890. exit (EXIT_FAILURE);
  891. }
  892. }
  893. else
  894. {
  895. if (scm_is_true (scm_internal_catch (SCM_BOOL_T,
  896. to_locale_string_body, &data,
  897. any_handler, NULL))
  898. || data.result == NULL || strcmp (data.result, result))
  899. {
  900. fprintf (stderr,
  901. "fail: scm_to_locale_string (%s) = %s\n", str, result);
  902. exit (EXIT_FAILURE);
  903. }
  904. }
  905. free (data.result);
  906. }
  907. static void
  908. test_locale_strings ()
  909. {
  910. const char *lstr = "This is not a string.";
  911. char *lstr2;
  912. SCM str, str2;
  913. char buf[20];
  914. size_t len;
  915. if (!scm_is_string (scm_c_eval_string ("\"foo\"")))
  916. {
  917. fprintf (stderr, "fail: scm_is_string (\"foo\") = true\n");
  918. exit (EXIT_FAILURE);
  919. }
  920. str = scm_from_locale_string (lstr);
  921. if (!scm_is_string (str))
  922. {
  923. fprintf (stderr, "fail: scm_is_string (str) = true\n");
  924. exit (EXIT_FAILURE);
  925. }
  926. lstr2 = scm_to_locale_string (str);
  927. if (strcmp (lstr, lstr2))
  928. {
  929. fprintf (stderr, "fail: lstr = lstr2\n");
  930. exit (EXIT_FAILURE);
  931. }
  932. free (lstr2);
  933. buf[15] = 'x';
  934. len = scm_to_locale_stringbuf (str, buf, 15);
  935. if (len != strlen (lstr))
  936. {
  937. fprintf (stderr, "fail: scm_to_locale_stringbuf (...) = strlen(lstr)\n");
  938. exit (EXIT_FAILURE);
  939. }
  940. if (buf[15] != 'x')
  941. {
  942. fprintf (stderr, "fail: scm_to_locale_stringbuf (...) no overrun\n");
  943. exit (EXIT_FAILURE);
  944. }
  945. if (strncmp (lstr, buf, 15))
  946. {
  947. fprintf (stderr, "fail: scm_to_locale_stringbuf (...) = lstr\n");
  948. exit (EXIT_FAILURE);
  949. }
  950. str2 = scm_from_locale_stringn (lstr, 10);
  951. if (!scm_is_string (str2))
  952. {
  953. fprintf (stderr, "fail: scm_is_string (str2) = true\n");
  954. exit (EXIT_FAILURE);
  955. }
  956. lstr2 = scm_to_locale_string (str2);
  957. if (strncmp (lstr, lstr2, 10))
  958. {
  959. fprintf (stderr, "fail: lstr = lstr2\n");
  960. exit (EXIT_FAILURE);
  961. }
  962. free (lstr2);
  963. buf[10] = 'x';
  964. len = scm_to_locale_stringbuf (str2, buf, 20);
  965. if (len != 10)
  966. {
  967. fprintf (stderr, "fail: scm_to_locale_stringbuf (...) = 10\n");
  968. exit (EXIT_FAILURE);
  969. }
  970. if (buf[10] != 'x')
  971. {
  972. fprintf (stderr, "fail: scm_to_locale_stringbuf (...) no overrun\n");
  973. exit (EXIT_FAILURE);
  974. }
  975. if (strncmp (lstr, buf, 10))
  976. {
  977. fprintf (stderr, "fail: scm_to_locale_stringbuf (...) = lstr\n");
  978. exit (EXIT_FAILURE);
  979. }
  980. lstr2 = scm_to_locale_stringn (str2, &len);
  981. if (len != 10)
  982. {
  983. fprintf (stderr, "fail: scm_to_locale_stringn, len = 10\n");
  984. exit (EXIT_FAILURE);
  985. }
  986. test_11 ("#f", NULL, 0, 1);
  987. test_11 ("\"foo\"", "foo", 0, 0);
  988. test_11 ("(string #\\f #\\nul)", NULL, 1, 0);
  989. }
  990. static void
  991. test_to_utf8_stringn ()
  992. {
  993. scm_t_wchar wstr[] = { 0x20, /* 0x20 */
  994. 0xDF, /* 0xC3, 0x9F */
  995. 0x65E5, /* 0xE6, 0x97, 0xA5 */
  996. 0x1D400 }; /* 0xF0, 0x9D, 0x90, 0x80 */
  997. SCM str0 = scm_from_utf32_stringn (wstr, 1); /* ASCII */
  998. SCM str1 = scm_from_utf32_stringn (wstr, 2); /* Narrow */
  999. SCM str2 = scm_from_utf32_stringn (wstr, 4); /* Wide */
  1000. char cstr0[] = { 0x20, 0 };
  1001. char cstr1[] = { 0x20, 0xC3, 0x9F, 0 };
  1002. char cstr2[] = { 0x20, 0xC3, 0x9F, 0xE6, 0x97, 0xA5,
  1003. 0xF0, 0x9D, 0x90, 0x80, 0 };
  1004. char *cstr;
  1005. size_t len;
  1006. /* Test conversion of ASCII string */
  1007. cstr = scm_to_utf8_stringn (str0, &len);
  1008. if (len + 1 != sizeof (cstr0) || memcmp (cstr, cstr0, len))
  1009. {
  1010. fprintf (stderr, "fail: scm_to_utf8_stringn (<ASCII>, &len)");
  1011. exit (EXIT_FAILURE);
  1012. }
  1013. free (cstr);
  1014. cstr = scm_to_utf8_stringn (str0, NULL);
  1015. if (memcmp (cstr, cstr0, len + 1))
  1016. {
  1017. fprintf (stderr, "fail: scm_to_utf8_stringn (<ASCII>, NULL)");
  1018. exit (EXIT_FAILURE);
  1019. }
  1020. free (cstr);
  1021. /* Test conversion of narrow string */
  1022. cstr = scm_to_utf8_stringn (str1, &len);
  1023. if (len + 1 != sizeof (cstr1) || memcmp (cstr, cstr1, len))
  1024. {
  1025. fprintf (stderr, "fail: scm_to_utf8_stringn (<NARROW>, &len)");
  1026. exit (EXIT_FAILURE);
  1027. }
  1028. free (cstr);
  1029. cstr = scm_to_utf8_stringn (str1, NULL);
  1030. if (memcmp (cstr, cstr1, len + 1))
  1031. {
  1032. fprintf (stderr, "fail: scm_to_utf8_stringn (<NARROW>, NULL)");
  1033. exit (EXIT_FAILURE);
  1034. }
  1035. free (cstr);
  1036. /* Test conversion of wide string */
  1037. cstr = scm_to_utf8_stringn (str2, &len);
  1038. if (len + 1 != sizeof (cstr2) || memcmp (cstr, cstr2, len))
  1039. {
  1040. fprintf (stderr, "fail: scm_to_utf8_stringn (<WIDE>, &len)");
  1041. exit (EXIT_FAILURE);
  1042. }
  1043. free (cstr);
  1044. cstr = scm_to_utf8_stringn (str2, NULL);
  1045. if (memcmp (cstr, cstr2, len + 1))
  1046. {
  1047. fprintf (stderr, "fail: scm_to_utf8_stringn (<WIDE>, NULL)");
  1048. exit (EXIT_FAILURE);
  1049. }
  1050. free (cstr);
  1051. }
  1052. static void
  1053. test_is_exact ()
  1054. {
  1055. if (1 != scm_is_exact (scm_c_eval_string ("3")))
  1056. {
  1057. fprintf (stderr, "fail: scm_is_exact (\"3\") = 1\n");
  1058. exit (EXIT_FAILURE);
  1059. }
  1060. if (0 != scm_is_exact (scm_c_eval_string ("3.0")))
  1061. {
  1062. fprintf (stderr, "fail: scm_is_exact (\"3.0\") = 0\n");
  1063. exit (EXIT_FAILURE);
  1064. }
  1065. }
  1066. static void
  1067. test_is_inexact ()
  1068. {
  1069. if (1 !=scm_is_inexact (scm_c_eval_string ("3.0")))
  1070. {
  1071. fprintf (stderr, "fail: scm_is_inexact (\"3.0\") = 1\n");
  1072. exit (EXIT_FAILURE);
  1073. }
  1074. if (0 != scm_is_inexact (scm_c_eval_string ("3")))
  1075. {
  1076. fprintf (stderr, "fail: scm_is_inexact (\"3\") = 0\n");
  1077. exit (EXIT_FAILURE);
  1078. }
  1079. }
  1080. static void
  1081. tests (void *data, int argc, char **argv)
  1082. {
  1083. test_is_signed_integer ();
  1084. test_is_unsigned_integer ();
  1085. test_to_signed_integer ();
  1086. test_to_unsigned_integer ();
  1087. test_from_signed_integer ();
  1088. test_from_unsigned_integer ();
  1089. test_int_sizes ();
  1090. test_from_double ();
  1091. test_to_double ();
  1092. test_locale_strings ();
  1093. test_to_utf8_stringn ();
  1094. test_is_exact ();
  1095. test_is_inexact ();
  1096. }
  1097. int
  1098. main (int argc, char *argv[])
  1099. {
  1100. ieee_init ();
  1101. scm_boot_guile (argc, argv, tests, NULL);
  1102. return 0;
  1103. }