rubysl-set 0.0.1 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (113) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +0 -1
  3. data/.travis.yml +8 -0
  4. data/README.md +2 -2
  5. data/Rakefile +0 -1
  6. data/lib/rubysl/set.rb +2 -0
  7. data/lib/rubysl/set/set.rb +1237 -0
  8. data/lib/{rubysl-set → rubysl/set}/version.rb +1 -1
  9. data/lib/set.rb +1 -0
  10. data/rubysl-set.gemspec +19 -18
  11. data/spec/add_spec.rb +26 -0
  12. data/spec/append_spec.rb +6 -0
  13. data/spec/classify_spec.rb +34 -0
  14. data/spec/clear_spec.rb +16 -0
  15. data/spec/collect_spec.rb +6 -0
  16. data/spec/constructor_spec.rb +14 -0
  17. data/spec/delete_if_spec.rb +45 -0
  18. data/spec/delete_spec.rb +36 -0
  19. data/spec/difference_spec.rb +6 -0
  20. data/spec/divide_spec.rb +33 -0
  21. data/spec/each_spec.rb +33 -0
  22. data/spec/empty_spec.rb +9 -0
  23. data/spec/enumerable/to_set_spec.rb +18 -0
  24. data/spec/eql_spec.rb +16 -0
  25. data/spec/equal_value_spec.rb +37 -0
  26. data/spec/exclusion_spec.rb +17 -0
  27. data/spec/flatten_merge_spec.rb +22 -0
  28. data/spec/flatten_spec.rb +39 -0
  29. data/spec/hash_spec.rb +14 -0
  30. data/spec/include_spec.rb +6 -0
  31. data/spec/initialize_copy_spec.rb +17 -0
  32. data/spec/initialize_spec.rb +23 -0
  33. data/spec/inspect_spec.rb +17 -0
  34. data/spec/intersection_spec.rb +10 -0
  35. data/spec/keep_if_spec.rb +39 -0
  36. data/spec/length_spec.rb +6 -0
  37. data/spec/map_spec.rb +6 -0
  38. data/spec/member_spec.rb +6 -0
  39. data/spec/merge_spec.rb +18 -0
  40. data/spec/minus_spec.rb +6 -0
  41. data/spec/plus_spec.rb +6 -0
  42. data/spec/pretty_print_cycle_spec.rb +9 -0
  43. data/spec/pretty_print_spec.rb +16 -0
  44. data/spec/proper_subset_spec.rb +33 -0
  45. data/spec/proper_superset_spec.rb +33 -0
  46. data/spec/reject_spec.rb +49 -0
  47. data/spec/replace_spec.rb +16 -0
  48. data/spec/select_spec.rb +43 -0
  49. data/spec/shared/add.rb +14 -0
  50. data/spec/shared/collect.rb +20 -0
  51. data/spec/shared/difference.rb +15 -0
  52. data/spec/shared/include.rb +7 -0
  53. data/spec/shared/intersection.rb +15 -0
  54. data/spec/shared/length.rb +6 -0
  55. data/spec/shared/union.rb +15 -0
  56. data/spec/size_spec.rb +6 -0
  57. data/spec/sortedset/add_spec.rb +34 -0
  58. data/spec/sortedset/append_spec.rb +6 -0
  59. data/spec/sortedset/classify_spec.rb +34 -0
  60. data/spec/sortedset/clear_spec.rb +16 -0
  61. data/spec/sortedset/collect_spec.rb +6 -0
  62. data/spec/sortedset/constructor_spec.rb +14 -0
  63. data/spec/sortedset/delete_if_spec.rb +47 -0
  64. data/spec/sortedset/delete_spec.rb +36 -0
  65. data/spec/sortedset/difference_spec.rb +6 -0
  66. data/spec/sortedset/divide_spec.rb +33 -0
  67. data/spec/sortedset/each_spec.rb +35 -0
  68. data/spec/sortedset/empty_spec.rb +9 -0
  69. data/spec/sortedset/eql_spec.rb +17 -0
  70. data/spec/sortedset/equal_value_spec.rb +12 -0
  71. data/spec/sortedset/exclusion_spec.rb +17 -0
  72. data/spec/sortedset/flatten_merge_spec.rb +7 -0
  73. data/spec/sortedset/flatten_spec.rb +47 -0
  74. data/spec/sortedset/hash_spec.rb +14 -0
  75. data/spec/sortedset/include_spec.rb +6 -0
  76. data/spec/sortedset/initialize_copy_spec.rb +17 -0
  77. data/spec/sortedset/initialize_spec.rb +31 -0
  78. data/spec/sortedset/inspect_spec.rb +9 -0
  79. data/spec/sortedset/intersection_spec.rb +10 -0
  80. data/spec/sortedset/keep_if_spec.rb +32 -0
  81. data/spec/sortedset/length_spec.rb +6 -0
  82. data/spec/sortedset/map_spec.rb +6 -0
  83. data/spec/sortedset/member_spec.rb +6 -0
  84. data/spec/sortedset/merge_spec.rb +18 -0
  85. data/spec/sortedset/minus_spec.rb +6 -0
  86. data/spec/sortedset/plus_spec.rb +6 -0
  87. data/spec/sortedset/pretty_print_cycle_spec.rb +9 -0
  88. data/spec/sortedset/pretty_print_spec.rb +16 -0
  89. data/spec/sortedset/proper_subset_spec.rb +32 -0
  90. data/spec/sortedset/proper_superset_spec.rb +32 -0
  91. data/spec/sortedset/reject_spec.rb +51 -0
  92. data/spec/sortedset/replace_spec.rb +16 -0
  93. data/spec/sortedset/select_spec.rb +36 -0
  94. data/spec/sortedset/shared/add.rb +14 -0
  95. data/spec/sortedset/shared/collect.rb +20 -0
  96. data/spec/sortedset/shared/difference.rb +15 -0
  97. data/spec/sortedset/shared/include.rb +7 -0
  98. data/spec/sortedset/shared/intersection.rb +15 -0
  99. data/spec/sortedset/shared/length.rb +6 -0
  100. data/spec/sortedset/shared/union.rb +15 -0
  101. data/spec/sortedset/size_spec.rb +6 -0
  102. data/spec/sortedset/subset_spec.rb +32 -0
  103. data/spec/sortedset/subtract_spec.rb +16 -0
  104. data/spec/sortedset/superset_spec.rb +32 -0
  105. data/spec/sortedset/to_a_spec.rb +7 -0
  106. data/spec/sortedset/union_spec.rb +10 -0
  107. data/spec/subset_spec.rb +33 -0
  108. data/spec/subtract_spec.rb +16 -0
  109. data/spec/superset_spec.rb +33 -0
  110. data/spec/to_a_spec.rb +7 -0
  111. data/spec/union_spec.rb +10 -0
  112. metadata +282 -86
  113. data/lib/rubysl-set.rb +0 -7
