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 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602
| #include "stdafx.h"
#include "Pt.h"
#include "Sgt.h"
#include "Rct.h"
#include "figure.h"
#include "graphSgt.h"
static void testPt(void);
static void testSgt(void);
static void testRct(void);
void testPartiel(void);
static void testGraph(void);
void testGraph2();
static void testCloserTo(figure f);
void testPtSgtRct(void)
{
testPt();
testSgt();
testRct();
testGraph();
}
void testPt(void) // Premiers test des points ___________________________________
{
// Construction par défaut et affichage, surcharge ==
pt p0;
cout << "p0 = " << p0 << endl;
assert( p0 == p0 );
// Construction par position
double x = dRand(-10, 20);
double y = dRand(-30, 40);
pt p1(x,y);
cout << "p1 = " << p1 << endl;
assert( EPSEQUAL(p1.getX(), x) );
assert( EPSEQUAL(p1.getY(), y) );
// Distance entre points
double d01 = p0.euclide(p1);
assert( EPSEQUAL(d01*d01, x*x + y*y) );
// Construction par copie implicite
pt p2(p1);
assert( p2 == p1 );
// Affectation implicite
pt p3(dRand(-50, 60), dRand(-70, 80));
cout << "p3 = " << p3 << endl;
p3 = p2;
cout << "p3 = " << p3 << endl;
assert( p3 == p2 );
// Déplacement et distance : on construit un triangle rectangle aléatoire et
// on vérifie le théoreme de Pythagore
pt pT1(dRand(-10, 20), dRand(-30, 40)); // pT1 : sommet de l'angle droit
pt pT2(pT1);
double dx12 = dRand(-50, 60);
pT2.offset( dx12, 0 ); // pT2 : à l'horizontal du sommet de l'angle droit
pt pT3(pT1);
double dy13 = dRand(-70, 80);
pT3.offset( 0, dy13 ); // pT3 à la verticale du sommet de l'angle droit
// Hypothenuse : pT2-pT3
double d12 = pT1.euclide(pT2);
double d13 = pT1.euclide(pT3);
double d23 = pT2.euclide(pT3);
assert( EPSEQUAL(d12*d12 + d13*d13, d23*d23) ); // Pythagore OK ?
// Test du point milieu
pt p6(dRand(-90,100), dRand(-110, 120));
pt p7 = p3.centerPt(p6);
assert( EPSEQUAL(p3.euclide(p7), p3.euclide(p6)/2.0) );
assert( EPSEQUAL(p6.euclide(p7), p3.euclide(p6)/2.0) );
assert( EPSEQUAL( p1.centerPt(p3).euclide(p1) + p1.centerPt(p3).euclide(p3), p1.euclide(p3)) );
// On vérifie closerTo en générant 3 points aléatoires pC1, pC2 et pC3.
// On calcule pClose le point du segment [pC1,pC2] le plus proche de pC3.
// Puis on vérifie qu'une sélection d'autres points aléatoires mais situés
// sur le segment de droite [pC1,pC2] sont tous plus éloignés.
pt pC1(dRand(-10, 20), dRand(-30, 40));
pt pC2(dRand(-40, 50), dRand(-60, 70));
pt pC3(dRand(-80, 90), dRand(-100, 110));
pt ptCloser = pC3.closerTo(pC1, pC2);
double dOpt = pC3.euclide(ptCloser);
for(int i=0; i<100; ++i)
{
d01 = dRand(0, 1);
pt ptTest(pC1);
// On déplace ptTest sur le segment [pC1,pC2]
ptTest.offset( d01*(pC2.getX()-pC1.getX()), d01*(pC2.getY()-pC1.getY()));
double dTest = ptTest.euclide(pC3);
assert( dTest >= dOpt - EPSILON); // EPSILON nécessaire !
}
}
#include "stdafx.h"
#include "Pt.h"
#include "Sgt.h"
#include "Rct.h"
#include "figure.h"
#include "graphSgt.h"
static void testPt(void);
static void testSgt(void);
static void testRct(void);
void testPartiel(void);
static void testGraph(void);
void testGraph2();
static void testCloserTo(figure f);
void testPtSgtRct(void)
{
testPt();
testSgt();
testRct();
testGraph();
}
void testPt(void) // Premiers test des points ___________________________________
{
// Construction par défaut et affichage, surcharge ==
pt p0;
cout << "p0 = " << p0 << endl;
assert( p0 == p0 );
// Construction par position
double x = dRand(-10, 20);
double y = dRand(-30, 40);
pt p1(x,y);
cout << "p1 = " << p1 << endl;
assert( EPSEQUAL(p1.getX(), x) );
assert( EPSEQUAL(p1.getY(), y) );
// Distance entre points
double d01 = p0.euclide(p1);
assert( EPSEQUAL(d01*d01, x*x + y*y) );
// Construction par copie implicite
pt p2(p1);
assert( p2 == p1 );
// Affectation implicite
pt p3(dRand(-50, 60), dRand(-70, 80));
cout << "p3 = " << p3 << endl;
p3 = p2;
cout << "p3 = " << p3 << endl;
assert( p3 == p2 );
// Déplacement et distance : on construit un triangle rectangle aléatoire et
// on vérifie le théoreme de Pythagore
pt pT1(dRand(-10, 20), dRand(-30, 40)); // pT1 : sommet de l'angle droit
pt pT2(pT1);
double dx12 = dRand(-50, 60);
pT2.offset( dx12, 0 ); // pT2 : à l'horizontal du sommet de l'angle droit
pt pT3(pT1);
double dy13 = dRand(-70, 80);
pT3.offset( 0, dy13 ); // pT3 à la verticale du sommet de l'angle droit
// Hypothenuse : pT2-pT3
double d12 = pT1.euclide(pT2);
double d13 = pT1.euclide(pT3);
double d23 = pT2.euclide(pT3);
assert( EPSEQUAL(d12*d12 + d13*d13, d23*d23) ); // Pythagore OK ?
// Test du point milieu
pt p6(dRand(-90,100), dRand(-110, 120));
pt p7 = p3.centerPt(p6);
assert( EPSEQUAL(p3.euclide(p7), p3.euclide(p6)/2.0) );
assert( EPSEQUAL(p6.euclide(p7), p3.euclide(p6)/2.0) );
assert( EPSEQUAL( p1.centerPt(p3).euclide(p1) + p1.centerPt(p3).euclide(p3), p1.euclide(p3)) );
// On vérifie closerTo en générant 3 points aléatoires pC1, pC2 et pC3.
// On calcule pClose le point du segment [pC1,pC2] le plus proche de pC3.
// Puis on vérifie qu'une sélection d'autres points aléatoires mais situés
// sur le segment de droite [pC1,pC2] sont tous plus éloignés.
pt pC1(dRand(-10, 20), dRand(-30, 40));
pt pC2(dRand(-40, 50), dRand(-60, 70));
pt pC3(dRand(-80, 90), dRand(-100, 110));
pt ptCloser = pC3.closerTo(pC1, pC2);
double dOpt = pC3.euclide(ptCloser);
for(int i=0; i<100; ++i)
{
d01 = dRand(0, 1);
pt ptTest(pC1);
// On déplace ptTest sur le segment [pC1,pC2]
ptTest.offset( d01*(pC2.getX()-pC1.getX()), d01*(pC2.getY()-pC1.getY()));
double dTest = ptTest.euclide(pC3);
assert( dTest >= dOpt - EPSILON); // EPSILON nécessaire !
}
}
void testSgt(void) // Les segments _____________________________________________
{
// A faire
// test construction à partir de 2 points pt10 et pt11
sgt s0;
pt p0;
assert(s0.get_p1() == p0);
assert(s0.get_p2() == pt());
pt p10(dRand(-10, 20), dRand(-10, 20));
pt p11(dRand(-10, 20), dRand(-10, 20));
sgt s1(p10, p11);
assert(p10 == s1.get_p1());
assert(p11 == s1.get_p2());
//test centerpoint
pt pC = s1.centerPt();
// PC est equidistant des extremités de s0
double d1 = pC.euclide(s0.get_p1());
double d2 = pC.euclide(s0.get_p2());
assert(EPSEQUAL(d1, d2));
//le pt central doit etre dur le segment s0 : d = 0
double d = s1.euclide(pC);
assert(EPSEQUAL(d, 0));
//test length
sgt s2(s1.get_p1(), pC); //moitié du degment s0
assert(EPSEQUAL(s2.length(), (s1.length())/ 2)); // verification égalité entre moitié de s0 et s0/2
//test offset
sgt s02(s1);
assert(s02 == s1);
double dx = dRand(-10, 20);
double dy = dRand(-10, 20);
s1.offset(dx, dy);
assert(EPSEQUAL(s1.length(), s02.length())); // on verifie la longueur du segment après offset
assert(EPSEQUAL(s1.get_p1().euclide(s02.get_p1()),
sqrt(dx*dx + dy*dy)));
assert(EPSEQUAL(s1.get_p2().euclide(s02.get_p2()),
sqrt(dx*dx + dy*dy)));
s1.offset(-dx, -dy); // on ramene le point a sa valeur initiale
assert(s1 == s02);
pt pa(dRand(-10, 20), dRand(-10, 20));
pt pb(dRand(-10, 20), dRand(-10, 20));
sgt s_inf(pa, pb);
s_inf.inflate(dRand(100));
cout << "s_inf = " << s_inf << endl;
}
void testRct(void)
{
//Variables utilisés pour test constructeur avec 4 params
double x1 = dRand(-1000, 1000);
double x2 = x1 + dRand(1000);
double y1 = dRand(-1000, 1000);
double y2 = y1 + dRand(1000);
//Fin des variables utilisés pour test 4 constructeurs
rct r0;//rectangle utiliser pour tester constructeur
rct r1(x1, x2, y1, y2); // Création du constructeur avec 4 params
//Création de coordonnées de deux points pour test de constructeur avec 2 points
double x1_2 = dRand(-1000, 1000);
double x2_2 = dRand(-1000, 1000);
double y1_2 = dRand(-1000, 1000);
double y2_2 = dRand(-1000, 1000);
//Fin de création de coordonnées de deux points pour test de constructeur avec 2 points
//Création de constructeur à deux params
rct r2(pt(x1_2, y1_2), pt(x2_2, y2_2));
pt p1 = r0.bottomLeft();//Point bas gauche du rectangle r0 (donc (0,0)
pt p2 = r1.bottomLeft();//Point bas gauche du rectangle r1 (donc (x1,y1)
pt p3 = r1.topRight();//Point haut droite du rectangle r1
rct rectverif(p2, p3);//Variable servant à la vérification de bottomleft et topright
// Variable utilisé pour vérifier que CenterPt renvoi le centre
pt centre = r0.centerPt();
pt calculcentre2((x1 + x2) / 2, (y1 + y2) / 2);
pt centre2 = r1.centerPt();
//Variable utilisé pour tester la largeur
double largeur = x2 - x1;
// Variable de détermination de la longueur
double longueur = y2 - y1;
// Variable utilisé pour contains
double xcont = dRand(x1, x2);
double ycont = dRand(y1, y2);
double xhors = x2 + dRand(1000);
double yhors = y1 - dRand(1000);
double x2cont = dRand(xcont, x2);
double y2cont = dRand(ycont, y2);
double x2hors = x1 - dRand(1000);
double y2hors = y1 + dRand(1000);
pt p4(xcont, ycont);
pt p5(x2cont, y2cont);
pt p6(xhors, yhors);
pt p7(x2hors, y2hors);
// verif contains sgt
sgt segcont(p4, p5); // sgt contenu
sgt seghors(p6, p7); // sgt totalement dehors
sgt segmhors(p4, p7); // sgt en partie contenu seulement
// Test du constructeur par defaut
assert(EPSEQUAL(r0.get_x1(), 0));
assert(EPSEQUAL(r0.get_y1(), 0));
assert(EPSEQUAL(r0.get_x2(), 0));
assert(EPSEQUAL(r0.get_y2(), 0));
// Test constructeur 4 params
assert(EPSEQUAL(r1.get_x1(), x1));
assert(EPSEQUAL(r1.get_y1(), y1));
assert(EPSEQUAL(r1.get_x2(), x2));
assert(EPSEQUAL(r1.get_y2(), y2));
//Test de constructeur 2 params
assert(EPSEQUAL(r2.get_x1(), x1_2) || EPSEQUAL(r2.get_x1(), x2_2));
assert(EPSEQUAL(r2.get_x2(), x1_2) || EPSEQUAL(r2.get_x2(), x2_2));
assert(EPSEQUAL(r2.get_y1(), y1_2) || EPSEQUAL(r2.get_y1(), y2_2));
assert(EPSEQUAL(r2.get_y2(), y1_2) || EPSEQUAL(r2.get_y2(), y2_2));
//Test de bottomLeft
assert(EPSEQUAL(p1.getX(), 0));
assert(EPSEQUAL(p1.getY(), 0));
assert(EPSEQUAL(p2.getX(), x1));
assert(EPSEQUAL(p2.getY(), y1));
// vérification topRight
assert(EPSEQUAL(p3.getX(), x2));
assert(EPSEQUAL(p3.getY(), y2));
//vérification de bottomleft et topright en comparant avec rectverif créé avec leur valeur
assert(r1 == rectverif);
// vérification que centerPt renvoi le centre
assert(calculcentre2 == centre2);
assert(centre == pt(0, 0));
//détermination de la largeur
assert(EPSEQUAL(r1.width(), largeur));
// détermination de la longueur
assert(EPSEQUAL(r1.height(), longueur));
// vérification de l'area
assert(EPSEQUAL(r1.area(), longueur * largeur));
assert(r1.contains(p4));
assert(r1.contains(p5));
assert(!r1.contains(p6));
assert(!r1.contains(p7));
// verif contains sgt
assert(r1.contains(segcont));
assert(!r1.contains(seghors));
assert(!r1.contains(segmhors));
// Test de la fonction offset
r2 = r1;
double ox = dRand(-100, 100);
double oy = dRand(-100, 100);
r2.offset(ox, oy);
// Test de la fonction inflate
r2.inflate(dRand(100));
assert(r1.euclide(p6) > 0.0); // point non contenu
assert(EPSEQUAL(r1.euclide(p4), 0.0)); // point contenu
//test de randPt
r2.randPt();
// Test de randSgt
r2.randSgt();
// Test de randRct
r2.randRct();
}
void testGraph(void)
{
sgt s1(pt(0, 0), pt(23, 23));// Segment a ajouter a figure pour test
rct Rclimit(0, dRand(1000, 10000), 0, dRand(1000, 10000));//Création d'un rectangle Rclimit
double dx = dRand(-10000, 10000);
double dy = dRand(-10000, 10000);
double aire_rCadre;
Rclimit.offset(dx,dy);//Déplacement du rectangle sur chaque axe grâce à inflate
//Demander si assert necessaire pour cela
figure fig(Rclimit);//Création d'une figure de Test
fig.add(s1,0);
rct rCadre = Rclimit;
aire_rCadre = rCadre.area();
rCadre.inflate(0.2*((Rclimit.width()+Rclimit.height()) / 2));
//On utilise pas d'assert lors de ce test car l'augmentation est approximative
cout <<"Aire Rclimit="<< Rclimit.area()<<"\n";
cout << "Aire rCadre=" << rCadre.area()<<"\n";
//Nombre aleatoire de segment à ajouter
int nbRand = iRand(10) + 10;
// Utilisation de la figure figCadre pour le test du destructeur de la question 10
{
figure figCadre(rCadre);
for (int i = 0; i < nbRand; i++)
{
sgt aleat = rCadre.randSgt();
unsigned long indexAjout = i;//correspond à pos dans add
bool ajout = figCadre.add(aleat, &indexAjout);
assert(ajout == rCadre.contains(aleat));
}
cout << figCadre << endl;
figure clone(figCadre);
cout << "f Before removing" << endl;
cout << figCadre << endl;
cout << "Clone g printing before removing on f" << endl;
cout << clone << endl;
figCadre.remove();
cout << "f After removing" << endl;
cout << figCadre << endl;
cout << "Clone printing after removing on f" << endl;
cout << clone << endl;
// Répétez 10 fois cette vérification dans une boucle
for (int i = 0; i < 10; i++) {
// Calculer un indice aléatoire de segment de la figure « f »
unsigned long index = iRand(figCadre.getSize());
//définissez le segment correspondant comme segment de référence.
sgt sgtRef;
sgtRef = figCadre.operator [](index).getSegment();
//Choisissez un point aléatoire du segment comme point de référence
pt ptRef = sgtRef.centerPt();
//Trouvez le (un) segment le plus proche du point de référence
unsigned long indexNearSegment = figCadre.closerTo(ptRef);
//Assertez quâil est à une distance nulle du point de référence {1L1A}.
double d = figCadre.operator [](indexNearSegment).getSegment().euclide(ptRef);
assert(d == 0);
assert(indexNearSegment == index);
}
}
}
void testGraph2(void) {
static double MIN = 1000;
static double MAX = 10000;
pt p0(0, 0);
double largeur = dRand(MIN, MAX);
double hauteur = dRand(MIN, MAX);
double x_Centre = (largeur - p0.getX()) / 2;
double y_Centre = (hauteur - p0.getY()) / 2;
pt centre(x_Centre, y_Centre);
rct rcLimit(centre, largeur, hauteur);
double dx = dRand((-1 * MAX), MAX);
double dy = dRand((-1 * MAX), MAX);
rcLimit.offset(dx, dy);
figure f(rcLimit);
rct rCadre = rcLimit;
rCadre.inflatePercent(20);
figure f1(rCadre);
// Ajoutez à la figure de test un nombre aléatoire (compris entre 10 et 20)
// de segments aléatoires inclus dans le rectangle « rCadre ».
int nbRand = iRand(11) + 10;
for (int i = 0; i < nbRand; i++)
{
sgt s = rCadre.randSgt();
unsigned long indexAjout = (unsigned long)i;
f1.add(s, &indexAjout);
}
cout << f1 << endl;
int selected = f1.getNbrSelected();
cout << selected << " segments sélectionnés" << endl;
{
figure fDestructor(f1);
cout << fDestructor << endl;
fDestructor.~figure();
}
figure f2(rCadre);
nbRand = iRand(11) + 10;
for (int i = 0; i < nbRand; i++)
{
sgt s = rCadre.randSgt();
unsigned long indexAjout = (unsigned long)i;
f2.add(s, &indexAjout);
}
cout << f2 << endl;
figure g(f2);
figure f3(f2);
unsigned long selectedf2 = f2.getNbrSelected();
unsigned long sizef2 = f2.getSize();
unsigned long unselectedf2 = sizef2 - selectedf2;
unsigned long deletedf2 = f2.remove();
unsigned long sizef2PostRemove = f2.getSize();
cout << f2 << endl;
assert(deletedf2 == selectedf2);
assert(sizef2 >= sizef2PostRemove);
assert(unselectedf2 == sizef2PostRemove);
assert(f2.getNbrSelected() == 0);
assert(g.getRctLimit() == f3.getRctLimit());
assert(g.getPenWidth() == f3.getPenWidth());
for (unsigned long int i = 0; i< g.getSize(); i++)
{
assert(f3.operator [](i).getSegment() == g.operator [](i).getSegment());
}
unsigned long indexRef = iRand(f3.getSize());
sgt sgtRef = f3.operator [](indexRef).getSegment();
//pt ptRef(dRand(min(sgtRef.get_p1().getX(), sgtRef.get_p2().getX()), max(sgtRef.get_p1().getX(), sgtRef.get_p2().getX())),
// dRand(min(sgtRef.get_p1().getY(), sgtRef.get_p2().getY()), max(sgtRef.get_p1().getY(), sgtRef.get_p2().getY())));
pt ptRef = sgtRef.centerPt();
unsigned long indexCloserSgt = f3.closerTo(ptRef);
assert(EPSEQUAL(f3.operator [](indexCloserSgt).getSegment().euclide(ptRef), 0));
assert(indexCloserSgt == indexRef);
// c. Répétez 10 fois cette vérification dans une boucle {+1L}.
for (int i = 0; i < 10; i++) {
testCloserTo(f3);
}
}
void testCloserTo(figure f) {
unsigned long indexRef = iRand(f.getSize());
sgt sgtRef = f.operator [](indexRef).getSegment();
//pt ptRef(dRand(min(sgtRef.get_p1().getX(), sgtRef.get_p2().getX()), max(sgtRef.get_p1().getX(), sgtRef.get_p2().getX())),
// dRand(min(sgtRef.get_p1().getY(), sgtRef.get_p2().getY()), max(sgtRef.get_p1().getY(), sgtRef.get_p2().getY())));
pt ptRef = sgtRef.centerPt();
unsigned long indexCloserSgt = f.closerTo(ptRef);
double d = f.operator [](indexCloserSgt).getSegment().euclide(ptRef);
assert(EPSEQUAL(d, 0));
assert(indexCloserSgt == indexRef);
}
void testPartiel(void)
{
int i = 0;
int N = 10;
double x1 = dRand(0, 99);
double x2 = x1 + dRand(1000);
double y1 = dRand(0, 199);
double y2 = y1 + dRand(1000);
rct r0(x1, x2, y1, y2);
//Test de notre constructeur:
for (i = 0; i < N; i++)
{
rct rTest(r0.centerPt(), r0.width(), r0.height());
//On test N fois que chaques points soient égaux
assert(EPSEQUAL(r0.get_x1(), rTest.get_x1()));
assert(EPSEQUAL(r0.get_x2(), rTest.get_x2()));
assert(EPSEQUAL(r0.get_y1(), rTest.get_y1()));
assert(EPSEQUAL(r0.get_y2(), rTest.get_y2()));
}
} |
Partager