Skip to content

Commit a6b9c93

Browse files
committed
Generated Custom Set
The custom-set exercise is generated using data from x-common repository. fixes #365 fixes #348 references exercism/problem-specifications#257
1 parent 0402d11 commit a6b9c93

File tree

6 files changed

+376
-70
lines changed

6 files changed

+376
-70
lines changed

bin/generate-custom-set

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/usr/bin/env ruby
2+
3+
require_relative '../lib/helper'
4+
require 'generator'
5+
require 'custom_set_cases'
6+
7+
Generator.new('custom-set', CustomSetCases).generate

exercises/custom-set/.version

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1

exercises/custom-set/custom_set_test.rb

Lines changed: 253 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -3,114 +3,299 @@
33
require 'minitest/autorun'
44
require_relative 'custom_set'
55

6+
# Test data version:
7+
# cdfb4a2
68
class CustomSetTest < Minitest::Test
7-
def test_equal
8-
assert_equal CustomSet.new([1, 3]), CustomSet.new([3, 1])
9-
refute_equal CustomSet.new([1, 3]), CustomSet.new([3, 1, 5])
10-
refute_equal CustomSet.new([1, 3, 5]), CustomSet.new([3, 1])
11-
refute_equal CustomSet.new([1, 3]), CustomSet.new([2, 1])
9+
def test_sets_with_no_elements_are_empty
10+
# skip
11+
set = CustomSet.new []
12+
assert_empty set
1213
end
1314

14-
def test_no_duplicates
15-
assert_equal CustomSet.new([1, 1]), CustomSet.new([1])
15+
def test_sets_with_elements_are_not_empty
16+
skip
17+
set = CustomSet.new [1]
18+
refute_empty set
19+
end
20+
21+
def test_nothing_is_contained_in_an_empty_set
22+
skip
23+
set = CustomSet.new []
24+
element = 1
25+
refute set.contains? element
26+
end
27+
28+
def test_when_the_element_is_in_the_set
29+
skip
30+
set = CustomSet.new [1, 2, 3]
31+
element = 1
32+
assert set.contains? element
1633
end
1734

18-
def test_delete
35+
def test_when_the_element_is_not_in_the_set
1936
skip
20-
assert_equal CustomSet.new([1, 3]), CustomSet.new([3, 2, 1]).delete(2)
21-
assert_equal CustomSet.new([1, 2, 3]), CustomSet.new([3, 2, 1]).delete(4)
22-
assert_equal CustomSet.new([1, 2, 3]), CustomSet.new([3, 2, 1]).delete(2.0)
23-
assert_equal CustomSet.new([1, 3]), CustomSet.new([3, 2.0, 1]).delete(2.0)
37+
set = CustomSet.new [1, 2, 3]
38+
element = 4
39+
refute set.contains? element
2440
end
2541

26-
def test_difference
42+
def test_empty_set_is_a_subset_of_another_empty_set
2743
skip
28-
assert_equal CustomSet.new([1, 3]),
29-
CustomSet.new([1, 2, 3]).difference(CustomSet.new([2, 4]))
44+
set1 = CustomSet.new []
45+
set2 = CustomSet.new []
46+
assert set2.subset? set1
47+
end
3048

31-
assert_equal CustomSet.new([1, 2.0, 3]),
32-
CustomSet.new([1, 2.0, 3]).difference(CustomSet.new([2, 4]))
49+
def test_empty_set_is_a_subset_of_non_empty_set
50+
skip
51+
set1 = CustomSet.new []
52+
set2 = CustomSet.new [1]
53+
assert set2.subset? set1
3354
end
3455

35-
def test_disjoint?
56+
def test_non_empty_set_is_not_a_subset_of_empty_set
3657
skip
37-
assert CustomSet.new([1, 2]).disjoint?(CustomSet.new([3, 4]))
38-
refute CustomSet.new([1, 2]).disjoint?(CustomSet.new([2, 3]))
39-
assert CustomSet.new([1.0, 2.0]).disjoint?(CustomSet.new([2, 3]))
40-
assert CustomSet.new.disjoint?(CustomSet.new)
58+
set1 = CustomSet.new [1]
59+
set2 = CustomSet.new []
60+
refute set2.subset? set1
4161
end
4262

