From df8fc1949c1a44f43f28ce6184989d8fb69991d5 Mon Sep 17 00:00:00 2001 From: Hermet Park Date: Tue, 19 Nov 2024 16:45:33 +0900 Subject: [PATCH] test: ++paint reference counting --- test/capi/capiPaint.cpp | 47 +++++++++++++++++++++++++++++++++++++++++ test/testPaint.cpp | 45 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 92 insertions(+) diff --git a/test/capi/capiPaint.cpp b/test/capi/capiPaint.cpp index 74cd2125..266b5cde 100644 --- a/test/capi/capiPaint.cpp +++ b/test/capi/capiPaint.cpp @@ -338,3 +338,50 @@ TEST_CASE("Paint LumaMask Composite Method", "[capiPaint]") REQUIRE(tvg_paint_del(paint) == TVG_RESULT_SUCCESS); } + +TEST_CASE("Paint Reference Counter", "[capiPaint]") +{ + Tvg_Paint* paint = tvg_shape_new(); + REQUIRE(tvg_paint_get_ref(paint) == 0); + REQUIRE(tvg_paint_unref(paint, false) == 0); + REQUIRE(tvg_paint_ref(paint) == 1); + REQUIRE(tvg_paint_ref(paint) == 2); + REQUIRE(tvg_paint_ref(paint) == 3); + REQUIRE(tvg_paint_unref(paint, true) == 2); + REQUIRE(tvg_paint_unref(paint, true) == 1); + REQUIRE(tvg_paint_unref(paint, true) == 0); + + tvg_engine_init(TVG_ENGINE_SW, 0); + + Tvg_Canvas* canvas = tvg_swcanvas_create(); + + paint = tvg_shape_new(); + REQUIRE(tvg_paint_ref(paint) == 1); + tvg_canvas_push(canvas, paint); + REQUIRE(tvg_paint_get_ref(paint) == 2); + tvg_canvas_clear(canvas, true, false); + REQUIRE(tvg_paint_get_ref(paint) == 1); + REQUIRE(tvg_paint_unref(paint, true) == 0); + + paint = tvg_shape_new(); + REQUIRE(tvg_paint_ref(paint) == 1); + Tvg_Paint* scene = tvg_scene_new(); + tvg_scene_push(scene, paint); + tvg_canvas_push(canvas, scene); + tvg_canvas_clear(canvas, true, false); + REQUIRE(tvg_paint_get_ref(paint) == 1); + REQUIRE(tvg_paint_unref(paint, true) == 0); + + paint = tvg_shape_new(); + REQUIRE(tvg_paint_ref(paint) == 1); + scene = tvg_scene_new(); + tvg_scene_push(scene, paint); + tvg_scene_clear(scene, true); + tvg_canvas_push(canvas, scene); + tvg_canvas_clear(canvas, true, false); + REQUIRE(tvg_paint_unref(paint, true) == 0); + + tvg_canvas_destroy(canvas); + + tvg_engine_term(TVG_ENGINE_SW); +} \ No newline at end of file diff --git a/test/testPaint.cpp b/test/testPaint.cpp index 467e07ee..10ce8dfd 100644 --- a/test/testPaint.cpp +++ b/test/testPaint.cpp @@ -286,4 +286,49 @@ TEST_CASE("Blending", "[tvgPaint]") //SoftLight REQUIRE(shape->blend(BlendMethod::SoftLight) == Result::Success); +} + +TEST_CASE("Refernce Count", "[tvgPaint]") +{ + auto shape = Shape::gen(); + REQUIRE(shape->refCnt() == 0); + REQUIRE(shape->unref(false) == 0); + REQUIRE(shape->ref() == 1); + REQUIRE(shape->ref() == 2); + REQUIRE(shape->ref() == 3); + REQUIRE(shape->unref() == 2); + REQUIRE(shape->unref() == 1); + REQUIRE(shape->unref() == 0); + + Initializer::init(0); + + auto canvas = unique_ptr(SwCanvas::gen()); + + shape = Shape::gen(); + REQUIRE(shape->ref() == 1); + canvas->push(shape); + REQUIRE(shape->refCnt() == 2); + canvas->clear(); + REQUIRE(shape->refCnt() == 1); + REQUIRE(shape->unref() == 0); + + shape = Shape::gen(); + REQUIRE(shape->ref() == 1); + auto scene = Scene::gen(); + scene->push(shape); + canvas->push(scene); + canvas->clear(); + REQUIRE(shape->refCnt() == 1); + REQUIRE(shape->unref() == 0); + + shape = Shape::gen(); + REQUIRE(shape->ref() == 1); + scene = Scene::gen(); + scene->push(shape); + scene->clear(); + canvas->push(scene); + canvas->clear(); + REQUIRE(shape->unref() == 0); + + Initializer::term(); } \ No newline at end of file