Skip to content

Commit 695ff51

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 695ff51

File tree

6 files changed

+374
-69
lines changed

6 files changed

+374
-69
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: 258 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -3,114 +3,303 @@
33
require 'minitest/autorun'
44
require_relative 'custom_set'
55

6+
# rubocop:disable Metrics/LineLength
7+
8+
# Test data version:
9+
# cdfb4a2
610
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])
11+
def test_sets_with_no_elements_are_empty
12+
# skip
13+
set = CustomSet.new []
14+
assert_equal CustomSet.new, set
15+
end
16+
17+
def test_sets_with_elements_are_not_empty
18+
skip
19+
set = CustomSet.new [1]
20+
refute_equal CustomSet.new, set
21+
end
22+
23+
def test_nothing_is_contained_in_an_empty_set
24+
# skip
25+
set = CustomSet.new []
26+
element = 1
27+
refute set.member? element
28+
end
29+
30+
def test_when_the_element_is_in_the_set
31+
skip
32+
set = CustomSet.new [1, 2, 3]
33+
element = 1
34+
assert set.member? element
35+
end
36+
37+
def test_when_the_element_is_not_in_the_set
38+
skip
39+
set = CustomSet.new [1, 2, 3]
40+
element = 4
41+
refute set.member? element
42+
end
43+
44+
def test_empty_set_is_a_subset_of_another_empty_set
45+
# skip
46+
set1 = CustomSet.new []
47+
set2 = CustomSet.new []
48+
assert set2.subset? set1
49+
end
50+
51+
def test_empty_set_is_a_subset_of_non_empty_set
52+
skip
53+
set1 = CustomSet.new []
54+
set2 = CustomSet.new [1]
55+
assert set2.subset? set1
56+
end
57+
58+
def test_non_empty_set_is_not_a_subset_of_empty_set
59+
skip
60+
set1 = CustomSet.new [1]
61+
set2 = CustomSet.new []
62+
refute set2.subset? set1
63+
end
64+
65+
def test_set_is_a_subset_of_set_with_exact_same_elements
66+
skip
67+
set1 = CustomSet.new [1, 2, 3]
68+
set2 = CustomSet.new [1, 2, 3]
69+
assert set2.subset? set1
70+
end
71+
72+
def test_set_is_a_subset_of_larger_set_with_same_elements
73+
skip
74+
set1 = CustomSet.new [1, 2, 3]
75+
set2 = CustomSet.new [4, 1, 2, 3]
76+
assert set2.subset? set1
77+
end
78+
79+
def test_set_is_not_a_subset_of_set_that_does_not_contain_its_elements
80+
skip
81+
set1 = CustomSet.new [1, 2, 3]
82+
set2 = CustomSet.new [4, 1, 3]
83+
refute set2.subset? set1
84+
end
85+
86+
def test_the_empty_set_is_disjoint_with_itself
87+
# skip
88+
set1 = CustomSet.new []
89+
set2 = CustomSet.new []
90+
assert set1.disjoint? set2
91+
end
92+
93+
def test_empty_set_is_disjoint_with_non_empty_set
94+
skip
95+
set1 = CustomSet.new []
96+
set2 = CustomSet.new [1]
97+
assert set1.disjoint? set2
1298
end
1399

14-
def test_no_duplicates
15-
assert_equal CustomSet.new([1, 1]), CustomSet.new([1])
100+
def test_non_empty_set_is_disjoint_with_empty_set
101+
skip
102+
set1 = CustomSet.new [1]
103+
set2 = CustomSet.new []
104+
assert set1.disjoint? set2
16105
end
17106

18-
def test_delete
107+
def test_sets_are_not_disjoint_if_they_share_an_element
19108
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)
109+
set1 = CustomSet.new [1, 2]
110+
set2 = CustomSet.new [2, 3]
111+
refute set1.disjoint? set2
24112
end
25113

26-
def test_difference
114+
def test_sets_are_disjoint_if_they_share_no_elements
27115
skip
28-
assert_equal CustomSet.new([1, 3]),
29-
CustomSet.new([1, 2, 3]).difference(CustomSet.new([2, 4]))
116+
set1 = CustomSet.new [1, 2]
117+
set2 = CustomSet.new [3, 4]
118+
assert set1.disjoint? set2
119+
end
30120

