Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Register custom operations for unsigned integers #480

Merged
merged 2 commits into from
Nov 11, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Makefile.tests
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,15 @@ tests/test-oo_style/generated_stubs.c: $(BUILDDIR)/test-oo_style-stub-generator.
tests/test-oo_style/generated_bindings.ml: $(BUILDDIR)/test-oo_style-stub-generator.$(BEST)
$< --ml-file $@

test-marshal.dir = tests/test-marshal
test-marshal.threads = yes
test-marshal.deps = str bigarray oUnit bytes
test-marshal.subproject_deps = ctypes ctypes-foreign-base \
ctypes-foreign-threaded cstubs tests-common
test-marshal.link_flags = -L$(BUILDDIR)/clib -ltest_functions
test-marshal: PROJECT=test-marshal
test-marshal: $$(BEST_TARGET)

test-type_printing.dir = tests/test-type_printing
test-type_printing.threads = yes
test-type_printing.deps = str bigarray oUnit bytes
Expand Down Expand Up @@ -1127,6 +1136,7 @@ TESTS += test-passable
TESTS += test-alignment
TESTS += test-views-stubs test-views-stub-generator test-views-generated test-views
TESTS += test-oo_style-stubs test-oo_style-stub-generator test-oo_style-generated test-oo_style
TESTS += test-marshal
TESTS += test-type_printing
TESTS += test-value_printing-stubs test-value_printing-stub-generator test-value_printing-generated test-value_printing
TESTS += test-complex-stubs test-complex-stub-generator test-complex-generated test-complex
Expand Down
3 changes: 3 additions & 0 deletions src/ctypes/unsigned.ml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
* See the file LICENSE for details.
*)

external init : unit -> unit = "ctypes_unsigned_init"
let () = init ()

(* Boxed unsigned types *)
module type Basics = sig
type t
Expand Down
7 changes: 7 additions & 0 deletions src/ctypes/unsigned_stubs.c
Original file line number Diff line number Diff line change
Expand Up @@ -213,3 +213,10 @@ value ctypes_int32_of_uint32 (value u) { return caml_copy_int32(Uint_custom_val(
value ctypes_uintptr_t_size (value _) { return Val_int(sizeof (uintptr_t)); }
value ctypes_intptr_t_size (value _) { return Val_int(sizeof (intptr_t)); }
value ctypes_ptrdiff_t_size (value _) { return Val_int(sizeof (ptrdiff_t)); }

value ctypes_unsigned_init(value unit)
{
caml_register_custom_operations(&caml_uint32_ops);
caml_register_custom_operations(&caml_uint64_ops);
return Val_unit;
}
34 changes: 34 additions & 0 deletions tests/test-marshal/test_marshal.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
(*
* Copyright (c) 2016 Jeremy Yallop.
*
* This file is distributed under the terms of the MIT License.
* See the file LICENSE for details.
*)

open OUnit2
open Ctypes
open Unsigned


(*
Test marshalling and unmarshalling custom integers
*)
let test_integer_marshalling _ =
let v = (
UInt8.zero, UInt16.zero, UInt32.zero, UInt64.zero,
UInt8.one, UInt16.one, UInt32.one, UInt64.one,
UInt8.of_string "100", UInt16.of_string "1000",
UInt32.of_string "10000", UInt64.of_string "100000",
UInt8.max_int, UInt16.max_int, UInt32.max_int, UInt64.max_int
) in
assert_equal v Marshal.(from_string (to_string v []) 0)


let suite = "Marshal tests" >:::
["integer marshalling"
>:: test_integer_marshalling;
]


let _ =
run_test_tt_main suite