Stdlib/Ring: fix ring's semantics

This commit is contained in:
Vincent Botbol 2018-10-26 03:08:09 +02:00 committed by Benjamin Canou
parent f898062fd4
commit 14b1ba2adc
No known key found for this signature in database
GPG Key ID: 73607948459DC5F8
2 changed files with 9 additions and 3 deletions

View File

@ -60,7 +60,9 @@ let test_overflow _ =
assert_equal_bool ~msg:__LOC__ true (P2p_acl.PeerRing.mem set (a "baz")); assert_equal_bool ~msg:__LOC__ true (P2p_acl.PeerRing.mem set (a "baz"));
P2p_acl.PeerRing.add set (a "zor"); P2p_acl.PeerRing.add set (a "zor");
assert_equal_bool ~msg:__LOC__ true (P2p_acl.PeerRing.mem set (a "zor")); assert_equal_bool ~msg:__LOC__ true (P2p_acl.PeerRing.mem set (a "zor"));
assert_equal_bool ~msg:__LOC__ false (P2p_acl.PeerRing.mem set (a "baz")) assert_equal_bool ~msg:__LOC__ false (P2p_acl.PeerRing.mem set (a "foo"));
assert_equal_bool ~msg:__LOC__ true (P2p_acl.PeerRing.mem set (a "bar"));
assert_equal_bool ~msg:__LOC__ true (P2p_acl.PeerRing.mem set (a "baz"))
let () = let () =
Alcotest.run ~argv:[|""|] "tezos-p2p" [ Alcotest.run ~argv:[|""|] "tezos-p2p" [

View File

@ -33,7 +33,11 @@ module Ring = struct
type 'a t = 'a raw ref type 'a t = 'a raw ref
let create size = ref (Empty size) let create size =
if size <= 0 then
invalid_arg "Ring.create: size must be positive"
else
ref (Empty size)
let add r v = let add r v =
match !r with match !r with
@ -52,7 +56,7 @@ module Ring = struct
| Empty _ -> None | Empty _ -> None
| Inited s -> | Inited s ->
if s.pos >= Array.length s.data - 1 then if s.pos >= Array.length s.data - 1 then
Some (s.data.(s.pos mod Array.length s.data)) Some (s.data.((s.pos + 1) mod Array.length s.data))
else else
None in None in
add r v ; replaced add r v ; replaced