43-
def test_empty
63+
def test_set_is_a_subset_of_set_with_exact_same_elements
4464
skip
45-
assert_equal CustomSet.new, CustomSet.new([1, 2]).empty
46-
assert_equal CustomSet.new, CustomSet.new.empty
65+
set1 = CustomSet.new [1, 2, 3]
66+
set2 = CustomSet.new [1, 2, 3]
67+
assert set2.subset? set1
68+
end
69+
70+
def test_set_is_a_subset_of_larger_set_with_same_elements
71+
skip
72+
set1 = CustomSet.new [1, 2, 3]
73+
set2 = CustomSet.new [4, 1, 2, 3]
74+
assert set2.subset? set1
75+
end
76+
77+
def test_set_is_not_a_subset_of_set_that_does_not_contain_its_elements
78+
skip
79+
set1 = CustomSet.new [1, 2, 3]
80+
set2 = CustomSet.new [4, 1, 3]
81+
refute set2.subset? set1
82+
end
83+
84+
def test_the_empty_set_is_disjoint_with_itself
85+
skip
86+
set1 = CustomSet.new []
87+
set2 = CustomSet.new []
88+
assert set1.disjoint? set2
89+
end
90+
91+
def test_empty_set_is_disjoint_with_non_empty_set
92+
skip
93+
set1 = CustomSet.new []
94+
set2 = CustomSet.new [1]
95+
assert set1.disjoint? set2
96+
end
97+
98+
def test_non_empty_set_is_disjoint_with_empty_set
99+
skip
100+
set1 = CustomSet.new [1]
101+
set2 = CustomSet.new []
102+
assert set1.disjoint? set2
103+
end
104+
105+
def test_sets_are_not_disjoint_if_they_share_an_element
106+
skip
107+
set1 = CustomSet.new [1, 2]
108+
set2 = CustomSet.new [2, 3]
109+
refute set1.disjoint? set2
110+
end
111+
112+
def test_sets_are_disjoint_if_they_share_no_elements
113+
skip
114+
set1 = CustomSet.new [1, 2]
115+
set2 = CustomSet.new [3, 4]
116+
assert set1.disjoint? set2
117+
end
118+
119+
def test_empty_sets_are_equal
120+
skip
121+
set1 = CustomSet.new []
122+
set2 = CustomSet.new []
123+
assert_equal set1, set2
124+
end
125+
126+
def test_empty_set_is_not_equal_to_non_empty_set
127+
skip
128+
set1 = CustomSet.new []
129+
set2 = CustomSet.new [1, 2, 3]
130+
refute_equal set1, set2
131+
end
132+
133+
def test_non_empty_set_is_not_equal_to_empty_set
134+
skip
135+
set1 = CustomSet.new [1, 2, 3]
136+
set2 = CustomSet.new []
137+
refute_equal set1, set2
138+
end
139+
140+
def test_sets_with_the_same_elements_are_equal
141+
skip
142+
set1 = CustomSet.new [1, 2]
143+
set2 = CustomSet.new [2, 1]
144+
assert_equal set1, set2
145+
end
146+
147+
def test_sets_with_different_elements_are_not_equal
148+
skip
149+
set1 = CustomSet.new [1, 2, 3]
150+
set2 = CustomSet.new [1, 2, 4]
151+
refute_equal set1, set2
152+
end
153+
154+
def test_add_to_empty_set
155+
skip
156+
set = CustomSet.new []
157+
expected = CustomSet.new [3]
158+
assert_equal expected, set.add(3)
159+
end
160+
161+
def test_add_to_non_empty_set
162+
skip
163+
set = CustomSet.new [1, 2, 4]
164+
expected = CustomSet.new [1, 2, 3, 4]
165+
assert_equal expected, set.add(3)
166+
end
167+
168+
def test_adding_an_existing_element_does_not_change_the_set
169+
skip
170+
set = CustomSet.new [1, 2, 3]
171+
expected = CustomSet.new [1, 2, 3]
172+
assert_equal expected, set.add(3)
173+
end
174+
175+
def test_intersection_of_two_empty_sets_is_an_empty_set
176+
skip
177+
set1 = CustomSet.new []
178+
set2 = CustomSet.new []
179+
expected = CustomSet.new []
180+
assert_equal expected, set2.intersection(set1)
181+
end
182+
183+
def test_intersection_of_an_empty_set_and_non_empty_set_is_an_empty_set
184+
skip
185+
set1 = CustomSet.new []
186+
set2 = CustomSet.new [3, 2, 5]
187+
expected = CustomSet.new []
188+
assert_equal expected, set2.intersection(set1)
189+
end
190+
191+
def test_intersection_of_a_non_empty_set_and_an_empty_set_is_an_empty_set
192+
skip
193+
set1 = CustomSet.new [1, 2, 3, 4]
194+
set2 = CustomSet.new []
195+
expected = CustomSet.new []
196+
assert_equal expected, set2.intersection(set1)
197+
end
198+
199+
def test_intersection_of_two_sets_with_no_shared_elements_is_an_empty_set
200+
skip
201+
set1 = CustomSet.new [1, 2, 3]
202+
set2 = CustomSet.new [4, 5, 6]
203+
expected = CustomSet.new []
204+
assert_equal expected, set2.intersection(set1)
47205
end
48206

