Skip to content

Commit b50beb7

Browse files
committed
Update tests.
1 parent d9efe3a commit b50beb7

File tree

5 files changed

+157
-141
lines changed

5 files changed

+157
-141
lines changed

test/bitmap_test.rb

Lines changed: 54 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,26 @@
66
class BitmapTest < Minitest::Test
77
def test_default_constructor
88
image = Bmp::Bitmap.new
9-
assert_equal 0, image.width
10-
assert_equal 0, image.height
11-
assert image.empty?
9+
assert_equal(0, image.width)
10+
assert_equal(0, image.height)
11+
assert(image.empty?)
1212
end
1313

1414
def test_dimensions_constructor
1515
image = Bmp::Bitmap.new(640, 480)
1616
assert_equal(640, image.width)
1717
assert_equal(480, image.height)
18-
refute image.empty?
18+
refute(image.empty?)
1919
end
2020

2121
def test_clone
2222
original = Bmp::Bitmap.new(100, 100)
2323
original.set(50, 50, Bmp::Red)
2424

2525
copy = original.clone
26-
assert_equal 100, copy.width
27-
assert_equal 100, copy.height
28-
assert_equal Bmp::Red.r, copy.get(50, 50).r
26+
assert_equal(100, copy.width)
27+
assert_equal(100, copy.height)
28+
assert_equal(Bmp::Red.r, copy.get(50, 50).r)
2929
end
3030

3131
def test_clear_default
@@ -34,29 +34,29 @@ def test_clear_default
3434

3535
# Should be black by default
3636
pixel = image.get(5, 5)
37-
assert_equal 0, pixel.r
38-
assert_equal 0, pixel.g
39-
assert_equal 0, pixel.b
37+
assert_equal(0, pixel.r)
38+
assert_equal(0, pixel.g)
39+
assert_equal(0, pixel.b)
4040
end
4141

4242
def test_clear_with_color
4343
image = Bmp::Bitmap.new(10, 10)
4444
image.clear(Bmp::Red)
4545

4646
pixel = image.get(5, 5)
47-
assert_equal 255, pixel.r
48-
assert_equal 0, pixel.g
49-
assert_equal 0, pixel.b
47+
assert_equal(255, pixel.r)
48+
assert_equal(0, pixel.g)
49+
assert_equal(0, pixel.b)
5050
end
5151

5252
def test_get_and_set
5353
image = Bmp::Bitmap.new(10, 10)
5454
image.set(5, 5, Bmp::Blue)
5555

5656
pixel = image.get(5, 5)
57-
assert_equal Bmp::Blue.r, pixel.r
58-
assert_equal Bmp::Blue.g, pixel.g
59-
assert_equal Bmp::Blue.b, pixel.b
57+
assert_equal(Bmp::Blue.r, pixel.r)
58+
assert_equal(Bmp::Blue.g, pixel.g)
59+
assert_equal(Bmp::Blue.b, pixel.b)
6060
end
6161

6262
def test_array_access
@@ -65,19 +65,19 @@ def test_array_access
6565

6666
# Linear index = x + width * y = 5 + 10 * 5 = 55
6767
pixel = image[55]
68-
assert_equal Bmp::Green.r, pixel.r
69-
assert_equal Bmp::Green.g, pixel.g
70-
assert_equal Bmp::Green.b, pixel.b
68+
assert_equal(Bmp::Green.r, pixel.r)
69+
assert_equal(Bmp::Green.g, pixel.g)
70+
assert_equal(Bmp::Green.b, pixel.b)
7171
end
7272

7373
def test_array_assignment
7474
image = Bmp::Bitmap.new(10, 10)
7575
image[55] = Bmp::Yellow
7676

7777
pixel = image.get(5, 5)
78-
assert_equal Bmp::Yellow.r, pixel.r
79-
assert_equal Bmp::Yellow.g, pixel.g
80-
assert_equal Bmp::Yellow.b, pixel.b
78+
assert_equal(Bmp::Yellow.r, pixel.r)
79+
assert_equal(Bmp::Yellow.g, pixel.g)
80+
assert_equal(Bmp::Yellow.b, pixel.b)
8181
end
8282

8383
def test_equality
@@ -90,32 +90,48 @@ def test_equality
9090
image3 = Bmp::Bitmap.new(10, 10)
9191
image3.clear(Bmp::Blue)
9292

93-
assert image1 == image2
94-
assert image1 != image3
93+
assert(image1 == image2)
94+
assert(image1 != image3)
9595
end
9696

