document CCBitField with a raw test

This commit is contained in:
Simon Cruanes 2015-08-11 20:34:52 +02:00
parent aa28542959
commit e54b5f32e6
2 changed files with 19 additions and 18 deletions

View file

@ -73,6 +73,7 @@ module Make(X : EMPTY) : BITFIELD = struct
width=2; width=2;
get=(fun x -> (x land mask) lsr n); get=(fun x -> (x land mask) lsr n);
set=(fun v x -> set=(fun v x ->
assert (x >= 0 && x < 4);
let x = x land (lnot mask) in let x = x land (lnot mask) in
x lor (v lsl n) x lor (v lsl n)
) )

View file

@ -35,29 +35,29 @@ module type BITFIELD = sig
(** New field of type 2-bits int *) (** New field of type 2-bits int *)
end end
(** Create a new bitfield type (** Create a new bitfield type *)
module Make(X : EMPTY) : BITFIELD
Example:
{[ (*$R
module B = CCBitField.Make(struct end);; let module B = CCBitField.Make(struct end) in
let x = B.bool ();; let x = B.bool () in
let y = B.int2 ();; let y = B.int2 () in
let z = B.bool ();; let z = B.bool () in
B.width y ;; (* = 2 *) assert_equal 2 (B.width y) ;
let f = B.empty let f = B.empty
|> B.set y 3 |> B.set y 3
|> B.set z true ;; |> B.set z true
(* = 14 *) in
B.get x f ;; (* false *) assert_equal 14 (f :> int) ;
B.get y f ;; (* 3 *)
B.get z f ;; (* true *) assert_equal false (B.get x f) ;
assert_equal 3 (B.get y f) ;
assert_equal (B.get z f);
()
*)
]}
*)
module Make(X : EMPTY) : BITFIELD