comparison examples/image/main.cpp @ 80:a162f380f02e

CMake: create examples, closes #615
author David Demelier <markand@malikania.fr>
date Sun, 22 Jan 2017 09:59:14 +0100
parents 78de82cc6bde
children 301599387b40
comparison
equal deleted inserted replaced
79:8b41e9a2e095 80:a162f380f02e
1 /* 1 /*
2 * main.cpp -- test Image 2 * main.cpp -- test Image
3 * 3 *
4 * Copyright (c) 2013-2016 Malikania Authors 4 * Copyright (c) 2013-2017 Malikania Authors
5 * 5 *
6 * Permission to use, copy, modify, and/or distribute this software for any 6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above 7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies. 8 * copyright notice and this permission notice appear in all copies.
9 * 9 *
17 */ 17 */
18 18
19 #include <chrono> 19 #include <chrono>
20 #include <thread> 20 #include <thread>
21 21
22 #define BOOST_TEST_MODULE "Image"
23 #include <boost/test/unit_test.hpp>
24
25 #include <malikania/client_resources_loader.hpp> 22 #include <malikania/client_resources_loader.hpp>
26 #include <malikania/image.hpp> 23 #include <malikania/image.hpp>
27 #include <malikania/resources_locator.hpp> 24 #include <malikania/resources_locator.hpp>
28 #include <malikania/window.hpp> 25 #include <malikania/window.hpp>
29 26
30 using namespace std::chrono_literals; 27 using namespace std::chrono_literals;
31 28
32 namespace { 29 void draw(mlk::window& window, mlk::client_resources_loader& loader)
33
34 mlk::window window(400, 400);
35
36 } // !namespace
37
38 class test_image {
39 protected:
40 mlk::directory_resources_locator m_locator;
41 mlk::client_resources_loader m_loader;
42
43 public:
44 test_image()
45 : m_locator(SOURCE_DIRECTORY "/resources")
46 , m_loader(m_locator)
47 {
48 }
49 };
50
51 BOOST_FIXTURE_TEST_SUITE(test_image_suite, test_image)
52
53 BOOST_AUTO_TEST_CASE(image100x10)
54 { 30 {
55 try { 31 try {
56 auto image = m_loader.load_image("images/100x10.png"); 32 auto image = loader.load_image("images/smiley.png");
57
58 BOOST_REQUIRE_EQUAL(100U, image.size().width());
59 BOOST_REQUIRE_EQUAL(10U, image.size().height());
60 } catch (const std::exception &ex) {
61 BOOST_FAIL(ex.what());
62 }
63 }
64
65 BOOST_AUTO_TEST_CASE(image16x16)
66 {
67 try {
68 auto image = m_loader.load_image("images/16x16.png");
69
70 BOOST_REQUIRE_EQUAL(16U, image.size().width());
71 BOOST_REQUIRE_EQUAL(16U, image.size().height());
72 } catch (const std::exception &ex) {
73 BOOST_FAIL(ex.what());
74 }
75 }
76
77 BOOST_AUTO_TEST_CASE(image10x100)
78 {
79 try {
80 auto image = m_loader.load_image("images/10x100.png");
81
82 BOOST_REQUIRE_EQUAL(10U, image.size().width());
83 BOOST_REQUIRE_EQUAL(100U, image.size().height());
84 } catch (const std::exception &ex) {
85 BOOST_FAIL(ex.what());
86 }
87 }
88
89 BOOST_AUTO_TEST_CASE(notfound)
90 {
91 BOOST_REQUIRE_THROW(m_loader.load_image("image-not-found"), std::exception);
92 }
93
94 BOOST_AUTO_TEST_CASE(draw)
95 {
96 try {
97 auto image = m_loader.load_image("images/smiley.png");
98 auto x = (400 / 2) - (image.size().width() / 2); 33 auto x = (400 / 2) - (image.size().width() / 2);
99 auto y = (400 / 2) - (image.size().height() / 2); 34 auto y = (400 / 2) - (image.size().height() / 2);
100 35
101 window.clear(); 36 window.clear();
102 image.draw(window, mlk::point(x, y)); 37 image.draw(window, mlk::point(x, y));
103 window.present(); 38 window.present();
104 39
105 std::this_thread::sleep_for(3s); 40 std::this_thread::sleep_for(3s);
106 } catch (const std::exception &ex) { 41 } catch (const std::exception &ex) {
107 BOOST_FAIL(ex.what()); 42 std::cerr << ex.what() << std::endl;
43 std::exit(1);
108 } 44 }
109 } 45 }
110 46
111 BOOST_AUTO_TEST_SUITE_END() 47 int main()
48 {
49 try {
50 mlk::window window(400, 400);
51 mlk::directory_resources_locator locator(SOURCE_DIRECTORY);
52 mlk::client_resources_loader loader(locator);
53
54 draw(window, loader);
55 } catch (const std::exception& ex) {
56 std::cerr << ex.what() << std::endl;
57 return 1;
58 }
59 }