Skip to content

Commit

Permalink
Merge pull request #3 from Guitarbum722/issue-2-unit-tests
Browse files Browse the repository at this point in the history
add unit tests
  • Loading branch information
Guitarbum722 authored Feb 13, 2019
2 parents 02f54d5 + 06d3945 commit 5462849
Show file tree
Hide file tree
Showing 8 changed files with 3,175 additions and 19 deletions.
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,9 @@ clean:

.PHONY: test
test: clean

.PHONY: test
test: clean
$(CC) -o tests/tests tests/tests.c qualified.c tests/unity/unity.c
tests/tests
rm -f tests/tests
19 changes: 0 additions & 19 deletions main.c

This file was deleted.

File renamed without changes.
4 changes: 4 additions & 0 deletions qual.h → qualified.h
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@

#ifndef _QUALIFIED_H
#define _QUALIFIED_H

char *strqsep(char **strp, const char *delim, const char *qual);
#endif
111 changes: 111 additions & 0 deletions tests/tests.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>

#include "../qualified.h"
#include "unity/unity.h"

struct table_test {
char* name;
char* expected;
};

/**
* test_strqsep validates that the proper tokens
* are returned.
*/
void
test_strqsep_comma_no_qual(void)
{
static struct table_test table_tests[] = {
{ .name = "first", .expected = "Don" },
{ .name = "second", .expected = "K" },
{ .name = "third", .expected = "Moby" },
{ .name = "fourth", .expected = "don.moby@nothing.com" },

};
char *got, *input, *original;
original = input = strdup("Don,K,Moby,don.moby@nothing.com");

for (int i = 0; i < sizeof(table_tests) / sizeof(struct table_test); i++) {
got = strqsep(&input, ",", "");
TEST_ASSERT_EQUAL_STRING(table_tests[i].expected, got);
}

free(original);
}

void
test_strqsep_comma_with_qual(void)
{
static struct table_test table_tests[] = {
{ .name = "first", .expected = "\"Don,,,\"" },
{ .name = "second", .expected = "K" },
{ .name = "third", .expected = "\"Moby, M.D.\"" },
{ .name = "fourth", .expected = "don.moby@nothing.com" },

};
char *got, *input, *original;
original = input = strdup("\"Don,,,\",K,\"Moby, M.D.\",don.moby@nothing.com");

for (int i = 0; i < sizeof(table_tests) / sizeof(struct table_test); i++) {
got = strqsep(&input, ",", "\"");
TEST_ASSERT_EQUAL_STRING(table_tests[i].expected, got);
}

free(original);
}

void
test_strqsep_pipe_delim_single_quote_qual(void)
{
static struct table_test table_tests[] = {
{ .name = "first", .expected = "Johnny" },
{ .name = "second", .expected = "',|, '" },
{ .name = "third", .expected = "Topics" },
{ .name = "fourth", .expected = "'Look|at|me'" },
};
char *got, *input, *original;
original = input = strdup("Johnny|',|, '|Topics|'Look|at|me'");

for (int i = 0; i < sizeof(table_tests) / sizeof(struct table_test); i++) {
got = strqsep(&input, "|", "'");
TEST_ASSERT_EQUAL_STRING(table_tests[i].expected, got);
}

free(original);
}

void
test_strqsep_null_input(void)
{
char *got, *input, *original;
original = input = strdup("");

got = strqsep(&input, ",", "");
TEST_ASSERT_EQUAL_STRING(NULL, got);

free(original);
}

void
test_strqsep_empty_input(void)
{
char *got, *field;

got = strqsep(NULL, "", "");
TEST_ASSERT_EQUAL_STRING(NULL, got);

}

int main(void) {
UNITY_BEGIN();

RUN_TEST(test_strqsep_comma_no_qual);
RUN_TEST(test_strqsep_comma_with_qual);
RUN_TEST(test_strqsep_pipe_delim_single_quote_qual);
RUN_TEST(test_strqsep_null_input);
RUN_TEST(test_strqsep_empty_input);

return UNITY_END();
}
Loading

0 comments on commit 5462849

Please sign in to comment.