summaryrefslogtreecommitdiff
path: root/libs/evoral/test/BeatsTest.cc
blob: 07eab1f1ec61230faeceff0492f8503451ba4d28 (plain)
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#include <stdlib.h>

#include "BeatsTest.hpp"
#include "temporal/beats.h"

CPPUNIT_TEST_SUITE_REGISTRATION(BeatsTest);

using namespace Evoral;
using namespace Temporal;

static const double delta = 1.5 / (double)Beats::PPQN;

void
BeatsTest::createTest()
{
	const Beats a(1, 2);
	CPPUNIT_ASSERT_EQUAL(1, a.get_beats());
	CPPUNIT_ASSERT_EQUAL(2, a.get_ticks());
	CPPUNIT_ASSERT_DOUBLES_EQUAL(1 + 2 / (double)Beats::PPQN, a.to_double(), delta);

	const Beats b(1.5);
	CPPUNIT_ASSERT_EQUAL(1, b.get_beats());
	CPPUNIT_ASSERT_EQUAL(Beats::PPQN / 2, b.get_ticks());
	CPPUNIT_ASSERT_DOUBLES_EQUAL(1.5, b.to_double(), delta);

	const Beats c = Beats::beats(6);
	CPPUNIT_ASSERT_EQUAL(6, c.get_beats());
	CPPUNIT_ASSERT_EQUAL(0, c.get_ticks());

	const Beats d = Beats::ticks(7);
	CPPUNIT_ASSERT_EQUAL(0, d.get_beats());
	CPPUNIT_ASSERT_EQUAL(7, d.get_ticks());

	Beats e(8, 9);
	e = d;
	CPPUNIT_ASSERT_EQUAL(d, e);


	// const Beats diff = n2 - n1;
	// CPPUNIT_ASSERT_EQUAL(-44, diff.get_beats());
	// CPPUNIT_ASSERT_EQUAL(44 / Beats::PPQN, diff.get_ticks());
	// CPPUNIT_ASSERT_DOUBLES_EQUAL(diff.to_double(), -44.44, delta);
}

void
BeatsTest::addTest()
{
	const Beats a(1, 2);
	const Beats b(3, 4);

	// Positive + positive
	const Beats c = a + b;
	CPPUNIT_ASSERT_EQUAL(4, c.get_beats());
	CPPUNIT_ASSERT_EQUAL(6, c.get_ticks());

	const Beats n1(-12.34);
	CPPUNIT_ASSERT_DOUBLES_EQUAL(-12.34, n1.to_double(), delta);

	const Beats n2(-56.78);
	CPPUNIT_ASSERT_DOUBLES_EQUAL(-56.78, n2.to_double(), delta);

	// Positive + negative
	const Beats p1(1.0);
	const Beats p_n = p1 + n1;
	CPPUNIT_ASSERT_EQUAL(-11, p_n.get_beats());
	CPPUNIT_ASSERT_EQUAL((int32_t)(Beats::PPQN * -0.34), p_n.get_ticks());
	CPPUNIT_ASSERT_DOUBLES_EQUAL(-11.34, p_n.to_double(), delta);

	// Negative + positive
	const Beats n_p = n1 + p1;
	CPPUNIT_ASSERT_EQUAL(-11, n_p.get_beats());
	CPPUNIT_ASSERT_EQUAL((int32_t)(Beats::PPQN * -0.34), n_p.get_ticks());
	CPPUNIT_ASSERT_DOUBLES_EQUAL(-11.34, n_p.to_double(), delta);

	// Negative + negative
	const Beats sum = n1 + n2;
	CPPUNIT_ASSERT_EQUAL(-69, sum.get_beats());
	//CPPUNIT_ASSERT_EQUAL((int32_t)(Beats::PPQN * -0.12), n_p.get_ticks());
	CPPUNIT_ASSERT_DOUBLES_EQUAL(-69.12, sum.to_double(), delta);
}

