test/dim_test.cc
author Alpar Juttner <alpar@cs.elte.hu>
Thu, 07 Feb 2008 22:25:42 +0000
changeset 68 a315a588a20d
parent 14 8685efdef52f
child 171 02f4d5d9bfd7
permissions -rw-r--r--
Some usefull math constants

lemon/math.h also includes the standard cmath, so one should prefer using
just lemon/math.h instead of cmath.
     1 /* -*- C++ -*-
     2  *
     3  * This file is a part of LEMON, a generic C++ optimization library
     4  *
     5  * Copyright (C) 2003-2008
     6  * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
     7  * (Egervary Research Group on Combinatorial Optimization, EGRES).
     8  *
     9  * Permission to use, modify and distribute this software is granted
    10  * provided that this copyright notice appears in all copies. For
    11  * precise terms see the accompanying LICENSE file.
    12  *
    13  * This software is provided "AS IS" with no warranty of any kind,
    14  * express or implied, and with no claim as to its suitability for any
    15  * purpose.
    16  *
    17  */
    18 
    19 #include <lemon/dim2.h>
    20 #include <iostream>
    21 #include "test_tools.h"
    22 
    23 using namespace std;
    24 using namespace lemon;
    25 
    26 int main()
    27 {
    28   cout << "Testing classes 'dim2::Point' and 'dim2::BoundingBox'." << endl;
    29 
    30   typedef dim2::Point<int> Point;
    31 
    32   Point p;
    33   check(p.size()==2, "Wrong vector initialization.");
    34 
    35   Point a(1,2);
    36   Point b(3,4);
    37   check(a[0]==1 && a[1]==2, "Wrong vector initialization.");
    38 
    39   p = a+b;
    40   check(p.x==4 && p.y==6, "Wrong vector addition.");
    41 
    42   p = a-b;
    43   check(p.x==-2 && p.y==-2, "Wrong vector subtraction.");
    44 
    45   check(a.normSquare()==5,"Wrong vector norm calculation.");
    46   check(a*b==11, "Wrong vector scalar product.");
    47 
    48   int l=2;
    49   p = a*l;
    50   check(p.x==2 && p.y==4, "Wrong vector multiplication by a scalar.");
    51 
    52   p = b/l;
    53   check(p.x==1 && p.y==2, "Wrong vector division by a scalar.");
    54 
    55   typedef dim2::BoundingBox<int> BB;
    56   BB box1;
    57   check(box1.empty(), "It should be empty.");
    58 
    59   box1.add(a);
    60   check(!box1.empty(), "It should not be empty.");
    61   box1.add(b);
    62 
    63   check(box1.bottomLeft().x==1 &&
    64         box1.bottomLeft().y==2 &&
    65         box1.topRight().x==3 &&
    66         box1.topRight().y==4,
    67         "Wrong addition of points to box.");
    68 
    69   p.x=2; p.y=3;
    70   check(box1.inside(p), "It should be inside.");
    71 
    72   p.x=1; p.y=3;
    73   check(box1.inside(p), "It should be inside.");
    74 
    75   p.x=0; p.y=3;
    76   check(!box1.inside(p), "It should not be inside.");
    77 
    78   BB box2(p);
    79   check(!box2.empty(),
    80         "It should not be empty. Constructed from 1 point.");
    81 
    82   box2.add(box1);
    83   check(box2.inside(p),
    84         "It should be inside. Incremented a box with another one.");
    85 
    86   return 0;
    87 }