diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index d24558e..7a8ff48 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -43,6 +43,7 @@ jobs: working-directory: ${{runner.workspace}}/build run: | cmake $GITHUB_WORKSPACE \ + -DCMAKE_CXX_STANDARD=20 \ -DCOMPILE_WARNING_AS_ERROR=ON \ -DACTSVG_BUILD_TESTING=ON \ -DACTSVG_BUILD_META=ON \ diff --git a/core/include/actsvg/core/svg.hpp b/core/include/actsvg/core/svg.hpp index b98f08c..677a430 100644 --- a/core/include/actsvg/core/svg.hpp +++ b/core/include/actsvg/core/svg.hpp @@ -10,9 +10,12 @@ #include #include +#include #include #include #include +#include +#include #include #include @@ -177,7 +180,7 @@ struct file { /// Default header tail definitions std::string _html_head = "\n\n"; - std::string _svg_head = " &vb_, bool adjust_ = true); - /** Write to ostream */ + /** Dump file into a string stream */ + std::stringstream to_stream() const; + + /** Produce a checksum for this file using a stable crc32 implementation + */ + std::uint32_t checksum() const; + + /** Write to ostream, it uses to_stream() */ friend std::ostream &operator<<(std::ostream &os_, const file &f_); }; diff --git a/core/include/actsvg/core/utils.hpp b/core/include/actsvg/core/utils.hpp index 7bed25b..04538cb 100644 --- a/core/include/actsvg/core/utils.hpp +++ b/core/include/actsvg/core/utils.hpp @@ -8,7 +8,9 @@ #pragma once +#include #include +#include #include #include #include @@ -314,6 +316,30 @@ point3_container place_vertices(const point3_container &pc_, return placed; } +/*** + * @brief Computes the CRC32 checksum of a given string. + * + * @param data The input string. + * @return The CRC32 checksum. + */ +static inline std::uint32_t crc32(const std::string &data) { + static constexpr std::array table = [] { + std::array tab{}; + for (std::uint32_t i = 0; i < 256; i++) { + std::uint32_t c = i; + for (int j = 0; j < 8; j++) + c = (c & 1) ? (0xEDB88320u ^ (c >> 1)) : (c >> 1); + tab[i] = c; + } + return tab; + }(); + + std::uint32_t crc = 0xFFFFFFFFu; + for (unsigned char ch : data) + crc = table[(crc ^ ch) & 0xFFu] ^ (crc >> 8); + return crc ^ 0xFFFFFFFFu; +} + } // namespace utils } // namespace actsvg diff --git a/core/src/core/svg.cpp b/core/src/core/svg.cpp index 3855374..8bd3988 100644 --- a/core/src/core/svg.cpp +++ b/core/src/core/svg.cpp @@ -19,6 +19,7 @@ #include "actsvg/core.hpp" #include "actsvg/core/defs.hpp" #include "actsvg/core/style.hpp" +#include "actsvg/core/utils.hpp" namespace actsvg::svg { bool object::is_defined() const { @@ -166,10 +167,11 @@ void file::set_view_box(const std::array &vbox_, bool adjust_) { } } -std::ostream &operator<<(std::ostream &os_, const file &f_) { +std::stringstream file::to_stream() const { + std::stringstream sstr; std::map definitions; - for (const auto &o : f_._objects) { + for (const auto &o : _objects) { if (o._active) { for (const auto &d : o._definitions) { definitions[d._id] = d; @@ -177,33 +179,44 @@ std::ostream &operator<<(std::ostream &os_, const file &f_) { } } - std::array vbox = f_._view_box.value_or(f_.view_box()); + std::array vbox = _view_box.value_or(view_box()); - if (f_._add_html) { - os_ << f_._html_head; + if (_add_html) { + sstr << _html_head; } - os_ << f_._svg_head; - os_ << " width=\"" << f_._width << "\" height=\"" << f_._height << "\""; - os_ << " viewBox=\"" << vbox[0] << " " << vbox[1] << " " << vbox[2] << " " - << vbox[3] << "\""; - os_ << f_._svg_def_end; + sstr << _svg_head; + sstr << " width=\"" << static_cast(_width) << "\" height=\"" + << static_cast(_height) << "\""; + sstr << " viewBox=\"" << static_cast(vbox[0]) << " " + << static_cast(vbox[1]) << " " << static_cast(vbox[2]) << " " + << static_cast(vbox[3]) << "\""; + sstr << _svg_def_end; + // Write the definitions first if (not definitions.empty()) { - os_ << ""; + sstr << ""; for (auto [key, value] : definitions) { - os_ << value; + sstr << value; } - os_ << ""; + sstr << ""; } // Now write the objects - for (auto &o : f_._objects) { - os_ << o; - } - os_ << f_._svg_tail; - if (f_._add_html) { - os_ << f_._html_tail; + std::ranges::for_each(_objects, [&sstr](const auto &o) { sstr << o; }); + + sstr << _svg_tail; + if (_add_html) { + sstr << _html_tail; } + return sstr; +} + +std::uint32_t file::checksum() const { + return utils::crc32(to_stream().str()); +} + +std::ostream &operator<<(std::ostream &os_, const file &f_) { + os_ << f_.to_stream().str(); return os_; } diff --git a/tests/common/test_checksum.hpp b/tests/common/test_checksum.hpp new file mode 100644 index 0000000..2603041 --- /dev/null +++ b/tests/common/test_checksum.hpp @@ -0,0 +1,113 @@ +// This file is part of the actsvg package. +// +// Copyright (C) 2025 CERN for the benefit of the ACTS project +// +// This Source Code Form is subject to the terms of the Mozilla Public +// License, v. 2.0. If a copy of the MPL was not distributed with this +// file, You can obtain one at http://mozilla.org/MPL/2.0/. + +#pragma once + +#include +#include +#include +#include + +namespace actsvg::test { + +// clang-format off +/// @brief Map of map to allow for multiple files per test +static std::map> + checksum_references = { +{"arc_plain", {{"test_core_arc.svg", 598987246u}}}, +{"arrows", {{"test_core_arrows.svg", 156197267u}}}, +{"barrel_x_y_view", {{"test_core_barrel_xy.svg", 475857493u}}}, +{"barrel_z_phi_view_grid", {{"test_core_barrel_grid_zphi.svg", 846567370u}}}, +{"barrel_z_phi_view", {{"test_core_barrel_zphi.svg", 2866154162u}}}, +{"bezier_single_segment", {{"test_core_bezier_single.svg", 1452396398u}}}, +{"bezier_spiral", {{"test_core_bezier_spiral.svg", 3456691438u}}}, +{"cartesian_grid", {{"test_core_cartesian_grid.svg", 2070215038u}}}, +{"circle_plain", {{"test_core_circle.svg", 3148444685u}}}, +{"circle_scaled", {{"test_core_circle_scaled.svg", 3227732129u}}}, +{"circle_shifted", {{"test_core_circle_shifted.svg", 1574532577u}}}, +{"copy_triangle", {{"test_core_copy_triangle.svg", 325380468u}}}, +{"disc_sector", {{"test_core_sector.svg", 2519980248u}}}, +{"endcap_x_y_view_grid", {{"test_core_endcap_grid_xy.svg", 2340117177u}}}, +{"endcap_x_y_view", {{"test_core_endcap_xy.svg", 3832402486u}}}, +{"endcap_z_r_view", {{"test_core_endcap_zr.svg", 1171393426u}}}, +{"fan_grid", {{"test_core_fan_grid.svg", 4027289050u}}}, +{"gradient_box_horizontal_label", {{"test_core_gradient_box_horizontal_label.svg", 474900462u}}}, +{"gradient_box_horizontal", {{"test_core_gradient_box_horizontal.svg", 305729971u}}}, +{"gradient_box_linear_x", {{"test_core_gradient_box.svg", 3168005651u}}}, +{"gradient_box_vertical_label", {{"test_core_gradient_box_vertical_label.svg", 4211281398u}}}, +{"gradient_box_vertical", {{"test_core_gradient_box_vertical.svg", 4211281398u}}}, +{"info_box", {{"test_core_infobox.svg", 391203306u}}}, +{"label_center_center", {{"test_core_label_center_center.svg", 2273812319u}}}, +{"label_left_bottom", {{"test_core_label_left_bottom.svg", 2284580567u}}}, +{"label_left_center", {{"test_core_label_left_center.svg", 3350259941u}}}, +{"label_left_top", {{"test_core_label_left_top.svg", 2653912820u}}}, +{"label_right_bottom", {{"test_core_label_right_bottom.svg", 858078508u}}}, +{"label_right_center", {{"test_core_label_right_center.svg", 3086957164u}}}, +{"label_right_top", {{"test_core_label_right_top.svg", 812048451u}}}, +{"line_plain", {{"test_core_line.svg", 2384224490u}}}, +{"line_scaled", {{"test_core_line_scaled.svg", 2130133868u}}}, +{"line_shifted", {{"test_core_line_shifted.svg", 681977029u}}}, +{"markers", {{"test_core_markers.svg", 1722066658u}}}, +{"measure", {{"test_core_measures.svg", 2274566291u}}}, +{"multiline_text_outside_playground", {{"test_core_text_multiline_range.svg", 660718925u}}}, +{"multiline_text", {{"test_core_text_multiline.svg", 986506060u}}}, +{"polar_grid", {{"test_core_polar_grid.svg", 4052172164u}}}, +{"polyline", {{"test_core_polyline.svg", 2285432013u}}}, +{"rectangle", {{"test_core_rectangle.svg", 3386630377u}}}, +{"tiled_cartesian_grid", {{"test_core_tiled_cartesian_grid.svg", 3030659065u}}}, +{"tiled_fan_grid", {{"test_core_tiled_fan_grid.svg", 232443122u}}}, +{"tiled_polar_grid", {{"test_core_tiled_polar_grid.svg", 4084096140u}}}, +{"triangle_highligh", {{"test_core_triangle_highlight.svg", 3994778312u}}}, +{"triangle_rotated_shifted", {{"test_core_triangle_rotated_shifted.svg", 3296491611u}}}, +{"triangle_rotated", {{"test_core_triangle_rotated.svg", 1263086013u}}}, +{"triangle_scaled", {{"test_core_triangle_scaled.svg", 3282708057u}}}, +{"triangle_shifted", {{"test_core_triangle_shifted.svg", 1124525001u}}}, +{"triangle", {{"test_core_triangle.svg", 42086428u}}}, +{"unconnected_text", {{"test_core_text.svg", 3211069024u}}}, +}; +// clang-format on + +/** @brief Checksum test against reference + * + * @param test_name Name of the test + * @param file_name Name of the file + * @param checksum Checksum value to test from reference + * + * @return True if the checksum matches the reference, false otherwise + */ +static inline bool checksum(const std::string &test_name, + const std::string &file_name, + std::size_t checksum) { + auto test_it = checksum_references.find(test_name); + if (test_it != checksum_references.end()) { + auto file_it = test_it->second.find(file_name); + if (file_it != test_it->second.end()) { + bool result = (file_it->second == checksum); + if (not result) { + std::fstream fs; + fs.open("checksum_mismatch_" + test_name + ".log", + std::ios::app); + fs << "Checksum mismatch! For updating, use this line: " + << '\n'; + fs << "{\"" << test_name << "\", {{\"" << file_name << "\", " + << checksum << "u}}}," << '\n'; + fs.close(); + } + return result; + } + } + std::fstream fs; + fs.open("checksum_not_found_" + test_name + ".log", std::ios::app); + fs << "Reference not found! If you want to add it, use this line: " << '\n'; + fs << "{\"" << test_name << "\", {{\"" << file_name << "\", " << checksum + << "u}}}," << '\n'; + fs.close(); + return false; +} + +} // namespace actsvg::test diff --git a/tests/core/arc.cpp b/tests/core/arc.cpp index 6084726..93da9c6 100644 --- a/tests/core/arc.cpp +++ b/tests/core/arc.cpp @@ -12,6 +12,7 @@ #include #include "../common/playground.hpp" +#include "../common/test_checksum.hpp" #include "actsvg/core/draw.hpp" using namespace actsvg; @@ -22,7 +23,8 @@ TEST(core, arc_plain) { // Write out the file std::ofstream fo; - fo.open("test_core_arc.svg"); + std::string file_name = "test_core_arc.svg"; + fo.open(file_name); scalar phi_min = -0.25; scalar phi_max = 0.75; @@ -44,4 +46,10 @@ TEST(core, arc_plain) { of.add_object(descr); fo << of; fo.close(); + + /// Checksum test against reference + std::string test_name = + ::testing::UnitTest::GetInstance()->current_test_info()->name(); + std::uint32_t file_checksum = of.checksum(); + EXPECT_TRUE(test::checksum(test_name, file_name, file_checksum)); } diff --git a/tests/core/arrows.cpp b/tests/core/arrows.cpp index 8fb9d89..26c8330 100644 --- a/tests/core/arrows.cpp +++ b/tests/core/arrows.cpp @@ -11,11 +11,12 @@ #include #include "../common/playground.hpp" +#include "../common/test_checksum.hpp" #include "actsvg/core.hpp" using namespace actsvg; -TEST(draw, arrows) { +TEST(core, arrows) { // Set a playground auto pg = test::playground({-400, -400}, {400, 400}); @@ -39,7 +40,14 @@ TEST(draw, arrows) { mfile.add_object(a2); std::ofstream tstream; - tstream.open("test_core_arrows.svg"); + std::string file_name = "test_core_arrows.svg"; + tstream.open(file_name); tstream << mfile; tstream.close(); + + /// Checksum test against reference + std::string test_name = + ::testing::UnitTest::GetInstance()->current_test_info()->name(); + std::uint32_t file_checksum = mfile.checksum(); + EXPECT_TRUE(test::checksum(test_name, file_name, file_checksum)); } diff --git a/tests/core/barrel.cpp b/tests/core/barrel.cpp index ec01733..f806533 100644 --- a/tests/core/barrel.cpp +++ b/tests/core/barrel.cpp @@ -13,6 +13,7 @@ #include #include +#include "../common/test_checksum.hpp" #include "actsvg/core.hpp" #include "actsvg/data/odd_pixel_barrel.hpp" @@ -20,7 +21,7 @@ using namespace actsvg; auto barrel_modules = data::generate_barrel_modules(); -TEST(barrel, x_y_view) { +TEST(core, barrel_x_y_view) { svg::file barrel_file; @@ -46,13 +47,20 @@ TEST(barrel, x_y_view) { barrel_file.add_objects(modules); // File output + std::string file_name = "test_core_barrel_xy.svg"; std::ofstream barrel_stream; - barrel_stream.open("test_core_barrel_xy.svg"); + barrel_stream.open(file_name); barrel_stream << barrel_file; barrel_stream.close(); + + /// Checksum test against reference + std::string test_name = + ::testing::UnitTest::GetInstance()->current_test_info()->name(); + std::uint32_t file_checksum = barrel_file.checksum(); + EXPECT_TRUE(test::checksum(test_name, file_name, file_checksum)); } -TEST(barrel, z_phi_view) { +TEST(core, barrel_z_phi_view) { svg::file barrel_file; @@ -82,13 +90,20 @@ TEST(barrel, z_phi_view) { barrel_file.add_objects(modules); // File output + std::string file_name = "test_core_barrel_zphi.svg"; std::ofstream barrel_stream; - barrel_stream.open("test_core_barrel_zphi.svg"); + barrel_stream.open(file_name); barrel_stream << barrel_file; barrel_stream.close(); + + // Checksum test against reference + std::string test_name = + ::testing::UnitTest::GetInstance()->current_test_info()->name(); + std::uint32_t file_checksum = barrel_file.checksum(); + EXPECT_TRUE(test::checksum(test_name, file_name, file_checksum)); } -TEST(barrel, z_phi_view_grid) { +TEST(core, barrel_z_phi_view_grid) { svg::file barrel_file; @@ -163,8 +178,15 @@ TEST(barrel, z_phi_view_grid) { barrel_file.add_objects(grid._sub_objects); // File output + std::string file_name = "test_core_barrel_grid_zphi.svg"; std::ofstream barrel_stream; - barrel_stream.open("test_core_barrel_grid_zphi.svg"); + barrel_stream.open(file_name); barrel_stream << barrel_file; barrel_stream.close(); + + // Checksum test against reference + std::string test_name = + ::testing::UnitTest::GetInstance()->current_test_info()->name(); + std::uint32_t file_checksum = barrel_file.checksum(); + EXPECT_TRUE(test::checksum(test_name, file_name, file_checksum)); } diff --git a/tests/core/bezier.cpp b/tests/core/bezier.cpp index 57fdaec..19a2f2e 100644 --- a/tests/core/bezier.cpp +++ b/tests/core/bezier.cpp @@ -12,6 +12,7 @@ #include #include "../common/playground.hpp" +#include "../common/test_checksum.hpp" #include "actsvg/core/draw.hpp" using namespace actsvg; @@ -31,8 +32,9 @@ TEST(core, bezier_single_segment) { auto pg = test::playground({-400, -400}, {400, 400}); // Write out the file + std::string file_name = "test_core_bezier_single.svg"; std::ofstream fo; - fo.open("test_core_bezier_single.svg"); + fo.open(file_name); auto c = draw::bezier("bezier", points_dirs); @@ -44,6 +46,12 @@ TEST(core, bezier_single_segment) { of.add_object(descr); fo << of; fo.close(); + + // Checksum test against reference + std::string test_name = + ::testing::UnitTest::GetInstance()->current_test_info()->name(); + std::uint32_t file_checksum = of.checksum(); + EXPECT_TRUE(test::checksum(test_name, file_name, file_checksum)); } TEST(core, bezier_spiral) { @@ -70,8 +78,9 @@ TEST(core, bezier_spiral) { auto pg = test::playground({-400, -400}, {400, 400}); // Write out the file + std::string file_name = "test_core_bezier_spiral.svg"; std::ofstream fo; - fo.open("test_core_bezier_spiral.svg"); + fo.open(file_name); auto c = draw::bezier("bezier", points_dirs); auto descr = @@ -83,4 +92,10 @@ TEST(core, bezier_spiral) { of.add_object(descr); fo << of; fo.close(); + + // Checksum test against reference + std::string test_name = + ::testing::UnitTest::GetInstance()->current_test_info()->name(); + std::uint32_t file_checksum = of.checksum(); + EXPECT_TRUE(test::checksum(test_name, file_name, file_checksum)); } diff --git a/tests/core/circle.cpp b/tests/core/circle.cpp index d3f61ef..bfb1487 100644 --- a/tests/core/circle.cpp +++ b/tests/core/circle.cpp @@ -12,6 +12,7 @@ #include #include "../common/playground.hpp" +#include "../common/test_checksum.hpp" #include "actsvg/core/draw.hpp" #include "actsvg/core/style.hpp" @@ -22,8 +23,9 @@ TEST(core, circle_plain) { auto pg = test::playground({-400, -400}, {400, 400}); // Write out the file + std::string file_name = "test_core_circle.svg"; std::ofstream fo; - fo.open("test_core_circle.svg"); + fo.open(file_name); // Draw the circles auto cc = draw::circle("cc", {0., 0.}, 10, @@ -42,6 +44,12 @@ TEST(core, circle_plain) { of.add_object(descr); fo << of; fo.close(); + + // Checksum test against reference + std::string test_name = + ::testing::UnitTest::GetInstance()->current_test_info()->name(); + std::uint32_t file_checksum = of.checksum(); + EXPECT_TRUE(test::checksum(test_name, file_name, file_checksum)); } TEST(core, circle_shifted) { @@ -52,8 +60,9 @@ TEST(core, circle_shifted) { style::transform t{{100, 100}}; // Write out the file + std::string file_name = "test_core_circle_shifted.svg"; std::ofstream fo; - fo.open("test_core_circle_shifted.svg"); + fo.open(file_name); auto c = draw::circle("c", {40., -20.}, 25., style::fill{style::color{{0, 125, 0}}}, @@ -68,6 +77,12 @@ TEST(core, circle_shifted) { of.add_object(descr); fo << of; fo.close(); + + // Checksum test against reference + std::string test_name = + ::testing::UnitTest::GetInstance()->current_test_info()->name(); + std::uint32_t file_checksum = of.checksum(); + EXPECT_TRUE(test::checksum(test_name, file_name, file_checksum)); } TEST(core, circle_scaled) { @@ -79,8 +94,9 @@ TEST(core, circle_scaled) { t._scale = {25, 25}; // Write out the file + std::string file_name = "test_core_circle_scaled.svg"; std::ofstream fo; - fo.open("test_core_circle_scaled.svg"); + fo.open(file_name); auto c = draw::circle("c", {-10., -10.}, 4, style::fill{style::color{{0, 125, 0}}}, @@ -96,4 +112,10 @@ TEST(core, circle_scaled) { of.add_object(descr); fo << of; fo.close(); + + // Checksum test against reference + std::string test_name = + ::testing::UnitTest::GetInstance()->current_test_info()->name(); + std::uint32_t file_checksum = of.checksum(); + EXPECT_TRUE(test::checksum(test_name, file_name, file_checksum)); } diff --git a/tests/core/copy.cpp b/tests/core/copy.cpp index c19ff6f..3e77d77 100644 --- a/tests/core/copy.cpp +++ b/tests/core/copy.cpp @@ -12,6 +12,7 @@ #include #include "../common/playground.hpp" +#include "../common/test_checksum.hpp" #include "actsvg/core/draw.hpp" using namespace actsvg; @@ -24,8 +25,9 @@ TEST(core, copy_triangle) { auto pg = test::playground({-400, -400}, {400, 400}); // Write out the file + std::string file_name = "test_core_copy_triangle.svg"; std::ofstream fo; - fo.open("test_core_copy_triangle.svg"); + fo.open(file_name); std::vector> triangle = { {-100, -100}, {-100, 100}, {100, 100}}; @@ -67,4 +69,10 @@ TEST(core, copy_triangle) { of.add_object(descr); fo << of; fo.close(); + + // Checksum test against reference + std::string test_name = + ::testing::UnitTest::GetInstance()->current_test_info()->name(); + std::uint32_t file_checksum = of.checksum(); + EXPECT_TRUE(test::checksum(test_name, file_name, file_checksum)); } diff --git a/tests/core/endcap.cpp b/tests/core/endcap.cpp index 0bde3bf..b01c836 100644 --- a/tests/core/endcap.cpp +++ b/tests/core/endcap.cpp @@ -13,6 +13,7 @@ #include #include +#include "../common/test_checksum.hpp" #include "actsvg/core.hpp" #include "actsvg/data/odd_pixel_endcap.hpp" @@ -20,7 +21,7 @@ using namespace actsvg; auto endcap_modules = data::generate_endcap_modules(); -TEST(endcap, z_r_view) { +TEST(core, endcap_z_r_view) { svg::file ec_file; @@ -47,13 +48,20 @@ TEST(endcap, z_r_view) { ec_file.add_objects(modules); // File output + std::string file_name = "test_core_endcap_zr.svg"; std::ofstream ec_stream; - ec_stream.open("test_core_endcap_zr.svg"); + ec_stream.open(file_name); ec_stream << ec_file; ec_stream.close(); + + // Checksum test against reference + std::string test_name = + ::testing::UnitTest::GetInstance()->current_test_info()->name(); + std::uint32_t file_checksum = ec_file.checksum(); + EXPECT_TRUE(test::checksum(test_name, file_name, file_checksum)); } -TEST(endcap, x_y_view) { +TEST(core, endcap_x_y_view) { svg::file ec_file; ec_file._height = 800; @@ -96,13 +104,20 @@ TEST(endcap, x_y_view) { ec_file.add_objects(labels); // File output + std::string file_name = "test_core_endcap_xy.svg"; std::ofstream ec_stream; - ec_stream.open("test_core_endcap_xy.svg"); + ec_stream.open(file_name); ec_stream << ec_file; ec_stream.close(); + + // Checksum test against reference + std::string test_name = + ::testing::UnitTest::GetInstance()->current_test_info()->name(); + std::uint32_t file_checksum = ec_file.checksum(); + EXPECT_TRUE(test::checksum(test_name, file_name, file_checksum)); } -TEST(endcap, x_y_view_grid) { +TEST(core, endcap_x_y_view_grid) { svg::file ec_file; ec_file._height = 800; @@ -167,7 +182,6 @@ TEST(endcap, x_y_view_grid) { } }; associations.push_back(sector_associations); - std::cout << std::endl; } // Build the connectors @@ -181,8 +195,15 @@ TEST(endcap, x_y_view_grid) { ec_file.add_objects(grid_sectors._sub_objects); // File output + std::string file_name = "test_core_endcap_grid_xy.svg"; std::ofstream ec_stream; - ec_stream.open("test_core_endcap_grid_xy.svg"); + ec_stream.open(file_name); ec_stream << ec_file; ec_stream.close(); + + // Checksum test against reference + std::string test_name = + ::testing::UnitTest::GetInstance()->current_test_info()->name(); + std::uint32_t file_checksum = ec_file.checksum(); + EXPECT_TRUE(test::checksum(test_name, file_name, file_checksum)); } diff --git a/tests/core/gradient.cpp b/tests/core/gradient.cpp index 99f14aa..def106c 100644 --- a/tests/core/gradient.cpp +++ b/tests/core/gradient.cpp @@ -12,6 +12,7 @@ #include #include "../common/playground.hpp" +#include "../common/test_checksum.hpp" #include "actsvg/core/draw.hpp" #include "actsvg/core/style.hpp" @@ -50,10 +51,17 @@ TEST(core, gradient_box_linear_x) { mfile.add_object(g); mfile.add_object(gbox); + std::string file_name = "test_core_gradient_box.svg"; std::ofstream tstream; - tstream.open("test_core_gradient_box.svg"); + tstream.open(file_name); tstream << mfile; tstream.close(); + + // Checksum test against reference + std::string test_name = + ::testing::UnitTest::GetInstance()->current_test_info()->name(); + std::uint32_t file_checksum = mfile.checksum(); + EXPECT_TRUE(test::checksum(test_name, file_name, file_checksum)); } TEST(core, gradient_box_vertical) { @@ -77,10 +85,17 @@ TEST(core, gradient_box_vertical) { mfile.add_object(pg); mfile.add_object(g); + std::string file_name = "test_core_gradient_box_vertical.svg"; std::ofstream tstream; - tstream.open("test_core_gradient_box_vertical.svg"); + tstream.open(file_name); tstream << mfile; tstream.close(); + + // Checksum test against reference + std::string test_name = + ::testing::UnitTest::GetInstance()->current_test_info()->name(); + std::uint32_t file_checksum = mfile.checksum(); + EXPECT_TRUE(test::checksum(test_name, file_name, file_checksum)); } TEST(core, gradient_box_vertical_label) { @@ -104,10 +119,17 @@ TEST(core, gradient_box_vertical_label) { mfile.add_object(pg); mfile.add_object(g); + std::string file_name = "test_core_gradient_box_vertical_label.svg"; std::ofstream tstream; - tstream.open("test_core_gradient_box_vertical_label.svg"); + tstream.open(file_name); tstream << mfile; tstream.close(); + + // Checksum test against reference + std::string test_name = + ::testing::UnitTest::GetInstance()->current_test_info()->name(); + std::uint32_t file_checksum = mfile.checksum(); + EXPECT_TRUE(test::checksum(test_name, file_name, file_checksum)); } TEST(core, gradient_box_horizontal) { @@ -131,10 +153,17 @@ TEST(core, gradient_box_horizontal) { mfile.add_object(pg); mfile.add_object(g); + std::string file_name = "test_core_gradient_box_horizontal.svg"; std::ofstream tstream; - tstream.open("test_core_gradient_box_horizontal.svg"); + tstream.open(file_name); tstream << mfile; tstream.close(); + + // Checksum test against reference + std::string test_name = + ::testing::UnitTest::GetInstance()->current_test_info()->name(); + std::uint32_t file_checksum = mfile.checksum(); + EXPECT_TRUE(test::checksum(test_name, file_name, file_checksum)); } TEST(core, gradient_box_horizontal_label) { @@ -159,8 +188,15 @@ TEST(core, gradient_box_horizontal_label) { mfile.add_object(pg); mfile.add_object(g); + std::string file_name = "test_core_gradient_box_horizontal_label.svg"; std::ofstream tstream; - tstream.open("test_core_gradient_box_horizontal_label.svg"); + tstream.open(file_name); tstream << mfile; tstream.close(); + + // Checksum test against reference + std::string test_name = + ::testing::UnitTest::GetInstance()->current_test_info()->name(); + std::uint32_t file_checksum = mfile.checksum(); + EXPECT_TRUE(test::checksum(test_name, file_name, file_checksum)); } diff --git a/tests/core/grids.cpp b/tests/core/grids.cpp index 5b3d8dc..771e797 100644 --- a/tests/core/grids.cpp +++ b/tests/core/grids.cpp @@ -12,20 +12,20 @@ #include #include "../common/playground.hpp" +#include "../common/test_checksum.hpp" #include "actsvg/core/draw.hpp" using namespace actsvg; TEST(core, cartesian_grid) { - svg::file ftemplate; - // Set a playground auto pg = test::playground({-400, -400}, {400, 400}); // Write out the file + std::string file_name = "test_core_cartesian_grid.svg"; std::ofstream fo; - fo.open("test_core_cartesian_grid.svg"); + fo.open(file_name); // A simple cartesian grid, non-tiled std::vector x_edges = {-100, -50, 0, 50, 100, 150}; @@ -38,30 +38,28 @@ TEST(core, cartesian_grid) { auto cartesian_grid = draw::cartesian_grid("nt_c_grid", x_edges, y_edges, dashed_red); - fo << ftemplate._html_head; - fo << ftemplate._svg_head; - fo << " width=\"900\" height=\"900\" viewBox=\"-450 -450 900 900\""; - fo << ftemplate._svg_def_end; - // Add the playground - fo << pg; - // Add the grid - fo << cartesian_grid; - // Close the file - fo << ftemplate._svg_tail; - fo << ftemplate._html_tail; + svg::file grid_file; + grid_file.add_object(pg); + grid_file.add_object(cartesian_grid); + fo << grid_file; fo.close(); + + // Checksum test against reference + std::string test_name = + ::testing::UnitTest::GetInstance()->current_test_info()->name(); + std::uint32_t file_checksum = grid_file.checksum(); + EXPECT_TRUE(test::checksum(test_name, file_name, file_checksum)); } TEST(core, tiled_cartesian_grid) { - svg::file ftemplate; - // Set a playground auto pg = test::playground({-400, -400}, {400, 400}); // Write out the file + std::string file_name = "test_core_tiled_cartesian_grid.svg"; std::ofstream fo; - fo.open("test_core_tiled_cartesian_grid.svg"); + fo.open(file_name); // A simple cartesian grid, non-tiled std::vector x_edges = {-100, -50, 0, 50, 100, 150}; @@ -80,30 +78,28 @@ TEST(core, tiled_cartesian_grid) { auto cartesian_grid = draw::tiled_cartesian_grid( "cartesian_grid", x_edges, y_edges, blue_tile, dashed_red); - fo << ftemplate._html_head; - fo << ftemplate._svg_head; - fo << " width=\"900\" height=\"900\" viewBox=\"-450 -450 900 900\""; - fo << ftemplate._svg_def_end; - // Add the playground - fo << pg; - // Add the grid - fo << cartesian_grid; - // Close the file - fo << ftemplate._svg_tail; - fo << ftemplate._html_tail; + svg::file grid_file; + grid_file.add_object(pg); + grid_file.add_object(cartesian_grid); + fo << grid_file; fo.close(); + + // Checksum test against reference + std::string test_name = + ::testing::UnitTest::GetInstance()->current_test_info()->name(); + std::uint32_t file_checksum = grid_file.checksum(); + EXPECT_TRUE(test::checksum(test_name, file_name, file_checksum)); } TEST(core, fan_grid) { - svg::file ftemplate; - // Set a playground auto pg = test::playground({-400, -400}, {400, 400}); // Write out the file + std::string file_name = "test_core_fan_grid.svg"; std::ofstream fo; - fo.open("test_core_fan_grid.svg"); + fo.open(file_name); // A simple cartesian grid, non-tiled std::vector x_edges_low = {-100, -50, 0, 50, 100}; @@ -117,30 +113,28 @@ TEST(core, fan_grid) { auto fan_grid = draw::fan_grid("fan_grid", x_edges_low, x_edges_high, y_edges, dashed_red); - fo << ftemplate._html_head; - fo << ftemplate._svg_head; - fo << " width=\"900\" height=\"900\" viewBox=\"-450 -450 900 900\""; - fo << ftemplate._svg_def_end; - // Add the playground - fo << pg; - // Add the grid - fo << fan_grid; - // Close the file - fo << ftemplate._svg_tail; - fo << ftemplate._html_tail; + svg::file grid_file; + grid_file.add_object(pg); + grid_file.add_object(fan_grid); + fo << grid_file; fo.close(); + + // Checksum test against reference + std::string test_name = + ::testing::UnitTest::GetInstance()->current_test_info()->name(); + std::uint32_t file_checksum = grid_file.checksum(); + EXPECT_TRUE(test::checksum(test_name, file_name, file_checksum)); } TEST(core, tiled_fan_grid) { - svg::file ftemplate; - // Set a playground auto pg = test::playground({-400, -400}, {400, 400}); // Write out the file + std::string file_name = "test_core_tiled_fan_grid.svg"; std::ofstream fo; - fo.open("test_core_tiled_fan_grid.svg"); + fo.open(file_name); // A simple cartesian grid, non-tiled std::vector x_edges_low = {-100, -50, 0, 50, 100}; @@ -178,30 +172,28 @@ TEST(core, tiled_fan_grid) { std::sort(tiles_test.begin(), tiles_test.end()); ASSERT_TRUE(tiles_test == tiles_reference); - fo << ftemplate._html_head; - fo << ftemplate._svg_head; - fo << " width=\"900\" height=\"900\" viewBox=\"-450 -450 900 900\""; - fo << ftemplate._svg_def_end; - // Add the playground - fo << pg; - // Add the grid - fo << fan_grid; - // Close the file - fo << ftemplate._svg_tail; - fo << ftemplate._html_tail; + svg::file grid_file; + grid_file.add_object(pg); + grid_file.add_object(fan_grid); + fo << grid_file; fo.close(); + + // Checksum test against reference + std::string test_name = + ::testing::UnitTest::GetInstance()->current_test_info()->name(); + std::uint32_t file_checksum = grid_file.checksum(); + EXPECT_TRUE(test::checksum(test_name, file_name, file_checksum)); } TEST(core, polar_grid) { - svg::file ftemplate; - // Set a playground auto pg = test::playground({-400, -400}, {400, 400}); // Write out the file + std::string file_name = "test_core_polar_grid.svg"; std::ofstream fo; - fo.open("test_core_polar_grid.svg"); + fo.open(file_name); // A simple cartesian grid, non-tiled std::vector r_edges_low = {50, 75, 100, 125, 150, 175}; @@ -215,30 +207,28 @@ TEST(core, polar_grid) { auto polar_grid = draw::polar_grid("polar_grid", r_edges_low, phi_edges, dashed_red); - fo << ftemplate._html_head; - fo << ftemplate._svg_head; - fo << " width=\"900\" height=\"900\" viewBox=\"-450 -450 900 900\""; - fo << ftemplate._svg_def_end; - // Add the playground - fo << pg; - // Add the grid - fo << polar_grid; - // Close the file - fo << ftemplate._svg_tail; - fo << ftemplate._html_tail; + svg::file grid_file; + grid_file.add_object(pg); + grid_file.add_object(polar_grid); + fo << grid_file; fo.close(); + + // Checksum test against reference + std::string test_name = + ::testing::UnitTest::GetInstance()->current_test_info()->name(); + std::uint32_t file_checksum = grid_file.checksum(); + EXPECT_TRUE(test::checksum(test_name, file_name, file_checksum)); } TEST(core, tiled_polar_grid) { - svg::file ftemplate; - // Set a playground auto pg = test::playground({-400, -400}, {400, 400}); // Write out the file + std::string file_name = "test_core_tiled_polar_grid.svg"; std::ofstream fo; - fo.open("test_core_tiled_polar_grid.svg"); + fo.open(file_name); // A simple cartesian grid, non-tiled std::vector r_edges_low = {50, 75, 100, 125, 150, 175}; @@ -258,16 +248,16 @@ TEST(core, tiled_polar_grid) { auto polar_grid = draw::tiled_polar_grid( "polar_grid", r_edges_low, phi_edges, blue_opaque, dashed_red); - fo << ftemplate._html_head; - fo << ftemplate._svg_head; - fo << " width=\"900\" height=\"900\" viewBox=\"-450 -450 900 900\""; - fo << ftemplate._svg_def_end; - // Add the playground - fo << pg; - // Add the grid - fo << polar_grid; - // Close the file - fo << ftemplate._svg_tail; - fo << ftemplate._html_tail; + svg::file grid_file; + grid_file.add_object(pg); + grid_file.add_object(polar_grid); + // Write and close the file + fo << grid_file; fo.close(); + + // Checksum test against reference + std::string test_name = + ::testing::UnitTest::GetInstance()->current_test_info()->name(); + std::uint32_t file_checksum = grid_file.checksum(); + EXPECT_TRUE(test::checksum(test_name, file_name, file_checksum)); } diff --git a/tests/core/infobox.cpp b/tests/core/infobox.cpp index b254b10..927d876 100644 --- a/tests/core/infobox.cpp +++ b/tests/core/infobox.cpp @@ -13,6 +13,7 @@ #include #include "../common/playground.hpp" +#include "../common/test_checksum.hpp" #include "actsvg/core.hpp" using namespace actsvg; @@ -57,8 +58,15 @@ TEST(core, info_box) { ifile.add_object(t); ifile.add_object(info_box); + std::string file_name = "test_core_infobox.svg"; std::ofstream tstream; - tstream.open("test_core_infobox.svg"); + tstream.open(file_name); tstream << ifile; tstream.close(); + + // Checksum test against reference + std::string test_name = + ::testing::UnitTest::GetInstance()->current_test_info()->name(); + std::uint32_t file_checksum = ifile.checksum(); + EXPECT_TRUE(test::checksum(test_name, file_name, file_checksum)); } diff --git a/tests/core/label.cpp b/tests/core/label.cpp index 41bada2..7ee1b8d 100644 --- a/tests/core/label.cpp +++ b/tests/core/label.cpp @@ -12,6 +12,7 @@ #include #include "../common/playground.hpp" +#include "../common/test_checksum.hpp" #include "actsvg/core/draw.hpp" using namespace actsvg; @@ -19,13 +20,14 @@ using namespace actsvg; namespace { // Helper function void test_label(style::label::horizontal h, style::label::vertical v, - const std::string& name) { + const std::string& name, const std::string& test_name) { // Set a playground auto pg = test::playground({-400, -400}, {400, 400}); // Write out the file std::ofstream fo; - fo.open(std::string("test_core_label_" + name + ".svg").c_str()); + std::string file_name = "test_core_label_" + name + ".svg"; + fo.open(file_name); auto box = draw::rectangle("box", {200, 100}, 150, 20, style::fill{style::color{{255, 0, 0}}}); @@ -39,40 +41,59 @@ void test_label(style::label::horizontal h, style::label::vertical v, of.add_object(draw::label("label", l)); fo << of; fo.close(); + + // Checksum test against reference + std::uint32_t file_checksum = of.checksum(); + EXPECT_TRUE(test::checksum(test_name, file_name, file_checksum)); } } // namespace TEST(core, label_left_bottom) { + std::string test_name = + ::testing::UnitTest::GetInstance()->current_test_info()->name(); + test_label(style::label::horizontal::left, style::label::vertical::bottom, - "left_bottom"); + "left_bottom", test_name); } TEST(core, label_right_bottom) { + std::string test_name = + ::testing::UnitTest::GetInstance()->current_test_info()->name(); test_label(style::label::horizontal::right, style::label::vertical::bottom, - "right_bottom"); + "right_bottom", test_name); } TEST(core, label_left_top) { + std::string test_name = + ::testing::UnitTest::GetInstance()->current_test_info()->name(); test_label(style::label::horizontal::left, style::label::vertical::top, - "left_top"); + "left_top", test_name); } TEST(core, label_right_top) { + std::string test_name = + ::testing::UnitTest::GetInstance()->current_test_info()->name(); test_label(style::label::horizontal::right, style::label::vertical::top, - "right_top"); + "right_top", test_name); } TEST(core, label_center_center) { + std::string test_name = + ::testing::UnitTest::GetInstance()->current_test_info()->name(); test_label(style::label::horizontal::center, style::label::vertical::center, - "center_center"); + "center_center", test_name); } TEST(core, label_left_center) { + std::string test_name = + ::testing::UnitTest::GetInstance()->current_test_info()->name(); test_label(style::label::horizontal::left, style::label::vertical::center, - "left_center"); + "left_center", test_name); } TEST(core, label_right_center) { + std::string test_name = + ::testing::UnitTest::GetInstance()->current_test_info()->name(); test_label(style::label::horizontal::right, style::label::vertical::center, - "right_center"); + "right_center", test_name); } diff --git a/tests/core/line.cpp b/tests/core/line.cpp index 0d235e9..e2602d5 100644 --- a/tests/core/line.cpp +++ b/tests/core/line.cpp @@ -12,6 +12,7 @@ #include #include "../common/playground.hpp" +#include "../common/test_checksum.hpp" #include "actsvg/core/draw.hpp" using namespace actsvg; @@ -29,83 +30,84 @@ TEST(core, line) { TEST(core, line_plain) { - svg::file ftemplate; - // Set a playground auto pg = test::playground({-400, -400}, {400, 400}); // Write out the file + std::string file_name = "test_core_line.svg"; std::ofstream fo; - fo.open("test_core_line.svg"); - - fo << ftemplate._html_head; - fo << ftemplate._svg_head; - fo << " width=\"900\" height=\"900\" viewBox=\"-450 -450 900 900\""; - fo << ftemplate._svg_def_end; - // Add the playground - fo << pg; - // Add the line - fo << draw::line("l", {40, -20.}, {80., 100.}, - style::stroke{style::color{{255, 0, 0}}, 2}); - // Close the file - fo << ftemplate._svg_tail; - fo << ftemplate._html_tail; + fo.open(file_name); + + auto line = draw::line("l", {4, -2.}, {8., 10.}, + style::stroke{style::color{{0, 0, 255}}, 2}); + + svg::file line_file; + line_file.add_object(pg); + line_file.add_object(line); + + fo << line_file; fo.close(); + + // Checksum test against reference + std::string test_name = + ::testing::UnitTest::GetInstance()->current_test_info()->name(); + std::uint32_t file_checksum = line_file.checksum(); + EXPECT_TRUE(test::checksum(test_name, file_name, file_checksum)); } TEST(core, line_shifted) { - svg::file ftemplate; - // Set a playground auto pg = test::playground({-400, -400}, {400, 400}); // Write out the file + std::string file_name = "test_core_line_shifted.svg"; std::ofstream fo; - fo.open("test_core_line_shifted.svg"); + fo.open(file_name); style::transform t{{100, 100}}; - - fo << ftemplate._html_head; - fo << ftemplate._svg_head; - fo << " width=\"900\" height=\"900\" viewBox=\"-450 -450 900 900\""; - fo << ftemplate._svg_def_end; - // Add the playground - fo << pg; // Add the line - fo << draw::line("l", {40, -20.}, {80., 100.}, - style::stroke{style::color{{0, 255, 0}}, 2}, t); + auto line = draw::line("l", {4, -2.}, {8., 10.}, + style::stroke{style::color{{0, 0, 255}}, 2}, t); + svg::file line_file; + + line_file.add_object(pg); + line_file.add_object(line); // Close the file - fo << ftemplate._svg_tail; - fo << ftemplate._html_tail; + fo << line_file; fo.close(); + + // Checksum test against reference + std::string test_name = + ::testing::UnitTest::GetInstance()->current_test_info()->name(); + std::uint32_t file_checksum = line_file.checksum(); + EXPECT_TRUE(test::checksum(test_name, file_name, file_checksum)); } TEST(core, line_scaled) { - svg::file ftemplate; - // Set a playground auto pg = test::playground({-400, -400}, {400, 400}); // Write out the file + std::string file_name = "test_core_line_scaled.svg"; std::ofstream fo; - fo.open("test_core_line_scaled.svg"); + fo.open(file_name); style::transform t; t._scale = {10, 10}; - - fo << ftemplate._html_head; - fo << ftemplate._svg_head; - fo << " width=\"900\" height=\"900\" viewBox=\"-450 -450 900 900\""; - fo << ftemplate._svg_def_end; - // Add the playground - fo << pg; // Add the line - fo << draw::line("l", {4, -2.}, {8., 10.}, - style::stroke{style::color{{0, 0, 255}}, 2}, t); - // Close the file - fo << ftemplate._svg_tail; - fo << ftemplate._html_tail; + auto line = draw::line("l", {4, -2.}, {8., 10.}, + style::stroke{style::color{{0, 0, 255}}, 2}, t); + svg::file line_file; + line_file.add_object(pg); + line_file.add_object(line); + fo << line_file; fo.close(); + + // Checksum test against reference + std::string test_name = + ::testing::UnitTest::GetInstance()->current_test_info()->name(); + std::uint32_t file_checksum = line_file.checksum(); + EXPECT_TRUE(test::checksum(test_name, file_name, file_checksum)); } diff --git a/tests/core/markers.cpp b/tests/core/markers.cpp index d98b4aa..2d7620e 100644 --- a/tests/core/markers.cpp +++ b/tests/core/markers.cpp @@ -11,11 +11,12 @@ #include #include "../common/playground.hpp" +#include "../common/test_checksum.hpp" #include "actsvg/core.hpp" using namespace actsvg; -TEST(draw, markers) { +TEST(core, markers) { // Set a playground auto pg = test::playground({-400, -400}, {400, 400}); @@ -118,8 +119,15 @@ TEST(draw, markers) { mfile.add_object(m5_f_g); mfile.add_object(m5_b_g); + std::string file_name = "test_core_markers.svg"; std::ofstream tstream; - tstream.open("test_core_markers.svg"); + tstream.open(file_name); tstream << mfile; tstream.close(); + + // Checksum test against reference + std::string test_name = + ::testing::UnitTest::GetInstance()->current_test_info()->name(); + std::uint32_t file_checksum = mfile.checksum(); + EXPECT_TRUE(test::checksum(test_name, file_name, file_checksum)); } diff --git a/tests/core/measures.cpp b/tests/core/measures.cpp index 9b99eb2..1729f2b 100644 --- a/tests/core/measures.cpp +++ b/tests/core/measures.cpp @@ -11,11 +11,12 @@ #include #include "../common/playground.hpp" +#include "../common/test_checksum.hpp" #include "actsvg/core.hpp" using namespace actsvg; -TEST(draw, measure) { +TEST(core, measure) { // Set a playground auto pg = test::playground({-400, -400}, {400, 400}); @@ -65,8 +66,15 @@ TEST(draw, measure) { mfile.add_object(m_arc); mfile.add_object(m_arc2); + std::string file_name = "test_core_measures.svg"; std::ofstream tstream; - tstream.open("test_core_measures.svg"); + tstream.open(file_name); tstream << mfile; tstream.close(); + + // Checksum test against reference + std::string test_name = + ::testing::UnitTest::GetInstance()->current_test_info()->name(); + std::uint32_t file_checksum = mfile.checksum(); + EXPECT_TRUE(test::checksum(test_name, file_name, file_checksum)); } diff --git a/tests/core/polygon.cpp b/tests/core/polygon.cpp index a8ea694..166c402 100644 --- a/tests/core/polygon.cpp +++ b/tests/core/polygon.cpp @@ -11,11 +11,12 @@ #include #include "../common/playground.hpp" +#include "../common/test_checksum.hpp" #include "actsvg/core.hpp" using namespace actsvg; -TEST(draw, triangle) { +TEST(core, triangle) { // Set a playground auto pg = test::playground({-400, -400}, {400, 400}); @@ -29,13 +30,20 @@ TEST(draw, triangle) { tfile0._objects.push_back(pg); tfile0._objects.push_back(tsvg0); + std::string file_name = "test_core_triangle.svg"; std::ofstream tstream; - tstream.open("test_core_triangle.svg"); + tstream.open(file_name); tstream << tfile0; tstream.close(); + + // Checksum test against reference + std::string test_name = + ::testing::UnitTest::GetInstance()->current_test_info()->name(); + std::uint32_t file_checksum = tfile0.checksum(); + EXPECT_TRUE(test::checksum(test_name, file_name, file_checksum)); } -TEST(draw, triangle_shifted) { +TEST(core, triangle_shifted) { // Set a playground auto pg = test::playground({-400, -400}, {400, 400}); @@ -51,13 +59,20 @@ TEST(draw, triangle_shifted) { tfile0._objects.push_back(pg); tfile0._objects.push_back(tsvg0); + std::string file_name = "test_core_triangle_shifted.svg"; std::ofstream tstream; - tstream.open("test_core_triangle_shifted.svg"); + tstream.open(file_name); tstream << tfile0; tstream.close(); + + // Checksum test against reference + std::string test_name = + ::testing::UnitTest::GetInstance()->current_test_info()->name(); + std::uint32_t file_checksum = tfile0.checksum(); + EXPECT_TRUE(test::checksum(test_name, file_name, file_checksum)); } -TEST(draw, triangle_rotated) { +TEST(core, triangle_rotated) { // Set a playground auto pg = test::playground({-400, -400}, {400, 400}); @@ -73,13 +88,20 @@ TEST(draw, triangle_rotated) { tfile0._objects.push_back(pg); tfile0._objects.push_back(tsvg0); + std::string file_name = "test_core_triangle_rotated.svg"; std::ofstream tstream; - tstream.open("test_core_triangle_rotated.svg"); + tstream.open(file_name); tstream << tfile0; tstream.close(); + + // Checksum test against reference + std::string test_name = + ::testing::UnitTest::GetInstance()->current_test_info()->name(); + std::uint32_t file_checksum = tfile0.checksum(); + EXPECT_TRUE(test::checksum(test_name, file_name, file_checksum)); } -TEST(draw, triangle_rotated_shifted) { +TEST(core, triangle_rotated_shifted) { // Set a playground auto pg = test::playground({-400, -400}, {400, 400}); @@ -96,13 +118,20 @@ TEST(draw, triangle_rotated_shifted) { tfile0._objects.push_back(pg); tfile0._objects.push_back(tsvg0); + std::string file_name = "test_core_triangle_rotated_shifted.svg"; std::ofstream tstream; - tstream.open("test_core_triangle_rotated_shifted.svg"); + tstream.open(file_name); tstream << tfile0; tstream.close(); + + // Checksum test against reference + std::string test_name = + ::testing::UnitTest::GetInstance()->current_test_info()->name(); + std::uint32_t file_checksum = tfile0.checksum(); + EXPECT_TRUE(test::checksum(test_name, file_name, file_checksum)); } -TEST(draw, triangle_scaled) { +TEST(core, triangle_scaled) { // Set a playground auto pg = test::playground({-400, -400}, {400, 400}); @@ -118,13 +147,20 @@ TEST(draw, triangle_scaled) { tfile0._objects.push_back(pg); tfile0._objects.push_back(tsvg0); + std::string file_name = "test_core_triangle_scaled.svg"; std::ofstream tstream; - tstream.open("test_core_triangle_scaled.svg"); + tstream.open(file_name); tstream << tfile0; tstream.close(); + + // Checksum test against reference + std::string test_name = + ::testing::UnitTest::GetInstance()->current_test_info()->name(); + std::uint32_t file_checksum = tfile0.checksum(); + EXPECT_TRUE(test::checksum(test_name, file_name, file_checksum)); } -TEST(draw, triangle_highligh) { +TEST(core, triangle_highligh) { // Set a playground auto pg = test::playground({-400, -400}, {400, 400}); @@ -141,13 +177,20 @@ TEST(draw, triangle_highligh) { tfile1.add_object(pg); tfile1.add_object(tsvg1); + std::string file_name = "test_core_triangle_highlight.svg"; std::ofstream tstream; - tstream.open("test_core_triangle_highlight.svg"); + tstream.open(file_name); tstream << tfile1; tstream.close(); + + // Checksum test against reference + std::string test_name = + ::testing::UnitTest::GetInstance()->current_test_info()->name(); + std::uint32_t file_checksum = tfile1.checksum(); + EXPECT_TRUE(test::checksum(test_name, file_name, file_checksum)); } -TEST(draw, disc_sector) { +TEST(core, disc_sector) { // Set a playground auto pg = test::playground({-400, -400}, {400, 400}); @@ -180,8 +223,15 @@ TEST(draw, disc_sector) { tfile1.add_object(red_sector); tfile1.add_object(green_sector); + std::string file_name = "test_core_sector.svg"; std::ofstream tstream; - tstream.open("test_core_sector.svg"); + tstream.open(file_name); tstream << tfile1; tstream.close(); + + // Checksum test against reference + std::string test_name = + ::testing::UnitTest::GetInstance()->current_test_info()->name(); + std::uint32_t file_checksum = tfile1.checksum(); + EXPECT_TRUE(test::checksum(test_name, file_name, file_checksum)); } diff --git a/tests/core/polyline.cpp b/tests/core/polyline.cpp index 03d1daf..44dd034 100644 --- a/tests/core/polyline.cpp +++ b/tests/core/polyline.cpp @@ -11,11 +11,12 @@ #include #include "../common/playground.hpp" +#include "../common/test_checksum.hpp" #include "actsvg/core.hpp" using namespace actsvg; -TEST(draw, polyline) { +TEST(core, polyline) { // Set a playground auto pg = test::playground({-400, -400}, {400, 400}); @@ -29,8 +30,15 @@ TEST(draw, polyline) { tfile._objects.push_back(pg); tfile._objects.push_back(pl); + std::string file_name = "test_core_polyline.svg"; std::ofstream tstream; - tstream.open("test_core_polyline.svg"); + tstream.open(file_name); tstream << tfile; tstream.close(); + + // Checksum test against reference + std::string test_name = + ::testing::UnitTest::GetInstance()->current_test_info()->name(); + std::uint32_t file_checksum = tfile.checksum(); + EXPECT_TRUE(test::checksum(test_name, file_name, file_checksum)); } diff --git a/tests/core/rectangle.cpp b/tests/core/rectangle.cpp index 4231fee..bfa67cd 100644 --- a/tests/core/rectangle.cpp +++ b/tests/core/rectangle.cpp @@ -11,11 +11,12 @@ #include #include "../common/playground.hpp" +#include "../common/test_checksum.hpp" #include "actsvg/core.hpp" using namespace actsvg; -TEST(draw, rectangle) { +TEST(core, rectangle) { // Set a playground auto pg = test::playground({-400, -400}, {400, 400}); @@ -37,8 +38,15 @@ TEST(draw, rectangle) { tfile0._objects.push_back(r0); tfile0._objects.push_back(r1); + std::string file_name = "test_core_rectangle.svg"; std::ofstream tstream; - tstream.open("test_core_rectangle.svg"); + tstream.open(file_name); tstream << tfile0; tstream.close(); + + // Checksum test against reference + std::string test_name = + ::testing::UnitTest::GetInstance()->current_test_info()->name(); + std::uint32_t file_checksum = tfile0.checksum(); + EXPECT_TRUE(test::checksum(test_name, file_name, file_checksum)); } diff --git a/tests/core/style.cpp b/tests/core/style.cpp index 3971f45..03fb429 100644 --- a/tests/core/style.cpp +++ b/tests/core/style.cpp @@ -10,15 +10,11 @@ #include -#include -#include -#include - #include "actsvg/core/svg.hpp" using namespace actsvg; -TEST(svg, fill_style) { +TEST(core, fill_style) { svg::object colored{"red_object"}; @@ -26,8 +22,6 @@ TEST(svg, fill_style) { style::fill red_fill{red}; colored._fill = red_fill; - std::cout << colored << std::endl; - svg::object highlight{"red_highlight_object"}; style::color red_hl{{255, 0, 0}}; @@ -35,11 +29,9 @@ TEST(svg, fill_style) { red_hl._highlight = {"mouseover", "mouseout"}; style::fill red_hl_fill{red_hl}; highlight._fill = red_hl_fill; - - std::cout << highlight << std::endl; } -TEST(svg, stroke_style) { +TEST(core, stroke_style) { svg::object stroked{"stroked_object"}; @@ -47,28 +39,23 @@ TEST(svg, stroke_style) { style::stroke black_stroke{black, 1, {3, 1, 3}}; stroked._stroke = black_stroke; - std::cout << stroked << std::endl; } -TEST(svg, transform) { +TEST(core, transform) { svg::object translated{"translated"}; style::transform t0{{1., 2., 0.}}; translated._transform = t0; - std::cout << translated << std::endl; svg::object rotated{"rotated"}; style::transform t1{{0., 0., 1.}}; rotated._transform = t1; - std::cout << rotated << std::endl; svg::object translated_rotated{"translated_rotated"}; style::transform t2{{3., 2., 1.}}; translated_rotated._transform = t2; - std::cout << translated_rotated << std::endl; svg::object translated_rotated_scaled{"translated_rotated_scaled"}; style::transform t3{{3., 2., 1.}}; t3._scale = {100., 1.}; translated_rotated_scaled._transform = t3; - std::cout << translated_rotated_scaled << std::endl; } diff --git a/tests/core/svg.cpp b/tests/core/svg.cpp index 56b2578..93d13f1 100644 --- a/tests/core/svg.cpp +++ b/tests/core/svg.cpp @@ -16,17 +16,20 @@ using namespace actsvg; -TEST(svg, empty_object) { +TEST(core, empty_object) { svg::object empty{"empty"}; std::stringstream ss; } -TEST(svg, file_set_view_box) { +TEST(core, file_set_view_box_and_checksum) { svg::file file; file.set_view_box({0, 0, 100, 100}); std::stringstream ss; ss << file; std::string svg = ss.str(); ASSERT_TRUE(svg.find("viewBox=\"0 0 100 100\"") != std::string::npos); + + std::size_t checksum = file.checksum(); + ASSERT_EQ(checksum, 3187404258u); } diff --git a/tests/core/text.cpp b/tests/core/text.cpp index 7b6fab3..9af4bf5 100644 --- a/tests/core/text.cpp +++ b/tests/core/text.cpp @@ -13,27 +13,24 @@ #include #include "../common/playground.hpp" +#include "../common/test_checksum.hpp" #include "actsvg/core.hpp" using namespace actsvg; -TEST(text, unconnected_text) { - - svg::file ftemplate; +TEST(core, unconnected_text) { // Set a playground auto pg = test::playground({-400, -400}, {400, 400}); // Write out the file + std::string file_name = "test_core_text.svg"; std::ofstream fo; - fo.open("test_core_text.svg"); + fo.open(file_name); - fo << ftemplate._html_head; - fo << ftemplate._svg_head; - fo << " width=\"900\" height=\"900\" viewBox=\"-450 -450 900 900\""; - fo << ftemplate._svg_def_end; // Add the playground - fo << pg; + svg::file text_file; + text_file.add_object(pg); style::color red{{255, 0, 0}}; style::font fs; @@ -41,7 +38,8 @@ TEST(text, unconnected_text) { fs._fc = red; // Add the text - fo << draw::text("t0", {10, 10}, {"Arial test text at (10,10)"}, fs); + auto text = draw::text("t0", {10, 10}, {"Arial test text at (10,10)"}, fs); + text_file.add_object(text); style::color blue{{0, 0, 255}}; style::font fsb; @@ -49,17 +47,20 @@ TEST(text, unconnected_text) { fsb._size = 20; fsb._fc = blue; - fo << draw::text("t1", {20, 80}, {"Bigger Times text"}, fsb); + text = draw::text("t1", {20, 80}, {"Bigger Times text"}, fsb); + text_file.add_object(text); - // Close the file - fo << ftemplate._svg_tail; - fo << ftemplate._html_tail; + fo << text_file; fo.close(); -} -TEST(text, multiline_text) { + // Checksum test against reference + std::string test_name = + ::testing::UnitTest::GetInstance()->current_test_info()->name(); + std::uint32_t file_checksum = text_file.checksum(); + EXPECT_TRUE(test::checksum(test_name, file_name, file_checksum)); +} - svg::file ftemplate; +TEST(core, multiline_text) { // Set a playground auto pg = test::playground({-400, -400}, {400, 400}); @@ -70,27 +71,28 @@ TEST(text, multiline_text) { fs._fc = red; // Write out the file + std::string file_name = "test_core_text_multiline.svg"; std::ofstream fo; - fo.open("test_core_text_multiline.svg"); + fo.open(file_name); - fo << ftemplate._html_head; - fo << ftemplate._svg_head; - fo << " width=\"900\" height=\"900\" viewBox=\"-450 -450 900 900\""; - fo << ftemplate._svg_def_end; - // Add the playground - fo << pg; + svg::file text_file; + text_file.add_object(pg); // Add the text - fo << draw::text("t0", {100, 100}, {"line 0", "line 1"}, fs); + auto text = draw::text("t0", {100, 100}, {"line 0", "line 1"}, fs); + text_file.add_object(text); // Close the file - fo << ftemplate._svg_tail; - fo << ftemplate._html_tail; + fo << text_file; fo.close(); -} -TEST(text, multiline_text_outside_playground) { + // Checksum test against reference + std::string test_name = + ::testing::UnitTest::GetInstance()->current_test_info()->name(); + std::uint32_t file_checksum = text_file.checksum(); + EXPECT_TRUE(test::checksum(test_name, file_name, file_checksum)); +} - svg::file ftemplate; +TEST(core, multiline_text_outside_playground) { svg::object pgo = svg::object::create_group("playground"); @@ -104,8 +106,11 @@ TEST(text, multiline_text_outside_playground) { fs._fc = red; // Write out the file, it should adapt the range for the outside text + std::string file_name = "test_core_text_multiline_range.svg"; std::ofstream fo; - fo.open("test_core_text_multiline_range.svg"); + fo.open(file_name); + + svg::file text_file; // Add the text auto t0 = draw::text("t0", {500, 500}, {"line 0", "line 1", "line 2"}, fs); @@ -115,6 +120,13 @@ TEST(text, multiline_text_outside_playground) { pgo.add_object(t1); // Add to the file anf write out - ftemplate.add_object(pgo); - fo << ftemplate; + text_file.add_object(pgo); + fo << text_file; + fo.close(); + + // Checksum test against reference + std::string test_name = + ::testing::UnitTest::GetInstance()->current_test_info()->name(); + std::uint32_t file_checksum = text_file.checksum(); + EXPECT_TRUE(test::checksum(test_name, file_name, file_checksum)); } diff --git a/tests/core/views.cpp b/tests/core/views.cpp index cfbc081..74ccd73 100644 --- a/tests/core/views.cpp +++ b/tests/core/views.cpp @@ -19,7 +19,7 @@ using namespace actsvg; using point3 = std::array; -TEST(views, xy) { +TEST(core, xy) { std::vector p3s = {{1., 2., 0.}, {3., 4., 0.}, {7., 8., 0.}}; views::x_y x_y_view; @@ -31,7 +31,7 @@ TEST(views, xy) { ASSERT_TRUE(expected == c); } -TEST(views, zr) { +TEST(core, zr) { std::vector p3s = {{1., 0., 3.}, {0., 4., 0.}, {2., 2., 2.}}; views::z_r z_r_view; @@ -44,7 +44,7 @@ TEST(views, zr) { ASSERT_TRUE(expected == c); } -TEST(views, zphi) { +TEST(core, zphi) { std::vector p3s = {{1., 0., 3.}, {0., 4., 0.}, {2., 2., 2.}}; views::z_phi z_phi_view; @@ -56,7 +56,7 @@ TEST(views, zphi) { ASSERT_TRUE(expected == c); } -TEST(views, zrphi) { +TEST(core, zrphi) { std::vector p3s = {{1., 0., 3.}, {0., 4., 0.}, {2., 2., 2.}}; views::z_rphi z_rphi_view;