Skip to content

Commit

Permalink
test: add tests to dstring
Browse files Browse the repository at this point in the history
  • Loading branch information
Silva97 committed Jan 16, 2025
1 parent ee07627 commit 5fe2b0e
Show file tree
Hide file tree
Showing 8 changed files with 269 additions and 88 deletions.
6 changes: 2 additions & 4 deletions tests/include/utils.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
#pragma once

#define UTILS_BASEPATH "tests/utils"

#define FILE_BASIC_MODULE "tests/utils/basic.mya"

#define UTILS_BASEPATH "tests/utils"
#define FILE_BASIC_MODULE UTILS_BASEPATH "/basic.mya"
#define FILE_BIG_PATH \
UTILS_BASEPATH \
"/filepath_with_a_big_name_to_check_if_its_not_breaking_something_because_of_its_size_so_we_ensure_thats_all_okay_" \
Expand Down
54 changes: 50 additions & 4 deletions tests/unit/types/cqueue.c → tests/unit/types/test_cqueue.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,18 @@ test_cqueue_init(void)

memset(queue.data, 0, sizeof(int) * 500);

free(queue.data);
cqueue_close(&queue);
}

void
test_cqueue_close(void)
{
cqueue_t queue;

cqueue_init(&queue, 5);
cqueue_close(&queue);

TEST_ASSERT_NULL(queue.data);
}

void
Expand All @@ -41,7 +52,7 @@ test_cqueue_add(void)

TEST_ASSERT_EQUAL(ERR_MAX_SIZE_EXCEEDED, cqueue_add(&queue, 6));

free(queue.data);
cqueue_close(&queue);
}

void
Expand All @@ -68,7 +79,7 @@ test_cqueue_get(void)
TEST_ASSERT_EQUAL(ERR_EMPTY, cqueue_get(&queue, &value));
TEST_ASSERT_EQUAL(3, value); // Expects to not change the value.

free(queue.data);
cqueue_close(&queue);
}

void
Expand Down Expand Up @@ -99,7 +110,7 @@ test_cqueue_lookup(void)
TEST_ASSERT_EQUAL(ERR_OK, cqueue_get(&queue, &value));
TEST_ASSERT_EQUAL(1, value);

free(queue.data);
cqueue_close(&queue);
}

void
Expand Down Expand Up @@ -134,6 +145,8 @@ test_cqueue_isfull(void)

TEST_ASSERT_EQUAL(ERR_OK, cqueue_get(&queue, &value));
TEST_ASSERT_FALSE(cqueue_isfull(&queue));

cqueue_close(&queue);
}

void
Expand Down Expand Up @@ -209,4 +222,37 @@ test_cqueue_circular_case(void)
TEST_ASSERT_EQUAL(0, queue._last);

TEST_ASSERT_TRUE(cqueue_isempty(&queue));

cqueue_close(&queue);
}

/////

int
main(void)
{
UNITY_BEGIN();

RUN_TEST(test_cqueue_init);
RUN_TEST(test_cqueue_close);
RUN_TEST(test_cqueue_add);
RUN_TEST(test_cqueue_get);
RUN_TEST(test_cqueue_lookup);
RUN_TEST(test_cqueue_isempty);
RUN_TEST(test_cqueue_isfull);
RUN_TEST(test_cqueue_circular_case);

return UNITY_END();
}

void
setUp(void)
{
//
}

void
tearDown(void)
{
//
}
118 changes: 118 additions & 0 deletions tests/unit/types/test_dstring.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
#include <string.h>

#include "dstring.h"
#include "unity.h"

void
test_dstring_init(void)
{
dstring_t string;

dstring_init(&string, 21);

TEST_ASSERT_EQUAL(0, string.length);
TEST_ASSERT_EQUAL(21, string._buffer_size);
TEST_ASSERT_NOT_NULL(string.data);

dstring_close(&string);
}

void
test_dstring_close(void)
{
dstring_t string;

dstring_init(&string, 21);

dstring_close(&string);

TEST_ASSERT_NULL(string.data);
}

void
test_dstring_putchar(void)
{
dstring_t string;

dstring_init(&string, 25);

for (int i = 0; i < 999; i++) {
dstring_putchar(&string, 'x');
}

TEST_ASSERT_EACH_EQUAL_CHAR('x', string.data, 999);
TEST_ASSERT_GREATER_THAN(999, string._buffer_size);

dstring_close(&string);
}

