-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_magic_square.rb
134 lines (110 loc) · 3.3 KB
/
test_magic_square.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
require "minitest/autorun"
require "./magic_square"
describe "OddMagicSquare" do
before do
@order = 3
@size = 9
@sum = 15
@square = MagicSquare::Factory(@order)
end
it 'has a size of 9' do
@square.size.must_equal @size
end
it 'has 3 rows with sum 15' do
(0...@order).each do |i|
(0...@order).inject(0) { |s, j| s + @square[i, j] }.must_equal @sum
end
end
it 'has 3 columns with sum 15' do
(0...@order).each do |i|
(0...@order).inject(0) { |s, j| s + @square[j, i] }.must_equal @sum
end
end
it 'has a diagonal from upper left to lower right with sum 15' do
(0...@order).inject(0) { |s, i| s + @square[i, i] }.must_equal @sum
end
it 'has a diagonal from upper right to lower left with sum 15' do
(0...@order).inject(0) { |s, i| s + @square[i, @order - 1 - i] }.must_equal @sum
end
end
describe "DoublyEvenMagicSquare" do
before do
@order = 4
@size = 16
@sum = 34
@square = MagicSquare::Factory(@order)
end
it 'has a size of 16' do
@square.size.must_equal @size
end
it 'has 4 rows with sum 34' do
(0...@order).each do |i|
(0...@order).inject(0) { |s, j| s + @square[i, j] }.must_equal @sum
end
end
it 'has 4 columns with sum 34' do
(0...@order).each do |i|
(0...@order).inject(0) { |s, j| s + @square[j, i] }.must_equal @sum
end
end
it 'has a diagonal from upper left to lower right with sum 34' do
(0...@order).inject(0) { |s, i| s + @square[i, i] }.must_equal @sum
end
it 'has a diagonal from upper right to lower left with sum 34' do
(0...@order).inject(0) { |s, i| s + @square[i, @order - 1 - i] }.must_equal @sum
end
end
describe "SinglyEvenMagicSquare" do
before do
@order = 6
@size = 36
@sum = 111
@square = MagicSquare::Factory(@order)
end
it 'has a size of 36' do
@square.size.must_equal @size
end
it 'has 6 rows with sum 111' do
(0...@order).each do |i|
(0...@order).inject(0) { |s, j| s + @square[i, j] }.must_equal @sum
end
end
it 'has 6 columns with sum 111' do
(0...@order).each do |i|
(0...@order).inject(0) { |s, j| s + @square[j, i] }.must_equal @sum
end
end
it 'has a diagonal from upper left to lower right with sum 111' do
(0...@order).inject(0) { |s, i| s + @square[i, i] }.must_equal @sum
end
it 'has a diagonal from upper right to lower left with sum 111' do
(0...@order).inject(0) { |s, i| s + @square[i, @order - 1 - i] }.must_equal @sum
end
end
describe "GivenExampleMagicSquare" do
before do
@order = 5
@size = 25
@sum = 65
@square = MagicSquare::Factory(@order)
end
it 'has a size of 25' do
@square.size.must_equal @size
end
it 'has 6 rows with sum 65' do
(0...@order).each do |i|
(0...@order).inject(0) { |s, j| s + @square[i, j] }.must_equal @sum
end
end
it 'has 6 columns with sum 65' do
(0...@order).each do |i|
(0...@order).inject(0) { |s, j| s + @square[j, i] }.must_equal @sum
end
end
it 'has a diagonal from upper left to lower right with sum 65' do
(0...@order).inject(0) { |s, i| s + @square[i, i] }.must_equal @sum
end
it 'has a diagonal from upper right to lower left with sum 65' do
(0...@order).inject(0) { |s, i| s + @square[i, @order - 1 - i] }.must_equal @sum
end
end