31-
assert_equal CustomSet.new([1, 2.0, 3]),
32-
CustomSet.new([1, 2.0, 3]).difference(CustomSet.new([2, 4]))
121+
def test_empty_sets_are_equal
122+
# skip
123+
set1 = CustomSet.new []
124+
set2 = CustomSet.new []
125+
assert_equal set1, set2
33126
end
34127

35-
def test_disjoint?
128+
def test_empty_set_is_not_equal_to_non_empty_set
36129
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)
130+
set1 = CustomSet.new []
131+
set2 = CustomSet.new [1, 2, 3]
132+
refute_equal set1, set2
41133
end
42134

43-
def test_empty
135+
def test_non_empty_set_is_not_equal_to_empty_set
44136
skip
45-
assert_equal CustomSet.new, CustomSet.new([1, 2]).empty
46-
assert_equal CustomSet.new, CustomSet.new.empty
137+
set1 = CustomSet.new [1, 2, 3]
138+
set2 = CustomSet.new []
139+
refute_equal set1, set2
47140
end
48141

49-
# rubocop:disable Metrics/LineLength
50-
def test_intersection
142+
def test_sets_with_the_same_elements_are_equal
51143
skip
52-
assert_equal CustomSet.new([:a, :c]),
53-
CustomSet.new([:a, :b, :c]).intersection(CustomSet.new([:a, :c, :d]))
144+
set1 = CustomSet.new [1, 2]
145+
set2 = CustomSet.new [2, 1]
146+
assert_equal set1, set2
147+
end
54148

55-
assert_equal CustomSet.new([3]),
56-
CustomSet.new([1, 2, 3]).intersection(CustomSet.new([1.0, 2.0, 3]))
149+
def test_sets_with_different_elements_are_not_equal
150+
skip
151+
set1 = CustomSet.new [1, 2, 3]
152+
set2 = CustomSet.new [1, 2, 4]
153+
refute_equal set1, set2
57154
end
58155

59-
def test_member?
156+
def test_add_to_empty_set
157+
# skip
158+
set = CustomSet.new []
159+
element = 3
160+
expected = CustomSet.new [3]
161+
assert_equal expected, set.put(element)
162+
end
163+
164+
def test_add_to_non_empty_set
60165
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)
166+
set = CustomSet.new [1, 2, 4]
167+
element = 3
168+
expected = CustomSet.new [1, 2, 3, 4]
169+
assert_equal expected, set.put(element)
65170
end
66171

67-
def test_put
172+
def test_adding_an_existing_element_does_not_change_the_set
68173
skip
69-
assert_equal CustomSet.new([1, 2, 3, 4]),
70-
CustomSet.new([1, 2, 4]).put(3)
174+
set = CustomSet.new [1, 2, 3]
175+
element = 3
176+
expected = CustomSet.new [1, 2, 3]
177+
assert_equal expected, set.put(element)
178+
end
71179

72-
assert_equal CustomSet.new([1, 2, 3]),
73-
CustomSet.new([1, 2, 3]).put(3)
180+
def test_intersection_of_two_empty_sets_is_an_empty_set
181+
# skip
182+
set1 = CustomSet.new []
183+
set2 = CustomSet.new []
184+
expected = CustomSet.new []
185+
assert_equal expected, set2.intersection(set1)
186+
end
74187

75-
assert_equal CustomSet.new([1, 2, 3, 3.0]),
76-
CustomSet.new([1, 2, 3]).put(3.0)
188+
def test_intersection_of_an_empty_set_and_non_empty_set_is_an_empty_set
189+
skip
190+
set1 = CustomSet.new []
191+
set2 = CustomSet.new [3, 2, 5]
192+
expected = CustomSet.new []
193+
assert_equal expected, set2.intersection(set1)
194+
end
195+
196+
def test_intersection_of_a_non_empty_set_and_an_empty_set_is_an_empty_set
197+
skip
198+
set1 = CustomSet.new [1, 2, 3, 4]
199+
set2 = CustomSet.new []
200+
expected = CustomSet.new []
201+
assert_equal expected, set2.intersection(set1)
77202
end
78203

