diff options
| author | Jean-Philippe Bruyère <jp_bruyere@hotmail.com> | 2026-06-24 13:45:33 +0200 |
|---|---|---|
| committer | Jean-Philippe Bruyère <jp_bruyere@hotmail.com> | 2026-09-04 11:06:23 +0200 |
| commit | f6283839a7421755dbf31d6de2bba684e7d11887 (patch) | |
| tree | 2f02aab6cbcd679c5df7806b4eff08eab16dc42c | |
| parent | d06ebbe62bf0e07a7875d8bfe95f0c4e955b1a36 (diff) | |
unit tests and samples save/restore
| -rw-r--r-- | gunit_tests/CMakeLists.txt | 2 | ||||
| -rw-r--r-- | gunit_tests/context.cpp | 88 | ||||
| -rw-r--r-- | gunit_tests/drawTestBase.h | 3 | ||||
| -rw-r--r-- | gunit_tests/imageDraw.cpp | 33 | ||||
| -rw-r--r-- | gunit_tests/matrices.cpp | 11 | ||||
| -rw-r--r-- | gunit_tests/save_restore.cpp | 85 | ||||
| -rw-r--r-- | gunit_tests/surface.cpp | 2 | ||||
| -rw-r--r-- | gunit_tests/unitTest.h | 11 | ||||
| -rw-r--r-- | samples/tests/save_restore.cpp | 32 | ||||
| -rw-r--r-- | tests/data/img512.png | bin | 0 -> 287981 bytes | |||
| -rw-r--r-- | tests/img_surf.c | 8 | ||||
| -rw-r--r-- | tests/save_restore.c | 29 |
12 files changed, 259 insertions, 45 deletions
diff --git a/gunit_tests/CMakeLists.txt b/gunit_tests/CMakeLists.txt index bc24a21..234c0ee 100644 --- a/gunit_tests/CMakeLists.txt +++ b/gunit_tests/CMakeLists.txt @@ -31,6 +31,8 @@ add_executable("${PROJECT_NAME}" imageDraw.cpp matrices.cpp patternDraw.cpp + save_restore.cpp + unitTest.h ) target_compile_definitions("${PROJECT_NAME}" PUBLIC GTEST_PNG_ROOT="${GTEST_PNG_ROOT}" diff --git a/gunit_tests/context.cpp b/gunit_tests/context.cpp index 8d44804..cc068a1 100644 --- a/gunit_tests/context.cpp +++ b/gunit_tests/context.cpp @@ -1,5 +1,19 @@ -#include "vkvg.h" #include <gtest/gtest.h> +#include "vkvg.h" + +#include "unitTest.h" + +#define EXPECT_CP(x, y) \ +{ \ + vkvg_get_current_point(ctx, &a, &b); \ + EXPECT_FLOAT_EQ(x, a); \ + EXPECT_FLOAT_EQ(y, b); \ +} +#define EXPECT_NO_CP() \ +{ \ + EXPECT_EQ(false, vkvg_has_current_point(ctx)); \ + EXPECT_CP(0, 0); \ +} // The fixture for testing class Foo. class ContextTest : public testing::Test { @@ -162,20 +176,11 @@ TEST_F(ContextTest, CtxDrawBasicNullContext) { // vkvg_text_run_destroy(NULL); vkvg_show_text_run(ctx, NULL); } -#define EXPECT_CP(x, y) \ - { \ - vkvg_get_current_point(ctx, &a, &b); \ - EXPECT_FLOAT_EQ(x, a); \ - EXPECT_FLOAT_EQ(y, b); \ - } -#define EXPECT_NO_CP() \ - { \ - EXPECT_EQ(false, vkvg_has_current_point(ctx)); \ - EXPECT_CP(0, 0); \ - } + TEST_F(ContextTest, CtxBasicPathCommands) { - float a = 0.0, b = 0.0, c = 0.0, d = 0.0; + float a = 0.0, b = 0.0, c = 0.0, d = 0.0; + VkvgContext ctx = vkvg_create(surf); EXPECT_NO_CP(); @@ -215,6 +220,63 @@ TEST_F(ContextTest, CtxBasicPathCommands) { vkvg_new_path(ctx); EXPECT_NO_CP(); + EXPECT_EQ(VKVG_STATUS_SUCCESS, vkvg_status(ctx)); vkvg_destroy(ctx); } + + +TEST_F(ContextTest, CtxSaveRestore) { + vkvg_matrix_t mat = {1, 0, 0, 1, 0, 0}; + + VkvgContext ctx = vkvg_create(surf); + vkvg_save(ctx); + EXPECT_EQ(VKVG_STATUS_SUCCESS, vkvg_status(ctx)); + + vkvg_set_opacity(ctx, 0.5f); + vkvg_set_line_width(ctx, 1.5f); + vkvg_set_miter_limit(ctx, 0.3f); + vkvg_set_line_cap(ctx, VKVG_LINE_CAP_BUTT); + vkvg_set_line_join(ctx, VKVG_LINE_JOIN_BEVEL); + vkvg_set_operator(ctx, VKVG_OPERATOR_SOURCE); + vkvg_set_fill_rule(ctx, VKVG_FILL_RULE_NON_ZERO); + const float dash[]{0.1f,0.5f}; + vkvg_set_dash(ctx, dash, 2, 0.5f); + vkvg_translate(ctx, 10, 10); + vkvg_set_font_size(ctx, 30); + vkvg_select_font_face(ctx, "mono"); + vkvg_set_source_color(ctx, 0xFAFAFAFA); + vkvg_move_to(ctx, 200, 250); + + vkvg_restore(ctx); + EXPECT_EQ(VKVG_STATUS_SUCCESS, vkvg_status(ctx)); + + EXPECT_EQ(1, vkvg_get_opacity(ctx)); + EXPECT_EQ(VKVG_LINE_CAP_BUTT, vkvg_get_line_cap(ctx)); + EXPECT_EQ(VKVG_LINE_JOIN_MITER, vkvg_get_line_join(ctx)); + EXPECT_EQ(VKVG_OPERATOR_OVER, vkvg_get_operator(ctx)); + EXPECT_EQ(VKVG_FILL_RULE_NON_ZERO, vkvg_get_fill_rule(ctx)); + EXPECT_EQ(10, vkvg_get_miter_limit(ctx)); + EXPECT_EQ(1, vkvg_get_line_width(ctx)); + vkvg_get_matrix(ctx, &mat); + CHECK_MAT(1, 0, 0, 1, 0, 0); + + + vkvg_save(ctx); + EXPECT_EQ(VKVG_STATUS_SUCCESS, vkvg_status(ctx)); + + VkvgSurface surf2 = vkvg_surface_create(dev, 256, 256); + vkvg_set_source_surface(ctx, surf2, 10, 10); + vkvg_paint(ctx); + + vkvg_restore(ctx); + + VkvgPattern srcPat = vkvg_get_source(ctx); + + /*vkvg_set_source_rgb(ctx, 0.4f, 0.5f, 0.6f); + vkvg_set_source_rgba(ctx, 0.4f, 0.5f, 0.6f, 0.5f); + + vkvg_set_source(ctx, NULL);*/ + + //EXPECT_EQ(VKVG_STATUS_SUCCESS, vkvg_status(ctx)); +}
\ No newline at end of file diff --git a/gunit_tests/drawTestBase.h b/gunit_tests/drawTestBase.h index 79b2de6..350e08c 100644 --- a/gunit_tests/drawTestBase.h +++ b/gunit_tests/drawTestBase.h @@ -1,5 +1,6 @@ -#include "vkvg.h" +#pragma once #include <gtest/gtest.h> +#include "vkvg.h" #include <filesystem> namespace fs = std::filesystem; diff --git a/gunit_tests/imageDraw.cpp b/gunit_tests/imageDraw.cpp index 136a89f..b548b8e 100644 --- a/gunit_tests/imageDraw.cpp +++ b/gunit_tests/imageDraw.cpp @@ -1,4 +1,5 @@ #include "drawTestBase.h" +#include <math.h> class ImageDrawTest : public DrawTestBase { @@ -16,10 +17,6 @@ class ImageDrawTest : public DrawTestBase { } }; TEST_F(ImageDrawTest, References) { - EXPECT_EQ(VKVG_STATUS_NULL_POINTER, vkvg_surface_status(NULL)); - EXPECT_EQ(0, vkvg_surface_get_reference_count(NULL)); - EXPECT_NO_FATAL_FAILURE(vkvg_surface_reference(NULL)); - VkvgSurface surf2 = vkvg_surface_create_from_image(NULL, NULL); EXPECT_EQ(VKVG_STATUS_NULL_POINTER, vkvg_surface_status(surf2)); surf2 = vkvg_surface_create_from_image(dev, NULL); @@ -142,6 +139,34 @@ TEST_F(ImageDrawTest, PaintImageOnImage) { vkvg_paint(ctx); vkvg_surface_destroy(imgSurf2); + + EXPECT_EQ(VKVG_STATUS_SUCCESS, vkvg_status(ctx)); + vkvg_destroy(ctx); + + compareWithRefImage(); +} + +TEST_F(ImageDrawTest, PaintImageClipped) { + VkvgContext ctx = vkvg_create(surf); + vkvg_arc(ctx, 140, 140, 100, 0, 2.f*M_PI); + vkvg_clip(ctx); + vkvg_set_source_surface(ctx, imgSurf, 0, 0); + vkvg_paint(ctx); + + EXPECT_EQ(VKVG_STATUS_SUCCESS, vkvg_status(ctx)); + vkvg_destroy(ctx); + + compareWithRefImage(); +} +TEST_F(ImageDrawTest, PaintImageClipped2) { + VkvgContext ctx = vkvg_create(surf); + vkvg_rectangle(ctx, 100,100, 200, 200); + vkvg_arc(ctx, 140, 140, 100, 0, 2.f*M_PI); + vkvg_clip(ctx); + vkvg_set_source_surface(ctx, imgSurf, 0, 0); + vkvg_paint(ctx); + + EXPECT_EQ(VKVG_STATUS_SUCCESS, vkvg_status(ctx)); vkvg_destroy(ctx); compareWithRefImage(); diff --git a/gunit_tests/matrices.cpp b/gunit_tests/matrices.cpp index 714c884..679346e 100644 --- a/gunit_tests/matrices.cpp +++ b/gunit_tests/matrices.cpp @@ -1,13 +1,6 @@ -#include "vkvg.h" #include <gtest/gtest.h> - -#define CHECK_MAT(_xx, _yx, _xy, _yy, _x0, _y0) \ - EXPECT_FLOAT_EQ(mat.xx, _xx); \ - EXPECT_FLOAT_EQ(mat.yx, _yx); \ - EXPECT_FLOAT_EQ(mat.xy, _xy); \ - EXPECT_FLOAT_EQ(mat.yy, _yy); \ - EXPECT_FLOAT_EQ(mat.x0, _x0); \ - EXPECT_FLOAT_EQ(mat.y0, _y0); +#include "vkvg.h" +#include "unitTest.h" TEST(MatrixTest, MatixInit) { vkvg_matrix_t mat = {1, 0, 0, 1, 0, 0}; diff --git a/gunit_tests/save_restore.cpp b/gunit_tests/save_restore.cpp new file mode 100644 index 0000000..104f7bc --- /dev/null +++ b/gunit_tests/save_restore.cpp @@ -0,0 +1,85 @@ +#include "drawTestBase.h" +#include <math.h> + +class SaveRestoreTest : public DrawTestBase { + + protected: + fs::path imgPath = fs::path(GTEST_DATA_ROOT) / "img512.png"; + VkvgSurface imgSurf; + + void SetUp() override { + surf = vkvg_surface_create(dev, 512, 512); + imgSurf = vkvg_surface_create_from_image(dev, (char*)imgPath.c_str()); + } + void TearDown() override { + vkvg_surface_destroy(imgSurf); + DrawTestBase::TearDown(); + } + void recurse_draw(VkvgContext ctx, int depth, int maxDepth) { + static float gap = 15.f; + depth++; + vkvg_save(ctx); + + vkvg_rectangle(ctx, gap * depth, gap * depth, 512.f - gap * 2.f * depth, 512.f - gap * 2.f * depth); + vkvg_clip(ctx); + vkvg_set_source_rgb(ctx, 0.3f/depth, 1.f / depth, 1.f); + vkvg_paint(ctx); + vkvg_arc(ctx, 256, 256, 256.f - gap * depth, 0, 2.f*M_PI); + vkvg_clip(ctx); + vkvg_set_source_surface(ctx, imgSurf, 0, 0); + vkvg_paint(ctx); + + if (depth < maxDepth) + recurse_draw(ctx, depth, maxDepth); + + vkvg_restore(ctx); + EXPECT_EQ(VKVG_STATUS_SUCCESS, vkvg_status(ctx)); + } +}; + +TEST_F(SaveRestoreTest, RestoreClip1) { + VkvgContext ctx = vkvg_create(surf); + vkvg_arc(ctx, 140, 140, 100, 0, 2.f*M_PI); + vkvg_clip(ctx); + vkvg_save(ctx); + vkvg_reset_clip(ctx); + vkvg_restore(ctx); + vkvg_set_source_surface(ctx, imgSurf, 0, 0); + vkvg_paint(ctx); + + EXPECT_EQ(VKVG_STATUS_SUCCESS, vkvg_status(ctx)); + vkvg_destroy(ctx); + + compareWithRefImage(); +} + +TEST_F(SaveRestoreTest, RecursePaint5) { + VkvgContext ctx = vkvg_create(surf); + + EXPECT_NO_FATAL_FAILURE(recurse_draw(ctx, 0, 5)); + + vkvg_destroy(ctx); + + compareWithRefImage(); +} + + +TEST_F(SaveRestoreTest, RecursePaint10) { + VkvgContext ctx = vkvg_create(surf); + + EXPECT_NO_FATAL_FAILURE(recurse_draw(ctx, 0, 10)); + + vkvg_destroy(ctx); + + compareWithRefImage(); +} + +TEST_F(SaveRestoreTest, RecursePaint16) { + VkvgContext ctx = vkvg_create(surf); + + EXPECT_NO_THROW(recurse_draw(ctx, 0, 16)); + + vkvg_destroy(ctx); + + compareWithRefImage(); +} diff --git a/gunit_tests/surface.cpp b/gunit_tests/surface.cpp index 03eb227..66df1b1 100644 --- a/gunit_tests/surface.cpp +++ b/gunit_tests/surface.cpp @@ -36,6 +36,8 @@ class SurfaceTest : public testing::Test { TEST_F(SurfaceTest, SurfCreate) { EXPECT_EQ(VKVG_STATUS_NULL_POINTER, vkvg_surface_status(NULL)); + EXPECT_EQ(0, vkvg_surface_get_reference_count(NULL)); + EXPECT_NO_FATAL_FAILURE(vkvg_surface_reference(NULL)); EXPECT_NO_FATAL_FAILURE(vkvg_surface_destroy(NULL)); VkvgSurface surf = vkvg_surface_create(NULL, 0, 0); diff --git a/gunit_tests/unitTest.h b/gunit_tests/unitTest.h new file mode 100644 index 0000000..9cd05d2 --- /dev/null +++ b/gunit_tests/unitTest.h @@ -0,0 +1,11 @@ +#pragma once + +#define CHECK_MAT(_xx, _yx, _xy, _yy, _x0, _y0) \ +EXPECT_FLOAT_EQ(mat.xx, _xx); \ + EXPECT_FLOAT_EQ(mat.yx, _yx); \ + EXPECT_FLOAT_EQ(mat.xy, _xy); \ + EXPECT_FLOAT_EQ(mat.yy, _yy); \ + EXPECT_FLOAT_EQ(mat.x0, _x0); \ + EXPECT_FLOAT_EQ(mat.y0, _y0); + + diff --git a/samples/tests/save_restore.cpp b/samples/tests/save_restore.cpp index 48833e3..063e085 100644 --- a/samples/tests/save_restore.cpp +++ b/samples/tests/save_restore.cpp @@ -1,14 +1,13 @@ #include "VkvgTest.hpp" +VkvgSurface imgSurf; + void recurse_draw(VkvgContext ctx, int depth) { depth++; vkvg_save(ctx); vkvg_translate(ctx, 5, 5); vkvg_rectangle(ctx, (float)depth, (float)depth, 200, 200); - /*vkvg_clip_preserve(ctx); - vkvg_set_source_rgb(ctx, 1.f/depth, 1.f / depth, 1.f / depth); - vkvg_fill_preserve(ctx);*/ vkvg_set_source_rgb(ctx, 0, 1, 0); vkvg_stroke(ctx); @@ -17,12 +16,39 @@ void recurse_draw(VkvgContext ctx, int depth) { vkvg_restore(ctx); } +void recurse_draw_clipped(VkvgContext ctx, int depth, int maxDepth) { + static float gap = 15.f; + depth++; + vkvg_save(ctx); + + vkvg_rectangle(ctx, gap * depth, gap * depth, 512.f - gap * 2.f * depth, 512.f - gap * 2.f * depth); + vkvg_clip(ctx); + vkvg_set_source_rgb(ctx, 0.3f/depth, 1.f / depth, 1.f); + vkvg_paint(ctx); + vkvg_arc(ctx, 256, 256, 256.f - gap * depth, 0, 2.f*M_PI); + vkvg_clip(ctx); + vkvg_set_source_surface(ctx, imgSurf, 0, 0); + vkvg_paint(ctx); + + if (depth < maxDepth) + recurse_draw_clipped(ctx, depth, maxDepth); + + vkvg_restore(ctx); + depth--; +} TEST(save_restrore) { VkvgContext ctx = vkvg_create(test->surf); recurse_draw(ctx, 0); vkvg_destroy(ctx); } +TEST(save_restrore_clipped) { + VkvgContext ctx = vkvg_create(test->surf); + imgSurf = vkvg_surface_create_from_image(test->device, GET_PATH("img512.png")); + recurse_draw_clipped(ctx, 0, 14); + vkvg_surface_destroy(imgSurf); + vkvg_destroy(ctx); +} diff --git a/tests/data/img512.png b/tests/data/img512.png Binary files differnew file mode 100644 index 0000000..6ac7090 --- /dev/null +++ b/tests/data/img512.png diff --git a/tests/img_surf.c b/tests/img_surf.c index 874e849..0d52ced 100644 --- a/tests/img_surf.c +++ b/tests/img_surf.c @@ -1,10 +1,10 @@ #include "test.h" -const char* imgPath = TESTS_DATA_ROOT "miroir.jpg"; -const char* imgPath2 = TESTS_DATA_ROOT "miroir.png"; +const char* imgPath = TESTS_DATA_ROOT "mirror.jpg"; +const char* imgPath2 = TESTS_DATA_ROOT "mirror.png"; const char* imgPath3 = TESTS_DATA_ROOT "filled.png"; -const char* imgPath4 = TESTS_DATA_ROOT "miroir2.png"; -const char* imgPath5 = TESTS_DATA_ROOT "miroir2-64.png"; +const char* imgPath4 = TESTS_DATA_ROOT "mirfor2.png"; +const char* imgPath5 = TESTS_DATA_ROOT "mirfor2-64.png"; void paint() { VkvgContext ctx = vkvg_create(surf); diff --git a/tests/save_restore.c b/tests/save_restore.c index 59c82b3..dc194a5 100644 --- a/tests/save_restore.c +++ b/tests/save_restore.c @@ -1,28 +1,35 @@ #include "test.h" -void recurse_draw(VkvgContext ctx, int depth) { +const char* imgPath = TESTS_DATA_ROOT "img512.png"; +VkvgSurface imgSurf; + +void recurse_draw(VkvgContext ctx, int depth, int maxDepth) { + static float gap = 15.f; depth++; vkvg_save(ctx); - vkvg_translate(ctx, 5, 5); - vkvg_rectangle(ctx, (float)depth, (float)depth, 200, 200); - /*vkvg_clip_preserve(ctx); - vkvg_set_source_rgb(ctx, 1.f/depth, 1.f / depth, 1.f / depth); - vkvg_fill_preserve(ctx);*/ - vkvg_set_source_rgb(ctx, 0, 1, 0); - vkvg_stroke(ctx); + vkvg_rectangle(ctx, gap * depth, gap * depth, 512.f - gap * 2.f * depth, 512.f - gap * 2.f * depth); + vkvg_clip(ctx); + vkvg_set_source_rgb(ctx, 0.3f/depth, 1.f / depth, 1.f); + vkvg_paint(ctx); + vkvg_arc(ctx, 256, 256, 256.f - gap * depth, 0, 2.f*M_PI); + vkvg_clip(ctx); + vkvg_set_source_surface(ctx, imgSurf, 0, 0); + vkvg_paint(ctx); - if (depth < 20) - recurse_draw(ctx, depth); + if (depth < maxDepth) + recurse_draw(ctx, depth, maxDepth); vkvg_restore(ctx); } void test() { VkvgContext ctx = vkvg_create(surf); + imgSurf = vkvg_surface_create_from_image(device, imgPath); - recurse_draw(ctx, 0); + recurse_draw(ctx, 0, 13); + vkvg_surface_destroy(imgSurf); vkvg_destroy(ctx); } |