diff options
author | Angelo Marletta <angelo.marletta@gmail.com> | 2012-06-16 03:24:54 +0100 |
---|---|---|
committer | Angelo Marletta <angelo.marletta@gmail.com> | 2012-06-16 03:24:54 +0100 |
commit | fe3e3c7e3be29d29b3b7efc22f4933c64e883776 (patch) | |
tree | 39857350c5a8206a96f4f7ff8278d9ab880c02fc /tests | |
parent | fe9833bae838296ec2e17161fe666faba769b101 (diff) | |
download | cpulimit-fe3e3c7e3be29d29b3b7efc22f4933c64e883776.tar.gz |
written two unit tests for process_iterator. tests pass
Diffstat (limited to 'tests')
-rwxr-xr-x | tests/process_iterator_test | bin | 24672 -> 0 bytes | |||
-rw-r--r-- | tests/process_iterator_test.c | 80 |
2 files changed, 73 insertions, 7 deletions
diff --git a/tests/process_iterator_test b/tests/process_iterator_test Binary files differdeleted file mode 100755 index f0204fd..0000000 --- a/tests/process_iterator_test +++ /dev/null diff --git a/tests/process_iterator_test.c b/tests/process_iterator_test.c index b8aec52..1822449 100644 --- a/tests/process_iterator_test.c +++ b/tests/process_iterator_test.c @@ -1,22 +1,88 @@ #include <stdio.h> #include <process_iterator.h> +#include <sys/types.h> +#include <unistd.h> +#include <assert.h> +#include <time.h> -int main() +int test_single_process() +{ + struct process_iterator it; + struct process process; + struct process_filter filter; + int count; + time_t now; + //don't iterate children + filter.pid = getpid(); + filter.include_children = 0; + count = 0; + now = time(NULL); + init_process_iterator(&it, &filter); + while (read_next_process(&it, &process) == 0) + { + assert(process.pid == getpid()); + assert(process.ppid == getppid()); + assert(process.cputime < 100); + assert(process.starttime == now || process.starttime == now - 1); + + count++; + } + assert(count == 1); + close_process_iterator(&it); + //iterate children + filter.pid = getpid(); + filter.include_children = 1; + count = 0; + now = time(NULL); + init_process_iterator(&it, &filter); + while (read_next_process(&it, &process) == 0) + { + assert(process.pid == getpid()); + assert(process.ppid == getppid()); + assert(process.cputime < 100); + assert(process.starttime == now || process.starttime == now - 1); + count++; + } + assert(count == 1); + close_process_iterator(&it); + return 0; +} + +int test_multiple_process() { struct process_iterator it; struct process process; struct process_filter filter; - filter.pid = 2981; + int child = fork(); + if (child == 0) + { + //child code + sleep(1); + return 0; + } + filter.pid = getpid(); filter.include_children = 1; init_process_iterator(&it, &filter); + int count = 0; + time_t now = time(NULL); while (read_next_process(&it, &process) == 0) { - printf("Read process %d\n", process.pid); - printf("Parent %d\n", process.ppid); - printf("Starttime %d\n", process.starttime); - printf("CPU time %d\n", process.cputime); - printf("\n"); + if (process.pid == getpid()) assert(process.ppid == getppid()); + else if (process.pid == child) assert(process.ppid == getpid()); + else assert(0); + assert(process.cputime < 100); + assert(process.starttime == now || process.starttime == now - 1); + count++; } + assert(count == 2); close_process_iterator(&it); return 0; } + +int main() +{ + printf("Current PID %d\n", getpid()); + test_single_process(); + test_multiple_process(); + return 0; +}
\ No newline at end of file |