tests: introduce catch2 unit tests infrastructure.

Catch2 is a multi-paradigm test framework for C++.
It is primarily distributed as a single header file,
very easy and simple to adopt this to thorvg project.

This patch introduces catch2 infrastsructure and one prototype as a sample.

You can refer "testInitializer.cpp", how to add unit test!
while ignoring else files such as "catch2.hpp", "testMain.cpp"

Also, enable Unit-tests with meson option when you change any thorvg code.

$meson build -Dtests=true.

launch tvgUnitTest in the build result then verify 100% coverage
before submitting any patches.
This commit is contained in:
Hermet Park 2021-06-04 16:46:34 +09:00 committed by GitHub
parent efa0035fe6
commit 6b7aa8ad9e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 18028 additions and 2 deletions

View file

@ -47,16 +47,24 @@ headers = [include_directories('inc'), include_directories('.')]
subdir('inc') subdir('inc')
subdir('src') subdir('src')
if get_option('tests') == true
subdir('test')
endif
summary = ''' summary = '''
Summary: Summary:
thorvg version : @0@ ThorVG version : @0@
Build type : @1@ Build Type : @1@
Prefix : @2@ Prefix : @2@
Tests : @3@
Examples : @4@
'''.format( '''.format(
meson.project_version(), meson.project_version(),
get_option('buildtype'), get_option('buildtype'),
get_option('prefix'), get_option('prefix'),
get_option('tests'),
get_option('examples'),
) )
message(summary) message(summary)

View file

@ -33,6 +33,11 @@ option('examples',
value: false, value: false,
description: 'Enable building examples') description: 'Enable building examples')
option('tests',
type: 'boolean',
value: false,
description: 'Enable building Unit Tests')
option('log', option('log',
type: 'boolean', type: 'boolean',
value: false, value: false,

17937
test/catch.hpp Normal file

File diff suppressed because it is too large Load diff

11
test/meson.build Normal file
View file

@ -0,0 +1,11 @@
test_file = [
'testMain.cpp',
'testInitializer.cpp',
]
tests = executable('tvgUnitTests',
test_file,
include_directories : headers,
link_with : thorvg_lib)
test('Unit Tests', tests)

60
test/testInitializer.cpp Normal file
View file

@ -0,0 +1,60 @@
/*
* Copyright (c) 2020-2021 Samsung Electronics Co., Ltd. All rights reserved.
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <thorvg.h>
#include "catch.hpp"
using namespace tvg;
TEST_CASE("Basic initialization", "[tvgInitializer]")
{
REQUIRE(Initializer::init(CanvasEngine::Sw, 0) == Result::Success);
REQUIRE(Initializer::term(CanvasEngine::Sw) == Result::Success);
}
TEST_CASE("Multiple initialization", "[tvgInitializer]")
{
REQUIRE(Initializer::init(CanvasEngine::Sw, 0) == Result::Success);
REQUIRE(Initializer::init(CanvasEngine::Sw, 0) == Result::Success);
REQUIRE(Initializer::term(CanvasEngine::Sw) == Result::Success);
REQUIRE(Initializer::init(CanvasEngine::Sw, 0) == Result::Success);
REQUIRE(Initializer::term(CanvasEngine::Sw) == Result::Success);
REQUIRE(Initializer::term(CanvasEngine::Sw) == Result::Success);
}
TEST_CASE("Negative termination", "[tvgInitializer]")
{
REQUIRE(Initializer::term(CanvasEngine::Sw) == Result::InsufficientCondition);
}
TEST_CASE("Many threads", "[tvgInitializer]")
{
REQUIRE(Initializer::init(CanvasEngine::Sw, -1) == Result::FailedAllocation);
REQUIRE(Initializer::term(CanvasEngine::Sw) == Result::InsufficientCondition);
}
TEST_CASE("Invalid engine", "[tvgInitializer]")
{
REQUIRE(Initializer::init(CanvasEngine(0), 0) == Result::InvalidArguments);
}

5
test/testMain.cpp Normal file
View file

@ -0,0 +1,5 @@
// The only purpose of this file is to DEFINE the catch config so it can include main()
#define CATCH_CONFIG_MAIN // This tells Catch to provide a main() - only do this in one cpp file
#include "catch.hpp"