lib: Add _get_float() wpsettings API

- also add the corresponding C & Lua tests.
This commit is contained in:
Ashok Sidipotu 2022-05-09 12:43:36 +05:30 committed by Julian Bouzas
parent b76baca520
commit 3a25bc14d2
6 changed files with 105 additions and 4 deletions

View file

@ -151,6 +151,32 @@ wp_settings_get_int (WpSettings *self, const gchar *setting,
return true;
}
/*!
* \brief Gets the float value of a setting
* \ingroup wpsettings
* \param self the settings object
* \param setting name of the setting
* \param val (out): the float value of the setting
* \returns TRUE if the setting is defined, FALSE otherwise
*/
gboolean
wp_settings_get_float (WpSettings *self, const gchar *setting,
gfloat *val)
{
g_return_val_if_fail (self, false);
g_return_val_if_fail (setting, false);
if (!(wp_object_get_active_features (WP_OBJECT (self)) &
WP_OBJECT_FEATURES_ALL))
return false;
if (!wp_properties_get (self->settings, setting))
return false;
spa_atof (wp_properties_get (self->settings, setting), val);
return true;
}
/*!
* \brief Applies the rules and returns the applied properties.
*

View file

@ -42,10 +42,15 @@ gboolean wp_settings_get_boolean (WpSettings *self, const gchar *setting,
WP_API
gboolean wp_settings_get_string (WpSettings *self, const gchar *setting,
const char **value);
WP_API
gboolean wp_settings_get_int (WpSettings *self, const gchar *setting,
gint64 *val);
WP_API
gboolean wp_settings_get_float (WpSettings *self, const gchar *setting,
gfloat *val);
WP_API
gboolean wp_settings_apply_rule (WpSettings *self, const gchar *rule,
WpProperties *client_props, WpProperties *applied_props);

View file

@ -1544,6 +1544,30 @@ get_int (lua_State *L)
return 1;
}
static gboolean
get_float (lua_State *L)
{
const char *setting = luaL_checkstring (L, 1);
const char *m = NULL;
if (lua_type (L, 2) == LUA_TSTRING)
m = luaL_checkstring (L, 2);
g_autoptr (WpSettings) s = wp_settings_get_instance (get_wp_core (L), m);
if (s)
{
gfloat value = 0;
if (wp_settings_get_float (s, setting, &value))
lua_pushnumber (L, value);
else
lua_pushnil (L);
}
else
lua_pushnil (L);
return 1;
}
static gboolean
apply_rule (lua_State *L)
{
@ -1574,6 +1598,7 @@ static const luaL_Reg settings_methods[] = {
{ "apply_rule", apply_rule },
{ "get_string", get_string },
{ "get_int", get_int },
{ "get_float", get_float },
{ NULL, NULL }
};

View file

@ -112,7 +112,7 @@ test_parsing_setup (TestSettingsFixture *self, gconstpointer user_data)
self->settings = g_steal_pointer (&settings);
/* total no.of settings in the conf file */
g_assert_cmpint (data.count, ==, 11);
g_assert_cmpint (data.count, ==, 13);
}
}
@ -129,7 +129,7 @@ static void
test_parsing (TestSettingsFixture *self, gconstpointer data)
{
/* total no.of settings in the conf file */
g_assert_cmpint (wp_properties_get_count(self->settings), ==, 11);
g_assert_cmpint (wp_properties_get_count(self->settings), ==, 13);
}
static void
@ -277,6 +277,7 @@ test_wpsettings (TestSettingsFixture *self, gconstpointer data)
g_assert_true (wp_settings_get_int (s, "test-property4-min-int", &value));
g_assert_cmpint (value, ==, G_MININT64);
value = 0;
g_assert_true (wp_settings_get_int (s, "test-property4-max-int-one-more",
&value));
g_assert_cmpint (value, ==, 0);
@ -305,6 +306,21 @@ test_wpsettings (TestSettingsFixture *self, gconstpointer data)
g_assert_cmpstr (value, ==, "-20");
}
{
gfloat value = 0.0;
/* _get_float () */
g_assert_false (wp_settings_get_float (s, "test-property-undefined",
&value));
g_assert_true (wp_settings_get_float (s, "test-property-float1",
&value));
g_assert_cmpfloat_with_epsilon (value, 3.14, 0.001);
g_assert_true (wp_settings_get_float (s, "test-property-float2",
&value));
g_assert_cmpfloat_with_epsilon (value, 0.4, 0.001);
}
/* test the wp_settings_get_instance () API */
{
g_autoptr (WpSettings) s1 =

View file

@ -129,6 +129,8 @@ wireplumber.settings = {
}
}
]
test-property-float1 = "3.14"
test-property-float2 = "0.4"
rule_three = [
{
matches = [
@ -136,6 +138,9 @@ wireplumber.settings = {
test.string6 = "~metal*"
test.table.entry = true
}
{
test-string6-wildcard = "~*"
}
]
actions = {
update-props = {
@ -161,7 +166,5 @@ wireplumber.settings = {
}
}
}
]
}

View file

@ -7,6 +7,7 @@ local value = Settings.get_boolean ("test-property1", "test-settings")
assert (value == false)
value = Settings.get_boolean ("test-property2", "test-settings")
assert ("boolean" == type (value))
assert (value == true)
value = Settings.get_boolean ("test-property1")
@ -22,6 +23,7 @@ value = Settings.get_int ("test-property-undefined", "test-settings")
assert (value == nil)
value = Settings.get_int ("test-property3-int", "test-settings")
assert ("number" == type (value))
assert (value == -20)
value = Settings.get_int ("test-property4-max-int", "test-settings")
@ -43,11 +45,24 @@ value = Settings.get_string ("test-property-undefined", "test-settings")
assert (value == nil)
value = Settings.get_string ("test-property4-string", "test-settings")
assert ("string" == type (value))
assert (value == "blahblah")
value = Settings.get_string ("test-property3-int", "test-settings")
assert (value == "-20")
-- test settings _get_float ()
value = Settings.get_float ("test-property-undefined", "test-settings")
assert (value == nil)
value = Settings.get_float ("test-property-float1", "test-settings")
assert ("number" == type (value))
assert ((value - 3.14) < 0.00001)
value = Settings.get_float ("test-property-float2", "test-settings")
assert ((value - 0.4) < 0.00001)
-- test rules
-- test #1
local cp = {
@ -147,3 +162,14 @@ assert (ap["prop.electrical.conductivity"] == "false")
assert (ap["prop.state"] == "gas")
assert (ap["prop.example"] == "neon")
-- test #9
local cp = {
["test-string6-wildcard"] = "wild_cat",
}
local applied, ap = Settings.apply_rule ("rule_three", cp, "test-settings")
assert (applied == true)
assert (ap["prop.electrical.conductivity"] == "true")
assert (ap["prop.state"] == "solid")
assert (ap["prop.example"] == "ferrous")