]> mj.ucw.cz Git - libucw.git/commitdiff
Main: Moved testing code to a separate file
authorMartin Mares <mj@ucw.cz>
Fri, 25 Feb 2011 18:33:59 +0000 (19:33 +0100)
committerMartin Mares <mj@ucw.cz>
Fri, 25 Feb 2011 18:33:59 +0000 (19:33 +0100)
ucw/main-test.c [new file with mode: 0644]
ucw/mainloop.c

diff --git a/ucw/main-test.c b/ucw/main-test.c
new file mode 100644 (file)
index 0000000..1df357b
--- /dev/null
@@ -0,0 +1,120 @@
+/*
+ *     UCW Library -- Main Loop: Testing
+ *
+ *     (c) 2004--2011 Martin Mares <mj@ucw.cz>
+ *
+ *     This software may be freely distributed and used according to the terms
+ *     of the GNU Lesser General Public License.
+ */
+
+#include "ucw/lib.h"
+#include "ucw/mainloop.h"
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#ifdef TEST
+
+static struct main_process mp;
+static struct main_block_io fin, fout;
+static struct main_hook hook;
+static struct main_timer tm;
+static struct main_signal sg;
+
+static byte rb[16];
+
+static void dread(struct main_block_io *bio)
+{
+  if (bio->rpos < bio->rlen)
+    {
+      msg(L_INFO, "Read EOF");
+      block_io_del(bio);
+    }
+  else
+    {
+      msg(L_INFO, "Read done");
+      block_io_read(bio, rb, sizeof(rb));
+    }
+}
+
+static void derror(struct main_block_io *bio, int cause)
+{
+  msg(L_INFO, "Error: %m !!! (cause %d)", cause);
+  block_io_del(bio);
+}
+
+static void dwrite(struct main_block_io *bio UNUSED)
+{
+  msg(L_INFO, "Write done");
+}
+
+static int dhook(struct main_hook *ho UNUSED)
+{
+  msg(L_INFO, "Hook called");
+  return 0;
+}
+
+static void dtimer(struct main_timer *tm)
+{
+  msg(L_INFO, "Timer tick");
+  timer_add_rel(tm, 11000);
+  timer_add_rel(tm, 10000);
+}
+
+static void dentry(void)
+{
+  log_fork();
+  msg(L_INFO, "*** SUBPROCESS START ***");
+  sleep(2);
+  msg(L_INFO, "*** SUBPROCESS FINISH ***");
+  exit(0);
+}
+
+static void dexit(struct main_process *pr)
+{
+  msg(L_INFO, "Subprocess %d exited with status %x", pr->pid, pr->status);
+}
+
+static void dsignal(struct main_signal *sg UNUSED)
+{
+  msg(L_INFO, "SIGINT received (use Ctrl-\\ to really quit)");
+}
+
+int
+main(void)
+{
+  log_init(NULL);
+  main_init();
+
+  fin.read_done = dread;
+  fin.error_handler = derror;
+  block_io_add(&fin, 0);
+  block_io_read(&fin, rb, sizeof(rb));
+
+  fout.write_done = dwrite;
+  fout.error_handler = derror;
+  block_io_add(&fout, 1);
+  block_io_write(&fout, "Hello, world!\n", 14);
+
+  hook.handler = dhook;
+  hook_add(&hook);
+
+  tm.handler = dtimer;
+  timer_add_rel(&tm,  1000);
+
+  sg.signum = SIGINT;
+  sg.handler = dsignal;
+  signal_add(&sg);
+
+  mp.handler = dexit;
+  if (!process_fork(&mp))
+    dentry();
+
+  main_debug();
+
+  main_loop();
+  msg(L_INFO, "Finished.");
+}
+
+#endif
index 5a5c498d213a6ebdfb3a6bdbc26a7f6586c89ed7..bf72f943145cd5e3460e89df8caf88c3f082ed05 100644 (file)
@@ -748,107 +748,3 @@ main_loop(void)
        }
     }
 }
-
-#ifdef TEST
-
-static struct main_process mp;
-static struct main_block_io fin, fout;
-static struct main_hook hook;
-static struct main_timer tm;
-static struct main_signal sg;
-
-static byte rb[16];
-
-static void dread(struct main_block_io *bio)
-{
-  if (bio->rpos < bio->rlen)
-    {
-      msg(L_INFO, "Read EOF");
-      block_io_del(bio);
-    }
-  else
-    {
-      msg(L_INFO, "Read done");
-      block_io_read(bio, rb, sizeof(rb));
-    }
-}
-
-static void derror(struct main_block_io *bio, int cause)
-{
-  msg(L_INFO, "Error: %m !!! (cause %d)", cause);
-  block_io_del(bio);
-}
-
-static void dwrite(struct main_block_io *bio UNUSED)
-{
-  msg(L_INFO, "Write done");
-}
-
-static int dhook(struct main_hook *ho UNUSED)
-{
-  msg(L_INFO, "Hook called");
-  return 0;
-}
-
-static void dtimer(struct main_timer *tm)
-{
-  msg(L_INFO, "Timer tick");
-  timer_add_rel(tm, 11000);
-  timer_add_rel(tm, 10000);
-}
-
-static void dentry(void)
-{
-  msg(L_INFO, "*** SUBPROCESS START ***");
-  sleep(2);
-  msg(L_INFO, "*** SUBPROCESS FINISH ***");
-  exit(0);
-}
-
-static void dexit(struct main_process *pr)
-{
-  msg(L_INFO, "Subprocess %d exited with status %x", pr->pid, pr->status);
-}
-
-static void dsignal(struct main_signal *sg UNUSED)
-{
-  msg(L_INFO, "SIGINT received (use Ctrl-\\ to really quit)");
-}
-
-int
-main(void)
-{
-  log_init(NULL);
-  main_init();
-
-  fin.read_done = dread;
-  fin.error_handler = derror;
-  block_io_add(&fin, 0);
-  block_io_read(&fin, rb, sizeof(rb));
-
-  fout.write_done = dwrite;
-  fout.error_handler = derror;
-  block_io_add(&fout, 1);
-  block_io_write(&fout, "Hello, world!\n", 14);
-
-  hook.handler = dhook;
-  hook_add(&hook);
-
-  tm.handler = dtimer;
-  timer_add_rel(&tm,  1000);
-
-  sg.signum = SIGINT;
-  sg.handler = dsignal;
-  signal_add(&sg);
-
-  mp.handler = dexit;
-  if (!process_fork(&mp))
-    dentry();
-
-  main_debug();
-
-  main_loop();
-  msg(L_INFO, "Finished.");
-}
-
-#endif