test-num2integral.c 4.3 KB

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