Skip to content

Commit 9613e11

Browse files
committed
Add vt_list test suite.
1 parent 26f964e commit 9613e11

File tree

5 files changed

+75
-36
lines changed

5 files changed

+75
-36
lines changed

cTools/libs/containers/tests/CMakeLists.txt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@
2020
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2121
# SOFTWARE.
2222

23+
add_vipon_test(
24+
NAME vt_list_test
25+
SOURCES vt_list_test.c
26+
LINK_LIBS vt_containers mem comdef
27+
)
28+
2329
add_vipon_test(
2430
NAME vt_vector_test
2531
SOURCES vt_vector_test.c
@@ -37,4 +43,3 @@ add_vipon_test(
3743
SOURCES vt_stack_test.c
3844
LINK_LIBS vt_containers mem comdef
3945
)
40-

cTools/libs/containers/vt_list.c

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,11 @@
2525
#include "mem.h"
2626
#include "vt_list.h"
2727

28-
vt_list_t *
29-
vt_list_create(size_t data_size)
28+
void
29+
vt_list_init(vt_list_t *l, size_t elem_size)
3030
{
31-
vt_list_t *l = Malloc(sizeof(vt_list_t));
32-
if (l == NULL)
33-
return NULL;
34-
35-
l->size = data_size;
31+
l->elem_size = elem_size;
3632
l->first = NULL;
37-
return l;
3833
}
3934

4035
static void
@@ -52,16 +47,15 @@ vt_list_elems_free(vt_list_elem_t *cur)
5247
}
5348

5449
void
55-
vt_list_free(vt_list_t *l)
50+
vt_list_fini(vt_list_t *l)
5651
{
5752
if (l == NULL)
5853
return;
5954

60-
l->size = (size_t)-1;
55+
l->elem_size = (size_t)-1;
6156
vt_list_elems_free(l->first);
6257

6358
l->first = NULL;
64-
free(l);
6559
}
6660

6761
static vt_list_elem_t *
@@ -71,8 +65,8 @@ create_list_elem(const vt_list_t *l, const void *data)
7165
if (new_elem == NULL)
7266
return NULL;
7367

74-
new_elem->data = Malloc(l->size);
75-
directCopyBytes(data, new_elem->data, l->size);
68+
new_elem->data = Malloc(l->elem_size);
69+
directCopyBytes(data, new_elem->data, l->elem_size);
7670
new_elem->next = NULL;
7771

7872
return new_elem;

cTools/libs/containers/vt_list.h

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/***
22
* MIT License
33
*
4-
* Copyright (c) 2024 Konychev Valera
4+
* Copyright (c) 2024-2026 Konychev Valera
55
*
66
* Permission is hereby granted, free of charge, to any person obtaining a copy
77
* of this software and associated documentation files (the "Software"), to deal
@@ -35,22 +35,62 @@ typedef struct vt_list_elem_t {
3535
} vt_list_elem_t;
3636

3737
typedef struct vt_list_t {
38-
size_t size;
38+
size_t elem_size;
3939
vt_list_elem_t *first;
4040
} vt_list_t ;
4141

42-
EXPORT_FUNC vt_list_t *
43-
vt_list_create(size_t data_size);
42+
/***
43+
* @brief
44+
* @param[in] l Pointer to the vector.
45+
* @param[in] i Iterator pointer.
46+
*/
47+
#define VT_LIST_FOR_EACH(lp, i) \
48+
for (i = (lp)->first; i != NULL; i = i->next)
49+
50+
/**
51+
* \brief Initiates list.
52+
*
53+
* \param[in] l Point to list needed to be initialized.
54+
* \param[in] elem_size Size of type should be stored in the list.
55+
*/
56+
EXPORT_FUNC void
57+
vt_list_init(vt_list_t *l, size_t elem_size);
4458

59+
/**
60+
* \brief Finalizes list at the pointer \param l.
61+
*
62+
* \param[in] l Point to list needed to be finalized.
63+
*/
4564
EXPORT_FUNC void
46-
vt_list_free(vt_list_t *l);
65+
vt_list_fini(vt_list_t *l);
4766

67+
/**
68+
* \brief Add \param data at the begin of the list \param l.
69+
*
70+
* \param[in] l Point to list.
71+
* \param[in] data Point to data need to be added at the begin
72+
*
73+
* \return 0 if success. -1 if fail.
74+
*/
4875
EXPORT_FUNC int
4976
vt_list_insert_at_begin(vt_list_t *l, const void *data);
5077

