Exemplo n.º 1
0
/**
 * pk_package_id_new_from_string:
 * @package_id: the text to pre-fill the object
 *
 * Creates a new #PkPackageId object with values taken from the supplied id.
 *
 * Return value: a new #PkPackageId object
 **/
PkPackageId *
pk_package_id_new_from_string (const gchar *package_id)
{
	gchar **sections;
	PkPackageId *id = NULL;

	g_return_val_if_fail (package_id != NULL, NULL);

	sections = pk_strsplit (package_id, 4);
	if (sections == NULL) {
		return NULL;
	}

	/* create new object */
	id = pk_package_id_new ();
	if (!egg_strzero (sections[0]))
		id->name = g_strdup (sections[0]);
	if (!egg_strzero (sections[1]))
		id->version = g_strdup (sections[1]);
	if (!egg_strzero (sections[2]))
		id->arch = g_strdup (sections[2]);
	if (!egg_strzero (sections[3]))
		id->data = g_strdup (sections[3]);
	g_strfreev (sections);
	return id;
}
Exemplo n.º 2
0
void
egg_string_test (EggTest *test)
{
    gboolean ret;
    gchar *text_safe;
    const gchar *temp;
    guint length;
    gint value;
    guint uvalue;
    gchar **id1;
    gchar **id2;

    if (!egg_test_start (test, "EggString"))
        return;

    /************************************************************
     ****************    String array equal    ******************
     ************************************************************/
    egg_test_title (test, "egg_strvequal same argument");
    id1 = g_strsplit ("the quick brown fox", " ", 0);
    if (egg_strvequal (id1, id1))
        egg_test_success (test, NULL);
    else
        egg_test_failed (test, "incorrect ret when both same");
    g_strfreev (id1);

    /************************************************************/
    egg_test_title (test, "egg_strvequal same");
    id1 = g_strsplit ("the quick brown fox", " ", 0);
    id2 = g_strsplit ("the quick brown fox", " ", 0);
    if (egg_strvequal (id1, id2))
        egg_test_success (test, NULL);
    else
        egg_test_failed (test, "incorrect ret when both same");
    g_strfreev (id1);
    g_strfreev (id2);

    /************************************************************/
    egg_test_title (test, "egg_strvequal different lengths");
    id1 = g_strsplit ("the quick brown", " ", 0);
    id2 = g_strsplit ("the quick brown fox", " ", 0);
    if (!egg_strvequal (id1, id2))
        egg_test_success (test, NULL);
    else
        egg_test_failed (test, "incorrect ret when both same");
    g_strfreev (id1);
    g_strfreev (id2);

    /************************************************************/
    egg_test_title (test, "egg_strvequal different");
    id1 = g_strsplit ("the quick brown fox", " ", 0);
    id2 = g_strsplit ("richard hughes maintainer dude", " ", 0);
    if (!egg_strvequal (id1, id2))
        egg_test_success (test, NULL);
    else
        egg_test_failed (test, "same when different");
    g_strfreev (id1);
    g_strfreev (id2);

    /************************************************************
     ****************          Zero            ******************
     ************************************************************/
    temp = NULL;
    egg_test_title (test, "test strzero (null)");
    ret = egg_strzero (NULL);
    if (ret)
        egg_test_success (test, NULL);
    else
        egg_test_failed (test, "failed null");

    /************************************************************/
    egg_test_title (test, "test strzero (null first char)");
    ret = egg_strzero ("");
    if (ret)
        egg_test_success (test, NULL);
    else
        egg_test_failed (test, "failed null");

    /************************************************************/
    egg_test_title (test, "test strzero (long string)");
    ret = egg_strzero ("Richard");
    if (!ret)
        egg_test_success (test, NULL);
    else
        egg_test_failed (test, "zero length word!");

    /************************************************************/
    egg_test_title (test, "id strcmp pass");
    ret = (g_strcmp0 ("moo;0.0.1;i386;fedora", "moo;0.0.1;i386;fedora") == 0);
    egg_test_assert (test, ret);

    /************************************************************/
    egg_test_title (test, "id strcmp fail");
    ret = (g_strcmp0 ("moo;0.0.1;i386;fedora", "moo;0.0.2;i386;fedora") == 0);
    egg_test_assert (test, !ret);

    /************************************************************
     ****************          strlen          ******************
     ************************************************************/
    egg_test_title (test, "strlen bigger");
    length = egg_strlen ("123456789", 20);
    if (length == 9)
        egg_test_success (test, NULL);
    else
        egg_test_failed (test, "failed the strlen %i", length);

    /************************************************************/
    egg_test_title (test, "strlen smaller");
    length = egg_strlen ("123456789", 5);
    if (length == 5)
        egg_test_success (test, NULL);
    else
        egg_test_failed (test, "failed the strlen %i", length);

    /************************************************************/
    egg_test_title (test, "strlen correct");
    length = egg_strlen ("123456789", 9);
    if (length == 9)
        egg_test_success (test, NULL);
    else
        egg_test_failed (test, "failed the strlen %i", length);

    /************************************************************
     ****************         Replace          ******************
     ************************************************************/
    egg_test_title (test, "replace start");
    text_safe = egg_strreplace ("richard\nhughes", "r", "e");
    if (g_strcmp0 (text_safe, "eichaed\nhughes") == 0)
        egg_test_success (test, NULL);
    else
        egg_test_failed (test, "failed the replace '%s'", text_safe);
    g_free (text_safe);

    /************************************************************/
    egg_test_title (test, "replace none");
    text_safe = egg_strreplace ("richard\nhughes", "dave", "e");
    if (g_strcmp0 (text_safe, "richard\nhughes") == 0)
        egg_test_success (test, NULL);
    else
        egg_test_failed (test, "failed the replace '%s'", text_safe);
    g_free (text_safe);

    /************************************************************/
    egg_test_title (test, "replace end");
    text_safe = egg_strreplace ("richard\nhughes", "s", "e");
    if (g_strcmp0 (text_safe, "richard\nhughee") == 0)
        egg_test_success (test, NULL);
    else
        egg_test_failed (test, "failed the replace '%s'", text_safe);
    g_free (text_safe);

    /************************************************************/
    egg_test_title (test, "replace unicode");
    text_safe = egg_strreplace ("richard\n- hughes", "\n- ", "\n• ");
    if (g_strcmp0 (text_safe, "richard\n• hughes") == 0)
        egg_test_success (test, NULL);
    else
        egg_test_failed (test, "failed the replace '%s'", text_safe);
    g_free (text_safe);

    /************************************************************
     **************        Convert numbers       ****************
     ************************************************************/
    egg_test_title (test, "convert valid number");
    ret = egg_strtoint ("234", &value);
    if (ret && value == 234)
        egg_test_success (test, NULL);
    else
        egg_test_failed (test, "value is %i", value);

    /************************************************************/
    egg_test_title (test, "convert negative valid number");
    ret = egg_strtoint ("-234", &value);
    if (ret && value == -234)
        egg_test_success (test, NULL);
    else
        egg_test_failed (test, "value is %i", value);

    /************************************************************/
    egg_test_title (test, "don't convert invalid number");
    ret = egg_strtoint ("dave", &value);
    if (!ret)
        egg_test_success (test, NULL);
    else
        egg_test_failed (test, "value is %i", value);

    /************************************************************/
    egg_test_title (test, "convert NULL to a number");
    ret = egg_strtouint (NULL, &uvalue);
    if (!ret)
        egg_test_success (test, NULL);
    else
        egg_test_failed (test, "value is %i", uvalue);

    /************************************************************/
    egg_test_title (test, "convert valid uint number");
    ret = egg_strtouint ("234", &uvalue);
    if (ret && uvalue == 234)
        egg_test_success (test, NULL);
    else
        egg_test_failed (test, "value is %i", uvalue);

    /************************************************************/
    egg_test_title (test, "convert invalid uint number");
    ret = egg_strtouint ("-234", &uvalue);
    if (ret == FALSE)
        egg_test_success (test, NULL);
    else
        egg_test_failed (test, "value is %i", uvalue);

    egg_test_end (test);
}