chiark / gitweb /
util: add split_pair() for splitting foo=bar strings
[elogind.git] / src / test / test-util.c
index 4768310fbed20f1ef0a4a778cf2b928e6628d1ab..315bc419c29391ad0f8fbb1b7ca20bd5479e473e 100644 (file)
@@ -521,6 +521,28 @@ static void test_parse_user_at_host(void) {
         assert_se(streq(host, "mikescomputer"));
 }
 
+static void test_split_pair(void) {
+        _cleanup_free_ char *a = NULL, *b = NULL;
+
+        assert_se(split_pair("", "", &a, &b) == -EINVAL);
+        assert_se(split_pair("foo=bar", "", &a, &b) == -EINVAL);
+        assert_se(split_pair("", "=", &a, &b) == -EINVAL);
+        assert_se(split_pair("foo=bar", "=", &a, &b) >= 0);
+        assert_se(streq(a, "foo"));
+        assert_se(streq(b, "bar"));
+        free(a);
+        free(b);
+        assert_se(split_pair("==", "==", &a, &b) >= 0);
+        assert_se(streq(a, ""));
+        assert_se(streq(b, ""));
+        free(a);
+        free(b);
+
+        assert_se(split_pair("===", "==", &a, &b) >= 0);
+        assert_se(streq(a, ""));
+        assert_se(streq(b, "="));
+}
+
 int main(int argc, char *argv[]) {
         test_streq_ptr();
         test_first_word();
@@ -555,6 +577,7 @@ int main(int argc, char *argv[]) {
         test_strextend();
         test_strrep();
         test_parse_user_at_host();
+        test_split_pair();
 
         return 0;
 }