]> git.donarmstrong.com Git - lilypond.git/blob - flower/test-interval-set.cc
Web-ja: update introduction
[lilypond.git] / flower / test-interval-set.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2012 Joe Neeman <joeneeman@gmail.com>
5
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.
10
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.
15
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/>.
18 */
19
20 #include "interval-set.hh"
21
22 #include "yaffut.hh"
23
24 using namespace std;
25
26 FUNC (interval_set_union)
27 {
28   vector<Interval> ivs;
29
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 ());
38
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 ());
45
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 ());
52
53   // Empty intervals.
54   ivs.push_back (Interval (infinity_f, -infinity_f));
55   result = Interval_set::interval_union (ivs);
56   EQUAL (result.intervals ().size (), 2);
57 }
58
59 FUNC (interval_set_nearest_point)
60 {
61   vector<Interval> ivs;
62
63   ivs.push_back (Interval (-3, -1));
64   ivs.push_back (Interval (1, 3));
65   Interval_set set = Interval_set::interval_union (ivs);
66
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);
71
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);
77
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);
83 }
84
85 FUNC (interval_set_complement)
86 {
87   vector<Interval> ivs;
88
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 ());
96
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 ());
103
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);
110 }