void
test_dstring_concat(void)
{
dstring_t string;

dstring_init(&string, 25);

dstring_concat(&string, "abc");
dstring_concat(&string, "def");
dstring_concat(&string, "ghij");

TEST_ASSERT_EQUAL_STRING("abcdefghij", string.data);

for (int i = 0; i < 300; i++) {
dstring_concat(&string, "xxx");
}

TEST_ASSERT_EACH_EQUAL_CHAR('x', string.data + 10, 900);
TEST_ASSERT_GREATER_THAN(910, string._buffer_size);

dstring_close(&string);
}

void
test_dstring_copy(void)
{
dstring_t string;

dstring_init(&string, 5);

char* expected = "Any string bigger than the allocated buffer of the string.";
size_t expected_length = strlen(expected);

dstring_copy(&string, expected);

TEST_ASSERT_EQUAL_STRING(expected, string.data);
TEST_ASSERT_EQUAL(expected_length, string.length);

TEST_ASSERT_GREATER_THAN(expected_length, string._buffer_size);

dstring_close(&string);
}

//////

int
main(void)
{
UNITY_BEGIN();

RUN_TEST(test_dstring_init);
RUN_TEST(test_dstring_close);
RUN_TEST(test_dstring_putchar);
RUN_TEST(test_dstring_concat);
RUN_TEST(test_dstring_copy);

return UNITY_END();
}

void
setUp(void)
{
//
}

void
tearDown(void)
{
//
}
43 changes: 36 additions & 7 deletions tests/unit/types/module.c → tests/unit/types/test_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,18 @@ test_module_init(void)
module_close(&module);
}

void
test_module_close(void)
{
module_t module;

TEST_ASSERT_EQUAL(ERR_OK, module_init(&module, FILE_BASIC_MODULE));
module_close(&module);

TEST_ASSERT_NULL(module.file);
TEST_ASSERT_NULL(module._queue.data);
}

void
test_module_init_with_filepath_bigger_than_maximum_expects_to_truncate(void)
{
Expand Down Expand Up @@ -93,14 +105,31 @@ test_module_lookup(void)
module_close(&module);
}

void
test_module_close(void)
/////

int
main(void)
{
module_t module;
UNITY_BEGIN();

TEST_ASSERT_EQUAL(ERR_OK, module_init(&module, FILE_BASIC_MODULE));
module_close(&module);
RUN_TEST(test_module_init);
RUN_TEST(test_module_close);
RUN_TEST(test_module_init_with_filepath_bigger_than_maximum_expects_to_truncate);
RUN_TEST(test_module_add_token);
RUN_TEST(test_module_getc);
RUN_TEST(test_module_lookup);

TEST_ASSERT_NULL(module.file);
TEST_ASSERT_NULL(module._queue.data);
return UNITY_END();
}

void
setUp(void)
{
//
}

void
tearDown(void)
{
//
}
56 changes: 56 additions & 0 deletions tests/unit/types/test_token.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#include <string.h>

#include "token.h"
#include "unity.h"

void
test_token_init(void)
{
token_t token;
token_init(&token, "any_token", TK_KEYWORD, 2, 3);

TEST_ASSERT_EQUAL(TK_KEYWORD, token.type);
TEST_ASSERT_EQUAL(2, token.line);
TEST_ASSERT_EQUAL(3, token.column);
TEST_ASSERT_EQUAL_STRING("any_token", token.lexeme.data);
}

void
test_token_init_with_a_very_big_lexeme(void)
{
token_t token;
static char big[1001];

memset(big, 'x', 1000);
big[1000] = '\0';

token_init(&token, big, TK_STRING, 2, 3);

TEST_ASSERT_EQUAL_INT(1000, token.lexeme.length);
TEST_ASSERT_EQUAL_STRING(big, token.lexeme.data);
}

/////

int
main(void)
{
UNITY_BEGIN();

RUN_TEST(test_token_init);
RUN_TEST(test_token_init_with_a_very_big_lexeme);

return UNITY_END();
}

void
setUp(void)
{
//
}

void
tearDown(void)
{
//
}
42 changes: 0 additions & 42 deletions tests/unit/types/test_types.c

This file was deleted.

Loading

0 comments on commit 5fe2b0e

Please sign in to comment.