Branch data Line data Source code
1 : : #include "test_libmem_all.h"
2 : :
3 : : /**
4 : : * @brief Verify empty descriptor state produced by m_create
5 : : *
6 : : * Creates one default data descriptor, one byte string descriptor, and
7 : : * one uint32_t string descriptor. The checks make sure that m_create
8 : : * sets the element width, mode flag, empty logical length, empty cached
9 : : * string length, zero reserve, and NULL data pointer consistently for
10 : : * every initial mode used by the public macro
11 : : *
12 : : * @return Return describing success or failure
13 : : */
14 : 1 : static Return test_libmem_0013_1(void)
15 : : {
16 : 1 : INITTEST;
17 : :
18 : : /* Cover the default data mode and explicit string mode for byte and wide elements */
19 : 1 : m_create(char,data_buffer);
20 : 1 : m_create(char,string_buffer,MEMORY_STRING);
21 : 1 : m_create(uint32_t,wide_string,MEMORY_STRING);
22 : :
23 : : /* Validate the empty data descriptor created by the default macro form */
24 : 1 : ASSERT(data_buffer->single_element_size == sizeof(char));
25 : 1 : ASSERT(data_buffer->actually_allocated_bytes == 0);
26 : 1 : ASSERT(data_buffer->length == 0);
27 : 1 : ASSERT(data_buffer->string_length == 0);
28 : 1 : ASSERT(data_buffer->is_string == false);
29 : 1 : ASSERT(data_buffer->data == NULL);
30 : :
31 : : /* Validate the empty byte string descriptor created with MEMORY_STRING */
32 : 1 : ASSERT(string_buffer->single_element_size == sizeof(char));
33 : 1 : ASSERT(string_buffer->actually_allocated_bytes == 0);
34 : 1 : ASSERT(string_buffer->length == 0);
35 : 1 : ASSERT(string_buffer->string_length == 0);
36 : 1 : ASSERT(string_buffer->is_string == true);
37 : 1 : ASSERT(string_buffer->data == NULL);
38 : :
39 : : /* Validate the empty wide string descriptor created with the same MEMORY_STRING flag */
40 : 1 : ASSERT(wide_string->single_element_size == sizeof(uint32_t));
41 : 1 : ASSERT(wide_string->actually_allocated_bytes == 0);
42 : 1 : ASSERT(wide_string->length == 0);
43 : 1 : ASSERT(wide_string->string_length == 0);
44 : 1 : ASSERT(wide_string->is_string == true);
45 : 1 : ASSERT(wide_string->data == NULL);
46 : :
47 : 1 : call(m_del(wide_string));
48 : 1 : call(m_del(string_buffer));
49 : 1 : call(m_del(data_buffer));
50 : :
51 : 1 : RETURN_STATUS;
52 : : }
53 : :
54 : : /**
55 : : * @brief Verify fixed-size append for a byte string descriptor
56 : : *
57 : : * Starts with an empty MEMORY_STRING char descriptor, appends the
58 : : * fixed-size "abc" literal including its terminator, and checks both
59 : : * string counters. The typed read-only view then confirms that the
60 : : * visible bytes and the trailing zero reached descriptor storage in
61 : : * the expected order
62 : : *
63 : : * @return Return describing success or failure
64 : : */
65 : 1 : static Return test_libmem_0013_2(void)
66 : : {
67 : 1 : INITTEST;
68 : :
69 : 1 : m_create(char,string_buffer,MEMORY_STRING);
70 : :
71 : : /* Fixed-string mode trusts that the last element in this array is the terminator */
72 : 1 : const char literal_suffix[] = "abc";
73 : :
74 : : /* Confirm the byte string starts empty before append */
75 : 1 : ASSERT(string_buffer->is_string == true);
76 : 1 : ASSERT(string_buffer->string_length == 0);
77 : 1 : ASSERT(string_buffer->length == 0);
78 : :
79 : : /* Append three visible characters plus the trailing terminator */
80 : 1 : ASSERT(SUCCESS == m_concat_fixed_string(string_buffer,sizeof(literal_suffix),literal_suffix));
81 : 1 : ASSERT(string_buffer->string_length == 3);
82 : 1 : ASSERT(string_buffer->length == 4);
83 : :
84 : : /* Read back the full fixed-string payload, including the terminator slot */
85 : 1 : const char *string_view = m_data_ro(char,string_buffer);
86 : 1 : ASSERT(string_view != NULL);
87 : :
88 : 1 : IF(string_view != NULL)
89 : : {
90 : 1 : ASSERT(string_view[0] == 'a');
91 : 1 : ASSERT(string_view[1] == 'b');
92 : 1 : ASSERT(string_view[2] == 'c');
93 : 1 : ASSERT(string_view[3] == '\0');
94 : : }
95 : :
96 : 1 : call(m_del(string_buffer));
97 : :
98 : 1 : RETURN_STATUS;
99 : : }
100 : :
101 : : /**
102 : : * @brief Verify fixed-size append for a uint32_t string descriptor
103 : : *
104 : : * Starts with an empty MEMORY_STRING uint32_t descriptor, appends a
105 : : * three-element fixed-size source whose last element is the wide
106 : : * terminator, and checks that string_length counts only visible
107 : : * elements while length includes the terminator slot. Reading the
108 : : * terminator as a full uint32_t covers the all-bytes-zero requirement
109 : : * for multi-byte string elements
110 : : *
111 : : * @return Return describing success or failure
112 : : */
113 : 1 : static Return test_libmem_0013_3(void)
114 : : {
115 : 1 : INITTEST;
116 : :
117 : 1 : m_create(uint32_t,wide_string,MEMORY_STRING);
118 : :
119 : : /* The last uint32_t element is the terminator trusted by fixed-string mode */
120 : 1 : const uint32_t wide_literal_suffix[] = {
121 : : UINT32_C(10),
122 : : UINT32_C(20),
123 : : UINT32_C(0)
124 : : };
125 : :
126 : : /* Confirm the wide string starts empty before append */
127 : 1 : ASSERT(wide_string->is_string == true);
128 : 1 : ASSERT(wide_string->string_length == 0);
129 : 1 : ASSERT(wide_string->length == 0);
130 : :
131 : : /* Append two visible uint32_t elements plus the wide terminator */
132 : 1 : ASSERT(SUCCESS == m_concat_fixed_string(wide_string,sizeof(wide_literal_suffix),wide_literal_suffix));
133 : 1 : ASSERT(wide_string->string_length == 2);
134 : 1 : ASSERT(wide_string->length == 3);
135 : :
136 : : /* Read back the full wide payload, including the all-zero terminator element */
137 : 1 : const uint32_t *wide_view = m_data_ro(uint32_t,wide_string);
138 : 1 : ASSERT(wide_view != NULL);
139 : :
140 : 1 : IF(wide_view != NULL)
141 : : {
142 : 1 : ASSERT(wide_view[0] == UINT32_C(10));
143 : 1 : ASSERT(wide_view[1] == UINT32_C(20));
144 : 1 : ASSERT(wide_view[2] == UINT32_C(0));
145 : : }
146 : :
147 : 1 : call(m_del(wide_string));
148 : :
149 : 1 : RETURN_STATUS;
150 : : }
151 : :
152 : : /**
153 : : * @brief Verify m_create initial modes and fixed-size string append behavior
154 : : *
155 : : * In plain terms, this suite checks two related things. First, m_create
156 : : * must build empty descriptors with the right mode, element size, length
157 : : * fields, reserve, and data pointer. Second, descriptors created in
158 : : * MEMORY_STRING mode must be usable immediately with
159 : : * m_concat_fixed_string for both ordinary byte strings and wider
160 : : * uint32_t string elements. The nested tests keep the creation contract
161 : : * separate from byte and wide fixed-string append behavior, so failures
162 : : * point at the broken layer instead of one large mixed scenario
163 : : *
164 : : * @return Return describing success or failure
165 : : */
166 : 1 : Return test_libmem_0013(void)
167 : : {
168 : 1 : INITTEST;
169 : :
170 : 1 : TEST(test_libmem_0013_1,"m_create initializes empty data and string descriptors");
171 : 1 : TEST(test_libmem_0013_2,"m_concat_fixed_string appends a byte string literal");
172 : 1 : TEST(test_libmem_0013_3,"m_concat_fixed_string appends a uint32_t fixed string");
173 : :
174 : 1 : RETURN_STATUS;
175 : : }
|