@@ -0,0 +1,14 @@
1
+ describe :set_add, :shared => true do
2
+ before :each do
3
+ @set = Set.new
4
+ end
5
+
6
+ it "adds the passed Object to self" do
7
+ @set.send(@method, "dog")
8
+ @set.should include("dog")
9
+ end
10
+
11
+ it "returns self" do
12
+ @set.send(@method, "dog").should equal(@set)
13
+ end
14
+ end
@@ -0,0 +1,20 @@
1
+ describe :set_collect_bang, :shared => true do
2
+ before(:each) do
3
+ @set = Set[1, 2, 3, 4, 5]
4
+ end
5
+
6
+ it "yields each Object in self" do
7
+ res = []
8
+ @set.send(@method) { |x| res << x }
9
+ res.sort.should == [1, 2, 3, 4, 5].sort
10
+ end
11
+
12
+ it "returns self" do
13
+ @set.send(@method) { |x| x }.should equal(@set)
14
+ end
15
+
16
+ it "replaces self with the return values of the block" do
17
+ @set.send(@method) { |x| x * 2 }
18
+ @set.should == Set[2, 4, 6, 8, 10]
19
+ end
20
+ end
@@ -0,0 +1,15 @@
1
+ describe :set_difference, :shared => true do
2
+ before :each do
3
+ @set = Set[:a, :b, :c]
4
+ end
5
+
6
+ it "returns a new Set containting self's elements excluding the elements in the passed Enumerable" do
7
+ @set.send(@method, Set[:a, :b]).should == Set[:c]
8
+ @set.send(@method, [:b, :c]).should == Set[:a]
9
+ end
10
+
11
+ it "raises an ArgumentError when passed a non-Enumerable" do
12
+ lambda { @set.send(@method, 1) }.should raise_error(ArgumentError)
13
+ lambda { @set.send(@method, Object.new) }.should raise_error(ArgumentError)
14
+ end
15
+ end
@@ -0,0 +1,7 @@
1
+ describe :set_include, :shared => true do
2
+ it "returns true when self contains the passed Object" do
3
+ set = Set[:a, :b, :c]
4
+ set.send(@method, :a).should be_true
5
+ set.send(@method, :e).should be_false
6
+ end
7
+ end
@@ -0,0 +1,15 @@
1
+ describe :set_intersection, :shared => true do
2
+ before :each do
3
+ @set = Set[:a, :b, :c]
4
+ end
5
+
6
+ it "returns a new Set containing only elements shared by self and the passed Enumerable" do
7
+ @set.send(@method, Set[:b, :c, :d, :e]).should == Set[:b, :c]
8
+ @set.send(@method, [:b, :c, :d]).should == Set[:b, :c]
9
+ end
10
+
11
+ it "raises an ArgumentError when passed a non-Enumerable" do
12
+ lambda { @set.send(@method, 1) }.should raise_error(ArgumentError)
13
+ lambda { @set.send(@method, Object.new) }.should raise_error(ArgumentError)
14
+ end
15
+ end
@@ -0,0 +1,6 @@
1
+ describe :set_length, :shared => true do
2
+ it "returns the number of elements in the set" do
3
+ set = Set[:a, :b, :c]
4
+ set.send(@method).should == 3
5
+ end
6
+ end
@@ -0,0 +1,15 @@
1
+ describe :set_union, :shared => true do
2
+ before :each do
3
+ @set = Set[:a, :b, :c]
4
+ end
5
+
6
+ it "returns a new Set containing all elements of self and the passed Enumerable" do
7
+ @set.send(@method, Set[:b, :d, :e]).should == Set[:a, :b, :c, :d, :e]
8
+ @set.send(@method, [:b, :e]).should == Set[:a, :b, :c, :e]
9
+ end
10
+
11
+ it "raises an ArgumentError when passed a non-Enumerable" do
12
+ lambda { @set.send(@method, 1) }.should raise_error(ArgumentError)
13
+ lambda { @set.send(@method, Object.new) }.should raise_error(ArgumentError)
14
+ end
15
+ end
data/spec/size_spec.rb ADDED
@@ -0,0 +1,6 @@
1
+ require File.expand_path('../shared/length', __FILE__)
2
+ require 'set'
3
+
4
+ describe "Set#size" do
5
+ it_behaves_like :set_length, :size
6
+ end
@@ -0,0 +1,34 @@
1
+ require 'set'
2
+ require File.expand_path('../shared/add', __FILE__)
3
+
4
+ describe "SortedSet#add" do
5
+ it_behaves_like :sorted_set_add, :add
6
+
7
+ ruby_bug "redmine #118", "1.9.1" do
8
+ it "takes only values which responds <=>" do
9
+ obj = mock('no_comparison_operator')
10
+ obj.should_receive(:respond_to?).with(:<=>).and_return(false)
11
+ lambda { SortedSet["hello"].add(obj) }.should raise_error(ArgumentError)
12
+ end
13
+ end
14
+ end
15
+
16
+ describe "SortedSet#add?" do
17
+ before :each do
18
+ @set = SortedSet.new
19
+ end
20
+
21
+ it "adds the passed Object to self" do
22
+ @set.add?("cat")
23
+ @set.should include("cat")
24
+ end
25
+
26
+ it "returns self when the Object has not yet been added to self" do
27
+ @set.add?("cat").should equal(@set)
28
+ end
29
+
30
+ it "returns nil when the Object has already been added to self" do
31
+ @set.add?("cat")
32
+ @set.add?("cat").should be_nil
33
+ end
34
+ end
@@ -0,0 +1,6 @@
1
+ require 'set'
2
+ require File.expand_path('../shared/add', __FILE__)
3
+
4
+ describe "SortedSet#<<" do
5
+ it_behaves_like :sorted_set_add, :<<
6
+ end
@@ -0,0 +1,34 @@
1
+ require 'set'
2
+
3
+ describe "SortedSet#classify" do
4
+ before(:each) do
5
+ @set = SortedSet["one", "two", "three", "four"]
6
+ end
7
+
8
+ it "yields each Object in self in sorted order" do
9
+ res = []
10
+ @set.classify { |x| res << x }
11
+ res.should == ["one", "two", "three", "four"].sort
12
+ end
13
+
14
+ ruby_version_is "" ... "1.8.8" do
15
+ it "raises a LocalJumpError when passed no block" do
16
+ lambda { @set.classify }.should raise_error(LocalJumpError)
17
+ end
18
+ end
19
+
20
+ ruby_version_is "1.8.8" do
21
+ it "returns an Enumerator when passed no block" do
22
+ enum = @set.classify
23
+ enum.should be_an_instance_of(enumerator_class)
24
+
25
+ classified = enum.each { |x| x.length }
26
+ classified.should == { 3 => SortedSet["one", "two"], 4 => SortedSet["four"], 5 => SortedSet["three"] }
27
+ end
28
+ end
29
+
30
+ it "classifies the Objects in self based on the block's return value" do
31
+ classified = @set.classify { |x| x.length }
32
+ classified.should == { 3 => SortedSet["one", "two"], 4 => SortedSet["four"], 5 => SortedSet["three"] }
33
+ end
34
+ end
@@ -0,0 +1,16 @@
1
+ require 'set'
2
+
3
+ describe "SortedSet#clear" do
4
+ before(:each) do
5
+ @set = SortedSet["one", "two", "three", "four"]
6
+ end
7
+
8
+ it "removes all elements from self" do
9
+ @set.clear
10
+ @set.should be_empty
11
+ end
12
+
13
+ it "returns self" do
14
+ @set.clear.should equal(@set)
15
+ end
16
+ end
@@ -0,0 +1,6 @@
1
+ require 'set'
2
+ require File.expand_path('../shared/collect', __FILE__)
3
+
4
+ describe "SortedSet#collect!" do
5
+ it_behaves_like :sorted_set_collect_bang, :collect!
6
+ end
@@ -0,0 +1,14 @@
1
+ require 'set'
2
+
3
+ describe "SortedSet[]" do
4
+ it "returns a new SortedSet populated with the passed Objects" do
5
+ set = SortedSet[1, 2, 3]
6
+
7
+ set.instance_of?(SortedSet).should be_true
8
+ set.size.should eql(3)
9
+
10
+ set.should include(1)
11
+ set.should include(2)
12
+ set.should include(3)
13
+ end
14
+ end
@@ -0,0 +1,47 @@
1
+ require 'set'
2
+
3
+ describe "SortedSet#delete_if" do
4
+ before(:each) do
5
+ @set = SortedSet["one", "two", "three"]
6
+ end
7
+
8
+ ruby_bug "http://redmine.ruby-lang.org/projects/ruby-18/issues/show?id=115", "1.8.7.7" do
9
+ it "yields each Object in self in sorted order" do
10
+ ret = []
11
+ @set.delete_if { |x| ret << x }
12
+ ret.should == ["one", "two", "three"].sort
13
+ end
14
+ end
15
+
16
+ it "deletes every element from self for which the passed block returns true" do
17
+ @set.delete_if { |x| x.size == 3 }
18
+ @set.size.should eql(1)
19
+
20
+ @set.should_not include("one")
21
+ @set.should_not include("two")
22
+ @set.should include("three")
23
+ end
24
+
25
+ it "returns self" do
26
+ @set.delete_if { |x| x }.should equal(@set)
27
+ end
28
+
29
+ ruby_version_is "" ... "1.8.8" do
30
+ it "raises a LocalJumpError when passed no block" do
31
+ lambda { @set.delete_if }.should raise_error(LocalJumpError)
32
+ end
33
+ end
34
+
35
+ ruby_version_is "1.8.8" do
36
+ it "returns an Enumerator when passed no block" do
37
+ enum = @set.delete_if
38
+ enum.should be_an_instance_of(enumerator_class)
39
+
40
+ enum.each { |x| x.size == 3 }
41
+
42
+ @set.should_not include("one")
43
+ @set.should_not include("two")
44
+ @set.should include("three")
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,36 @@
1
+ require 'set'
2
+
3
+ describe "SortedSet#delete" do
4
+ before(:each) do
5
+ @set = SortedSet["a", "b", "c"]
6
+ end
7
+
8
+ it "deletes the passed Object from self" do
9
+ @set.delete("a")
10
+ @set.should_not include("a")
11
+ end
12
+
13
+ it "returns self" do
14
+ @set.delete("a").should equal(@set)
15
+ @set.delete("x").should equal(@set)
16
+ end
17
+ end
18
+
19
+ describe "SortedSet#delete?" do
20
+ before(:each) do
21
+ @set = SortedSet["a", "b", "c"]
22
+ end
23
+
24
+ it "deletes the passed Object from self" do
25
+ @set.delete?("a")
26
+ @set.should_not include("a")
27
+ end
28
+
29
+ it "returns self when the passed Object is in self" do
30
+ @set.delete?("a").should equal(@set)
31
+ end
32
+
33
+ it "returns nil when the passed Object is not in self" do
34
+ @set.delete?("x").should be_nil
35
+ end
36
+ end
@@ -0,0 +1,6 @@
1
+ require 'set'
2
+ require File.expand_path('../shared/difference', __FILE__)
3
+
4
+ describe "SortedSet#difference" do
5
+ it_behaves_like :sorted_set_difference, :difference
6
+ end
@@ -0,0 +1,33 @@
1
+ require 'set'
2
+
3
+ describe "SortedSet#divide" do
4
+ it "divides self into a set of subsets based on the blocks return values" do
5
+ set = SortedSet["one", "two", "three", "four", "five"].divide { |x| x.length }
6
+ set.map { |x| x.to_a }.to_a.sort.should == [["five", "four"], ["one", "two"], ["three"]]
7
+ end
8
+
9
+ it "yields each Object in self in sorted order" do
10
+ ret = []
11
+ SortedSet["one", "two", "three", "four", "five"].divide { |x| ret << x }
12
+ ret.should == ["one", "two", "three", "four", "five"].sort
13
+ end
14
+
15
+ # BUG: Does not raise a LocalJumpError, but a NoMethodError
16
+ #
17
+ # it "raises a LocalJumpError when not passed a block" do
18
+ # lambda { SortedSet[1].divide }.should raise_error(LocalJumpError)
19
+ # end
20
+ end
21
+
22
+ describe "SortedSet#divide when passed a block with an arity of 2" do
23
+ it "divides self into a set of subsets based on the blocks return values" do
24
+ set = SortedSet[1, 3, 4, 6, 9, 10, 11].divide { |x, y| (x - y).abs == 1 }
25
+ set.map { |x| x.to_a }.to_a.sort.should == [[1], [3, 4], [6], [9, 10, 11]]
26
+ end
27
+
28
+ it "yields each two Objects to the block" do
29
+ ret = []
30
+ SortedSet[1, 2].divide { |x, y| ret << [x, y] }
31
+ ret.should == [[1, 1], [1, 2], [2, 1], [2, 2]]
32
+ end
33
+ end
@@ -0,0 +1,35 @@
1
+ require 'set'
2
+
3
+ describe "SortedSet#each" do
4
+ before(:each) do
5
+ @set = SortedSet[1, 2, 3]
6
+ end
7
+
8
+ it "yields each Object in self in sorted order" do
9
+ ret = []
10
+ SortedSet["one", "two", "three"].each { |x| ret << x }
11
+ ret.should == ["one", "two", "three"].sort
12
+ end
13
+
14
+ ruby_bug "http://redmine.ruby-lang.org/issues/show/116", "1.8.7.7" do
15
+ it "returns self" do
16
+ @set.each { |x| x }.should equal(@set)
17
+ end
18
+ end
19
+
20
+ ruby_version_is "1.8.7" do
21
+ it "returns an Enumerator when not passed a block" do
22
+ enum = @set.each
23
+
24
+ ret = []
25
+ enum.each { |x| ret << x }
26
+ ret.sort.should == [1, 2, 3]
27
+ end
28
+ end
29
+
30
+ ruby_version_is "" ... "1.8.7" do
31
+ it "raises a LocalJumpError when not passed a block" do
32
+ lambda { @set.each }.should raise_error(LocalJumpError)
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,9 @@
1
+ require 'set'
2
+
3
+ describe "SortedSet#empty?" do
4
+ it "returns true if self is empty" do
5
+ SortedSet[].empty?.should be_true
6
+ SortedSet[1].empty?.should be_false
7
+ SortedSet[1,2,3].empty?.should be_false
8
+ end
9
+ end
@@ -0,0 +1,17 @@
1
+ require 'set'
2
+
3
+ ruby_version_is "1.8.7" do
4
+ describe "SortedSet#eql?" do
5
+ it "returns true when the passed argument is a SortedSet and contains the same elements" do
6
+ SortedSet[].should eql(SortedSet[])
7
+ SortedSet[1, 2, 3].should eql(SortedSet[1, 2, 3])
8
+ SortedSet[1, 2, 3].should eql(SortedSet[3, 2, 1])
9
+
10
+ # SortedSet["a", :b, ?c].should eql(SortedSet[?c, :b, "a"])
11
+
12
+ SortedSet[1, 2, 3].should_not eql(SortedSet[1.0, 2, 3])
13
+ SortedSet[1, 2, 3].should_not eql(SortedSet[2, 3])
14
+ SortedSet[1, 2, 3].should_not eql(SortedSet[])
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,12 @@
1
+ require 'set'
2
+
3
+ describe "SortedSet#==" do
4
+ it "returns true when the passed Object is a SortedSet and self and the Object contain the same elements" do
5
+ SortedSet[].should == SortedSet[]
6
+ SortedSet[1, 2, 3].should == SortedSet[1, 2, 3]
7
+ SortedSet["1", "2", "3"].should == SortedSet["1", "2", "3"]
8
+
9
+ SortedSet[1, 2, 3].should_not == SortedSet[1.0, 2, 3]
10
+ SortedSet[1, 2, 3].should_not == [1, 2, 3]
11
+ end
12
+ end