test-num2integral.c 4.3 KB

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