49207
# rubocop:disable Metrics/LineLength
50-
def test_intersection
208+
def test_intersection_of_two_sets_with_shared_elements_is_a_set_of_the_shared_elements
51209
skip
52-
assert_equal CustomSet.new([:a, :c]),
53-
CustomSet.new([:a, :b, :c]).intersection(CustomSet.new([:a, :c, :d]))
210+
set1 = CustomSet.new [1, 2, 3, 4]
211+
set2 = CustomSet.new [3, 2, 5]
212+
expected = CustomSet.new [2, 3]
213+
assert_equal expected, set2.intersection(set1)
214+
end
54215

55-
assert_equal CustomSet.new([3]),
56-
CustomSet.new([1, 2, 3]).intersection(CustomSet.new([1.0, 2.0, 3]))
216+
def test_difference_of_two_empty_sets_is_an_empty_set
217+
skip
218+
set1 = CustomSet.new []
219+
set2 = CustomSet.new []
220+
expected = CustomSet.new []
221+
assert_equal expected, set1.difference(set2)
57222
end
58223

59-
def test_member?
224+
def test_difference_of_empty_set_and_non_empty_set_is_an_empty_set
60225
skip
61-
assert CustomSet.new([1, 2, 3]).member?(2)
62-
assert CustomSet.new(1..3).member?(2)
63-
refute CustomSet.new(1..3).member?(2.0)
64-
refute CustomSet.new(1..3).member?(4)
226+
set1 = CustomSet.new []
227+
set2 = CustomSet.new [3, 2, 5]
228+
expected = CustomSet.new []
229+
assert_equal expected, set1.difference(set2)
65230
end
66231

67-
def test_put
232+
def test_difference_of_a_non_empty_set_and_an_empty_set_is_the_non_empty_set
68233
skip
69-
assert_equal CustomSet.new([1, 2, 3, 4]),
70-
CustomSet.new([1, 2, 4]).put(3)
234+
set1 = CustomSet.new [1, 2, 3, 4]
235+
set2 = CustomSet.new []
236+
expected = CustomSet.new [1, 2, 3, 4]
237+
assert_equal expected, set1.difference(set2)
238+
end
71239

72-
assert_equal CustomSet.new([1, 2, 3]),
73-
CustomSet.new([1, 2, 3]).put(3)
240+
def test_difference_of_two_non_empty_sets_is_a_set_of_elements_that_are_only_in_the_first_set
241+
skip
242+
set1 = CustomSet.new [3, 2, 1]
243+
set2 = CustomSet.new [2, 4]
244+
expected = CustomSet.new [1, 3]
245+
assert_equal expected, set1.difference(set2)
246+
end
74247

