Mesa (master): util/xmlconfig: Add a unit test of the code.

GitLab Mirror gitlab-mirror at kemper.freedesktop.org
Fri Sep 25 19:51:24 UTC 2020


Module: Mesa
Branch: master
Commit: 1eb79dfadedb8b77cdc55a6a11f859c1e53521d4
URL:    http://cgit.freedesktop.org/mesa/mesa/commit/?id=1eb79dfadedb8b77cdc55a6a11f859c1e53521d4

Author: Eric Anholt <eric at anholt.net>
Date:   Thu Sep 10 15:13:03 2020 -0700

util/xmlconfig: Add a unit test of the code.

I want to build a non-XML-based alternative for Android, and to do that I
want to know that my equivalent code still works.

Reviewed-by: Kristian H. Kristensen <hoegsberg at google.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/6753>

---

 src/util/driconf.h           |   8 +--
 src/util/meson.build         |  11 +++
 src/util/tests/xmlconfig.cpp | 158 +++++++++++++++++++++++++++++++++++++++++++
 src/util/xmlconfig.h         |   8 +++
 4 files changed, 181 insertions(+), 4 deletions(-)

diff --git a/src/util/driconf.h b/src/util/driconf.h
index 12bfadb1b36..6c13578eb0b 100644
--- a/src/util/driconf.h
+++ b/src/util/driconf.h
@@ -82,7 +82,7 @@
 
 /** \brief Begin an option definition with restrictions on valid values */
 #define DRI_CONF_OPT_BEGIN_V(name,type,def,valid) \
-"<option name=\""#name"\" type=\""#type"\" default=\""#def"\" valid=\""valid"\">\n"
+"<option name=\""#name"\" type=\""#type"\" default=\""#def"\" valid=\"" valid "\">\n"
 
 /** \brief End an option description */
 #define DRI_CONF_OPT_END \
@@ -90,11 +90,11 @@
 
 /** \brief A verbal description (empty version) */
 #define DRI_CONF_DESC(text) \
-"<description lang=\"en\" text=\""text"\"/>\n"
+"<description lang=\"en\" text=\"" text "\"/>\n"
 
 /** \brief Begining of a verbal description */
 #define DRI_CONF_DESC_BEGIN(text) \
-"<description lang=\"en\" text=\""text"\">\n"
+"<description lang=\"en\" text=\"" text "\">\n"
 
 /** \brief End a description */
 #define DRI_CONF_DESC_END \
@@ -102,7 +102,7 @@
 
 /** \brief A verbal description of an enum value */
 #define DRI_CONF_ENUM(value,text) \