9797
def test_each_iterator
98-
image = Bmp::Bitmap.new(10, 10)
98+
image = Bmp::Bitmap.new(2, 2)
9999
count = 0
100100

101-
image.each do |pixel|
102-
count += 1
103-
pixel.r = 128
101+
image.each.with_index do |pixel, i|
102+
count +=1
103+
pixel.r = i
104+
pixel.g = i + 1
105+
pixel.b = i + 2
104106
end
105107

106-
assert_equal 100, count
107-
assert_equal 128, image.get(0, 0).r
108+
assert_equal(4, count)
109+
expected = [Bmp::Pixel.new(red: 0, green: 1, blue: 2),
110+
Bmp::Pixel.new(red: 1, green: 2, blue: 3),
111+
Bmp::Pixel.new(red: 2, green: 3, blue: 4),
112+
Bmp::Pixel.new(red: 3, green: 4, blue: 5)]
113+
114+
assert_equal(expected, image.each.to_a)
108115
end
109116

110117
def test_each_reverse_iterator
111-
image = Bmp::Bitmap.new(10, 10)
112-
count = 0
113-
114-
image.each_reverse do |_pixel|
115-
count += 1
116-
end
117-
118-
assert_equal 100, count
118+
image = Bmp::Bitmap.new(2, 2)
119+
count = 0
120+
121+
image.each_reverse.with_index do |pixel, i|
122+
count +=1
123+
pixel.r = i
124+
pixel.g = i + 1
125+
pixel.b = i + 2
126+
end
127+
128+
assert_equal(4, count)
129+
expected = [Bmp::Pixel.new(red: 3, green: 4, blue: 5),
130+
Bmp::Pixel.new(red: 2, green: 3, blue: 4),
131+
Bmp::Pixel.new(red: 1, green: 2, blue: 3),
132+
Bmp::Pixel.new(red: 0, green: 1, blue: 2)]
133+
134+
assert_equal(expected, image.each.to_a)
119135
end
120136

121137
def test_inspect

test/draw_primitives_test.rb

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,67 +12,67 @@ def test_draw_line
1212
@image.draw_line(0, 0, 99, 99, Bmp::Red)
1313

1414
# Check that line pixels are set
15-
assert_equal Bmp::Red.r, @image.get(0, 0).r
16-
assert_equal Bmp::Red.r, @image.get(50, 50).r
17-
assert_equal Bmp::Red.r, @image.get(99, 99).r
15+
assert_equal(Bmp::Red.r, @image.get(0, 0).r)
16+
assert_equal(Bmp::Red.r, @image.get(50, 50).r)
17+
assert_equal(Bmp::Red.r, @image.get(99, 99).r)
1818
end
1919

2020
def test_draw_rect
2121
@image.draw_rect(10, 10, 20, 20, Bmp::Blue)
2222

2323
# Check corners
24-
assert_equal Bmp::Blue.b, @image.get(10, 10).b
25-
assert_equal Bmp::Blue.b, @image.get(29, 10).b
26-
assert_equal Bmp::Blue.b, @image.get(10, 29).b
27-
assert_equal Bmp::Blue.b, @image.get(29, 29).b
24+
assert_equal(Bmp::Blue.b, @image.get(10, 10).b)
25+
assert_equal(Bmp::Blue.b, @image.get(29, 10).b)
26+
assert_equal(Bmp::Blue.b, @image.get(10, 29).b)
27+
assert_equal(Bmp::Blue.b, @image.get(29, 29).b)
2828

2929
# Check that interior is not filled
30-
assert_equal Bmp::White.r, @image.get(15, 15).r
30+
assert_equal(Bmp::White.r, @image.get(15, 15).r)
3131
end
3232

3333
def test_fill_rect
3434
@image.fill_rect(10, 10, 20, 20, Bmp::Green)
3535

3636
# Check interior is filled
37-
assert_equal Bmp::Green.g, @image.get(15, 15).g
38-
assert_equal Bmp::Green.g, @image.get(20, 20).g
37+
assert_equal(Bmp::Green.g, @image.get(15, 15).g)
38+
assert_equal(Bmp::Green.g, @image.get(20, 20).g)
3939
end
4040

4141
def test_draw_triangle
4242
@image.draw_triangle(50, 10, 10, 90, 90, 90, Bmp::Cyan)
4343

4444
# Check vertices
45-
assert_equal Bmp::Cyan.r, @image.get(50, 10).r
46-
assert_equal Bmp::Cyan.r, @image.get(10, 90).r
47-
assert_equal Bmp::Cyan.r, @image.get(90, 90).r
45+
assert_equal(Bmp::Cyan.r, @image.get(50, 10).r)
46+
assert_equal(Bmp::Cyan.r, @image.get(10, 90).r)
47+
assert_equal(Bmp::Cyan.r, @image.get(90, 90).r)
4848
end
4949

