1 //  This file is part of the computer game Kartofel.
2 //  Copyright (C) 2008 Pawel Aleksander Fedorynski <pfedor@fuw.edu.pl>
3 //
4 //  This program is free software; you can redistribute it and/or modify
5 //  it under the terms of the GNU General Public License as published by
6 //  the Free Software Foundation; either version 2 of the License, or
7 //  (at your option) any later version.
8 //
9 //  This program is distributed in the hope that it will be useful,
10 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
11 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 //  GNU General Public License for more details.
13 //
14 //  You should have received a copy of the GNU General Public License
15 //  along with this program; if not, write to the Free Software
16 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17 
18 #include <cstdlib>
19 #include <iostream>
20 #include <sstream>
21 #include <string>
22 
23 #include "util.h"
24 
25 using namespace kartofel;
26 using namespace std;
27 
28 namespace {
29 
TestFI(int x0,int y0,int x1,int y1,int xc,int yc,int r,int res_size,int res0x=0,int res0y=0,int res1x=0,int res1y=0)30 void TestFI(int x0, int y0, int x1, int y1, int xc, int yc, int r,
31             int res_size, int res0x = 0, int res0y = 0,
32             int res1x = 0, int res1y = 0)
33 {
34   if (res_size == 2 &&
35       (res0x > res1x || (res0x == res1x && res0y > res1y))) {
36     swap(res0x, res1x);
37     swap(res0y, res1y);
38   }
39   vector<Point> res;
40   stringstream ss;
41   ss << "x0: " << x0 << ", y0: " << y0 << ", x1: " << x1 << ", y1: " << y1
42      << ", xc: " << xc << ", yc: " << yc << ", r: " << r << endl;
43   ss << "Expected result: [";
44   if (res_size >= 1) {
45     ss << "(" << res0x << ", " << res0y << ")";
46   }
47   if (res_size >= 2) {
48     ss << ", (" << res1x << ", " << res1y << ")";
49   }
50   ss << "]\n";
51   FindIntersections(x0, y0, x1, y1, xc, yc, r, &res);
52   if (res.size() == 2 &&
53       (res[0].x > res[1].x || (res[0].x == res[1].x && res[0].y > res[1].y))) {
54     swap(res[0], res[1]);
55   }
56   ss << "Actual result: [";
57   for (int i = 0; i < res.size(); i++) {
58     if (i == 1) {
59       ss << ", ";
60     }
61     ss << "(" << res[i].x << ", " << res[i].y << ")";
62   }
63   ss << "]\n";
64   if (res.size() != res_size) {
65     cout << ss.str() << "FAIL\n";
66     exit(-1);
67   }
68   if (res_size >= 1 && (res[0].x != res0x || res[0].y != res0y)) {
69     cout << ss.str() << "FAIL\n";
70     exit(-1);
71   }
72   if (res_size == 2 && (res[1].x != res1x || res[1].y != res1y)) {
73     cout << ss.str() << "FAIL\n";
74     exit(-1);
75   }
76   //cout << ss.str();
77 }
78 
TestLSI(int x1,int y1,int x2,int y2,int x3,int y3,int x4,int y4,bool res,int res_ix=0,int res_iy=0)79 void TestLSI(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4,
80              bool res, int res_ix = 0, int res_iy = 0)
81 {
82   stringstream ss;
83   ss << "x1: " << x1 << ", y1: " << y1 << ", x2: " << x2 << ", y2: " << y2
84      << ", x3: " << x3 << ", y3: " << y3 << ", x4: " << x4 << ", y4: " << y4
85      << endl;
86   ss << "Expected result: " << (res ? "true" : "false");
87   if (res) {
88     ss << " (" << res_ix << ", " << res_iy << ")";
89   }
90   ss << endl;
91   LineSegment ls1(x1, y1, x2, y2);
92   LineSegment ls2(x3, y3, x4, y4);
93   int ix, iy;
94   bool isec = LineSegmentsIntersect(ls1, ls2, &ix, &iy);
95   ss << "Actual result: " << (isec ? "true" : "false");
96   if (isec) {
97     ss << " (" << ix << ", " << iy << ")";
98   }
99   ss << endl;
100   if (res != isec  ||
101       (res  &&  isec  &&  (ix != res_ix || iy != res_iy))) {
102     cout << ss.str() << "FAIL\n";
103     exit(-1);
104   }
105   //cout << ss.str();
106 }
107 
108 }  // anonymous namespace
109 
main()110 int main()
111 {
112   TestFI(0, 0, 2, 0, 1, 1, 1, 1, 1, 0);
113   TestFI(1, 1, 1000, 1000, 1000, 500, 30, 0);
114   TestFI(2, 3, 100, 4, 1000, 200, 25, 0);
115   TestFI(0, 0, 102, 102, 103, 103, 4, 1, 100, 100);
116   TestFI(3, -1, 9, 5, 4, 4, 4, 2, 4, 0, 8, 4);
117   TestFI(0, 0, 10000, 1, 0, 1, 1, 1, 0, 0);
118   TestFI(5, 1, 9, 5, 4, 4, 4, 1, 8, 4);
119   TestFI(0, 0, 8000, 6000, 1000, 7000, 5000, 1, 4000, 3000);
120   TestFI(-1000, 1000, 9000, 6000, 1000, 7000, 5000, 2, 1000, 2000, 5000, 4000);
121   TestFI(1300, 3200, 1600, 3500, 1100, 3700, 500, 2, 1400, 3300, 1500, 3400);
122   TestFI(4, 3, 6, 7, 1, 7, 5, 2, 4, 3, 6, 7);
123   TestFI(2, 0, 102, 100, 1, 7, 5, 0);
124   TestFI(1000, 2300, 1040, 2300, 1305, 2300, 300, 1, 1005, 2300);
125 
126   TestLSI(1, 3, 5, 1, 1, 1, 7, 4, true, 3, 2);
127   TestLSI(5000, 1000, 1000, 3000, 1000, 1000, 7000, 4000, true, 3000, 2000);
128   TestLSI(0, 0, 10, 0, 5, -5, 5, 5, true, 5, 0);
129   TestLSI(1, 1, 2, 2, 1, 1, 5, 0, true, 1, 1);
130   TestLSI(0, 1, 1, 10, 1, 0, 10, 1, false);
131   TestLSI(10, 10, 100, 10, 20, 10, 200, 10, true, 20, 10);
132   TestLSI(100, 100, 100, 200, 100, 50, 100, 150, true, 100, 100);
133   TestLSI(100, 10, 10, 10, 200, 10, 20, 10, true, 100, 10);
134   TestLSI(100, 100, 100, 200, 100, 150, 100, 50, true, 100, 100);
135   TestLSI(0, 0, 5000, 5000, 1, 0, 5001, 5000, false);
136   TestLSI(0, 0, 100, 0, 200, 0, 90, 0, true, 90, 0);
137   TestLSI(10, 1000, 20, 900, 30, 800, 60, 500, false);
138   TestLSI(1000, 1000, 1060, 1060, 1030, 1050, 1031, 900, true, 1030, 1030);
139   TestLSI(0, 0, 2, 4, 1, 2, 13, 1, true, 1, 2);
140   TestLSI(10000, 1000, 1000, 2000, 2000, 4000, 0, 0, true, 1000, 2000);
141   TestLSI(2800, 1700, 3000, 1700, 2400, 1700, 2600, 1700, false);
142   TestLSI(2800, 1700, 3000, 1700, 3000, 8000, 3000, 8200, false);
143 
144   cout << "OK\n";
145 }
146