From 339a5921b8e7129ed1e3da08efd84f4a56eeede1 Mon Sep 17 00:00:00 2001 From: cyrgani Date: Sat, 22 Jun 2024 22:21:14 +0200 Subject: [PATCH 1/4] wrap context in ManuallyDrop to fix test UB --- src/lib.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 76c5d7e7..b68a0c06 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -40,6 +40,7 @@ use miniquad::*; use std::collections::{HashMap, HashSet}; use std::future::Future; +use std::mem::ManuallyDrop; use std::pin::Pin; mod exec; @@ -452,7 +453,7 @@ impl Context { } #[no_mangle] -static mut CONTEXT: Option = None; +static mut CONTEXT: ManuallyDrop> = ManuallyDrop::new(None); // This is required for #[macroquad::test] // @@ -843,7 +844,7 @@ impl Window { } let mut context = Context::new(); context.update_on = update_on.unwrap_or_default(); - unsafe { CONTEXT = Some(context) }; + unsafe { CONTEXT = ManuallyDrop::new(Some(context)) }; Box::new(Stage {}) }); From 8b513b13730bdeee4d961e545004998cb12e3fef Mon Sep 17 00:00:00 2001 From: cyrgani Date: Sat, 22 Jun 2024 22:22:06 +0200 Subject: [PATCH 2/4] add missing reference in doctest --- src/experimental/animation.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/experimental/animation.rs b/src/experimental/animation.rs index 815280eb..d3885a4c 100644 --- a/src/experimental/animation.rs +++ b/src/experimental/animation.rs @@ -40,7 +40,7 @@ //! clear_background(WHITE); //! // Now we can draw our character //! draw_texture_ex( -//! image, +//! &image, //! 10., //! 10., //! WHITE, From 91b7388b0c21393b398137030251cdbe0141a671 Mon Sep 17 00:00:00 2001 From: cyrgani Date: Thu, 11 Jul 2024 21:57:03 +0200 Subject: [PATCH 3/4] remove broken doc example since fixing it would be nontrivial --- src/text.rs | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/src/text.rs b/src/text.rs index 3f25161b..1df6fcd7 100644 --- a/src/text.rs +++ b/src/text.rs @@ -201,16 +201,6 @@ impl Font { /// Sets the [FilterMode](https://docs.rs/miniquad/latest/miniquad/graphics/enum.FilterMode.html#) of this font's texture atlas. /// /// Use Nearest if you need integer-ratio scaling for pixel art, for example. - /// - /// # Example - /// ``` - /// # use macroquad::prelude::*; - /// # #[macroquad::main("test")] - /// # async fn main() { - /// let font = Font::default(); - /// font.set_filter(FilterMode::Linear); - /// # } - /// ``` pub fn set_filter(&mut self, filter_mode: miniquad::FilterMode) { self.atlas.lock().unwrap().set_filter(filter_mode); } From b481f992a66bdc2de426750d9b8c7c0e2aa336fd Mon Sep 17 00:00:00 2001 From: cyrgani Date: Sun, 28 Jul 2024 23:38:58 +0200 Subject: [PATCH 4/4] put each test in an individual file --- src/lib.rs | 5 +- tests/coroutine_execution_order.rs | 14 +++++ tests/coroutine_manual_poll.rs | 42 ++++++++++++++ tests/coroutine_manual_poll_delay.rs | 33 +++++++++++ tests/coroutine_memory.rs | 17 ++++++ tests/coroutine_pause.rs | 87 ---------------------------- tests/coroutine_values.rs | 22 +------ 7 files changed, 110 insertions(+), 110 deletions(-) create mode 100644 tests/coroutine_execution_order.rs create mode 100644 tests/coroutine_manual_poll.rs create mode 100644 tests/coroutine_manual_poll_delay.rs create mode 100644 tests/coroutine_memory.rs delete mode 100644 tests/coroutine_pause.rs diff --git a/src/lib.rs b/src/lib.rs index b68a0c06..76c5d7e7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -40,7 +40,6 @@ use miniquad::*; use std::collections::{HashMap, HashSet}; use std::future::Future; -use std::mem::ManuallyDrop; use std::pin::Pin; mod exec; @@ -453,7 +452,7 @@ impl Context { } #[no_mangle] -static mut CONTEXT: ManuallyDrop> = ManuallyDrop::new(None); +static mut CONTEXT: Option = None; // This is required for #[macroquad::test] // @@ -844,7 +843,7 @@ impl Window { } let mut context = Context::new(); context.update_on = update_on.unwrap_or_default(); - unsafe { CONTEXT = ManuallyDrop::new(Some(context)) }; + unsafe { CONTEXT = Some(context) }; Box::new(Stage {}) }); diff --git a/tests/coroutine_execution_order.rs b/tests/coroutine_execution_order.rs new file mode 100644 index 00000000..8c6b0c2e --- /dev/null +++ b/tests/coroutine_execution_order.rs @@ -0,0 +1,14 @@ +use macroquad::{experimental::coroutines::start_coroutine, window::next_frame}; + +#[macroquad::test] +async fn coroutine_execution_order() { + start_coroutine(async move { + println!("a"); + next_frame().await; + println!("b"); + }); + println!("c"); + next_frame().await; + println!("d"); + next_frame().await; +} diff --git a/tests/coroutine_manual_poll.rs b/tests/coroutine_manual_poll.rs new file mode 100644 index 00000000..c9efbfa8 --- /dev/null +++ b/tests/coroutine_manual_poll.rs @@ -0,0 +1,42 @@ +use macroquad::{ + experimental::{coroutines::start_coroutine, scene}, + window::next_frame, +}; + +#[macroquad::test] +async fn coroutine_manual_poll() { + struct Player { + state: i32, + } + impl scene::Node for Player {} + + let player = scene::add_node(Player { state: 0 }); + + let mut coroutine = start_coroutine(async move { + loop { + scene::get_node(player).state += 1; + next_frame().await; + } + }); + + // make sure that coroutine is not yet polled + assert_eq!(scene::get_node(player).state, 0); + + coroutine.set_manual_poll(); + + // still not polled + assert_eq!(scene::get_node(player).state, 0); + + coroutine.poll(0.1); + assert_eq!(scene::get_node(player).state, 1); + + next_frame().await; + next_frame().await; + + // make sure that after main loop's next_frame coroutine was not polled + assert_eq!(scene::get_node(player).state, 1); + + // and that we still can poll + coroutine.poll(0.1); + assert_eq!(scene::get_node(player).state, 2); +} diff --git a/tests/coroutine_manual_poll_delay.rs b/tests/coroutine_manual_poll_delay.rs new file mode 100644 index 00000000..a3190936 --- /dev/null +++ b/tests/coroutine_manual_poll_delay.rs @@ -0,0 +1,33 @@ +use macroquad::experimental::{ + coroutines::{start_coroutine, wait_seconds}, + scene, +}; + +#[macroquad::test] +async fn coroutine_manual_poll_delay() { + struct Player { + state: i32, + } + impl scene::Node for Player {} + + let player = scene::add_node(Player { state: 0 }); + + let mut coroutine = start_coroutine(async move { + wait_seconds(1.).await; + scene::get_node(player).state = 1; + }); + + coroutine.set_manual_poll(); + + assert_eq!(scene::get_node(player).state, 0); + + // not 1 second yet, coroutine will have "now": 0.0, "delta": 0.9, (0.0 + 0.9) < 1.0 + coroutine.poll(0.9); + + assert_eq!(scene::get_node(player).state, 0); + + // coroutine will have "now": 0.1, delta: 0.11, (0.9 + 0.11) > 1.0, wait_for_seconds pass + coroutine.poll(0.11); + + assert_eq!(scene::get_node(player).state, 1); +} diff --git a/tests/coroutine_memory.rs b/tests/coroutine_memory.rs new file mode 100644 index 00000000..e8844dce --- /dev/null +++ b/tests/coroutine_memory.rs @@ -0,0 +1,17 @@ +use macroquad::{experimental::coroutines::start_coroutine, telemetry, window::next_frame}; + +#[macroquad::test] +async fn coroutine_memory() { + for _ in 0..20 { + start_coroutine(async move { + next_frame().await; + }); + + next_frame().await; + } + + // wait for the last one to finish + next_frame().await; + + assert_eq!(telemetry::active_coroutines_count(), 0); +} diff --git a/tests/coroutine_pause.rs b/tests/coroutine_pause.rs deleted file mode 100644 index 23aefb3d..00000000 --- a/tests/coroutine_pause.rs +++ /dev/null @@ -1,87 +0,0 @@ -use macroquad::{ - experimental::{ - coroutines::{start_coroutine, wait_seconds}, - scene, - }, - window::next_frame, -}; - -#[macroquad::test] -async fn coroutine_execution_order() { - start_coroutine(async move { - println!("a"); - next_frame().await; - println!("b"); - }); - println!("c"); - next_frame().await; - println!("d"); - next_frame().await; -} - -#[macroquad::test] -async fn coroutine_manual_poll() { - struct Player { - state: i32, - } - impl scene::Node for Player {} - - let player = scene::add_node(Player { state: 0 }); - - let mut coroutine = start_coroutine(async move { - loop { - scene::get_node(player).state += 1; - next_frame().await; - } - }); - - // make sure that coroutine is not yet polled - assert_eq!(scene::get_node(player).state, 0); - - coroutine.set_manual_poll(); - - // still not polled - assert_eq!(scene::get_node(player).state, 0); - - coroutine.poll(0.1); - assert_eq!(scene::get_node(player).state, 1); - - next_frame().await; - next_frame().await; - - // make sure that after main loop's next_frame coroutine was not polled - assert_eq!(scene::get_node(player).state, 1); - - // and that we still can poll - coroutine.poll(0.1); - assert_eq!(scene::get_node(player).state, 2); -} - -#[macroquad::test] -async fn coroutine_manual_poll_delay() { - struct Player { - state: i32, - } - impl scene::Node for Player {} - - let player = scene::add_node(Player { state: 0 }); - - let mut coroutine = start_coroutine(async move { - wait_seconds(1.).await; - scene::get_node(player).state = 1; - }); - - coroutine.set_manual_poll(); - - assert_eq!(scene::get_node(player).state, 0); - - // not 1 second yet, coroutine will have "now": 0.0, "delta": 0.9, (0.0 + 0.9) < 1.0 - coroutine.poll(0.9); - - assert_eq!(scene::get_node(player).state, 0); - - // coroutine will have "now": 0.1, delta: 0.11, (0.9 + 0.11) > 1.0, wait_for_seconds pass - coroutine.poll(0.11); - - assert_eq!(scene::get_node(player).state, 1); -} diff --git a/tests/coroutine_values.rs b/tests/coroutine_values.rs index 34005c1d..55dc0bf1 100644 --- a/tests/coroutine_values.rs +++ b/tests/coroutine_values.rs @@ -1,7 +1,7 @@ -use macroquad::{experimental::coroutines::start_coroutine, telemetry, window::next_frame}; +use macroquad::{experimental::coroutines::start_coroutine, window::next_frame}; #[macroquad::test] -async fn coroutine_value() { +async fn coroutine_values() { let mut coroutine = start_coroutine(async move { next_frame().await; 1 @@ -16,21 +16,3 @@ async fn coroutine_value() { assert_eq!(coroutine.retrieve(), Some(1)); } - -#[macroquad::test] -async fn coroutine_memory() { - use macroquad::prelude::*; - - for _ in 0..20 { - start_coroutine(async move { - next_frame().await; - }); - - next_frame().await; - } - - // wait for the last one to finish - next_frame().await; - - assert_eq!(telemetry::active_coroutines_count(), 0); -}