5050
def test_fill_triangle
5151
@image.fill_triangle(50, 10, 10, 90, 90, 90, Bmp::Magenta)
5252

5353
# Check interior is filled (center of triangle)
54-
assert_equal Bmp::Magenta.r, @image.get(50, 60).r
54+
assert_equal(Bmp::Magenta.r, @image.get(50, 60).r)
5555
end
5656

5757
def test_draw_circle
5858
@image.draw_circle(50, 50, 20, Bmp::Yellow)
5959

6060
# Check points on circle edge
61-
assert_equal Bmp::Yellow.r, @image.get(50, 30).r # Top
62-
assert_equal Bmp::Yellow.r, @image.get(50, 70).r # Bottom
63-
assert_equal Bmp::Yellow.r, @image.get(30, 50).r # Left
64-
assert_equal Bmp::Yellow.r, @image.get(70, 50).r # Right
61+
assert_equal(Bmp::Yellow.r, @image.get(50, 30).r) # Top
62+
assert_equal(Bmp::Yellow.r, @image.get(50, 70).r) # Bottom
63+
assert_equal(Bmp::Yellow.r, @image.get(30, 50).r) # Left
64+
assert_equal(Bmp::Yellow.r, @image.get(70, 50).r) # Right
6565

6666
# Check center is not filled
67-
assert_equal Bmp::White.r, @image.get(50, 50).r
67+
assert_equal(Bmp::White.r, @image.get(50, 50).r)
6868
end
6969

7070
def test_fill_circle
7171
@image.fill_circle(50, 50, 20, Bmp::Orange)
7272

7373
# Check center is filled
74-
assert_equal Bmp::Orange.r, @image.get(50, 50).r
75-
assert_equal Bmp::Orange.r, @image.get(45, 50).r
74+
assert_equal(Bmp::Orange.r, @image.get(50, 50).r)
75+
assert_equal(Bmp::Orange.r, @image.get(45, 50).r)
7676
end
7777

7878
def test_out_of_bounds_raises_exception

test/file_io_test.rb

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ def test_save_and_load
2626
assert(File.size(path) > 0)
2727

2828
loaded = Bmp::Bitmap.new(path)
29-
assert_equal 100, loaded.width
30-
assert_equal 50, loaded.height
31-
assert_equal Bmp::Red.r, loaded.get(0, 0).r
32-
assert_equal Bmp::Blue.b, loaded.get(50, 25).b
29+
assert_equal(100, loaded.width)
30+
assert_equal(50, loaded.height)
31+
assert_equal(Bmp::Red.r, loaded.get(0, 0).r)
32+
assert_equal(Bmp::Blue.b, loaded.get(50, 25).b)
3333
end
3434

3535
def test_load_into_existing_bitmap
@@ -45,8 +45,8 @@ def test_load_into_existing_bitmap
4545
std_path = Std::Filesystem::Path.new(path)
4646
image.load(std_path)
4747

48-
assert_equal 100, image.width
49-
assert_equal 100, image.height
48+
assert_equal(100, image.width)
49+
assert_equal(100, image.height)
5050
end
5151

5252
def test_load_nonexistent_file_raises
@@ -74,7 +74,7 @@ def test_bitmap_file_format
7474
# Check BMP magic number
7575
File.open(path, "rb") do |f|
7676
magic = f.read(2)
77-
assert_equal "BM", magic
77+
assert_equal("BM", magic)
7878
end
7979
end
8080

@@ -100,9 +100,9 @@ def test_roundtrip_preserves_pixels
100100
50.times do |x|
101101
orig_pixel = original.get(x, y)
102102
load_pixel = loaded.get(x, y)
103-
assert_equal orig_pixel.r, load_pixel.r, "Mismatch at (#{x}, #{y})"
104-
assert_equal orig_pixel.g, load_pixel.g, "Mismatch at (#{x}, #{y})"
105-
assert_equal orig_pixel.b, load_pixel.b, "Mismatch at (#{x}, #{y})"
103+
assert_equal(orig_pixel.r, load_pixel.r, "Mismatch at (#{x}, #{y})")
104+
assert_equal(orig_pixel.g, load_pixel.g, "Mismatch at (#{x}, #{y})")
105+
assert_equal(orig_pixel.b, load_pixel.b, "Mismatch at (#{x}, #{y})")
106106
end
107107
end
108108
end

0 commit comments

Comments
 (0)