Permalink
Browse files

test: check (Cstruct.create (-1)) fails and (Cstruct.sub works)

  • Loading branch information...
David Scott
David Scott committed Dec 10, 2013
1 parent 1b612b8 commit 3718e6869e666296cf9e92054ae8f9f33aefe320
Showing with 283 additions and 15 deletions.
  1. +2 −1 Makefile
  2. +10 −0 _oasis
  3. +15 −2 _tags
  4. +23 −3 lib_test/bounds.ml
  5. +9 −3 myocamlbuild.ml
  6. +224 −6 setup.ml
View
@@ -14,7 +14,7 @@ setup.ml: _oasis
oasis setup
setup.data: setup.ml
- ocaml setup.ml -configure $(LWT) $(UNIX) $(ASYNC)
+ ocaml setup.ml -configure $(LWT) $(UNIX) $(ASYNC) --enable-tests
build: setup.data setup.ml
ocaml setup.ml -build -j $(J)
@@ -27,6 +27,7 @@ install: setup.data setup.ml
test: setup.ml build
./test.sh
+ ocaml setup.ml -test
reinstall: setup.ml
ocamlfind remove $(NAME) || true
View
10 _oasis
@@ -62,6 +62,16 @@ Library "cstruct-syntax"
XMETADescription: Syntax extension for Cstruct
XMETARequires: camlp4
+Executable test_bounds
+ Install: false
+ Path: lib_test
+ BuildDepends: cstruct,unix,oUnit
+ MainIs: bounds.ml
+
+Test test_bounds
+ Command: ./bounds.byte
+ Run: true
+
Document api
Title: Documentation and API reference
Type: ocamlbuild (0.3)
View
17 _tags
@@ -1,7 +1,7 @@
true: inline(10)
true: debug
# OASIS_START
-# DO NOT EDIT (digest: 0cbaead08ad7addfb80687ce8e3f2fcf)
+# DO NOT EDIT (digest: 60dceed12f46a3257c5355789042d89e)
# Ignore VCS directories, you can use the same kind of rule outside
# OASIS_START/STOP if you want to exclude directories that contains
# useless stuff for the build process
@@ -44,8 +44,8 @@ true: debug
# Library unix_cstruct
"unix/unix_cstruct.cmxs": use_unix_cstruct
<unix/*.ml{,i}>: use_cstruct
-<unix/*.ml{,i}>: pkg_bigarray
<unix/*.ml{,i}>: pkg_unix
+<unix/*.ml{,i}>: pkg_bigarray
<unix/*.ml{,i}>: pkg_ocplib-endian
<unix/*.ml{,i}>: pkg_ocplib-endian.bigstring
# Library cstruct-syntax
@@ -54,6 +54,19 @@ true: debug
<syntax/*.ml{,i}>: pkg_camlp4.quotations.r
<syntax/*.ml{,i}>: pkg_camlp4.extend
<syntax/*.ml{,i}>: pkg_bigarray
+# Executable test_bounds
+"lib_test/bounds.byte": use_cstruct
+"lib_test/bounds.byte": pkg_unix
+"lib_test/bounds.byte": pkg_oUnit
+"lib_test/bounds.byte": pkg_bigarray
+"lib_test/bounds.byte": pkg_ocplib-endian
+"lib_test/bounds.byte": pkg_ocplib-endian.bigstring
+<lib_test/*.ml{,i}>: use_cstruct
+<lib_test/*.ml{,i}>: pkg_unix
+<lib_test/*.ml{,i}>: pkg_oUnit
+<lib_test/*.ml{,i}>: pkg_bigarray
+<lib_test/*.ml{,i}>: pkg_ocplib-endian
+<lib_test/*.ml{,i}>: pkg_ocplib-endian.bigstring
# OASIS_STOP
<syntax/*.ml>: syntax_camlp4o, pkg_camlp4
<lib_test/*.ml{i}>: syntax_camlp4o, pkg_camlp4, pkg_cstruct.syntax
View
@@ -15,13 +15,24 @@
*)
open OUnit
+let to_string { Cstruct.buffer; off; len } =
+ Printf.sprintf "buffer length = %d; off=%d; len=%d" (Bigarray.Array1.dim buffer) off len
+
(* Check we can create and use an empty cstruct *)
let test_empty_cstruct () =
let x = Cstruct.create 0 in
assert_equal ~printer:string_of_int 0 (Cstruct.len x);
let y = Cstruct.to_string x in
assert_equal "" y
+(* Check that we can't create a cstruct with a negative length *)
+let test_anti_cstruct () =
+ try
+ let x = Cstruct.create (-1) in
+ failwith (Printf.sprintf "test_anti_cstruct: %s" (to_string x))
+ with Invalid_argument _ ->
+ ()
+
(* Check we can shift in the +ve direction *)
let test_positive_shift () =
let x = Cstruct.create 1 in
@@ -36,9 +47,6 @@ let test_negative_shift () =
assert_equal ~printer:string_of_int 0 z.Cstruct.off;
assert_equal ~printer:string_of_int 10 z.Cstruct.len
-let to_string { Cstruct.buffer; off; len } =
- Printf.sprintf "buffer length = %d; off=%d; len=%d" (Bigarray.Array1.dim buffer) off len
-
(* Check that an attempt to shift beyond the end of the buffer fails *)
let test_bad_positive_shift () =
let x = Cstruct.create 10 in
@@ -55,6 +63,16 @@ let test_bad_negative_shift () =
failwith (Printf.sprintf "test_bad_negative_shift: %s" (to_string y))
with Invalid_argument _ -> ()
+(* Check that 'sub' works *)
+let test_sub () =
+ let x = Cstruct.create 100 in
+ let y = Cstruct.sub x 10 80 in
+ assert_equal ~printer:string_of_int 10 y.Cstruct.off;
+ assert_equal ~printer:string_of_int 80 y.Cstruct.len;
+ let z = Cstruct.sub y 10 60 in
+ assert_equal ~printer:string_of_int 20 z.Cstruct.off;
+ assert_equal ~printer:string_of_int 60 z.Cstruct.len
+
let _ =
let verbose = ref false in
Arg.parse [
@@ -64,10 +82,12 @@ let _ =
let suite = "bounds" >::: [
"test empty cstruct" >:: test_empty_cstruct;
+ "test anti cstruct" >:: test_anti_cstruct;
"test positive shift" >:: test_positive_shift;
"test negative shift" >:: test_negative_shift;
"test bad positive shift" >:: test_bad_positive_shift;
"test bad negative shift" >:: test_bad_negative_shift;
+ "test_sub" >:: test_sub;
] in
run_test_tt ~verbose:!verbose suite
View
@@ -1,5 +1,5 @@
(* OASIS_START *)
-(* DO NOT EDIT (digest: 6ebffb9dd78d03c0fddd8514a30190ad) *)
+(* DO NOT EDIT (digest: 20f218895d8163c06c729dce2f5b089b) *)
module OASISGettext = struct
(* # 21 "src/oasis/OASISGettext.ml" *)
@@ -494,12 +494,18 @@ let package_default =
S [A "-ccopt"; A "-O3"; A "-ccopt"; A "-Wall"])
])
];
- includes = [("unix", ["lib"]); ("lwt", ["lib"]); ("async", ["lib"])];
+ includes =
+ [
+ ("unix", ["lib"]);
+ ("lwt", ["lib"]);
+ ("lib_test", ["lib"]);
+ ("async", ["lib"])
+ ];
}
;;
let dispatch_default = MyOCamlbuildBase.dispatch_default package_default;;
-# 504 "myocamlbuild.ml"
+# 510 "myocamlbuild.ml"
(* OASIS_STOP *)
Ocamlbuild_plugin.dispatch dispatch_default;;
Oops, something went wrong.

0 comments on commit 3718e68

Please sign in to comment.