[systemd-commits] src/test

Thomas H.P. Andersen phomes at kemper.freedesktop.org
Wed Feb 20 13:37:09 PST 2013


 src/test/test-util.c |   65 +++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 65 insertions(+)

New commits:
commit 8d99e5f53057f490b93d2ea9cb2b669bbee21d00
Author: Thomas Hindoe Paaboel Andersen <phomes at gmail.com>
Date:   Wed Feb 20 22:34:06 2013 +0100

    tests: more tests for util.c
    
    tests for:
      test_parse_pid
      test_parse_uid
      test_safe_atolli
      test_safe_atod

diff --git a/src/test/test-util.c b/src/test/test-util.c
index 9ba984c..e4dea09 100644
--- a/src/test/test-util.c
+++ b/src/test/test-util.c
@@ -68,6 +68,67 @@ static void test_parse_boolean(void) {
         assert_se(parse_boolean("") < 0);
 }
 
+static void test_parse_pid(void) {
+        int r;
+        pid_t pid;
+
+        r = parse_pid("100", &pid);
+        assert_se(r == 0);
+        assert_se(pid == 100);
+
+        r = parse_pid("0x7FFFFFFF", &pid);
+        assert_se(r == 0);
+        assert_se(pid == 2147483647);
+
+        pid = 65; /* pid is left unchanged on ERANGE. Set to known arbitrary value. */
+        r = parse_pid("0", &pid);
+        assert_se(r == -ERANGE);
+        assert_se(pid == 65);
+
+        pid = 65; /* pid is left unchanged on ERANGE. Set to known arbitrary value. */
+        r = parse_pid("-100", &pid);
+        assert_se(r == -ERANGE);
+        assert_se(pid == 65);
+
+        pid = 65; /* pid is left unchanged on ERANGE. Set to known arbitrary value. */
+        r = parse_pid("0xFFFFFFFFFFFFFFFFF", &pid);
+        assert(r == -ERANGE);
+        assert_se(pid == 65);
+}
+
+static void test_parse_uid(void) {
+        int r;
+        uid_t uid;
+
+        r = parse_uid("100", &uid);
+        assert_se(r == 0);
+        assert_se(uid == 100);
+}
+
+static void test_safe_atolli(void) {
+        int r;
+        long long l;
+
+        r = safe_atolli("12345", &l);
+        assert_se(r == 0);
+        assert_se(l == 12345);
+
+        r = safe_atolli("junk", &l);
+        assert_se(r == -EINVAL);
+}
+
+static void test_safe_atod(void) {
+        int r;
+        double d;
+
+        r = safe_atod("0.2244", &d);
+        assert_se(r == 0);
+        assert_se(abs(d - 0.2244) < 0.000001);
+
+        r = safe_atod("junk", &d);
+        assert_se(r == -EINVAL);
+}
+
 static void test_foreach_word_quoted(void) {
         char *w, *state;
         size_t l;
@@ -115,6 +176,10 @@ int main(int argc, char *argv[]) {
         test_first_word();
         test_parse_boolean();
         test_default_term_for_tty();
+        test_parse_pid();
+        test_parse_uid();
+        test_safe_atolli();
+        test_safe_atod();
         test_foreach_word_quoted();
         test_memdup_multiply();
 



More information about the systemd-commits mailing list