[kdbus] sync with kdbus (kdbus.h - commit: 5ae1ecac44cb)
[platform/upstream/glib.git] / tests / gio-test.c
index 17ac543..f0f70cd 100644 (file)
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the
- * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
- * Boston, MA 02111-1307, USA.
+ * License along with this library; if not, see <http://www.gnu.org/licenses/>.
  */
 
 /* A test program for the main loop and IO channel code.
  * Just run it. Optional parameter is number of sub-processes.
  */
 
+#undef G_DISABLE_ASSERT
+#undef G_LOG_DOMAIN
+
 #include "config.h"
 
 #include <glib.h>
   #include <process.h>
   #define STRICT
   #include <windows.h>
-#else
-  #ifdef HAVE_UNISTD_H
-    #include <unistd.h>
-  #endif
+  #define pipe(fds) _pipe(fds, 4096, _O_BINARY)
+#endif
+
+#ifdef G_OS_UNIX
+  #include <unistd.h>
 #endif
 
 static int nrunning;
@@ -64,7 +66,7 @@ read_all (int         fd,
          guint      *bytes_read)
 {
   guint left = nbytes;
-  guint nb;
+  gsize nb;
   GIOError error = G_IO_ERROR_NONE;
   char *bufp = buffer;
 
@@ -78,10 +80,7 @@ read_all (int         fd,
       
       if (error != G_IO_ERROR_NONE)
        {
-         g_print ("gio-test: ...from %d: G_IO_ERROR_%s\n", fd,
-                  (error == G_IO_ERROR_AGAIN ? "AGAIN" :
-                   (error == G_IO_ERROR_INVAL ? "INVAL" :
-                    (error == G_IO_ERROR_UNKNOWN ? "UNKNOWN" : "???"))));
+         g_print ("gio-test: ...from %d: %d\n", fd, error);
          if (error == G_IO_ERROR_AGAIN)
            continue;
          break;
@@ -102,7 +101,7 @@ shutdown_source (gpointer data)
     {
       nrunning--;
       if (nrunning == 0)
-       g_main_quit (main_loop);
+       g_main_loop_quit (main_loop);
     }
 }
 
@@ -114,11 +113,13 @@ recv_message (GIOChannel  *channel,
   gint fd = g_io_channel_unix_get_fd (channel);
   gboolean retval = TRUE;
 
+#ifdef VERBOSE
   g_print ("gio-test: ...from %d:%s%s%s%s\n", fd,
           (cond & G_IO_ERR) ? " ERR" : "",
           (cond & G_IO_HUP) ? " HUP" : "",
           (cond & G_IO_IN)  ? " IN"  : "",
           (cond & G_IO_PRI) ? " PRI" : "");
+#endif
 
   if (cond & (G_IO_ERR | G_IO_HUP))
     {
@@ -139,7 +140,9 @@ recv_message (GIOChannel  *channel,
        {
          if (nb == 0)
            {
+#ifdef VERBOSE
              g_print ("gio-test: ...from %d: EOF\n", fd);
+#endif
              shutdown_source (data);
              return FALSE;
            }
@@ -149,12 +152,7 @@ recv_message (GIOChannel  *channel,
          for (i = 0; i < nkiddies; i++)
            if (seqtab[i].fd == fd)
              {
-               if (seq != seqtab[i].seq)
-                 {
-                   g_print ("gio-test: ...from %d: invalid sequence number %d, expected %d\n",
-                            fd, seq, seqtab[i].seq);
-                   g_assert_not_reached ();
-                 }
+                g_assert_cmpint (seq, ==, seqtab[i].seq);
                seqtab[i].seq++;
                break;
              }
@@ -167,22 +165,20 @@ recv_message (GIOChannel  *channel,
       
       if (nb == 0)
        {
+#ifdef VERBOSE
          g_print ("gio-test: ...from %d: EOF\n", fd);
+#endif
          shutdown_source (data);
          return FALSE;
        }
       
       g_assert (nb == sizeof (nbytes));
 
-      if (nbytes >= BUFSIZE)
-       {
-         g_print ("gio-test: ...from %d: nbytes = %d (%#x)!\n", fd, nbytes, nbytes);
-         g_assert_not_reached ();
-       }
+      g_assert_cmpint (nbytes, <, BUFSIZE);
       g_assert (nbytes >= 0 && nbytes < BUFSIZE);
-      
+#ifdef VERBOSE      
       g_print ("gio-test: ...from %d: %d bytes\n", fd, nbytes);
-      
+#endif      
       if (nbytes > 0)
        {
          error = read_all (fd, channel, buf, nbytes, &nb);
@@ -192,19 +188,18 @@ recv_message (GIOChannel  *channel,
 
          if (nb == 0)
            {
+#ifdef VERBOSE
              g_print ("gio-test: ...from %d: EOF\n", fd);
+#endif
              shutdown_source (data);
              return FALSE;
            }
       
          for (j = 0; j < nbytes; j++)
-           if (buf[j] != ' ' + ((nbytes + j) % 95))
-             {
-               g_print ("gio-test: ...from %d: buf[%d] == '%c', should be '%c'\n",
-                        fd, j, buf[j], 'a' + ((nbytes + j) % 32));
-               g_assert_not_reached ();
-             }
+            g_assert (buf[j] == ' ' + ((nbytes + j) % 95));
+#ifdef VERBOSE
          g_print ("gio-test: ...from %d: OK\n", fd);
+#endif
        }
     }
   return retval;
@@ -301,7 +296,7 @@ main (int    argc,
          exit (1);
        }
 
-      hwnd = CreateWindow (klass, "gio-test", 0, 0, 0, 10, 10,
+      hwnd = CreateWindow (MAKEINTATOM(klass), "gio-test", 0, 0, 0, 10, 10,
                           NULL, NULL, wcl.hInstance, NULL);
       if (!hwnd)
        {
@@ -309,7 +304,7 @@ main (int    argc,
          exit (1);
        }
 
-      windows_messages_channel = g_io_channel_win32_new_messages (hwnd);
+      windows_messages_channel = g_io_channel_win32_new_messages ((guint)hwnd);
       g_io_add_watch (windows_messages_channel, G_IO_IN, recv_windows_message, 0);
 #endif
 
@@ -346,6 +341,7 @@ main (int    argc,
                                     pipe_to_sub[0], pipe_from_sub[1]);
          
          system (cmdline);
+          g_free (cmdline);
 #endif
          close (pipe_to_sub[0]);
          close (pipe_from_sub [1]);
@@ -362,11 +358,16 @@ main (int    argc,
                   (end.tv_usec - start.tv_usec) / 1000,
                   pollresult);
 #endif
+          g_io_channel_unref (my_read_channel);
        }
       
-      main_loop = g_main_new (FALSE);
+      main_loop = g_main_loop_new (NULL, FALSE);
       
-      g_main_run (main_loop);
+      g_main_loop_run (main_loop);
+
+      g_main_loop_unref (main_loop);
+      g_free (seqtab);
+      g_free (id);
     }
   else if (argc == 3)
     {
@@ -398,8 +399,10 @@ main (int    argc,
          buflen = rand() % BUFSIZE;
          for (j = 0; j < buflen; j++)
            buf[j] = ' ' + ((buflen + j) % 95);
+#ifdef VERBOSE
          g_print ("gio-test: child writing %d+%d bytes to %d\n",
                   (int)(sizeof(i) + sizeof(buflen)), buflen, writefd);
+#endif
          write (writefd, &i, sizeof (i));
          write (writefd, &buflen, sizeof (buflen));
          write (writefd, buf, buflen);
@@ -416,7 +419,9 @@ main (int    argc,
            }
 #endif
        }
+#ifdef VERBOSE
       g_print ("gio-test: child exiting, closing %d\n", writefd);
+#endif
       close (writefd);
     }
   else