Skip to content

Commit 2dffafb

Browse files
committed
Add unit tests
1 parent bfc1ada commit 2dffafb

File tree

1 file changed

+119
-23
lines changed

1 file changed

+119
-23
lines changed

tests/xtd.drawing.unit_tests/src/xtd/drawing/tests/bitmap_tests.cpp

Lines changed: 119 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,39 @@ using namespace xtd::tunit;
1616

1717
namespace xtd::drawing::tests {
1818
class test_class_(bitmap_tests) {
19-
static bitmap create_image() {
20-
auto bmp = bitmap {3, 2};
21-
bmp.set_pixel(0, 0, color::red);
22-
bmp.set_pixel(1, 0, color::lime);
23-
bmp.set_pixel(2, 0, color::blue);
24-
bmp.set_pixel(0, 1, color::aqua);
25-
bmp.set_pixel(1, 1, color::fuchsia);
26-
bmp.set_pixel(2, 1, color::yellow);
19+
static bitmap create_image(const xtd::drawing::size& size) {
20+
if (size.height < 0 || size.height > 4) throw argument_exception {};
21+
if (size.width < 0 || size.width > 4) throw argument_exception {};
22+
auto [w, h] = size;
23+
auto bmp = bitmap {w, h};
24+
25+
if (w >= 1 && h >= 1) bmp.set_pixel(0, 0, color::red);
26+
if (w >= 2 && h >= 1) bmp.set_pixel(1, 0, color::lime);
27+
if (w >= 3 && h >= 1) bmp.set_pixel(2, 0, color::blue);
28+
if (w >= 4 && h >= 1) bmp.set_pixel(3, 0, color::white);
29+
if (w >= 1 && h >= 2) bmp.set_pixel(0, 1, color::aqua);
30+
if (w >= 2 && h >= 2) bmp.set_pixel(1, 1, color::fuchsia);
31+
if (w >= 3 && h >= 2) bmp.set_pixel(2, 1, color::yellow);
32+
if (w >= 4 && h >= 2) bmp.set_pixel(3, 1, color::silver);
33+
if (w >= 1 && h >= 3) bmp.set_pixel(0, 2, color::dark_red);
34+
if (w >= 2 && h >= 3) bmp.set_pixel(1, 2, color::green);
35+
if (w >= 3 && h >= 3) bmp.set_pixel(2, 2, color::navy);
36+
if (w >= 4 && h >= 3) bmp.set_pixel(3, 2, color::gray);
37+
if (w >= 1 && h >= 4) bmp.set_pixel(0, 3, color::teal);
38+
if (w >= 2 && h >= 4) bmp.set_pixel(1, 3, color::purple);
39+
if (w >= 3 && h >= 4) bmp.set_pixel(2, 3, color::olive);
40+
if (w >= 4 && h >= 4) bmp.set_pixel(3, 3, color::black);
2741

28-
/*for (auto y = 0; y < bmp.height(); ++y) {
42+
/*println("bitmap {} x {} : ", w, h);
43+
for (auto y = 0; y < bmp.height(); ++y) {
2944
for (auto x = 0; x < bmp.width(); ++x)
30-
print("0x{:X8} ", bmp.get_pixel(x, y).to_argb());
45+
print(" 0x{:X8}", bmp.get_pixel(x, y).to_argb());
3146
println();
3247
}*/
33-
48+
3449
return bmp;
3550
}
36-
51+
3752
void test_method_(empty) {
3853
auto bmp = bitmap::empty;
3954

@@ -55,7 +70,7 @@ namespace xtd::drawing::tests {
5570
}
5671

5772
void test_method_(create_from_image) {
58-
auto bmp = bitmap {create_image()};
73+
auto bmp = bitmap {create_image({3, 2})};
5974

6075
assert::are_equal(2, bmp.flags());
6176
collection_assert::are_equal({guid("7462dc86-6180-4c7e-8e3f-ee7333a7a483")}, bmp.frame_dimentions_list());
@@ -82,7 +97,7 @@ namespace xtd::drawing::tests {
8297
}
8398

8499
void test_method_(create_from_image_and_size_greather_than_image) {
85-
auto bmp = bitmap {create_image(), {4, 3}};
100+
auto bmp = bitmap {create_image({3, 2}), {4, 3}};
86101

87102
assert::are_equal(2, bmp.flags());
88103
collection_assert::are_equal({guid("7462dc86-6180-4c7e-8e3f-ee7333a7a483")}, bmp.frame_dimentions_list());
@@ -115,7 +130,7 @@ namespace xtd::drawing::tests {
115130
}
116131

117132
void test_method_(create_from_image_and_size_less_than_image) {
118-
auto bmp = bitmap {create_image(), {2, 1}};
133+
auto bmp = bitmap {create_image({3, 2}), {2, 1}};
119134

120135
assert::are_equal(2, bmp.flags());
121136
collection_assert::are_equal({guid("7462dc86-6180-4c7e-8e3f-ee7333a7a483")}, bmp.frame_dimentions_list());
@@ -138,7 +153,7 @@ namespace xtd::drawing::tests {
138153
}
139154

140155
void test_method_(create_from_image_and_size_same_than_image) {
141-
auto bmp = bitmap {create_image(), {3, 2}};
156+
auto bmp = bitmap {create_image({3, 2}), {3, 2}};
142157

143158
assert::are_equal(2, bmp.flags());
144159
collection_assert::are_equal({guid("7462dc86-6180-4c7e-8e3f-ee7333a7a483")}, bmp.frame_dimentions_list());
@@ -165,15 +180,15 @@ namespace xtd::drawing::tests {
165180
}
166181

167182
void test_method_(create_from_image_and_size_with_invalid_height) {
168-
assert::throws<argument_exception>([] {auto bmp = bitmap {create_image(), {1, 0}};});
183+
assert::throws<argument_exception>([] {auto bmp = bitmap {create_image({3, 2}), {1, 0}};});
169184
}
170185

171186
void test_method_(create_from_image_and_size_with_invalid_width) {
172-
assert::throws<argument_exception>([] {auto bmp = bitmap {create_image(), {0, 1}};});
187+
assert::throws<argument_exception>([] {auto bmp = bitmap {create_image({3, 2}), {0, 1}};});
173188
}
174189

175190
void test_method_(create_from_image_width_and_height_greather_than_image) {
176-
auto bmp = bitmap {create_image(), 4, 3};
191+
auto bmp = bitmap {create_image({3, 2}), 4, 3};
177192

178193
assert::are_equal(2, bmp.flags());
179194
collection_assert::are_equal({guid("7462dc86-6180-4c7e-8e3f-ee7333a7a483")}, bmp.frame_dimentions_list());
@@ -206,7 +221,7 @@ namespace xtd::drawing::tests {
206221
}
207222

208223
void test_method_(create_from_image_width_and_height_less_than_image) {
209-
auto bmp = bitmap {create_image(), {2, 1}};
224+
auto bmp = bitmap {create_image({3, 2}), {2, 1}};
210225

211226
assert::are_equal(2, bmp.flags());
212227
collection_assert::are_equal({guid("7462dc86-6180-4c7e-8e3f-ee7333a7a483")}, bmp.frame_dimentions_list());
@@ -229,7 +244,7 @@ namespace xtd::drawing::tests {
229244
}
230245

231246
void test_method_(create_from_image_width_and_height_same_than_image) {
232-
auto bmp = bitmap {create_image(), 3, 2};
247+
auto bmp = bitmap {create_image({3, 2}), 3, 2};
233248

234249
assert::are_equal(2, bmp.flags());
235250
collection_assert::are_equal({guid("7462dc86-6180-4c7e-8e3f-ee7333a7a483")}, bmp.frame_dimentions_list());
@@ -256,11 +271,11 @@ namespace xtd::drawing::tests {
256271
}
257272

258273
void test_method_(create_from_image_width_and_invalid_height) {
259-
assert::throws<argument_exception>([] {auto bmp = bitmap {create_image(), 1, 0};});
274+
assert::throws<argument_exception>([] {auto bmp = bitmap {create_image({3, 2}), 1, 0};});
260275
}
261276

262277
void test_method_(create_from_image_invalid_width_and_height) {
263-
assert::throws<argument_exception>([] {auto bmp = bitmap {create_image(), 0, 1};});
278+
assert::throws<argument_exception>([] {auto bmp = bitmap {create_image({3, 2}), 0, 1};});
264279
}
265280

266281
void test_method_(create_from_file_bmp) {
@@ -290,6 +305,60 @@ namespace xtd::drawing::tests {
290305
assert::are_equal(color::yellow, bmp.get_pixel(2, 1));
291306
}
292307

308+
void test_method_(create_from_file_gif) {
309+
auto bmp = bitmap {path::combine(environment::get_folder_path(environment::special_folder::application_resources), "bitmap_3_2.gif")};
310+
311+
assert::are_equal(2, bmp.flags());
312+
collection_assert::are_equal({guid("6aedbd6d-3fb5-418a-83a6-7f45229dc872")}, bmp.frame_dimentions_list());
313+
assert::is_not_zero(bmp.handle());
314+
assert::are_equal(2, bmp.height());
315+
//assert::are_equal(96.0f, bmp.horizontal_resolution(), 1.0f);
316+
collection_assert::is_empty(bmp.palette().entries());
317+
assert::are_equal(size_f {3., 2.}, bmp.physical_dimension());
318+
assert::are_equal(imaging::pixel_format::format_32bpp_argb, bmp.pixel_format());
319+
collection_assert::is_empty(bmp.property_id_list());
320+
collection_assert::is_empty(bmp.property_items());
321+
assert::are_equal(imaging::image_format::gif(), bmp.raw_format());
322+
assert::are_equal(size {3, 2}, bmp.size());
323+
assert::is_false(bmp.tag().has_value());
324+
//assert::are_equal(96.0f, bmp.vertical_resolution(), 1.0f);
325+
assert::are_equal(3, bmp.width());
326+
327+
assert::are_equal(color::red, bmp.get_pixel(0, 0));
328+
assert::are_equal(color::lime, bmp.get_pixel(1, 0));
329+
assert::are_equal(color::blue, bmp.get_pixel(2, 0));
330+
assert::are_equal(color::aqua, bmp.get_pixel(0, 1));
331+
assert::are_equal(color::fuchsia, bmp.get_pixel(1, 1));
332+
assert::are_equal(color::yellow, bmp.get_pixel(2, 1));
333+
}
334+
335+
void test_method_(create_from_file_exif) {
336+
auto bmp = bitmap {path::combine(environment::get_folder_path(environment::special_folder::application_resources), "bitmap_3_2.exif")};
337+
338+
assert::are_equal(2, bmp.flags());
339+
collection_assert::are_equal({guid("7462dc86-6180-4c7e-8e3f-ee7333a7a483")}, bmp.frame_dimentions_list());
340+
assert::is_not_zero(bmp.handle());
341+
assert::are_equal(2, bmp.height());
342+
//assert::are_equal(96.0f, bmp.horizontal_resolution(), 1.0f);
343+
collection_assert::is_empty(bmp.palette().entries());
344+
assert::are_equal(size_f {3., 2.}, bmp.physical_dimension());
345+
assert::are_equal(imaging::pixel_format::format_32bpp_argb, bmp.pixel_format());
346+
collection_assert::is_empty(bmp.property_id_list());
347+
collection_assert::is_empty(bmp.property_items());
348+
assert::are_equal(imaging::image_format::exif(), bmp.raw_format());
349+
assert::are_equal(size {3, 2}, bmp.size());
350+
assert::is_false(bmp.tag().has_value());
351+
//assert::are_equal(96.0f, bmp.vertical_resolution(), 1.0f);
352+
assert::are_equal(3, bmp.width());
353+
354+
assert::are_equal(color::red, bmp.get_pixel(0, 0));
355+
assert::are_equal(color::lime, bmp.get_pixel(1, 0));
356+
assert::are_equal(color::blue, bmp.get_pixel(2, 0));
357+
assert::are_equal(color::aqua, bmp.get_pixel(0, 1));
358+
assert::are_equal(color::fuchsia, bmp.get_pixel(1, 1));
359+
assert::are_equal(color::yellow, bmp.get_pixel(2, 1));
360+
}
361+
293362
void test_method_(create_from_file_jpg) {
294363
auto bmp = bitmap {path::combine(environment::get_folder_path(environment::special_folder::application_resources), "bitmap_3_2.jpg")};
295364

@@ -344,6 +413,33 @@ namespace xtd::drawing::tests {
344413
assert::are_equal(color::yellow, bmp.get_pixel(2, 1));
345414
}
346415

416+
void test_method_(create_from_file_tiff) {
417+
auto bmp = bitmap {path::combine(environment::get_folder_path(environment::special_folder::application_resources), "bitmap_3_2.tiff")};
418+
419+
assert::are_equal(2, bmp.flags());
420+
collection_assert::are_equal({guid("7462dc86-6180-4c7e-8e3f-ee7333a7a483")}, bmp.frame_dimentions_list());
421+
assert::is_not_zero(bmp.handle());
422+
assert::are_equal(2, bmp.height());
423+
//assert::are_equal(96.0f, bmp.horizontal_resolution(), 1.0f);
424+
collection_assert::is_empty(bmp.palette().entries());
425+
assert::are_equal(size_f {3., 2.}, bmp.physical_dimension());
426+
assert::are_equal(imaging::pixel_format::format_32bpp_argb, bmp.pixel_format());
427+
collection_assert::is_empty(bmp.property_id_list());
428+
collection_assert::is_empty(bmp.property_items());
429+
assert::are_equal(imaging::image_format::tiff(), bmp.raw_format());
430+
assert::are_equal(size {3, 2}, bmp.size());
431+
assert::is_false(bmp.tag().has_value());
432+
//assert::are_equal(96.0f, bmp.vertical_resolution(), 1.0f);
433+
assert::are_equal(3, bmp.width());
434+
435+
assert::are_equal(color::red, bmp.get_pixel(0, 0));
436+
assert::are_equal(color::lime, bmp.get_pixel(1, 0));
437+
assert::are_equal(color::blue, bmp.get_pixel(2, 0));
438+
assert::are_equal(color::aqua, bmp.get_pixel(0, 1));
439+
assert::are_equal(color::fuchsia, bmp.get_pixel(1, 1));
440+
assert::are_equal(color::yellow, bmp.get_pixel(2, 1));
441+
}
442+
347443
void test_method_(create_with_with_and_heght) {
348444
auto bmp = bitmap {3, 2};
349445

0 commit comments

Comments
 (0)