File: test_variadic.ml

package info (click to toggle)
ocaml-ctypes 0.24.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,180 kB
  • sloc: ml: 13,406; ansic: 3,316; makefile: 72
file content (76 lines) | stat: -rw-r--r-- 2,016 bytes parent folder | download | duplicates (2)
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
(*
 * Copyright (c) 2013 Jeremy Yallop.
 *
 * This file is distributed under the terms of the MIT License.
 * See the file LICENSE for details.
 *)

(* Tests for binding variadic functions. *)

[@@@ocaml.warning "-6"]

open OUnit2
open Ctypes
[@@@warning "-6"]

module Common_tests(S : Cstubs.FOREIGN with type 'a result = 'a
                                        and type 'a return = 'a) =
struct
  module M = Functions.Stubs(S)
  open Signed
  open Unsigned
  open M

  (*
    Test calling snprintf.
  *)
  let test_snprintf _ =
    let bufsz = 128 in
    let write snprintf apply =
      let buf = allocate_n char bufsz in
      let _ : int = apply (snprintf buf bufsz) in
      coerce (ptr char) string buf
    in
    begin
      assert_equal "an int: 100."
        (write snprintf_int
           (fun k -> k "an int: %d." 100));

      assert_equal "a char A and a uint 33."
        (write snprintf_char_unsigned
           (fun k -> k "a char %c and a uint %u." 'A' (UInt.of_int 33)));

      let ref_string =
        match Sys.word_size with
          | 32 -> "a long long 2147483647 and an int -4."
          | 64 -> "a long long 9223372036854775807 and an int -4."
          | n  -> failwith (Printf.sprintf "This test doesn't yet support word size %d" n)
      in
      let format_string =
        match Sys.os_type with
          | "Win32" -> "a long long %I64d and an int %d."
          | _ -> "a long long %lld and an int %d."
      in
      assert_equal ref_string
        (write snprintf_longlong_int
           (fun k -> k format_string
             (LLong.of_nativeint Nativeint.max_int) (-4)));

      assert_equal "a string abcde and an unsigned short ffd."
        (write snprintf_string_ushort
           (fun k -> k "a string %s and an unsigned short %hx."
             "abcde" (UShort.of_int 0xffd)));
    end
end


module Stub_tests = Common_tests(Generated_bindings)

let suite = "Variadic tests" >:::
  ["snprintf"
    >:: Stub_tests.test_snprintf;
  ]


let _ =
  run_test_tt_main suite