79-
def test_size
204+
def test_intersection_of_two_sets_with_no_shared_elements_is_an_empty_set
80205
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
206+
set1 = CustomSet.new [1, 2, 3]
207+
set2 = CustomSet.new [4, 5, 6]
208+
expected = CustomSet.new []
209+
assert_equal expected, set2.intersection(set1)
84210
end
85211

86-
def test_subset?
212+
def test_intersection_of_two_sets_with_shared_elements_is_a_set_of_the_shared_elements
87213
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)
214+
set1 = CustomSet.new [1, 2, 3, 4]
215+
set2 = CustomSet.new [3, 2, 5]
216+
expected = CustomSet.new [2, 3]
217+
assert_equal expected, set2.intersection(set1)
94218
end
95219

96-
def test_to_a
220+
def test_difference_of_two_empty_sets_is_an_empty_set
221+
# skip
222+
set1 = CustomSet.new []
223+
set2 = CustomSet.new []
224+
expected = CustomSet.new([])
225+
assert_equal expected, set1.difference(set2)
226+
end
227+
228+
def test_difference_of_empty_set_and_non_empty_set_is_an_empty_set
97229
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
230+
set1 = CustomSet.new []
231+
set2 = CustomSet.new [3, 2, 5]
232+
expected = CustomSet.new([])
233+
assert_equal expected, set1.difference(set2)
101234
end
102235

103-
def test_union # rubocop:disable Metrics/MethodLength
236+
def test_difference_of_a_non_empty_set_and_an_empty_set_is_the_non_empty_set
237+
skip
238+
set1 = CustomSet.new [1, 2, 3, 4]
239+
set2 = CustomSet.new []
240+
expected = CustomSet.new([1, 2, 3, 4])
241+
assert_equal expected, set1.difference(set2)
242+
end
243+
244+
def test_difference_of_two_non_empty_sets_is_a_set_of_elements_that_are_only_in_the_first_set
245+
skip
246+
set1 = CustomSet.new [3, 2, 1]
247+
set2 = CustomSet.new [2, 4]
248+
expected = CustomSet.new([1, 3])
249+
assert_equal expected, set1.difference(set2)
250+
end
251+
252+
def test_union_of_empty_sets_is_an_empty_set
253+
# skip
254+
set1 = CustomSet.new []
255+
set2 = CustomSet.new []
256+
expected = CustomSet.new([])
257+
assert_equal expected, set1.union(set2)
258+
end
259+
260+
def test_union_of_an_empty_set_and_non_empty_set_is_the_non_empty_set
261+
skip
262+
set1 = CustomSet.new []
263+
set2 = CustomSet.new [2]
264+
expected = CustomSet.new([2])
265+
assert_equal expected, set1.union(set2)
266+
end
267+
268+
def test_union_of_a_non_empty_set_and_empty_set_is_the_non_empty_set
269+
skip
270+
set1 = CustomSet.new [1, 3]
271+
set2 = CustomSet.new []
272+
expected = CustomSet.new([1, 3])
273+
assert_equal expected, set1.union(set2)
274+
end
275+
276+
def test_union_of_non_empty_sets_contains_all_unique_elements
277+
skip
278+
set1 = CustomSet.new [1, 3]
279+
set2 = CustomSet.new [2, 3]
280+
expected = CustomSet.new([3, 2, 1])
281+
assert_equal expected, set1.union(set2)
282+
end
283+
284+
# Problems in exercism evolve over time, as we find better ways to ask
285+
# questions.
286+
# The version number refers to the version of the problem you solved,
287+
# not your solution.
288+
#
289+
# Define a constant named VERSION inside of the top level BookKeeping
290+
# module, which may be placed near the end of your file.
291+
#
292+
# In your file, it will look like this:
293+
#
294+
# module BookKeeping
295+
# VERSION = 1 # Where the version number matches the one in the test.
296+
# end
297+
#
298+
# If you are curious, read more about constants on RubyDoc:
299+
# http://ruby-doc.org/docs/ruby-doc-bundle/UsersGuide/rg/constants.html
300+
301+
def test_bookkeeping
104302
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)
303+
assert_equal 1, BookKeeping::VERSION
115304
end
116305
end

exercises/custom-set/example.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,7 @@ def initialize(input_datum)
9090
@datum = input_datum
9191
end
9292
end
93+
94+
module BookKeeping
95+
VERSION = 1
96+
end

0 commit comments

Comments
 (0)