mirror of
https://github.com/c-cube/ocaml-containers.git
synced 2025-12-06 11:15:31 -05:00
document CCBitField with a raw test
This commit is contained in:
parent
aa28542959
commit
e54b5f32e6
2 changed files with 19 additions and 18 deletions
|
|
@ -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)
|
||||||
)
|
)
|
||||||
|
|
|
||||||
|
|
@ -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
|
||||||
|
let module B = CCBitField.Make(struct end) in
|
||||||
|
|
||||||
{[
|
let x = B.bool () in
|
||||||
module B = CCBitField.Make(struct end);;
|
let y = B.int2 () in
|
||||||
|
let z = B.bool () in
|
||||||
|
|
||||||
let x = B.bool ();;
|
assert_equal 2 (B.width y) ;
|
||||||
let y = B.int2 ();;
|
|
||||||
let z = B.bool ();;
|
|
||||||
|
|
||||||
B.width y ;; (* = 2 *)
|
|
||||||
|
|
||||||
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
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue