From dbdea2a4a6e2a9e7f3feabade189dc78d1938628 Mon Sep 17 00:00:00 2001 From: Kim Woelders Date: Sat, 4 Dec 2021 08:23:11 +0100 Subject: test: Add context test --- test/Makefile.am | 4 ++ test/test_context.cpp | 144 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 148 insertions(+) create mode 100644 test/test_context.cpp (limited to 'test') diff --git a/test/Makefile.am b/test/Makefile.am index ac0f419..fcbd305 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -7,6 +7,7 @@ CLEANFILES = file.c img_save-*.* GTEST_LIBS = -lgtest -lstdc++ GTESTS = test_file + GTESTS += test_context GTESTS += test_load GTESTS += test_load_2 GTESTS += test_save @@ -35,6 +36,9 @@ test_file_SOURCES = test_file.cpp nodist_test_file_SOURCES = file.c test_file_LDADD = $(LIBS) +test_context_SOURCES = test_context.cpp +test_context_LDADD = $(LIBS) + test_load_SOURCES = test_load.cpp test_load_LDADD = $(LIBS) diff --git a/test/test_context.cpp b/test/test_context.cpp new file mode 100644 index 0000000..d89a90b --- /dev/null +++ b/test/test_context.cpp @@ -0,0 +1,144 @@ +#include + +#include +#include + +#define NULC ((Imlib_Context*)0) + +#define HAVE_INITIAL_CTX 0 + +int debug = 0; + +#define D(...) if (debug) printf(__VA_ARGS__) +#define D2(...) if (debug > 1) printf(__VA_ARGS__) +#if 0 +EAPI Imlib_Context imlib_context_new(void); +EAPI void imlib_context_free(Imlib_Context context); + +EAPI void imlib_context_push(Imlib_Context context); +EAPI void imlib_context_pop(void); +EAPI Imlib_Context imlib_context_get(void); +#endif + +TEST(CTX, ctx) +{ + Imlib_Context ctx, ctx0, ctx1; + + // Part 1 + + ctx = imlib_context_get(); + D("%d: ctx = %p\n", __LINE__, ctx); +#if !HAVE_INITIAL_CTX + EXPECT_EQ(ctx, NULC); + + // Delete NULL should just complain + imlib_context_free(ctx); +#else + EXPECT_TRUE(ctx); + + // Delete initial should just be ignored + imlib_context_free(ctx); +#endif + + // Part 2 + +#if !HAVE_INITIAL_CTX + ctx0 = imlib_context_new(); // Create initial context + D("%d: ctx0 = %p\n", __LINE__, ctx0); + EXPECT_TRUE(ctx0); + // * NB! After first call to imlib_context_push() we cannot ged rid of + // the first context + imlib_context_push(ctx0); + + ctx = imlib_context_get(); + D("%d: ctx = %p\n", __LINE__, ctx); + EXPECT_EQ(ctx, ctx0); // Ctx is initial +#else + ctx0 = imlib_context_get(); + D("%d: ctx0 = %p\n", __LINE__, ctx0); + EXPECT_TRUE(ctx0); +#endif + + // Attempt to pop first (should not succeed) + imlib_context_pop(); + ctx = imlib_context_get(); + D("%d: ctx = %p\n", __LINE__, ctx); + EXPECT_EQ(ctx, ctx0); // Ctx is still initial + + // Attempt to free first (should not succeed) + imlib_context_free(ctx); + ctx = imlib_context_get(); + D("%d: ctx = %p\n", __LINE__, ctx); + EXPECT_EQ(ctx, ctx0); // Ctx is still initial + + // Part 3 + + ctx1 = imlib_context_new(); + D("%d: ctx1 = %p\n", __LINE__, ctx1); + EXPECT_TRUE(ctx1); + EXPECT_NE(ctx0, ctx1); + + ctx = imlib_context_get(); + D("%d: ctx = %p\n", __LINE__, ctx); + EXPECT_EQ(ctx, ctx0); // Ctx still default + + imlib_context_push(ctx1); + ctx = imlib_context_get(); + D("%d: ctx = %p\n", __LINE__, ctx); + EXPECT_EQ(ctx, ctx1); // Ctx now new + + // Attempt to free new current (should not succeed) + imlib_context_free(ctx1); + ctx = imlib_context_get(); + D("%d: ctx = %p\n", __LINE__, ctx); + EXPECT_EQ(ctx, ctx1); // Ctx still new + + imlib_context_pop(); + ctx = imlib_context_get(); + D("%d: ctx = %p\n", __LINE__, ctx); + EXPECT_EQ(ctx, ctx0); // Ctx now default + +#if 0 + // No! - The previous delete set ->dirty which will have caused + // imlib_context_pop() above to free it. + // Free new context, now unused + imlib_context_free(ctx1); + ctx = imlib_context_get(); + D("%d: ctx = %p\n", __LINE__, ctx); + EXPECT_EQ(ctx, ctx0); // Ctx still default +#endif + + imlib_context_pop(); + ctx = imlib_context_get(); + D("%d: ctx = %p\n", __LINE__, ctx); + EXPECT_EQ(ctx, ctx0); // Ctx still default + + // Free initial context (should not succeed) + imlib_context_free(ctx0); + ctx = imlib_context_get(); + D("%d: ctx = %p\n", __LINE__, ctx); + EXPECT_EQ(ctx, ctx0); // Ctx still default +} + +int +main(int argc, char **argv) +{ + const char *s; + + ::testing::InitGoogleTest(&argc, argv); + + for (argc--, argv++; argc > 0; argc--, argv++) + { + s = argv[0]; + if (*s++ != '-') + break; + switch (*s) + { + case 'd': + debug++; + break; + } + } + + return RUN_ALL_TESTS(); +} -- cgit v1.2.1