Skip to content

Commit

Permalink
tests: more tests for util.c
Browse files Browse the repository at this point in the history
tests for:
  test_parse_pid
  test_parse_uid
  test_safe_atolli
  test_safe_atod
  • Loading branch information
phomes committed Feb 20, 2013
1 parent 599391d commit 8d99e5f
Showing 1 changed file with 65 additions and 0 deletions.
65 changes: 65 additions & 0 deletions src/test/test-util.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();

Expand Down

0 comments on commit 8d99e5f

Please sign in to comment.