-"<enum value=\""#value"\" text=\""text"\"/>\n"
+"<enum value=\""#value"\" text=\"" text "\"/>\n"
 
 
 /**
diff --git a/src/util/meson.build b/src/util/meson.build
index 67f7bd7b832..02993eac86a 100644
--- a/src/util/meson.build
+++ b/src/util/meson.build
@@ -219,6 +219,17 @@ idep_xmlconfig = declare_dependency(
 )
 
 if with_tests
+  if host_machine.system() != 'windows'
+    test('xmlconfig',
+      executable('xmlconfig_test',
+        files('tests/xmlconfig.cpp'),
+        include_directories : [inc_include, inc_src],
+        dependencies : [idep_mesautil, idep_xmlconfig, idep_gtest],
+      ),
+      suite : ['util'],
+        )
+  endif
+
   test(
     'u_atomic',
     executable(
diff --git a/src/util/tests/xmlconfig.cpp b/src/util/tests/xmlconfig.cpp
new file mode 100644
index 00000000000..a95a90a5f64
--- /dev/null
+++ b/src/util/tests/xmlconfig.cpp
@@ -0,0 +1,158 @@
+/*
+ * Copyright © 2020 Google LLC
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ */
+
+#include <gtest/gtest.h>
+#include <driconf.h>
+#include <xmlconfig.h>
+
+class xmlconfig_test : public ::testing::Test {
+protected:
+   xmlconfig_test();
+   ~xmlconfig_test();
+
+   driOptionCache options;
+
+   void driconf(const char *driconf);
+};
+
+xmlconfig_test::xmlconfig_test()
+{
+}
+
+xmlconfig_test::~xmlconfig_test()
+{
+   driDestroyOptionInfo(&options);
+}
+
+/* wraps a DRI_CONF_OPT_* in the required xml bits */
+#define DRI_CONF_TEST_OPT(x) \
+   DRI_CONF_BEGIN \
+   DRI_CONF_SECTION_MISCELLANEOUS \
+   x \
+   DRI_CONF_SECTION_END \
+   DRI_CONF_END
+
+void
+xmlconfig_test::driconf(const char *driconf)
+{
+   /* If your XML fails to parse, printing it here can help. */
+   /* printf("%s", driconf); */
+
+   driParseOptionInfo(&options, driconf);
+}
+
+TEST_F(xmlconfig_test, bools)
+{
+   driconf(DRI_CONF_TEST_OPT(
+              DRI_CONF_GLSL_ZERO_INIT("false")
+              DRI_CONF_ALWAYS_HAVE_DEPTH_BUFFER("true")));
+
+   EXPECT_EQ(driQueryOptionb(&options, "glsl_zero_init"), false);
+   EXPECT_EQ(driQueryOptionb(&options, "always_have_depth_buffer"), true);
+}
+
+TEST_F(xmlconfig_test, ints)
+{
+   driconf(DRI_CONF_TEST_OPT(
+              DRI_CONF_OPT_BEGIN_V(opt, int, 2, "0:999")
+              DRI_CONF_DESC("option")
+              DRI_CONF_OPT_END));
+
+   EXPECT_EQ(driQueryOptioni(&options, "opt"), 2);
+}
+
+TEST_F(xmlconfig_test, floats)
+{
+   driconf(DRI_CONF_TEST_OPT(
+              DRI_CONF_OPT_BEGIN_V(opt, float, 2.0, "1.0,2.0,3.0")
+              DRI_CONF_DESC("option")
+              DRI_CONF_OPT_END));
+
+   EXPECT_EQ(driQueryOptionf(&options, "opt"), 2.0);
+}
+
+TEST_F(xmlconfig_test, enums)
+{
+   driconf(DRI_CONF_TEST_OPT(
+              DRI_CONF_VBLANK_MODE(DRI_CONF_VBLANK_DEF_INTERVAL_1)));
+
+   EXPECT_EQ(driQueryOptioni(&options, "vblank_mode"), DRI_CONF_VBLANK_DEF_INTERVAL_1);
+}
+
+TEST_F(xmlconfig_test, string)
+{
+   driconf(DRI_CONF_TEST_OPT(
+              DRI_CONF_OPT_BEGIN(opt, string, value)
+              DRI_CONF_DESC("option")
+              DRI_CONF_OPT_END));
+
+   EXPECT_STREQ(driQueryOptionstr(&options, "opt"), "value");
+}
+
+TEST_F(xmlconfig_test, check_option)
+{
+   driconf(DRI_CONF_TEST_OPT(
+              DRI_CONF_GLSL_ZERO_INIT("true")
+              DRI_CONF_ALWAYS_HAVE_DEPTH_BUFFER("true")));
+
+   EXPECT_EQ(driCheckOption(&options, "glsl_zero_init", DRI_BOOL), true);
+
+   EXPECT_EQ(driCheckOption(&options, "glsl_zero_init", DRI_ENUM), false);
+   EXPECT_EQ(driCheckOption(&options, "glsl_zero_init", DRI_INT), false);
+   EXPECT_EQ(driCheckOption(&options, "glsl_zero_init", DRI_FLOAT), false);
+   EXPECT_EQ(driCheckOption(&options, "glsl_zero_init", DRI_STRING), false);
+
+   EXPECT_EQ(driCheckOption(&options, "not_present", DRI_BOOL), false);
+}
+
+TEST_F(xmlconfig_test, copy_cache)
+{
+   driconf(DRI_CONF_TEST_OPT(
+              DRI_CONF_OPT_BEGIN_B(mesa_b_option, "true")
+              DRI_CONF_OPT_END
+              DRI_CONF_OPT_BEGIN(mesa_s_option, string, value)
+              DRI_CONF_DESC("option")
+              DRI_CONF_OPT_END));
+
+   driOptionCache cache;
+
+   /* This tries to parse user config files.  We've called our option
+    * "mesa_test_option" so the test shouldn't end up with something from the
+    * user's homedir/environment that would override us.
+    */
+   driParseConfigFiles(&cache, &options,
+                       0, "driver", "drm",
+                       NULL, 0,
+                       NULL, 0);
+
+   /* Can we inspect the cache? */
+   EXPECT_EQ(driCheckOption(&cache, "mesa_b_option", DRI_BOOL), true);
+   EXPECT_EQ(driCheckOption(&cache, "mesa_s_option", DRI_STRING), true);
+   EXPECT_EQ(driCheckOption(&cache, "mesa_test_unknown_option", DRI_BOOL), false);
+
+   /* Did the value get copied? */
+   EXPECT_EQ(driQueryOptionb(&cache, "mesa_b_option"), true);
+   EXPECT_STREQ(driQueryOptionstr(&cache, "mesa_s_option"), "value");
+
+   driDestroyOptionCache(&cache);
+}
diff --git a/src/util/xmlconfig.h b/src/util/xmlconfig.h
index 3ecd5188634..213393b2f63 100644
--- a/src/util/xmlconfig.h
+++ b/src/util/xmlconfig.h
@@ -35,6 +35,10 @@
 #include <stdint.h>
 #include <string.h>
 
+#ifdef __cplusplus
+extern "C" {
+#endif
+
 #define STRING_CONF_MAXLEN 25
 
 /** \brief Option data types */
@@ -181,4 +185,8 @@ driComputeOptionsSha1(const driOptionCache *cache, unsigned char *sha1)
    ralloc_free(ctx);
 }
 
+#ifdef __cplusplus
+} /* extern C */
+#endif
+
 #endif



More information about the mesa-commit mailing list