From 820d8c04ba26ad28db9c53f11f8f483804bcf3a6 Mon Sep 17 00:00:00 2001 From: Jeremy Yallop Date: Fri, 11 Nov 2016 06:43:33 +0000 Subject: [PATCH 1/2] Add tests for marshalling unsigned integers. --- Makefile.tests | 10 +++++++++ tests/test-marshal/test_marshal.ml | 34 ++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 tests/test-marshal/test_marshal.ml diff --git a/Makefile.tests b/Makefile.tests index 95e82f62..3cffc54d 100644 --- a/Makefile.tests +++ b/Makefile.tests @@ -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 @@ -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 diff --git a/tests/test-marshal/test_marshal.ml b/tests/test-marshal/test_marshal.ml new file mode 100644 index 00000000..7ff252eb --- /dev/null +++ b/tests/test-marshal/test_marshal.ml @@ -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 From 867ba92c4d8552d548f15f26b41a0446e15ec321 Mon Sep 17 00:00:00 2001 From: Jeremy Yallop Date: Fri, 11 Nov 2016 06:53:44 +0000 Subject: [PATCH 2/2] Register custom operations for unsigned integers. --- src/ctypes/unsigned.ml | 3 +++ src/ctypes/unsigned_stubs.c | 7 +++++++ 2 files changed, 10 insertions(+) diff --git a/src/ctypes/unsigned.ml b/src/ctypes/unsigned.ml index 97db75d4..e0ea9098 100644 --- a/src/ctypes/unsigned.ml +++ b/src/ctypes/unsigned.ml @@ -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 diff --git a/src/ctypes/unsigned_stubs.c b/src/ctypes/unsigned_stubs.c index 073321c3..17fdf11b 100644 --- a/src/ctypes/unsigned_stubs.c +++ b/src/ctypes/unsigned_stubs.c @@ -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; +}