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 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
|
/* SPDX-License-Identifier: GPL-2.0-or-later */
/*!********************************************************************
Audacity: A Digital Audio Editor
NumericConverterTests.cpp
Dmitry Vedenko
**********************************************************************/
#include <catch2/catch.hpp>
#include "formatters/ParsedNumericConverterFormatter.h"
#include "formatters/BeatsNumericConverterFormatter.h"
#include "NumericConverterFormatterContext.h"
#include "Project.h"
#include "ProjectRate.h"
#include "ProjectTimeSignature.h"
#include "MockedPrefs.h"
#include "MockedAudio.h"
TEST_CASE("ParsedNumericConverterFormatter", "")
{
auto context = FormatterContext::SampleRateContext(44100.0);
auto hhmmssFormatter = CreateParsedNumericConverterFormatter(
context, NumericConverterType_TIME(), Verbatim("0100 h 060 m 060 s"));
REQUIRE(
hhmmssFormatter->ValueToString(0.0, false).valueString ==
"00 h 00 m 00 s");
REQUIRE(
hhmmssFormatter->ValueToString(30.0, false).valueString ==
"00 h 00 m 30 s");
REQUIRE(
hhmmssFormatter->ValueToString(60.0, false).valueString ==
"00 h 01 m 00 s");
REQUIRE(
hhmmssFormatter->ValueToString(60.0, false)
.fieldValueStrings.size() == 3);
REQUIRE(
hhmmssFormatter->ValueToString(60.0, false).fieldValueStrings[0] ==
"00");
REQUIRE(
hhmmssFormatter->ValueToString(60.0, false).fieldValueStrings[1] ==
"01");
REQUIRE(
hhmmssFormatter->ValueToString(60.0, false).fieldValueStrings[2] ==
"00");
REQUIRE(
hhmmssFormatter->StringToValue("foobar").has_value() == false);
REQUIRE(
hhmmssFormatter->StringToValue("01 h 30 m 15 s").has_value() == true);
REQUIRE(
*hhmmssFormatter->StringToValue("01 h 30 m 15 s") ==
Approx(60 * 60 + 30 * 60 + 15));
}
TEST_CASE("BeatsNumericConverterFormatter", "")
{
MockedPrefs mockedPrefs;
MockedAudio mockedAudio;
auto project = AudacityProject::Create();
auto& timeSignature = ProjectTimeSignature::Get(*project);
timeSignature.SetTempo(120.0);
timeSignature.SetUpperTimeSignature(3);
timeSignature.SetLowerTimeSignature(4);
auto basicFormatter = CreateBeatsNumericConverterFormatter(FormatterContext::ProjectContext(*project));
REQUIRE(
basicFormatter->ValueToString(-1.0, false).valueString ==
"--- bar -- beat");
REQUIRE(
basicFormatter->ValueToString(0.0, false).valueString ==
"001 bar 01 beat");
REQUIRE(
basicFormatter->ValueToString(0.6, false).valueString ==
"001 bar 02 beat");
REQUIRE(
basicFormatter->ValueToString(1.0, false).valueString ==
"001 bar 03 beat");
REQUIRE(
basicFormatter->ValueToString(1.6, false).valueString ==
"002 bar 01 beat");
REQUIRE(basicFormatter->StringToValue("foobar").has_value() == false);
REQUIRE(basicFormatter->StringToValue("001 bar 01 beat").has_value() == true);
REQUIRE(
*basicFormatter->StringToValue("001 bar 01 beat") == Approx(0));
REQUIRE(
*basicFormatter->StringToValue("001 bar 02 beat") == Approx(0.5));
REQUIRE(
*basicFormatter->StringToValue("002 bar 01 beat") == Approx(1.5));
timeSignature.SetTempo(120.0);
timeSignature.SetUpperTimeSignature(3);
timeSignature.SetLowerTimeSignature(4);
auto fracFormatter = CreateBeatsNumericConverterFormatter(
FormatterContext::ProjectContext(*project), 16);
REQUIRE(
fracFormatter->ValueToString(-1.0, false).valueString ==
"--- bar -- beat --");
REQUIRE(
fracFormatter->ValueToString(0.0, false).valueString ==
"001 bar 01 beat 01");
REQUIRE(
fracFormatter->ValueToString(0.6, false).valueString ==
"001 bar 02 beat 01");
REQUIRE(
fracFormatter->ValueToString(1.0, false).valueString ==
"001 bar 03 beat 01");
REQUIRE(
fracFormatter->ValueToString(1.9, false).valueString ==
"002 bar 01 beat 04");
REQUIRE(
*fracFormatter->StringToValue("001 bar 01 beat 01") == Approx(0));
REQUIRE(
*fracFormatter->StringToValue("001 bar 02 beat 01") == Approx(0.5));
REQUIRE(
*fracFormatter->StringToValue("002 bar 01 beat 01") == Approx(1.5));
REQUIRE(
*fracFormatter->StringToValue("001 bar 01 beat 02") == Approx(0.125));
REQUIRE(
*fracFormatter->StringToValue("001 bar 02 beat 04") == Approx(0.875));
REQUIRE(
*fracFormatter->StringToValue("002 bar 01 beat 09") ==
Approx(1.5 + 0.0 + 8 * (0.5 / 4)));
REQUIRE(fracFormatter->SingleStep(0.0, 2, true) == Approx(1.5));
REQUIRE(fracFormatter->SingleStep(0.0, 1, true) == Approx(15.0));
REQUIRE(fracFormatter->SingleStep(0.0, 4, true) == Approx(0.5));
REQUIRE(fracFormatter->SingleStep(0.0, 3, true) == Approx(5));
auto durationFormatter = CreateBeatsNumericConverterFormatter(
FormatterContext::ProjectContext(*project), 16, false);
REQUIRE(
durationFormatter->ValueToString(-1.0, false).valueString ==
"--- bar -- beat --");
REQUIRE(
durationFormatter->ValueToString(0.0, false).valueString ==
"000 bar 00 beat 00");
REQUIRE(
durationFormatter->ValueToString(0.6, false).valueString ==
"000 bar 01 beat 00");
REQUIRE(
durationFormatter->ValueToString(1.0, false).valueString ==
"000 bar 02 beat 00");
REQUIRE(
durationFormatter->ValueToString(1.9, false).valueString ==
"001 bar 00 beat 03");
REQUIRE(*durationFormatter->StringToValue("000 bar 00 beat 00") == Approx(0));
REQUIRE(*durationFormatter->StringToValue("000 bar 01 beat 00") == Approx(0.5));
REQUIRE(*durationFormatter->StringToValue("001 bar 00 beat 00") == Approx(1.5));
REQUIRE(
*durationFormatter->StringToValue("000 bar 00 beat 01") == Approx(0.125));
REQUIRE(
*durationFormatter->StringToValue("000 bar 01 beat 03") == Approx(0.875));
REQUIRE(
*durationFormatter->StringToValue("001 bar 00 beat 08") ==
Approx(1.5 + 0.0 + 8 * (0.5 / 4)));
REQUIRE(durationFormatter->SingleStep(0.0, 2, true) == Approx(1.5));
REQUIRE(durationFormatter->SingleStep(0.0, 1, true) == Approx(15.0));
REQUIRE(durationFormatter->SingleStep(0.0, 4, true) == Approx(0.5));
REQUIRE(durationFormatter->SingleStep(0.0, 3, true) == Approx(5));
auto longFormatterTest = [&](double tempo)
{
timeSignature.SetTempo(tempo);
timeSignature.SetUpperTimeSignature(4);
timeSignature.SetLowerTimeSignature(4);
//const auto t = *basicFormatter->StringToValue("009 bar 02 beat");
//const auto r = basicFormatter->ValueToString(t, true).valueString;
//REQUIRE("009 bar 02 beat" == r);
for (int32_t bar = 0; bar < 100000; ++bar)
{
for (int32_t beat = 0; beat < 4; ++beat)
{
const auto value = bar * timeSignature.GetBarDuration() +
beat * timeSignature.GetBeatDuration();
basicFormatter->UpdateFormatForValue(value, true);
const auto formattedString =
wxString::Format("%03d bar %02d beat", bar + 1, beat + 1);
REQUIRE(
*basicFormatter->StringToValue(formattedString) ==
Approx(value));
REQUIRE(
basicFormatter
->ValueToString(
*basicFormatter->StringToValue(formattedString), true)
.valueString == formattedString);
}
}
};
longFormatterTest(88);
longFormatterTest(117);
}
|