xref: /aosp_15_r20/external/skia/modules/bentleyottmann/tests/BentleyOttmann1Test.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 // Copyright 2023 Google LLC
2 // Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
3 
4 #include "modules/bentleyottmann/include/BentleyOttmann1.h"
5 
6 #include "modules/bentleyottmann/include/Point.h"
7 #include "modules/bentleyottmann/include/Segment.h"
8 #include "tests/Test.h"
9 
10 #include <vector>
11 
12 using namespace bentleyottmann;
13 
DEF_TEST(BO_bentley_ottmann_1_Basic,reporter)14 DEF_TEST(BO_bentley_ottmann_1_Basic, reporter) {
15 
16     {
17         Segment s0 = {{-1, 0}, {1,  0}},
18                 s1 = {{ 0, 1}, {0, -1}};
19 
20         std::vector<Segment> segments;
21         segments.push_back(s0);
22         segments.push_back(s1);
23 
24         auto possibleCrossings = bentley_ottmann_1(segments);
25 
26         REPORTER_ASSERT(reporter, possibleCrossings.has_value());
27 
28         if (possibleCrossings) {
29             auto crossings = possibleCrossings.value();
30             REPORTER_ASSERT(reporter, crossings.size() == 1);
31             Point p = {0, 0};
32             REPORTER_ASSERT(reporter, crossings[0].crossing == p);
33             REPORTER_ASSERT(reporter, (crossings[0].s0 == s0 && crossings[0].s1 == s1) ||
34                                       (crossings[0].s0 == s1 && crossings[0].s1 == s0));
35         }
36     }
37     {
38         Point p0 = {-50, -100},
39               p1 = { 50, -100},
40               p2 = {  0, 100};
41         Segment s0 = {p0, p1},
42                 s1 = {p1, p2},
43                 s2 = {p2, p0};
44 
45         std::vector<Segment> segments{s0, s1, s2};
46         auto possibleCrossings = bentley_ottmann_1(segments);
47 
48         REPORTER_ASSERT(reporter, possibleCrossings.has_value());
49         if (possibleCrossings) {
50             auto crossings = possibleCrossings.value();
51             REPORTER_ASSERT(reporter, crossings.size() == 0);
52         }
53     }
54     {
55         Point p0 = {-50, 100},
56               p1 = { 50, 100},
57               p2 = {  0, -100};
58         Segment s0 = {p0, p1},
59                 s1 = {p1, p2},
60                 s2 = {p2, p0};
61 
62         std::vector<Segment> segments{s0, s1, s2};
63         auto possibleCrossings = bentley_ottmann_1(segments);
64 
65         REPORTER_ASSERT(reporter, possibleCrossings.has_value());
66         if (possibleCrossings) {
67             auto crossings = possibleCrossings.value();
68             REPORTER_ASSERT(reporter, crossings.size() == 0);
69         }
70     }
71 }
72