various fixes
[ocaml-event.git] / unittest.ml
blob0edd7561a2c8f7b1b5e56d7bd3e8c741ac24de64
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
12 let create_event = Liboevent.create
14 (* Tests the creation of new events *)
15 let test_create_event _ =
16 let e1 = create_event () in
17 let e2 = create_event () in
18 "Events should be different" @? (e1 <> e2)
20 (* Tests if pending can be called with and without the optional float *)
21 let test_pending _ =
22 let e = create_event () in
23 ignore(Liboevent.pending e [])
25 (* Test eof on a read callback *)
26 let test_read_eof _ =
27 let test_string = "This is a test string\n\n\n" in
28 let buflen = 512 in
29 let buf = String.create buflen in
30 let read_count = ref 0 in
31 let evt = create_event () in
32 let read_cb fd event_type =
33 (* read data from the fd *)
34 let len = Unix.read fd buf 0 buflen in
35 (* when 0 bytes are read this is the EOF, and we are done. *)
36 if len <> 0 then
37 begin
38 read_count := !read_count + len;
39 Liboevent.add evt None
40 end
43 (* Create a socket pair for testing *)
44 let s1, s2 = Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0 in
45 let _ = Unix.write s1 test_string 0 (String.length test_string) in
47 (* A shutdown_send will cause an EOF on the reading end *)
48 Unix.shutdown s1 Unix.SHUTDOWN_SEND;
50 (* Setup the event *)
51 Liboevent.set evt s2 [Liboevent.READ] false read_cb;
52 Liboevent.add evt None;
53 Liboevent.dispatch ();
55 (* Now its time to check some things *)
56 assert_equal (String.length test_string) ! read_count
58 (* This is not really a test (yet) *)
59 let call_set _ =
60 let do_nothing _ _ =
62 in
63 let e1 = Liboevent.create () in
64 Liboevent.set e1 Unix.stderr [Liboevent.WRITE] false do_nothing;
65 Liboevent.set e1 Unix.stdout [Liboevent.WRITE] false do_nothing;
66 Liboevent.set e1 Unix.stdin [Liboevent.READ] false do_nothing;
67 Liboevent.add e1 (Some 0.1);
68 Liboevent.loop(Liboevent.ONCE)
70 (* Construct the test suite *)
71 let suite = "event" >:::
72 ["create_event" >:: test_create_event;
73 "test_pending" >:: test_pending;
74 "test_read_eof" >:: test_read_eof;
75 "call_set" >:: call_set;
78 (* Run the tests in the test suite *)
79 let _ =
80 run_test_tt_main suite