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"
27 FUNC (interval_set_union)
31 // Overlapping intervals.
32 ivs.push_back (Interval (-1, 1));
33 ivs.push_back (Interval (0, 3));
34 ivs.push_back (Interval (1, 2));
35 Interval_set result = Interval_set::interval_union (ivs);
36 EQUAL (result.intervals ().size (), 1);
37 // Compare intervals using to_string, since yaffut doesn't know how to compare intervals.
38 EQUAL (result.intervals ()[0].to_string (), Interval (-1, 3).to_string ());
40 // Non-overlapping intervals.
41 ivs.push_back (Interval (-5, -4));
42 result = Interval_set::interval_union (ivs);
43 EQUAL (result.intervals ().size (), 2);
44 EQUAL (result.intervals ()[0].to_string (), Interval (-5, -4).to_string ());
45 EQUAL (result.intervals ()[1].to_string (), Interval (-1, 3).to_string ());
47 // Infinite intervals.
48 ivs.push_back (Interval (-infinity_f, -4));
49 result = Interval_set::interval_union (ivs);
50 EQUAL (result.intervals ().size (), 2);
51 EQUAL (result.intervals ()[0].to_string (), Interval (-infinity_f, -4).to_string ());
52 EQUAL (result.intervals ()[1].to_string (), Interval (-1, 3).to_string ());
55 ivs.push_back (Interval (infinity_f, -infinity_f));
56 result = Interval_set::interval_union (ivs);
57 EQUAL (result.intervals ().size (), 2);
60 FUNC (interval_set_nearest_point)
64 ivs.push_back (Interval (-3, -1));
65 ivs.push_back (Interval (1, 3));
66 Interval_set set = Interval_set::interval_union (ivs);
68 // If the point is in the set, direction does not matter.
69 EQUAL (set.nearest_point (-2, UP), -2);
70 EQUAL (set.nearest_point (-2, DOWN), -2);
71 EQUAL (set.nearest_point (-2, CENTER), -2);
73 // If the point is not in the set, direction does matter.
74 EQUAL (set.nearest_point (-0.5, UP), 1);
75 EQUAL (set.nearest_point (-0.5, DOWN), -1);
76 EQUAL (set.nearest_point (-0.5, CENTER), -1);
77 EQUAL (set.nearest_point (0.5, CENTER), 1);
79 // The return value can be +- infinity.
80 EQUAL (set.nearest_point (5, UP), infinity_f);
81 EQUAL (set.nearest_point (5, DOWN), 3);
82 EQUAL (set.nearest_point (-5, DOWN), -infinity_f);
83 EQUAL (set.nearest_point (-5, UP), -3);
86 FUNC (interval_set_complement)
90 ivs.push_back (Interval (-3, -1));
91 ivs.push_back (Interval (1, 3));
92 Interval_set set = Interval_set::interval_union (ivs).complement ();
93 EQUAL (set.intervals ().size (), 3);
94 EQUAL (set.intervals ()[0].to_string (), Interval (-infinity_f, -3).to_string ());
95 EQUAL (set.intervals ()[1].to_string (), Interval (-1, 1).to_string ());
96 EQUAL (set.intervals ()[2].to_string (), Interval (3, infinity_f).to_string ());
98 // Half-infinite sets are handled correctly.
99 ivs.push_back (Interval (-infinity_f, -2));
100 set = Interval_set::interval_union (ivs).complement ();
101 EQUAL (set.intervals ().size (), 2);
102 EQUAL (set.intervals ()[0].to_string (), Interval (-1, 1).to_string ());
103 EQUAL (set.intervals ()[1].to_string (), Interval (3, infinity_f).to_string ());
105 // Full and empty sets are handled correctly.
106 set = Interval_set ().complement ();
107 EQUAL (set.intervals ().size (), 1);
108 EQUAL (set.intervals ()[0].to_string (), Interval (-infinity_f, infinity_f).to_string ());
109 set = set.complement ();
110 EQUAL (set.intervals ().size (), 0);