Skip to content

Commit

Permalink
Merge pull request #586 from ferdnyc/framemapper-tests
Browse files Browse the repository at this point in the history
Various unit tests: Increase coverage
  • Loading branch information
ferdnyc committed Nov 5, 2020
2 parents 896b307 + 6b4ed3a commit fb87cc7
Show file tree
Hide file tree
Showing 4 changed files with 207 additions and 74 deletions.
13 changes: 6 additions & 7 deletions codecov.yml
Expand Up @@ -7,11 +7,10 @@ coverage:
base: pr # Only post a status to pull requests
informational: true # Don't block PRs based on coverage stats (yet?)
ignore:
- "/examples"
- "/bindings"
- "/thirdparty/jsoncpp"
- "/doc"
- "/cmake"
- "/*.md"
- "bindings"
- "examples/**/*"
- "bindings/**/*"
- "thirdparty/jsoncpp"
- "doc"
- "cmake"
- "*.md"
- "src/openshot_autogen"
76 changes: 64 additions & 12 deletions tests/Color_Tests.cpp
Expand Up @@ -33,23 +33,37 @@
#define DONT_SET_USING_JUCE_NAMESPACE 1
#include "OpenShot.h"

using namespace std;
using namespace openshot;
SUITE(Color) {

TEST(Color_Default_Constructor)
TEST(Default_Constructor)
{
// Create an empty color
Color c1;
openshot::Color c1;

CHECK_CLOSE(0.0f, c1.red.GetValue(0), 0.00001);
CHECK_CLOSE(0.0f, c1.green.GetValue(0), 0.00001);
CHECK_CLOSE(0.0f, c1.blue.GetValue(0), 0.00001);
}

TEST(Color_Animate_Colors)
TEST(Keyframe_constructor)
{
std::vector<openshot::Keyframe> kfs{0, 0, 0, 0};
int64_t i(0);
for (auto& kf : kfs) {
kf.AddPoint(100, ++i * 20);
}
auto c = openshot::Color(kfs[0], kfs[1], kfs[2], kfs[3]);

CHECK_CLOSE(20, c.red.GetLong(100), 0.01);
CHECK_CLOSE(40, c.green.GetLong(100), 0.01);
CHECK_CLOSE(60, c.blue.GetLong(100), 0.01);
CHECK_CLOSE(80, c.alpha.GetLong(100), 0.01);
}

TEST(Animate_Colors)
{
// Create an empty color
Color c1;
openshot::Color c1;

// Set starting color (on frame 0)
c1.red.AddPoint(1, 0);
Expand All @@ -67,7 +81,7 @@ TEST(Color_Animate_Colors)
CHECK_CLOSE(160, c1.blue.GetLong(500), 0.01);
}

TEST(Color_HEX_Value)
TEST(HEX_Value)
{
// Color
openshot::Color c;
Expand All @@ -84,7 +98,7 @@ TEST(Color_HEX_Value)

}

TEST(Color_HEX_Constructor)
TEST(HEX_Constructor)
{
// Color
openshot::Color c("#4586db");
Expand All @@ -97,19 +111,19 @@ TEST(Color_HEX_Constructor)
CHECK_EQUAL("#ffffff", c.GetColorHex(100));
}

TEST(Color_Distance)
TEST(Distance)
{
// Color
openshot::Color c1("#040a0c");
openshot::Color c2("#0c0c04");
openshot::Color c3("#000000");
openshot::Color c4("#ffffff");

CHECK_CLOSE(19.0f, Color::GetDistance(c1.red.GetInt(1), c1.blue.GetInt(1), c1.green.GetInt(1), c2.red.GetInt(1), c2.blue.GetInt(1), c2.green.GetInt(1)), 0.001);
CHECK_CLOSE(764.0f, Color::GetDistance(c3.red.GetInt(1), c3.blue.GetInt(1), c3.green.GetInt(1), c4.red.GetInt(1), c4.blue.GetInt(1), c4.green.GetInt(1)), 0.001);
CHECK_CLOSE(19.0f, openshot::Color::GetDistance(c1.red.GetInt(1), c1.blue.GetInt(1), c1.green.GetInt(1), c2.red.GetInt(1), c2.blue.GetInt(1), c2.green.GetInt(1)), 0.001);
CHECK_CLOSE(764.0f, openshot::Color::GetDistance(c3.red.GetInt(1), c3.blue.GetInt(1), c3.green.GetInt(1), c4.red.GetInt(1), c4.blue.GetInt(1), c4.green.GetInt(1)), 0.001);
}

TEST(Color_RGBA_Constructor)
TEST(RGBA_Constructor)
{
// Color
openshot::Color c(69, 134, 219, 255);
Expand All @@ -126,3 +140,41 @@ TEST(Color_RGBA_Constructor)
CHECK_EQUAL("#4586db", c1.GetColorHex(1));
CHECK_EQUAL(128, c1.alpha.GetInt(1));
}

TEST(Json)
{
openshot::Color c(128, 128, 128, 0);
openshot::Color c1;
c1.red.AddPoint(1, 128);
c1.green.AddPoint(1, 128);
c1.blue.AddPoint(1, 128);
c1.alpha.AddPoint(1, 0);
// Check that JSON produced is identical
auto j = c.Json();
auto j1 = c1.Json();
CHECK_EQUAL(j, j1);
// Check Json::Value representation
auto jv = c.JsonValue();
auto jv_string = jv.toStyledString();
CHECK_EQUAL(jv_string, j1);
}

TEST(SetJson) {
const std::string json_input = R"json(
{
"red": { "Points": [ { "co": { "X": 1.0, "Y": 0.0 }, "interpolation": 0 } ] },
"green": { "Points": [ { "co": { "X": 1.0, "Y": 128.0 }, "interpolation": 0 } ] },
"blue": { "Points": [ { "co": { "X": 1.0, "Y": 64.0 }, "interpolation": 0 } ] },
"alpha": { "Points": [ { "co": { "X": 1.0, "Y": 192.0 }, "interpolation": 0 } ] }
}
)json";
openshot::Color c;
CHECK_THROW(c.SetJson("}{"), openshot::InvalidJSON);
c.SetJson(json_input);
CHECK_CLOSE(0, c.red.GetLong(10), 0.01);
CHECK_CLOSE(128, c.green.GetLong(10), 0.01);
CHECK_CLOSE(64, c.blue.GetLong(10), 0.01);
CHECK_CLOSE(192, c.alpha.GetLong(10), 0.01);
}

} // SUITE
79 changes: 53 additions & 26 deletions tests/FrameMapper_Tests.cpp
Expand Up @@ -31,33 +31,45 @@
#include "UnitTest++.h"
// Prevent name clashes with juce::UnitTest
#define DONT_SET_USING_JUCE_NAMESPACE 1
#include "OpenShot.h"
#include "CacheMemory.h"
#include "Clip.h"
#include "DummyReader.h"
#include "FFmpegReader.h"
#include "Fraction.h"
#include "Frame.h"
#include "FrameMapper.h"
#include "Timeline.h"

