2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 2012 Joe Neeman <joeneeman@gmail.com>
6 LilyPond is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 LilyPond is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with LilyPond. If not, see <http://www.gnu.org/licenses/>.
20 #include "interval-set.hh"
26 FUNC (interval_set_union)
30 // Overlapping intervals.
31 ivs.push_back (Interval (-1, 1));
32 ivs.push_back (Interval (0, 3));
33 ivs.push_back (Interval (1, 2));
34 Interval_set result = Interval_set::interval_union (ivs);
35 EQUAL (result.intervals ().size (), 1);
36 // Compare intervals using to_string, since yaffut doesn't know how to compare intervals.
37 EQUAL (result.intervals ()[0].to_string (), Interval (-1, 3).to_string ());
39 // Non-overlapping intervals.
40 ivs.push_back (Interval (-5, -4));
41 result = Interval_set::interval_union (ivs);
42 EQUAL (result.intervals ().size (), 2);
43 EQUAL (result.intervals ()[0].to_string (), Interval (-5, -4).to_string ());
44 EQUAL (result.intervals ()[1].to_string (), Interval (-1, 3).to_string ());
46 // Infinite intervals.
47 ivs.push_back (Interval (-infinity_f, -4));
48 result = Interval_set::interval_union (ivs);
49 EQUAL (result.intervals ().size (), 2);
50 EQUAL (result.intervals ()[0].to_string (), Interval (-infinity_f, -4).to_string ());
51 EQUAL (result.intervals ()[1].to_string (), Interval (-1, 3).to_string ());
54 ivs.push_back (Interval (infinity_f, -infinity_f));
55 result = Interval_set::interval_union (ivs);
56 EQUAL (result.intervals ().size (), 2);
59 FUNC (interval_set_nearest_point)
63 ivs.push_back (Interval (-3, -1));
64 ivs.push_back (Interval (1, 3));
65 Interval_set set = Interval_set::interval_union (ivs);
67 // If the point is in the set, direction does not matter.
68 EQUAL (set.nearest_point (-2, UP), -2);
69 EQUAL (set.nearest_point (-2, DOWN), -2);
70 EQUAL (set.nearest_point (-2, CENTER), -2);
72 // If the point is not in the set, direction does matter.
73 EQUAL (set.nearest_point (-0.5, UP), 1);
74 EQUAL (set.nearest_point (-0.5, DOWN), -1);
75 EQUAL (set.nearest_point (-0.5, CENTER), -1);
76 EQUAL (set.nearest_point (0.5, CENTER), 1);
78 // The return value can be +- infinity.
79 EQUAL (set.nearest_point (5, UP), infinity_f);
80 EQUAL (set.nearest_point (5, DOWN), 3);
81 EQUAL (set.nearest_point (-5, DOWN), -infinity_f);
82 EQUAL (set.nearest_point (-5, UP), -3);
85 FUNC (interval_set_complement)
89 ivs.push_back (Interval (-3, -1));
90 ivs.push_back (Interval (1, 3));
91 Interval_set set = Interval_set::interval_union (ivs).complement ();
92 EQUAL (set.intervals ().size (), 3);
93 EQUAL (set.intervals ()[0].to_string (), Interval (-infinity_f, -3).to_string ());
94 EQUAL (set.intervals ()[1].to_string (), Interval (-1, 1).to_string ());
95 EQUAL (set.intervals ()[2].to_string (), Interval (3, infinity_f).to_string ());
97 // Half-infinite sets are handled correctly.
98 ivs.push_back (Interval (-infinity_f, -2));
99 set = Interval_set::interval_union (ivs).complement ();
100 EQUAL (set.intervals ().size (), 2);
101 EQUAL (set.intervals ()[0].to_string (), Interval (-1, 1).to_string ());
102 EQUAL (set.intervals ()[1].to_string (), Interval (3, infinity_f).to_string ());
104 // Full and empty sets are handled correctly.
105 set = Interval_set ().complement ();
106 EQUAL (set.intervals ().size (), 1);
107 EQUAL (set.intervals ()[0].to_string (), Interval (-infinity_f, infinity_f).to_string ());
108 set = set.complement ();
109 EQUAL (set.intervals ().size (), 0);