-
Notifications
You must be signed in to change notification settings - Fork 12
/
test_sorted_set.rb
95 lines (77 loc) · 2.1 KB
/
test_sorted_set.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
require 'test/unit/assertions.rb'
include Test::Unit::Assertions
class BasicSortedSet
# Create your internal data structure here. It should be an empty array
def initialize
@ary = []
end
# Use the array's native insert method
def insert(element)
if @ary.include? element
return false
else
@ary << element
return element
end
end
# Use the array's native include method
def include?(element)
@ary.include? element
end
# Use the array's native sort method
def get_sorted_array
@ary.sort
end
end
class TestSortedSet
def initialize(set_type)
@test_set_type = set_type
@set_type = BasicSortedSet
end
def test
puts "="*10
puts "Testing #{@test_set_type.new.name}"
puts "="*10
test_insert_include
end
def test_insert_include
test_set = @test_set_type.new
set = @set_type.new
100.times do |x|
decision = (rand * 100).to_i
if decision % 2 == 0
a = test_set.insert(x)
b = set.insert(x)
assert a == b, "Inserting #{x} failed: \nGot: #{a}\nExpected: #{b}"
end
end
puts "Inserting once works!"
100.times do |x|
decision = (rand * 100).to_i % 2
if decision
a = test_set.insert(x)
b = set.insert(x)
assert a == b, "Inserting #{x} a second time failed: \nGot: #{a}\nExpected: #{b}"
end
end
puts "Inserting twice works properly!"
100.times do |x|
assert test_set.include?(x) == set.include?(x), "Include? for #{x} gives wrong result"
end
puts "Inclusion works properly!"
test_set = @test_set_type.new
set = @set_type.new
100.times do
x = (rand * 1000).to_i
set.insert x
test_set.insert x
end
assert test_set.get_sorted_array == set.get_sorted_array, "Sorting the array failed. Here is the array #{test_set.get_sorted_array}"
puts "Sorting works properly!"
end
end
require_relative 'sorted_set.rb'
TestSortedSet.new(BasicArraySortedSet).test
TestSortedSet.new(HashSortedSet).test
TestSortedSet.new(ArraySortedSet).test
TestSortedSet.new(BinaryArraySortedSet).test