File tree Expand file tree Collapse file tree 2 files changed +759
-194
lines changed Expand file tree Collapse file tree 2 files changed +759
-194
lines changed Original file line number Diff line number Diff line change @@ -892,18 +892,53 @@ describe "Hash" do
892
892
893
893
it " creates with initial capacity" do
894
894
hash = Hash (Int32 , Int32 ).new(initial_capacity: 1234 )
895
- hash.@buckets_size .should eq(1234 )
895
+ hash.@indices_size_pow2 .should eq(11 )
896
896
end
897
897
898
898
it " creates with initial capacity and default value" do
899
899
hash = Hash (Int32 , Int32 ).new(default_value: 3 , initial_capacity: 1234 )
900
900
hash[1 ].should eq(3 )
901
- hash.@buckets_size .should eq(1234 )
901
+ hash.@indices_size_pow2 .should eq(11 )
902
902
end
903
903
904
904
it " creates with initial capacity and block" do
905
905
hash = Hash (Int32 , Int32 ).new(initial_capacity: 1234 ) { |h , k | h[k] = 3 }
906
906
hash[1 ].should eq(3 )
907
- hash.@buckets_size .should eq(1234 )
907
+ hash.@indices_size_pow2 .should eq(11 )
908
+ end
909
+
910
+ describe " some edge cases while changing the implementation to open addressing" do
911
+ it " edge case 1" do
912
+ h = {1 => 10 }
913
+ h[1 ]?.should eq(10 )
914
+ h.size.should eq(1 )
915
+
916
+ h.delete(1 )
917
+ h[1 ]?.should be_nil
918
+ h.size.should eq(0 )
919
+
920
+ h[2 ] = 10
921
+ h[2 ]?.should eq(10 )
922
+ h.size.should eq(1 )
923
+
924
+ h[2 ] = 10
925
+ h[2 ]?.should eq(10 )
926
+ h.size.should eq(1 )
927
+ end
928
+
929
+ it " edge case 2" do
930
+ hash = Hash (Int32 , Int32 ).new(initial_capacity: 0 )
931
+ hash.@indices_size_pow2 .should eq(0 )
932
+ hash[1 ] = 2
933
+ hash[1 ].should eq(2 )
934
+ end
935
+
936
+ it " edge case 3" do
937
+ h = {} of Int32 => Int32
938
+ (1 << 17 ).times do |i |
939
+ h[i] = i
940
+ h[i].should eq(i)
941
+ end
942
+ end
908
943
end
909
944
end
You can’t perform that action at this time.
0 commit comments