void
BeatsTest::subtractTest()
{
	const Beats a(1, 2);
	const Beats b(3, 4);

	// Positive - positive
	const Beats c = b - a;
	CPPUNIT_ASSERT_EQUAL(2, c.get_beats());
	CPPUNIT_ASSERT_EQUAL(2, c.get_ticks());

	const Beats n1(-12.34);
	CPPUNIT_ASSERT_DOUBLES_EQUAL(-12.34, n1.to_double(), delta);

	const Beats n2(-56.78);
	CPPUNIT_ASSERT_DOUBLES_EQUAL(-56.78, n2.to_double(), delta);

	// Positive - negative
	const Beats p1(1.0);
	const Beats p_n = p1 - n1;
	CPPUNIT_ASSERT_EQUAL(13, p_n.get_beats());
	CPPUNIT_ASSERT_EQUAL((int32_t)(Beats::PPQN * 0.34), p_n.get_ticks());
	CPPUNIT_ASSERT_DOUBLES_EQUAL(13.34, p_n.to_double(), delta);

	// Negative - positive
	const Beats n_p = n1 - p1;
	CPPUNIT_ASSERT_EQUAL(-13, n_p.get_beats());
	CPPUNIT_ASSERT_EQUAL((int32_t)(Beats::PPQN * -0.34), n_p.get_ticks());
	CPPUNIT_ASSERT_DOUBLES_EQUAL(-13.34, n_p.to_double(), delta);

	// Negative - negative
	const Beats diff = n1 - n2;
	CPPUNIT_ASSERT_EQUAL(44, diff.get_beats());
	CPPUNIT_ASSERT_EQUAL((int32_t)lrint(Beats::PPQN * 0.44), diff.get_ticks());
	CPPUNIT_ASSERT_DOUBLES_EQUAL(44.44, diff.to_double(), delta);
}

void
BeatsTest::multiplyTest()
{
	CPPUNIT_ASSERT_DOUBLES_EQUAL(3.0, (Beats(1.5) * 2.0).to_double(), delta);
	CPPUNIT_ASSERT_DOUBLES_EQUAL(-10.0, (Beats(5.0) * -2.0).to_double(), delta);
	CPPUNIT_ASSERT_DOUBLES_EQUAL(-10.0, (Beats(-5.0) * 2.0).to_double(), delta);
}

void
BeatsTest::roundTest()
{
	Beats a(1, 1);

	// Round a up
	const Beats au = a.round_up_to_beat();
	CPPUNIT_ASSERT_EQUAL(au.get_beats(), 2);
	CPPUNIT_ASSERT_EQUAL(au.get_ticks(), 0);

	// Round a down
	const Beats ad = a.round_down_to_beat();
	CPPUNIT_ASSERT_EQUAL(ad.get_beats(), 1);
	CPPUNIT_ASSERT_EQUAL(ad.get_ticks(), 0);

	// Round result down again
	const Beats add = ad.round_down_to_beat();
	CPPUNIT_ASSERT_EQUAL(ad, add);

	// Round result up
	const Beats adu = ad.round_up_to_beat();
	CPPUNIT_ASSERT_EQUAL(ad, adu);

	// Snap to 1.5
	const Beats snapped = a.snap_to(Beats(1.5));
	CPPUNIT_ASSERT_EQUAL(snapped.get_beats(), 1);
	CPPUNIT_ASSERT_EQUAL(snapped.get_ticks(), Beats::PPQN / 2);
}

void
BeatsTest::convertTest()
{
	const Beats a = Beats::ticks_at_rate(72000, 48000);
	CPPUNIT_ASSERT_DOUBLES_EQUAL(1, a.get_beats(), delta);
	CPPUNIT_ASSERT_DOUBLES_EQUAL(Beats::PPQN / 2, a.get_ticks(), delta);
	CPPUNIT_ASSERT_DOUBLES_EQUAL(1.5, a.to_double(), delta);

	const Beats b = Beats::ticks_at_rate(8, 48000);
	CPPUNIT_ASSERT_DOUBLES_EQUAL(0, b.get_beats(), delta);
	CPPUNIT_ASSERT_DOUBLES_EQUAL(Beats::PPQN * 8 / 48000, b.get_ticks(), delta);
	CPPUNIT_ASSERT_DOUBLES_EQUAL((8 / 48000.0), b.to_double(), delta);

	CPPUNIT_ASSERT_EQUAL(int64_t(1.5 * Beats::PPQN), a.to_ticks());
	CPPUNIT_ASSERT_EQUAL(int64_t(1.5 * 192), a.to_ticks(192));
}