Browse Source

Unit tests: Automatically reset counters on shutdown

Michael Ragazzon 1 year ago
parent
commit
147357749f

+ 8 - 0
Tests/Source/Common/TestsInterface.cpp

@@ -178,3 +178,11 @@ void TestsRenderInterface::ReleaseShader(Rml::CompiledShaderHandle /*shader*/)
 {
 	counters.release_shader += 1;
 }
+void TestsRenderInterface::ResetCounters()
+{
+	counters_from_previous_reset = std::exchange(counters, Counters());
+}
+void TestsRenderInterface::Reset()
+{
+	ResetCounters();
+}

+ 6 - 2
Tests/Source/Common/TestsInterface.h

@@ -102,12 +102,16 @@ public:
 	void ReleaseShader(Rml::CompiledShaderHandle shader) override;
 
 	const Counters& GetCounters() const { return counters; }
-	void ResetCounters() { counters = {}; }
+	void ResetCounters();
+	const Counters& GetCountersFromPreviousReset() const { return counters_from_previous_reset; }
 
-	void Reset() { ResetCounters(); }
+	void Reset();
 
 private:
+	void VerifyMeshes();
+
 	Counters counters = {};
+	Counters counters_from_previous_reset = {};
 };
 
 #endif

+ 40 - 40
Tests/Source/Common/TestsShell.cpp

@@ -73,44 +73,43 @@ TestsRenderInterface shell_render_interface;
 
 static void InitializeShell(bool allow_debugger)
 {
+	REQUIRE(!shell_initialized);
+
 	// Initialize shell and create context.
-	if (!shell_initialized)
-	{
-		shell_initialized = true;
-		debugger_allowed = allow_debugger;
-		REQUIRE(Shell::Initialize());
+	shell_initialized = true;
+	debugger_allowed = allow_debugger;
+	REQUIRE(Shell::Initialize());
 
 #ifdef RMLUI_TESTS_USE_SHELL
-		// Initialize the backend and launch a window.
-		REQUIRE(Backend::Initialize("RmlUi Tests", window_size.x, window_size.y, true));
+	// Initialize the backend and launch a window.
+	REQUIRE(Backend::Initialize("RmlUi Tests", window_size.x, window_size.y, true));
 
-		// Use our custom tests system interface.
-		Rml::SetSystemInterface(&tests_system_interface);
-		// However, use the backend's render interface.
-		Rml::SetRenderInterface(Backend::GetRenderInterface());
+	// Use our custom tests system interface.
+	Rml::SetSystemInterface(&tests_system_interface);
+	// However, use the backend's render interface.
+	Rml::SetRenderInterface(Backend::GetRenderInterface());
 
-		REQUIRE(Rml::Initialise());
-		shell_context = Rml::CreateContext("main", window_size);
-		Shell::LoadFonts();
+	REQUIRE(Rml::Initialise());
+	shell_context = Rml::CreateContext("main", window_size);
+	Shell::LoadFonts();
 
-		if (allow_debugger)
-		{
-			Rml::Debugger::Initialise(shell_context);
-			num_documents_begin = shell_context->GetNumDocuments();
-		}
+	if (allow_debugger)
+	{
+		Rml::Debugger::Initialise(shell_context);
+		num_documents_begin = shell_context->GetNumDocuments();
+	}
 
-		shell_context->GetRootElement()->AddEventListener(Rml::EventId::Keydown, &shell_event_listener, true);
+	shell_context->GetRootElement()->AddEventListener(Rml::EventId::Keydown, &shell_event_listener, true);
 
 #else
-		// Set our custom system and render interfaces.
-		Rml::SetSystemInterface(&tests_system_interface);
-		Rml::SetRenderInterface(&shell_render_interface);
+	// Set our custom system and render interfaces.
+	Rml::SetSystemInterface(&tests_system_interface);
+	Rml::SetRenderInterface(&shell_render_interface);
 
-		REQUIRE(Rml::Initialise());
-		shell_context = Rml::CreateContext("main", window_size);
-		Shell::LoadFonts();
+	REQUIRE(Rml::Initialise());
+	shell_context = Rml::CreateContext("main", window_size);
+	Shell::LoadFonts();
 #endif
-	}
 }
 
 Rml::Context* TestsShell::GetContext(bool allow_debugger)
@@ -155,27 +154,28 @@ void TestsShell::RenderLoop()
 
 void TestsShell::ShutdownShell()
 {
-	if (shell_initialized)
+	REQUIRE(shell_initialized);
+
+	if (debugger_allowed)
 	{
-		if (debugger_allowed)
-		{
-			RMLUI_ASSERTMSG(shell_context->GetNumDocuments() == num_documents_begin, "Make sure all previously opened documents have been closed.");
-			(void)num_documents_begin;
-		}
+		RMLUI_ASSERTMSG(shell_context->GetNumDocuments() == num_documents_begin, "Make sure all previously opened documents have been closed.");
+		(void)num_documents_begin;
+	}
+
+	tests_system_interface.SetNumExpectedWarnings(0);
 
-		tests_system_interface.SetNumExpectedWarnings(0);
+	Rml::Shutdown();
 
-		Rml::Shutdown();
+	shell_render_interface.Reset();
 
 #ifdef RMLUI_TESTS_USE_SHELL
-		Backend::Shutdown();
+	Backend::Shutdown();
 #endif
 
-		Shell::Shutdown();
+	Shell::Shutdown();
 
-		shell_context = nullptr;
-		shell_initialized = false;
-	}
+	shell_context = nullptr;
+	shell_initialized = false;
 }
 
 void TestsShell::SetNumExpectedWarnings(int num_warnings)

+ 5 - 6
Tests/Source/UnitTests/Core.cpp

@@ -131,7 +131,6 @@ TEST_CASE("core.release_resources")
 	if (!render_interface)
 		return;
 
-	render_interface->Reset();
 	const auto& counters = render_interface->GetCounters();
 
 	Context* context = TestsShell::GetContext();
@@ -218,11 +217,12 @@ TEST_CASE("core.release_resources")
 
 	TestsShell::ShutdownShell();
 
-	// Finally, verify that all generated and loaded resources are released during shutdown.
-	CHECK(counters.generate_texture + counters.load_texture == counters.release_texture);
-	CHECK(counters.compile_geometry == counters.release_geometry);
+	// Counters are reset during shutdown.
+	const auto& counters_at_shutdown = render_interface->GetCountersFromPreviousReset();
 
-	render_interface->Reset();
+	// Finally, verify that all generated and loaded resources were released during shutdown.
+	CHECK(counters_at_shutdown.generate_texture + counters_at_shutdown.load_texture == counters_at_shutdown.release_texture);
+	CHECK(counters_at_shutdown.compile_geometry == counters_at_shutdown.release_geometry);
 }
 
 TEST_CASE("core.initialize")
@@ -253,5 +253,4 @@ TEST_CASE("core.initialize")
 	}
 
 	Rml::Shutdown();
-	render_interface->Reset();
 }

+ 0 - 2
Tests/Source/UnitTests/Filter.cpp

@@ -127,8 +127,6 @@ TEST_CASE("filter")
 	Context* context = TestsShell::GetContext();
 	REQUIRE(context);
 
-	TestsShell::GetTestsRenderInterface()->ResetCounters();
-
 	FilterTestInstancer instancer;
 	Rml::Factory::RegisterFilterInstancer("test", &instancer);