INFO702 - TPs
Loading...
Searching...
No Matches
testGrayLevelImage2D.cpp
1#include <cassert>
2#include <iostream>
3#include <stdexcept>
4#include "GrayLevelImage2D.hpp"
5
6int main()
7{
8 GrayLevelImage2D img(8, 8, 5); // imagette 8 × 8 remplie de 5
9 assert(img.w() == 8 && img.h() == 8);
10 for (int y = 0; y < img.h(); ++y) {
11 for (int x = 0; x < img.w(); ++x) {
12 assert(img.at(x, y) == 5);
13 // La conversion affiche un entier plutôt qu'un caractère.
14 std::cout << ' ' << int(img.at(x, y));
15 }
16 std::cout << '\n';
17 }
18
19 img.at(2, 3) = 42; // modification d'un pixel par référence
20 const GrayLevelImage2D& cimg = img;
21 assert(cimg.at(2, 3) == 42); // accès en lecture sur une image constante
22
23 GrayLevelImage2D copie = img;
24 copie.at(2, 3) = 7;
25 assert(cimg.at(2, 3) == 42); // la copie possède ses propres pixels
26 copie.fill(12);
27 for (int y = 0; y < copie.h(); ++y)
28 for (int x = 0; x < copie.w(); ++x)
29 assert(copie.at(x, y) == 12);
30
31 GrayLevelImage2D vide;
32 assert(vide.w() == 0 && vide.h() == 0);
33 bool exceptionRecue = false;
34 try {
35 img.at(img.w(), 0); // cette abscisse est hors de l'image
36 } catch (const std::out_of_range&) {
37 exceptionRecue = true;
38 }
39 assert(exceptionRecue);
40 std::cout << "Tests de la partie 2 passes.\n";
41}