Interpret EINTR as G_IO_ERROR_AGAIN.
[platform/upstream/glib.git] / testglib.c
index 3a20d62..940851b 100644 (file)
  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  * Boston, MA 02111-1307, USA.
  */
+
+/*
+ * Modified by the GLib Team and others 1997-1999.  See the AUTHORS
+ * file for a list of people on the GLib Team.  See the ChangeLog
+ * files for a list of changes.  These files are distributed with
+ * GLib at ftp://ftp.gtk.org/pub/gtk/. 
+ */
+
 #undef G_LOG_DOMAIN
 
 #include <stdio.h>
@@ -39,6 +47,9 @@ else \
 #define        C2P(c)          ((gpointer) ((long) (c)))
 #define        P2C(p)          ((gchar) ((long) (p)))
 
+#define GLIB_TEST_STRING "el dorado "
+#define GLIB_TEST_STRING_5 "el do"
+
 static gboolean
 node_build_string (GNode    *node,
                   gpointer  data)
@@ -66,7 +77,7 @@ g_node_test (void)
   GNode *node_G;
   GNode *node_J;
   guint i;
-  gchar *tstring;
+  gchar *tstring, *cstring;
 
   g_print ("checking n-way trees: ");
   failed = FALSE;
@@ -154,7 +165,18 @@ g_node_test (void)
   g_node_traverse (root, G_LEVEL_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
   TEST (tstring, strcmp (tstring, "ABFEDCGKJIH") == 0);
   g_free (tstring); tstring = NULL;
-  
+
+  cstring = NULL;
+  node = g_node_copy (root);
+  TEST (NULL, g_node_n_nodes (root, G_TRAVERSE_ALL) == g_node_n_nodes (node, G_TRAVERSE_ALL));
+  TEST (NULL, g_node_max_height (root) == g_node_max_height (node));
+  g_node_traverse (root, G_IN_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
+  g_node_traverse (node, G_IN_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &cstring);
+  TEST (cstring, strcmp (tstring, cstring) == 0);
+  g_free (tstring); tstring = NULL;
+  g_free (cstring); cstring = NULL;
+  g_node_destroy (node);
+
   g_node_destroy (root);
 
   /* allocation tests */
@@ -177,7 +199,7 @@ g_node_test (void)
     g_print ("ok\n");
 }
 
-gboolean
+static gboolean
 my_hash_callback_remove (gpointer key,
                         gpointer value,
                         gpointer user_data)
@@ -190,7 +212,7 @@ my_hash_callback_remove (gpointer key,
   return FALSE;
 }
 
-void
+static void
 my_hash_callback_remove_test (gpointer key,
                              gpointer value,
                              gpointer user_data)
@@ -201,7 +223,7 @@ my_hash_callback_remove_test (gpointer key,
     g_print ("bad!\n");
 }
 
-void
+static void
 my_hash_callback (gpointer key,
                  gpointer value,
                  gpointer user_data)
@@ -210,20 +232,20 @@ my_hash_callback (gpointer key,
   *d = 1;
 }
 
-guint
+static guint
 my_hash (gconstpointer key)
 {
   return (guint) *((const gint*) key);
 }
 
-gint
+static gint
 my_hash_compare (gconstpointer a,
                 gconstpointer b)
 {
   return *((const gint*) a) == *((const gint*) b);
 }
 
-gint
+static gint
 my_list_compare_one (gconstpointer a, gconstpointer b)
 {
   gint one = *((const gint*)a);
@@ -231,7 +253,7 @@ my_list_compare_one (gconstpointer a, gconstpointer b)
   return one-two;
 }
 
-gint
+static gint
 my_list_compare_two (gconstpointer a, gconstpointer b)
 {
   gint one = *((const gint*)a);
@@ -239,14 +261,14 @@ my_list_compare_two (gconstpointer a, gconstpointer b)
   return two-one;
 }
 
-/* void
+/* static void
 my_list_print (gpointer a, gpointer b)
 {
   gint three = *((gint*)a);
   g_print("%d", three);
 }; */
 
-gint
+static gint
 my_compare (gconstpointer a,
            gconstpointer b)
 {
@@ -256,7 +278,7 @@ my_compare (gconstpointer a,
   return *cha - *chb;
 }
 
-gint
+static gint
 my_traverse (gpointer key,
             gpointer value,
             gpointer data)
@@ -280,7 +302,7 @@ main (int   argc,
   gint morenums[10] = { 8, 9, 7, 0, 3, 2, 5, 1, 4, 6};
   gchar *string;
 
-  gchar *mem[10000], *tmp_string, *tmp_string_2;
+  gchar *mem[10000], *tmp_string = NULL, *tmp_string_2;
   gint i, j;
   GArray *garray;
   GPtrArray *gparray;
@@ -295,7 +317,7 @@ main (int   argc,
     gchar *filename;
     gchar *dirname;
   } dirname_checks[] = {
-#ifndef NATIVE_WIN32
+#ifndef G_OS_WIN32
     { "/", "/" },
     { "////", "/" },
     { ".////", "." },
@@ -324,7 +346,8 @@ main (int   argc,
   guint16 gu16t1 = 0x44afU, gu16t2 = 0xaf44U;
   guint32 gu32t1 = 0x02a7f109U, gu32t2 = 0x09f1a702U;
 #ifdef G_HAVE_GINT64
-  guint64 gu64t1 = 0x1d636b02300a7aa7U, gu64t2 = 0xa77a0a30026b631dU;
+  guint64 gu64t1 = G_GINT64_CONSTANT(0x1d636b02300a7aa7U),
+         gu64t2 = G_GINT64_CONSTANT(0xa77a0a30026b631dU);
 #endif
 
   g_print ("TestGLib v%u.%u.%u (i:%u b:%u)\n",
@@ -337,6 +360,10 @@ main (int   argc,
   string = g_get_current_dir ();
   g_print ("cwd: %s\n", string);
   g_free (string);
+  g_print ("user: %s\n", g_get_user_name ());
+  g_print ("real: %s\n", g_get_real_name ());
+  g_print ("home: %s\n", g_get_home_dir ());
+  g_print ("tmp-dir: %s\n", g_get_tmp_dir ());
 
   /* type sizes */
   g_print ("checking size of gint8: %d", (int)sizeof (gint8));
@@ -425,6 +452,26 @@ main (int   argc,
     }
 
   g_list_free (list);
+  list = NULL;
+
+  for (i = 0; i < 10; i++)
+    list = g_list_prepend (list, &morenums[i]);
+
+  list = g_list_sort (list, my_list_compare_two);
+
+  /*
+  g_print("\n");
+  g_list_foreach (list, my_list_print, NULL);
+  */
+
+  for (i = 0; i < 10; i++)
+    {
+      t = g_list_nth (list, i);
+      if (*((gint*) t->data) != (9 - i))
+         g_error ("Merge sort failed");
+    }
+
+  g_list_free (list);
 
   g_print ("ok\n");
 
@@ -480,6 +527,26 @@ main (int   argc,
     }
 
   g_slist_free(slist);
+  slist = NULL;
+
+  for (i = 0; i < 10; i++)
+    slist = g_slist_prepend (slist, &morenums[i]);
+
+  slist = g_slist_sort (slist, my_list_compare_two);
+
+  /*
+  g_print("\n");
+  g_slist_foreach (slist, my_list_print, NULL);
+  */
+
+  for (i = 0; i < 10; i++)
+    {
+      st = g_slist_nth (slist, i);
+      if (*((gint*) st->data) != (9 - i))
+         g_error("Sorted insert failed");
+    }
+
+  g_slist_free(slist);
 
   g_print ("ok\n");
 
@@ -643,8 +710,8 @@ main (int   argc,
   for (i = 0; i < 10000; i++)
     g_string_append_c (string1, 'a'+(i%26));
 
-#if !(defined (_MSC_VER) || defined (__LCC__))
-  /* MSVC and LCC use the same run-time C library, which doesn't like
+#ifndef G_OS_WIN32
+  /* MSVC, mingw32 and LCC use the same run-time C library, which doesn't like
      the %10000.10000f format... */
   g_string_sprintf (string2, "%s|%0100d|%s|%s|%0*d|%*.*f|%10000.10000f",
                    "this pete guy sure is a wuss, like he's the number ",
@@ -670,6 +737,65 @@ main (int   argc,
   g_print ("and next 70:\n%s\n", string2->str+142);
   g_print ("last 70 chars:\n%s\n", string2->str+string2->len - 70);
 
+  g_string_free (string1, TRUE);
+  g_string_free (string2, TRUE);
+
+  /* append */
+  string1 = g_string_new ("firsthalf");
+  g_string_append (string1, "lasthalf");
+  g_assert (strcmp (string1->str, "firsthalflasthalf") == 0);
+  g_string_free (string1, TRUE);
+
+  /* append_len */
+
+  string1 = g_string_new ("firsthalf");
+  g_string_append_len (string1, "lasthalfjunkjunk", strlen ("lasthalf"));
+  g_assert (strcmp (string1->str, "firsthalflasthalf") == 0);
+  g_string_free (string1, TRUE);  
+  
+  /* prepend */
+  string1 = g_string_new ("lasthalf");
+  g_string_prepend (string1, "firsthalf");
+  g_assert (strcmp (string1->str, "firsthalflasthalf") == 0);
+  g_string_free (string1, TRUE);
+
+  /* prepend_len */
+  string1 = g_string_new ("lasthalf");
+  g_string_prepend_len (string1, "firsthalfjunkjunk", strlen ("firsthalf"));
+  g_assert (strcmp (string1->str, "firsthalflasthalf") == 0);
+  g_string_free (string1, TRUE);
+  
+  /* insert */
+  string1 = g_string_new ("firstlast");
+  g_string_insert (string1, 5, "middle");
+  g_assert (strcmp (string1->str, "firstmiddlelast") == 0);
+  g_string_free (string1, TRUE);
+
+  /* insert with pos == end of the string */
+  string1 = g_string_new ("firstmiddle");
+  g_string_insert (string1, strlen ("firstmiddle"), "last");
+  g_assert (strcmp (string1->str, "firstmiddlelast") == 0);
+  g_string_free (string1, TRUE);
+  
+  /* insert_len */
+
+  string1 = g_string_new ("firstlast");
+  g_string_insert_len (string1, 5, "middlejunkjunk", strlen ("middle"));
+  g_assert (strcmp (string1->str, "firstmiddlelast") == 0);
+  g_string_free (string1, TRUE);
+
+  /* insert_len with magic -1 pos for append */
+  string1 = g_string_new ("first");
+  g_string_insert_len (string1, -1, "lastjunkjunk", strlen ("last"));
+  g_assert (strcmp (string1->str, "firstlast") == 0);
+  g_string_free (string1, TRUE);
+  
+  /* insert_len with magic -1 len for strlen-the-string */
+  string1 = g_string_new ("first");
+  g_string_insert_len (string1, 5, "last", -1);
+  g_assert (strcmp (string1->str, "firstlast") == 0);
+  g_string_free (string1, TRUE);
+  
   g_print ("ok\n");
 
   g_print ("checking timers...\n");
@@ -704,6 +830,37 @@ main (int   argc,
 
   g_print ("ok\n");
 
+  g_print ("checking g_strdup...");
+  g_assert(g_strdup(NULL) == NULL);
+  string = g_strdup(GLIB_TEST_STRING);
+  g_assert(string != NULL);
+  g_assert(strcmp(string, GLIB_TEST_STRING) == 0);
+  g_free(string);
+
+  g_print ("ok\n");
+
+  g_print ("checking g_strconcat...");
+  string = g_strconcat(GLIB_TEST_STRING, NULL);
+  g_assert(string != NULL);
+  g_assert(strcmp(string, GLIB_TEST_STRING) == 0);
+  g_free(string);
+  string = g_strconcat(GLIB_TEST_STRING, GLIB_TEST_STRING, 
+                      GLIB_TEST_STRING, NULL);
+  g_assert(string != NULL);
+  g_assert(strcmp(string, GLIB_TEST_STRING GLIB_TEST_STRING
+                         GLIB_TEST_STRING) == 0);
+  g_free(string);
+  
+  g_print ("ok\n");
+
+  g_print ("checking g_strdup_printf...");
+  string = g_strdup_printf ("%05d %-5s", 21, "test");
+  g_assert (string != NULL);
+  g_assert (strcmp(string, "00021 test ") == 0);
+  g_free (string);
+
+  g_print ("ok\n");
+
   /* g_debug (argv[0]); */
 
   /* Relation tests */
@@ -816,18 +973,24 @@ main (int   argc,
   string = NULL;
   g_print (string);
 
-  g_print ("endian macro tests...");
+  g_print ("checking endian macros (host is ");
 #if G_BYTE_ORDER == G_BIG_ENDIAN
-  g_print ("big endian...");
+  g_print ("big endian)...");
 #else
-  g_print ("little endian...");
+  g_print ("little endian)...");
 #endif
   g_assert (GUINT16_SWAP_LE_BE (gu16t1) == gu16t2);  
   g_assert (GUINT32_SWAP_LE_BE (gu32t1) == gu32t2);  
 #ifdef G_HAVE_GINT64
   g_assert (GUINT64_SWAP_LE_BE (gu64t1) == gu64t2);  
 #endif
+
   g_print ("ok\n");
 
+#ifdef G_OS_WIN32
+  g_print ("current locale: %s\n", g_win32_getlocale ());
+#endif
+
   return 0;
 }
+