test-num2integral.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /* Copyright (C) 1999,2000,2001,2003,2004, 2006, 2008 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
  5. * License as published by the Free Software Foundation; either
  6. * version 2.1 of the License, or (at your option) any later version.
  7. *
  8. * This library is distributed in the hope that it will be useful,
  9. * but 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 02110-1301 USA
  16. */
  17. #ifndef HAVE_CONFIG_H
  18. # include <config.h>
  19. #endif
  20. #include <libguile.h>
  21. #include <stdio.h>
  22. #include <assert.h>
  23. #if SCM_ENABLE_DISCOURAGED == 1
  24. SCM out_of_range_handler (void *data, SCM key, SCM args);
  25. SCM call_num2long_long_body (void *data);
  26. SCM call_num2ulong_long_body (void *data);
  27. /* expect to catch an `out-of-range' exception */
  28. SCM
  29. out_of_range_handler (void *data, SCM key, SCM args)
  30. {
  31. assert (scm_equal_p (key, scm_str2symbol ("out-of-range")));
  32. return SCM_BOOL_T;
  33. }
  34. SCM
  35. call_num2long_long_body (void *data)
  36. {
  37. scm_num2long_long (* (SCM *) data, SCM_ARG1, "call_num2long_long_body");
  38. return SCM_BOOL_F;
  39. }
  40. SCM
  41. call_num2ulong_long_body (void *data)
  42. {
  43. scm_num2ulong_long (* (SCM *) data, SCM_ARG1, "call_num2ulong_long_body");
  44. return SCM_BOOL_F;
  45. }
  46. static void
  47. test_long_long ()
  48. {
  49. #if SCM_SIZEOF_LONG_LONG != 0
  50. {
  51. SCM n = scm_long_long2num (SCM_I_LLONG_MIN);
  52. long long result = scm_num2long_long(n, 0, "main");
  53. assert (result == SCM_I_LLONG_MIN);
  54. }
  55. /* LLONG_MIN - 1 */
  56. {
  57. SCM n = scm_difference (scm_long_long2num (SCM_I_LLONG_MIN), scm_from_int (1));
  58. SCM caught = scm_internal_catch (SCM_BOOL_T, call_num2long_long_body, &n,
  59. out_of_range_handler, NULL);
  60. assert (scm_is_true (caught));
  61. }
  62. /* SCM_I_LLONG_MIN + SCM_I_LLONG_MIN/2 */
  63. {
  64. SCM n = scm_sum (scm_long_long2num (SCM_I_LLONG_MIN),
  65. scm_long_long2num (SCM_I_LLONG_MIN / 2));
  66. SCM caught = scm_internal_catch (SCM_BOOL_T, call_num2long_long_body, &n,
  67. out_of_range_handler, NULL);
  68. assert (scm_is_true (caught));
  69. }
  70. /* SCM_I_LLONG_MAX + 1 */
  71. {
  72. SCM n = scm_sum (scm_long_long2num (SCM_I_LLONG_MAX), scm_from_int (1));
  73. SCM caught = scm_internal_catch (SCM_BOOL_T, call_num2long_long_body, &n,
  74. out_of_range_handler, NULL);
  75. assert (scm_is_true (caught));
  76. }
  77. /* 2^1024 */
  78. {
  79. SCM n = scm_ash (scm_from_int (1), scm_from_int (1024));
  80. SCM caught = scm_internal_catch (SCM_BOOL_T, call_num2long_long_body, &n,
  81. out_of_range_handler, NULL);
  82. assert (scm_is_true (caught));
  83. }
  84. /* -2^1024 */
  85. {
  86. SCM n = scm_difference (scm_from_int (0),
  87. scm_ash (scm_from_int (1), scm_from_int (1024)));
  88. SCM caught = scm_internal_catch (SCM_BOOL_T, call_num2long_long_body, &n,
  89. out_of_range_handler, NULL);
  90. assert (scm_is_true (caught));
  91. }
  92. #endif /* SCM_SIZEOF_LONG_LONG != 0 */
  93. }
  94. static void
  95. test_ulong_long ()
  96. {
  97. #if SCM_SIZEOF_LONG_LONG != 0
  98. {
  99. SCM n = scm_ulong_long2num (SCM_I_ULLONG_MAX);
  100. unsigned long long result = scm_num2ulong_long(n, 0, "main");
  101. assert (result == SCM_I_ULLONG_MAX);
  102. }
  103. /* -1 */
  104. {
  105. SCM n = scm_from_int (-1);
  106. SCM caught = scm_internal_catch (SCM_BOOL_T, call_num2ulong_long_body, &n,
  107. out_of_range_handler, NULL);
  108. assert (scm_is_true (caught));
  109. }
  110. /* SCM_I_ULLONG_MAX + 1 */
  111. {
  112. SCM n = scm_sum (scm_ulong_long2num (SCM_I_ULLONG_MAX), scm_from_int (1));
  113. SCM caught = scm_internal_catch (SCM_BOOL_T, call_num2ulong_long_body, &n,
  114. out_of_range_handler, NULL);
  115. assert (scm_is_true (caught));
  116. }
  117. /* 2^1024 */
  118. {
  119. SCM n = scm_ash (scm_from_int (1), scm_from_int (1024));
  120. SCM caught = scm_internal_catch (SCM_BOOL_T, call_num2long_long_body, &n,
  121. out_of_range_handler, NULL);
  122. assert (scm_is_true (caught));
  123. }
  124. #endif /* SCM_SIZEOF_LONG_LONG != 0 */
  125. }
  126. static void
  127. tests (void *data, int argc, char **argv)
  128. {
  129. test_long_long ();
  130. test_ulong_long ();
  131. }
  132. int
  133. main (int argc, char *argv[])
  134. {
  135. scm_boot_guile (argc, argv, tests, NULL);
  136. return 0;
  137. }
  138. #else /* SCM_ENABLE_DISCOURAGED == 0 */
  139. int
  140. main (int argc, char *argv[])
  141. {
  142. return 0;
  143. }
  144. #endif /* SCM_ENABLE_DISCOURAGED == 0 */