anchor.diff:      Set = ?set:new(8),
       ?assertError(badarg, ?set:is_member(8, Set)).
   
  +%% An empty piece set should contain 0 pieces
  +empty_size_test() ->
  +    ?assertEqual(0, ?set:size(?set:new(8))),
  +    ?assertEqual(0, ?set:size(?set:new(14))).
  +
  +full_size_test() ->
  +    Set0 = ?set:from_binary(<<255:8>>, 8),
  +    ?assertEqual(8, ?set:size(Set0)),
  +    Set1 = ?set:from_binary(<<255:8, 1:1, 0:7>>, 9),
  +    ?assertEqual(9, ?set:size(Set1)).
  +
  +%% An empty set should be converted to an empty list
  +empty_list_test() ->
  +    Set = ?set:new(8),
  +    ?assertEqual([], ?set:to_list(Set)).
  +
  +%% Expect the list to be ordered from smallest to largest
  +list_order_test() ->
  +    Set = ?set:from_binary(<<1:1, 0:7, 1:1, 0:7>>, 9),
  +    ?assertEqual([0,8], ?set:to_list(Set)).
  +
  +%% Expect an empty list to be converted to an empty set
  +from_empty_list_test() ->
  +    Set0 = ?set:new(8),
  +    Set1 = ?set:from_list([], 8),
  +    ?assertEqual(Set0, Set1).
  +
  +from_full_list_test() ->
  +    Set0 = ?set:from_binary(<<255:8>>, 8),
  +    Set1 = ?set:from_list(lists:seq(0,7), 8),
  +    ?assertEqual(Set0, Set1).
  +
   %%
   %% Modifying the contents of a pieceset
   %%
  
positive.code:     Bitfield = <<1:1, 0:1, 0:6, 1:1, 0:7>>,
      ?assertEqual(Bitfield, ?set:to_binary(Inter)).
  
  
  %%
  %% Conversion from piecesets to bitfields should produce valid bitfields.
  
negative.code: %% Convert a piece set to a bitfield, the bitfield will
  %% be padded with at most 7 bits set to zero.
  %% @end
  -spec to_binary(pieceset()) -> binary().
  to_binary(Pieceset) ->
      #pieceset{elements=Elements} = Pieceset,
      Elements.
  
  %% @doc
  %% Convert an ordered list of piece indexes to a piece set.
  %% @end
  -spec from_list(list(pos_integer()), pos_integer()) -> pieceset().
  from_list(List, Size) ->
      Pieceset = new(Size),
      from_list_(List, Pieceset).
  
  from_list_([], Pieceset) ->
      Pieceset;
  from_list_([H|T], Pieceset) ->
      from_list_(T, insert(H, Pieceset)).
  
  
  %% @doc
  %% Convert a piece set to an ordered list of the piece indexes
  %% that are members of this set.
  %% @end
  -spec to_list(pieceset()) -> list(pos_integer()).
  to_list(Pieceset) ->
      #pieceset{elements=Elements} = Pieceset,
      to_list(Elements, 0).
  
  to_list(<<1:1, Rest/bitstring>>, Index) ->
      [Index|to_list(Rest, Index + 1)];
  to_list(<<0:1, Rest/bitstring>>, Index) ->
      to_list(Rest, Index + 1);
  to_list(<<>>, _) ->
      [].
  
  %% @doc
  %% Returns true if the piece is a member of the piece set,
  %% false if not. If the piece index is negative or is larger
  %% than the size of this piece set, the function exits with badarg.
  %% @end
  -spec is_member(pos_integer(), pieceset()) -> boolean().
  is_member(PieceIndex, _) when PieceIndex < 0 ->
      error(badarg);
  is_member(PieceIndex, Pieceset) ->
  