using namespace std;
using namespace openshot;

TEST(FrameMapper_Get_Valid_Frame)
SUITE(FrameMapper) {

TEST(NoOp_GetMappedFrame)
{
// Create a reader
DummyReader r(Fraction(24,1), 720, 480, 22000, 2, 5.0);

// Create mapping between 24 fps and 29.97 fps using classic pulldown
FrameMapper mapping(&r, Fraction(30000, 1001), PULLDOWN_CLASSIC, 22000, 2, LAYOUT_STEREO);
// Create mapping between 24 fps and 24 fps without pulldown
FrameMapper mapping(&r, Fraction(24, 1), PULLDOWN_NONE, 22000, 2, LAYOUT_STEREO);
CHECK_EQUAL("FrameMapper", mapping.Name());

try
{
// Should find this frame
MappedFrame f = mapping.GetMappedFrame(125);
CHECK(true); // success
}
catch (OutOfBoundsFrame &e)
{
// Unexpected failure to find frame
CHECK(false);
}
// Should find this frame
MappedFrame f = mapping.GetMappedFrame(100);
CHECK_EQUAL(100, f.Odd.Frame);
CHECK_EQUAL(100, f.Even.Frame);

// Should return end frame
f = mapping.GetMappedFrame(150);
CHECK_EQUAL(120, f.Odd.Frame);
CHECK_EQUAL(120, f.Even.Frame);

mapping.Close();
mapping.Reader(nullptr);
CHECK_THROW(mapping.Reader(), ReaderClosed);
}

TEST(FrameMapper_Invalid_Frame_Too_Small)
TEST(Invalid_Frame_Too_Small)
{
// Create a reader
DummyReader r(Fraction(24,1), 720, 480, 22000, 2, 5.0);
Expand All @@ -70,7 +82,7 @@ TEST(FrameMapper_Invalid_Frame_Too_Small)

}

TEST(FrameMapper_24_fps_to_30_fps_Pulldown_Classic)
TEST(24_fps_to_30_fps_Pulldown_Classic)
{
// Create a reader
DummyReader r(Fraction(24,1), 720, 480, 22000, 2, 5.0);
Expand All @@ -87,7 +99,7 @@ TEST(FrameMapper_24_fps_to_30_fps_Pulldown_Classic)
CHECK_EQUAL(3, frame3.Even.Frame);
}

TEST(FrameMapper_24_fps_to_30_fps_Pulldown_Advanced)
TEST(24_fps_to_30_fps_Pulldown_Advanced)
{
// Create a reader
DummyReader r(Fraction(24,1), 720, 480, 22000, 2, 5.0);
Expand All @@ -107,7 +119,7 @@ TEST(FrameMapper_24_fps_to_30_fps_Pulldown_Advanced)
CHECK_EQUAL(3, frame4.Even.Frame);
}

TEST(FrameMapper_24_fps_to_30_fps_Pulldown_None)
TEST(24_fps_to_30_fps_Pulldown_None)
{
// Create a reader
DummyReader r(Fraction(24,1), 720, 480, 22000, 2, 5.0);
Expand All @@ -124,7 +136,7 @@ TEST(FrameMapper_24_fps_to_30_fps_Pulldown_None)
CHECK_EQUAL(4, frame5.Even.Frame);
}

TEST(FrameMapper_30_fps_to_24_fps_Pulldown_Classic)
TEST(30_fps_to_24_fps_Pulldown_Classic)
{
// Create a reader
DummyReader r(Fraction(30, 1), 720, 480, 22000, 2, 5.0);
Expand All @@ -144,7 +156,7 @@ TEST(FrameMapper_30_fps_to_24_fps_Pulldown_Classic)
CHECK_EQUAL(6, frame5.Even.Frame);
}

TEST(FrameMapper_30_fps_to_24_fps_Pulldown_Advanced)
TEST(30_fps_to_24_fps_Pulldown_Advanced)
{
// Create a reader
DummyReader r(Fraction(30, 1), 720, 480, 22000, 2, 5.0);
Expand All @@ -164,7 +176,7 @@ TEST(FrameMapper_30_fps_to_24_fps_Pulldown_Advanced)
CHECK_EQUAL(5, frame4.Even.Frame);
}

TEST(FrameMapper_30_fps_to_24_fps_Pulldown_None)
TEST(30_fps_to_24_fps_Pulldown_None)
{
// Create a reader
DummyReader r(Fraction(30, 1), 720, 480, 22000, 2, 5.0);
Expand All @@ -181,7 +193,7 @@ TEST(FrameMapper_30_fps_to_24_fps_Pulldown_None)
CHECK_EQUAL(6, frame5.Even.Frame);
}

TEST(FrameMapper_resample_audio_48000_to_41000)
TEST(resample_audio_48000_to_41000)
{
// Create a reader: 24 fps, 2 channels, 48000 sample rate
stringstream path;
Expand Down Expand Up @@ -211,7 +223,7 @@ TEST(FrameMapper_resample_audio_48000_to_41000)
map.Close();
}

TEST (FrameMapper_resample_audio_mapper) {
TEST(resample_audio_mapper) {
// This test verifies that audio data can be resampled on FrameMapper
// instances, even on frame rates that do not divide evenly, and that no audio data is misplaced
// or duplicated. We verify this by creating a SIN wave, add those data points to a DummyReader,
Expand Down Expand Up @@ -350,7 +362,7 @@ TEST (FrameMapper_resample_audio_mapper) {
r.Close();
}

TEST (FrameMapper_redistribute_samples_per_frame) {
TEST(redistribute_samples_per_frame) {
// This test verifies that audio data is correctly aligned on
// FrameMapper instances. We do this by creating 2 Clips based on the same parent reader
// (i.e. same exact audio sample data). We use a Timeline to overlap these clips
Expand Down Expand Up @@ -471,4 +483,19 @@ TEST (FrameMapper_redistribute_samples_per_frame) {
// Clean up
cache.Clear();
r.Close();
}
}

TEST(Json)
{
DummyReader r(Fraction(30,1), 1280, 720, 48000, 2, 5.0);
FrameMapper map(&r, Fraction(30, 1), PULLDOWN_NONE, 48000, 2, LAYOUT_STEREO);

// Read JSON config & write it back again
const std::string map_config = map.Json();
map.SetJson(map_config);

CHECK_EQUAL(48000, map.info.sample_rate);
CHECK_EQUAL(30, map.info.fps.num);
}

} // SUITE

0 comments on commit fb87cc7

Please sign in to comment.