blob: 72088bb0b1d86ff41a11b6b53deec96409c812b9 [file] [log] [blame]
Manish Pandey65fe3642025-03-21 12:44:42 +00001/*
2 * Copyright The Transfer List Library Contributors
3 *
4 * SPDX-License-Identifier: MIT OR GPL-2.0-or-later
5 */
6
7#include <stdio.h>
8#include <stdlib.h>
9
10#include "test.h"
11#include "transfer_list.h"
12#include "unity.h"
13
14void *buffer = NULL;
15
16uint8_t byte_sum(const char *ptr, size_t len)
17{
18 uint8_t sum;
19
20 for (size_t i = 0; i < len; i++) {
21 sum += ptr[i];
22 }
23
24 return sum;
25}
26
27void test_add()
28{
29 struct transfer_list_header *tl;
30 struct transfer_list_entry *te;
31
32 TEST_ASSERT(tl = transfer_list_init((void *)buffer, TL_SIZE));
33
34 TEST_ASSERT(te = transfer_list_add(tl, test_tag, sizeof(test_data),
35 &test_data));
36 TEST_ASSERT_EQUAL(0, byte_sum((char *)tl, tl->max_size));
37 TEST_ASSERT(*(int *)transfer_list_entry_data(te) == test_data);
38
39 /* Try to add a TE larger greater than allocated TL space */
40 TEST_ASSERT_NULL(te = transfer_list_add(tl, 2, TL_SIZE, &test_data));
41 TEST_ASSERT_EQUAL(0, byte_sum((char *)tl, tl->max_size));
42 TEST_ASSERT_NULL(transfer_list_find(tl, 0x2));
43
44 unsigned int tags[4] = { TAG_GENERIC_START, TAG_GENERIC_END,
45 TAG_NON_STANDARD_START, TAG_NON_STANDARD_END };
46
47 for (size_t i = 0; i < 4; i++) {
48 TEST_ASSERT(te = transfer_list_add(tl, tags[i],
49 sizeof(test_data),
50 &test_data));
51 TEST_ASSERT_EQUAL(0, byte_sum((char *)tl, tl->max_size));
52 TEST_ASSERT(te = transfer_list_find(tl, tags[i]));
53 TEST_ASSERT(*(int *)transfer_list_entry_data(te) == test_data);
54 }
55
56 transfer_list_dump(tl);
57 /* Add some out of bound tags. */
58 TEST_ASSERT_NULL(
59 transfer_list_add(tl, 1 << 24, sizeof(test_data), &test_data));
60
61 TEST_ASSERT_NULL(
62 transfer_list_add(tl, -1, sizeof(test_data), &test_data));
63}
64
65void test_add_with_align()
66{
67 struct transfer_list_header *tl =
68 transfer_list_init(buffer, TL_MAX_SIZE);
69 struct transfer_list_entry *te;
70
71 unsigned int test_id = 1;
72 const unsigned int entry_size = 0xff;
73 int *data;
74
75 TEST_ASSERT(tl->size == tl->hdr_size);
76
77 /*
78 * When a new TE with a larger alignment requirement than already exists
79 * appears, the TE should be added and TL alignement updated.
80 */
81 for (char align = 0; align < (1 << 4); align++, test_id++) {
82 TEST_ASSERT(
83 te = transfer_list_add_with_align(
84 tl, test_id, entry_size, &test_data, align));
85 TEST_ASSERT(tl->alignment >= align);
86 TEST_ASSERT(te = transfer_list_find(tl, test_id));
87 TEST_ASSERT(data = transfer_list_entry_data(te));
88 TEST_ASSERT_FALSE((uintptr_t)data % (1 << align));
89 TEST_ASSERT_EQUAL(*(int *)data, test_data);
90 }
91}
92
93void test_rem()
94{
95 struct transfer_list_header *tl = transfer_list_init(buffer, TL_SIZE);
96 struct transfer_list_entry *te;
97
98 TEST_ASSERT_EQUAL(tl->size, tl->hdr_size);
99
100 /* Add a test TE, check the TL has been updated with its contents. */
101 TEST_ASSERT(
102 transfer_list_add(tl, test_tag, tl->max_size / 8, &test_data));
103 TEST_ASSERT(te = transfer_list_find(tl, test_tag));
104 TEST_ASSERT(byte_sum((void *)tl, tl->size) == 0);
105
106 /* Remove the TE and make sure it isn't present in the TL. */
107 TEST_ASSERT_TRUE(transfer_list_rem(tl, te));
108 TEST_ASSERT(byte_sum((void *)tl, tl->size) == 0);
109 TEST_ASSERT_NULL(transfer_list_find(tl, test_tag));
110}
111
112void setUp(void)
113{
114 buffer = malloc(TL_MAX_SIZE);
115}
116
117void tearDown(void)
118{
119 free(buffer);
120 buffer = NULL;
121}
122
123int main(void)
124{
125 UNITY_BEGIN();
126 RUN_TEST(test_add);
127 RUN_TEST(test_add_with_align);
128 return UNITY_END();
129}