File: misc.ml

package info (click to toggle)
ocaml-luv 0.5.14-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,504 kB
  • sloc: ml: 11,130; makefile: 6,223; sh: 4,592; ansic: 1,517; python: 38
file content (91 lines) | stat: -rw-r--r-- 2,484 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
open Test_helpers

let tests = [
  "env", [
    "environ", `Quick, begin fun () ->
      Unix.putenv "LUV_TESTER" "42";
      let environment = Luv.Env.environ () |> check_success_result "environ" in
      Unix.putenv "LUV_TESTER" "";
      let found =
        List.exists
          (fun (name, value) -> name = "LUV_TESTER" && value = "42") environment
      in
      if not found then
        Alcotest.failf "environment variable not found"
    end;
  ];

  "system name", [
    "uname", `Quick, begin fun () ->
      let uname = Luv.System_info.uname () |> check_success_result "uname" in

      if uname.sysname = "" then
        Alcotest.fail "sysname empty";

      if uname.machine = "" then
        Alcotest.fail "machine empty";

    end;
  ];

  "time", [
    "gettimeofday", `Quick, begin fun () ->
      let timeval =
        Luv.Time.gettimeofday () |> check_success_result "gettimeofday" in
      let uv_time =
        let open Luv.Time in
        (Int64.to_float timeval.sec) +.
          (Int32.to_float timeval.usec) *. 1e-6
      in
      let ocaml_time = Unix.gettimeofday () in

      let delta = abs_float (uv_time -. ocaml_time) in
      if delta > 1. then
        Alcotest.failf "times: %f %f" uv_time ocaml_time
    end;

    "sleep: basic", `Quick, begin fun () ->
      Luv.Time.sleep 100
    end;

    "sleep: multithreading", `Quick, begin fun () ->
      let start_time = Unix.gettimeofday () in

      let thread =
        Luv.Thread.create (fun () -> Luv.Time.sleep 500)
        |> check_success_result "create"
      in

      Luv.Time.sleep 500;

      Luv.Thread.join thread
      |> check_success_result "join";

      let elapsed = Unix.gettimeofday () -. start_time in
      if elapsed > 0.75 then
        Alcotest.failf "%f s elapsed" elapsed
    end;
  ];

  "random", [
    "async", `Quick, begin fun () ->
      let content = String.make 16 'a' in
      let buffer = Luv.Buffer.from_string content in
      Luv.Random.random buffer begin fun result ->
        check_success_result "random" result;
        if Luv.Buffer.to_string buffer = content then
          Alcotest.fail "buffer contents"
      end;
      run ()
    end;

    "sync", `Quick, begin fun () ->
      let content = String.make 16 'a' in
      let buffer = Luv.Buffer.from_string content in
      Luv.Random.Sync.random buffer
      |> check_success_result "random";
      if Luv.Buffer.to_string buffer = content then
        Alcotest.fail "buffer contents"
    end;
  ];
]