75-
assert_equal CustomSet.new([1, 2, 3, 3.0]),
76-
CustomSet.new([1, 2, 3]).put(3.0)
248+
def test_union_of_empty_sets_is_an_empty_set
249+
skip
250+
set1 = CustomSet.new []
251+
set2 = CustomSet.new []
252+
expected = CustomSet.new []
253+
assert_equal expected, set1.union(set2)
77254
end
78255

79-
def test_size
256+
def test_union_of_an_empty_set_and_non_empty_set_is_the_non_empty_set
80257
skip
81-
assert_equal 0, CustomSet.new.size
82-
assert_equal 3, CustomSet.new([1, 2, 3]).size
83-
assert_equal 3, CustomSet.new([1, 2, 3, 2]).size
258+
set1 = CustomSet.new []
259+
set2 = CustomSet.new [2]
260+
expected = CustomSet.new [2]
261+
assert_equal expected, set1.union(set2)
84262
end
85263

86-
def test_subset?
264+
def test_union_of_a_non_empty_set_and_empty_set_is_the_non_empty_set
87265
skip
88-
assert CustomSet.new([1, 2, 3]).subset?(CustomSet.new([1, 2, 3]))
89-
assert CustomSet.new([4, 1, 2, 3]).subset?(CustomSet.new([1, 2, 3]))
90-
refute CustomSet.new([4, 1, 3]).subset?(CustomSet.new([1, 2, 3]))
91-
refute CustomSet.new([1, 2, 3, 4]).subset?(CustomSet.new([1, 2, 3.0]))
92-
assert CustomSet.new([4, 1, 3]).subset?(CustomSet.new)
93-
assert CustomSet.new.subset?(CustomSet.new)
266+
set1 = CustomSet.new [1, 3]
267+
set2 = CustomSet.new []
268+
expected = CustomSet.new [1, 3]
269+
assert_equal expected, set1.union(set2)
94270
end
95271

96-
def test_to_a
272+
def test_union_of_non_empty_sets_contains_all_unique_elements
97273
skip
98-
assert_equal [], CustomSet.new.to_a.sort
99-
assert_equal [1, 2, 3], CustomSet.new([3, 1, 2]).to_a.sort
100-
assert_equal [1, 2, 3], CustomSet.new([3, 1, 2, 1]).to_a.sort
274+
set1 = CustomSet.new [1, 3]
275+
set2 = CustomSet.new [2, 3]
276+
expected = CustomSet.new [3, 2, 1]
277+
assert_equal expected, set1.union(set2)
101278
end
102279

103-
def test_union # rubocop:disable Metrics/MethodLength
280+
# Problems in exercism evolve over time, as we find better ways to ask
281+
# questions.
282+
# The version number refers to the version of the problem you solved,
283+
# not your solution.
284+
#
285+
# Define a constant named VERSION inside of the top level BookKeeping
286+
# module, which may be placed near the end of your file.
287+
#
288+
# In your file, it will look like this:
289+
#
290+
# module BookKeeping
291+
# VERSION = 1 # Where the version number matches the one in the test.
292+
# end
293+
#
294+
# If you are curious, read more about constants on RubyDoc:
295+
# http://ruby-doc.org/docs/ruby-doc-bundle/UsersGuide/rg/constants.html
296+
297+
def test_bookkeeping
104298
skip
105-
assert_equal CustomSet.new([3, 2, 1]),
106-
CustomSet.new([1, 3]).union(CustomSet.new([2]))
107-
assert_equal CustomSet.new([3.0, 3, 2, 1]),
108-
CustomSet.new([1, 3]).union(CustomSet.new([2, 3.0]))
109-
assert_equal CustomSet.new([3, 1]),
110-
CustomSet.new([1, 3]).union(CustomSet.new)
111-
assert_equal CustomSet.new([2]),
112-
CustomSet.new([2]).union(CustomSet.new)
113-
assert_equal CustomSet.new([]),
114-
CustomSet.new.union(CustomSet.new)
299+
assert_equal 1, BookKeeping::VERSION
115300
end
116301
end

0 commit comments

Comments
 (0)