From b9d2c542a0b1f030d0aa80ccedf1c82e0019ba51 Mon Sep 17 00:00:00 2001 From: terencehill Date: Sat, 18 Mar 2023 00:30:42 +0100 Subject: [PATCH] Add some automated tests of intrusive lists when they change during a loop (run tests with sv_cmd runtest) --- qcsrc/lib/intrusivelist.qh | 57 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/qcsrc/lib/intrusivelist.qh b/qcsrc/lib/intrusivelist.qh index eabce8e71..938a7f963 100644 --- a/qcsrc/lib/intrusivelist.qh +++ b/qcsrc/lib/intrusivelist.qh @@ -1,6 +1,7 @@ #pragma once #include "iter.qh" +#include "test.qh" /** * Maximum amount of creatable lists. @@ -282,3 +283,59 @@ void ONREMOVE(entity this) } } } + + +#define IL_TEST_BUILD() s = il_test_build(il_test, ent1, ent2, ent3, ent4, ent5) + +string il_test_build(entity il_test, entity ent1, entity ent2, entity ent3, entity ent4, entity ent5) +{ + IL_CLEAR(il_test); + IL_PUSH(il_test, ent1); + IL_PUSH(il_test, ent2); + IL_PUSH(il_test, ent3); + IL_PUSH(il_test, ent4); + IL_PUSH(il_test, ent5); + return ""; +} + +TEST(intrusivelist, ModificationsWhileLooping) +{ + IntrusiveList il_test = IL_NEW(); + entity ent1 = new(1), ent2 = new(2), ent3 = new(3), ent4 = new(4), ent5 = new(5); + string s; + + IL_TEST_BUILD(); + IL_EACH(il_test, true, + { + s = strcat(s, it.classname); + if (it == ent2) IL_REMOVE(il_test, ent3); + if (it == ent4) IL_PUSH(il_test, ent3); + }); + EXPECT_TRUE(s == "12453"); + + IL_TEST_BUILD(); + IL_EACH(il_test, true, + { + s = strcat(s, it.classname); + if (it == ent2) IL_REMOVE(il_test, ent2); + if (it == ent3) IL_REMOVE(il_test, ent3); + if (it == ent3) IL_REMOVE(il_test, ent4); + if (it == ent5) IL_POP(il_test); + }); + EXPECT_TRUE(s == "1235"); + + IL_TEST_BUILD(); + IL_REMOVE(il_test, ent5); + IL_EACH(il_test, true, + { + s = strcat(s, it.classname); + if (it == ent1) IL_SHIFT(il_test); + if (it == ent4) IL_POP(il_test); + }); + EXPECT_TRUE(s == "1234"); + + IL_DELETE(il_test); + delete(ent1); delete(ent2); delete(ent3); delete(ent4); delete(ent5); + + SUCCEED(); +} -- 2.39.2