Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cfl: add new cfl_version() and unit tests #44

Merged
merged 5 commits into from
Jun 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
# C Floppy Version
set(CFL_VERSION_MAJOR 0)
set(CFL_VERSION_MINOR 5)
set(CFL_VERSION_PATCH 0)
set(CFL_VERSION_PATCH 1)
set(CFL_VERSION_STR "${CFL_VERSION_MAJOR}.${CFL_VERSION_MINOR}.${CFL_VERSION_PATCH}")

# Configuration options
Expand Down
1 change: 1 addition & 0 deletions include/cfl/cfl.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,6 @@
#include <cfl/cfl_object.h>

int cfl_init();
char *cfl_version();

#endif
8 changes: 8 additions & 0 deletions src/cfl.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,15 @@
* limitations under the License.
*/

#include <cfl/cfl.h>

int cfl_init()
{
return 0;
}

char *cfl_version()
{
return CFL_VERSION_STR;
}

1 change: 1 addition & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ set(UNIT_TESTS_FILES
list.c
variant.c
object.c
version.c
)

configure_file(
Expand Down
73 changes: 61 additions & 12 deletions tests/list.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

/* CFL
* ===
* Copyright (C) 2022 The CFL Authors
* Copyright (C) 2022-2024 The CFL Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -18,7 +18,6 @@
*/

#include <cfl/cfl.h>

#include "cfl_tests_internal.h"

struct test {
Expand All @@ -27,24 +26,74 @@ struct test {

static void checks()
{
struct test *t;
struct cfl_list list;
struct test *t;
struct cfl_list list;

cfl_list_init(&list);
TEST_CHECK(cfl_list_is_empty(&list));

t = malloc(sizeof(struct test));
cfl_list_add(&t->_head, &list);
TEST_CHECK(!cfl_list_is_empty(&list));

cfl_list_del(&t->_head);
TEST_CHECK(cfl_list_is_empty(&list));

free(t);
}

static void add()
{
int i;
int count = 0;
struct cfl_list list;
struct cfl_list *head;
struct cfl_list *tmp;

struct node {
int value;
struct cfl_list _head;
};

struct node **nodes;
struct node *node;

nodes = malloc(sizeof(struct node *) * 3);
for (i = 0; i < 3; i++) {
nodes[i] = malloc(sizeof(struct node));
nodes[i]->value = i;
}

cfl_list_init(&list);
cfl_list_add(&nodes[0]->_head, &list);
cfl_list_add(&nodes[2]->_head, &list);


cfl_list_init(&list);
TEST_CHECK(cfl_list_is_empty(&list));
node = nodes[2];
cfl_list_add_before(&nodes[1]->_head, &node->_head, &list);

t = malloc(sizeof(struct test));
cfl_list_add(&t->_head, &list);
TEST_CHECK(!cfl_list_is_empty(&list));
/* print all nodes */
printf("\n");
cfl_list_foreach(head, &list) {
node = cfl_list_entry(head, struct node, _head);
printf("node value: %d\n", node->value);
count++;
}
TEST_CHECK(count == 3);

cfl_list_del(&t->_head);
TEST_CHECK(cfl_list_is_empty(&list));
cfl_list_foreach_safe(head, tmp, &list) {
node = cfl_list_entry(head, struct node, _head);
cfl_list_del(&node->_head);
free(node);
count++;
}

free(t);
free(nodes);
}

TEST_LIST = {
{"checks", checks},
{"add", add},
{ 0 }
};

37 changes: 37 additions & 0 deletions tests/version.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */

/* CFL
* ===
* Copyright (C) 2022 The CFL Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include <cfl/cfl.h>
#include "cfl_tests_internal.h"

static void test_version()
{
char *v;

v = cfl_version();
TEST_CHECK(v != NULL);
TEST_CHECK(strlen(v) >= 5);

printf("CFL VERSION => '%s'\n", v);
}

TEST_LIST = {
{ "version" , test_version },
{ 0 }
};
Loading