78+
/**
79+
* \brief Append \param data to the list \param l.
80+
*
81+
* \param[in] l Point to list.
82+
* \param[in] data Point to data need to be appended
83+
*
84+
* \return 0 if success. -1 if fail.
85+
*/
5186
EXPORT_FUNC int
5287
vt_list_append(vt_list_t *l, const void *data);
5388

89+
/**
90+
* \brief Reverse list \param l.
91+
*
92+
* \param[in] l Point to the list.
93+
*/
5494
EXPORT_FUNC void
5595
vt_list_reverse(vt_list_t *l);
5696

cTools/libs/containers/vt_vector.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ EXPORT_FUNC int
7171
vt_vector_init(vt_vector_t *v, size_t capacity, size_t elem_size);
7272

7373
/**
74-
* \brief Finalizes vector sotred at the pointer \param src.
74+
* \brief Finalizes vector sotred at the pointer \param v.
7575
*
7676
* \param[in] v Point to vector needed to be finalized.
7777
*/

cTools/libs/test/test.h

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/***
22
* MIT License
33
*
4-
* Copyright (c) 2021 Konychev Valera
4+
* Copyright (c) 2021-2026 Konychev Valera
55
*
66
* Permission is hereby granted, free of charge, to any person obtaining a copy
77
* of this software and associated documentation files (the "Software"), to deal
@@ -31,29 +31,29 @@
3131
#ifndef __cplusplus
3232
#include <stdlib.h>
3333

34-
#define EXPECT_FUNC_EQ(func, res) \
35-
if ((func) != (res)) { \
34+
#define EXPECT_FUNC_EQ(func, result) \
35+
if ((func) != (result)) { \
3636
VT_ERROR(STRINGIZE(func) " fail"); \
3737
exit(EXIT_FAILURE); \
3838
}
3939

40-
#define EXPECT_VAL_EQ(val, res, format) \
41-
if ((val) != (res)) { \
42-
VT_ERROR("expect: " format " result: " format, (val), (res));\
43-
exit(EXIT_FAILURE); \
40+
#define EXPECT_VAL_EQ(expect, result, format) \
41+
if ((expect) != (result)) { \
42+
VT_ERROR("expect: " format " result: " format, (expect), (result));\
43+
exit(EXIT_FAILURE); \
4444
}
4545

46-
#define EXPECT_VAL_NOT_EQ(val, res, format) \
47-
if ((val) == (res)) { \
48-
VT_ERROR(format); \
49-
exit(EXIT_FAILURE); \
46+
#define EXPECT_VAL_NOT_EQ(expect, result, format) \
47+
if ((expect) == (result)) { \
48+
VT_ERROR(format); \
49+
exit(EXIT_FAILURE); \
5050
}
5151

52-
#define EXPECT_BYTE_EQ(val, res) EXPECT_VAL_EQ(val, res, "%.2x")
53-
#define EXPECT_INT_EQ(val, res) EXPECT_VAL_EQ(val, res, "%d")
54-
#define EXPECT_SIZET_EQ(val, res) EXPECT_VAL_EQ(val, res, "%zu")
55-
#define EXPECT_BOOL_EQ(val, res) EXPECT_VAL_EQ(val, res, "%d")
56-
#define EXPECT_POINTER_EQ(val, res) EXPECT_VAL_EQ(val, res, "%p")
52+
#define EXPECT_BYTE_EQ(expect, result) EXPECT_VAL_EQ(expect, result, "%.2x")
53+
#define EXPECT_INT_EQ(expect, result) EXPECT_VAL_EQ(expect, result, "%d")
54+
#define EXPECT_SIZET_EQ(expect, result) EXPECT_VAL_EQ(expect, result, "%zu")
55+
#define EXPECT_BOOL_EQ(expect, result) EXPECT_VAL_EQ(expect, result, "%d")
56+
#define EXPECT_POINTER_EQ(expect, result) EXPECT_VAL_EQ(expect, result, "%p")
5757

5858
#define EXPECT_STR_EQ(str0, str1) \
5959
if (strcmp(str0, str1)) { \

0 commit comments

Comments
 (0)