update unit tests
[ocaml-event.git] / unittest.ml
blobf30fb01ff3b19867d651b802ca0eda5dcb114f97
1 (***********************************************************************)
2 (* The OcamlEvent library *)
3 (* *)
4 (* Copyright 2002, 2003 Maas-Maarten Zeeman. All rights reserved. See *)
5 (* LICENCE for details. *)
6 (***********************************************************************)
8 (* $Id: unittest.ml,v 1.3 2009-11-26 09:10:37 maas Exp $ *)
10 open OUnit
11 open Libevent
13 (* Tests the creation of new events *)
14 let test_create_event () =
15 let e1 = create () in
16 let e2 = create () in
17 "Events should be different" @? (e1 <> e2)
19 (* Tests if pending can be called with and without the optional float *)
20 let test_pending () =
21 todo "not implemented"
23 let e = create () in
24 ignore (pending e [])
27 (* Test eof on a read callback *)
28 let test_read_eof () =
29 let test_string = "This is a test string\n\n\n" in
30 let buflen = 512 in
31 let buf = String.create buflen in
32 let read_count = ref 0 in
33 let evt = create () in
34 let read_cb fd event_type =
35 (* read data from the fd *)
36 let len = Unix.read fd buf 0 buflen in
37 (* when 0 bytes are read this is the EOF, and we are done. *)
38 if len <> 0 then
39 begin
40 read_count := !read_count + len;
41 add evt None
42 end
45 (* Create a socket pair for testing *)
46 let s1, s2 = Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0 in
47 let _ = Unix.write s1 test_string 0 (String.length test_string) in
49 (* A shutdown_send will cause an EOF on the reading end *)
50 Unix.shutdown s1 Unix.SHUTDOWN_SEND;
52 (* Setup the event *)
53 Global.set evt s2 [READ] false read_cb;
54 add evt None;
55 Global.dispatch ();
57 (* Now its time to check some things *)
58 assert_equal (String.length test_string) ! read_count
60 (* This is not really a test (yet) *)
61 let call_set () =
62 let do_nothing _ _ =
65 let e1 = create () in
66 Global.set e1 Unix.stderr [WRITE] false do_nothing;
67 Global.set e1 Unix.stdout [WRITE] false do_nothing;
68 Global.set e1 Unix.stdin [READ] false do_nothing;
69 add e1 (Some 0.1);
70 Global.loop ONCE
72 (* Construct the test suite *)
73 let suite = "event" >:::
74 ["create_event" >:: test_create_event;
75 "test_pending" >:: test_pending;
76 "test_read_eof" >:: test_read_eof;
77 "call_set" >:: call_set;
80 (* Run the tests in the test suite *)
81 let _ =
82 run_test_tt_main suite