test-num2integral.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /* Copyright (C) 1999, 2000, 2001, 2003, 2004, 2006, 2008, 2010, 2011
  2. * 2012 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. #include <libguile.h>
  23. #include <stdio.h>
  24. #include <assert.h>
  25. #include <limits.h>
  26. SCM out_of_range_handler (void *data, SCM key, SCM args);
  27. SCM call_num2long_long_body (void *data);
  28. SCM call_num2ulong_long_body (void *data);
  29. /* expect to catch an `out-of-range' exception */
  30. SCM
  31. out_of_range_handler (void *data, SCM key, SCM args)
  32. {
  33. assert (scm_is_eq (key, scm_from_locale_symbol ("out-of-range")));
  34. return SCM_BOOL_T;
  35. }
  36. SCM
  37. call_num2long_long_body (void *data)
  38. {
  39. scm_to_long_long (* (SCM *) data);
  40. return SCM_BOOL_F;
  41. }
  42. SCM
  43. call_num2ulong_long_body (void *data)
  44. {
  45. scm_to_ulong_long (* (SCM *) data);
  46. return SCM_BOOL_F;
  47. }
  48. static void
  49. test_long_long ()
  50. {
  51. {
  52. SCM n = scm_from_long_long (LLONG_MIN);
  53. long long result = scm_to_long_long(n);
  54. assert (result == LLONG_MIN);
  55. }
  56. /* LLONG_MIN - 1 */
  57. {
  58. SCM n = scm_difference (scm_from_long_long (LLONG_MIN), scm_from_int (1));
  59. SCM caught = scm_internal_catch (SCM_BOOL_T, call_num2long_long_body, &n,
  60. out_of_range_handler, NULL);
  61. assert (scm_is_true (caught));
  62. }
  63. /* SCM_I_LLONG_MIN + SCM_I_LLONG_MIN/2 */
  64. {
  65. SCM n = scm_sum (scm_from_long_long (LLONG_MIN),
  66. scm_from_long_long (LLONG_MIN / 2));
  67. SCM caught = scm_internal_catch (SCM_BOOL_T, call_num2long_long_body, &n,
  68. out_of_range_handler, NULL);
  69. assert (scm_is_true (caught));
  70. }
  71. /* SCM_I_LLONG_MAX + 1 */
  72. {
  73. SCM n = scm_sum (scm_from_long_long (LLONG_MAX), scm_from_int (1));
  74. SCM caught = scm_internal_catch (SCM_BOOL_T, call_num2long_long_body, &n,
  75. out_of_range_handler, NULL);
  76. assert (scm_is_true (caught));
  77. }
  78. /* 2^1024 */
  79. {
  80. SCM n = scm_ash (scm_from_int (1), scm_from_int (1024));
  81. SCM caught = scm_internal_catch (SCM_BOOL_T, call_num2long_long_body, &n,
  82. out_of_range_handler, NULL);
  83. assert (scm_is_true (caught));
  84. }
  85. /* -2^1024 */
  86. {
  87. SCM n = scm_difference (scm_from_int (0),
  88. scm_ash (scm_from_int (1), scm_from_int (1024)));
  89. SCM caught = scm_internal_catch (SCM_BOOL_T, call_num2long_long_body, &n,
  90. out_of_range_handler, NULL);
  91. assert (scm_is_true (caught));
  92. }
  93. }
  94. static void
  95. test_ulong_long ()
  96. {
  97. {
  98. SCM n = scm_from_ulong_long (ULLONG_MAX);
  99. unsigned long long result = scm_to_ulong_long(n);
  100. assert (result == ULLONG_MAX);
  101. }
  102. /* -1 */
  103. {
  104. SCM n = scm_from_int (-1);
  105. SCM caught = scm_internal_catch (SCM_BOOL_T, call_num2ulong_long_body, &n,
  106. out_of_range_handler, NULL);
  107. assert (scm_is_true (caught));
  108. }
  109. /* SCM_I_ULLONG_MAX + 1 */
  110. {
  111. SCM n = scm_sum (scm_from_ulong_long (ULLONG_MAX), scm_from_int (1));
  112. SCM caught = scm_internal_catch (SCM_BOOL_T, call_num2ulong_long_body, &n,
  113. out_of_range_handler, NULL);
  114. assert (scm_is_true (caught));
  115. }
  116. /* 2^1024 */
  117. {
  118. SCM n = scm_ash (scm_from_int (1), scm_from_int (1024));
  119. SCM caught = scm_internal_catch (SCM_BOOL_T, call_num2long_long_body, &n,
  120. out_of_range_handler, NULL);
  121. assert (scm_is_true (caught));
  122. }
  123. }
  124. static void
  125. tests (void *data, int argc, char **argv)
  126. {
  127. test_long_long ();
  128. test_ulong_long ();
  129. }
  130. int
  131. main (int argc, char *argv[])
  132. {
  133. scm_boot_guile (argc, argv, tests, NULL);
  134. return 0;
  135. }