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
// Copyright 2023 The rust-ggstd authors. All rights reserved.
// Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
use super::{rect, Point, Rectangle, ZR};
#[test]
fn test_rectangle() {
// in checks that every point in f is in g.
let in_fn = |f: &Rectangle, g: &Rectangle| -> Result<(), String> {
if !f.inside(g) {
return Err(format!(
"f={:?}, f.inside({:?}): got false, want true",
f, g
));
}
for y in f.min.y..f.max.y {
for x in f.min.x..f.max.x {
let p = Point::new(x, y);
if !p.inside(g) {
return Err(format!(
"p={:?}, p.inside({:?}): got false, want true",
p, g
));
}
}
}
Ok(())
};
let rects = &[
rect(0, 0, 10, 10),
rect(10, 0, 20, 10),
rect(1, 2, 3, 4),
rect(4, 6, 10, 10),
rect(2, 3, 12, 5),
rect(-1, -2, 0, 0),
rect(-1, -2, 4, 6),
rect(-10, -20, 30, 40),
rect(8, 8, 8, 8),
rect(88, 88, 88, 88),
rect(6, 5, 4, 3),
];
// r.Eq(s) should be equivalent to every point in r being in s, and every
// point in s being in r.
for r in rects {
for s in rects {
let got = r.eq(s);
let want = in_fn(r, s).is_ok() && in_fn(s, r).is_ok();
assert_eq!(
got, want,
"Eq: r={:?}, s={:?}: got {}, want {}",
r, s, got, want
);
}
}
// The intersection should be the largest rectangle a such that every point
// in a is both in r and in s.
for r in rects {
for s in rects {
let a = r.intersect(s);
let res = in_fn(&a, r);
assert!(
res.is_ok(),
"intersect: r={:?}, s={:?}, a={:?}, a not in r: {:?}",
r,
s,
a,
res
);
let res = in_fn(&a, s);
assert!(
res.is_ok(),
"intersect: r={:?}, s={:?}, a={:?}, a not in s: {:?}",
r,
s,
a,
res
);
let is_zero = a == ZR;
let overlaps = r.overlaps(s);
assert!(
is_zero != overlaps,
"intersect: r={:?}, s={:?}, a={:?}: isZero={} same as overlaps={}",
r,
s,
a,
is_zero,
overlaps
);
let mut larger_than_a = [a, a, a, a];
larger_than_a[0].min.x -= 1;
larger_than_a[1].min.y -= 1;
larger_than_a[2].max.x += 1;
larger_than_a[3].max.y += 1;
for (i, b) in larger_than_a.iter().enumerate() {
if b.empty() {
// b isn't actually larger than a.
continue;
}
if in_fn(b, r).is_ok() && in_fn(b, s).is_ok() {
panic!(
"intersect: r={:?}, s={:?}, a={:?}, b={:?}, i={}: intersection could be larger",
r, s, a, b, i
);
}
}
}
}
// The union should be the smallest rectangle a such that every point in r
// is in a and every point in s is in a.
for r in rects {
for s in rects {
let a = r.union(s);
let res = in_fn(r, &a);
assert!(
res.is_ok(),
"Union: r={:?}, s={:?}, a={:?}, r not in a: {:?}",
r,
s,
a,
res
);
// }
let res = in_fn(s, &a);
assert!(
res.is_ok(),
"Union: r={:?}, s={:?}, a={:?}, s not in a: {:?}",
r,
s,
a,
res
);
if a.empty() {
// You can't get any smaller than a.
continue;
}
let mut smaller_than_a = [a, a, a, a];
smaller_than_a[0].min.x += 1;
smaller_than_a[1].min.y += 1;
smaller_than_a[2].max.x -= 1;
smaller_than_a[3].max.y -= 1;
for (i, b) in smaller_than_a.iter().enumerate() {
if in_fn(r, b).is_ok() && in_fn(s, b).is_ok() {
panic!(
"Union: r={:?}, s={:?}, a={:?}, b={:?}, i={}: union could be smaller",
r, s, a, b, i
);
}
}
}
}
}