This repository has been archived on 2015-04-30. You can view files and clone it, but cannot push or open issues or pull requests.
stream/c/libjshtest.c
2015-04-30 17:08:08 +00:00

167 lines
3.8 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "jsh.h"
#include "minunit.h"
int tests_run = 0;
void usage(const char *program)
{
fprintf(stderr, "Usage: %s", program);
}
// return 0 if files are the same, else -1
int fd_cmp(FILE *expected, FILE *actual)
{
fseek(expected, 0, SEEK_SET);
fseek(actual, 0, SEEK_SET);
char e;
while ((e = fgetc(expected)) != EOF) {
if (e != fgetc(actual)) {
return -1;
}
}
return 0;
}
static char *test_new_stream(void)
{
struct stream *s = jsh_new_stream("stdout", stdout, 10);
mu_assert(
"test_new_stream: Stream returned null pointer",
s != NULL);
mu_assert(
"test_new_stream: Did not set name",
!strncmp(s->name, "stdout", 6));
mu_assert(
"test_new_stream: Did not set out file descriptor",
s->out == stdout);
mu_assert(
"test_new_stream: counter != 0",
s->counter == 0);
mu_assert(
"test_new_stream: root != s",
s->root == s);
mu_assert(
"test_new_stream: data == NULL",
s->data != NULL);
mu_assert(
"test_new_stream: next != NULL",
s->next == NULL);
mu_assert(
"test_new_stream: first_child != NULL",
s->first_child == NULL);
mu_assert(
"test_new_stream: first != true",
s->first == true);
jsh_close_stream(s);
return 0;
}
static char *test_cannot_make_new_stream(void)
{
mu_assert(
"test_cannot_make_new_stream: Should not be able to make new stream",
jsh_new_stream(NULL, stdout, 10) == NULL);
mu_assert(
"test_cannot_make_new_stream: Should not be able to make new stream",
jsh_new_stream("test", NULL, 10) == NULL);
mu_assert(
"test_cannot_make_new_stream: Should not be able to make new stream",
jsh_new_stream("test", stdout, 0) == NULL);
return 0;
}
static char *test_output(void)
{
FILE *stdtest = tmpfile();
FILE *stdexpected = tmpfile();
fprintf(stdexpected, "[{\"test\": [1, 2, 3]}]");
struct stream *s = jsh_new_stream("stdtest", stdtest, 10);
jsh_start_stream(s);
jsh_output(s, "test", json_integer(1));
jsh_output(s, "test", json_integer(2));
jsh_output(s, "test", json_integer(3));
jsh_end_stream(s);
mu_assert(
"test_output: Did not output correctly",
fd_cmp(stdtest, stdexpected) == 0);
jsh_close_stream(s);
return 0;
}
static char *test_buf_size(void)
{
FILE *stdtest = tmpfile();
FILE *stdexpected = tmpfile();
fprintf(stdexpected, "[{\"test\": [1, 2]}, {\"test\": [3]}]");
struct stream *s = jsh_new_stream("stdout", stdtest, 2);
jsh_start_stream(s);
jsh_output(s, "test", json_integer(1));
jsh_output(s, "test", json_integer(2));
jsh_output(s, "test", json_integer(3));
jsh_end_stream(s);
mu_assert(
"test_buf_size: Did not output correctly",
fd_cmp(stdtest, stdexpected) == 0);
jsh_close_stream(s);
return 0;
}
static char *test_substream(void)
{
FILE *stdtest = tmpfile();
FILE *stdexpected = tmpfile();
fprintf(stdexpected, "[{\"s2\": [{\"test2\": [1, 2]}]}, {\"s2\": [{\"test2\": [3]}]}]");
struct stream *s = jsh_new_stream("stdout", stdtest, 2);
struct stream *s2 = jsh_new_substream(s, "s2");
jsh_start_stream(s);
jsh_output(s2, "test2", json_integer(1));
jsh_output(s2, "test2", json_integer(2));
jsh_output(s2, "test2", json_integer(3));
jsh_end_stream(s);
mu_assert(
"test_substream: Output not the same",
fd_cmp(stdtest, stdexpected) == 0);
jsh_close_stream(s);
return 0;
}
static char *all_tests()
{
mu_run_test(test_new_stream);
mu_run_test(test_buf_size);
mu_run_test(test_output);
mu_run_test(test_cannot_make_new_stream);
mu_run_test(test_substream);
return 0;
}
int main(int argc, const char *argv[])
{
if (argc > 1) {
usage(argv[0]);
exit(1);
}
printf("Running jsh tests...\n");
char *result = all_tests();
if (result != 0) {
printf("%s\n", result);
} else {
printf("ALL TESTS PASSED.\n");
}
printf("Tests run: %d\n", tests_run);
return result != 0;
return 0;
}