test-num2integral.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /* Copyright 1999-2001,2003-2004,2006,2008,2010-2012,2014,2018
  2. Free Software Foundation, Inc.
  3. This file is part of Guile.
  4. Guile is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Lesser General Public License as published
  6. by the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. Guile is distributed in the hope that it will be useful, but WITHOUT
  9. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  11. License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with Guile. If not, see
  14. <https://www.gnu.org/licenses/>. */
  15. #ifdef HAVE_CONFIG_H
  16. # include <config.h>
  17. #endif
  18. #undef NDEBUG
  19. #include <libguile.h>
  20. #include <stdio.h>
  21. #include <assert.h>
  22. #include <limits.h>
  23. SCM out_of_range_handler (void *data, SCM key, SCM args);
  24. SCM call_num2long_long_body (void *data);
  25. SCM call_num2ulong_long_body (void *data);
  26. /* expect to catch an `out-of-range' exception */
  27. SCM
  28. out_of_range_handler (void *data, SCM key, SCM args)
  29. {
  30. assert (scm_is_eq (key, scm_from_locale_symbol ("out-of-range")));
  31. return SCM_BOOL_T;
  32. }
  33. SCM
  34. call_num2long_long_body (void *data)
  35. {
  36. scm_to_long_long (* (SCM *) data);
  37. return SCM_BOOL_F;
  38. }
  39. SCM
  40. call_num2ulong_long_body (void *data)
  41. {
  42. scm_to_ulong_long (* (SCM *) data);
  43. return SCM_BOOL_F;
  44. }
  45. static void
  46. test_long_long ()
  47. {
  48. {
  49. SCM n = scm_from_long_long (LLONG_MIN);
  50. long long result = scm_to_long_long(n);
  51. assert (result == LLONG_MIN);
  52. }
  53. /* LLONG_MIN - 1 */
  54. {
  55. SCM n = scm_difference (scm_from_long_long (LLONG_MIN), scm_from_int (1));
  56. SCM caught = scm_internal_catch (SCM_BOOL_T, call_num2long_long_body, &n,
  57. out_of_range_handler, NULL);
  58. assert (scm_is_true (caught));
  59. }
  60. /* SCM_I_LLONG_MIN + SCM_I_LLONG_MIN/2 */
  61. {
  62. SCM n = scm_sum (scm_from_long_long (LLONG_MIN),
  63. scm_from_long_long (LLONG_MIN / 2));
  64. SCM caught = scm_internal_catch (SCM_BOOL_T, call_num2long_long_body, &n,
  65. out_of_range_handler, NULL);
  66. assert (scm_is_true (caught));
  67. }
  68. /* SCM_I_LLONG_MAX + 1 */
  69. {
  70. SCM n = scm_sum (scm_from_long_long (LLONG_MAX), scm_from_int (1));
  71. SCM caught = scm_internal_catch (SCM_BOOL_T, call_num2long_long_body, &n,
  72. out_of_range_handler, NULL);
  73. assert (scm_is_true (caught));
  74. }
  75. /* 2^1024 */
  76. {
  77. SCM n = scm_ash (scm_from_int (1), scm_from_int (1024));
  78. SCM caught = scm_internal_catch (SCM_BOOL_T, call_num2long_long_body, &n,
  79. out_of_range_handler, NULL);
  80. assert (scm_is_true (caught));
  81. }
  82. /* -2^1024 */
  83. {
  84. SCM n = scm_difference (scm_from_int (0),
  85. scm_ash (scm_from_int (1), scm_from_int (1024)));
  86. SCM caught = scm_internal_catch (SCM_BOOL_T, call_num2long_long_body, &n,
  87. out_of_range_handler, NULL);
  88. assert (scm_is_true (caught));
  89. }
  90. }
  91. static void
  92. test_ulong_long ()
  93. {
  94. {
  95. SCM n = scm_from_ulong_long (ULLONG_MAX);
  96. unsigned long long result = scm_to_ulong_long(n);
  97. assert (result == ULLONG_MAX);
  98. }
  99. /* -1 */
  100. {
  101. SCM n = scm_from_int (-1);
  102. SCM caught = scm_internal_catch (SCM_BOOL_T, call_num2ulong_long_body, &n,
  103. out_of_range_handler, NULL);
  104. assert (scm_is_true (caught));
  105. }
  106. /* SCM_I_ULLONG_MAX + 1 */
  107. {
  108. SCM n = scm_sum (scm_from_ulong_long (ULLONG_MAX), scm_from_int (1));
  109. SCM caught = scm_internal_catch (SCM_BOOL_T, call_num2ulong_long_body, &n,
  110. out_of_range_handler, NULL);
  111. assert (scm_is_true (caught));
  112. }
  113. /* 2^1024 */
  114. {
  115. SCM n = scm_ash (scm_from_int (1), scm_from_int (1024));
  116. SCM caught = scm_internal_catch (SCM_BOOL_T, call_num2long_long_body, &n,
  117. out_of_range_handler, NULL);
  118. assert (scm_is_true (caught));
  119. }
  120. }
  121. static void
  122. tests (void *data, int argc, char **argv)
  123. {
  124. test_long_long ();
  125. test_ulong_long ();
  126. }
  127. int
  128. main (int argc, char *argv[])
  129. {
  130. scm_boot_guile (argc, argv, tests, NULL);
  131. return 0;
  132. }