Merge remote-tracking branch 'origin/stable-2.0'
[bpt/guile.git] / test-suite / standalone / test-conversion.c
index d201874..700e5b3 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (C) 1999,2000,2001,2003,2004, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
+/* Copyright (C) 1999,2000,2001,2003,2004, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public License
@@ -22,8 +22,8 @@
 
 #include <libguile.h>
 
+#include <stdlib.h>
 #include <stdio.h>
-#include <assert.h>
 #include <string.h>
 
 #ifdef HAVE_INTTYPES_H
@@ -51,7 +51,7 @@ test_1 (const char *str, scm_t_intmax min, scm_t_intmax max,
       fprintf (stderr, "fail: scm_is_signed_integer (%s, "
               "%" PRIiMAX ", %" PRIiMAX ") == %d\n",
               str, min, max, result);
-      exit (1);
+      exit (EXIT_FAILURE);
     }
 }
 
@@ -137,7 +137,7 @@ test_2 (const char *str, scm_t_uintmax min, scm_t_uintmax max,
       fprintf (stderr, "fail: scm_is_unsigned_integer (%s, "
               "%" PRIuMAX ", %" PRIuMAX ") == %d\n",
               str, min, max, result);
-      exit (1);
+      exit (EXIT_FAILURE);
     }
 }
 
@@ -258,7 +258,7 @@ test_3 (const char *str, scm_t_intmax min, scm_t_intmax max,
                   "fail: scm_to_signed_int (%s, "
                   "%" PRIiMAX ", %" PRIiMAX ") -> out of range\n",
                   str, min, max);
-         exit (1);
+         exit (EXIT_FAILURE);
        }
     }
   else if (type_error)
@@ -271,7 +271,7 @@ test_3 (const char *str, scm_t_intmax min, scm_t_intmax max,
                   "fail: scm_to_signed_int (%s, "
                   "%" PRIiMAX", %" PRIiMAX ") -> wrong type\n",
                   str, min, max);
-         exit (1);
+         exit (EXIT_FAILURE);
        }
     }
   else
@@ -285,7 +285,7 @@ test_3 (const char *str, scm_t_intmax min, scm_t_intmax max,
                   "fail: scm_to_signed_int (%s, "
                   "%" PRIiMAX ", %" PRIiMAX ") = %" PRIiMAX "\n",
                   str, min, max, result);
-         exit (1);
+         exit (EXIT_FAILURE);
        }
     }
 }
@@ -393,7 +393,7 @@ test_4 (const char *str, scm_t_uintmax min, scm_t_uintmax max,
                   "fail: scm_to_unsigned_int (%s, "
                   "%" PRIuMAX ", %" PRIuMAX ") -> out of range\n",
                   str, min, max);
-         exit (1);
+         exit (EXIT_FAILURE);
        }
     }
   else if (type_error)
@@ -406,7 +406,7 @@ test_4 (const char *str, scm_t_uintmax min, scm_t_uintmax max,
                   "fail: scm_to_unsigned_int (%s, "
                   "%" PRIuMAX ", %" PRIuMAX ") -> wrong type\n",
                   str, min, max);
-         exit (1);
+         exit (EXIT_FAILURE);
        }
     }
   else
@@ -420,7 +420,7 @@ test_4 (const char *str, scm_t_uintmax min, scm_t_uintmax max,
                   "fail: scm_to_unsigned_int (%s, "
                   "%" PRIuMAX ", %" PRIuMAX ") == %" PRIuMAX "\n",
                   str, min, max, result);
-         exit (1);
+         exit (EXIT_FAILURE);
        }
     }
 }
@@ -476,7 +476,7 @@ test_5 (scm_t_intmax val, const char *result)
     {
       fprintf (stderr, "fail: scm_from_signed_integer (%" PRIiMAX ") == %s\n",
               val, result);
-      exit (1);
+      exit (EXIT_FAILURE);
     }
 }
 
@@ -509,7 +509,7 @@ test_6 (scm_t_uintmax val, const char *result)
       fprintf (stderr, "fail: scm_from_unsigned_integer (%"
               PRIuMAX ") == %s\n",
               val, result);
-      exit (1);
+      exit (EXIT_FAILURE);
     }
 }
 
@@ -537,7 +537,7 @@ test_7s (SCM n, scm_t_intmax c_n, const char *result, const char *func)
   if (scm_is_false (scm_equal_p (n, r)))
     {
       fprintf (stderr, "fail: %s (%" PRIiMAX ") == %s\n", func, c_n, result);
-      exit (1);
+      exit (EXIT_FAILURE);
     }
 }
 
@@ -551,7 +551,7 @@ test_7u (SCM n, scm_t_uintmax c_n, const char *result, const char *func)
   if (scm_is_false (scm_equal_p (n, r)))
     {
       fprintf (stderr, "fail: %s (%" PRIuMAX ") == %s\n", func, c_n, result);
-      exit (1);
+      exit (EXIT_FAILURE);
     }
 }
 
@@ -587,7 +587,7 @@ test_8s (const char *str, scm_t_intmax (*func) (SCM), const char *func_name,
        {
          fprintf (stderr,
                   "fail: %s (%s) -> out of range\n", func_name, str);
-         exit (1);
+         exit (EXIT_FAILURE);
        }
     }
   else if (type_error)
@@ -598,7 +598,7 @@ test_8s (const char *str, scm_t_intmax (*func) (SCM), const char *func_name,
        {
          fprintf (stderr,
                   "fail: %s (%s) -> wrong type\n", func_name, str);
-         exit (1);
+         exit (EXIT_FAILURE);
        }
     }
   else
@@ -610,7 +610,7 @@ test_8s (const char *str, scm_t_intmax (*func) (SCM), const char *func_name,
        {
          fprintf (stderr,
                   "fail: %s (%s) = %" PRIiMAX "\n", func_name, str, result);
-         exit (1);
+         exit (EXIT_FAILURE);
        }
     }
 }
@@ -645,7 +645,7 @@ test_8u (const char *str, scm_t_uintmax (*func) (SCM), const char *func_name,
        {
          fprintf (stderr,
                   "fail: %s (%s) -> out of range\n", func_name, str);
-         exit (1);
+         exit (EXIT_FAILURE);
        }
     }
   else if (type_error)
@@ -656,7 +656,7 @@ test_8u (const char *str, scm_t_uintmax (*func) (SCM), const char *func_name,
        {
          fprintf (stderr,
                   "fail: %s (%s) -> wrong type\n", func_name, str);
-         exit (1);
+         exit (EXIT_FAILURE);
        }
     }
   else
@@ -668,7 +668,7 @@ test_8u (const char *str, scm_t_uintmax (*func) (SCM), const char *func_name,
        {
          fprintf (stderr,
                   "fail: %s (%s) = %" PRIiMAX "\n", func_name, str, result);
-         exit (1);
+         exit (EXIT_FAILURE);
        }
     }
 }
@@ -703,10 +703,8 @@ DEFSTST (scm_to_int16)
 DEFUTST (scm_to_uint16)
 DEFSTST (scm_to_int32)
 DEFUTST (scm_to_uint32)
-#ifdef SCM_HAVE_T_INT64
 DEFSTST (scm_to_int64)
 DEFUTST (scm_to_uint64)
-#endif
 
 #define TEST_8S(v,f,r,re,te) test_8s (v, tst_##f, #f, r, re, te)
 #define TEST_8U(v,f,r,re,te) test_8u (v, tst_##f, #f, r, re, te)
@@ -746,11 +744,9 @@ test_int_sizes ()
   TEST_7S (scm_from_int32,  SCM_T_INT32_MAX+1LL, "-2147483648");
   TEST_7U (scm_from_uint32, SCM_T_UINT32_MAX,     "4294967295");
 
-#if SCM_HAVE_T_INT64
   TEST_7S (scm_from_int64,  SCM_T_INT64_MIN,  "-9223372036854775808");
   TEST_7S (scm_from_int64,  SCM_T_INT64_MAX,   "9223372036854775807");
   TEST_7U (scm_from_uint64, SCM_T_UINT64_MAX, "18446744073709551615");
-#endif
 
   TEST_8S ("91",   scm_to_schar,   91, 0, 0);
   TEST_8U ("91",   scm_to_uchar,   91, 0, 0);
@@ -795,7 +791,6 @@ test_int_sizes ()
   TEST_8U ("-1",          scm_to_uint32,                0, 1, 0);
   TEST_8U ("#f",          scm_to_uint32,                0, 0, 1);
 
-#if SCM_HAVE_T_INT64
   TEST_8S ("-9223372036854775808", scm_to_int64,   SCM_T_INT64_MIN, 0, 0);
   TEST_8S ("9223372036854775807",  scm_to_int64,   SCM_T_INT64_MAX, 0, 0);
   TEST_8S ("9223372036854775808",  scm_to_int64,                 0, 1, 0);
@@ -804,7 +799,6 @@ test_int_sizes ()
   TEST_8U ("18446744073709551616", scm_to_uint64,                0, 1, 0);
   TEST_8U ("-1",                   scm_to_uint64,                0, 1, 0);
   TEST_8U ("#f",                   scm_to_uint64,                0, 0, 1);
-#endif
 
 }
 
@@ -815,7 +809,7 @@ test_9 (double val, const char *result)
   if (scm_is_false (scm_eqv_p (res, scm_from_double (val))))
     {
       fprintf (stderr, "fail: scm_from_double (%g) == %s\n", val, result);
-      exit (1);
+      exit (EXIT_FAILURE);
     }
 }
 
@@ -902,7 +896,7 @@ test_10 (const char *val, double result, int type_error)
        {
          fprintf (stderr,
                   "fail: scm_double (%s) -> wrong type\n", val);
-         exit (1);
+         exit (EXIT_FAILURE);
        }
     }
   else
@@ -914,7 +908,7 @@ test_10 (const char *val, double result, int type_error)
        {
          fprintf (stderr,
                   "fail: scm_to_double (%s) = %g\n", val, result);
-         exit (1);
+         exit (EXIT_FAILURE);
        }
     }
 }
@@ -959,7 +953,7 @@ test_11 (const char *str, const char *result, int misc_error, int type_error)
        {
          fprintf (stderr,
                   "fail: scm_to_locale_string (%s) -> misc error\n", str);
-         exit (1);
+         exit (EXIT_FAILURE);
        }
     }
   else if (type_error)
@@ -970,7 +964,7 @@ test_11 (const char *str, const char *result, int misc_error, int type_error)
        {
          fprintf (stderr,
                   "fail: scm_to_locale_string (%s) -> wrong type\n", str);
-         exit (1);
+         exit (EXIT_FAILURE);
        }
     }
   else
@@ -982,7 +976,7 @@ test_11 (const char *str, const char *result, int misc_error, int type_error)
        {
          fprintf (stderr,
                   "fail: scm_to_locale_string (%s) = %s\n", str, result);
-         exit (1);
+         exit (EXIT_FAILURE);
        }
     }
 
@@ -1001,7 +995,7 @@ test_locale_strings ()
   if (!scm_is_string (scm_c_eval_string ("\"foo\"")))
     {
       fprintf (stderr, "fail: scm_is_string (\"foo\") = true\n");
-      exit (1);
+      exit (EXIT_FAILURE);
     }
 
   str = scm_from_locale_string (lstr);
@@ -1009,14 +1003,14 @@ test_locale_strings ()
   if (!scm_is_string (str))
     {
       fprintf (stderr, "fail: scm_is_string (str) = true\n");
-      exit (1);
+      exit (EXIT_FAILURE);
     }
 
   lstr2 = scm_to_locale_string (str);
   if (strcmp (lstr, lstr2))
     {
       fprintf (stderr, "fail: lstr = lstr2\n");
-      exit (1);
+      exit (EXIT_FAILURE);
     }
   free (lstr2);
 
@@ -1025,17 +1019,17 @@ test_locale_strings ()
   if (len != strlen (lstr))
     {
       fprintf (stderr, "fail: scm_to_locale_stringbuf (...) = strlen(lstr)\n");
-      exit (1);
+      exit (EXIT_FAILURE);
     }
   if (buf[15] != 'x')
     {
       fprintf (stderr, "fail: scm_to_locale_stringbuf (...) no overrun\n");
-      exit (1);
+      exit (EXIT_FAILURE);
     }
   if (strncmp (lstr, buf, 15))
     {
       fprintf (stderr, "fail: scm_to_locale_stringbuf (...) = lstr\n");
-      exit (1);
+      exit (EXIT_FAILURE);
     }
 
   str2 = scm_from_locale_stringn (lstr, 10);
@@ -1043,14 +1037,14 @@ test_locale_strings ()
   if (!scm_is_string (str2))
     {
       fprintf (stderr, "fail: scm_is_string (str2) = true\n");
-      exit (1);
+      exit (EXIT_FAILURE);
     }
 
   lstr2 = scm_to_locale_string (str2);
   if (strncmp (lstr, lstr2, 10))
     {
       fprintf (stderr, "fail: lstr = lstr2\n");
-      exit (1);
+      exit (EXIT_FAILURE);
     }
   free (lstr2);
 
@@ -1059,24 +1053,24 @@ test_locale_strings ()
   if (len != 10)
     {
       fprintf (stderr, "fail: scm_to_locale_stringbuf (...) = 10\n");
-      exit (1);
+      exit (EXIT_FAILURE);
     }
   if (buf[10] != 'x')
     {
       fprintf (stderr, "fail: scm_to_locale_stringbuf (...) no overrun\n");
-      exit (1);
+      exit (EXIT_FAILURE);
     }
   if (strncmp (lstr, buf, 10))
     {
       fprintf (stderr, "fail: scm_to_locale_stringbuf (...) = lstr\n");
-      exit (1);
+      exit (EXIT_FAILURE);
     }
 
   lstr2 = scm_to_locale_stringn (str2, &len);
   if (len != 10)
     {
       fprintf (stderr, "fail: scm_to_locale_stringn, len = 10\n");
-      exit (1);
+      exit (EXIT_FAILURE);
     }
 
   test_11 ("#f", NULL, 0, 1);
@@ -1084,6 +1078,105 @@ test_locale_strings ()
   test_11 ("(string #\\f #\\nul)", NULL, 1, 0);
 }
 
+static void
+test_to_utf8_stringn ()
+{
+  scm_t_wchar wstr[] = { 0x20,      /* 0x20 */
+                         0xDF,      /* 0xC3, 0x9F */
+                         0x65E5,    /* 0xE6, 0x97, 0xA5 */
+                         0x1D400 }; /* 0xF0, 0x9D, 0x90, 0x80 */
+
+  SCM str0 = scm_from_utf32_stringn (wstr, 1); /* ASCII */
+  SCM str1 = scm_from_utf32_stringn (wstr, 2); /* Narrow */
+  SCM str2 = scm_from_utf32_stringn (wstr, 4); /* Wide */
+
+  char cstr0[] = { 0x20, 0 };
+  char cstr1[] = { 0x20, 0xC3, 0x9F, 0 };
+  char cstr2[] = { 0x20, 0xC3, 0x9F, 0xE6, 0x97, 0xA5,
+                   0xF0, 0x9D, 0x90, 0x80, 0 };
+  char *cstr;
+  size_t len;
+
+  /* Test conversion of ASCII string */
+  cstr = scm_to_utf8_stringn (str0, &len);
+  if (len + 1 != sizeof (cstr0) || memcmp (cstr, cstr0, len))
+    {
+      fprintf (stderr, "fail: scm_to_utf8_stringn (<ASCII>, &len)");
+      exit (EXIT_FAILURE);
+    }
+  free (cstr);
+  cstr = scm_to_utf8_stringn (str0, NULL);
+  if (memcmp (cstr, cstr0, len + 1))
+    {
+      fprintf (stderr, "fail: scm_to_utf8_stringn (<ASCII>, NULL)");
+      exit (EXIT_FAILURE);
+    }
+  free (cstr);
+
+  /* Test conversion of narrow string */
+  cstr = scm_to_utf8_stringn (str1, &len);
+  if (len + 1 != sizeof (cstr1) || memcmp (cstr, cstr1, len))
+    {
+      fprintf (stderr, "fail: scm_to_utf8_stringn (<NARROW>, &len)");
+      exit (EXIT_FAILURE);
+    }
+  free (cstr);
+  cstr = scm_to_utf8_stringn (str1, NULL);
+  if (memcmp (cstr, cstr1, len + 1))
+    {
+      fprintf (stderr, "fail: scm_to_utf8_stringn (<NARROW>, NULL)");
+      exit (EXIT_FAILURE);
+    }
+  free (cstr);
+
+  /* Test conversion of wide string */
+  cstr = scm_to_utf8_stringn (str2, &len);
+  if (len + 1 != sizeof (cstr2) || memcmp (cstr, cstr2, len))
+    {
+      fprintf (stderr, "fail: scm_to_utf8_stringn (<WIDE>, &len)");
+      exit (EXIT_FAILURE);
+    }
+  free (cstr);
+  cstr = scm_to_utf8_stringn (str2, NULL);
+  if (memcmp (cstr, cstr2, len + 1))
+    {
+      fprintf (stderr, "fail: scm_to_utf8_stringn (<WIDE>, NULL)");
+      exit (EXIT_FAILURE);
+    }
+  free (cstr);
+}
+
+static void
+test_is_exact ()
+{
+  if (1 != scm_is_exact (scm_c_eval_string ("3")))
+    {
+      fprintf (stderr, "fail: scm_is_exact (\"3\") = 1\n");
+      exit (EXIT_FAILURE);
+    }
+  if (0 != scm_is_exact (scm_c_eval_string ("3.0")))
+    {
+      fprintf (stderr, "fail: scm_is_exact (\"3.0\") = 0\n");
+      exit (EXIT_FAILURE);
+    }
+}
+
+static void
+test_is_inexact ()
+{
+  if (1 !=scm_is_inexact (scm_c_eval_string ("3.0")))
+    {
+      fprintf (stderr, "fail: scm_is_inexact (\"3.0\") = 1\n");
+      exit (EXIT_FAILURE);
+    }
+  if (0 != scm_is_inexact (scm_c_eval_string ("3")))
+    {
+      fprintf (stderr, "fail: scm_is_inexact (\"3\") = 0\n");
+      exit (EXIT_FAILURE);
+    }
+}
+
+
 static void
 tests (void *data, int argc, char **argv)
 {
@@ -1097,6 +1190,9 @@ tests (void *data, int argc, char **argv)
   test_from_double ();
   test_to_double ();
   test_locale_strings ();
+  test_to_utf8_stringn ();
+  test_is_exact ();
+  test_is_inexact ();
 }
 
 int